nv40_graph.c 11 KB

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