nve0_fifo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #define NVE0_FIFO_ENGINE_NUM 32
  29. static void nve0_fifo_isr(struct drm_device *);
  30. struct nve0_fifo_engine {
  31. struct nouveau_gpuobj *playlist[2];
  32. int cur_playlist;
  33. };
  34. struct nve0_fifo_priv {
  35. struct nouveau_fifo_priv base;
  36. struct nve0_fifo_engine engine[NVE0_FIFO_ENGINE_NUM];
  37. struct {
  38. struct nouveau_gpuobj *mem;
  39. struct nouveau_vma bar;
  40. } user;
  41. int spoon_nr;
  42. };
  43. struct nve0_fifo_chan {
  44. struct nouveau_fifo_chan base;
  45. u32 engine;
  46. };
  47. static void
  48. nve0_fifo_playlist_update(struct drm_device *dev, u32 engine)
  49. {
  50. struct drm_nouveau_private *dev_priv = dev->dev_private;
  51. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  52. struct nve0_fifo_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  53. struct nve0_fifo_engine *peng = &priv->engine[engine];
  54. struct nouveau_gpuobj *cur;
  55. u32 match = (engine << 16) | 0x00000001;
  56. int ret, i, p;
  57. cur = peng->playlist[peng->cur_playlist];
  58. if (unlikely(cur == NULL)) {
  59. ret = nouveau_gpuobj_new(dev, NULL, 0x8000, 0x1000, 0, &cur);
  60. if (ret) {
  61. NV_ERROR(dev, "PFIFO: playlist alloc failed\n");
  62. return;
  63. }
  64. peng->playlist[peng->cur_playlist] = cur;
  65. }
  66. peng->cur_playlist = !peng->cur_playlist;
  67. for (i = 0, p = 0; i < priv->base.channels; i++) {
  68. u32 ctrl = nv_rd32(dev, 0x800004 + (i * 8)) & 0x001f0001;
  69. if (ctrl != match)
  70. continue;
  71. nv_wo32(cur, p + 0, i);
  72. nv_wo32(cur, p + 4, 0x00000000);
  73. p += 8;
  74. }
  75. pinstmem->flush(dev);
  76. nv_wr32(dev, 0x002270, cur->vinst >> 12);
  77. nv_wr32(dev, 0x002274, (engine << 20) | (p >> 3));
  78. if (!nv_wait(dev, 0x002284 + (engine * 4), 0x00100000, 0x00000000))
  79. NV_ERROR(dev, "PFIFO: playlist %d update timeout\n", engine);
  80. }
  81. static int
  82. nve0_fifo_context_new(struct nouveau_channel *chan, int engine)
  83. {
  84. struct drm_device *dev = chan->dev;
  85. struct drm_nouveau_private *dev_priv = dev->dev_private;
  86. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  87. struct nve0_fifo_priv *priv = nv_engine(dev, engine);
  88. struct nve0_fifo_chan *fctx;
  89. u64 usermem = priv->user.mem->vinst + chan->id * 512;
  90. u64 ib_virt = chan->pushbuf_base + chan->dma.ib_base * 4;
  91. int ret = 0, i;
  92. fctx = chan->engctx[engine] = kzalloc(sizeof(*fctx), GFP_KERNEL);
  93. if (!fctx)
  94. return -ENOMEM;
  95. fctx->engine = 0; /* PGRAPH */
  96. /* allocate vram for control regs, map into polling area */
  97. chan->user = ioremap_wc(pci_resource_start(dev->pdev, 1) +
  98. priv->user.bar.offset + (chan->id * 512), 512);
  99. if (!chan->user) {
  100. ret = -ENOMEM;
  101. goto error;
  102. }
  103. for (i = 0; i < 0x100; i += 4)
  104. nv_wo32(chan->ramin, i, 0x00000000);
  105. nv_wo32(chan->ramin, 0x08, lower_32_bits(usermem));
  106. nv_wo32(chan->ramin, 0x0c, upper_32_bits(usermem));
  107. nv_wo32(chan->ramin, 0x10, 0x0000face);
  108. nv_wo32(chan->ramin, 0x30, 0xfffff902);
  109. nv_wo32(chan->ramin, 0x48, lower_32_bits(ib_virt));
  110. nv_wo32(chan->ramin, 0x4c, drm_order(chan->dma.ib_max + 1) << 16 |
  111. upper_32_bits(ib_virt));
  112. nv_wo32(chan->ramin, 0x84, 0x20400000);
  113. nv_wo32(chan->ramin, 0x94, 0x30000001);
  114. nv_wo32(chan->ramin, 0x9c, 0x00000100);
  115. nv_wo32(chan->ramin, 0xac, 0x0000001f);
  116. nv_wo32(chan->ramin, 0xe4, 0x00000000);
  117. nv_wo32(chan->ramin, 0xe8, chan->id);
  118. nv_wo32(chan->ramin, 0xf8, 0x10003080); /* 0x002310 */
  119. nv_wo32(chan->ramin, 0xfc, 0x10000010); /* 0x002350 */
  120. pinstmem->flush(dev);
  121. nv_wr32(dev, 0x800000 + (chan->id * 8), 0x80000000 |
  122. (chan->ramin->vinst >> 12));
  123. nv_mask(dev, 0x800004 + (chan->id * 8), 0x00000400, 0x00000400);
  124. nve0_fifo_playlist_update(dev, fctx->engine);
  125. nv_mask(dev, 0x800004 + (chan->id * 8), 0x00000400, 0x00000400);
  126. error:
  127. if (ret)
  128. priv->base.base.context_del(chan, engine);
  129. return ret;
  130. }
  131. static void
  132. nve0_fifo_context_del(struct nouveau_channel *chan, int engine)
  133. {
  134. struct nve0_fifo_chan *fctx = chan->engctx[engine];
  135. struct drm_device *dev = chan->dev;
  136. nv_mask(dev, 0x800004 + (chan->id * 8), 0x00000800, 0x00000800);
  137. nv_wr32(dev, 0x002634, chan->id);
  138. if (!nv_wait(dev, 0x0002634, 0xffffffff, chan->id))
  139. NV_WARN(dev, "0x2634 != chid: 0x%08x\n", nv_rd32(dev, 0x2634));
  140. nve0_fifo_playlist_update(dev, fctx->engine);
  141. nv_wr32(dev, 0x800000 + (chan->id * 8), 0x00000000);
  142. if (chan->user) {
  143. iounmap(chan->user);
  144. chan->user = NULL;
  145. }
  146. chan->engctx[NVOBJ_ENGINE_FIFO] = NULL;
  147. kfree(fctx);
  148. }
  149. static int
  150. nve0_fifo_init(struct drm_device *dev, int engine)
  151. {
  152. struct drm_nouveau_private *dev_priv = dev->dev_private;
  153. struct nve0_fifo_priv *priv = nv_engine(dev, engine);
  154. struct nve0_fifo_chan *fctx;
  155. int i;
  156. /* reset PFIFO, enable all available PSUBFIFO areas */
  157. nv_mask(dev, 0x000200, 0x00000100, 0x00000000);
  158. nv_mask(dev, 0x000200, 0x00000100, 0x00000100);
  159. nv_wr32(dev, 0x000204, 0xffffffff);
  160. priv->spoon_nr = hweight32(nv_rd32(dev, 0x000204));
  161. NV_DEBUG(dev, "PFIFO: %d subfifo(s)\n", priv->spoon_nr);
  162. /* PSUBFIFO[n] */
  163. for (i = 0; i < priv->spoon_nr; i++) {
  164. nv_mask(dev, 0x04013c + (i * 0x2000), 0x10000100, 0x00000000);
  165. nv_wr32(dev, 0x040108 + (i * 0x2000), 0xffffffff); /* INTR */
  166. nv_wr32(dev, 0x04010c + (i * 0x2000), 0xfffffeff); /* INTR_EN */
  167. }
  168. nv_wr32(dev, 0x002254, 0x10000000 | priv->user.bar.offset >> 12);
  169. nv_wr32(dev, 0x002a00, 0xffffffff);
  170. nv_wr32(dev, 0x002100, 0xffffffff);
  171. nv_wr32(dev, 0x002140, 0xbfffffff);
  172. /* restore PFIFO context table */
  173. for (i = 0; i < priv->base.channels; i++) {
  174. struct nouveau_channel *chan = dev_priv->channels.ptr[i];
  175. if (!chan || !(fctx = chan->engctx[engine]))
  176. continue;
  177. nv_wr32(dev, 0x800000 + (i * 8), 0x80000000 |
  178. (chan->ramin->vinst >> 12));
  179. nv_mask(dev, 0x800004 + (i * 8), 0x00000400, 0x00000400);
  180. nve0_fifo_playlist_update(dev, fctx->engine);
  181. nv_mask(dev, 0x800004 + (i * 8), 0x00000400, 0x00000400);
  182. }
  183. return 0;
  184. }
  185. static int
  186. nve0_fifo_fini(struct drm_device *dev, int engine, bool suspend)
  187. {
  188. struct nve0_fifo_priv *priv = nv_engine(dev, engine);
  189. int i;
  190. for (i = 0; i < priv->base.channels; i++) {
  191. if (!(nv_rd32(dev, 0x800004 + (i * 8)) & 1))
  192. continue;
  193. nv_mask(dev, 0x800004 + (i * 8), 0x00000800, 0x00000800);
  194. nv_wr32(dev, 0x002634, i);
  195. if (!nv_wait(dev, 0x002634, 0xffffffff, i)) {
  196. NV_INFO(dev, "PFIFO: kick ch %d failed: 0x%08x\n",
  197. i, nv_rd32(dev, 0x002634));
  198. return -EBUSY;
  199. }
  200. }
  201. nv_wr32(dev, 0x002140, 0x00000000);
  202. return 0;
  203. }
  204. struct nouveau_enum nve0_fifo_fault_unit[] = {
  205. {}
  206. };
  207. struct nouveau_enum nve0_fifo_fault_reason[] = {
  208. { 0x00, "PT_NOT_PRESENT" },
  209. { 0x01, "PT_TOO_SHORT" },
  210. { 0x02, "PAGE_NOT_PRESENT" },
  211. { 0x03, "VM_LIMIT_EXCEEDED" },
  212. { 0x04, "NO_CHANNEL" },
  213. { 0x05, "PAGE_SYSTEM_ONLY" },
  214. { 0x06, "PAGE_READ_ONLY" },
  215. { 0x0a, "COMPRESSED_SYSRAM" },
  216. { 0x0c, "INVALID_STORAGE_TYPE" },
  217. {}
  218. };
  219. struct nouveau_enum nve0_fifo_fault_hubclient[] = {
  220. {}
  221. };
  222. struct nouveau_enum nve0_fifo_fault_gpcclient[] = {
  223. {}
  224. };
  225. struct nouveau_bitfield nve0_fifo_subfifo_intr[] = {
  226. { 0x00200000, "ILLEGAL_MTHD" },
  227. { 0x00800000, "EMPTY_SUBC" },
  228. {}
  229. };
  230. static void
  231. nve0_fifo_isr_vm_fault(struct drm_device *dev, int unit)
  232. {
  233. u32 inst = nv_rd32(dev, 0x2800 + (unit * 0x10));
  234. u32 valo = nv_rd32(dev, 0x2804 + (unit * 0x10));
  235. u32 vahi = nv_rd32(dev, 0x2808 + (unit * 0x10));
  236. u32 stat = nv_rd32(dev, 0x280c + (unit * 0x10));
  237. u32 client = (stat & 0x00001f00) >> 8;
  238. NV_INFO(dev, "PFIFO: %s fault at 0x%010llx [",
  239. (stat & 0x00000080) ? "write" : "read", (u64)vahi << 32 | valo);
  240. nouveau_enum_print(nve0_fifo_fault_reason, stat & 0x0000000f);
  241. printk("] from ");
  242. nouveau_enum_print(nve0_fifo_fault_unit, unit);
  243. if (stat & 0x00000040) {
  244. printk("/");
  245. nouveau_enum_print(nve0_fifo_fault_hubclient, client);
  246. } else {
  247. printk("/GPC%d/", (stat & 0x1f000000) >> 24);
  248. nouveau_enum_print(nve0_fifo_fault_gpcclient, client);
  249. }
  250. printk(" on channel 0x%010llx\n", (u64)inst << 12);
  251. }
  252. static void
  253. nve0_fifo_isr_subfifo_intr(struct drm_device *dev, int unit)
  254. {
  255. u32 stat = nv_rd32(dev, 0x040108 + (unit * 0x2000));
  256. u32 addr = nv_rd32(dev, 0x0400c0 + (unit * 0x2000));
  257. u32 data = nv_rd32(dev, 0x0400c4 + (unit * 0x2000));
  258. u32 chid = nv_rd32(dev, 0x040120 + (unit * 0x2000)) & 0x7f;
  259. u32 subc = (addr & 0x00070000);
  260. u32 mthd = (addr & 0x00003ffc);
  261. NV_INFO(dev, "PSUBFIFO %d:", unit);
  262. nouveau_bitfield_print(nve0_fifo_subfifo_intr, stat);
  263. NV_INFO(dev, "PSUBFIFO %d: ch %d subc %d mthd 0x%04x data 0x%08x\n",
  264. unit, chid, subc, mthd, data);
  265. nv_wr32(dev, 0x0400c0 + (unit * 0x2000), 0x80600008);
  266. nv_wr32(dev, 0x040108 + (unit * 0x2000), stat);
  267. }
  268. static void
  269. nve0_fifo_isr(struct drm_device *dev)
  270. {
  271. u32 stat = nv_rd32(dev, 0x002100);
  272. if (stat & 0x00000100) {
  273. NV_INFO(dev, "PFIFO: unknown status 0x00000100\n");
  274. nv_wr32(dev, 0x002100, 0x00000100);
  275. stat &= ~0x00000100;
  276. }
  277. if (stat & 0x10000000) {
  278. u32 units = nv_rd32(dev, 0x00259c);
  279. u32 u = units;
  280. while (u) {
  281. int i = ffs(u) - 1;
  282. nve0_fifo_isr_vm_fault(dev, i);
  283. u &= ~(1 << i);
  284. }
  285. nv_wr32(dev, 0x00259c, units);
  286. stat &= ~0x10000000;
  287. }
  288. if (stat & 0x20000000) {
  289. u32 units = nv_rd32(dev, 0x0025a0);
  290. u32 u = units;
  291. while (u) {
  292. int i = ffs(u) - 1;
  293. nve0_fifo_isr_subfifo_intr(dev, i);
  294. u &= ~(1 << i);
  295. }
  296. nv_wr32(dev, 0x0025a0, units);
  297. stat &= ~0x20000000;
  298. }
  299. if (stat & 0x40000000) {
  300. NV_INFO(dev, "PFIFO: unknown status 0x40000000\n");
  301. nv_mask(dev, 0x002a00, 0x00000000, 0x00000000);
  302. stat &= ~0x40000000;
  303. }
  304. if (stat) {
  305. NV_INFO(dev, "PFIFO: unhandled status 0x%08x\n", stat);
  306. nv_wr32(dev, 0x002100, stat);
  307. nv_wr32(dev, 0x002140, 0);
  308. }
  309. }
  310. static void
  311. nve0_fifo_destroy(struct drm_device *dev, int engine)
  312. {
  313. struct drm_nouveau_private *dev_priv = dev->dev_private;
  314. struct nve0_fifo_priv *priv = nv_engine(dev, engine);
  315. int i;
  316. nouveau_vm_put(&priv->user.bar);
  317. nouveau_gpuobj_ref(NULL, &priv->user.mem);
  318. for (i = 0; i < NVE0_FIFO_ENGINE_NUM; i++) {
  319. nouveau_gpuobj_ref(NULL, &priv->engine[i].playlist[0]);
  320. nouveau_gpuobj_ref(NULL, &priv->engine[i].playlist[1]);
  321. }
  322. dev_priv->eng[engine] = NULL;
  323. kfree(priv);
  324. }
  325. int
  326. nve0_fifo_create(struct drm_device *dev)
  327. {
  328. struct drm_nouveau_private *dev_priv = dev->dev_private;
  329. struct nve0_fifo_priv *priv;
  330. int ret;
  331. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  332. if (!priv)
  333. return -ENOMEM;
  334. priv->base.base.destroy = nve0_fifo_destroy;
  335. priv->base.base.init = nve0_fifo_init;
  336. priv->base.base.fini = nve0_fifo_fini;
  337. priv->base.base.context_new = nve0_fifo_context_new;
  338. priv->base.base.context_del = nve0_fifo_context_del;
  339. priv->base.channels = 4096;
  340. dev_priv->eng[NVOBJ_ENGINE_FIFO] = &priv->base.base;
  341. ret = nouveau_gpuobj_new(dev, NULL, priv->base.channels * 512, 0x1000,
  342. NVOBJ_FLAG_ZERO_ALLOC, &priv->user.mem);
  343. if (ret)
  344. goto error;
  345. ret = nouveau_vm_get(dev_priv->bar1_vm, priv->user.mem->size,
  346. 12, NV_MEM_ACCESS_RW, &priv->user.bar);
  347. if (ret)
  348. goto error;
  349. nouveau_vm_map(&priv->user.bar, *(struct nouveau_mem **)priv->user.mem->node);
  350. nouveau_irq_register(dev, 8, nve0_fifo_isr);
  351. error:
  352. if (ret)
  353. priv->base.base.destroy(dev, NVOBJ_ENGINE_FIFO);
  354. return ret;
  355. }