nv50_graph.c 11 KB

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