nv40_graph.c 13 KB

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