nv40_graph.c 13 KB

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