2 * Copyright (C) 2007 Ben Skeggs.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "nouveau_drv.h"
29 #include "nouveau_fifo.h"
30 #include "nouveau_ramht.h"
32 struct nv40_graph_engine {
33 struct nouveau_exec_engine base;
38 nv40_graph_context_new(struct nouveau_channel *chan, int engine)
40 struct nv40_graph_engine *pgraph = nv_engine(chan->dev, engine);
41 struct drm_device *dev = chan->dev;
42 struct drm_nouveau_private *dev_priv = dev->dev_private;
43 struct nouveau_gpuobj *grctx = NULL;
47 ret = nouveau_gpuobj_new(dev, NULL, pgraph->grctx_size, 16,
48 NVOBJ_FLAG_ZERO_ALLOC, &grctx);
52 /* Initialise default context values */
53 nv40_grctx_fill(dev, grctx);
54 nv_wo32(grctx, 0, grctx->vinst);
56 /* init grctx pointer in ramfc, and on PFIFO if channel is
57 * already active there
59 spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
60 nv_wo32(chan->ramfc, 0x38, grctx->vinst >> 4);
61 nv_mask(dev, 0x002500, 0x00000001, 0x00000000);
62 if ((nv_rd32(dev, 0x003204) & 0x0000001f) == chan->id)
63 nv_wr32(dev, 0x0032e0, grctx->vinst >> 4);
64 nv_mask(dev, 0x002500, 0x00000001, 0x00000001);
65 spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
67 chan->engctx[engine] = grctx;
72 nv40_graph_context_del(struct nouveau_channel *chan, int engine)
74 struct nouveau_gpuobj *grctx = chan->engctx[engine];
75 struct drm_device *dev = chan->dev;
76 struct drm_nouveau_private *dev_priv = dev->dev_private;
77 u32 inst = 0x01000000 | (grctx->pinst >> 4);
80 spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
81 nv_mask(dev, 0x400720, 0x00000000, 0x00000001);
82 if (nv_rd32(dev, 0x40032c) == inst)
83 nv_mask(dev, 0x40032c, 0x01000000, 0x00000000);
84 if (nv_rd32(dev, 0x400330) == inst)
85 nv_mask(dev, 0x400330, 0x01000000, 0x00000000);
86 nv_mask(dev, 0x400720, 0x00000001, 0x00000001);
87 spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
89 /* Free the context resources */
90 nouveau_gpuobj_ref(NULL, &grctx);
91 chan->engctx[engine] = NULL;
95 nv40_graph_object_new(struct nouveau_channel *chan, int engine,
96 u32 handle, u16 class)
98 struct drm_device *dev = chan->dev;
99 struct nouveau_gpuobj *obj = NULL;
102 ret = nouveau_gpuobj_new(dev, chan, 20, 16, NVOBJ_FLAG_ZERO_FREE, &obj);
108 nv_wo32(obj, 0x00, class);
109 nv_wo32(obj, 0x04, 0x00000000);
111 nv_wo32(obj, 0x08, 0x00000000);
113 nv_wo32(obj, 0x08, 0x01000000);
115 nv_wo32(obj, 0x0c, 0x00000000);
116 nv_wo32(obj, 0x10, 0x00000000);
118 ret = nouveau_ramht_insert(chan, handle, obj);
119 nouveau_gpuobj_ref(NULL, &obj);
124 nv40_graph_set_tile_region(struct drm_device *dev, int i)
126 struct drm_nouveau_private *dev_priv = dev->dev_private;
127 struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
129 switch (dev_priv->chipset) {
131 case 0x41: /* guess */
134 case 0x45: /* guess */
136 nv_wr32(dev, NV20_PGRAPH_TSIZE(i), tile->pitch);
137 nv_wr32(dev, NV20_PGRAPH_TLIMIT(i), tile->limit);
138 nv_wr32(dev, NV20_PGRAPH_TILE(i), tile->addr);
139 nv_wr32(dev, NV40_PGRAPH_TSIZE1(i), tile->pitch);
140 nv_wr32(dev, NV40_PGRAPH_TLIMIT1(i), tile->limit);
141 nv_wr32(dev, NV40_PGRAPH_TILE1(i), tile->addr);
145 nv_wr32(dev, NV20_PGRAPH_TSIZE(i), tile->pitch);
146 nv_wr32(dev, NV20_PGRAPH_TLIMIT(i), tile->limit);
147 nv_wr32(dev, NV20_PGRAPH_TILE(i), tile->addr);
156 nv_wr32(dev, NV47_PGRAPH_TSIZE(i), tile->pitch);
157 nv_wr32(dev, NV47_PGRAPH_TLIMIT(i), tile->limit);
158 nv_wr32(dev, NV47_PGRAPH_TILE(i), tile->addr);
159 nv_wr32(dev, NV40_PGRAPH_TSIZE1(i), tile->pitch);
160 nv_wr32(dev, NV40_PGRAPH_TLIMIT1(i), tile->limit);
161 nv_wr32(dev, NV40_PGRAPH_TILE1(i), tile->addr);
176 nv40_graph_init(struct drm_device *dev, int engine)
178 struct nv40_graph_engine *pgraph = nv_engine(dev, engine);
179 struct drm_nouveau_private *dev_priv = dev->dev_private;
180 struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
184 nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) &
185 ~NV_PMC_ENABLE_PGRAPH);
186 nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
187 NV_PMC_ENABLE_PGRAPH);
189 /* generate and upload context program */
190 nv40_grctx_init(dev, &pgraph->grctx_size);
192 /* No context present currently */
193 nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, 0x00000000);
195 nv_wr32(dev, NV03_PGRAPH_INTR , 0xFFFFFFFF);
196 nv_wr32(dev, NV40_PGRAPH_INTR_EN, 0xFFFFFFFF);
198 nv_wr32(dev, NV04_PGRAPH_DEBUG_0, 0xFFFFFFFF);
199 nv_wr32(dev, NV04_PGRAPH_DEBUG_0, 0x00000000);
200 nv_wr32(dev, NV04_PGRAPH_DEBUG_1, 0x401287c0);
201 nv_wr32(dev, NV04_PGRAPH_DEBUG_3, 0xe0de8055);
202 nv_wr32(dev, NV10_PGRAPH_DEBUG_4, 0x00008000);
203 nv_wr32(dev, NV04_PGRAPH_LIMIT_VIOL_PIX, 0x00be3c5f);
205 nv_wr32(dev, NV10_PGRAPH_CTX_CONTROL, 0x10010100);
206 nv_wr32(dev, NV10_PGRAPH_STATE , 0xFFFFFFFF);
208 j = nv_rd32(dev, 0x1540) & 0xff;
210 for (i = 0; !(j & 1); j >>= 1, i++)
212 nv_wr32(dev, 0x405000, i);
215 if (dev_priv->chipset == 0x40) {
216 nv_wr32(dev, 0x4009b0, 0x83280fff);
217 nv_wr32(dev, 0x4009b4, 0x000000a0);
219 nv_wr32(dev, 0x400820, 0x83280eff);
220 nv_wr32(dev, 0x400824, 0x000000a0);
223 switch (dev_priv->chipset) {
226 nv_wr32(dev, 0x4009b8, 0x0078e366);
227 nv_wr32(dev, 0x4009bc, 0x0000014c);
230 case 0x42: /* pciid also 0x00Cx */
231 /* case 0x0120: XXX (pciid) */
232 nv_wr32(dev, 0x400828, 0x007596ff);
233 nv_wr32(dev, 0x40082c, 0x00000108);
236 nv_wr32(dev, 0x400828, 0x0072cb77);
237 nv_wr32(dev, 0x40082c, 0x00000108);
242 case 0x4c: /* G7x-based C51 */
244 nv_wr32(dev, 0x400860, 0);
245 nv_wr32(dev, 0x400864, 0);
250 nv_wr32(dev, 0x400828, 0x07830610);
251 nv_wr32(dev, 0x40082c, 0x0000016A);
257 nv_wr32(dev, 0x400b38, 0x2ffff800);
258 nv_wr32(dev, 0x400b3c, 0x00006000);
260 /* Tiling related stuff. */
261 switch (dev_priv->chipset) {
264 nv_wr32(dev, 0x400bc4, 0x1003d888);
265 nv_wr32(dev, 0x400bbc, 0xb7a7b500);
268 nv_wr32(dev, 0x400bc4, 0x0000e024);
269 nv_wr32(dev, 0x400bbc, 0xb7a7b520);
274 nv_wr32(dev, 0x400bc4, 0x1003d888);
275 nv_wr32(dev, 0x400bbc, 0xb7a7b540);
281 /* Turn all the tiling regions off. */
282 for (i = 0; i < pfb->num_tiles; i++)
283 nv40_graph_set_tile_region(dev, i);
285 /* begin RAM config */
286 vramsz = pci_resource_len(dev->pdev, 0) - 1;
287 switch (dev_priv->chipset) {
289 nv_wr32(dev, 0x4009A4, nv_rd32(dev, NV04_PFB_CFG0));
290 nv_wr32(dev, 0x4009A8, nv_rd32(dev, NV04_PFB_CFG1));
291 nv_wr32(dev, 0x4069A4, nv_rd32(dev, NV04_PFB_CFG0));
292 nv_wr32(dev, 0x4069A8, nv_rd32(dev, NV04_PFB_CFG1));
293 nv_wr32(dev, 0x400820, 0);
294 nv_wr32(dev, 0x400824, 0);
295 nv_wr32(dev, 0x400864, vramsz);
296 nv_wr32(dev, 0x400868, vramsz);
299 switch (dev_priv->chipset) {
307 nv_wr32(dev, 0x4009F0, nv_rd32(dev, NV04_PFB_CFG0));
308 nv_wr32(dev, 0x4009F4, nv_rd32(dev, NV04_PFB_CFG1));
311 nv_wr32(dev, 0x400DF0, nv_rd32(dev, NV04_PFB_CFG0));
312 nv_wr32(dev, 0x400DF4, nv_rd32(dev, NV04_PFB_CFG1));
315 nv_wr32(dev, 0x4069F0, nv_rd32(dev, NV04_PFB_CFG0));
316 nv_wr32(dev, 0x4069F4, nv_rd32(dev, NV04_PFB_CFG1));
317 nv_wr32(dev, 0x400840, 0);
318 nv_wr32(dev, 0x400844, 0);
319 nv_wr32(dev, 0x4008A0, vramsz);
320 nv_wr32(dev, 0x4008A4, vramsz);
328 nv40_graph_fini(struct drm_device *dev, int engine, bool suspend)
330 u32 inst = nv_rd32(dev, 0x40032c);
331 if (inst & 0x01000000) {
332 nv_wr32(dev, 0x400720, 0x00000000);
333 nv_wr32(dev, 0x400784, inst);
334 nv_mask(dev, 0x400310, 0x00000020, 0x00000020);
335 nv_mask(dev, 0x400304, 0x00000001, 0x00000001);
336 if (!nv_wait(dev, 0x400300, 0x00000001, 0x00000000)) {
337 u32 insn = nv_rd32(dev, 0x400308);
338 NV_ERROR(dev, "PGRAPH: ctxprog timeout 0x%08x\n", insn);
340 nv_mask(dev, 0x40032c, 0x01000000, 0x00000000);
346 nv40_graph_isr_chid(struct drm_device *dev, u32 inst)
348 struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
349 struct drm_nouveau_private *dev_priv = dev->dev_private;
350 struct nouveau_gpuobj *grctx;
354 spin_lock_irqsave(&dev_priv->channels.lock, flags);
355 for (i = 0; i < pfifo->channels; i++) {
356 if (!dev_priv->channels.ptr[i])
358 grctx = dev_priv->channels.ptr[i]->engctx[NVOBJ_ENGINE_GR];
360 if (grctx && grctx->pinst == inst)
363 spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
368 nv40_graph_isr(struct drm_device *dev)
372 while ((stat = nv_rd32(dev, NV03_PGRAPH_INTR))) {
373 u32 nsource = nv_rd32(dev, NV03_PGRAPH_NSOURCE);
374 u32 nstatus = nv_rd32(dev, NV03_PGRAPH_NSTATUS);
375 u32 inst = (nv_rd32(dev, 0x40032c) & 0x000fffff) << 4;
376 u32 chid = nv40_graph_isr_chid(dev, inst);
377 u32 addr = nv_rd32(dev, NV04_PGRAPH_TRAPPED_ADDR);
378 u32 subc = (addr & 0x00070000) >> 16;
379 u32 mthd = (addr & 0x00001ffc);
380 u32 data = nv_rd32(dev, NV04_PGRAPH_TRAPPED_DATA);
381 u32 class = nv_rd32(dev, 0x400160 + subc * 4) & 0xffff;
384 if (stat & NV_PGRAPH_INTR_ERROR) {
385 if (nsource & NV03_PGRAPH_NSOURCE_ILLEGAL_MTHD) {
386 if (!nouveau_gpuobj_mthd_call2(dev, chid, class, mthd, data))
387 show &= ~NV_PGRAPH_INTR_ERROR;
389 if (nsource & NV03_PGRAPH_NSOURCE_DMA_VTX_PROTECTION) {
390 nv_mask(dev, 0x402000, 0, 0);
394 nv_wr32(dev, NV03_PGRAPH_INTR, stat);
395 nv_wr32(dev, NV04_PGRAPH_FIFO, 0x00000001);
397 if (show && nouveau_ratelimit()) {
398 NV_INFO(dev, "PGRAPH -");
399 nouveau_bitfield_print(nv10_graph_intr, show);
401 nouveau_bitfield_print(nv04_graph_nsource, nsource);
403 nouveau_bitfield_print(nv10_graph_nstatus, nstatus);
405 NV_INFO(dev, "PGRAPH - ch %d (0x%08x) subc %d "
406 "class 0x%04x mthd 0x%04x data 0x%08x\n",
407 chid, inst, subc, class, mthd, data);
413 nv40_graph_destroy(struct drm_device *dev, int engine)
415 struct nv40_graph_engine *pgraph = nv_engine(dev, engine);
417 nouveau_irq_unregister(dev, 12);
419 NVOBJ_ENGINE_DEL(dev, GR);
424 nv40_graph_create(struct drm_device *dev)
426 struct nv40_graph_engine *pgraph;
428 pgraph = kzalloc(sizeof(*pgraph), GFP_KERNEL);
432 pgraph->base.destroy = nv40_graph_destroy;
433 pgraph->base.init = nv40_graph_init;
434 pgraph->base.fini = nv40_graph_fini;
435 pgraph->base.context_new = nv40_graph_context_new;
436 pgraph->base.context_del = nv40_graph_context_del;
437 pgraph->base.object_new = nv40_graph_object_new;
438 pgraph->base.set_tile_region = nv40_graph_set_tile_region;
440 NVOBJ_ENGINE_ADD(dev, GR, &pgraph->base);
441 nouveau_irq_register(dev, 12, nv40_graph_isr);
443 NVOBJ_CLASS(dev, 0x0030, GR); /* null */
444 NVOBJ_CLASS(dev, 0x0039, GR); /* m2mf */
445 NVOBJ_CLASS(dev, 0x004a, GR); /* gdirect */
446 NVOBJ_CLASS(dev, 0x009f, GR); /* imageblit (nv12) */
447 NVOBJ_CLASS(dev, 0x008a, GR); /* ifc */
448 NVOBJ_CLASS(dev, 0x0089, GR); /* sifm */
449 NVOBJ_CLASS(dev, 0x3089, GR); /* sifm (nv40) */
450 NVOBJ_CLASS(dev, 0x0062, GR); /* surf2d */
451 NVOBJ_CLASS(dev, 0x3062, GR); /* surf2d (nv40) */
452 NVOBJ_CLASS(dev, 0x0043, GR); /* rop */
453 NVOBJ_CLASS(dev, 0x0012, GR); /* beta1 */
454 NVOBJ_CLASS(dev, 0x0072, GR); /* beta4 */
455 NVOBJ_CLASS(dev, 0x0019, GR); /* cliprect */
456 NVOBJ_CLASS(dev, 0x0044, GR); /* pattern */
457 NVOBJ_CLASS(dev, 0x309e, GR); /* swzsurf */
460 if (nv44_graph_class(dev))
461 NVOBJ_CLASS(dev, 0x4497, GR);
463 NVOBJ_CLASS(dev, 0x4097, GR);