nv50_graph.c 10 KB

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