nv40_graph.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. struct nouveau_channel *
  31. nv40_graph_channel(struct drm_device *dev)
  32. {
  33. struct drm_nouveau_private *dev_priv = dev->dev_private;
  34. uint32_t inst;
  35. int i;
  36. inst = nv_rd32(dev, NV40_PGRAPH_CTXCTL_CUR);
  37. if (!(inst & NV40_PGRAPH_CTXCTL_CUR_LOADED))
  38. return NULL;
  39. inst = (inst & NV40_PGRAPH_CTXCTL_CUR_INSTANCE) << 4;
  40. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  41. struct nouveau_channel *chan = dev_priv->fifos[i];
  42. if (chan && chan->ramin_grctx &&
  43. chan->ramin_grctx->instance == inst)
  44. return chan;
  45. }
  46. return NULL;
  47. }
  48. int
  49. nv40_graph_create_context(struct nouveau_channel *chan)
  50. {
  51. struct drm_device *dev = chan->dev;
  52. struct drm_nouveau_private *dev_priv = dev->dev_private;
  53. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  54. struct nouveau_grctx ctx = {};
  55. int ret;
  56. ret = nouveau_gpuobj_new_ref(dev, chan, NULL, 0, pgraph->grctx_size,
  57. 16, NVOBJ_FLAG_ZERO_ALLOC,
  58. &chan->ramin_grctx);
  59. if (ret)
  60. return ret;
  61. /* Initialise default context values */
  62. ctx.dev = chan->dev;
  63. ctx.mode = NOUVEAU_GRCTX_VALS;
  64. ctx.data = chan->ramin_grctx->gpuobj;
  65. nv40_grctx_init(&ctx);
  66. nv_wo32(dev, chan->ramin_grctx->gpuobj, 0,
  67. chan->ramin_grctx->gpuobj->im_pramin->start);
  68. return 0;
  69. }
  70. void
  71. nv40_graph_destroy_context(struct nouveau_channel *chan)
  72. {
  73. nouveau_gpuobj_ref_del(chan->dev, &chan->ramin_grctx);
  74. }
  75. static int
  76. nv40_graph_transfer_context(struct drm_device *dev, uint32_t inst, int save)
  77. {
  78. uint32_t old_cp, tv = 1000, tmp;
  79. int i;
  80. old_cp = nv_rd32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER);
  81. nv_wr32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER, inst);
  82. tmp = nv_rd32(dev, NV40_PGRAPH_CTXCTL_0310);
  83. tmp |= save ? NV40_PGRAPH_CTXCTL_0310_XFER_SAVE :
  84. NV40_PGRAPH_CTXCTL_0310_XFER_LOAD;
  85. nv_wr32(dev, NV40_PGRAPH_CTXCTL_0310, tmp);
  86. tmp = nv_rd32(dev, NV40_PGRAPH_CTXCTL_0304);
  87. tmp |= NV40_PGRAPH_CTXCTL_0304_XFER_CTX;
  88. nv_wr32(dev, NV40_PGRAPH_CTXCTL_0304, tmp);
  89. nouveau_wait_for_idle(dev);
  90. for (i = 0; i < tv; i++) {
  91. if (nv_rd32(dev, NV40_PGRAPH_CTXCTL_030C) == 0)
  92. break;
  93. }
  94. nv_wr32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER, old_cp);
  95. if (i == tv) {
  96. uint32_t ucstat = nv_rd32(dev, NV40_PGRAPH_CTXCTL_UCODE_STAT);
  97. NV_ERROR(dev, "Failed: Instance=0x%08x Save=%d\n", inst, save);
  98. NV_ERROR(dev, "IP: 0x%02x, Opcode: 0x%08x\n",
  99. ucstat >> NV40_PGRAPH_CTXCTL_UCODE_STAT_IP_SHIFT,
  100. ucstat & NV40_PGRAPH_CTXCTL_UCODE_STAT_OP_MASK);
  101. NV_ERROR(dev, "0x40030C = 0x%08x\n",
  102. nv_rd32(dev, NV40_PGRAPH_CTXCTL_030C));
  103. return -EBUSY;
  104. }
  105. return 0;
  106. }
  107. /* Restore the context for a specific channel into PGRAPH */
  108. int
  109. nv40_graph_load_context(struct nouveau_channel *chan)
  110. {
  111. struct drm_device *dev = chan->dev;
  112. uint32_t inst;
  113. int ret;
  114. if (!chan->ramin_grctx)
  115. return -EINVAL;
  116. inst = chan->ramin_grctx->instance >> 4;
  117. ret = nv40_graph_transfer_context(dev, inst, 0);
  118. if (ret)
  119. return ret;
  120. /* 0x40032C, no idea of it's exact function. Could simply be a
  121. * record of the currently active PGRAPH context. It's currently
  122. * unknown as to what bit 24 does. The nv ddx has it set, so we will
  123. * set it here too.
  124. */
  125. nv_wr32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER, inst);
  126. nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR,
  127. (inst & NV40_PGRAPH_CTXCTL_CUR_INSTANCE) |
  128. NV40_PGRAPH_CTXCTL_CUR_LOADED);
  129. /* 0x32E0 records the instance address of the active FIFO's PGRAPH
  130. * context. If at any time this doesn't match 0x40032C, you will
  131. * recieve PGRAPH_INTR_CONTEXT_SWITCH
  132. */
  133. nv_wr32(dev, NV40_PFIFO_GRCTX_INSTANCE, inst);
  134. return 0;
  135. }
  136. int
  137. nv40_graph_unload_context(struct drm_device *dev)
  138. {
  139. uint32_t inst;
  140. int ret;
  141. inst = nv_rd32(dev, NV40_PGRAPH_CTXCTL_CUR);
  142. if (!(inst & NV40_PGRAPH_CTXCTL_CUR_LOADED))
  143. return 0;
  144. inst &= NV40_PGRAPH_CTXCTL_CUR_INSTANCE;
  145. ret = nv40_graph_transfer_context(dev, inst, 1);
  146. nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, inst);
  147. return ret;
  148. }
  149. void
  150. nv40_graph_set_region_tiling(struct drm_device *dev, int i, uint32_t addr,
  151. uint32_t size, uint32_t pitch)
  152. {
  153. struct drm_nouveau_private *dev_priv = dev->dev_private;
  154. uint32_t limit = max(1u, addr + size) - 1;
  155. if (pitch)
  156. addr |= 1;
  157. switch (dev_priv->chipset) {
  158. case 0x44:
  159. case 0x4a:
  160. case 0x4e:
  161. nv_wr32(dev, NV20_PGRAPH_TSIZE(i), pitch);
  162. nv_wr32(dev, NV20_PGRAPH_TLIMIT(i), limit);
  163. nv_wr32(dev, NV20_PGRAPH_TILE(i), addr);
  164. break;
  165. case 0x46:
  166. case 0x47:
  167. case 0x49:
  168. case 0x4b:
  169. nv_wr32(dev, NV47_PGRAPH_TSIZE(i), pitch);
  170. nv_wr32(dev, NV47_PGRAPH_TLIMIT(i), limit);
  171. nv_wr32(dev, NV47_PGRAPH_TILE(i), addr);
  172. nv_wr32(dev, NV40_PGRAPH_TSIZE1(i), pitch);
  173. nv_wr32(dev, NV40_PGRAPH_TLIMIT1(i), limit);
  174. nv_wr32(dev, NV40_PGRAPH_TILE1(i), addr);
  175. break;
  176. default:
  177. nv_wr32(dev, NV20_PGRAPH_TSIZE(i), pitch);
  178. nv_wr32(dev, NV20_PGRAPH_TLIMIT(i), limit);
  179. nv_wr32(dev, NV20_PGRAPH_TILE(i), addr);
  180. nv_wr32(dev, NV40_PGRAPH_TSIZE1(i), pitch);
  181. nv_wr32(dev, NV40_PGRAPH_TLIMIT1(i), limit);
  182. nv_wr32(dev, NV40_PGRAPH_TILE1(i), addr);
  183. break;
  184. }
  185. }
  186. /*
  187. * G70 0x47
  188. * G71 0x49
  189. * NV45 0x48
  190. * G72[M] 0x46
  191. * G73 0x4b
  192. * C51_G7X 0x4c
  193. * C51 0x4e
  194. */
  195. int
  196. nv40_graph_init(struct drm_device *dev)
  197. {
  198. struct drm_nouveau_private *dev_priv =
  199. (struct drm_nouveau_private *)dev->dev_private;
  200. struct nouveau_fb_engine *pfb = &dev_priv->engine.fb;
  201. struct nouveau_grctx ctx = {};
  202. uint32_t vramsz, *cp;
  203. int i, j;
  204. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) &
  205. ~NV_PMC_ENABLE_PGRAPH);
  206. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) |
  207. NV_PMC_ENABLE_PGRAPH);
  208. cp = kmalloc(sizeof(*cp) * 256, GFP_KERNEL);
  209. if (!cp)
  210. return -ENOMEM;
  211. ctx.dev = dev;
  212. ctx.mode = NOUVEAU_GRCTX_PROG;
  213. ctx.data = cp;
  214. ctx.ctxprog_max = 256;
  215. nv40_grctx_init(&ctx);
  216. dev_priv->engine.graph.grctx_size = ctx.ctxvals_pos * 4;
  217. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_INDEX, 0);
  218. for (i = 0; i < ctx.ctxprog_len; i++)
  219. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_DATA, cp[i]);
  220. kfree(cp);
  221. /* No context present currently */
  222. nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, 0x00000000);
  223. nv_wr32(dev, NV03_PGRAPH_INTR , 0xFFFFFFFF);
  224. nv_wr32(dev, NV40_PGRAPH_INTR_EN, 0xFFFFFFFF);
  225. nv_wr32(dev, NV04_PGRAPH_DEBUG_0, 0xFFFFFFFF);
  226. nv_wr32(dev, NV04_PGRAPH_DEBUG_0, 0x00000000);
  227. nv_wr32(dev, NV04_PGRAPH_DEBUG_1, 0x401287c0);
  228. nv_wr32(dev, NV04_PGRAPH_DEBUG_3, 0xe0de8055);
  229. nv_wr32(dev, NV10_PGRAPH_DEBUG_4, 0x00008000);
  230. nv_wr32(dev, NV04_PGRAPH_LIMIT_VIOL_PIX, 0x00be3c5f);
  231. nv_wr32(dev, NV10_PGRAPH_CTX_CONTROL, 0x10010100);
  232. nv_wr32(dev, NV10_PGRAPH_STATE , 0xFFFFFFFF);
  233. j = nv_rd32(dev, 0x1540) & 0xff;
  234. if (j) {
  235. for (i = 0; !(j & 1); j >>= 1, i++)
  236. ;
  237. nv_wr32(dev, 0x405000, i);
  238. }
  239. if (dev_priv->chipset == 0x40) {
  240. nv_wr32(dev, 0x4009b0, 0x83280fff);
  241. nv_wr32(dev, 0x4009b4, 0x000000a0);
  242. } else {
  243. nv_wr32(dev, 0x400820, 0x83280eff);
  244. nv_wr32(dev, 0x400824, 0x000000a0);
  245. }
  246. switch (dev_priv->chipset) {
  247. case 0x40:
  248. case 0x45:
  249. nv_wr32(dev, 0x4009b8, 0x0078e366);
  250. nv_wr32(dev, 0x4009bc, 0x0000014c);
  251. break;
  252. case 0x41:
  253. case 0x42: /* pciid also 0x00Cx */
  254. /* case 0x0120: XXX (pciid) */
  255. nv_wr32(dev, 0x400828, 0x007596ff);
  256. nv_wr32(dev, 0x40082c, 0x00000108);
  257. break;
  258. case 0x43:
  259. nv_wr32(dev, 0x400828, 0x0072cb77);
  260. nv_wr32(dev, 0x40082c, 0x00000108);
  261. break;
  262. case 0x44:
  263. case 0x46: /* G72 */
  264. case 0x4a:
  265. case 0x4c: /* G7x-based C51 */
  266. case 0x4e:
  267. nv_wr32(dev, 0x400860, 0);
  268. nv_wr32(dev, 0x400864, 0);
  269. break;
  270. case 0x47: /* G70 */
  271. case 0x49: /* G71 */
  272. case 0x4b: /* G73 */
  273. nv_wr32(dev, 0x400828, 0x07830610);
  274. nv_wr32(dev, 0x40082c, 0x0000016A);
  275. break;
  276. default:
  277. break;
  278. }
  279. nv_wr32(dev, 0x400b38, 0x2ffff800);
  280. nv_wr32(dev, 0x400b3c, 0x00006000);
  281. /* Tiling related stuff. */
  282. switch (dev_priv->chipset) {
  283. case 0x44:
  284. case 0x4a:
  285. nv_wr32(dev, 0x400bc4, 0x1003d888);
  286. nv_wr32(dev, 0x400bbc, 0xb7a7b500);
  287. break;
  288. case 0x46:
  289. nv_wr32(dev, 0x400bc4, 0x0000e024);
  290. nv_wr32(dev, 0x400bbc, 0xb7a7b520);
  291. break;
  292. case 0x4c:
  293. case 0x4e:
  294. case 0x67:
  295. nv_wr32(dev, 0x400bc4, 0x1003d888);
  296. nv_wr32(dev, 0x400bbc, 0xb7a7b540);
  297. break;
  298. default:
  299. break;
  300. }
  301. /* Turn all the tiling regions off. */
  302. for (i = 0; i < pfb->num_tiles; i++)
  303. nv40_graph_set_region_tiling(dev, i, 0, 0, 0);
  304. /* begin RAM config */
  305. vramsz = pci_resource_len(dev->pdev, 0) - 1;
  306. switch (dev_priv->chipset) {
  307. case 0x40:
  308. nv_wr32(dev, 0x4009A4, nv_rd32(dev, NV04_PFB_CFG0));
  309. nv_wr32(dev, 0x4009A8, nv_rd32(dev, NV04_PFB_CFG1));
  310. nv_wr32(dev, 0x4069A4, nv_rd32(dev, NV04_PFB_CFG0));
  311. nv_wr32(dev, 0x4069A8, nv_rd32(dev, NV04_PFB_CFG1));
  312. nv_wr32(dev, 0x400820, 0);
  313. nv_wr32(dev, 0x400824, 0);
  314. nv_wr32(dev, 0x400864, vramsz);
  315. nv_wr32(dev, 0x400868, vramsz);
  316. break;
  317. default:
  318. switch (dev_priv->chipset) {
  319. case 0x46:
  320. case 0x47:
  321. case 0x49:
  322. case 0x4b:
  323. nv_wr32(dev, 0x400DF0, nv_rd32(dev, NV04_PFB_CFG0));
  324. nv_wr32(dev, 0x400DF4, nv_rd32(dev, NV04_PFB_CFG1));
  325. break;
  326. default:
  327. nv_wr32(dev, 0x4009F0, nv_rd32(dev, NV04_PFB_CFG0));
  328. nv_wr32(dev, 0x4009F4, nv_rd32(dev, NV04_PFB_CFG1));
  329. break;
  330. }
  331. nv_wr32(dev, 0x4069F0, nv_rd32(dev, NV04_PFB_CFG0));
  332. nv_wr32(dev, 0x4069F4, nv_rd32(dev, NV04_PFB_CFG1));
  333. nv_wr32(dev, 0x400840, 0);
  334. nv_wr32(dev, 0x400844, 0);
  335. nv_wr32(dev, 0x4008A0, vramsz);
  336. nv_wr32(dev, 0x4008A4, vramsz);
  337. break;
  338. }
  339. return 0;
  340. }
  341. void nv40_graph_takedown(struct drm_device *dev)
  342. {
  343. }
  344. struct nouveau_pgraph_object_class nv40_graph_grclass[] = {
  345. { 0x0030, false, NULL }, /* null */
  346. { 0x0039, false, NULL }, /* m2mf */
  347. { 0x004a, false, NULL }, /* gdirect */
  348. { 0x009f, false, NULL }, /* imageblit (nv12) */
  349. { 0x008a, false, NULL }, /* ifc */
  350. { 0x0089, false, NULL }, /* sifm */
  351. { 0x3089, false, NULL }, /* sifm (nv40) */
  352. { 0x0062, false, NULL }, /* surf2d */
  353. { 0x3062, false, NULL }, /* surf2d (nv40) */
  354. { 0x0043, false, NULL }, /* rop */
  355. { 0x0012, false, NULL }, /* beta1 */
  356. { 0x0072, false, NULL }, /* beta4 */
  357. { 0x0019, false, NULL }, /* cliprect */
  358. { 0x0044, false, NULL }, /* pattern */
  359. { 0x309e, false, NULL }, /* swzsurf */
  360. { 0x4097, false, NULL }, /* curie (nv40) */
  361. { 0x4497, false, NULL }, /* curie (nv44) */
  362. {}
  363. };