nv40_graph.c 11 KB

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