nv40_graph.c 11 KB

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