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