nv50_graph.c 13 KB

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