nv50_graph.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 "nouveau_vm.h"
  33. #include "nv50_evo.h"
  34. static int nv50_graph_register(struct drm_device *);
  35. static void nv50_graph_isr(struct drm_device *);
  36. static void
  37. nv50_graph_init_reset(struct drm_device *dev)
  38. {
  39. uint32_t pmc_e = NV_PMC_ENABLE_PGRAPH | (1 << 21);
  40. NV_DEBUG(dev, "\n");
  41. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) & ~pmc_e);
  42. nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) | pmc_e);
  43. }
  44. static void
  45. nv50_graph_init_intr(struct drm_device *dev)
  46. {
  47. NV_DEBUG(dev, "\n");
  48. nouveau_irq_register(dev, 12, nv50_graph_isr);
  49. nv_wr32(dev, NV03_PGRAPH_INTR, 0xffffffff);
  50. nv_wr32(dev, 0x400138, 0xffffffff);
  51. nv_wr32(dev, NV40_PGRAPH_INTR_EN, 0xffffffff);
  52. }
  53. static void
  54. nv50_graph_init_regs__nv(struct drm_device *dev)
  55. {
  56. struct drm_nouveau_private *dev_priv = dev->dev_private;
  57. uint32_t units = nv_rd32(dev, 0x1540);
  58. int i;
  59. NV_DEBUG(dev, "\n");
  60. nv_wr32(dev, 0x400804, 0xc0000000);
  61. nv_wr32(dev, 0x406800, 0xc0000000);
  62. nv_wr32(dev, 0x400c04, 0xc0000000);
  63. nv_wr32(dev, 0x401800, 0xc0000000);
  64. nv_wr32(dev, 0x405018, 0xc0000000);
  65. nv_wr32(dev, 0x402000, 0xc0000000);
  66. for (i = 0; i < 16; i++) {
  67. if (units & 1 << i) {
  68. if (dev_priv->chipset < 0xa0) {
  69. nv_wr32(dev, 0x408900 + (i << 12), 0xc0000000);
  70. nv_wr32(dev, 0x408e08 + (i << 12), 0xc0000000);
  71. nv_wr32(dev, 0x408314 + (i << 12), 0xc0000000);
  72. } else {
  73. nv_wr32(dev, 0x408600 + (i << 11), 0xc0000000);
  74. nv_wr32(dev, 0x408708 + (i << 11), 0xc0000000);
  75. nv_wr32(dev, 0x40831c + (i << 11), 0xc0000000);
  76. }
  77. }
  78. }
  79. nv_wr32(dev, 0x400108, 0xffffffff);
  80. nv_wr32(dev, 0x400824, 0x00004000);
  81. nv_wr32(dev, 0x400500, 0x00010001);
  82. }
  83. static void
  84. nv50_graph_init_regs(struct drm_device *dev)
  85. {
  86. NV_DEBUG(dev, "\n");
  87. nv_wr32(dev, NV04_PGRAPH_DEBUG_3,
  88. (1 << 2) /* HW_CONTEXT_SWITCH_ENABLED */);
  89. nv_wr32(dev, 0x402ca8, 0x800);
  90. }
  91. static int
  92. nv50_graph_init_ctxctl(struct drm_device *dev)
  93. {
  94. struct drm_nouveau_private *dev_priv = dev->dev_private;
  95. struct nouveau_grctx ctx = {};
  96. uint32_t *cp;
  97. int i;
  98. NV_DEBUG(dev, "\n");
  99. cp = kmalloc(512 * 4, GFP_KERNEL);
  100. if (!cp) {
  101. NV_ERROR(dev, "failed to allocate ctxprog\n");
  102. dev_priv->engine.graph.accel_blocked = true;
  103. return 0;
  104. }
  105. ctx.dev = dev;
  106. ctx.mode = NOUVEAU_GRCTX_PROG;
  107. ctx.data = cp;
  108. ctx.ctxprog_max = 512;
  109. if (!nv50_grctx_init(&ctx)) {
  110. dev_priv->engine.graph.grctx_size = ctx.ctxvals_pos * 4;
  111. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_INDEX, 0);
  112. for (i = 0; i < ctx.ctxprog_len; i++)
  113. nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_DATA, cp[i]);
  114. } else {
  115. dev_priv->engine.graph.accel_blocked = true;
  116. }
  117. kfree(cp);
  118. nv_wr32(dev, 0x400320, 4);
  119. nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, 0);
  120. nv_wr32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER, 0);
  121. return 0;
  122. }
  123. int
  124. nv50_graph_init(struct drm_device *dev)
  125. {
  126. int ret;
  127. NV_DEBUG(dev, "\n");
  128. nv50_graph_init_reset(dev);
  129. nv50_graph_init_regs__nv(dev);
  130. nv50_graph_init_regs(dev);
  131. ret = nv50_graph_init_ctxctl(dev);
  132. if (ret)
  133. return ret;
  134. ret = nv50_graph_register(dev);
  135. if (ret)
  136. return ret;
  137. nv50_graph_init_intr(dev);
  138. return 0;
  139. }
  140. void
  141. nv50_graph_takedown(struct drm_device *dev)
  142. {
  143. NV_DEBUG(dev, "\n");
  144. nv_wr32(dev, 0x40013c, 0x00000000);
  145. nouveau_irq_unregister(dev, 12);
  146. }
  147. void
  148. nv50_graph_fifo_access(struct drm_device *dev, bool enabled)
  149. {
  150. const uint32_t mask = 0x00010001;
  151. if (enabled)
  152. nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) | mask);
  153. else
  154. nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) & ~mask);
  155. }
  156. struct nouveau_channel *
  157. nv50_graph_channel(struct drm_device *dev)
  158. {
  159. struct drm_nouveau_private *dev_priv = dev->dev_private;
  160. uint32_t inst;
  161. int i;
  162. /* Be sure we're not in the middle of a context switch or bad things
  163. * will happen, such as unloading the wrong pgraph context.
  164. */
  165. if (!nv_wait(dev, 0x400300, 0x00000001, 0x00000000))
  166. NV_ERROR(dev, "Ctxprog is still running\n");
  167. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  168. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  169. return NULL;
  170. inst = (inst & NV50_PGRAPH_CTXCTL_CUR_INSTANCE) << 12;
  171. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  172. struct nouveau_channel *chan = dev_priv->channels.ptr[i];
  173. if (chan && chan->ramin && chan->ramin->vinst == inst)
  174. return chan;
  175. }
  176. return NULL;
  177. }
  178. int
  179. nv50_graph_create_context(struct nouveau_channel *chan)
  180. {
  181. struct drm_device *dev = chan->dev;
  182. struct drm_nouveau_private *dev_priv = dev->dev_private;
  183. struct nouveau_gpuobj *ramin = chan->ramin;
  184. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  185. struct nouveau_grctx ctx = {};
  186. int hdr, ret;
  187. NV_DEBUG(dev, "ch%d\n", chan->id);
  188. ret = nouveau_gpuobj_new(dev, chan, pgraph->grctx_size, 0,
  189. NVOBJ_FLAG_ZERO_ALLOC |
  190. NVOBJ_FLAG_ZERO_FREE, &chan->ramin_grctx);
  191. if (ret)
  192. return ret;
  193. hdr = (dev_priv->chipset == 0x50) ? 0x200 : 0x20;
  194. nv_wo32(ramin, hdr + 0x00, 0x00190002);
  195. nv_wo32(ramin, hdr + 0x04, chan->ramin_grctx->vinst +
  196. pgraph->grctx_size - 1);
  197. nv_wo32(ramin, hdr + 0x08, chan->ramin_grctx->vinst);
  198. nv_wo32(ramin, hdr + 0x0c, 0);
  199. nv_wo32(ramin, hdr + 0x10, 0);
  200. nv_wo32(ramin, hdr + 0x14, 0x00010000);
  201. ctx.dev = chan->dev;
  202. ctx.mode = NOUVEAU_GRCTX_VALS;
  203. ctx.data = chan->ramin_grctx;
  204. nv50_grctx_init(&ctx);
  205. nv_wo32(chan->ramin_grctx, 0x00000, chan->ramin->vinst >> 12);
  206. dev_priv->engine.instmem.flush(dev);
  207. return 0;
  208. }
  209. void
  210. nv50_graph_destroy_context(struct nouveau_channel *chan)
  211. {
  212. struct drm_device *dev = chan->dev;
  213. struct drm_nouveau_private *dev_priv = dev->dev_private;
  214. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  215. int i, hdr = (dev_priv->chipset == 0x50) ? 0x200 : 0x20;
  216. unsigned long flags;
  217. NV_DEBUG(dev, "ch%d\n", chan->id);
  218. if (!chan->ramin)
  219. return;
  220. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  221. pgraph->fifo_access(dev, false);
  222. if (pgraph->channel(dev) == chan)
  223. pgraph->unload_context(dev);
  224. for (i = hdr; i < hdr + 24; i += 4)
  225. nv_wo32(chan->ramin, i, 0);
  226. dev_priv->engine.instmem.flush(dev);
  227. pgraph->fifo_access(dev, true);
  228. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  229. nouveau_gpuobj_ref(NULL, &chan->ramin_grctx);
  230. }
  231. static int
  232. nv50_graph_do_load_context(struct drm_device *dev, uint32_t inst)
  233. {
  234. uint32_t fifo = nv_rd32(dev, 0x400500);
  235. nv_wr32(dev, 0x400500, fifo & ~1);
  236. nv_wr32(dev, 0x400784, inst);
  237. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x40);
  238. nv_wr32(dev, 0x400320, nv_rd32(dev, 0x400320) | 0x11);
  239. nv_wr32(dev, 0x400040, 0xffffffff);
  240. (void)nv_rd32(dev, 0x400040);
  241. nv_wr32(dev, 0x400040, 0x00000000);
  242. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 1);
  243. if (nouveau_wait_for_idle(dev))
  244. nv_wr32(dev, 0x40032c, inst | (1<<31));
  245. nv_wr32(dev, 0x400500, fifo);
  246. return 0;
  247. }
  248. int
  249. nv50_graph_load_context(struct nouveau_channel *chan)
  250. {
  251. uint32_t inst = chan->ramin->vinst >> 12;
  252. NV_DEBUG(chan->dev, "ch%d\n", chan->id);
  253. return nv50_graph_do_load_context(chan->dev, inst);
  254. }
  255. int
  256. nv50_graph_unload_context(struct drm_device *dev)
  257. {
  258. uint32_t inst;
  259. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
  260. if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
  261. return 0;
  262. inst &= NV50_PGRAPH_CTXCTL_CUR_INSTANCE;
  263. nouveau_wait_for_idle(dev);
  264. nv_wr32(dev, 0x400784, inst);
  265. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x20);
  266. nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 0x01);
  267. nouveau_wait_for_idle(dev);
  268. nv_wr32(dev, NV50_PGRAPH_CTXCTL_CUR, inst);
  269. return 0;
  270. }
  271. static void
  272. nv50_graph_context_switch(struct drm_device *dev)
  273. {
  274. uint32_t inst;
  275. nv50_graph_unload_context(dev);
  276. inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_NEXT);
  277. inst &= NV50_PGRAPH_CTXCTL_NEXT_INSTANCE;
  278. nv50_graph_do_load_context(dev, inst);
  279. nv_wr32(dev, NV40_PGRAPH_INTR_EN, nv_rd32(dev,
  280. NV40_PGRAPH_INTR_EN) | NV_PGRAPH_INTR_CONTEXT_SWITCH);
  281. }
  282. static int
  283. nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan,
  284. u32 class, u32 mthd, u32 data)
  285. {
  286. struct nouveau_gpuobj *gpuobj;
  287. gpuobj = nouveau_ramht_find(chan, data);
  288. if (!gpuobj)
  289. return -ENOENT;
  290. if (nouveau_notifier_offset(gpuobj, NULL))
  291. return -EINVAL;
  292. chan->nvsw.vblsem = gpuobj;
  293. chan->nvsw.vblsem_offset = ~0;
  294. return 0;
  295. }
  296. static int
  297. nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan,
  298. u32 class, u32 mthd, u32 data)
  299. {
  300. if (nouveau_notifier_offset(chan->nvsw.vblsem, &data))
  301. return -ERANGE;
  302. chan->nvsw.vblsem_offset = data >> 2;
  303. return 0;
  304. }
  305. static int
  306. nv50_graph_nvsw_vblsem_release_val(struct nouveau_channel *chan,
  307. u32 class, u32 mthd, u32 data)
  308. {
  309. chan->nvsw.vblsem_rval = data;
  310. return 0;
  311. }
  312. static int
  313. nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan,
  314. u32 class, u32 mthd, u32 data)
  315. {
  316. struct drm_device *dev = chan->dev;
  317. struct drm_nouveau_private *dev_priv = dev->dev_private;
  318. if (!chan->nvsw.vblsem || chan->nvsw.vblsem_offset == ~0 || data > 1)
  319. return -EINVAL;
  320. drm_vblank_get(dev, data);
  321. chan->nvsw.vblsem_head = data;
  322. list_add(&chan->nvsw.vbl_wait, &dev_priv->vbl_waiting);
  323. return 0;
  324. }
  325. static int
  326. nv50_graph_nvsw_mthd_page_flip(struct nouveau_channel *chan,
  327. u32 class, u32 mthd, u32 data)
  328. {
  329. struct nouveau_page_flip_state s;
  330. if (!nouveau_finish_page_flip(chan, &s)) {
  331. /* XXX - Do something here */
  332. }
  333. return 0;
  334. }
  335. static int
  336. nv50_graph_register(struct drm_device *dev)
  337. {
  338. struct drm_nouveau_private *dev_priv = dev->dev_private;
  339. if (dev_priv->engine.graph.registered)
  340. return 0;
  341. NVOBJ_CLASS(dev, 0x506e, SW); /* nvsw */
  342. NVOBJ_MTHD (dev, 0x506e, 0x018c, nv50_graph_nvsw_dma_vblsem);
  343. NVOBJ_MTHD (dev, 0x506e, 0x0400, nv50_graph_nvsw_vblsem_offset);
  344. NVOBJ_MTHD (dev, 0x506e, 0x0404, nv50_graph_nvsw_vblsem_release_val);
  345. NVOBJ_MTHD (dev, 0x506e, 0x0408, nv50_graph_nvsw_vblsem_release);
  346. NVOBJ_MTHD (dev, 0x506e, 0x0500, nv50_graph_nvsw_mthd_page_flip);
  347. NVOBJ_CLASS(dev, 0x0030, GR); /* null */
  348. NVOBJ_CLASS(dev, 0x5039, GR); /* m2mf */
  349. NVOBJ_CLASS(dev, 0x502d, GR); /* 2d */
  350. /* tesla */
  351. if (dev_priv->chipset == 0x50)
  352. NVOBJ_CLASS(dev, 0x5097, GR); /* tesla (nv50) */
  353. else
  354. if (dev_priv->chipset < 0xa0)
  355. NVOBJ_CLASS(dev, 0x8297, GR); /* tesla (nv8x/nv9x) */
  356. else {
  357. switch (dev_priv->chipset) {
  358. case 0xa0:
  359. case 0xaa:
  360. case 0xac:
  361. NVOBJ_CLASS(dev, 0x8397, GR);
  362. break;
  363. case 0xa3:
  364. case 0xa5:
  365. case 0xa8:
  366. NVOBJ_CLASS(dev, 0x8597, GR);
  367. break;
  368. case 0xaf:
  369. NVOBJ_CLASS(dev, 0x8697, GR);
  370. break;
  371. }
  372. }
  373. /* compute */
  374. NVOBJ_CLASS(dev, 0x50c0, GR);
  375. if (dev_priv->chipset > 0xa0 &&
  376. dev_priv->chipset != 0xaa &&
  377. dev_priv->chipset != 0xac)
  378. NVOBJ_CLASS(dev, 0x85c0, GR);
  379. dev_priv->engine.graph.registered = true;
  380. return 0;
  381. }
  382. void
  383. nv50_graph_tlb_flush(struct drm_device *dev)
  384. {
  385. nv50_vm_flush_engine(dev, 0);
  386. }
  387. void
  388. nv86_graph_tlb_flush(struct drm_device *dev)
  389. {
  390. struct drm_nouveau_private *dev_priv = dev->dev_private;
  391. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  392. bool idle, timeout = false;
  393. unsigned long flags;
  394. u64 start;
  395. u32 tmp;
  396. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  397. nv_mask(dev, 0x400500, 0x00000001, 0x00000000);
  398. start = ptimer->read(dev);
  399. do {
  400. idle = true;
  401. for (tmp = nv_rd32(dev, 0x400380); tmp && idle; tmp >>= 3) {
  402. if ((tmp & 7) == 1)
  403. idle = false;
  404. }
  405. for (tmp = nv_rd32(dev, 0x400384); tmp && idle; tmp >>= 3) {
  406. if ((tmp & 7) == 1)
  407. idle = false;
  408. }
  409. for (tmp = nv_rd32(dev, 0x400388); tmp && idle; tmp >>= 3) {
  410. if ((tmp & 7) == 1)
  411. idle = false;
  412. }
  413. } while (!idle && !(timeout = ptimer->read(dev) - start > 2000000000));
  414. if (timeout) {
  415. NV_ERROR(dev, "PGRAPH TLB flush idle timeout fail: "
  416. "0x%08x 0x%08x 0x%08x 0x%08x\n",
  417. nv_rd32(dev, 0x400700), nv_rd32(dev, 0x400380),
  418. nv_rd32(dev, 0x400384), nv_rd32(dev, 0x400388));
  419. }
  420. nv50_vm_flush_engine(dev, 0);
  421. nv_mask(dev, 0x400500, 0x00000001, 0x00000001);
  422. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  423. }
  424. static struct nouveau_enum nv50_mp_exec_error_names[] =
  425. {
  426. { 3, "STACK_UNDERFLOW" },
  427. { 4, "QUADON_ACTIVE" },
  428. { 8, "TIMEOUT" },
  429. { 0x10, "INVALID_OPCODE" },
  430. { 0x40, "BREAKPOINT" },
  431. {}
  432. };
  433. static struct nouveau_bitfield nv50_graph_trap_m2mf[] = {
  434. { 0x00000001, "NOTIFY" },
  435. { 0x00000002, "IN" },
  436. { 0x00000004, "OUT" },
  437. {}
  438. };
  439. static struct nouveau_bitfield nv50_graph_trap_vfetch[] = {
  440. { 0x00000001, "FAULT" },
  441. {}
  442. };
  443. static struct nouveau_bitfield nv50_graph_trap_strmout[] = {
  444. { 0x00000001, "FAULT" },
  445. {}
  446. };
  447. static struct nouveau_bitfield nv50_graph_trap_ccache[] = {
  448. { 0x00000001, "FAULT" },
  449. {}
  450. };
  451. /* There must be a *lot* of these. Will take some time to gather them up. */
  452. static struct nouveau_enum nv50_data_error_names[] = {
  453. { 4, "INVALID_VALUE" },
  454. { 5, "INVALID_ENUM" },
  455. { 8, "INVALID_OBJECT" },
  456. { 0xc, "INVALID_BITFIELD" },
  457. { 0x28, "MP_NO_REG_SPACE" },
  458. { 0x2b, "MP_BLOCK_SIZE_MISMATCH" },
  459. {}
  460. };
  461. static struct nouveau_bitfield nv50_graph_intr[] = {
  462. { 0x00000001, "NOTIFY" },
  463. { 0x00000002, "COMPUTE_QUERY" },
  464. { 0x00000010, "ILLEGAL_MTHD" },
  465. { 0x00000020, "ILLEGAL_CLASS" },
  466. { 0x00000040, "DOUBLE_NOTIFY" },
  467. { 0x00001000, "CONTEXT_SWITCH" },
  468. { 0x00010000, "BUFFER_NOTIFY" },
  469. { 0x00100000, "DATA_ERROR" },
  470. { 0x00200000, "TRAP" },
  471. { 0x01000000, "SINGLE_STEP" },
  472. {}
  473. };
  474. static void
  475. nv50_pgraph_mp_trap(struct drm_device *dev, int tpid, int display)
  476. {
  477. struct drm_nouveau_private *dev_priv = dev->dev_private;
  478. uint32_t units = nv_rd32(dev, 0x1540);
  479. uint32_t addr, mp10, status, pc, oplow, ophigh;
  480. int i;
  481. int mps = 0;
  482. for (i = 0; i < 4; i++) {
  483. if (!(units & 1 << (i+24)))
  484. continue;
  485. if (dev_priv->chipset < 0xa0)
  486. addr = 0x408200 + (tpid << 12) + (i << 7);
  487. else
  488. addr = 0x408100 + (tpid << 11) + (i << 7);
  489. mp10 = nv_rd32(dev, addr + 0x10);
  490. status = nv_rd32(dev, addr + 0x14);
  491. if (!status)
  492. continue;
  493. if (display) {
  494. nv_rd32(dev, addr + 0x20);
  495. pc = nv_rd32(dev, addr + 0x24);
  496. oplow = nv_rd32(dev, addr + 0x70);
  497. ophigh= nv_rd32(dev, addr + 0x74);
  498. NV_INFO(dev, "PGRAPH_TRAP_MP_EXEC - "
  499. "TP %d MP %d: ", tpid, i);
  500. nouveau_enum_print(nv50_mp_exec_error_names, status);
  501. printk(" at %06x warp %d, opcode %08x %08x\n",
  502. pc&0xffffff, pc >> 24,
  503. oplow, ophigh);
  504. }
  505. nv_wr32(dev, addr + 0x10, mp10);
  506. nv_wr32(dev, addr + 0x14, 0);
  507. mps++;
  508. }
  509. if (!mps && display)
  510. NV_INFO(dev, "PGRAPH_TRAP_MP_EXEC - TP %d: "
  511. "No MPs claiming errors?\n", tpid);
  512. }
  513. static void
  514. nv50_pgraph_tp_trap(struct drm_device *dev, int type, uint32_t ustatus_old,
  515. uint32_t ustatus_new, int display, const char *name)
  516. {
  517. struct drm_nouveau_private *dev_priv = dev->dev_private;
  518. int tps = 0;
  519. uint32_t units = nv_rd32(dev, 0x1540);
  520. int i, r;
  521. uint32_t ustatus_addr, ustatus;
  522. for (i = 0; i < 16; i++) {
  523. if (!(units & (1 << i)))
  524. continue;
  525. if (dev_priv->chipset < 0xa0)
  526. ustatus_addr = ustatus_old + (i << 12);
  527. else
  528. ustatus_addr = ustatus_new + (i << 11);
  529. ustatus = nv_rd32(dev, ustatus_addr) & 0x7fffffff;
  530. if (!ustatus)
  531. continue;
  532. tps++;
  533. switch (type) {
  534. case 6: /* texture error... unknown for now */
  535. nv50_fb_vm_trap(dev, display, name);
  536. if (display) {
  537. NV_ERROR(dev, "magic set %d:\n", i);
  538. for (r = ustatus_addr + 4; r <= ustatus_addr + 0x10; r += 4)
  539. NV_ERROR(dev, "\t0x%08x: 0x%08x\n", r,
  540. nv_rd32(dev, r));
  541. }
  542. break;
  543. case 7: /* MP error */
  544. if (ustatus & 0x00010000) {
  545. nv50_pgraph_mp_trap(dev, i, display);
  546. ustatus &= ~0x00010000;
  547. }
  548. break;
  549. case 8: /* TPDMA error */
  550. {
  551. uint32_t e0c = nv_rd32(dev, ustatus_addr + 4);
  552. uint32_t e10 = nv_rd32(dev, ustatus_addr + 8);
  553. uint32_t e14 = nv_rd32(dev, ustatus_addr + 0xc);
  554. uint32_t e18 = nv_rd32(dev, ustatus_addr + 0x10);
  555. uint32_t e1c = nv_rd32(dev, ustatus_addr + 0x14);
  556. uint32_t e20 = nv_rd32(dev, ustatus_addr + 0x18);
  557. uint32_t e24 = nv_rd32(dev, ustatus_addr + 0x1c);
  558. nv50_fb_vm_trap(dev, display, name);
  559. /* 2d engine destination */
  560. if (ustatus & 0x00000010) {
  561. if (display) {
  562. NV_INFO(dev, "PGRAPH_TRAP_TPDMA_2D - TP %d - Unknown fault at address %02x%08x\n",
  563. i, e14, e10);
  564. NV_INFO(dev, "PGRAPH_TRAP_TPDMA_2D - TP %d - e0c: %08x, e18: %08x, e1c: %08x, e20: %08x, e24: %08x\n",
  565. i, e0c, e18, e1c, e20, e24);
  566. }
  567. ustatus &= ~0x00000010;
  568. }
  569. /* Render target */
  570. if (ustatus & 0x00000040) {
  571. if (display) {
  572. NV_INFO(dev, "PGRAPH_TRAP_TPDMA_RT - TP %d - Unknown fault at address %02x%08x\n",
  573. i, e14, e10);
  574. NV_INFO(dev, "PGRAPH_TRAP_TPDMA_RT - TP %d - e0c: %08x, e18: %08x, e1c: %08x, e20: %08x, e24: %08x\n",
  575. i, e0c, e18, e1c, e20, e24);
  576. }
  577. ustatus &= ~0x00000040;
  578. }
  579. /* CUDA memory: l[], g[] or stack. */
  580. if (ustatus & 0x00000080) {
  581. if (display) {
  582. if (e18 & 0x80000000) {
  583. /* g[] read fault? */
  584. NV_INFO(dev, "PGRAPH_TRAP_TPDMA - TP %d - Global read fault at address %02x%08x\n",
  585. i, e14, e10 | ((e18 >> 24) & 0x1f));
  586. e18 &= ~0x1f000000;
  587. } else if (e18 & 0xc) {
  588. /* g[] write fault? */
  589. NV_INFO(dev, "PGRAPH_TRAP_TPDMA - TP %d - Global write fault at address %02x%08x\n",
  590. i, e14, e10 | ((e18 >> 7) & 0x1f));
  591. e18 &= ~0x00000f80;
  592. } else {
  593. NV_INFO(dev, "PGRAPH_TRAP_TPDMA - TP %d - Unknown CUDA fault at address %02x%08x\n",
  594. i, e14, e10);
  595. }
  596. NV_INFO(dev, "PGRAPH_TRAP_TPDMA - TP %d - e0c: %08x, e18: %08x, e1c: %08x, e20: %08x, e24: %08x\n",
  597. i, e0c, e18, e1c, e20, e24);
  598. }
  599. ustatus &= ~0x00000080;
  600. }
  601. }
  602. break;
  603. }
  604. if (ustatus) {
  605. if (display)
  606. NV_INFO(dev, "%s - TP%d: Unhandled ustatus 0x%08x\n", name, i, ustatus);
  607. }
  608. nv_wr32(dev, ustatus_addr, 0xc0000000);
  609. }
  610. if (!tps && display)
  611. NV_INFO(dev, "%s - No TPs claiming errors?\n", name);
  612. }
  613. static int
  614. nv50_pgraph_trap_handler(struct drm_device *dev, u32 display, u64 inst, u32 chid)
  615. {
  616. u32 status = nv_rd32(dev, 0x400108);
  617. u32 ustatus;
  618. if (!status && display) {
  619. NV_INFO(dev, "PGRAPH - TRAP: no units reporting traps?\n");
  620. return 1;
  621. }
  622. /* DISPATCH: Relays commands to other units and handles NOTIFY,
  623. * COND, QUERY. If you get a trap from it, the command is still stuck
  624. * in DISPATCH and you need to do something about it. */
  625. if (status & 0x001) {
  626. ustatus = nv_rd32(dev, 0x400804) & 0x7fffffff;
  627. if (!ustatus && display) {
  628. NV_INFO(dev, "PGRAPH_TRAP_DISPATCH - no ustatus?\n");
  629. }
  630. nv_wr32(dev, 0x400500, 0x00000000);
  631. /* Known to be triggered by screwed up NOTIFY and COND... */
  632. if (ustatus & 0x00000001) {
  633. u32 addr = nv_rd32(dev, 0x400808);
  634. u32 subc = (addr & 0x00070000) >> 16;
  635. u32 mthd = (addr & 0x00001ffc);
  636. u32 datal = nv_rd32(dev, 0x40080c);
  637. u32 datah = nv_rd32(dev, 0x400810);
  638. u32 class = nv_rd32(dev, 0x400814);
  639. u32 r848 = nv_rd32(dev, 0x400848);
  640. NV_INFO(dev, "PGRAPH - TRAP DISPATCH_FAULT\n");
  641. if (display && (addr & 0x80000000)) {
  642. NV_INFO(dev, "PGRAPH - ch %d (0x%010llx) "
  643. "subc %d class 0x%04x mthd 0x%04x "
  644. "data 0x%08x%08x "
  645. "400808 0x%08x 400848 0x%08x\n",
  646. chid, inst, subc, class, mthd, datah,
  647. datal, addr, r848);
  648. } else
  649. if (display) {
  650. NV_INFO(dev, "PGRAPH - no stuck command?\n");
  651. }
  652. nv_wr32(dev, 0x400808, 0);
  653. nv_wr32(dev, 0x4008e8, nv_rd32(dev, 0x4008e8) & 3);
  654. nv_wr32(dev, 0x400848, 0);
  655. ustatus &= ~0x00000001;
  656. }
  657. if (ustatus & 0x00000002) {
  658. u32 addr = nv_rd32(dev, 0x40084c);
  659. u32 subc = (addr & 0x00070000) >> 16;
  660. u32 mthd = (addr & 0x00001ffc);
  661. u32 data = nv_rd32(dev, 0x40085c);
  662. u32 class = nv_rd32(dev, 0x400814);
  663. NV_INFO(dev, "PGRAPH - TRAP DISPATCH_QUERY\n");
  664. if (display && (addr & 0x80000000)) {
  665. NV_INFO(dev, "PGRAPH - ch %d (0x%010llx) "
  666. "subc %d class 0x%04x mthd 0x%04x "
  667. "data 0x%08x 40084c 0x%08x\n",
  668. chid, inst, subc, class, mthd,
  669. data, addr);
  670. } else
  671. if (display) {
  672. NV_INFO(dev, "PGRAPH - no stuck command?\n");
  673. }
  674. nv_wr32(dev, 0x40084c, 0);
  675. ustatus &= ~0x00000002;
  676. }
  677. if (ustatus && display) {
  678. NV_INFO(dev, "PGRAPH - TRAP_DISPATCH (unknown "
  679. "0x%08x)\n", ustatus);
  680. }
  681. nv_wr32(dev, 0x400804, 0xc0000000);
  682. nv_wr32(dev, 0x400108, 0x001);
  683. status &= ~0x001;
  684. if (!status)
  685. return 0;
  686. }
  687. /* M2MF: Memory to memory copy engine. */
  688. if (status & 0x002) {
  689. u32 ustatus = nv_rd32(dev, 0x406800) & 0x7fffffff;
  690. if (display) {
  691. NV_INFO(dev, "PGRAPH - TRAP_M2MF");
  692. nouveau_bitfield_print(nv50_graph_trap_m2mf, ustatus);
  693. printk("\n");
  694. NV_INFO(dev, "PGRAPH - TRAP_M2MF %08x %08x %08x %08x\n",
  695. nv_rd32(dev, 0x406804), nv_rd32(dev, 0x406808),
  696. nv_rd32(dev, 0x40680c), nv_rd32(dev, 0x406810));
  697. }
  698. /* No sane way found yet -- just reset the bugger. */
  699. nv_wr32(dev, 0x400040, 2);
  700. nv_wr32(dev, 0x400040, 0);
  701. nv_wr32(dev, 0x406800, 0xc0000000);
  702. nv_wr32(dev, 0x400108, 0x002);
  703. status &= ~0x002;
  704. }
  705. /* VFETCH: Fetches data from vertex buffers. */
  706. if (status & 0x004) {
  707. u32 ustatus = nv_rd32(dev, 0x400c04) & 0x7fffffff;
  708. if (display) {
  709. NV_INFO(dev, "PGRAPH - TRAP_VFETCH");
  710. nouveau_bitfield_print(nv50_graph_trap_vfetch, ustatus);
  711. printk("\n");
  712. NV_INFO(dev, "PGRAPH - TRAP_VFETCH %08x %08x %08x %08x\n",
  713. nv_rd32(dev, 0x400c00), nv_rd32(dev, 0x400c08),
  714. nv_rd32(dev, 0x400c0c), nv_rd32(dev, 0x400c10));
  715. }
  716. nv_wr32(dev, 0x400c04, 0xc0000000);
  717. nv_wr32(dev, 0x400108, 0x004);
  718. status &= ~0x004;
  719. }
  720. /* STRMOUT: DirectX streamout / OpenGL transform feedback. */
  721. if (status & 0x008) {
  722. ustatus = nv_rd32(dev, 0x401800) & 0x7fffffff;
  723. if (display) {
  724. NV_INFO(dev, "PGRAPH - TRAP_STRMOUT");
  725. nouveau_bitfield_print(nv50_graph_trap_strmout, ustatus);
  726. printk("\n");
  727. NV_INFO(dev, "PGRAPH - TRAP_STRMOUT %08x %08x %08x %08x\n",
  728. nv_rd32(dev, 0x401804), nv_rd32(dev, 0x401808),
  729. nv_rd32(dev, 0x40180c), nv_rd32(dev, 0x401810));
  730. }
  731. /* No sane way found yet -- just reset the bugger. */
  732. nv_wr32(dev, 0x400040, 0x80);
  733. nv_wr32(dev, 0x400040, 0);
  734. nv_wr32(dev, 0x401800, 0xc0000000);
  735. nv_wr32(dev, 0x400108, 0x008);
  736. status &= ~0x008;
  737. }
  738. /* CCACHE: Handles code and c[] caches and fills them. */
  739. if (status & 0x010) {
  740. ustatus = nv_rd32(dev, 0x405018) & 0x7fffffff;
  741. if (display) {
  742. NV_INFO(dev, "PGRAPH - TRAP_CCACHE");
  743. nouveau_bitfield_print(nv50_graph_trap_ccache, ustatus);
  744. printk("\n");
  745. NV_INFO(dev, "PGRAPH - TRAP_CCACHE %08x %08x %08x %08x"
  746. " %08x %08x %08x\n",
  747. nv_rd32(dev, 0x405800), nv_rd32(dev, 0x405804),
  748. nv_rd32(dev, 0x405808), nv_rd32(dev, 0x40580c),
  749. nv_rd32(dev, 0x405810), nv_rd32(dev, 0x405814),
  750. nv_rd32(dev, 0x40581c));
  751. }
  752. nv_wr32(dev, 0x405018, 0xc0000000);
  753. nv_wr32(dev, 0x400108, 0x010);
  754. status &= ~0x010;
  755. }
  756. /* Unknown, not seen yet... 0x402000 is the only trap status reg
  757. * remaining, so try to handle it anyway. Perhaps related to that
  758. * unknown DMA slot on tesla? */
  759. if (status & 0x20) {
  760. ustatus = nv_rd32(dev, 0x402000) & 0x7fffffff;
  761. if (display)
  762. NV_INFO(dev, "PGRAPH - TRAP_UNKC04 0x%08x\n", ustatus);
  763. nv_wr32(dev, 0x402000, 0xc0000000);
  764. /* no status modifiction on purpose */
  765. }
  766. /* TEXTURE: CUDA texturing units */
  767. if (status & 0x040) {
  768. nv50_pgraph_tp_trap(dev, 6, 0x408900, 0x408600, display,
  769. "PGRAPH - TRAP_TEXTURE");
  770. nv_wr32(dev, 0x400108, 0x040);
  771. status &= ~0x040;
  772. }
  773. /* MP: CUDA execution engines. */
  774. if (status & 0x080) {
  775. nv50_pgraph_tp_trap(dev, 7, 0x408314, 0x40831c, display,
  776. "PGRAPH - TRAP_MP");
  777. nv_wr32(dev, 0x400108, 0x080);
  778. status &= ~0x080;
  779. }
  780. /* TPDMA: Handles TP-initiated uncached memory accesses:
  781. * l[], g[], stack, 2d surfaces, render targets. */
  782. if (status & 0x100) {
  783. nv50_pgraph_tp_trap(dev, 8, 0x408e08, 0x408708, display,
  784. "PGRAPH - TRAP_TPDMA");
  785. nv_wr32(dev, 0x400108, 0x100);
  786. status &= ~0x100;
  787. }
  788. if (status) {
  789. if (display)
  790. NV_INFO(dev, "PGRAPH - TRAP: unknown 0x%08x\n", status);
  791. nv_wr32(dev, 0x400108, status);
  792. }
  793. return 1;
  794. }
  795. static int
  796. nv50_graph_isr_chid(struct drm_device *dev, u64 inst)
  797. {
  798. struct drm_nouveau_private *dev_priv = dev->dev_private;
  799. struct nouveau_channel *chan;
  800. unsigned long flags;
  801. int i;
  802. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  803. for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
  804. chan = dev_priv->channels.ptr[i];
  805. if (!chan || !chan->ramin)
  806. continue;
  807. if (inst == chan->ramin->vinst)
  808. break;
  809. }
  810. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  811. return i;
  812. }
  813. static void
  814. nv50_graph_isr(struct drm_device *dev)
  815. {
  816. u32 stat;
  817. while ((stat = nv_rd32(dev, 0x400100))) {
  818. u64 inst = (u64)(nv_rd32(dev, 0x40032c) & 0x0fffffff) << 12;
  819. u32 chid = nv50_graph_isr_chid(dev, inst);
  820. u32 addr = nv_rd32(dev, NV04_PGRAPH_TRAPPED_ADDR);
  821. u32 subc = (addr & 0x00070000) >> 16;
  822. u32 mthd = (addr & 0x00001ffc);
  823. u32 data = nv_rd32(dev, NV04_PGRAPH_TRAPPED_DATA);
  824. u32 class = nv_rd32(dev, 0x400814);
  825. u32 show = stat;
  826. if (stat & 0x00000010) {
  827. if (!nouveau_gpuobj_mthd_call2(dev, chid, class,
  828. mthd, data))
  829. show &= ~0x00000010;
  830. }
  831. if (stat & 0x00001000) {
  832. nv_wr32(dev, 0x400500, 0x00000000);
  833. nv_wr32(dev, 0x400100, 0x00001000);
  834. nv_mask(dev, 0x40013c, 0x00001000, 0x00000000);
  835. nv50_graph_context_switch(dev);
  836. stat &= ~0x00001000;
  837. show &= ~0x00001000;
  838. }
  839. show = (show && nouveau_ratelimit()) ? show : 0;
  840. if (show & 0x00100000) {
  841. u32 ecode = nv_rd32(dev, 0x400110);
  842. NV_INFO(dev, "PGRAPH - DATA_ERROR ");
  843. nouveau_enum_print(nv50_data_error_names, ecode);
  844. printk("\n");
  845. }
  846. if (stat & 0x00200000) {
  847. if (!nv50_pgraph_trap_handler(dev, show, inst, chid))
  848. show &= ~0x00200000;
  849. }
  850. nv_wr32(dev, 0x400100, stat);
  851. nv_wr32(dev, 0x400500, 0x00010001);
  852. if (show) {
  853. NV_INFO(dev, "PGRAPH -");
  854. nouveau_bitfield_print(nv50_graph_intr, show);
  855. printk("\n");
  856. NV_INFO(dev, "PGRAPH - ch %d (0x%010llx) subc %d "
  857. "class 0x%04x mthd 0x%04x data 0x%08x\n",
  858. chid, inst, subc, class, mthd, data);
  859. }
  860. }
  861. if (nv_rd32(dev, 0x400824) & (1 << 31))
  862. nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) & ~(1 << 31));
  863. }