drm/nvc0-/gr: generate cs register lists from grctx data
[linux-2.6-block.git] / drivers / gpu / drm / nouveau / core / engine / graph / nvc0.c
CommitLineData
4b223eef 1/*
ebb945a9 2 * Copyright 2012 Red Hat Inc.
4b223eef
BS
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
02a841d4 25#include "nvc0.h"
0411de85 26
ebb945a9
BS
27/*******************************************************************************
28 * Graphics object classes
29 ******************************************************************************/
30
30f4e087 31struct nouveau_oclass
ebb945a9
BS
32nvc0_graph_sclass[] = {
33 { 0x902d, &nouveau_object_ofuncs },
34 { 0x9039, &nouveau_object_ofuncs },
35 { 0x9097, &nouveau_object_ofuncs },
36 { 0x90c0, &nouveau_object_ofuncs },
37 {}
38};
39
ebb945a9
BS
40/*******************************************************************************
41 * PGRAPH context
42 ******************************************************************************/
966a5b7d 43
ac1499d9 44int
ebb945a9
BS
45nvc0_graph_context_ctor(struct nouveau_object *parent,
46 struct nouveau_object *engine,
47 struct nouveau_oclass *oclass, void *args, u32 size,
48 struct nouveau_object **pobject)
966a5b7d 49{
ebb945a9
BS
50 struct nouveau_vm *vm = nouveau_client(parent)->vm;
51 struct nvc0_graph_priv *priv = (void *)engine;
ac1499d9
BS
52 struct nvc0_graph_data *data = priv->mmio_data;
53 struct nvc0_graph_mmio *mmio = priv->mmio_list;
ebb945a9 54 struct nvc0_graph_chan *chan;
966a5b7d 55 int ret, i;
966a5b7d 56
ebb945a9
BS
57 /* allocate memory for context, and fill with default values */
58 ret = nouveau_graph_context_create(parent, engine, oclass, NULL,
59 priv->size, 0x100,
60 NVOBJ_FLAG_ZERO_ALLOC, &chan);
61 *pobject = nv_object(chan);
966a5b7d
BS
62 if (ret)
63 return ret;
64
ac1499d9
BS
65 /* allocate memory for a "mmio list" buffer that's used by the HUB
66 * fuc to modify some per-context register settings on first load
67 * of the context.
68 */
617a6cbd
BS
69 ret = nouveau_gpuobj_new(nv_object(chan), NULL, 0x1000, 0x100, 0,
70 &chan->mmio);
73a60c0d
BS
71 if (ret)
72 return ret;
73
ebb945a9 74 ret = nouveau_gpuobj_map_vm(nv_gpuobj(chan->mmio), vm,
3863c9bc 75 NV_MEM_ACCESS_RW | NV_MEM_ACCESS_SYS,
ebb945a9 76 &chan->mmio_vma);
73a60c0d
BS
77 if (ret)
78 return ret;
79
ac1499d9
BS
80 /* allocate buffers referenced by mmio list */
81 for (i = 0; data->size && i < ARRAY_SIZE(priv->mmio_data); i++) {
617a6cbd
BS
82 ret = nouveau_gpuobj_new(nv_object(chan), NULL, data->size,
83 data->align, 0, &chan->data[i].mem);
ac1499d9
BS
84 if (ret)
85 return ret;
73a60c0d 86
ebb945a9
BS
87 ret = nouveau_gpuobj_map_vm(chan->data[i].mem, vm, data->access,
88 &chan->data[i].vma);
ac1499d9
BS
89 if (ret)
90 return ret;
966a5b7d 91
ac1499d9 92 data++;
966a5b7d
BS
93 }
94
ac1499d9
BS
95 /* finally, fill in the mmio list and point the context at it */
96 for (i = 0; mmio->addr && i < ARRAY_SIZE(priv->mmio_list); i++) {
97 u32 addr = mmio->addr;
98 u32 data = mmio->data;
966a5b7d 99
ac1499d9 100 if (mmio->shift) {
ebb945a9 101 u64 info = chan->data[mmio->buffer].vma.offset;
ac1499d9
BS
102 data |= info >> mmio->shift;
103 }
73a60c0d 104
ebb945a9
BS
105 nv_wo32(chan->mmio, chan->mmio_nr++ * 4, addr);
106 nv_wo32(chan->mmio, chan->mmio_nr++ * 4, data);
ac1499d9
BS
107 mmio++;
108 }
73a60c0d 109
ac1499d9 110 for (i = 0; i < priv->size; i += 4)
ebb945a9 111 nv_wo32(chan, i, priv->data[i / 4]);
966a5b7d 112
ac1499d9 113 if (!priv->firmware) {
ebb945a9
BS
114 nv_wo32(chan, 0x00, chan->mmio_nr / 2);
115 nv_wo32(chan, 0x04, chan->mmio_vma.offset >> 8);
0411de85 116 } else {
ebb945a9
BS
117 nv_wo32(chan, 0xf4, 0);
118 nv_wo32(chan, 0xf8, 0);
119 nv_wo32(chan, 0x10, chan->mmio_nr / 2);
120 nv_wo32(chan, 0x14, lower_32_bits(chan->mmio_vma.offset));
121 nv_wo32(chan, 0x18, upper_32_bits(chan->mmio_vma.offset));
122 nv_wo32(chan, 0x1c, 1);
123 nv_wo32(chan, 0x20, 0);
124 nv_wo32(chan, 0x28, 0);
125 nv_wo32(chan, 0x2c, 0);
0411de85 126 }
966a5b7d 127
ebb945a9 128 return 0;
4b223eef
BS
129}
130
ac1499d9 131void
ebb945a9 132nvc0_graph_context_dtor(struct nouveau_object *object)
4b223eef 133{
ebb945a9 134 struct nvc0_graph_chan *chan = (void *)object;
ac1499d9
BS
135 int i;
136
ebb945a9
BS
137 for (i = 0; i < ARRAY_SIZE(chan->data); i++) {
138 nouveau_gpuobj_unmap(&chan->data[i].vma);
139 nouveau_gpuobj_ref(NULL, &chan->data[i].mem);
ac1499d9 140 }
966a5b7d 141
ebb945a9
BS
142 nouveau_gpuobj_unmap(&chan->mmio_vma);
143 nouveau_gpuobj_ref(NULL, &chan->mmio);
ac1499d9 144
ebb945a9 145 nouveau_graph_context_destroy(&chan->base);
4b223eef
BS
146}
147
ebb945a9
BS
148/*******************************************************************************
149 * PGRAPH engine/subdev functions
150 ******************************************************************************/
151
30f4e087
BS
152struct nvc0_graph_init
153nvc0_graph_init_regs[] = {
154 { 0x400080, 1, 0x04, 0x003083c2 },
155 { 0x400088, 1, 0x04, 0x00006fe7 },
156 { 0x40008c, 1, 0x04, 0x00000000 },
157 { 0x400090, 1, 0x04, 0x00000030 },
158 { 0x40013c, 1, 0x04, 0x013901f7 },
159 { 0x400140, 1, 0x04, 0x00000100 },
160 { 0x400144, 1, 0x04, 0x00000000 },
161 { 0x400148, 1, 0x04, 0x00000110 },
162 { 0x400138, 1, 0x04, 0x00000000 },
163 { 0x400130, 2, 0x04, 0x00000000 },
164 { 0x400124, 1, 0x04, 0x00000002 },
165 {}
166};
167
168struct nvc0_graph_init
169nvc0_graph_init_unk40xx[] = {
170 { 0x40415c, 1, 0x04, 0x00000000 },
171 { 0x404170, 1, 0x04, 0x00000000 },
172 {}
173};
174
175struct nvc0_graph_init
176nvc0_graph_init_unk44xx[] = {
177 { 0x404488, 2, 0x04, 0x00000000 },
178 {}
179};
180
181struct nvc0_graph_init
182nvc0_graph_init_unk78xx[] = {
183 { 0x407808, 1, 0x04, 0x00000000 },
184 {}
185};
186
187struct nvc0_graph_init
188nvc0_graph_init_unk60xx[] = {
189 { 0x406024, 1, 0x04, 0x00000000 },
190 {}
191};
192
193struct nvc0_graph_init
194nvc0_graph_init_unk58xx[] = {
195 { 0x405844, 1, 0x04, 0x00ffffff },
196 { 0x405850, 1, 0x04, 0x00000000 },
197 { 0x405908, 1, 0x04, 0x00000000 },
198 {}
199};
200
201struct nvc0_graph_init
202nvc0_graph_init_unk80xx[] = {
203 { 0x40803c, 1, 0x04, 0x00000000 },
204 {}
205};
206
207struct nvc0_graph_init
208nvc0_graph_init_gpc[] = {
209 { 0x4184a0, 1, 0x04, 0x00000000 },
210 { 0x418604, 1, 0x04, 0x00000000 },
211 { 0x418680, 1, 0x04, 0x00000000 },
212 { 0x418714, 1, 0x04, 0x80000000 },
213 { 0x418384, 1, 0x04, 0x00000000 },
214 { 0x418814, 3, 0x04, 0x00000000 },
215 { 0x418b04, 1, 0x04, 0x00000000 },
216 { 0x4188c8, 1, 0x04, 0x80000000 },
217 { 0x4188cc, 1, 0x04, 0x00000000 },
218 { 0x4188d0, 1, 0x04, 0x00010000 },
219 { 0x4188d4, 1, 0x04, 0x00000001 },
220 { 0x418910, 1, 0x04, 0x00010001 },
221 { 0x418914, 1, 0x04, 0x00000301 },
222 { 0x418918, 1, 0x04, 0x00800000 },
223 { 0x418980, 1, 0x04, 0x77777770 },
224 { 0x418984, 3, 0x04, 0x77777777 },
225 { 0x418c04, 1, 0x04, 0x00000000 },
226 { 0x418c88, 1, 0x04, 0x00000000 },
227 { 0x418d00, 1, 0x04, 0x00000000 },
228 { 0x418f08, 1, 0x04, 0x00000000 },
229 { 0x418e00, 1, 0x04, 0x00000050 },
230 { 0x418e08, 1, 0x04, 0x00000000 },
231 { 0x41900c, 1, 0x04, 0x00000000 },
232 { 0x419018, 1, 0x04, 0x00000000 },
233 {}
234};
235
236static struct nvc0_graph_init
237nvc0_graph_init_tpc[] = {
238 { 0x419d08, 2, 0x04, 0x00000000 },
239 { 0x419d10, 1, 0x04, 0x00000014 },
240 { 0x419ab0, 1, 0x04, 0x00000000 },
241 { 0x419ab8, 1, 0x04, 0x000000e7 },
242 { 0x419abc, 2, 0x04, 0x00000000 },
243 { 0x41980c, 3, 0x04, 0x00000000 },
244 { 0x419844, 1, 0x04, 0x00000000 },
245 { 0x41984c, 1, 0x04, 0x00005bc5 },
246 { 0x419850, 4, 0x04, 0x00000000 },
247 { 0x419c98, 1, 0x04, 0x00000000 },
248 { 0x419ca8, 1, 0x04, 0x80000000 },
249 { 0x419cb4, 1, 0x04, 0x00000000 },
250 { 0x419cb8, 1, 0x04, 0x00008bf4 },
251 { 0x419cbc, 1, 0x04, 0x28137606 },
252 { 0x419cc0, 2, 0x04, 0x00000000 },
253 { 0x419bd4, 1, 0x04, 0x00800000 },
254 { 0x419bdc, 1, 0x04, 0x00000000 },
255 { 0x419d2c, 1, 0x04, 0x00000000 },
256 { 0x419c0c, 1, 0x04, 0x00000000 },
257 { 0x419e00, 1, 0x04, 0x00000000 },
258 { 0x419ea0, 1, 0x04, 0x00000000 },
259 { 0x419ea4, 1, 0x04, 0x00000100 },
260 { 0x419ea8, 1, 0x04, 0x00001100 },
261 { 0x419eac, 1, 0x04, 0x11100702 },
262 { 0x419eb0, 1, 0x04, 0x00000003 },
263 { 0x419eb4, 4, 0x04, 0x00000000 },
264 { 0x419ec8, 1, 0x04, 0x06060618 },
265 { 0x419ed0, 1, 0x04, 0x0eff0e38 },
266 { 0x419ed4, 1, 0x04, 0x011104f1 },
267 { 0x419edc, 1, 0x04, 0x00000000 },
268 { 0x419f00, 1, 0x04, 0x00000000 },
269 { 0x419f2c, 1, 0x04, 0x00000000 },
270 {}
271};
272
273struct nvc0_graph_init
274nvc0_graph_init_unk88xx[] = {
275 { 0x40880c, 1, 0x04, 0x00000000 },
276 { 0x408910, 9, 0x04, 0x00000000 },
277 { 0x408950, 1, 0x04, 0x00000000 },
278 { 0x408954, 1, 0x04, 0x0000ffff },
279 { 0x408984, 1, 0x04, 0x00000000 },
280 { 0x408988, 1, 0x04, 0x08040201 },
281 { 0x40898c, 1, 0x04, 0x80402010 },
282 {}
283};
284
285void
286nvc0_graph_mmio(struct nvc0_graph_priv *priv, struct nvc0_graph_init *init)
4b223eef 287{
30f4e087
BS
288 for (; init && init->count; init++) {
289 u32 addr = init->addr, i;
290 for (i = 0; i < init->count; i++) {
291 nv_wr32(priv, addr, init->data);
292 addr += init->pitch;
293 }
294 }
ebb945a9
BS
295}
296
297void
30f4e087 298nvc0_graph_icmd(struct nvc0_graph_priv *priv, struct nvc0_graph_init *init)
ebb945a9 299{
30f4e087
BS
300 u32 addr, data;
301 int i, j;
302
303 nv_wr32(priv, 0x400208, 0x80000000);
304 for (i = 0; init->count; init++, i++) {
305 if (!i || data != init->data) {
306 nv_wr32(priv, 0x400204, init->data);
307 data = init->data;
308 }
ebb945a9 309
30f4e087
BS
310 addr = init->addr;
311 for (j = 0; j < init->count; j++) {
312 nv_wr32(priv, 0x400200, addr);
313 addr += init->pitch;
314 while (nv_rd32(priv, 0x400700) & 0x00000002) {}
315 }
316 }
317 nv_wr32(priv, 0x400208, 0x00000000);
318}
319
320void
321nvc0_graph_mthd(struct nvc0_graph_priv *priv, struct nvc0_graph_mthd *mthds)
322{
323 struct nvc0_graph_mthd *mthd;
324 struct nvc0_graph_init *init;
325 int i = 0, j;
326 u32 data;
327
328 while ((mthd = &mthds[i++]) && (init = mthd->init)) {
329 u32 addr = 0x80000000 | mthd->oclass;
330 for (data = 0; init->count; init++) {
331 if (data != init->data) {
332 nv_wr32(priv, 0x40448c, init->data);
333 data = init->data;
334 }
335
336 addr = (addr & 0x8000ffff) | (init->addr << 14);
337 for (j = 0; j < init->count; j++) {
338 nv_wr32(priv, 0x404488, addr);
339 addr += init->pitch << 14;
340 }
341 }
342 }
343}
344
345u64
346nvc0_graph_units(struct nouveau_graph *graph)
347{
348 struct nvc0_graph_priv *priv = (void *)graph;
349 u64 cfg;
350
351 cfg = (u32)priv->gpc_nr;
352 cfg |= (u32)priv->tpc_total << 8;
353 cfg |= (u64)priv->rop_nr << 32;
354
355 return cfg;
ebb945a9
BS
356}
357
30f4e087
BS
358static const struct nouveau_enum nve0_sked_error[] = {
359 { 7, "CONSTANT_BUFFER_SIZE" },
360 { 9, "LOCAL_MEMORY_SIZE_POS" },
361 { 10, "LOCAL_MEMORY_SIZE_NEG" },
362 { 11, "WARP_CSTACK_SIZE" },
363 { 12, "TOTAL_TEMP_SIZE" },
364 { 13, "REGISTER_COUNT" },
365 { 18, "TOTAL_THREADS" },
366 { 20, "PROGRAM_OFFSET" },
367 { 21, "SHARED_MEMORY_SIZE" },
368 { 25, "SHARED_CONFIG_TOO_SMALL" },
369 { 26, "TOTAL_REGISTER_COUNT" },
370 {}
371};
372
373static const struct nouveau_enum nvc0_gpc_rop_error[] = {
374 { 1, "RT_PITCH_OVERRUN" },
375 { 4, "RT_WIDTH_OVERRUN" },
376 { 5, "RT_HEIGHT_OVERRUN" },
377 { 7, "ZETA_STORAGE_TYPE_MISMATCH" },
378 { 8, "RT_STORAGE_TYPE_MISMATCH" },
379 { 10, "RT_LINEAR_MISMATCH" },
380 {}
381};
382
ebb945a9 383static void
30f4e087 384nvc0_graph_trap_gpc_rop(struct nvc0_graph_priv *priv, int gpc)
ebb945a9 385{
30f4e087
BS
386 u32 trap[4];
387 int i;
ebb945a9 388
30f4e087
BS
389 trap[0] = nv_rd32(priv, GPC_UNIT(gpc, 0x0420));
390 trap[1] = nv_rd32(priv, GPC_UNIT(gpc, 0x0434));
391 trap[2] = nv_rd32(priv, GPC_UNIT(gpc, 0x0438));
392 trap[3] = nv_rd32(priv, GPC_UNIT(gpc, 0x043c));
ebb945a9 393
30f4e087
BS
394 nv_error(priv, "GPC%d/PROP trap:", gpc);
395 for (i = 0; i <= 29; ++i) {
396 if (!(trap[0] & (1 << i)))
397 continue;
398 pr_cont(" ");
399 nouveau_enum_print(nvc0_gpc_rop_error, i);
400 }
401 pr_cont("\n");
402
403 nv_error(priv, "x = %u, y = %u, format = %x, storage type = %x\n",
404 trap[1] & 0xffff, trap[1] >> 16, (trap[2] >> 8) & 0x3f,
405 trap[3] & 0xff);
406 nv_wr32(priv, GPC_UNIT(gpc, 0x0420), 0xc0000000);
ebb945a9
BS
407}
408
fec43a72
BS
409static const struct nouveau_enum nvc0_mp_warp_error[] = {
410 { 0x00, "NO_ERROR" },
411 { 0x01, "STACK_MISMATCH" },
412 { 0x05, "MISALIGNED_PC" },
413 { 0x08, "MISALIGNED_GPR" },
414 { 0x09, "INVALID_OPCODE" },
415 { 0x0d, "GPR_OUT_OF_BOUNDS" },
416 { 0x0e, "MEM_OUT_OF_BOUNDS" },
417 { 0x0f, "UNALIGNED_MEM_ACCESS" },
418 { 0x11, "INVALID_PARAM" },
419 {}
420};
421
422static const struct nouveau_bitfield nvc0_mp_global_error[] = {
423 { 0x00000004, "MULTIPLE_WARP_ERRORS" },
424 { 0x00000008, "OUT_OF_STACK_SPACE" },
425 {}
426};
427
428static void
429nvc0_graph_trap_mp(struct nvc0_graph_priv *priv, int gpc, int tpc)
430{
431 u32 werr = nv_rd32(priv, TPC_UNIT(gpc, tpc, 0x648));
432 u32 gerr = nv_rd32(priv, TPC_UNIT(gpc, tpc, 0x650));
433
434 nv_error(priv, "GPC%i/TPC%i/MP trap:", gpc, tpc);
435 nouveau_bitfield_print(nvc0_mp_global_error, gerr);
436 if (werr) {
437 pr_cont(" ");
438 nouveau_enum_print(nvc0_mp_warp_error, werr & 0xffff);
439 }
440 pr_cont("\n");
441
442 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x648), 0x00000000);
443 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x650), gerr);
444}
445
f73221e4
BS
446static void
447nvc0_graph_trap_tpc(struct nvc0_graph_priv *priv, int gpc, int tpc)
448{
449 u32 stat = nv_rd32(priv, TPC_UNIT(gpc, tpc, 0x0508));
450
451 if (stat & 0x00000001) {
452 u32 trap = nv_rd32(priv, TPC_UNIT(gpc, tpc, 0x0224));
453 nv_error(priv, "GPC%d/TPC%d/TEX: 0x%08x\n", gpc, tpc, trap);
454 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x0224), 0xc0000000);
f73221e4
BS
455 stat &= ~0x00000001;
456 }
457
458 if (stat & 0x00000002) {
fec43a72 459 nvc0_graph_trap_mp(priv, gpc, tpc);
f73221e4
BS
460 stat &= ~0x00000002;
461 }
462
463 if (stat & 0x00000004) {
464 u32 trap = nv_rd32(priv, TPC_UNIT(gpc, tpc, 0x0084));
465 nv_error(priv, "GPC%d/TPC%d/POLY: 0x%08x\n", gpc, tpc, trap);
466 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x0084), 0xc0000000);
f73221e4
BS
467 stat &= ~0x00000004;
468 }
469
470 if (stat & 0x00000008) {
471 u32 trap = nv_rd32(priv, TPC_UNIT(gpc, tpc, 0x048c));
472 nv_error(priv, "GPC%d/TPC%d/L1C: 0x%08x\n", gpc, tpc, trap);
473 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x048c), 0xc0000000);
f73221e4
BS
474 stat &= ~0x00000008;
475 }
476
477 if (stat) {
478 nv_error(priv, "GPC%d/TPC%d/0x%08x: unknown\n", gpc, tpc, stat);
f73221e4
BS
479 }
480}
481
482static void
483nvc0_graph_trap_gpc(struct nvc0_graph_priv *priv, int gpc)
484{
485 u32 stat = nv_rd32(priv, GPC_UNIT(gpc, 0x2c90));
486 int tpc;
487
488 if (stat & 0x00000001) {
30f4e087 489 nvc0_graph_trap_gpc_rop(priv, gpc);
f73221e4
BS
490 stat &= ~0x00000001;
491 }
492
493 if (stat & 0x00000002) {
494 u32 trap = nv_rd32(priv, GPC_UNIT(gpc, 0x0900));
495 nv_error(priv, "GPC%d/ZCULL: 0x%08x\n", gpc, trap);
496 nv_wr32(priv, GPC_UNIT(gpc, 0x0900), 0xc0000000);
f73221e4
BS
497 stat &= ~0x00000002;
498 }
499
500 if (stat & 0x00000004) {
501 u32 trap = nv_rd32(priv, GPC_UNIT(gpc, 0x1028));
502 nv_error(priv, "GPC%d/CCACHE: 0x%08x\n", gpc, trap);
503 nv_wr32(priv, GPC_UNIT(gpc, 0x1028), 0xc0000000);
f73221e4
BS
504 stat &= ~0x00000004;
505 }
506
507 if (stat & 0x00000008) {
508 u32 trap = nv_rd32(priv, GPC_UNIT(gpc, 0x0824));
509 nv_error(priv, "GPC%d/ESETUP: 0x%08x\n", gpc, trap);
510 nv_wr32(priv, GPC_UNIT(gpc, 0x0824), 0xc0000000);
f73221e4
BS
511 stat &= ~0x00000009;
512 }
513
514 for (tpc = 0; tpc < priv->tpc_nr[gpc]; tpc++) {
515 u32 mask = 0x00010000 << tpc;
516 if (stat & mask) {
517 nvc0_graph_trap_tpc(priv, gpc, tpc);
518 nv_wr32(priv, GPC_UNIT(gpc, 0x2c90), mask);
519 stat &= ~mask;
520 }
521 }
522
523 if (stat) {
524 nv_error(priv, "GPC%d/0x%08x: unknown\n", gpc, stat);
f73221e4
BS
525 }
526}
527
528static void
529nvc0_graph_trap_intr(struct nvc0_graph_priv *priv)
530{
531 u32 trap = nv_rd32(priv, 0x400108);
30f4e087 532 int rop, gpc, i;
f73221e4
BS
533
534 if (trap & 0x00000001) {
535 u32 stat = nv_rd32(priv, 0x404000);
536 nv_error(priv, "DISPATCH 0x%08x\n", stat);
537 nv_wr32(priv, 0x404000, 0xc0000000);
538 nv_wr32(priv, 0x400108, 0x00000001);
539 trap &= ~0x00000001;
540 }
541
542 if (trap & 0x00000002) {
543 u32 stat = nv_rd32(priv, 0x404600);
544 nv_error(priv, "M2MF 0x%08x\n", stat);
545 nv_wr32(priv, 0x404600, 0xc0000000);
546 nv_wr32(priv, 0x400108, 0x00000002);
547 trap &= ~0x00000002;
548 }
549
550 if (trap & 0x00000008) {
551 u32 stat = nv_rd32(priv, 0x408030);
552 nv_error(priv, "CCACHE 0x%08x\n", stat);
553 nv_wr32(priv, 0x408030, 0xc0000000);
554 nv_wr32(priv, 0x400108, 0x00000008);
555 trap &= ~0x00000008;
556 }
557
558 if (trap & 0x00000010) {
559 u32 stat = nv_rd32(priv, 0x405840);
560 nv_error(priv, "SHADER 0x%08x\n", stat);
561 nv_wr32(priv, 0x405840, 0xc0000000);
562 nv_wr32(priv, 0x400108, 0x00000010);
563 trap &= ~0x00000010;
564 }
565
566 if (trap & 0x00000040) {
567 u32 stat = nv_rd32(priv, 0x40601c);
568 nv_error(priv, "UNK6 0x%08x\n", stat);
569 nv_wr32(priv, 0x40601c, 0xc0000000);
570 nv_wr32(priv, 0x400108, 0x00000040);
571 trap &= ~0x00000040;
572 }
573
574 if (trap & 0x00000080) {
575 u32 stat = nv_rd32(priv, 0x404490);
576 nv_error(priv, "MACRO 0x%08x\n", stat);
577 nv_wr32(priv, 0x404490, 0xc0000000);
578 nv_wr32(priv, 0x400108, 0x00000080);
579 trap &= ~0x00000080;
580 }
581
30f4e087
BS
582 if (trap & 0x00000100) {
583 u32 stat = nv_rd32(priv, 0x407020);
584
585 nv_error(priv, "SKED:");
586 for (i = 0; i <= 29; ++i) {
587 if (!(stat & (1 << i)))
588 continue;
589 pr_cont(" ");
590 nouveau_enum_print(nve0_sked_error, i);
591 }
592 pr_cont("\n");
593
594 if (stat & 0x3fffffff)
595 nv_wr32(priv, 0x407020, 0x40000000);
596 nv_wr32(priv, 0x400108, 0x00000100);
597 trap &= ~0x00000100;
598 }
599
f73221e4
BS
600 if (trap & 0x01000000) {
601 u32 stat = nv_rd32(priv, 0x400118);
602 for (gpc = 0; stat && gpc < priv->gpc_nr; gpc++) {
603 u32 mask = 0x00000001 << gpc;
604 if (stat & mask) {
605 nvc0_graph_trap_gpc(priv, gpc);
606 nv_wr32(priv, 0x400118, mask);
607 stat &= ~mask;
608 }
609 }
610 nv_wr32(priv, 0x400108, 0x01000000);
611 trap &= ~0x01000000;
612 }
613
614 if (trap & 0x02000000) {
615 for (rop = 0; rop < priv->rop_nr; rop++) {
616 u32 statz = nv_rd32(priv, ROP_UNIT(rop, 0x070));
617 u32 statc = nv_rd32(priv, ROP_UNIT(rop, 0x144));
618 nv_error(priv, "ROP%d 0x%08x 0x%08x\n",
619 rop, statz, statc);
620 nv_wr32(priv, ROP_UNIT(rop, 0x070), 0xc0000000);
621 nv_wr32(priv, ROP_UNIT(rop, 0x144), 0xc0000000);
622 }
623 nv_wr32(priv, 0x400108, 0x02000000);
624 trap &= ~0x02000000;
625 }
626
627 if (trap) {
628 nv_error(priv, "TRAP UNHANDLED 0x%08x\n", trap);
629 nv_wr32(priv, 0x400108, trap);
630 }
631}
632
30f4e087
BS
633static void
634nvc0_graph_ctxctl_debug_unit(struct nvc0_graph_priv *priv, u32 base)
635{
636 nv_error(priv, "%06x - done 0x%08x\n", base,
637 nv_rd32(priv, base + 0x400));
638 nv_error(priv, "%06x - stat 0x%08x 0x%08x 0x%08x 0x%08x\n", base,
639 nv_rd32(priv, base + 0x800), nv_rd32(priv, base + 0x804),
640 nv_rd32(priv, base + 0x808), nv_rd32(priv, base + 0x80c));
641 nv_error(priv, "%06x - stat 0x%08x 0x%08x 0x%08x 0x%08x\n", base,
642 nv_rd32(priv, base + 0x810), nv_rd32(priv, base + 0x814),
643 nv_rd32(priv, base + 0x818), nv_rd32(priv, base + 0x81c));
644}
645
646void
647nvc0_graph_ctxctl_debug(struct nvc0_graph_priv *priv)
648{
649 u32 gpcnr = nv_rd32(priv, 0x409604) & 0xffff;
650 u32 gpc;
651
652 nvc0_graph_ctxctl_debug_unit(priv, 0x409000);
653 for (gpc = 0; gpc < gpcnr; gpc++)
654 nvc0_graph_ctxctl_debug_unit(priv, 0x502000 + (gpc * 0x8000));
655}
656
657static void
658nvc0_graph_ctxctl_isr(struct nvc0_graph_priv *priv)
659{
660 u32 ustat = nv_rd32(priv, 0x409c18);
661
662 if (ustat & 0x00000001)
663 nv_error(priv, "CTXCTL ucode error\n");
664 if (ustat & 0x00080000)
665 nv_error(priv, "CTXCTL watchdog timeout\n");
666 if (ustat & ~0x00080001)
667 nv_error(priv, "CTXCTL 0x%08x\n", ustat);
668
669 nvc0_graph_ctxctl_debug(priv);
670 nv_wr32(priv, 0x409c20, ustat);
671}
672
ebb945a9
BS
673static void
674nvc0_graph_intr(struct nouveau_subdev *subdev)
675{
72a14827 676 struct nouveau_fifo *pfifo = nouveau_fifo(subdev);
ebb945a9 677 struct nouveau_engine *engine = nv_engine(subdev);
72a14827
BS
678 struct nouveau_object *engctx;
679 struct nouveau_handle *handle;
680 struct nvc0_graph_priv *priv = (void *)subdev;
681 u64 inst = nv_rd32(priv, 0x409b00) & 0x0fffffff;
ebb945a9
BS
682 u32 stat = nv_rd32(priv, 0x400100);
683 u32 addr = nv_rd32(priv, 0x400704);
684 u32 mthd = (addr & 0x00003ffc);
685 u32 subc = (addr & 0x00070000) >> 16;
686 u32 data = nv_rd32(priv, 0x400708);
687 u32 code = nv_rd32(priv, 0x400110);
688 u32 class = nv_rd32(priv, 0x404200 + (subc * 4));
72a14827
BS
689 int chid;
690
691 engctx = nouveau_engctx_get(engine, inst);
692 chid = pfifo->chid(pfifo, engctx);
ebb945a9
BS
693
694 if (stat & 0x00000010) {
72a14827 695 handle = nouveau_handle_get_class(engctx, class);
ebb945a9 696 if (!handle || nv_call(handle->object, mthd, data)) {
93260d3c
MS
697 nv_error(priv,
698 "ILLEGAL_MTHD ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x\n",
699 chid, inst << 12, nouveau_client_name(engctx),
700 subc, class, mthd, data);
ebb945a9 701 }
72a14827 702 nouveau_handle_put(handle);
ebb945a9
BS
703 nv_wr32(priv, 0x400100, 0x00000010);
704 stat &= ~0x00000010;
705 }
706
707 if (stat & 0x00000020) {
93260d3c
MS
708 nv_error(priv,
709 "ILLEGAL_CLASS ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x\n",
710 chid, inst << 12, nouveau_client_name(engctx), subc,
711 class, mthd, data);
ebb945a9
BS
712 nv_wr32(priv, 0x400100, 0x00000020);
713 stat &= ~0x00000020;
714 }
715
716 if (stat & 0x00100000) {
717 nv_error(priv, "DATA_ERROR [");
718 nouveau_enum_print(nv50_data_error_names, code);
93260d3c
MS
719 pr_cont("] ch %d [0x%010llx %s] subc %d class 0x%04x mthd 0x%04x data 0x%08x\n",
720 chid, inst << 12, nouveau_client_name(engctx), subc,
721 class, mthd, data);
ebb945a9
BS
722 nv_wr32(priv, 0x400100, 0x00100000);
723 stat &= ~0x00100000;
724 }
725
726 if (stat & 0x00200000) {
93260d3c
MS
727 nv_error(priv, "TRAP ch %d [0x%010llx %s]\n", chid, inst << 12,
728 nouveau_client_name(engctx));
f73221e4 729 nvc0_graph_trap_intr(priv);
ebb945a9
BS
730 nv_wr32(priv, 0x400100, 0x00200000);
731 stat &= ~0x00200000;
732 }
733
734 if (stat & 0x00080000) {
735 nvc0_graph_ctxctl_isr(priv);
736 nv_wr32(priv, 0x400100, 0x00080000);
737 stat &= ~0x00080000;
738 }
739
740 if (stat) {
741 nv_error(priv, "unknown stat 0x%08x\n", stat);
742 nv_wr32(priv, 0x400100, stat);
743 }
744
745 nv_wr32(priv, 0x400500, 0x00010001);
72a14827 746 nouveau_engctx_put(engctx);
ebb945a9
BS
747}
748
30f4e087
BS
749void
750nvc0_graph_init_fw(struct nvc0_graph_priv *priv, u32 fuc_base,
751 struct nvc0_graph_fuc *code, struct nvc0_graph_fuc *data)
ebb945a9 752{
30f4e087 753 int i;
ebb945a9 754
30f4e087
BS
755 nv_wr32(priv, fuc_base + 0x01c0, 0x01000000);
756 for (i = 0; i < data->size / 4; i++)
757 nv_wr32(priv, fuc_base + 0x01c4, data->data[i]);
ebb945a9 758
30f4e087
BS
759 nv_wr32(priv, fuc_base + 0x0180, 0x01000000);
760 for (i = 0; i < code->size / 4; i++) {
761 if ((i & 0x3f) == 0)
762 nv_wr32(priv, fuc_base + 0x0188, i >> 6);
763 nv_wr32(priv, fuc_base + 0x0184, code->data[i]);
764 }
966a5b7d
BS
765}
766
a32b2ffb
BS
767static void
768nvc0_graph_init_csdata(struct nvc0_graph_priv *priv,
769 struct nvc0_graph_init *init,
770 u32 falcon, u32 starstar, u32 base)
771{
772 u32 addr = init->addr;
773 u32 next = addr;
774 u32 star, temp;
775
776 nv_wr32(priv, falcon + 0x01c0, 0x02000000 + starstar);
777 star = nv_rd32(priv, falcon + 0x01c4);
778 temp = nv_rd32(priv, falcon + 0x01c4);
779 if (temp > star)
780 star = temp;
781 nv_wr32(priv, falcon + 0x01c0, 0x01000000 + star);
782
783 do {
784 if (init->addr != next) {
785 while (addr < next) {
786 u32 nr = min((int)(next - addr) / 4, 32);
787 nv_wr32(priv, falcon + 0x01c4,
788 ((nr - 1) << 26) | (addr - base));
789 addr += nr * 4;
790 star += 4;
791 }
792 addr = next = init->addr;
793 }
794 next += init->count * 4;
795 } while ((init++)->count);
796
797 nv_wr32(priv, falcon + 0x01c0, 0x01000004 + starstar);
798 nv_wr32(priv, falcon + 0x01c4, star);
799}
800
30f4e087
BS
801int
802nvc0_graph_init_ctxctl(struct nvc0_graph_priv *priv)
966a5b7d 803{
30f4e087 804 struct nvc0_graph_oclass *oclass = (void *)nv_object(priv)->oclass;
a32b2ffb
BS
805 struct nvc0_grctx_oclass *cclass = (void *)nv_engine(priv)->cclass;
806 struct nvc0_graph_init *init;
30f4e087
BS
807 u32 r000260;
808 int i;
ebb945a9 809
30f4e087
BS
810 if (priv->firmware) {
811 /* load fuc microcode */
812 r000260 = nv_mask(priv, 0x000260, 0x00000001, 0x00000000);
813 nvc0_graph_init_fw(priv, 0x409000, &priv->fuc409c,
814 &priv->fuc409d);
815 nvc0_graph_init_fw(priv, 0x41a000, &priv->fuc41ac,
816 &priv->fuc41ad);
817 nv_wr32(priv, 0x000260, r000260);
ebb945a9 818
30f4e087
BS
819 /* start both of them running */
820 nv_wr32(priv, 0x409840, 0xffffffff);
821 nv_wr32(priv, 0x41a10c, 0x00000000);
822 nv_wr32(priv, 0x40910c, 0x00000000);
823 nv_wr32(priv, 0x41a100, 0x00000002);
824 nv_wr32(priv, 0x409100, 0x00000002);
825 if (!nv_wait(priv, 0x409800, 0x00000001, 0x00000001))
826 nv_warn(priv, "0x409800 wait failed\n");
ebb945a9 827
30f4e087
BS
828 nv_wr32(priv, 0x409840, 0xffffffff);
829 nv_wr32(priv, 0x409500, 0x7fffffff);
830 nv_wr32(priv, 0x409504, 0x00000021);
7e22e71e 831
30f4e087
BS
832 nv_wr32(priv, 0x409840, 0xffffffff);
833 nv_wr32(priv, 0x409500, 0x00000000);
834 nv_wr32(priv, 0x409504, 0x00000010);
835 if (!nv_wait_ne(priv, 0x409800, 0xffffffff, 0x00000000)) {
836 nv_error(priv, "fuc09 req 0x10 timeout\n");
837 return -EBUSY;
838 }
839 priv->size = nv_rd32(priv, 0x409800);
0411de85 840
ebb945a9
BS
841 nv_wr32(priv, 0x409840, 0xffffffff);
842 nv_wr32(priv, 0x409500, 0x00000000);
843 nv_wr32(priv, 0x409504, 0x00000016);
844 if (!nv_wait_ne(priv, 0x409800, 0xffffffff, 0x00000000)) {
845 nv_error(priv, "fuc09 req 0x16 timeout\n");
ac1499d9
BS
846 return -EBUSY;
847 }
848
ebb945a9
BS
849 nv_wr32(priv, 0x409840, 0xffffffff);
850 nv_wr32(priv, 0x409500, 0x00000000);
851 nv_wr32(priv, 0x409504, 0x00000025);
852 if (!nv_wait_ne(priv, 0x409800, 0xffffffff, 0x00000000)) {
853 nv_error(priv, "fuc09 req 0x25 timeout\n");
ac1499d9
BS
854 return -EBUSY;
855 }
856
30f4e087
BS
857 if (nv_device(priv)->chipset >= 0xe0) {
858 nv_wr32(priv, 0x409800, 0x00000000);
859 nv_wr32(priv, 0x409500, 0x00000001);
860 nv_wr32(priv, 0x409504, 0x00000030);
861 if (!nv_wait_ne(priv, 0x409800, 0xffffffff, 0x00000000)) {
862 nv_error(priv, "fuc09 req 0x30 timeout\n");
863 return -EBUSY;
864 }
865
866 nv_wr32(priv, 0x409810, 0xb00095c8);
867 nv_wr32(priv, 0x409800, 0x00000000);
868 nv_wr32(priv, 0x409500, 0x00000001);
869 nv_wr32(priv, 0x409504, 0x00000031);
870 if (!nv_wait_ne(priv, 0x409800, 0xffffffff, 0x00000000)) {
871 nv_error(priv, "fuc09 req 0x31 timeout\n");
872 return -EBUSY;
873 }
874
875 nv_wr32(priv, 0x409810, 0x00080420);
876 nv_wr32(priv, 0x409800, 0x00000000);
877 nv_wr32(priv, 0x409500, 0x00000001);
878 nv_wr32(priv, 0x409504, 0x00000032);
879 if (!nv_wait_ne(priv, 0x409800, 0xffffffff, 0x00000000)) {
880 nv_error(priv, "fuc09 req 0x32 timeout\n");
881 return -EBUSY;
882 }
883
884 nv_wr32(priv, 0x409614, 0x00000070);
885 nv_wr32(priv, 0x409614, 0x00000770);
886 nv_wr32(priv, 0x40802c, 0x00000001);
887 }
888
ebb945a9
BS
889 if (priv->data == NULL) {
890 int ret = nvc0_grctx_generate(priv);
891 if (ret) {
892 nv_error(priv, "failed to construct context\n");
893 return ret;
894 }
895 }
896
897 return 0;
0411de85 898 }
966a5b7d 899
ac1499d9 900 /* load HUB microcode */
ebb945a9
BS
901 r000260 = nv_mask(priv, 0x000260, 0x00000001, 0x00000000);
902 nv_wr32(priv, 0x4091c0, 0x01000000);
30f4e087
BS
903 for (i = 0; i < oclass->fecs.ucode->data.size / 4; i++)
904 nv_wr32(priv, 0x4091c4, oclass->fecs.ucode->data.data[i]);
ac1499d9 905
ebb945a9 906 nv_wr32(priv, 0x409180, 0x01000000);
30f4e087 907 for (i = 0; i < oclass->fecs.ucode->code.size / 4; i++) {
ac1499d9 908 if ((i & 0x3f) == 0)
ebb945a9 909 nv_wr32(priv, 0x409188, i >> 6);
30f4e087 910 nv_wr32(priv, 0x409184, oclass->fecs.ucode->code.data[i]);
ac1499d9
BS
911 }
912
a32b2ffb
BS
913 for (i = 0; (init = cclass->hub[i]); i++) {
914 nvc0_graph_init_csdata(priv, init, 0x409000, 0x000, 0x000000);
915 }
916
ac1499d9 917 /* load GPC microcode */
ebb945a9 918 nv_wr32(priv, 0x41a1c0, 0x01000000);
30f4e087
BS
919 for (i = 0; i < oclass->gpccs.ucode->data.size / 4; i++)
920 nv_wr32(priv, 0x41a1c4, oclass->gpccs.ucode->data.data[i]);
ac1499d9 921
ebb945a9 922 nv_wr32(priv, 0x41a180, 0x01000000);
30f4e087 923 for (i = 0; i < oclass->gpccs.ucode->code.size / 4; i++) {
ac1499d9 924 if ((i & 0x3f) == 0)
ebb945a9 925 nv_wr32(priv, 0x41a188, i >> 6);
30f4e087 926 nv_wr32(priv, 0x41a184, oclass->gpccs.ucode->code.data[i]);
ac1499d9 927 }
ebb945a9 928 nv_wr32(priv, 0x000260, r000260);
966a5b7d 929
a32b2ffb
BS
930 if ((init = cclass->gpc[0]))
931 nvc0_graph_init_csdata(priv, init, 0x41a000, 0x000, 0x418000);
932 if ((init = cclass->gpc[2]))
933 nvc0_graph_init_csdata(priv, init, 0x41a000, 0x004, 0x419800);
934 if ((init = cclass->gpc[3]))
935 nvc0_graph_init_csdata(priv, init, 0x41a000, 0x008, 0x41be00);
936
ac1499d9 937 /* start HUB ucode running, it'll init the GPCs */
ebb945a9
BS
938 nv_wr32(priv, 0x40910c, 0x00000000);
939 nv_wr32(priv, 0x409100, 0x00000002);
940 if (!nv_wait(priv, 0x409800, 0x80000000, 0x80000000)) {
941 nv_error(priv, "HUB_INIT timed out\n");
942 nvc0_graph_ctxctl_debug(priv);
966a5b7d
BS
943 return -EBUSY;
944 }
966a5b7d 945
ebb945a9 946 priv->size = nv_rd32(priv, 0x409804);
ac1499d9 947 if (priv->data == NULL) {
ebb945a9 948 int ret = nvc0_grctx_generate(priv);
ac1499d9 949 if (ret) {
ebb945a9 950 nv_error(priv, "failed to construct context\n");
ac1499d9
BS
951 return ret;
952 }
966a5b7d
BS
953 }
954
955 return 0;
4b223eef
BS
956}
957
30f4e087 958int
ebb945a9 959nvc0_graph_init(struct nouveau_object *object)
4b223eef 960{
30f4e087 961 struct nvc0_graph_oclass *oclass = (void *)object->oclass;
ebb945a9 962 struct nvc0_graph_priv *priv = (void *)object;
30f4e087
BS
963 const u32 magicgpc918 = DIV_ROUND_UP(0x00800000, priv->tpc_total);
964 u32 data[TPC_MAX / 8] = {};
965 u8 tpcnr[GPC_MAX];
966 int gpc, tpc, rop;
967 int ret, i;
966a5b7d 968
ebb945a9
BS
969 ret = nouveau_graph_init(&priv->base);
970 if (ret)
971 return ret;
972
30f4e087
BS
973 nv_wr32(priv, GPC_BCAST(0x0880), 0x00000000);
974 nv_wr32(priv, GPC_BCAST(0x08a4), 0x00000000);
975 nv_wr32(priv, GPC_BCAST(0x0888), 0x00000000);
976 nv_wr32(priv, GPC_BCAST(0x088c), 0x00000000);
977 nv_wr32(priv, GPC_BCAST(0x0890), 0x00000000);
978 nv_wr32(priv, GPC_BCAST(0x0894), 0x00000000);
979 nv_wr32(priv, GPC_BCAST(0x08b4), priv->unk4188b4->addr >> 8);
980 nv_wr32(priv, GPC_BCAST(0x08b8), priv->unk4188b8->addr >> 8);
981
982 for (i = 0; oclass->mmio[i]; i++)
983 nvc0_graph_mmio(priv, oclass->mmio[i]);
984
985 /* affects TFB offset queries */
986 nv_wr32(priv, TPC_UNIT(0, 0, 0x5c), 1);
987
988 memcpy(tpcnr, priv->tpc_nr, sizeof(priv->tpc_nr));
989 for (i = 0, gpc = -1; i < priv->tpc_total; i++) {
990 do {
991 gpc = (gpc + 1) % priv->gpc_nr;
992 } while (!tpcnr[gpc]);
993 tpc = priv->tpc_nr[gpc] - tpcnr[gpc]--;
994
995 data[i / 8] |= tpc << ((i % 8) * 4);
996 }
997
998 nv_wr32(priv, GPC_BCAST(0x0980), data[0]);
999 nv_wr32(priv, GPC_BCAST(0x0984), data[1]);
1000 nv_wr32(priv, GPC_BCAST(0x0988), data[2]);
1001 nv_wr32(priv, GPC_BCAST(0x098c), data[3]);
1002
1003 for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
1004 nv_wr32(priv, GPC_UNIT(gpc, 0x0914),
1005 priv->magic_not_rop_nr << 8 | priv->tpc_nr[gpc]);
1006 nv_wr32(priv, GPC_UNIT(gpc, 0x0910), 0x00040000 |
1007 priv->tpc_total);
1008 nv_wr32(priv, GPC_UNIT(gpc, 0x0918), magicgpc918);
1009 }
1010
1011 nv_wr32(priv, GPC_BCAST(0x1bd4), magicgpc918);
1012 nv_wr32(priv, GPC_BCAST(0x08ac), nv_rd32(priv, 0x100800));
ebb945a9
BS
1013
1014 nv_wr32(priv, 0x400500, 0x00010001);
30f4e087 1015
ebb945a9
BS
1016 nv_wr32(priv, 0x400100, 0xffffffff);
1017 nv_wr32(priv, 0x40013c, 0xffffffff);
1018
30f4e087
BS
1019 nv_wr32(priv, 0x409c24, 0x000f0000);
1020 nv_wr32(priv, 0x404000, 0xc0000000);
1021 nv_wr32(priv, 0x404600, 0xc0000000);
1022 nv_wr32(priv, 0x408030, 0xc0000000);
1023 nv_wr32(priv, 0x40601c, 0xc0000000);
1024 nv_wr32(priv, 0x404490, 0xc0000000);
1025 nv_wr32(priv, 0x406018, 0xc0000000);
1026 nv_wr32(priv, 0x405840, 0xc0000000);
1027 nv_wr32(priv, 0x405844, 0x00ffffff);
1028 nv_mask(priv, 0x419cc0, 0x00000008, 0x00000008);
1029 nv_mask(priv, 0x419eb4, 0x00001000, 0x00001000);
1030
1031 for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
1032 nv_wr32(priv, GPC_UNIT(gpc, 0x0420), 0xc0000000);
1033 nv_wr32(priv, GPC_UNIT(gpc, 0x0900), 0xc0000000);
1034 nv_wr32(priv, GPC_UNIT(gpc, 0x1028), 0xc0000000);
1035 nv_wr32(priv, GPC_UNIT(gpc, 0x0824), 0xc0000000);
1036 for (tpc = 0; tpc < priv->tpc_nr[gpc]; tpc++) {
1037 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x508), 0xffffffff);
1038 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x50c), 0xffffffff);
1039 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x224), 0xc0000000);
1040 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x48c), 0xc0000000);
1041 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x084), 0xc0000000);
1042 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x644), 0x001ffffe);
1043 nv_wr32(priv, TPC_UNIT(gpc, tpc, 0x64c), 0x0000000f);
1044 }
1045 nv_wr32(priv, GPC_UNIT(gpc, 0x2c90), 0xffffffff);
1046 nv_wr32(priv, GPC_UNIT(gpc, 0x2c94), 0xffffffff);
1047 }
1048
1049 for (rop = 0; rop < priv->rop_nr; rop++) {
1050 nv_wr32(priv, ROP_UNIT(rop, 0x144), 0xc0000000);
1051 nv_wr32(priv, ROP_UNIT(rop, 0x070), 0xc0000000);
1052 nv_wr32(priv, ROP_UNIT(rop, 0x204), 0xffffffff);
1053 nv_wr32(priv, ROP_UNIT(rop, 0x208), 0xffffffff);
1054 }
ebb945a9
BS
1055
1056 nv_wr32(priv, 0x400108, 0xffffffff);
1057 nv_wr32(priv, 0x400138, 0xffffffff);
1058 nv_wr32(priv, 0x400118, 0xffffffff);
1059 nv_wr32(priv, 0x400130, 0xffffffff);
1060 nv_wr32(priv, 0x40011c, 0xffffffff);
1061 nv_wr32(priv, 0x400134, 0xffffffff);
30f4e087 1062
ebb945a9 1063 nv_wr32(priv, 0x400054, 0x34ce3464);
30f4e087
BS
1064 return nvc0_graph_init_ctxctl(priv);
1065}
1066
1067static void
1068nvc0_graph_dtor_fw(struct nvc0_graph_fuc *fuc)
1069{
1070 kfree(fuc->data);
1071 fuc->data = NULL;
1072}
1073
1074int
1075nvc0_graph_ctor_fw(struct nvc0_graph_priv *priv, const char *fwname,
1076 struct nvc0_graph_fuc *fuc)
1077{
1078 struct nouveau_device *device = nv_device(priv);
1079 const struct firmware *fw;
1080 char f[32];
1081 int ret;
1082
1083 snprintf(f, sizeof(f), "nouveau/nv%02x_%s", device->chipset, fwname);
1084 ret = request_firmware(&fw, f, &device->pdev->dev);
1085 if (ret) {
1086 snprintf(f, sizeof(f), "nouveau/%s", fwname);
1087 ret = request_firmware(&fw, f, &device->pdev->dev);
1088 if (ret) {
1089 nv_error(priv, "failed to load %s\n", fwname);
1090 return ret;
1091 }
1092 }
1093
1094 fuc->size = fw->size;
1095 fuc->data = kmemdup(fw->data, fuc->size, GFP_KERNEL);
1096 release_firmware(fw);
1097 return (fuc->data != NULL) ? 0 : -ENOMEM;
1098}
1099
1100void
1101nvc0_graph_dtor(struct nouveau_object *object)
1102{
1103 struct nvc0_graph_priv *priv = (void *)object;
1104
1105 kfree(priv->data);
1106
1107 nvc0_graph_dtor_fw(&priv->fuc409c);
1108 nvc0_graph_dtor_fw(&priv->fuc409d);
1109 nvc0_graph_dtor_fw(&priv->fuc41ac);
1110 nvc0_graph_dtor_fw(&priv->fuc41ad);
1111
1112 nouveau_gpuobj_ref(NULL, &priv->unk4188b8);
1113 nouveau_gpuobj_ref(NULL, &priv->unk4188b4);
1114
1115 nouveau_graph_destroy(&priv->base);
1116}
1117
1118int
1119nvc0_graph_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
1120 struct nouveau_oclass *bclass, void *data, u32 size,
1121 struct nouveau_object **pobject)
1122{
1123 struct nvc0_graph_oclass *oclass = (void *)bclass;
1124 struct nouveau_device *device = nv_device(parent);
1125 struct nvc0_graph_priv *priv;
1126 bool enable = device->chipset != 0xd7;
1127 int ret, i;
1128
1129 ret = nouveau_graph_create(parent, engine, bclass, enable, &priv);
1130 *pobject = nv_object(priv);
1131 if (ret)
1132 return ret;
1133
1134 nv_subdev(priv)->unit = 0x18001000;
1135 nv_subdev(priv)->intr = nvc0_graph_intr;
1136
1137 priv->base.units = nvc0_graph_units;
1138
1139 if (nouveau_boolopt(device->cfgopt, "NvGrUseFW", false)) {
1140 nv_info(priv, "using external firmware\n");
1141 if (nvc0_graph_ctor_fw(priv, "fuc409c", &priv->fuc409c) ||
1142 nvc0_graph_ctor_fw(priv, "fuc409d", &priv->fuc409d) ||
1143 nvc0_graph_ctor_fw(priv, "fuc41ac", &priv->fuc41ac) ||
1144 nvc0_graph_ctor_fw(priv, "fuc41ad", &priv->fuc41ad))
1145 return -EINVAL;
1146 priv->firmware = true;
1147 }
1148
1149 ret = nouveau_gpuobj_new(nv_object(priv), NULL, 0x1000, 256, 0,
1150 &priv->unk4188b4);
1151 if (ret)
1152 return ret;
ebb945a9 1153
30f4e087
BS
1154 ret = nouveau_gpuobj_new(nv_object(priv), NULL, 0x1000, 256, 0,
1155 &priv->unk4188b8);
b10f20d5 1156 if (ret)
a82dd49f
BS
1157 return ret;
1158
30f4e087
BS
1159 for (i = 0; i < 0x1000; i += 4) {
1160 nv_wo32(priv->unk4188b4, i, 0x00000010);
1161 nv_wo32(priv->unk4188b8, i, 0x00000010);
1162 }
1163
1164 priv->rop_nr = (nv_rd32(priv, 0x409604) & 0x001f0000) >> 16;
1165 priv->gpc_nr = nv_rd32(priv, 0x409604) & 0x0000001f;
1166 for (i = 0; i < priv->gpc_nr; i++) {
1167 priv->tpc_nr[i] = nv_rd32(priv, GPC_UNIT(i, 0x2608));
1168 priv->tpc_total += priv->tpc_nr[i];
1169 }
1170
1171 /*XXX: these need figuring out... though it might not even matter */
1172 switch (nv_device(priv)->chipset) {
1173 case 0xc0:
1174 if (priv->tpc_total == 11) { /* 465, 3/4/4/0, 4 */
1175 priv->magic_not_rop_nr = 0x07;
1176 } else
1177 if (priv->tpc_total == 14) { /* 470, 3/3/4/4, 5 */
1178 priv->magic_not_rop_nr = 0x05;
1179 } else
1180 if (priv->tpc_total == 15) { /* 480, 3/4/4/4, 6 */
1181 priv->magic_not_rop_nr = 0x06;
1182 }
1183 break;
1184 case 0xc3: /* 450, 4/0/0/0, 2 */
1185 priv->magic_not_rop_nr = 0x03;
1186 break;
1187 case 0xc4: /* 460, 3/4/0/0, 4 */
1188 priv->magic_not_rop_nr = 0x01;
1189 break;
1190 case 0xc1: /* 2/0/0/0, 1 */
1191 priv->magic_not_rop_nr = 0x01;
1192 break;
1193 case 0xc8: /* 4/4/3/4, 5 */
1194 priv->magic_not_rop_nr = 0x06;
1195 break;
1196 case 0xce: /* 4/4/0/0, 4 */
1197 priv->magic_not_rop_nr = 0x03;
1198 break;
1199 case 0xcf: /* 4/0/0/0, 3 */
1200 priv->magic_not_rop_nr = 0x03;
1201 break;
1202 case 0xd9: /* 1/0/0/0, 1 */
1203 priv->magic_not_rop_nr = 0x01;
1204 break;
1205 }
1206
1207 nv_engine(priv)->cclass = *oclass->cclass;
1208 nv_engine(priv)->sclass = oclass->sclass;
4b223eef
BS
1209 return 0;
1210}
1211
30f4e087
BS
1212struct nvc0_graph_init *
1213nvc0_graph_init_mmio[] = {
1214 nvc0_graph_init_regs,
1215 nvc0_graph_init_unk40xx,
1216 nvc0_graph_init_unk44xx,
1217 nvc0_graph_init_unk78xx,
1218 nvc0_graph_init_unk60xx,
1219 nvc0_graph_init_unk58xx,
1220 nvc0_graph_init_unk80xx,
1221 nvc0_graph_init_gpc,
1222 nvc0_graph_init_tpc,
1223 nvc0_graph_init_unk88xx,
1224 NULL
1225};
1226
1227#include "fuc/hubnvc0.fuc.h"
1228
1229struct nvc0_graph_ucode
1230nvc0_graph_fecs_ucode = {
1231 .code.data = nvc0_grhub_code,
1232 .code.size = sizeof(nvc0_grhub_code),
1233 .data.data = nvc0_grhub_data,
1234 .data.size = sizeof(nvc0_grhub_data),
1235};
1236
1237#include "fuc/gpcnvc0.fuc.h"
1238
1239struct nvc0_graph_ucode
1240nvc0_graph_gpccs_ucode = {
1241 .code.data = nvc0_grgpc_code,
1242 .code.size = sizeof(nvc0_grgpc_code),
1243 .data.data = nvc0_grgpc_data,
1244 .data.size = sizeof(nvc0_grgpc_data),
1245};
1246
1247struct nouveau_oclass *
1248nvc0_graph_oclass = &(struct nvc0_graph_oclass) {
1249 .base.handle = NV_ENGINE(GR, 0xc0),
1250 .base.ofuncs = &(struct nouveau_ofuncs) {
ebb945a9
BS
1251 .ctor = nvc0_graph_ctor,
1252 .dtor = nvc0_graph_dtor,
1253 .init = nvc0_graph_init,
1254 .fini = _nouveau_graph_fini,
1255 },
30f4e087
BS
1256 .cclass = &nvc0_grctx_oclass,
1257 .sclass = nvc0_graph_sclass,
1258 .mmio = nvc0_graph_init_mmio,
1259 .fecs.ucode = &nvc0_graph_fecs_ucode,
1260 .gpccs.ucode = &nvc0_graph_gpccs_ucode,
1261}.base;