nv50_graph.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 int nv50_graph_register(struct drm_device *);
  32. static void
  33. nv50_graph_init_reset(struct drm_device *dev)
  34. {
  35. uint32_t pmc_e = NV_PMC_ENABLE_PGRAPH | (1 << 21);
  36. NV_DEBUG(dev, "\n");
  37. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) & ~pmc_e);
  38. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) | pmc_e);
  39. }
  40. static void
  41. nv50_graph_init_intr(struct drm_device *dev)
  42. {
  43. NV_DEBUG(dev, "\n");
  44. nv_wr32(dev, NV03_PGRAPH_INTR, 0xffffffff);
  45. nv_wr32(dev, 0x400138, 0xffffffff);
  46. nv_wr32(dev, NV40_PGRAPH_INTR_EN, 0xffffffff);
  47. }
  48. static void
  49. nv50_graph_init_regs__nv(struct drm_device *dev)
  50. {
  51. struct drm_nouveau_private *dev_priv = dev->dev_private;
  52. uint32_t units = nv_rd32(dev, 0x1540);
  53. int i;
  54. NV_DEBUG(dev, "\n");
  55. nv_wr32(dev, 0x400804, 0xc0000000);
  56. nv_wr32(dev, 0x406800, 0xc0000000);
  57. nv_wr32(dev, 0x400c04, 0xc0000000);
  58. nv_wr32(dev, 0x401800, 0xc0000000);
  59. nv_wr32(dev, 0x405018, 0xc0000000);
  60. nv_wr32(dev, 0x402000, 0xc0000000);
  61. for (i = 0; i < 16; i++) {
  62. if (units & 1 << i) {
  63. if (dev_priv->chipset < 0xa0) {
  64. nv_wr32(dev, 0x408900 + (i << 12), 0xc0000000);
  65. nv_wr32(dev, 0x408e08 + (i << 12), 0xc0000000);
  66. nv_wr32(dev, 0x408314 + (i << 12), 0xc0000000);
  67. } else {
  68. nv_wr32(dev, 0x408600 + (i << 11), 0xc0000000);
  69. nv_wr32(dev, 0x408708 + (i << 11), 0xc0000000);
  70. nv_wr32(dev, 0x40831c + (i << 11), 0xc0000000);
  71. }
  72. }
  73. }
  74. nv_wr32(dev, 0x400108, 0xffffffff);
  75. nv_wr32(dev, 0x400824, 0x00004000);
  76. nv_wr32(dev, 0x400500, 0x00010001);
  77. }
  78. static void
  79. nv50_graph_init_regs(struct drm_device *dev)
  80. {
  81. NV_DEBUG(dev, "\n");
  82. nv_wr32(dev, NV04_PGRAPH_DEBUG_3,
  83. (1 << 2) /* HW_CONTEXT_SWITCH_ENABLED */);
  84. nv_wr32(dev, 0x402ca8, 0x800);
  85. }
  86. static int
  87. nv50_graph_init_ctxctl(struct drm_device *dev)
  88. {
  89. struct drm_nouveau_private *dev_priv = dev->dev_private;
  90. struct nouveau_grctx ctx = {};
  91. uint32_t *cp;
  92. int i;
  93. NV_DEBUG(dev, "\n");
  94. cp = kmalloc(512 * 4, GFP_KERNEL);
  95. if (!cp) {
  96. NV_ERROR(dev, "failed to allocate ctxprog\n");
  97. dev_priv->engine.graph.accel_blocked = true;
  98. return 0;
  99. }
  100. ctx.dev = dev;
  101. ctx.mode = NOUVEAU_GRCTX_PROG;
  102. ctx.data = cp;
  103. ctx.ctxprog_max = 512;
  104. if (!nv50_grctx_init(&ctx)) {
  105. dev_priv->engine.graph.grctx_size = ctx.ctxvals_pos * 4;
  106. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_INDEX, 0);
  107. for (i = 0; i < ctx.ctxprog_len; i++)
  108. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_DATA, cp[i]);
  109. } else {
  110. dev_priv->engine.graph.accel_blocked = true;
  111. }
  112. kfree(cp);
  113. nv_wr32(dev, 0x400320, 4);
  114. nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, 0);
  115. nv_wr32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER, 0);
  116. return 0;
  117. }
  118. int
  119. nv50_graph_init(struct drm_device *dev)
  120. {
  121. int ret;
  122. NV_DEBUG(dev, "\n");
  123. nv50_graph_init_reset(dev);
  124. nv50_graph_init_regs__nv(dev);
  125. nv50_graph_init_regs(dev);
  126. ret = nv50_graph_init_ctxctl(dev);
  127. if (ret)
  128. return ret;
  129. ret = nv50_graph_register(dev);
  130. if (ret)
  131. return ret;
  132. nv50_graph_init_intr(dev);
  133. return 0;
  134. }
  135. void
  136. nv50_graph_takedown(struct drm_device *dev)
  137. {
  138. NV_DEBUG(dev, "\n");
  139. }
  140. void
  141. nv50_graph_fifo_access(struct drm_device *dev, bool enabled)
  142. {
  143. const uint32_t mask = 0x00010001;
  144. if (enabled)
  145. nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) | mask);
  146. else
  147. nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) & ~mask);
  148. }
  149. struct nouveau_channel *
  150. nv50_graph_channel(struct drm_device *dev)
  151. {
  152. struct drm_nouveau_private *dev_priv = dev->dev_private;
  153. uint32_t inst;
  154. int i;
  155. /* Be sure we're not in the middle of a context switch or bad things
  156. * will happen, such as unloading the wrong pgraph context.
  157. */
  158. if (!nv_wait(dev, 0x400300, 0x00000001, 0x00000000))
  159. NV_ERROR(dev, "Ctxprog is still running\n");
  160. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  161. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  162. return NULL;
  163. inst = (inst & NV50_PGRAPH_CTXCTL_CUR_INSTANCE) << 12;
  164. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  165. struct nouveau_channel *chan = dev_priv->channels.ptr[i];
  166. if (chan && chan->ramin && chan->ramin->vinst == inst)
  167. return chan;
  168. }
  169. return NULL;
  170. }
  171. int
  172. nv50_graph_create_context(struct nouveau_channel *chan)
  173. {
  174. struct drm_device *dev = chan->dev;
  175. struct drm_nouveau_private *dev_priv = dev->dev_private;
  176. struct nouveau_gpuobj *ramin = chan->ramin;
  177. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  178. struct nouveau_grctx ctx = {};
  179. int hdr, ret;
  180. NV_DEBUG(dev, "ch%d\n", chan->id);
  181. ret = nouveau_gpuobj_new(dev, chan, pgraph->grctx_size, 0,
  182. NVOBJ_FLAG_ZERO_ALLOC |
  183. NVOBJ_FLAG_ZERO_FREE, &chan->ramin_grctx);
  184. if (ret)
  185. return ret;
  186. hdr = (dev_priv->chipset == 0x50) ? 0x200 : 0x20;
  187. nv_wo32(ramin, hdr + 0x00, 0x00190002);
  188. nv_wo32(ramin, hdr + 0x04, chan->ramin_grctx->vinst +
  189. pgraph->grctx_size - 1);
  190. nv_wo32(ramin, hdr + 0x08, chan->ramin_grctx->vinst);
  191. nv_wo32(ramin, hdr + 0x0c, 0);
  192. nv_wo32(ramin, hdr + 0x10, 0);
  193. nv_wo32(ramin, hdr + 0x14, 0x00010000);
  194. ctx.dev = chan->dev;
  195. ctx.mode = NOUVEAU_GRCTX_VALS;
  196. ctx.data = chan->ramin_grctx;
  197. nv50_grctx_init(&ctx);
  198. nv_wo32(chan->ramin_grctx, 0x00000, chan->ramin->vinst >> 12);
  199. dev_priv->engine.instmem.flush(dev);
  200. return 0;
  201. }
  202. void
  203. nv50_graph_destroy_context(struct nouveau_channel *chan)
  204. {
  205. struct drm_device *dev = chan->dev;
  206. struct drm_nouveau_private *dev_priv = dev->dev_private;
  207. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  208. int i, hdr = (dev_priv->chipset == 0x50) ? 0x200 : 0x20;
  209. unsigned long flags;
  210. NV_DEBUG(dev, "ch%d\n", chan->id);
  211. if (!chan->ramin)
  212. return;
  213. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  214. pgraph->fifo_access(dev, false);
  215. if (pgraph->channel(dev) == chan)
  216. pgraph->unload_context(dev);
  217. for (i = hdr; i < hdr + 24; i += 4)
  218. nv_wo32(chan->ramin, i, 0);
  219. dev_priv->engine.instmem.flush(dev);
  220. pgraph->fifo_access(dev, true);
  221. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  222. nouveau_gpuobj_ref(NULL, &chan->ramin_grctx);
  223. }
  224. static int
  225. nv50_graph_do_load_context(struct drm_device *dev, uint32_t inst)
  226. {
  227. uint32_t fifo = nv_rd32(dev, 0x400500);
  228. nv_wr32(dev, 0x400500, fifo & ~1);
  229. nv_wr32(dev, 0x400784, inst);
  230. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x40);
  231. nv_wr32(dev, 0x400320, nv_rd32(dev, 0x400320) | 0x11);
  232. nv_wr32(dev, 0x400040, 0xffffffff);
  233. (void)nv_rd32(dev, 0x400040);
  234. nv_wr32(dev, 0x400040, 0x00000000);
  235. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 1);
  236. if (nouveau_wait_for_idle(dev))
  237. nv_wr32(dev, 0x40032c, inst | (1<<31));
  238. nv_wr32(dev, 0x400500, fifo);
  239. return 0;
  240. }
  241. int
  242. nv50_graph_load_context(struct nouveau_channel *chan)
  243. {
  244. uint32_t inst = chan->ramin->vinst >> 12;
  245. NV_DEBUG(chan->dev, "ch%d\n", chan->id);
  246. return nv50_graph_do_load_context(chan->dev, inst);
  247. }
  248. int
  249. nv50_graph_unload_context(struct drm_device *dev)
  250. {
  251. uint32_t inst;
  252. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  253. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  254. return 0;
  255. inst &= NV50_PGRAPH_CTXCTL_CUR_INSTANCE;
  256. nouveau_wait_for_idle(dev);
  257. nv_wr32(dev, 0x400784, inst);
  258. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x20);
  259. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 0x01);
  260. nouveau_wait_for_idle(dev);
  261. nv_wr32(dev, NV50_PGRAPH_CTXCTL_CUR, inst);
  262. return 0;
  263. }
  264. void
  265. nv50_graph_context_switch(struct drm_device *dev)
  266. {
  267. uint32_t inst;
  268. nv50_graph_unload_context(dev);
  269. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_NEXT);
  270. inst &= NV50_PGRAPH_CTXCTL_NEXT_INSTANCE;
  271. nv50_graph_do_load_context(dev, inst);
  272. nv_wr32(dev, NV40_PGRAPH_INTR_EN, nv_rd32(dev,
  273. NV40_PGRAPH_INTR_EN) | NV_PGRAPH_INTR_CONTEXT_SWITCH);
  274. }
  275. static int
  276. nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan,
  277. u32 class, u32 mthd, u32 data)
  278. {
  279. struct nouveau_gpuobj *gpuobj;
  280. gpuobj = nouveau_ramht_find(chan, data);
  281. if (!gpuobj)
  282. return -ENOENT;
  283. if (nouveau_notifier_offset(gpuobj, NULL))
  284. return -EINVAL;
  285. chan->nvsw.vblsem = gpuobj;
  286. chan->nvsw.vblsem_offset = ~0;
  287. return 0;
  288. }
  289. static int
  290. nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan,
  291. u32 class, u32 mthd, u32 data)
  292. {
  293. if (nouveau_notifier_offset(chan->nvsw.vblsem, &data))
  294. return -ERANGE;
  295. chan->nvsw.vblsem_offset = data >> 2;
  296. return 0;
  297. }
  298. static int
  299. nv50_graph_nvsw_vblsem_release_val(struct nouveau_channel *chan,
  300. u32 class, u32 mthd, u32 data)
  301. {
  302. chan->nvsw.vblsem_rval = data;
  303. return 0;
  304. }
  305. static int
  306. nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan,
  307. u32 class, u32 mthd, u32 data)
  308. {
  309. struct drm_device *dev = chan->dev;
  310. struct drm_nouveau_private *dev_priv = dev->dev_private;
  311. if (!chan->nvsw.vblsem || chan->nvsw.vblsem_offset == ~0 || data > 1)
  312. return -EINVAL;
  313. if (!(nv_rd32(dev, NV50_PDISPLAY_INTR_EN_1) &
  314. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(data))) {
  315. nv_wr32(dev, NV50_PDISPLAY_INTR_1,
  316. NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(data));
  317. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
  318. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(data));
  319. }
  320. list_add(&chan->nvsw.vbl_wait, &dev_priv->vbl_waiting);
  321. return 0;
  322. }
  323. static int
  324. nv50_graph_register(struct drm_device *dev)
  325. {
  326. struct drm_nouveau_private *dev_priv = dev->dev_private;
  327. if (dev_priv->engine.graph.registered)
  328. return 0;
  329. NVOBJ_CLASS(dev, 0x506e, SW); /* nvsw */
  330. NVOBJ_MTHD (dev, 0x506e, 0x018c, nv50_graph_nvsw_dma_vblsem);
  331. NVOBJ_MTHD (dev, 0x506e, 0x0400, nv50_graph_nvsw_vblsem_offset);
  332. NVOBJ_MTHD (dev, 0x506e, 0x0404, nv50_graph_nvsw_vblsem_release_val);
  333. NVOBJ_MTHD (dev, 0x506e, 0x0408, nv50_graph_nvsw_vblsem_release);
  334. NVOBJ_CLASS(dev, 0x0030, GR); /* null */
  335. NVOBJ_CLASS(dev, 0x5039, GR); /* m2mf */
  336. NVOBJ_CLASS(dev, 0x502d, GR); /* 2d */
  337. NVOBJ_CLASS(dev, 0x50c0, GR); /* compute */
  338. NVOBJ_CLASS(dev, 0x85c0, GR); /* compute (nva3, nva5, nva8) */
  339. /* tesla */
  340. if (dev_priv->chipset == 0x50)
  341. NVOBJ_CLASS(dev, 0x5097, GR); /* tesla (nv50) */
  342. else
  343. if (dev_priv->chipset < 0xa0)
  344. NVOBJ_CLASS(dev, 0x8297, GR); /* tesla (nv8x/nv9x) */
  345. else {
  346. switch (dev_priv->chipset) {
  347. case 0xa0:
  348. case 0xaa:
  349. case 0xac:
  350. NVOBJ_CLASS(dev, 0x8397, GR);
  351. break;
  352. case 0xa3:
  353. case 0xa5:
  354. case 0xa8:
  355. NVOBJ_CLASS(dev, 0x8597, GR);
  356. break;
  357. case 0xaf:
  358. NVOBJ_CLASS(dev, 0x8697, GR);
  359. break;
  360. }
  361. }
  362. dev_priv->engine.graph.registered = true;
  363. return 0;
  364. }
  365. void
  366. nv50_graph_tlb_flush(struct drm_device *dev)
  367. {
  368. nv50_vm_flush(dev, 0);
  369. }
  370. void
  371. nv86_graph_tlb_flush(struct drm_device *dev)
  372. {
  373. struct drm_nouveau_private *dev_priv = dev->dev_private;
  374. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  375. bool idle, timeout = false;
  376. unsigned long flags;
  377. u64 start;
  378. u32 tmp;
  379. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  380. nv_mask(dev, 0x400500, 0x00000001, 0x00000000);
  381. start = ptimer->read(dev);
  382. do {
  383. idle = true;
  384. for (tmp = nv_rd32(dev, 0x400380); tmp && idle; tmp >>= 3) {
  385. if ((tmp & 7) == 1)
  386. idle = false;
  387. }
  388. for (tmp = nv_rd32(dev, 0x400384); tmp && idle; tmp >>= 3) {
  389. if ((tmp & 7) == 1)
  390. idle = false;
  391. }
  392. for (tmp = nv_rd32(dev, 0x400388); tmp && idle; tmp >>= 3) {
  393. if ((tmp & 7) == 1)
  394. idle = false;
  395. }
  396. } while (!idle && !(timeout = ptimer->read(dev) - start > 2000000000));
  397. if (timeout) {
  398. NV_ERROR(dev, "PGRAPH TLB flush idle timeout fail: "
  399. "0x%08x 0x%08x 0x%08x 0x%08x\n",
  400. nv_rd32(dev, 0x400700), nv_rd32(dev, 0x400380),
  401. nv_rd32(dev, 0x400384), nv_rd32(dev, 0x400388));
  402. }
  403. nv50_vm_flush(dev, 0);
  404. nv_mask(dev, 0x400500, 0x00000001, 0x00000001);
  405. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  406. }