]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/gpu/drm/nouveau/nvc0_graph.c
drm/nvc0: implement pgraph engine hooks
[mv-sheeva.git] / drivers / gpu / drm / nouveau / nvc0_graph.c
1 /*
2  * Copyright 2010 Red Hat Inc.
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
25 #include <linux/firmware.h>
26
27 #include "drmP.h"
28
29 #include "nouveau_drv.h"
30 #include "nouveau_mm.h"
31 #include "nvc0_graph.h"
32
33 static void nvc0_graph_isr(struct drm_device *);
34 static int  nvc0_graph_unload_context_to(struct drm_device *dev, u64 chan);
35
36 void
37 nvc0_graph_fifo_access(struct drm_device *dev, bool enabled)
38 {
39 }
40
41 struct nouveau_channel *
42 nvc0_graph_channel(struct drm_device *dev)
43 {
44         return NULL;
45 }
46
47 static int
48 nvc0_graph_construct_context(struct nouveau_channel *chan)
49 {
50         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
51         struct nvc0_graph_priv *priv = dev_priv->engine.graph.priv;
52         struct nvc0_graph_chan *grch = chan->pgraph_ctx;
53         struct drm_device *dev = chan->dev;
54         int ret, i;
55         u32 *ctx;
56
57         ctx = kmalloc(priv->grctx_size, GFP_KERNEL);
58         if (!ctx)
59                 return -ENOMEM;
60
61         nvc0_graph_load_context(chan);
62
63         nv_wo32(grch->grctx, 0x1c, 1);
64         nv_wo32(grch->grctx, 0x20, 0);
65         nv_wo32(grch->grctx, 0x28, 0);
66         nv_wo32(grch->grctx, 0x2c, 0);
67         dev_priv->engine.instmem.flush(dev);
68
69         ret = nvc0_grctx_generate(chan);
70         if (ret) {
71                 kfree(ctx);
72                 return ret;
73         }
74
75         ret = nvc0_graph_unload_context_to(dev, chan->ramin->vinst);
76         if (ret) {
77                 kfree(ctx);
78                 return ret;
79         }
80
81         for (i = 0; i < priv->grctx_size; i += 4)
82                 ctx[i / 4] = nv_ro32(grch->grctx, i);
83
84         priv->grctx_vals = ctx;
85         return 0;
86 }
87
88 static int
89 nvc0_graph_create_context_mmio_list(struct nouveau_channel *chan)
90 {
91         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
92         struct nvc0_graph_priv *priv = dev_priv->engine.graph.priv;
93         struct nvc0_graph_chan *grch = chan->pgraph_ctx;
94         struct drm_device *dev = chan->dev;
95         int i = 0, gpc, tp, ret;
96         u32 magic;
97
98         ret = nouveau_gpuobj_new(dev, NULL, 0x2000, 256, NVOBJ_FLAG_VM,
99                                  &grch->unk408004);
100         if (ret)
101                 return ret;
102
103         ret = nouveau_gpuobj_new(dev, NULL, 0x8000, 256, NVOBJ_FLAG_VM,
104                                  &grch->unk40800c);
105         if (ret)
106                 return ret;
107
108         ret = nouveau_gpuobj_new(dev, NULL, 384 * 1024, 4096, NVOBJ_FLAG_VM,
109                                  &grch->unk418810);
110         if (ret)
111                 return ret;
112
113         ret = nouveau_gpuobj_new(dev, NULL, 0x1000, 0, NVOBJ_FLAG_VM,
114                                  &grch->mmio);
115         if (ret)
116                 return ret;
117
118
119         nv_wo32(grch->mmio, i++ * 4, 0x00408004);
120         nv_wo32(grch->mmio, i++ * 4, grch->unk408004->vinst >> 8);
121         nv_wo32(grch->mmio, i++ * 4, 0x00408008);
122         nv_wo32(grch->mmio, i++ * 4, 0x80000018);
123
124         nv_wo32(grch->mmio, i++ * 4, 0x0040800c);
125         nv_wo32(grch->mmio, i++ * 4, grch->unk40800c->vinst >> 8);
126         nv_wo32(grch->mmio, i++ * 4, 0x00408010);
127         nv_wo32(grch->mmio, i++ * 4, 0x80000000);
128
129         nv_wo32(grch->mmio, i++ * 4, 0x00418810);
130         nv_wo32(grch->mmio, i++ * 4, 0x80000000 | grch->unk418810->vinst >> 12);
131         nv_wo32(grch->mmio, i++ * 4, 0x00419848);
132         nv_wo32(grch->mmio, i++ * 4, 0x10000000 | grch->unk418810->vinst >> 12);
133
134         nv_wo32(grch->mmio, i++ * 4, 0x00419004);
135         nv_wo32(grch->mmio, i++ * 4, grch->unk40800c->vinst >> 8);
136         nv_wo32(grch->mmio, i++ * 4, 0x00419008);
137         nv_wo32(grch->mmio, i++ * 4, 0x00000000);
138
139         nv_wo32(grch->mmio, i++ * 4, 0x00418808);
140         nv_wo32(grch->mmio, i++ * 4, grch->unk408004->vinst >> 8);
141         nv_wo32(grch->mmio, i++ * 4, 0x0041880c);
142         nv_wo32(grch->mmio, i++ * 4, 0x80000018);
143
144         magic = 0x02180000;
145         nv_wo32(grch->mmio, i++ * 4, 0x00405830);
146         nv_wo32(grch->mmio, i++ * 4, magic);
147         for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
148                 for (tp = 0; tp < priv->tp_nr[gpc]; tp++, magic += 0x02fc) {
149                         u32 reg = 0x504520 + (gpc * 0x8000) + (tp * 0x0800);
150                         nv_wo32(grch->mmio, i++ * 4, reg);
151                         nv_wo32(grch->mmio, i++ * 4, magic);
152                 }
153         }
154
155         grch->mmio_nr = i / 2;
156         return 0;
157 }
158
159 int
160 nvc0_graph_create_context(struct nouveau_channel *chan)
161 {
162         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
163         struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
164         struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
165         struct nvc0_graph_priv *priv = pgraph->priv;
166         struct nvc0_graph_chan *grch;
167         struct drm_device *dev = chan->dev;
168         struct nouveau_gpuobj *grctx;
169         int ret, i;
170
171         chan->pgraph_ctx = kzalloc(sizeof(*grch), GFP_KERNEL);
172         if (!chan->pgraph_ctx)
173                 return -ENOMEM;
174         grch = chan->pgraph_ctx;
175
176         ret = nouveau_gpuobj_new(dev, NULL, priv->grctx_size, 256,
177                                  NVOBJ_FLAG_VM | NVOBJ_FLAG_ZERO_ALLOC,
178                                  &grch->grctx);
179         if (ret)
180                 goto error;
181         chan->ramin_grctx = grch->grctx;
182         grctx = grch->grctx;
183
184         ret = nvc0_graph_create_context_mmio_list(chan);
185         if (ret)
186                 goto error;
187
188         nv_wo32(chan->ramin, 0x0210, lower_32_bits(grctx->vinst) | 4);
189         nv_wo32(chan->ramin, 0x0214, upper_32_bits(grctx->vinst));
190         pinstmem->flush(dev);
191
192         if (!priv->grctx_vals) {
193                 ret = nvc0_graph_construct_context(chan);
194                 if (ret)
195                         goto error;
196         }
197
198         for (i = 0; i < priv->grctx_size; i += 4)
199                 nv_wo32(grctx, i, priv->grctx_vals[i / 4]);
200
201         nv_wo32(grctx, 0xf4, 0);
202         nv_wo32(grctx, 0xf8, 0);
203         nv_wo32(grctx, 0x10, grch->mmio_nr);
204         nv_wo32(grctx, 0x14, lower_32_bits(grch->mmio->vinst));
205         nv_wo32(grctx, 0x18, upper_32_bits(grch->mmio->vinst));
206         nv_wo32(grctx, 0x1c, 1);
207         nv_wo32(grctx, 0x20, 0);
208         nv_wo32(grctx, 0x28, 0);
209         nv_wo32(grctx, 0x2c, 0);
210         pinstmem->flush(dev);
211         return 0;
212
213 error:
214         pgraph->destroy_context(chan);
215         return ret;
216 }
217
218 void
219 nvc0_graph_destroy_context(struct nouveau_channel *chan)
220 {
221         struct nvc0_graph_chan *grch;
222
223         grch = chan->pgraph_ctx;
224         chan->pgraph_ctx = NULL;
225         if (!grch)
226                 return;
227
228         nouveau_gpuobj_ref(NULL, &grch->mmio);
229         nouveau_gpuobj_ref(NULL, &grch->unk418810);
230         nouveau_gpuobj_ref(NULL, &grch->unk40800c);
231         nouveau_gpuobj_ref(NULL, &grch->unk408004);
232         nouveau_gpuobj_ref(NULL, &grch->grctx);
233         chan->ramin_grctx = NULL;
234 }
235
236 int
237 nvc0_graph_load_context(struct nouveau_channel *chan)
238 {
239         struct drm_device *dev = chan->dev;
240
241         nv_wr32(dev, 0x409840, 0x00000030);
242         nv_wr32(dev, 0x409500, 0x80000000 | chan->ramin->vinst >> 12);
243         nv_wr32(dev, 0x409504, 0x00000003);
244         if (!nv_wait(dev, 0x409800, 0x00000010, 0x00000010))
245                 NV_ERROR(dev, "PGRAPH: load_ctx timeout\n");
246
247         printk(KERN_ERR "load_ctx 0x%08x\n", nv_rd32(dev, 0x409b00));
248         return 0;
249 }
250
251 static int
252 nvc0_graph_unload_context_to(struct drm_device *dev, u64 chan)
253 {
254         nv_wr32(dev, 0x409840, 0x00000003);
255         nv_wr32(dev, 0x409500, 0x80000000 | chan >> 12);
256         nv_wr32(dev, 0x409504, 0x00000009);
257         if (!nv_wait(dev, 0x409800, 0x00000001, 0x00000000)) {
258                 NV_ERROR(dev, "PGRAPH: unload_ctx timeout\n");
259                 return -EBUSY;
260         }
261
262         return 0;
263 }
264
265 int
266 nvc0_graph_unload_context(struct drm_device *dev)
267 {
268         u64 inst = (u64)(nv_rd32(dev, 0x409b00) & 0x0fffffff) << 12;
269         return nvc0_graph_unload_context_to(dev, inst);
270 }
271
272 static void
273 nvc0_graph_destroy(struct drm_device *dev)
274 {
275         struct drm_nouveau_private *dev_priv = dev->dev_private;
276         struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
277         struct nvc0_graph_priv *priv;
278
279         priv = pgraph->priv;
280         if (!priv)
281                 return;
282
283         nouveau_irq_unregister(dev, 12);
284
285         nouveau_gpuobj_ref(NULL, &priv->unk4188b8);
286         nouveau_gpuobj_ref(NULL, &priv->unk4188b4);
287
288         if (priv->grctx_vals)
289                 kfree(priv->grctx_vals);
290         kfree(priv);
291 }
292
293 void
294 nvc0_graph_takedown(struct drm_device *dev)
295 {
296         nvc0_graph_destroy(dev);
297 }
298
299 static int
300 nvc0_graph_create(struct drm_device *dev)
301 {
302         struct drm_nouveau_private *dev_priv = dev->dev_private;
303         struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
304         struct nvc0_graph_priv *priv;
305         int ret, gpc, i;
306
307         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
308         if (!priv)
309                 return -ENOMEM;
310         pgraph->priv = priv;
311
312         ret = nouveau_gpuobj_new(dev, NULL, 0x1000, 256, 0, &priv->unk4188b4);
313         if (ret)
314                 goto error;
315
316         ret = nouveau_gpuobj_new(dev, NULL, 0x1000, 256, 0, &priv->unk4188b8);
317         if (ret)
318                 goto error;
319
320         for (i = 0; i < 0x1000; i += 4) {
321                 nv_wo32(priv->unk4188b4, i, 0x00000010);
322                 nv_wo32(priv->unk4188b8, i, 0x00000010);
323         }
324
325         priv->gpc_nr  =  nv_rd32(dev, 0x409604) & 0x0000001f;
326         priv->rop_nr = (nv_rd32(dev, 0x409604) & 0x001f0000) >> 16;
327         for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
328                 priv->tp_nr[gpc] = nv_rd32(dev, GPC_UNIT(gpc, 0x2608));
329                 priv->tp_total += priv->tp_nr[gpc];
330         }
331
332         /*XXX: these need figuring out... */
333         switch (dev_priv->chipset) {
334         case 0xc0:
335                 if (priv->tp_total == 11) { /* 465, 3/4/4/0, 4 */
336                         priv->magic_not_rop_nr = 0x07;
337                         priv->magic419bd0      = 0x0a360000;
338                         priv->magic419be4      = 0x04c33a54;
339                         /* filled values up to tp_total, the rest 0 */
340                         priv->magicgpc980[0]   = 0x22111000;
341                         priv->magicgpc980[1]   = 0x00000233;
342                         priv->magicgpc980[2]   = 0x00000000;
343                         priv->magicgpc980[3]   = 0x00000000;
344                         priv->magicgpc918      = 0x000ba2e9;
345                 } else
346                 if (priv->tp_total == 14) { /* 470, 3/3/4/4, 5 */
347                         priv->magic_not_rop_nr = 0x05;
348                         priv->magic419bd0      = 0x043c0000;
349                         priv->magic419be4      = 0x09041208;
350                         priv->magicgpc980[0]   = 0x11110000;
351                         priv->magicgpc980[1]   = 0x00233222;
352                         priv->magicgpc980[2]   = 0x00000000;
353                         priv->magicgpc980[3]   = 0x00000000;
354                         priv->magicgpc918      = 0x00092493;
355                 } else
356                 if (priv->tp_total == 15) { /* 480, 3/4/4/4, 6 */
357                         priv->magic_not_rop_nr = 0x06;
358                         priv->magic419bd0      = 0x023e0000;
359                         priv->magic419be4      = 0x10414104;
360                         priv->magicgpc980[0]   = 0x11110000;
361                         priv->magicgpc980[1]   = 0x03332222;
362                         priv->magicgpc980[2]   = 0x00000000;
363                         priv->magicgpc980[3]   = 0x00000000;
364                         priv->magicgpc918      = 0x00088889;
365                 }
366                 break;
367         case 0xc3: /* 450, 4/0/0/0, 2 */
368                 priv->magic_not_rop_nr = 0x03;
369                 priv->magic419bd0      = 0x00500000;
370                 priv->magic419be4      = 0x00000000;
371                 priv->magicgpc980[0]   = 0x00003210;
372                 priv->magicgpc980[1]   = 0x00000000;
373                 priv->magicgpc980[2]   = 0x00000000;
374                 priv->magicgpc980[3]   = 0x00000000;
375                 priv->magicgpc918      = 0x00200000;
376                 break;
377         case 0xc4: /* 460, 3/4/0/0, 4 */
378                 priv->magic_not_rop_nr = 0x01;
379                 priv->magic419bd0      = 0x045c0000;
380                 priv->magic419be4      = 0x09041208;
381                 priv->magicgpc980[0]   = 0x02321100;
382                 priv->magicgpc980[1]   = 0x00000000;
383                 priv->magicgpc980[2]   = 0x00000000;
384                 priv->magicgpc980[3]   = 0x00000000;
385                 priv->magicgpc918      = 0x00124925;
386                 break;
387         }
388
389         if (!priv->magic419bd0) {
390                 NV_ERROR(dev, "PGRAPH: unknown config: %d/%d/%d/%d, %d\n",
391                          priv->tp_nr[0], priv->tp_nr[1], priv->tp_nr[2],
392                          priv->tp_nr[3], priv->rop_nr);
393                 /* use 0xc3's values... */
394                 priv->magic_not_rop_nr = 0x03;
395                 priv->magic419bd0      = 0x00500000;
396                 priv->magic419be4      = 0x00000000;
397                 priv->magicgpc980[0]   = 0x00003210;
398                 priv->magicgpc980[1]   = 0x00000000;
399                 priv->magicgpc980[2]   = 0x00000000;
400                 priv->magicgpc980[3]   = 0x00000000;
401                 priv->magicgpc918      = 0x00200000;
402         }
403
404         nouveau_irq_register(dev, 12, nvc0_graph_isr);
405         NVOBJ_CLASS(dev, 0x902d, GR); /* 2D */
406         NVOBJ_CLASS(dev, 0x9039, GR); /* M2MF */
407         NVOBJ_CLASS(dev, 0x9097, GR); /* 3D */
408         NVOBJ_CLASS(dev, 0x90c0, GR); /* COMPUTE */
409         return 0;
410
411 error:
412         nvc0_graph_destroy(dev);
413         return ret;
414 }
415
416 static void
417 nvc0_graph_init_obj418880(struct drm_device *dev)
418 {
419         struct drm_nouveau_private *dev_priv = dev->dev_private;
420         struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
421         struct nvc0_graph_priv *priv = pgraph->priv;
422         int i;
423
424         nv_wr32(dev, GPC_BCAST(0x0880), 0x00000000);
425         nv_wr32(dev, GPC_BCAST(0x08a4), 0x00000000);
426         for (i = 0; i < 4; i++)
427                 nv_wr32(dev, GPC_BCAST(0x0888) + (i * 4), 0x00000000);
428         nv_wr32(dev, GPC_BCAST(0x08b4), priv->unk4188b4->vinst >> 8);
429         nv_wr32(dev, GPC_BCAST(0x08b8), priv->unk4188b8->vinst >> 8);
430 }
431
432 static void
433 nvc0_graph_init_regs(struct drm_device *dev)
434 {
435         nv_wr32(dev, 0x400080, 0x003083c2);
436         nv_wr32(dev, 0x400088, 0x00006fe7);
437         nv_wr32(dev, 0x40008c, 0x00000000);
438         nv_wr32(dev, 0x400090, 0x00000030);
439         nv_wr32(dev, 0x40013c, 0x013901f7);
440         nv_wr32(dev, 0x400140, 0x00000100);
441         nv_wr32(dev, 0x400144, 0x00000000);
442         nv_wr32(dev, 0x400148, 0x00000110);
443         nv_wr32(dev, 0x400138, 0x00000000);
444         nv_wr32(dev, 0x400130, 0x00000000);
445         nv_wr32(dev, 0x400134, 0x00000000);
446         nv_wr32(dev, 0x400124, 0x00000002);
447 }
448
449 static void
450 nvc0_graph_init_gpc_0(struct drm_device *dev)
451 {
452         struct drm_nouveau_private *dev_priv = dev->dev_private;
453         struct nvc0_graph_priv *priv = dev_priv->engine.graph.priv;
454         int gpc;
455         
456         //      TP      ROP UNKVAL(magic_not_rop_nr)
457         // 450: 4/0/0/0 2        3
458         // 460: 3/4/0/0 4        1
459         // 465: 3/4/4/0 4        7
460         // 470: 3/3/4/4 5        5
461         // 480: 3/4/4/4 6        6
462
463         // magicgpc918
464         // 450: 00200000 00000000001000000000000000000000
465         // 460: 00124925 00000000000100100100100100100101
466         // 465: 000ba2e9 00000000000010111010001011101001
467         // 470: 00092493 00000000000010010010010010010011
468         // 480: 00088889 00000000000010001000100010001001
469
470         /* filled values up to tp_total, remainder 0 */
471         // 450: 00003210 00000000 00000000 00000000
472         // 460: 02321100 00000000 00000000 00000000
473         // 465: 22111000 00000233 00000000 00000000
474         // 470: 11110000 00233222 00000000 00000000
475         // 480: 11110000 03332222 00000000 00000000
476         
477         nv_wr32(dev, GPC_BCAST(0x0980), priv->magicgpc980[0]);
478         nv_wr32(dev, GPC_BCAST(0x0984), priv->magicgpc980[1]);
479         nv_wr32(dev, GPC_BCAST(0x0988), priv->magicgpc980[2]);
480         nv_wr32(dev, GPC_BCAST(0x098c), priv->magicgpc980[3]);
481
482         for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
483                 nv_wr32(dev, GPC_UNIT(gpc, 0x0914), priv->magic_not_rop_nr << 8 |
484                                                   priv->tp_nr[gpc]);
485                 nv_wr32(dev, GPC_UNIT(gpc, 0x0910), 0x00040000 | priv->tp_total);
486                 nv_wr32(dev, GPC_UNIT(gpc, 0x0918), priv->magicgpc918);
487         }
488
489         nv_wr32(dev, GPC_BCAST(0x1bd4), priv->magicgpc918);
490         nv_wr32(dev, GPC_BCAST(0x08ac), priv->rop_nr);
491 }
492
493 static void
494 nvc0_graph_init_units(struct drm_device *dev)
495 {
496         nv_wr32(dev, 0x409c24, 0x000f0000);
497         nv_wr32(dev, 0x404000, 0xc0000000); /* DISPATCH */
498         nv_wr32(dev, 0x404600, 0xc0000000); /* M2MF */
499         nv_wr32(dev, 0x408030, 0xc0000000);
500         nv_wr32(dev, 0x40601c, 0xc0000000);
501         nv_wr32(dev, 0x404490, 0xc0000000); /* MACRO */
502         nv_wr32(dev, 0x406018, 0xc0000000);
503         nv_wr32(dev, 0x405840, 0xc0000000);
504         nv_wr32(dev, 0x405844, 0x00ffffff);
505         nv_mask(dev, 0x419cc0, 0x00000008, 0x00000008);
506         nv_mask(dev, 0x419eb4, 0x00001000, 0x00001000);
507 }
508
509 static void
510 nvc0_graph_init_gpc_1(struct drm_device *dev)
511 {
512         struct drm_nouveau_private *dev_priv = dev->dev_private;
513         struct nvc0_graph_priv *priv = dev_priv->engine.graph.priv;
514         int gpc, tp;
515
516         for (gpc = 0; gpc < priv->gpc_nr; gpc++) {
517                 nv_wr32(dev, GPC_UNIT(gpc, 0x0420), 0xc0000000);
518                 nv_wr32(dev, GPC_UNIT(gpc, 0x0900), 0xc0000000);
519                 nv_wr32(dev, GPC_UNIT(gpc, 0x1028), 0xc0000000);
520                 nv_wr32(dev, GPC_UNIT(gpc, 0x0824), 0xc0000000);
521                 for (tp = 0; tp < priv->tp_nr[gpc]; tp++) {
522                         nv_wr32(dev, TP_UNIT(gpc, tp, 0x508), 0xffffffff);
523                         nv_wr32(dev, TP_UNIT(gpc, tp, 0x50c), 0xffffffff);
524                         nv_wr32(dev, TP_UNIT(gpc, tp, 0x224), 0xc0000000);
525                         nv_wr32(dev, TP_UNIT(gpc, tp, 0x48c), 0xc0000000);
526                         nv_wr32(dev, TP_UNIT(gpc, tp, 0x084), 0xc0000000);
527                         nv_wr32(dev, TP_UNIT(gpc, tp, 0xe44), 0x001ffffe);
528                         nv_wr32(dev, TP_UNIT(gpc, tp, 0xe4c), 0x0000000f);
529                 }
530                 nv_wr32(dev, GPC_UNIT(gpc, 0x2c90), 0xffffffff);
531                 nv_wr32(dev, GPC_UNIT(gpc, 0x2c94), 0xffffffff);
532         }
533 }
534
535 static void
536 nvc0_graph_init_rop(struct drm_device *dev)
537 {
538         struct drm_nouveau_private *dev_priv = dev->dev_private;
539         struct nvc0_graph_priv *priv = dev_priv->engine.graph.priv;
540         int rop;
541
542         for (rop = 0; rop < priv->rop_nr; rop++) {
543                 nv_wr32(dev, ROP_UNIT(rop, 0x144), 0xc0000000);
544                 nv_wr32(dev, ROP_UNIT(rop, 0x070), 0xc0000000);
545                 nv_wr32(dev, ROP_UNIT(rop, 0x204), 0xffffffff);
546                 nv_wr32(dev, ROP_UNIT(rop, 0x208), 0xffffffff);
547         }
548 }
549
550 static int
551 nvc0_fuc_load_fw(struct drm_device *dev, u32 fuc_base,
552                  const char *code_fw, const char *data_fw)
553 {
554         const struct firmware *fw;
555         char name[32];
556         int ret, i;
557
558         snprintf(name, sizeof(name), "nouveau/%s", data_fw);
559         ret = request_firmware(&fw, name, &dev->pdev->dev);
560         if (ret) {
561                 NV_ERROR(dev, "failed to load %s\n", data_fw);
562                 return ret;
563         }
564
565         nv_wr32(dev, fuc_base + 0x01c0, 0x01000000);
566         for (i = 0; i < fw->size / 4; i++)
567                 nv_wr32(dev, fuc_base + 0x01c4, ((u32 *)fw->data)[i]);
568         release_firmware(fw);
569
570         snprintf(name, sizeof(name), "nouveau/%s", code_fw);
571         ret = request_firmware(&fw, name, &dev->pdev->dev);
572         if (ret) {
573                 NV_ERROR(dev, "failed to load %s\n", code_fw);
574                 return ret;
575         }
576
577         nv_wr32(dev, fuc_base + 0x0180, 0x01000000);
578         for (i = 0; i < fw->size / 4; i++) {
579                 if ((i & 0x3f) == 0)
580                         nv_wr32(dev, fuc_base + 0x0188, i >> 6);
581                 nv_wr32(dev, fuc_base + 0x0184, ((u32 *)fw->data)[i]);
582         }
583         release_firmware(fw);
584
585         return 0;
586 }
587
588 static int
589 nvc0_graph_init_ctxctl(struct drm_device *dev)
590 {
591         struct drm_nouveau_private *dev_priv = dev->dev_private;
592         struct nvc0_graph_priv *priv = dev_priv->engine.graph.priv;
593         u32 r000260;
594         int ret;
595
596         /* load fuc microcode */
597         r000260 = nv_mask(dev, 0x000260, 0x00000001, 0x00000000);
598         ret = nvc0_fuc_load_fw(dev, 0x409000, "fuc409c", "fuc409d");
599         if (ret == 0)
600                 nvc0_fuc_load_fw(dev, 0x41a000, "fuc41ac", "fuc41ad");
601         nv_wr32(dev, 0x000260, r000260);
602
603         if (ret)
604                 return ret;
605
606         /* start both of them running */
607         nv_wr32(dev, 0x409840, 0xffffffff);
608         nv_wr32(dev, 0x41a10c, 0x00000000);
609         nv_wr32(dev, 0x40910c, 0x00000000);
610         nv_wr32(dev, 0x41a100, 0x00000002);
611         nv_wr32(dev, 0x409100, 0x00000002);
612         if (!nv_wait(dev, 0x409800, 0x00000001, 0x00000001))
613                 NV_INFO(dev, "0x409800 wait failed\n");
614
615         nv_wr32(dev, 0x409840, 0xffffffff);
616         nv_wr32(dev, 0x409500, 0x7fffffff);
617         nv_wr32(dev, 0x409504, 0x00000021);
618
619         nv_wr32(dev, 0x409840, 0xffffffff);
620         nv_wr32(dev, 0x409500, 0x00000000);
621         nv_wr32(dev, 0x409504, 0x00000010);
622         if (!nv_wait_ne(dev, 0x409800, 0xffffffff, 0x00000000)) {
623                 NV_ERROR(dev, "fuc09 req 0x10 timeout\n");
624                 return -EBUSY;
625         }
626         priv->grctx_size = nv_rd32(dev, 0x409800);
627
628         nv_wr32(dev, 0x409840, 0xffffffff);
629         nv_wr32(dev, 0x409500, 0x00000000);
630         nv_wr32(dev, 0x409504, 0x00000016);
631         if (!nv_wait_ne(dev, 0x409800, 0xffffffff, 0x00000000)) {
632                 NV_ERROR(dev, "fuc09 req 0x16 timeout\n");
633                 return -EBUSY;
634         }
635
636         nv_wr32(dev, 0x409840, 0xffffffff);
637         nv_wr32(dev, 0x409500, 0x00000000);
638         nv_wr32(dev, 0x409504, 0x00000025);
639         if (!nv_wait_ne(dev, 0x409800, 0xffffffff, 0x00000000)) {
640                 NV_ERROR(dev, "fuc09 req 0x25 timeout\n");
641                 return -EBUSY;
642         }
643
644         return 0;
645 }
646
647 int
648 nvc0_graph_init(struct drm_device *dev)
649 {
650         struct drm_nouveau_private *dev_priv = dev->dev_private;
651         struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
652         struct nvc0_graph_priv *priv;
653         int ret;
654
655         dev_priv->engine.graph.accel_blocked = true;
656
657         switch (dev_priv->chipset) {
658         case 0xc0:
659         case 0xc3:
660         case 0xc4:
661                 break;
662         default:
663                 NV_ERROR(dev, "PGRAPH: unsupported chipset, please report!\n");
664                 if (nouveau_noaccel != 0)
665                         return 0;
666                 break;
667         }
668
669         nv_mask(dev, 0x000200, 0x18001000, 0x00000000);
670         nv_mask(dev, 0x000200, 0x18001000, 0x18001000);
671
672         if (!pgraph->priv) {
673                 ret = nvc0_graph_create(dev);
674                 if (ret)
675                         return ret;
676         }
677         priv = pgraph->priv;
678
679         nvc0_graph_init_obj418880(dev);
680         nvc0_graph_init_regs(dev);
681         //nvc0_graph_init_unitplemented_magics(dev);
682         nvc0_graph_init_gpc_0(dev);
683         //nvc0_graph_init_unitplemented_c242(dev);
684
685         nv_wr32(dev, 0x400500, 0x00010001);
686         nv_wr32(dev, 0x400100, 0xffffffff);
687         nv_wr32(dev, 0x40013c, 0xffffffff);
688
689         nvc0_graph_init_units(dev);
690         nvc0_graph_init_gpc_1(dev);
691         nvc0_graph_init_rop(dev);
692
693         nv_wr32(dev, 0x400108, 0xffffffff);
694         nv_wr32(dev, 0x400138, 0xffffffff);
695         nv_wr32(dev, 0x400118, 0xffffffff);
696         nv_wr32(dev, 0x400130, 0xffffffff);
697         nv_wr32(dev, 0x40011c, 0xffffffff);
698         nv_wr32(dev, 0x400134, 0xffffffff);
699         nv_wr32(dev, 0x400054, 0x34ce3464);
700
701         ret = nvc0_graph_init_ctxctl(dev);
702         if (ret)
703                 return ret;
704
705         dev_priv->engine.graph.accel_blocked = false;
706         return 0;
707 }
708
709 static struct nouveau_enum nvc0_graph_data_error[] = {
710         { 5, "INVALID_ENUM" },
711         {}
712 };
713
714 static int
715 nvc0_graph_isr_chid(struct drm_device *dev, u64 inst)
716 {
717         struct drm_nouveau_private *dev_priv = dev->dev_private;
718         struct nouveau_channel *chan;
719         unsigned long flags;
720         int i;
721
722         spin_lock_irqsave(&dev_priv->channels.lock, flags);
723         for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
724                 chan = dev_priv->channels.ptr[i];
725                 if (!chan || !chan->ramin)
726                         continue;
727
728                 if (inst == chan->ramin->vinst)
729                         break;
730         }
731         spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
732         return i;
733 }
734
735 static void
736 nvc0_graph_isr(struct drm_device *dev)
737 {
738         u64 inst = (u64)(nv_rd32(dev, 0x409b00) & 0x0fffffff) << 12;
739         u32 chid = nvc0_graph_isr_chid(dev, inst);
740         u32 stat = nv_rd32(dev, 0x400100);
741         u32 addr = nv_rd32(dev, 0x400704);
742         u32 mthd = (addr & 0x00003ffc);
743         u32 subc = (addr & 0x00070000) >> 16;
744         u32 data = nv_rd32(dev, 0x400708);
745         u32 code = nv_rd32(dev, 0x400110);
746         u32 class = nv_rd32(dev, 0x404200 + (subc * 4));
747
748         if (stat & 0x00000010) {
749                 NV_INFO(dev, "PGRAPH: ILLEGAL_MTHD ch %d [0x%010llx] subc %d "
750                              "class 0x%04x mthd 0x%04x data 0x%08x\n",
751                         chid, inst, subc, class, mthd, data);
752                 nv_wr32(dev, 0x400100, 0x00000010);
753                 stat &= ~0x00000010;
754         }
755
756         if (stat & 0x00100000) {
757                 NV_INFO(dev, "PGRAPH: DATA_ERROR [");
758                 nouveau_enum_print(nvc0_graph_data_error, code);
759                 printk("] ch %d [0x%010llx] subc %d class 0x%04x "
760                        "mthd 0x%04x data 0x%08x\n",
761                        chid, inst, subc, class, mthd, data);
762                 nv_wr32(dev, 0x400100, 0x00100000);
763                 stat &= ~0x00100000;
764         }
765
766         if (stat & 0x00080000) {
767                 u32 ustat = nv_rd32(dev, 0x409c18);
768
769                 NV_INFO(dev, "PGRAPH: CTXCTRL ustat 0x%08x\n", ustat);
770
771                 nv_wr32(dev, 0x409c20, ustat);
772                 nv_wr32(dev, 0x400100, 0x00080000);
773                 stat &= ~0x00080000;
774         }
775
776         if (stat) {
777                 NV_INFO(dev, "PGRAPH: unknown stat 0x%08x\n", stat);
778                 nv_wr32(dev, 0x400100, stat);
779         }
780
781         nv_wr32(dev, 0x400500, 0x00010001);
782 }