nv50_graph.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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, 0x401804, 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. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  141. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  142. return NULL;
  143. inst = (inst & NV50_PGRAPH_CTXCTL_CUR_INSTANCE) << 12;
  144. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  145. struct nouveau_channel *chan = dev_priv->fifos[i];
  146. if (chan && chan->ramin && chan->ramin->instance == inst)
  147. return chan;
  148. }
  149. return NULL;
  150. }
  151. int
  152. nv50_graph_create_context(struct nouveau_channel *chan)
  153. {
  154. struct drm_device *dev = chan->dev;
  155. struct drm_nouveau_private *dev_priv = dev->dev_private;
  156. struct nouveau_gpuobj *ramin = chan->ramin->gpuobj;
  157. struct nouveau_gpuobj *ctx;
  158. uint32_t grctx_size = 0x70000;
  159. int hdr, ret;
  160. NV_DEBUG(dev, "ch%d\n", chan->id);
  161. ret = nouveau_gpuobj_new_ref(dev, chan, NULL, 0, grctx_size, 0x1000,
  162. NVOBJ_FLAG_ZERO_ALLOC |
  163. NVOBJ_FLAG_ZERO_FREE, &chan->ramin_grctx);
  164. if (ret)
  165. return ret;
  166. ctx = chan->ramin_grctx->gpuobj;
  167. hdr = IS_G80 ? 0x200 : 0x20;
  168. dev_priv->engine.instmem.prepare_access(dev, true);
  169. nv_wo32(dev, ramin, (hdr + 0x00)/4, 0x00190002);
  170. nv_wo32(dev, ramin, (hdr + 0x04)/4, chan->ramin_grctx->instance +
  171. grctx_size - 1);
  172. nv_wo32(dev, ramin, (hdr + 0x08)/4, chan->ramin_grctx->instance);
  173. nv_wo32(dev, ramin, (hdr + 0x0c)/4, 0);
  174. nv_wo32(dev, ramin, (hdr + 0x10)/4, 0);
  175. nv_wo32(dev, ramin, (hdr + 0x14)/4, 0x00010000);
  176. dev_priv->engine.instmem.finish_access(dev);
  177. dev_priv->engine.instmem.prepare_access(dev, true);
  178. nouveau_grctx_vals_load(dev, ctx);
  179. nv_wo32(dev, ctx, 0x00000/4, chan->ramin->instance >> 12);
  180. if ((dev_priv->chipset & 0xf0) == 0xa0)
  181. nv_wo32(dev, ctx, 0x00004/4, 0x00000000);
  182. else
  183. nv_wo32(dev, ctx, 0x0011c/4, 0x00000000);
  184. dev_priv->engine.instmem.finish_access(dev);
  185. return 0;
  186. }
  187. void
  188. nv50_graph_destroy_context(struct nouveau_channel *chan)
  189. {
  190. struct drm_device *dev = chan->dev;
  191. struct drm_nouveau_private *dev_priv = dev->dev_private;
  192. int i, hdr = IS_G80 ? 0x200 : 0x20;
  193. NV_DEBUG(dev, "ch%d\n", chan->id);
  194. if (!chan->ramin || !chan->ramin->gpuobj)
  195. return;
  196. dev_priv->engine.instmem.prepare_access(dev, true);
  197. for (i = hdr; i < hdr + 24; i += 4)
  198. nv_wo32(dev, chan->ramin->gpuobj, i/4, 0);
  199. dev_priv->engine.instmem.finish_access(dev);
  200. nouveau_gpuobj_ref_del(dev, &chan->ramin_grctx);
  201. }
  202. static int
  203. nv50_graph_do_load_context(struct drm_device *dev, uint32_t inst)
  204. {
  205. uint32_t fifo = nv_rd32(dev, 0x400500);
  206. nv_wr32(dev, 0x400500, fifo & ~1);
  207. nv_wr32(dev, 0x400784, inst);
  208. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x40);
  209. nv_wr32(dev, 0x400320, nv_rd32(dev, 0x400320) | 0x11);
  210. nv_wr32(dev, 0x400040, 0xffffffff);
  211. (void)nv_rd32(dev, 0x400040);
  212. nv_wr32(dev, 0x400040, 0x00000000);
  213. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 1);
  214. if (nouveau_wait_for_idle(dev))
  215. nv_wr32(dev, 0x40032c, inst | (1<<31));
  216. nv_wr32(dev, 0x400500, fifo);
  217. return 0;
  218. }
  219. int
  220. nv50_graph_load_context(struct nouveau_channel *chan)
  221. {
  222. uint32_t inst = chan->ramin->instance >> 12;
  223. NV_DEBUG(chan->dev, "ch%d\n", chan->id);
  224. return nv50_graph_do_load_context(chan->dev, inst);
  225. }
  226. int
  227. nv50_graph_unload_context(struct drm_device *dev)
  228. {
  229. uint32_t inst, fifo = nv_rd32(dev, 0x400500);
  230. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  231. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  232. return 0;
  233. inst &= NV50_PGRAPH_CTXCTL_CUR_INSTANCE;
  234. nv_wr32(dev, 0x400500, fifo & ~1);
  235. nv_wr32(dev, 0x400784, inst);
  236. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x20);
  237. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 0x01);
  238. nouveau_wait_for_idle(dev);
  239. nv_wr32(dev, 0x400500, fifo);
  240. nv_wr32(dev, NV50_PGRAPH_CTXCTL_CUR, inst);
  241. return 0;
  242. }
  243. void
  244. nv50_graph_context_switch(struct drm_device *dev)
  245. {
  246. uint32_t inst;
  247. nv50_graph_unload_context(dev);
  248. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_NEXT);
  249. inst &= NV50_PGRAPH_CTXCTL_NEXT_INSTANCE;
  250. nv50_graph_do_load_context(dev, inst);
  251. nv_wr32(dev, NV40_PGRAPH_INTR_EN, nv_rd32(dev,
  252. NV40_PGRAPH_INTR_EN) | NV_PGRAPH_INTR_CONTEXT_SWITCH);
  253. }
  254. static int
  255. nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan, int grclass,
  256. int mthd, uint32_t data)
  257. {
  258. struct nouveau_gpuobj_ref *ref = NULL;
  259. if (nouveau_gpuobj_ref_find(chan, data, &ref))
  260. return -ENOENT;
  261. if (nouveau_notifier_offset(ref->gpuobj, NULL))
  262. return -EINVAL;
  263. chan->nvsw.vblsem = ref->gpuobj;
  264. chan->nvsw.vblsem_offset = ~0;
  265. return 0;
  266. }
  267. static int
  268. nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan, int grclass,
  269. int mthd, uint32_t data)
  270. {
  271. if (nouveau_notifier_offset(chan->nvsw.vblsem, &data))
  272. return -ERANGE;
  273. chan->nvsw.vblsem_offset = data >> 2;
  274. return 0;
  275. }
  276. static int
  277. nv50_graph_nvsw_vblsem_release_val(struct nouveau_channel *chan, int grclass,
  278. int mthd, uint32_t data)
  279. {
  280. chan->nvsw.vblsem_rval = data;
  281. return 0;
  282. }
  283. static int
  284. nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan, int grclass,
  285. int mthd, uint32_t data)
  286. {
  287. struct drm_device *dev = chan->dev;
  288. struct drm_nouveau_private *dev_priv = dev->dev_private;
  289. if (!chan->nvsw.vblsem || chan->nvsw.vblsem_offset == ~0 || data > 1)
  290. return -EINVAL;
  291. if (!(nv_rd32(dev, NV50_PDISPLAY_INTR_EN) &
  292. NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data))) {
  293. nv_wr32(dev, NV50_PDISPLAY_INTR_1,
  294. NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(data));
  295. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, nv_rd32(dev,
  296. NV50_PDISPLAY_INTR_EN) |
  297. NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data));
  298. }
  299. list_add(&chan->nvsw.vbl_wait, &dev_priv->vbl_waiting);
  300. return 0;
  301. }
  302. static struct nouveau_pgraph_object_method nv50_graph_nvsw_methods[] = {
  303. { 0x018c, nv50_graph_nvsw_dma_vblsem },
  304. { 0x0400, nv50_graph_nvsw_vblsem_offset },
  305. { 0x0404, nv50_graph_nvsw_vblsem_release_val },
  306. { 0x0408, nv50_graph_nvsw_vblsem_release },
  307. {}
  308. };
  309. struct nouveau_pgraph_object_class nv50_graph_grclass[] = {
  310. { 0x506e, true, nv50_graph_nvsw_methods }, /* nvsw */
  311. { 0x0030, false, NULL }, /* null */
  312. { 0x5039, false, NULL }, /* m2mf */
  313. { 0x502d, false, NULL }, /* 2d */
  314. { 0x50c0, false, NULL }, /* compute */
  315. { 0x5097, false, NULL }, /* tesla (nv50) */
  316. { 0x8297, false, NULL }, /* tesla (nv80/nv90) */
  317. { 0x8397, false, NULL }, /* tesla (nva0) */
  318. { 0x8597, false, NULL }, /* tesla (nva8) */
  319. {}
  320. };