1/* $NetBSD: nouveau_subdev_ltcg_gf100.c,v 1.1.1.1 2014/08/06 12:36:31 riastradh Exp $ */
2
3/*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_ltcg_gf100.c,v 1.1.1.1 2014/08/06 12:36:31 riastradh Exp $");
29
30#include <subdev/fb.h>
31#include <subdev/timer.h>
32
33#include "gf100.h"
34
35static void
36gf100_ltcg_lts_isr(struct gf100_ltcg_priv *priv, int ltc, int lts)
37{
38 u32 base = 0x141000 + (ltc * 0x2000) + (lts * 0x400);
39 u32 stat = nv_rd32(priv, base + 0x020);
40
41 if (stat) {
42 nv_info(priv, "LTC%d_LTS%d: 0x%08x\n", ltc, lts, stat);
43 nv_wr32(priv, base + 0x020, stat);
44 }
45}
46
47static void
48gf100_ltcg_intr(struct nouveau_subdev *subdev)
49{
50 struct gf100_ltcg_priv *priv = (void *)subdev;
51 u32 mask;
52
53 mask = nv_rd32(priv, 0x00017c);
54 while (mask) {
55 u32 lts, ltc = __ffs(mask);
56 for (lts = 0; lts < priv->lts_nr; lts++)
57 gf100_ltcg_lts_isr(priv, ltc, lts);
58 mask &= ~(1 << ltc);
59 }
60
61 /* we do something horribly wrong and upset PMFB a lot, so mask off
62 * interrupts from it after the first one until it's fixed
63 */
64 nv_mask(priv, 0x000640, 0x02000000, 0x00000000);
65}
66
67int
68gf100_ltcg_tags_alloc(struct nouveau_ltcg *ltcg, u32 n,
69 struct nouveau_mm_node **pnode)
70{
71 struct gf100_ltcg_priv *priv = (struct gf100_ltcg_priv *)ltcg;
72 int ret;
73
74 ret = nouveau_mm_head(&priv->tags, 1, n, n, 1, pnode);
75 if (ret)
76 *pnode = NULL;
77
78 return ret;
79}
80
81void
82gf100_ltcg_tags_free(struct nouveau_ltcg *ltcg, struct nouveau_mm_node **pnode)
83{
84 struct gf100_ltcg_priv *priv = (struct gf100_ltcg_priv *)ltcg;
85
86 nouveau_mm_free(&priv->tags, pnode);
87}
88
89static void
90gf100_ltcg_tags_clear(struct nouveau_ltcg *ltcg, u32 first, u32 count)
91{
92 struct gf100_ltcg_priv *priv = (struct gf100_ltcg_priv *)ltcg;
93 u32 last = first + count - 1;
94 int p, i;
95
96 BUG_ON((first > last) || (last >= priv->num_tags));
97
98 nv_wr32(priv, 0x17e8cc, first);
99 nv_wr32(priv, 0x17e8d0, last);
100 nv_wr32(priv, 0x17e8c8, 0x4); /* trigger clear */
101
102 /* wait until it's finished with clearing */
103 for (p = 0; p < priv->ltc_nr; ++p) {
104 for (i = 0; i < priv->lts_nr; ++i)
105 nv_wait(priv, 0x1410c8 + p * 0x2000 + i * 0x400, ~0, 0);
106 }
107}
108
109/* TODO: Figure out tag memory details and drop the over-cautious allocation.
110 */
111int
112gf100_ltcg_init_tag_ram(struct nouveau_fb *pfb, struct gf100_ltcg_priv *priv)
113{
114 u32 tag_size, tag_margin, tag_align;
115 int ret;
116
117 /* tags for 1/4 of VRAM should be enough (8192/4 per GiB of VRAM) */
118 priv->num_tags = (pfb->ram->size >> 17) / 4;
119 if (priv->num_tags > (1 << 17))
120 priv->num_tags = 1 << 17; /* we have 17 bits in PTE */
121 priv->num_tags = (priv->num_tags + 63) & ~63; /* round up to 64 */
122
123 tag_align = priv->ltc_nr * 0x800;
124 tag_margin = (tag_align < 0x6000) ? 0x6000 : tag_align;
125
126 /* 4 part 4 sub: 0x2000 bytes for 56 tags */
127 /* 3 part 4 sub: 0x6000 bytes for 168 tags */
128 /*
129 * About 147 bytes per tag. Let's be safe and allocate x2, which makes
130 * 0x4980 bytes for 64 tags, and round up to 0x6000 bytes for 64 tags.
131 *
132 * For 4 GiB of memory we'll have 8192 tags which makes 3 MiB, < 0.1 %.
133 */
134 tag_size = (priv->num_tags / 64) * 0x6000 + tag_margin;
135 tag_size += tag_align;
136 tag_size = (tag_size + 0xfff) >> 12; /* round up */
137
138 ret = nouveau_mm_tail(&pfb->vram, 1, tag_size, tag_size, 1,
139 &priv->tag_ram);
140 if (ret) {
141 priv->num_tags = 0;
142 } else {
143 u64 tag_base = (priv->tag_ram->offset << 12) + tag_margin;
144
145 tag_base += tag_align - 1;
146 ret = do_div(tag_base, tag_align);
147
148 priv->tag_base = tag_base;
149 }
150 ret = nouveau_mm_init(&priv->tags, 0, priv->num_tags, 1);
151
152 return ret;
153}
154
155static int
156gf100_ltcg_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
157 struct nouveau_oclass *oclass, void *data, u32 size,
158 struct nouveau_object **pobject)
159{
160 struct gf100_ltcg_priv *priv;
161 struct nouveau_fb *pfb = nouveau_fb(parent);
162 u32 parts, mask;
163 int ret, i;
164
165 ret = nouveau_ltcg_create(parent, engine, oclass, &priv);
166 *pobject = nv_object(priv);
167 if (ret)
168 return ret;
169
170 parts = nv_rd32(priv, 0x022438);
171 mask = nv_rd32(priv, 0x022554);
172 for (i = 0; i < parts; i++) {
173 if (!(mask & (1 << i)))
174 priv->ltc_nr++;
175 }
176 priv->lts_nr = nv_rd32(priv, 0x17e8dc) >> 28;
177
178 ret = gf100_ltcg_init_tag_ram(pfb, priv);
179 if (ret)
180 return ret;
181
182 priv->base.tags_alloc = gf100_ltcg_tags_alloc;
183 priv->base.tags_free = gf100_ltcg_tags_free;
184 priv->base.tags_clear = gf100_ltcg_tags_clear;
185
186 nv_subdev(priv)->intr = gf100_ltcg_intr;
187 return 0;
188}
189
190void
191gf100_ltcg_dtor(struct nouveau_object *object)
192{
193 struct nouveau_ltcg *ltcg = (struct nouveau_ltcg *)object;
194 struct gf100_ltcg_priv *priv = (struct gf100_ltcg_priv *)ltcg;
195 struct nouveau_fb *pfb = nouveau_fb(ltcg->base.base.parent);
196
197 nouveau_mm_fini(&priv->tags);
198 nouveau_mm_free(&pfb->vram, &priv->tag_ram);
199
200 nouveau_ltcg_destroy(ltcg);
201}
202
203static int
204gf100_ltcg_init(struct nouveau_object *object)
205{
206 struct nouveau_ltcg *ltcg = (struct nouveau_ltcg *)object;
207 struct gf100_ltcg_priv *priv = (struct gf100_ltcg_priv *)ltcg;
208 int ret;
209
210 ret = nouveau_ltcg_init(ltcg);
211 if (ret)
212 return ret;
213
214 nv_mask(priv, 0x17e820, 0x00100000, 0x00000000); /* INTR_EN &= ~0x10 */
215 nv_wr32(priv, 0x17e8d8, priv->ltc_nr);
216 if (nv_device(ltcg)->card_type >= NV_E0)
217 nv_wr32(priv, 0x17e000, priv->ltc_nr);
218 nv_wr32(priv, 0x17e8d4, priv->tag_base);
219 return 0;
220}
221
222struct nouveau_oclass *
223gf100_ltcg_oclass = &(struct nouveau_oclass) {
224 .handle = NV_SUBDEV(LTCG, 0xc0),
225 .ofuncs = &(struct nouveau_ofuncs) {
226 .ctor = gf100_ltcg_ctor,
227 .dtor = gf100_ltcg_dtor,
228 .init = gf100_ltcg_init,
229 .fini = _nouveau_ltcg_fini,
230 },
231};
232