nv50_graph.c 11 KB

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