nv40_graph.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (C) 2007 Ben Skeggs.
  3. * All Rights Reserved.
  4. *
  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:
  12. *
  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.
  16. *
  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.
  24. *
  25. */
  26. #include "drmP.h"
  27. #include "drm.h"
  28. #include "nouveau_drv.h"
  29. #include "nouveau_fifo.h"
  30. #include "nouveau_ramht.h"
  31. struct nv40_graph_engine {
  32. struct nouveau_exec_engine base;
  33. u32 grctx_size;
  34. };
  35. static int
  36. nv40_graph_context_new(struct nouveau_channel *chan, int engine)
  37. {
  38. struct nv40_graph_engine *pgraph = nv_engine(chan->dev, engine);
  39. struct drm_device *dev = chan->dev;
  40. struct drm_nouveau_private *dev_priv = dev->dev_private;
  41. struct nouveau_gpuobj *grctx = NULL;
  42. unsigned long flags;
  43. int ret;
  44. ret = nouveau_gpuobj_new(dev, NULL, pgraph->grctx_size, 16,
  45. NVOBJ_FLAG_ZERO_ALLOC, &grctx);
  46. if (ret)
  47. return ret;
  48. /* Initialise default context values */
  49. nv40_grctx_fill(dev, grctx);
  50. nv_wo32(grctx, 0, grctx->vinst);
  51. /* init grctx pointer in ramfc, and on PFIFO if channel is
  52. * already active there
  53. */
  54. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  55. nv_wo32(chan->ramfc, 0x38, grctx->vinst >> 4);
  56. nv_mask(dev, 0x002500, 0x00000001, 0x00000000);
  57. if ((nv_rd32(dev, 0x003204) & 0x0000001f) == chan->id)
  58. nv_wr32(dev, 0x0032e0, grctx->vinst >> 4);
  59. nv_mask(dev, 0x002500, 0x00000001, 0x00000001);
  60. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  61. chan->engctx[engine] = grctx;
  62. return 0;
  63. }
  64. static void
  65. nv40_graph_context_del(struct nouveau_channel *chan, int engine)
  66. {
  67. struct nouveau_gpuobj *grctx = chan->engctx[engine];
  68. struct drm_device *dev = chan->dev;
  69. struct drm_nouveau_private *dev_priv = dev->dev_private;
  70. u32 inst = 0x01000000 | (grctx->pinst >> 4);
  71. unsigned long flags;
  72. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  73. nv_mask(dev, 0x400720, 0x00000000, 0x00000001);
  74. if (nv_rd32(dev, 0x40032c) == inst)
  75. nv_mask(dev, 0x40032c, 0x01000000, 0x00000000);
  76. if (nv_rd32(dev, 0x400330) == inst)
  77. nv_mask(dev, 0x400330, 0x01000000, 0x00000000);
  78. nv_mask(dev, 0x400720, 0x00000001, 0x00000001);
  79. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  80. /* Free the context resources */
  81. nouveau_gpuobj_ref(NULL, &grctx);
  82. chan->engctx[engine] = NULL;
  83. }
  84. int
  85. nv40_graph_object_new(struct nouveau_channel *chan, int engine,
  86. u32 handle, u16 class)
  87. {
  88. struct drm_device *dev = chan->dev;
  89. struct nouveau_gpuobj *obj = NULL;
  90. int ret;
  91. ret = nouveau_gpuobj_new(dev, chan, 20, 16, NVOBJ_FLAG_ZERO_FREE, &obj);
  92. if (ret)
  93. return ret;
  94. obj->engine = 1;
  95. obj->class = class;
  96. nv_wo32(obj, 0x00, class);
  97. nv_wo32(obj, 0x04, 0x00000000);
  98. #ifndef __BIG_ENDIAN
  99. nv_wo32(obj, 0x08, 0x00000000);
  100. #else
  101. nv_wo32(obj, 0x08, 0x01000000);
  102. #endif
  103. nv_wo32(obj, 0x0c, 0x00000000);
  104. nv_wo32(obj, 0x10, 0x00000000);
  105. ret = nouveau_ramht_insert(chan, handle, obj);
  106. nouveau_gpuobj_ref(NULL, &obj);
  107. return ret;
  108. }
  109. static void
  110. nv40_graph_set_tile_region(struct drm_device *dev, int i)
  111. {
  112. struct drm_nouveau_private *dev_priv = dev->dev_private;
  113. struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
  114. switch (dev_priv->chipset) {
  115. case 0x40:
  116. case 0x41: /* guess */
  117. case 0x42:
  118. case 0x43:
  119. case 0x45: /* guess */
  120. case 0x4e:
  121. nv_wr32(dev, NV20_PGRAPH_TSIZE(i), tile->pitch);
  122. nv_wr32(dev, NV20_PGRAPH_TLIMIT(i), tile->limit);
  123. nv_wr32(dev, NV20_PGRAPH_TILE(i), tile->addr);
  124. nv_wr32(dev, NV40_PGRAPH_TSIZE1(i), tile->pitch);
  125. nv_wr32(dev, NV40_PGRAPH_TLIMIT1(i), tile->limit);
  126. nv_wr32(dev, NV40_PGRAPH_TILE1(i), tile->addr);
  127. break;
  128. case 0x44:
  129. case 0x4a:
  130. nv_wr32(dev, NV20_PGRAPH_TSIZE(i), tile->pitch);
  131. nv_wr32(dev, NV20_PGRAPH_TLIMIT(i), tile->limit);
  132. nv_wr32(dev, NV20_PGRAPH_TILE(i), tile->addr);
  133. break;
  134. case 0x46:
  135. case 0x47:
  136. case 0x49:
  137. case 0x4b:
  138. case 0x4c:
  139. case 0x67:
  140. default:
  141. nv_wr32(dev, NV47_PGRAPH_TSIZE(i), tile->pitch);
  142. nv_wr32(dev, NV47_PGRAPH_TLIMIT(i), tile->limit);
  143. nv_wr32(dev, NV47_PGRAPH_TILE(i), tile->addr);
  144. nv_wr32(dev, NV40_PGRAPH_TSIZE1(i), tile->pitch);
  145. nv_wr32(dev, NV40_PGRAPH_TLIMIT1(i), tile->limit);
  146. nv_wr32(dev, NV40_PGRAPH_TILE1(i), tile->addr);
  147. break;
  148. }
  149. }
  150. /*
  151. * G70 0x47
  152. * G71 0x49
  153. * NV45 0x48
  154. * G72[M] 0x46
  155. * G73 0x4b
  156. * C51_G7X 0x4c
  157. * C51 0x4e
  158. */
  159. int
  160. nv40_graph_init(struct drm_device *dev, int engine)
  161. {
  162. struct nv40_graph_engine *pgraph = nv_engine(dev, engine);
  163. struct drm_nouveau_private *dev_priv = dev->dev_private;
  164. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  165. uint32_t vramsz;
  166. int i, j;
  167. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) &
  168. ~NV_PMC_ENABLE_PGRAPH);
  169. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  170. NV_PMC_ENABLE_PGRAPH);
  171. /* generate and upload context program */
  172. nv40_grctx_init(dev, &pgraph->grctx_size);
  173. /* No context present currently */
  174. nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, 0x00000000);
  175. nv_wr32(dev, NV03_PGRAPH_INTR , 0xFFFFFFFF);
  176. nv_wr32(dev, NV40_PGRAPH_INTR_EN, 0xFFFFFFFF);
  177. nv_wr32(dev, NV04_PGRAPH_DEBUG_0, 0xFFFFFFFF);
  178. nv_wr32(dev, NV04_PGRAPH_DEBUG_0, 0x00000000);
  179. nv_wr32(dev, NV04_PGRAPH_DEBUG_1, 0x401287c0);
  180. nv_wr32(dev, NV04_PGRAPH_DEBUG_3, 0xe0de8055);
  181. nv_wr32(dev, NV10_PGRAPH_DEBUG_4, 0x00008000);
  182. nv_wr32(dev, NV04_PGRAPH_LIMIT_VIOL_PIX, 0x00be3c5f);
  183. nv_wr32(dev, NV10_PGRAPH_CTX_CONTROL, 0x10010100);
  184. nv_wr32(dev, NV10_PGRAPH_STATE , 0xFFFFFFFF);
  185. j = nv_rd32(dev, 0x1540) & 0xff;
  186. if (j) {
  187. for (i = 0; !(j & 1); j >>= 1, i++)
  188. ;
  189. nv_wr32(dev, 0x405000, i);
  190. }
  191. if (dev_priv->chipset == 0x40) {
  192. nv_wr32(dev, 0x4009b0, 0x83280fff);
  193. nv_wr32(dev, 0x4009b4, 0x000000a0);
  194. } else {
  195. nv_wr32(dev, 0x400820, 0x83280eff);
  196. nv_wr32(dev, 0x400824, 0x000000a0);
  197. }
  198. switch (dev_priv->chipset) {
  199. case 0x40:
  200. case 0x45:
  201. nv_wr32(dev, 0x4009b8, 0x0078e366);
  202. nv_wr32(dev, 0x4009bc, 0x0000014c);
  203. break;
  204. case 0x41:
  205. case 0x42: /* pciid also 0x00Cx */
  206. /* case 0x0120: XXX (pciid) */
  207. nv_wr32(dev, 0x400828, 0x007596ff);
  208. nv_wr32(dev, 0x40082c, 0x00000108);
  209. break;
  210. case 0x43:
  211. nv_wr32(dev, 0x400828, 0x0072cb77);
  212. nv_wr32(dev, 0x40082c, 0x00000108);
  213. break;
  214. case 0x44:
  215. case 0x46: /* G72 */
  216. case 0x4a:
  217. case 0x4c: /* G7x-based C51 */
  218. case 0x4e:
  219. nv_wr32(dev, 0x400860, 0);
  220. nv_wr32(dev, 0x400864, 0);
  221. break;
  222. case 0x47: /* G70 */
  223. case 0x49: /* G71 */
  224. case 0x4b: /* G73 */
  225. nv_wr32(dev, 0x400828, 0x07830610);
  226. nv_wr32(dev, 0x40082c, 0x0000016A);
  227. break;
  228. default:
  229. break;
  230. }
  231. nv_wr32(dev, 0x400b38, 0x2ffff800);
  232. nv_wr32(dev, 0x400b3c, 0x00006000);
  233. /* Tiling related stuff. */
  234. switch (dev_priv->chipset) {
  235. case 0x44:
  236. case 0x4a:
  237. nv_wr32(dev, 0x400bc4, 0x1003d888);
  238. nv_wr32(dev, 0x400bbc, 0xb7a7b500);
  239. break;
  240. case 0x46:
  241. nv_wr32(dev, 0x400bc4, 0x0000e024);
  242. nv_wr32(dev, 0x400bbc, 0xb7a7b520);
  243. break;
  244. case 0x4c:
  245. case 0x4e:
  246. case 0x67:
  247. nv_wr32(dev, 0x400bc4, 0x1003d888);
  248. nv_wr32(dev, 0x400bbc, 0xb7a7b540);
  249. break;
  250. default:
  251. break;
  252. }
  253. /* Turn all the tiling regions off. */
  254. for (i = 0; i < pfb->num_tiles; i++)
  255. nv40_graph_set_tile_region(dev, i);
  256. /* begin RAM config */
  257. vramsz = pci_resource_len(dev->pdev, 0) - 1;
  258. switch (dev_priv->chipset) {
  259. case 0x40:
  260. nv_wr32(dev, 0x4009A4, nv_rd32(dev, NV04_PFB_CFG0));
  261. nv_wr32(dev, 0x4009A8, nv_rd32(dev, NV04_PFB_CFG1));
  262. nv_wr32(dev, 0x4069A4, nv_rd32(dev, NV04_PFB_CFG0));
  263. nv_wr32(dev, 0x4069A8, nv_rd32(dev, NV04_PFB_CFG1));
  264. nv_wr32(dev, 0x400820, 0);
  265. nv_wr32(dev, 0x400824, 0);
  266. nv_wr32(dev, 0x400864, vramsz);
  267. nv_wr32(dev, 0x400868, vramsz);
  268. break;
  269. default:
  270. switch (dev_priv->chipset) {
  271. case 0x41:
  272. case 0x42:
  273. case 0x43:
  274. case 0x45:
  275. case 0x4e:
  276. case 0x44:
  277. case 0x4a:
  278. nv_wr32(dev, 0x4009F0, nv_rd32(dev, NV04_PFB_CFG0));
  279. nv_wr32(dev, 0x4009F4, nv_rd32(dev, NV04_PFB_CFG1));
  280. break;
  281. default:
  282. nv_wr32(dev, 0x400DF0, nv_rd32(dev, NV04_PFB_CFG0));
  283. nv_wr32(dev, 0x400DF4, nv_rd32(dev, NV04_PFB_CFG1));
  284. break;
  285. }
  286. nv_wr32(dev, 0x4069F0, nv_rd32(dev, NV04_PFB_CFG0));
  287. nv_wr32(dev, 0x4069F4, nv_rd32(dev, NV04_PFB_CFG1));
  288. nv_wr32(dev, 0x400840, 0);
  289. nv_wr32(dev, 0x400844, 0);
  290. nv_wr32(dev, 0x4008A0, vramsz);
  291. nv_wr32(dev, 0x4008A4, vramsz);
  292. break;
  293. }
  294. return 0;
  295. }
  296. static int
  297. nv40_graph_fini(struct drm_device *dev, int engine, bool suspend)
  298. {
  299. u32 inst = nv_rd32(dev, 0x40032c);
  300. if (inst & 0x01000000) {
  301. nv_wr32(dev, 0x400720, 0x00000000);
  302. nv_wr32(dev, 0x400784, inst);
  303. nv_mask(dev, 0x400310, 0x00000020, 0x00000020);
  304. nv_mask(dev, 0x400304, 0x00000001, 0x00000001);
  305. if (!nv_wait(dev, 0x400300, 0x00000001, 0x00000000)) {
  306. u32 insn = nv_rd32(dev, 0x400308);
  307. NV_ERROR(dev, "PGRAPH: ctxprog timeout 0x%08x\n", insn);
  308. }
  309. nv_mask(dev, 0x40032c, 0x01000000, 0x00000000);
  310. }
  311. return 0;
  312. }
  313. static int
  314. nv40_graph_isr_chid(struct drm_device *dev, u32 inst)
  315. {
  316. struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  317. struct drm_nouveau_private *dev_priv = dev->dev_private;
  318. struct nouveau_gpuobj *grctx;
  319. unsigned long flags;
  320. int i;
  321. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  322. for (i = 0; i < pfifo->channels; i++) {
  323. if (!dev_priv->channels.ptr[i])
  324. continue;
  325. grctx = dev_priv->channels.ptr[i]->engctx[NVOBJ_ENGINE_GR];
  326. if (grctx && grctx->pinst == inst)
  327. break;
  328. }
  329. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  330. return i;
  331. }
  332. static void
  333. nv40_graph_isr(struct drm_device *dev)
  334. {
  335. u32 stat;
  336. while ((stat = nv_rd32(dev, NV03_PGRAPH_INTR))) {
  337. u32 nsource = nv_rd32(dev, NV03_PGRAPH_NSOURCE);
  338. u32 nstatus = nv_rd32(dev, NV03_PGRAPH_NSTATUS);
  339. u32 inst = (nv_rd32(dev, 0x40032c) & 0x000fffff) << 4;
  340. u32 chid = nv40_graph_isr_chid(dev, inst);
  341. u32 addr = nv_rd32(dev, NV04_PGRAPH_TRAPPED_ADDR);
  342. u32 subc = (addr & 0x00070000) >> 16;
  343. u32 mthd = (addr & 0x00001ffc);
  344. u32 data = nv_rd32(dev, NV04_PGRAPH_TRAPPED_DATA);
  345. u32 class = nv_rd32(dev, 0x400160 + subc * 4) & 0xffff;
  346. u32 show = stat;
  347. if (stat & NV_PGRAPH_INTR_ERROR) {
  348. if (nsource & NV03_PGRAPH_NSOURCE_ILLEGAL_MTHD) {
  349. if (!nouveau_gpuobj_mthd_call2(dev, chid, class, mthd, data))
  350. show &= ~NV_PGRAPH_INTR_ERROR;
  351. } else
  352. if (nsource & NV03_PGRAPH_NSOURCE_DMA_VTX_PROTECTION) {
  353. nv_mask(dev, 0x402000, 0, 0);
  354. }
  355. }
  356. nv_wr32(dev, NV03_PGRAPH_INTR, stat);
  357. nv_wr32(dev, NV04_PGRAPH_FIFO, 0x00000001);
  358. if (show && nouveau_ratelimit()) {
  359. NV_INFO(dev, "PGRAPH -");
  360. nouveau_bitfield_print(nv10_graph_intr, show);
  361. printk(" nsource:");
  362. nouveau_bitfield_print(nv04_graph_nsource, nsource);
  363. printk(" nstatus:");
  364. nouveau_bitfield_print(nv10_graph_nstatus, nstatus);
  365. printk("\n");
  366. NV_INFO(dev, "PGRAPH - ch %d (0x%08x) subc %d "
  367. "class 0x%04x mthd 0x%04x data 0x%08x\n",
  368. chid, inst, subc, class, mthd, data);
  369. }
  370. }
  371. }
  372. static void
  373. nv40_graph_destroy(struct drm_device *dev, int engine)
  374. {
  375. struct nv40_graph_engine *pgraph = nv_engine(dev, engine);
  376. nouveau_irq_unregister(dev, 12);
  377. NVOBJ_ENGINE_DEL(dev, GR);
  378. kfree(pgraph);
  379. }
  380. int
  381. nv40_graph_create(struct drm_device *dev)
  382. {
  383. struct nv40_graph_engine *pgraph;
  384. pgraph = kzalloc(sizeof(*pgraph), GFP_KERNEL);
  385. if (!pgraph)
  386. return -ENOMEM;
  387. pgraph->base.destroy = nv40_graph_destroy;
  388. pgraph->base.init = nv40_graph_init;
  389. pgraph->base.fini = nv40_graph_fini;
  390. pgraph->base.context_new = nv40_graph_context_new;
  391. pgraph->base.context_del = nv40_graph_context_del;
  392. pgraph->base.object_new = nv40_graph_object_new;
  393. pgraph->base.set_tile_region = nv40_graph_set_tile_region;
  394. NVOBJ_ENGINE_ADD(dev, GR, &pgraph->base);
  395. nouveau_irq_register(dev, 12, nv40_graph_isr);
  396. NVOBJ_CLASS(dev, 0x0030, GR); /* null */
  397. NVOBJ_CLASS(dev, 0x0039, GR); /* m2mf */
  398. NVOBJ_CLASS(dev, 0x004a, GR); /* gdirect */
  399. NVOBJ_CLASS(dev, 0x009f, GR); /* imageblit (nv12) */
  400. NVOBJ_CLASS(dev, 0x008a, GR); /* ifc */
  401. NVOBJ_CLASS(dev, 0x0089, GR); /* sifm */
  402. NVOBJ_CLASS(dev, 0x3089, GR); /* sifm (nv40) */
  403. NVOBJ_CLASS(dev, 0x0062, GR); /* surf2d */
  404. NVOBJ_CLASS(dev, 0x3062, GR); /* surf2d (nv40) */
  405. NVOBJ_CLASS(dev, 0x0043, GR); /* rop */
  406. NVOBJ_CLASS(dev, 0x0012, GR); /* beta1 */
  407. NVOBJ_CLASS(dev, 0x0072, GR); /* beta4 */
  408. NVOBJ_CLASS(dev, 0x0019, GR); /* cliprect */
  409. NVOBJ_CLASS(dev, 0x0044, GR); /* pattern */
  410. NVOBJ_CLASS(dev, 0x309e, GR); /* swzsurf */
  411. /* curie */
  412. if (nv44_graph_class(dev))
  413. NVOBJ_CLASS(dev, 0x4497, GR);
  414. else
  415. NVOBJ_CLASS(dev, 0x4097, GR);
  416. return 0;
  417. }