nv50_graph.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. #define IS_G80 ((dev_priv->chipset & 0xf0) == 0x50)
  31. static void
  32. nv50_graph_init_reset(struct drm_device *dev)
  33. {
  34. uint32_t pmc_e = NV_PMC_ENABLE_PGRAPH | (1 << 21);
  35. NV_DEBUG(dev, "\n");
  36. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) & ~pmc_e);
  37. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) | pmc_e);
  38. }
  39. static void
  40. nv50_graph_init_intr(struct drm_device *dev)
  41. {
  42. NV_DEBUG(dev, "\n");
  43. nv_wr32(dev, NV03_PGRAPH_INTR, 0xffffffff);
  44. nv_wr32(dev, 0x400138, 0xffffffff);
  45. nv_wr32(dev, NV40_PGRAPH_INTR_EN, 0xffffffff);
  46. }
  47. static void
  48. nv50_graph_init_regs__nv(struct drm_device *dev)
  49. {
  50. struct drm_nouveau_private *dev_priv = dev->dev_private;
  51. uint32_t units = nv_rd32(dev, 0x1540);
  52. int i;
  53. NV_DEBUG(dev, "\n");
  54. nv_wr32(dev, 0x400804, 0xc0000000);
  55. nv_wr32(dev, 0x406800, 0xc0000000);
  56. nv_wr32(dev, 0x400c04, 0xc0000000);
  57. nv_wr32(dev, 0x401800, 0xc0000000);
  58. nv_wr32(dev, 0x405018, 0xc0000000);
  59. nv_wr32(dev, 0x402000, 0xc0000000);
  60. for (i = 0; i < 16; i++) {
  61. if (units & 1 << i) {
  62. if (dev_priv->chipset < 0xa0) {
  63. nv_wr32(dev, 0x408900 + (i << 12), 0xc0000000);
  64. nv_wr32(dev, 0x408e08 + (i << 12), 0xc0000000);
  65. nv_wr32(dev, 0x408314 + (i << 12), 0xc0000000);
  66. } else {
  67. nv_wr32(dev, 0x408600 + (i << 11), 0xc0000000);
  68. nv_wr32(dev, 0x408708 + (i << 11), 0xc0000000);
  69. nv_wr32(dev, 0x40831c + (i << 11), 0xc0000000);
  70. }
  71. }
  72. }
  73. nv_wr32(dev, 0x400108, 0xffffffff);
  74. nv_wr32(dev, 0x400824, 0x00004000);
  75. nv_wr32(dev, 0x400500, 0x00010001);
  76. }
  77. static void
  78. nv50_graph_init_regs(struct drm_device *dev)
  79. {
  80. NV_DEBUG(dev, "\n");
  81. nv_wr32(dev, NV04_PGRAPH_DEBUG_3,
  82. (1 << 2) /* HW_CONTEXT_SWITCH_ENABLED */);
  83. nv_wr32(dev, 0x402ca8, 0x800);
  84. }
  85. static int
  86. nv50_graph_init_ctxctl(struct drm_device *dev)
  87. {
  88. struct drm_nouveau_private *dev_priv = dev->dev_private;
  89. NV_DEBUG(dev, "\n");
  90. if (nouveau_ctxfw) {
  91. nouveau_grctx_prog_load(dev);
  92. dev_priv->engine.graph.grctx_size = 0x70000;
  93. }
  94. if (!dev_priv->engine.graph.ctxprog) {
  95. struct nouveau_grctx ctx = {};
  96. uint32_t *cp = kmalloc(512 * 4, GFP_KERNEL);
  97. int i;
  98. if (!cp) {
  99. NV_ERROR(dev, "Couldn't alloc ctxprog! Disabling acceleration.\n");
  100. dev_priv->engine.graph.accel_blocked = true;
  101. return 0;
  102. }
  103. ctx.dev = dev;
  104. ctx.mode = NOUVEAU_GRCTX_PROG;
  105. ctx.data = cp;
  106. ctx.ctxprog_max = 512;
  107. if (!nv50_grctx_init(&ctx)) {
  108. dev_priv->engine.graph.grctx_size = ctx.ctxvals_pos * 4;
  109. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_INDEX, 0);
  110. for (i = 0; i < ctx.ctxprog_len; i++)
  111. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_DATA, cp[i]);
  112. } else {
  113. dev_priv->engine.graph.accel_blocked = true;
  114. }
  115. kfree(cp);
  116. }
  117. nv_wr32(dev, 0x400320, 4);
  118. nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, 0);
  119. nv_wr32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER, 0);
  120. return 0;
  121. }
  122. int
  123. nv50_graph_init(struct drm_device *dev)
  124. {
  125. int ret;
  126. NV_DEBUG(dev, "\n");
  127. nv50_graph_init_reset(dev);
  128. nv50_graph_init_regs__nv(dev);
  129. nv50_graph_init_regs(dev);
  130. nv50_graph_init_intr(dev);
  131. ret = nv50_graph_init_ctxctl(dev);
  132. if (ret)
  133. return ret;
  134. return 0;
  135. }
  136. void
  137. nv50_graph_takedown(struct drm_device *dev)
  138. {
  139. NV_DEBUG(dev, "\n");
  140. nouveau_grctx_fini(dev);
  141. }
  142. void
  143. nv50_graph_fifo_access(struct drm_device *dev, bool enabled)
  144. {
  145. const uint32_t mask = 0x00010001;
  146. if (enabled)
  147. nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) | mask);
  148. else
  149. nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) & ~mask);
  150. }
  151. struct nouveau_channel *
  152. nv50_graph_channel(struct drm_device *dev)
  153. {
  154. struct drm_nouveau_private *dev_priv = dev->dev_private;
  155. uint32_t inst;
  156. int i;
  157. /* Be sure we're not in the middle of a context switch or bad things
  158. * will happen, such as unloading the wrong pgraph context.
  159. */
  160. if (!nv_wait(0x400300, 0x00000001, 0x00000000))
  161. NV_ERROR(dev, "Ctxprog is still running\n");
  162. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  163. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  164. return NULL;
  165. inst = (inst & NV50_PGRAPH_CTXCTL_CUR_INSTANCE) << 12;
  166. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  167. struct nouveau_channel *chan = dev_priv->fifos[i];
  168. if (chan && chan->ramin && chan->ramin->instance == inst)
  169. return chan;
  170. }
  171. return NULL;
  172. }
  173. int
  174. nv50_graph_create_context(struct nouveau_channel *chan)
  175. {
  176. struct drm_device *dev = chan->dev;
  177. struct drm_nouveau_private *dev_priv = dev->dev_private;
  178. struct nouveau_gpuobj *ramin = chan->ramin->gpuobj;
  179. struct nouveau_gpuobj *ctx;
  180. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  181. int hdr, ret;
  182. NV_DEBUG(dev, "ch%d\n", chan->id);
  183. ret = nouveau_gpuobj_new_ref(dev, chan, NULL, 0, pgraph->grctx_size,
  184. 0x1000, NVOBJ_FLAG_ZERO_ALLOC |
  185. NVOBJ_FLAG_ZERO_FREE, &chan->ramin_grctx);
  186. if (ret)
  187. return ret;
  188. ctx = chan->ramin_grctx->gpuobj;
  189. hdr = IS_G80 ? 0x200 : 0x20;
  190. dev_priv->engine.instmem.prepare_access(dev, true);
  191. nv_wo32(dev, ramin, (hdr + 0x00)/4, 0x00190002);
  192. nv_wo32(dev, ramin, (hdr + 0x04)/4, chan->ramin_grctx->instance +
  193. pgraph->grctx_size - 1);
  194. nv_wo32(dev, ramin, (hdr + 0x08)/4, chan->ramin_grctx->instance);
  195. nv_wo32(dev, ramin, (hdr + 0x0c)/4, 0);
  196. nv_wo32(dev, ramin, (hdr + 0x10)/4, 0);
  197. nv_wo32(dev, ramin, (hdr + 0x14)/4, 0x00010000);
  198. dev_priv->engine.instmem.finish_access(dev);
  199. dev_priv->engine.instmem.prepare_access(dev, true);
  200. if (!pgraph->ctxprog) {
  201. struct nouveau_grctx ctx = {};
  202. ctx.dev = chan->dev;
  203. ctx.mode = NOUVEAU_GRCTX_VALS;
  204. ctx.data = chan->ramin_grctx->gpuobj;
  205. nv50_grctx_init(&ctx);
  206. } else {
  207. nouveau_grctx_vals_load(dev, ctx);
  208. }
  209. nv_wo32(dev, ctx, 0x00000/4, chan->ramin->instance >> 12);
  210. dev_priv->engine.instmem.finish_access(dev);
  211. return 0;
  212. }
  213. void
  214. nv50_graph_destroy_context(struct nouveau_channel *chan)
  215. {
  216. struct drm_device *dev = chan->dev;
  217. struct drm_nouveau_private *dev_priv = dev->dev_private;
  218. int i, hdr = IS_G80 ? 0x200 : 0x20;
  219. NV_DEBUG(dev, "ch%d\n", chan->id);
  220. if (!chan->ramin || !chan->ramin->gpuobj)
  221. return;
  222. dev_priv->engine.instmem.prepare_access(dev, true);
  223. for (i = hdr; i < hdr + 24; i += 4)
  224. nv_wo32(dev, chan->ramin->gpuobj, i/4, 0);
  225. dev_priv->engine.instmem.finish_access(dev);
  226. nouveau_gpuobj_ref_del(dev, &chan->ramin_grctx);
  227. }
  228. static int
  229. nv50_graph_do_load_context(struct drm_device *dev, uint32_t inst)
  230. {
  231. uint32_t fifo = nv_rd32(dev, 0x400500);
  232. nv_wr32(dev, 0x400500, fifo & ~1);
  233. nv_wr32(dev, 0x400784, inst);
  234. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x40);
  235. nv_wr32(dev, 0x400320, nv_rd32(dev, 0x400320) | 0x11);
  236. nv_wr32(dev, 0x400040, 0xffffffff);
  237. (void)nv_rd32(dev, 0x400040);
  238. nv_wr32(dev, 0x400040, 0x00000000);
  239. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 1);
  240. if (nouveau_wait_for_idle(dev))
  241. nv_wr32(dev, 0x40032c, inst | (1<<31));
  242. nv_wr32(dev, 0x400500, fifo);
  243. return 0;
  244. }
  245. int
  246. nv50_graph_load_context(struct nouveau_channel *chan)
  247. {
  248. uint32_t inst = chan->ramin->instance >> 12;
  249. NV_DEBUG(chan->dev, "ch%d\n", chan->id);
  250. return nv50_graph_do_load_context(chan->dev, inst);
  251. }
  252. int
  253. nv50_graph_unload_context(struct drm_device *dev)
  254. {
  255. uint32_t inst;
  256. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  257. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  258. return 0;
  259. inst &= NV50_PGRAPH_CTXCTL_CUR_INSTANCE;
  260. nouveau_wait_for_idle(dev);
  261. nv_wr32(dev, 0x400784, inst);
  262. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x20);
  263. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 0x01);
  264. nouveau_wait_for_idle(dev);
  265. nv_wr32(dev, NV50_PGRAPH_CTXCTL_CUR, inst);
  266. return 0;
  267. }
  268. void
  269. nv50_graph_context_switch(struct drm_device *dev)
  270. {
  271. uint32_t inst;
  272. nv50_graph_unload_context(dev);
  273. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_NEXT);
  274. inst &= NV50_PGRAPH_CTXCTL_NEXT_INSTANCE;
  275. nv50_graph_do_load_context(dev, inst);
  276. nv_wr32(dev, NV40_PGRAPH_INTR_EN, nv_rd32(dev,
  277. NV40_PGRAPH_INTR_EN) | NV_PGRAPH_INTR_CONTEXT_SWITCH);
  278. }
  279. static int
  280. nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan, int grclass,
  281. int mthd, uint32_t data)
  282. {
  283. struct nouveau_gpuobj_ref *ref = NULL;
  284. if (nouveau_gpuobj_ref_find(chan, data, &ref))
  285. return -ENOENT;
  286. if (nouveau_notifier_offset(ref->gpuobj, NULL))
  287. return -EINVAL;
  288. chan->nvsw.vblsem = ref->gpuobj;
  289. chan->nvsw.vblsem_offset = ~0;
  290. return 0;
  291. }
  292. static int
  293. nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan, int grclass,
  294. int mthd, uint32_t data)
  295. {
  296. if (nouveau_notifier_offset(chan->nvsw.vblsem, &data))
  297. return -ERANGE;
  298. chan->nvsw.vblsem_offset = data >> 2;
  299. return 0;
  300. }
  301. static int
  302. nv50_graph_nvsw_vblsem_release_val(struct nouveau_channel *chan, int grclass,
  303. int mthd, uint32_t data)
  304. {
  305. chan->nvsw.vblsem_rval = data;
  306. return 0;
  307. }
  308. static int
  309. nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan, int grclass,
  310. int mthd, uint32_t data)
  311. {
  312. struct drm_device *dev = chan->dev;
  313. struct drm_nouveau_private *dev_priv = dev->dev_private;
  314. if (!chan->nvsw.vblsem || chan->nvsw.vblsem_offset == ~0 || data > 1)
  315. return -EINVAL;
  316. if (!(nv_rd32(dev, NV50_PDISPLAY_INTR_EN) &
  317. NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data))) {
  318. nv_wr32(dev, NV50_PDISPLAY_INTR_1,
  319. NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(data));
  320. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, nv_rd32(dev,
  321. NV50_PDISPLAY_INTR_EN) |
  322. NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data));
  323. }
  324. list_add(&chan->nvsw.vbl_wait, &dev_priv->vbl_waiting);
  325. return 0;
  326. }
  327. static struct nouveau_pgraph_object_method nv50_graph_nvsw_methods[] = {
  328. { 0x018c, nv50_graph_nvsw_dma_vblsem },
  329. { 0x0400, nv50_graph_nvsw_vblsem_offset },
  330. { 0x0404, nv50_graph_nvsw_vblsem_release_val },
  331. { 0x0408, nv50_graph_nvsw_vblsem_release },
  332. {}
  333. };
  334. struct nouveau_pgraph_object_class nv50_graph_grclass[] = {
  335. { 0x506e, true, nv50_graph_nvsw_methods }, /* nvsw */
  336. { 0x0030, false, NULL }, /* null */
  337. { 0x5039, false, NULL }, /* m2mf */
  338. { 0x502d, false, NULL }, /* 2d */
  339. { 0x50c0, false, NULL }, /* compute */
  340. { 0x85c0, false, NULL }, /* compute (nva3, nva5, nva8) */
  341. { 0x5097, false, NULL }, /* tesla (nv50) */
  342. { 0x8297, false, NULL }, /* tesla (nv8x/nv9x) */
  343. { 0x8397, false, NULL }, /* tesla (nva0, nvaa, nvac) */
  344. { 0x8597, false, NULL }, /* tesla (nva3, nva5, nva8) */
  345. {}
  346. };