nv40_graph.c 12 KB

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