nv50_graph.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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_ramht.h"
  30. #include "nouveau_grctx.h"
  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. struct nouveau_grctx ctx = {};
  90. uint32_t *cp;
  91. int i;
  92. NV_DEBUG(dev, "\n");
  93. cp = kmalloc(512 * 4, GFP_KERNEL);
  94. if (!cp) {
  95. NV_ERROR(dev, "failed to allocate ctxprog\n");
  96. dev_priv->engine.graph.accel_blocked = true;
  97. return 0;
  98. }
  99. ctx.dev = dev;
  100. ctx.mode = NOUVEAU_GRCTX_PROG;
  101. ctx.data = cp;
  102. ctx.ctxprog_max = 512;
  103. if (!nv50_grctx_init(&ctx)) {
  104. dev_priv->engine.graph.grctx_size = ctx.ctxvals_pos * 4;
  105. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_INDEX, 0);
  106. for (i = 0; i < ctx.ctxprog_len; i++)
  107. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_DATA, cp[i]);
  108. } else {
  109. dev_priv->engine.graph.accel_blocked = true;
  110. }
  111. kfree(cp);
  112. nv_wr32(dev, 0x400320, 4);
  113. nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, 0);
  114. nv_wr32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER, 0);
  115. return 0;
  116. }
  117. int
  118. nv50_graph_init(struct drm_device *dev)
  119. {
  120. int ret;
  121. NV_DEBUG(dev, "\n");
  122. nv50_graph_init_reset(dev);
  123. nv50_graph_init_regs__nv(dev);
  124. nv50_graph_init_regs(dev);
  125. nv50_graph_init_intr(dev);
  126. ret = nv50_graph_init_ctxctl(dev);
  127. if (ret)
  128. return ret;
  129. return 0;
  130. }
  131. void
  132. nv50_graph_takedown(struct drm_device *dev)
  133. {
  134. NV_DEBUG(dev, "\n");
  135. }
  136. void
  137. nv50_graph_fifo_access(struct drm_device *dev, bool enabled)
  138. {
  139. const uint32_t mask = 0x00010001;
  140. if (enabled)
  141. nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) | mask);
  142. else
  143. nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) & ~mask);
  144. }
  145. struct nouveau_channel *
  146. nv50_graph_channel(struct drm_device *dev)
  147. {
  148. struct drm_nouveau_private *dev_priv = dev->dev_private;
  149. uint32_t inst;
  150. int i;
  151. /* Be sure we're not in the middle of a context switch or bad things
  152. * will happen, such as unloading the wrong pgraph context.
  153. */
  154. if (!nv_wait(dev, 0x400300, 0x00000001, 0x00000000))
  155. NV_ERROR(dev, "Ctxprog is still running\n");
  156. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  157. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  158. return NULL;
  159. inst = (inst & NV50_PGRAPH_CTXCTL_CUR_INSTANCE) << 12;
  160. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  161. struct nouveau_channel *chan = dev_priv->fifos[i];
  162. if (chan && chan->ramin && chan->ramin->vinst == inst)
  163. return chan;
  164. }
  165. return NULL;
  166. }
  167. int
  168. nv50_graph_create_context(struct nouveau_channel *chan)
  169. {
  170. struct drm_device *dev = chan->dev;
  171. struct drm_nouveau_private *dev_priv = dev->dev_private;
  172. struct nouveau_gpuobj *ramin = chan->ramin;
  173. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  174. struct nouveau_grctx ctx = {};
  175. int hdr, ret;
  176. NV_DEBUG(dev, "ch%d\n", chan->id);
  177. ret = nouveau_gpuobj_new(dev, chan, pgraph->grctx_size, 0x1000,
  178. NVOBJ_FLAG_ZERO_ALLOC |
  179. NVOBJ_FLAG_ZERO_FREE, &chan->ramin_grctx);
  180. if (ret)
  181. return ret;
  182. hdr = (dev_priv->chipset == 0x50) ? 0x200 : 0x20;
  183. nv_wo32(ramin, hdr + 0x00, 0x00190002);
  184. nv_wo32(ramin, hdr + 0x04, chan->ramin_grctx->vinst +
  185. pgraph->grctx_size - 1);
  186. nv_wo32(ramin, hdr + 0x08, chan->ramin_grctx->vinst);
  187. nv_wo32(ramin, hdr + 0x0c, 0);
  188. nv_wo32(ramin, hdr + 0x10, 0);
  189. nv_wo32(ramin, hdr + 0x14, 0x00010000);
  190. ctx.dev = chan->dev;
  191. ctx.mode = NOUVEAU_GRCTX_VALS;
  192. ctx.data = chan->ramin_grctx;
  193. nv50_grctx_init(&ctx);
  194. nv_wo32(chan->ramin_grctx, 0x00000, chan->ramin->vinst >> 12);
  195. dev_priv->engine.instmem.flush(dev);
  196. return 0;
  197. }
  198. void
  199. nv50_graph_destroy_context(struct nouveau_channel *chan)
  200. {
  201. struct drm_device *dev = chan->dev;
  202. struct drm_nouveau_private *dev_priv = dev->dev_private;
  203. int i, hdr = (dev_priv->chipset == 0x50) ? 0x200 : 0x20;
  204. NV_DEBUG(dev, "ch%d\n", chan->id);
  205. if (!chan->ramin)
  206. return;
  207. for (i = hdr; i < hdr + 24; i += 4)
  208. nv_wo32(chan->ramin, i, 0);
  209. dev_priv->engine.instmem.flush(dev);
  210. nouveau_gpuobj_ref(NULL, &chan->ramin_grctx);
  211. }
  212. static int
  213. nv50_graph_do_load_context(struct drm_device *dev, uint32_t inst)
  214. {
  215. uint32_t fifo = nv_rd32(dev, 0x400500);
  216. nv_wr32(dev, 0x400500, fifo & ~1);
  217. nv_wr32(dev, 0x400784, inst);
  218. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x40);
  219. nv_wr32(dev, 0x400320, nv_rd32(dev, 0x400320) | 0x11);
  220. nv_wr32(dev, 0x400040, 0xffffffff);
  221. (void)nv_rd32(dev, 0x400040);
  222. nv_wr32(dev, 0x400040, 0x00000000);
  223. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 1);
  224. if (nouveau_wait_for_idle(dev))
  225. nv_wr32(dev, 0x40032c, inst | (1<<31));
  226. nv_wr32(dev, 0x400500, fifo);
  227. return 0;
  228. }
  229. int
  230. nv50_graph_load_context(struct nouveau_channel *chan)
  231. {
  232. uint32_t inst = chan->ramin->vinst >> 12;
  233. NV_DEBUG(chan->dev, "ch%d\n", chan->id);
  234. return nv50_graph_do_load_context(chan->dev, inst);
  235. }
  236. int
  237. nv50_graph_unload_context(struct drm_device *dev)
  238. {
  239. uint32_t inst;
  240. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  241. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  242. return 0;
  243. inst &= NV50_PGRAPH_CTXCTL_CUR_INSTANCE;
  244. nouveau_wait_for_idle(dev);
  245. nv_wr32(dev, 0x400784, inst);
  246. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x20);
  247. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 0x01);
  248. nouveau_wait_for_idle(dev);
  249. nv_wr32(dev, NV50_PGRAPH_CTXCTL_CUR, inst);
  250. return 0;
  251. }
  252. void
  253. nv50_graph_context_switch(struct drm_device *dev)
  254. {
  255. uint32_t inst;
  256. nv50_graph_unload_context(dev);
  257. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_NEXT);
  258. inst &= NV50_PGRAPH_CTXCTL_NEXT_INSTANCE;
  259. nv50_graph_do_load_context(dev, inst);
  260. nv_wr32(dev, NV40_PGRAPH_INTR_EN, nv_rd32(dev,
  261. NV40_PGRAPH_INTR_EN) | NV_PGRAPH_INTR_CONTEXT_SWITCH);
  262. }
  263. static int
  264. nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan, int grclass,
  265. int mthd, uint32_t data)
  266. {
  267. struct nouveau_gpuobj *gpuobj;
  268. gpuobj = nouveau_ramht_find(chan, data);
  269. if (!gpuobj)
  270. return -ENOENT;
  271. if (nouveau_notifier_offset(gpuobj, NULL))
  272. return -EINVAL;
  273. chan->nvsw.vblsem = gpuobj;
  274. chan->nvsw.vblsem_offset = ~0;
  275. return 0;
  276. }
  277. static int
  278. nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan, int grclass,
  279. int mthd, uint32_t data)
  280. {
  281. if (nouveau_notifier_offset(chan->nvsw.vblsem, &data))
  282. return -ERANGE;
  283. chan->nvsw.vblsem_offset = data >> 2;
  284. return 0;
  285. }
  286. static int
  287. nv50_graph_nvsw_vblsem_release_val(struct nouveau_channel *chan, int grclass,
  288. int mthd, uint32_t data)
  289. {
  290. chan->nvsw.vblsem_rval = data;
  291. return 0;
  292. }
  293. static int
  294. nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan, int grclass,
  295. int mthd, uint32_t data)
  296. {
  297. struct drm_device *dev = chan->dev;
  298. struct drm_nouveau_private *dev_priv = dev->dev_private;
  299. if (!chan->nvsw.vblsem || chan->nvsw.vblsem_offset == ~0 || data > 1)
  300. return -EINVAL;
  301. if (!(nv_rd32(dev, NV50_PDISPLAY_INTR_EN) &
  302. NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data))) {
  303. nv_wr32(dev, NV50_PDISPLAY_INTR_1,
  304. NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(data));
  305. nv_wr32(dev, NV50_PDISPLAY_INTR_EN, nv_rd32(dev,
  306. NV50_PDISPLAY_INTR_EN) |
  307. NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data));
  308. }
  309. list_add(&chan->nvsw.vbl_wait, &dev_priv->vbl_waiting);
  310. return 0;
  311. }
  312. static struct nouveau_pgraph_object_method nv50_graph_nvsw_methods[] = {
  313. { 0x018c, nv50_graph_nvsw_dma_vblsem },
  314. { 0x0400, nv50_graph_nvsw_vblsem_offset },
  315. { 0x0404, nv50_graph_nvsw_vblsem_release_val },
  316. { 0x0408, nv50_graph_nvsw_vblsem_release },
  317. {}
  318. };
  319. struct nouveau_pgraph_object_class nv50_graph_grclass[] = {
  320. { 0x506e, true, nv50_graph_nvsw_methods }, /* nvsw */
  321. { 0x0030, false, NULL }, /* null */
  322. { 0x5039, false, NULL }, /* m2mf */
  323. { 0x502d, false, NULL }, /* 2d */
  324. { 0x50c0, false, NULL }, /* compute */
  325. { 0x85c0, false, NULL }, /* compute (nva3, nva5, nva8) */
  326. { 0x5097, false, NULL }, /* tesla (nv50) */
  327. { 0x8297, false, NULL }, /* tesla (nv8x/nv9x) */
  328. { 0x8397, false, NULL }, /* tesla (nva0, nvaa, nvac) */
  329. { 0x8597, false, NULL }, /* tesla (nva3, nva5, nva8) */
  330. {}
  331. };
  332. void
  333. nv50_graph_tlb_flush(struct drm_device *dev)
  334. {
  335. nv50_vm_flush(dev, 0);
  336. }
  337. void
  338. nv86_graph_tlb_flush(struct drm_device *dev)
  339. {
  340. struct drm_nouveau_private *dev_priv = dev->dev_private;
  341. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  342. bool idle, timeout = false;
  343. unsigned long flags;
  344. u64 start;
  345. u32 tmp;
  346. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  347. nv_mask(dev, 0x400500, 0x00000001, 0x00000000);
  348. start = ptimer->read(dev);
  349. do {
  350. idle = true;
  351. for (tmp = nv_rd32(dev, 0x400380); tmp && idle; tmp >>= 3) {
  352. if ((tmp & 7) == 1)
  353. idle = false;
  354. }
  355. for (tmp = nv_rd32(dev, 0x400384); tmp && idle; tmp >>= 3) {
  356. if ((tmp & 7) == 1)
  357. idle = false;
  358. }
  359. for (tmp = nv_rd32(dev, 0x400388); tmp && idle; tmp >>= 3) {
  360. if ((tmp & 7) == 1)
  361. idle = false;
  362. }
  363. } while (!idle && !(timeout = ptimer->read(dev) - start > 2000000000));
  364. if (timeout) {
  365. NV_ERROR(dev, "PGRAPH TLB flush idle timeout fail: "
  366. "0x%08x 0x%08x 0x%08x 0x%08x\n",
  367. nv_rd32(dev, 0x400700), nv_rd32(dev, 0x400380),
  368. nv_rd32(dev, 0x400384), nv_rd32(dev, 0x400388));
  369. }
  370. nv50_vm_flush(dev, 0);
  371. nv_mask(dev, 0x400500, 0x00000001, 0x00000001);
  372. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  373. }