nvc0_fifo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_mm.h"
  27. #include "nouveau_fifo.h"
  28. static void nvc0_fifo_isr(struct drm_device *);
  29. struct nvc0_fifo_priv {
  30. struct nouveau_fifo_priv base;
  31. struct nouveau_gpuobj *playlist[2];
  32. int cur_playlist;
  33. struct nouveau_vma user_vma;
  34. int spoon_nr;
  35. };
  36. struct nvc0_fifo_chan {
  37. struct nouveau_fifo_chan base;
  38. struct nouveau_gpuobj *user;
  39. };
  40. static void
  41. nvc0_fifo_playlist_update(struct drm_device *dev)
  42. {
  43. struct drm_nouveau_private *dev_priv = dev->dev_private;
  44. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  45. struct nvc0_fifo_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  46. struct nouveau_gpuobj *cur;
  47. int i, p;
  48. cur = priv->playlist[priv->cur_playlist];
  49. priv->cur_playlist = !priv->cur_playlist;
  50. for (i = 0, p = 0; i < 128; i++) {
  51. if (!(nv_rd32(dev, 0x3004 + (i * 8)) & 1))
  52. continue;
  53. nv_wo32(cur, p + 0, i);
  54. nv_wo32(cur, p + 4, 0x00000004);
  55. p += 8;
  56. }
  57. pinstmem->flush(dev);
  58. nv_wr32(dev, 0x002270, cur->vinst >> 12);
  59. nv_wr32(dev, 0x002274, 0x01f00000 | (p >> 3));
  60. if (!nv_wait(dev, 0x00227c, 0x00100000, 0x00000000))
  61. NV_ERROR(dev, "PFIFO - playlist update failed\n");
  62. }
  63. static int
  64. nvc0_fifo_context_new(struct nouveau_channel *chan, int engine)
  65. {
  66. struct drm_device *dev = chan->dev;
  67. struct drm_nouveau_private *dev_priv = dev->dev_private;
  68. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  69. struct nvc0_fifo_priv *priv = nv_engine(dev, engine);
  70. struct nvc0_fifo_chan *fctx;
  71. u64 ib_virt = chan->pushbuf_base + chan->dma.ib_base * 4;
  72. int ret, i;
  73. fctx = chan->engctx[engine] = kzalloc(sizeof(*fctx), GFP_KERNEL);
  74. if (!fctx)
  75. return -ENOMEM;
  76. chan->user = ioremap_wc(pci_resource_start(dev->pdev, 1) +
  77. priv->user_vma.offset + (chan->id * 0x1000),
  78. PAGE_SIZE);
  79. if (!chan->user) {
  80. ret = -ENOMEM;
  81. goto error;
  82. }
  83. /* allocate vram for control regs, map into polling area */
  84. ret = nouveau_gpuobj_new(dev, NULL, 0x1000, 0x1000,
  85. NVOBJ_FLAG_ZERO_ALLOC, &fctx->user);
  86. if (ret)
  87. goto error;
  88. nouveau_vm_map_at(&priv->user_vma, chan->id * 0x1000,
  89. *(struct nouveau_mem **)fctx->user->node);
  90. for (i = 0; i < 0x100; i += 4)
  91. nv_wo32(chan->ramin, i, 0x00000000);
  92. nv_wo32(chan->ramin, 0x08, lower_32_bits(fctx->user->vinst));
  93. nv_wo32(chan->ramin, 0x0c, upper_32_bits(fctx->user->vinst));
  94. nv_wo32(chan->ramin, 0x10, 0x0000face);
  95. nv_wo32(chan->ramin, 0x30, 0xfffff902);
  96. nv_wo32(chan->ramin, 0x48, lower_32_bits(ib_virt));
  97. nv_wo32(chan->ramin, 0x4c, drm_order(chan->dma.ib_max + 1) << 16 |
  98. upper_32_bits(ib_virt));
  99. nv_wo32(chan->ramin, 0x54, 0x00000002);
  100. nv_wo32(chan->ramin, 0x84, 0x20400000);
  101. nv_wo32(chan->ramin, 0x94, 0x30000001);
  102. nv_wo32(chan->ramin, 0x9c, 0x00000100);
  103. nv_wo32(chan->ramin, 0xa4, 0x1f1f1f1f);
  104. nv_wo32(chan->ramin, 0xa8, 0x1f1f1f1f);
  105. nv_wo32(chan->ramin, 0xac, 0x0000001f);
  106. nv_wo32(chan->ramin, 0xb8, 0xf8000000);
  107. nv_wo32(chan->ramin, 0xf8, 0x10003080); /* 0x002310 */
  108. nv_wo32(chan->ramin, 0xfc, 0x10000010); /* 0x002350 */
  109. pinstmem->flush(dev);
  110. nv_wr32(dev, 0x003000 + (chan->id * 8), 0xc0000000 |
  111. (chan->ramin->vinst >> 12));
  112. nv_wr32(dev, 0x003004 + (chan->id * 8), 0x001f0001);
  113. nvc0_fifo_playlist_update(dev);
  114. error:
  115. if (ret)
  116. priv->base.base.context_del(chan, engine);
  117. return ret;
  118. }
  119. static void
  120. nvc0_fifo_context_del(struct nouveau_channel *chan, int engine)
  121. {
  122. struct nvc0_fifo_chan *fctx = chan->engctx[engine];
  123. struct drm_device *dev = chan->dev;
  124. nv_mask(dev, 0x003004 + (chan->id * 8), 0x00000001, 0x00000000);
  125. nv_wr32(dev, 0x002634, chan->id);
  126. if (!nv_wait(dev, 0x0002634, 0xffffffff, chan->id))
  127. NV_WARN(dev, "0x2634 != chid: 0x%08x\n", nv_rd32(dev, 0x2634));
  128. nvc0_fifo_playlist_update(dev);
  129. nv_wr32(dev, 0x003000 + (chan->id * 8), 0x00000000);
  130. nouveau_gpuobj_ref(NULL, &fctx->user);
  131. if (chan->user) {
  132. iounmap(chan->user);
  133. chan->user = NULL;
  134. }
  135. chan->engctx[engine] = NULL;
  136. kfree(fctx);
  137. }
  138. static int
  139. nvc0_fifo_init(struct drm_device *dev, int engine)
  140. {
  141. struct drm_nouveau_private *dev_priv = dev->dev_private;
  142. struct nvc0_fifo_priv *priv = nv_engine(dev, engine);
  143. struct nouveau_channel *chan;
  144. int i;
  145. /* reset PFIFO, enable all available PSUBFIFO areas */
  146. nv_mask(dev, 0x000200, 0x00000100, 0x00000000);
  147. nv_mask(dev, 0x000200, 0x00000100, 0x00000100);
  148. nv_wr32(dev, 0x000204, 0xffffffff);
  149. nv_wr32(dev, 0x002204, 0xffffffff);
  150. priv->spoon_nr = hweight32(nv_rd32(dev, 0x002204));
  151. NV_DEBUG(dev, "PFIFO: %d subfifo(s)\n", priv->spoon_nr);
  152. /* assign engines to subfifos */
  153. if (priv->spoon_nr >= 3) {
  154. nv_wr32(dev, 0x002208, ~(1 << 0)); /* PGRAPH */
  155. nv_wr32(dev, 0x00220c, ~(1 << 1)); /* PVP */
  156. nv_wr32(dev, 0x002210, ~(1 << 1)); /* PPP */
  157. nv_wr32(dev, 0x002214, ~(1 << 1)); /* PBSP */
  158. nv_wr32(dev, 0x002218, ~(1 << 2)); /* PCE0 */
  159. nv_wr32(dev, 0x00221c, ~(1 << 1)); /* PCE1 */
  160. }
  161. /* PSUBFIFO[n] */
  162. for (i = 0; i < priv->spoon_nr; i++) {
  163. nv_mask(dev, 0x04013c + (i * 0x2000), 0x10000100, 0x00000000);
  164. nv_wr32(dev, 0x040108 + (i * 0x2000), 0xffffffff); /* INTR */
  165. nv_wr32(dev, 0x04010c + (i * 0x2000), 0xfffffeff); /* INTR_EN */
  166. }
  167. nv_mask(dev, 0x002200, 0x00000001, 0x00000001);
  168. nv_wr32(dev, 0x002254, 0x10000000 | priv->user_vma.offset >> 12);
  169. nv_wr32(dev, 0x002a00, 0xffffffff); /* clears PFIFO.INTR bit 30 */
  170. nv_wr32(dev, 0x002100, 0xffffffff);
  171. nv_wr32(dev, 0x002140, 0xbfffffff);
  172. /* restore PFIFO context table */
  173. for (i = 0; i < 128; i++) {
  174. chan = dev_priv->channels.ptr[i];
  175. if (!chan || !chan->engctx[engine])
  176. continue;
  177. nv_wr32(dev, 0x003000 + (i * 8), 0xc0000000 |
  178. (chan->ramin->vinst >> 12));
  179. nv_wr32(dev, 0x003004 + (i * 8), 0x001f0001);
  180. }
  181. nvc0_fifo_playlist_update(dev);
  182. return 0;
  183. }
  184. static int
  185. nvc0_fifo_fini(struct drm_device *dev, int engine, bool suspend)
  186. {
  187. int i;
  188. for (i = 0; i < 128; i++) {
  189. if (!(nv_rd32(dev, 0x003004 + (i * 8)) & 1))
  190. continue;
  191. nv_mask(dev, 0x003004 + (i * 8), 0x00000001, 0x00000000);
  192. nv_wr32(dev, 0x002634, i);
  193. if (!nv_wait(dev, 0x002634, 0xffffffff, i)) {
  194. NV_INFO(dev, "PFIFO: kick ch %d failed: 0x%08x\n",
  195. i, nv_rd32(dev, 0x002634));
  196. return -EBUSY;
  197. }
  198. }
  199. nv_wr32(dev, 0x002140, 0x00000000);
  200. return 0;
  201. }
  202. struct nouveau_enum nvc0_fifo_fault_unit[] = {
  203. { 0x00, "PGRAPH" },
  204. { 0x03, "PEEPHOLE" },
  205. { 0x04, "BAR1" },
  206. { 0x05, "BAR3" },
  207. { 0x07, "PFIFO" },
  208. { 0x10, "PBSP" },
  209. { 0x11, "PPPP" },
  210. { 0x13, "PCOUNTER" },
  211. { 0x14, "PVP" },
  212. { 0x15, "PCOPY0" },
  213. { 0x16, "PCOPY1" },
  214. { 0x17, "PDAEMON" },
  215. {}
  216. };
  217. struct nouveau_enum nvc0_fifo_fault_reason[] = {
  218. { 0x00, "PT_NOT_PRESENT" },
  219. { 0x01, "PT_TOO_SHORT" },
  220. { 0x02, "PAGE_NOT_PRESENT" },
  221. { 0x03, "VM_LIMIT_EXCEEDED" },
  222. { 0x04, "NO_CHANNEL" },
  223. { 0x05, "PAGE_SYSTEM_ONLY" },
  224. { 0x06, "PAGE_READ_ONLY" },
  225. { 0x0a, "COMPRESSED_SYSRAM" },
  226. { 0x0c, "INVALID_STORAGE_TYPE" },
  227. {}
  228. };
  229. struct nouveau_enum nvc0_fifo_fault_hubclient[] = {
  230. { 0x01, "PCOPY0" },
  231. { 0x02, "PCOPY1" },
  232. { 0x04, "DISPATCH" },
  233. { 0x05, "CTXCTL" },
  234. { 0x06, "PFIFO" },
  235. { 0x07, "BAR_READ" },
  236. { 0x08, "BAR_WRITE" },
  237. { 0x0b, "PVP" },
  238. { 0x0c, "PPPP" },
  239. { 0x0d, "PBSP" },
  240. { 0x11, "PCOUNTER" },
  241. { 0x12, "PDAEMON" },
  242. { 0x14, "CCACHE" },
  243. { 0x15, "CCACHE_POST" },
  244. {}
  245. };
  246. struct nouveau_enum nvc0_fifo_fault_gpcclient[] = {
  247. { 0x01, "TEX" },
  248. { 0x0c, "ESETUP" },
  249. { 0x0e, "CTXCTL" },
  250. { 0x0f, "PROP" },
  251. {}
  252. };
  253. struct nouveau_bitfield nvc0_fifo_subfifo_intr[] = {
  254. /* { 0x00008000, "" } seen with null ib push */
  255. { 0x00200000, "ILLEGAL_MTHD" },
  256. { 0x00800000, "EMPTY_SUBC" },
  257. {}
  258. };
  259. static void
  260. nvc0_fifo_isr_vm_fault(struct drm_device *dev, int unit)
  261. {
  262. u32 inst = nv_rd32(dev, 0x2800 + (unit * 0x10));
  263. u32 valo = nv_rd32(dev, 0x2804 + (unit * 0x10));
  264. u32 vahi = nv_rd32(dev, 0x2808 + (unit * 0x10));
  265. u32 stat = nv_rd32(dev, 0x280c + (unit * 0x10));
  266. u32 client = (stat & 0x00001f00) >> 8;
  267. NV_INFO(dev, "PFIFO: %s fault at 0x%010llx [",
  268. (stat & 0x00000080) ? "write" : "read", (u64)vahi << 32 | valo);
  269. nouveau_enum_print(nvc0_fifo_fault_reason, stat & 0x0000000f);
  270. printk("] from ");
  271. nouveau_enum_print(nvc0_fifo_fault_unit, unit);
  272. if (stat & 0x00000040) {
  273. printk("/");
  274. nouveau_enum_print(nvc0_fifo_fault_hubclient, client);
  275. } else {
  276. printk("/GPC%d/", (stat & 0x1f000000) >> 24);
  277. nouveau_enum_print(nvc0_fifo_fault_gpcclient, client);
  278. }
  279. printk(" on channel 0x%010llx\n", (u64)inst << 12);
  280. }
  281. static int
  282. nvc0_fifo_page_flip(struct drm_device *dev, u32 chid)
  283. {
  284. struct nvc0_fifo_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  285. struct drm_nouveau_private *dev_priv = dev->dev_private;
  286. struct nouveau_channel *chan = NULL;
  287. unsigned long flags;
  288. int ret = -EINVAL;
  289. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  290. if (likely(chid >= 0 && chid < priv->base.channels)) {
  291. chan = dev_priv->channels.ptr[chid];
  292. if (likely(chan))
  293. ret = nouveau_finish_page_flip(chan, NULL);
  294. }
  295. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  296. return ret;
  297. }
  298. static void
  299. nvc0_fifo_isr_subfifo_intr(struct drm_device *dev, int unit)
  300. {
  301. u32 stat = nv_rd32(dev, 0x040108 + (unit * 0x2000));
  302. u32 addr = nv_rd32(dev, 0x0400c0 + (unit * 0x2000));
  303. u32 data = nv_rd32(dev, 0x0400c4 + (unit * 0x2000));
  304. u32 chid = nv_rd32(dev, 0x040120 + (unit * 0x2000)) & 0x7f;
  305. u32 subc = (addr & 0x00070000);
  306. u32 mthd = (addr & 0x00003ffc);
  307. u32 show = stat;
  308. if (stat & 0x00200000) {
  309. if (mthd == 0x0054) {
  310. if (!nvc0_fifo_page_flip(dev, chid))
  311. show &= ~0x00200000;
  312. }
  313. }
  314. if (show) {
  315. NV_INFO(dev, "PFIFO%d:", unit);
  316. nouveau_bitfield_print(nvc0_fifo_subfifo_intr, show);
  317. NV_INFO(dev, "PFIFO%d: ch %d subc %d mthd 0x%04x data 0x%08x\n",
  318. unit, chid, subc, mthd, data);
  319. }
  320. nv_wr32(dev, 0x0400c0 + (unit * 0x2000), 0x80600008);
  321. nv_wr32(dev, 0x040108 + (unit * 0x2000), stat);
  322. }
  323. static void
  324. nvc0_fifo_isr(struct drm_device *dev)
  325. {
  326. u32 stat = nv_rd32(dev, 0x002100);
  327. if (stat & 0x00000100) {
  328. NV_INFO(dev, "PFIFO: unknown status 0x00000100\n");
  329. nv_wr32(dev, 0x002100, 0x00000100);
  330. stat &= ~0x00000100;
  331. }
  332. if (stat & 0x10000000) {
  333. u32 units = nv_rd32(dev, 0x00259c);
  334. u32 u = units;
  335. while (u) {
  336. int i = ffs(u) - 1;
  337. nvc0_fifo_isr_vm_fault(dev, i);
  338. u &= ~(1 << i);
  339. }
  340. nv_wr32(dev, 0x00259c, units);
  341. stat &= ~0x10000000;
  342. }
  343. if (stat & 0x20000000) {
  344. u32 units = nv_rd32(dev, 0x0025a0);
  345. u32 u = units;
  346. while (u) {
  347. int i = ffs(u) - 1;
  348. nvc0_fifo_isr_subfifo_intr(dev, i);
  349. u &= ~(1 << i);
  350. }
  351. nv_wr32(dev, 0x0025a0, units);
  352. stat &= ~0x20000000;
  353. }
  354. if (stat & 0x40000000) {
  355. NV_INFO(dev, "PFIFO: unknown status 0x40000000\n");
  356. nv_mask(dev, 0x002a00, 0x00000000, 0x00000000);
  357. stat &= ~0x40000000;
  358. }
  359. if (stat) {
  360. NV_INFO(dev, "PFIFO: unhandled status 0x%08x\n", stat);
  361. nv_wr32(dev, 0x002100, stat);
  362. nv_wr32(dev, 0x002140, 0);
  363. }
  364. }
  365. static void
  366. nvc0_fifo_destroy(struct drm_device *dev, int engine)
  367. {
  368. struct nvc0_fifo_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  369. struct drm_nouveau_private *dev_priv = dev->dev_private;
  370. nouveau_vm_put(&priv->user_vma);
  371. nouveau_gpuobj_ref(NULL, &priv->playlist[1]);
  372. nouveau_gpuobj_ref(NULL, &priv->playlist[0]);
  373. dev_priv->eng[engine] = NULL;
  374. kfree(priv);
  375. }
  376. int
  377. nvc0_fifo_create(struct drm_device *dev)
  378. {
  379. struct drm_nouveau_private *dev_priv = dev->dev_private;
  380. struct nvc0_fifo_priv *priv;
  381. int ret;
  382. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  383. if (!priv)
  384. return -ENOMEM;
  385. priv->base.base.destroy = nvc0_fifo_destroy;
  386. priv->base.base.init = nvc0_fifo_init;
  387. priv->base.base.fini = nvc0_fifo_fini;
  388. priv->base.base.context_new = nvc0_fifo_context_new;
  389. priv->base.base.context_del = nvc0_fifo_context_del;
  390. priv->base.channels = 128;
  391. dev_priv->eng[NVOBJ_ENGINE_FIFO] = &priv->base.base;
  392. ret = nouveau_gpuobj_new(dev, NULL, 4096, 4096, 0, &priv->playlist[0]);
  393. if (ret)
  394. goto error;
  395. ret = nouveau_gpuobj_new(dev, NULL, 4096, 4096, 0, &priv->playlist[1]);
  396. if (ret)
  397. goto error;
  398. ret = nouveau_vm_get(dev_priv->bar1_vm, priv->base.channels * 0x1000,
  399. 12, NV_MEM_ACCESS_RW, &priv->user_vma);
  400. if (ret)
  401. goto error;
  402. nouveau_irq_register(dev, 8, nvc0_fifo_isr);
  403. error:
  404. if (ret)
  405. priv->base.base.destroy(dev, NVOBJ_ENGINE_FIFO);
  406. return ret;
  407. }