nv31_mpeg.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright 2011 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_fifo.h"
  27. #include "nouveau_ramht.h"
  28. struct nv31_mpeg_engine {
  29. struct nouveau_exec_engine base;
  30. atomic_t refcount;
  31. };
  32. static int
  33. nv31_mpeg_context_new(struct nouveau_channel *chan, int engine)
  34. {
  35. struct nv31_mpeg_engine *pmpeg = nv_engine(chan->dev, engine);
  36. if (!atomic_add_unless(&pmpeg->refcount, 1, 1))
  37. return -EBUSY;
  38. chan->engctx[engine] = (void *)0xdeadcafe;
  39. return 0;
  40. }
  41. static void
  42. nv31_mpeg_context_del(struct nouveau_channel *chan, int engine)
  43. {
  44. struct nv31_mpeg_engine *pmpeg = nv_engine(chan->dev, engine);
  45. atomic_dec(&pmpeg->refcount);
  46. chan->engctx[engine] = NULL;
  47. }
  48. static int
  49. nv40_mpeg_context_new(struct nouveau_channel *chan, int engine)
  50. {
  51. struct drm_device *dev = chan->dev;
  52. struct drm_nouveau_private *dev_priv = dev->dev_private;
  53. struct nouveau_gpuobj *ctx = NULL;
  54. unsigned long flags;
  55. int ret;
  56. NV_DEBUG(dev, "ch%d\n", chan->id);
  57. ret = nouveau_gpuobj_new(dev, NULL, 264 * 4, 16, NVOBJ_FLAG_ZERO_ALLOC |
  58. NVOBJ_FLAG_ZERO_FREE, &ctx);
  59. if (ret)
  60. return ret;
  61. nv_wo32(ctx, 0x78, 0x02001ec1);
  62. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  63. nv_mask(dev, 0x002500, 0x00000001, 0x00000000);
  64. if ((nv_rd32(dev, 0x003204) & 0x1f) == chan->id)
  65. nv_wr32(dev, 0x00330c, ctx->pinst >> 4);
  66. nv_wo32(chan->ramfc, 0x54, ctx->pinst >> 4);
  67. nv_mask(dev, 0x002500, 0x00000001, 0x00000001);
  68. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  69. chan->engctx[engine] = ctx;
  70. return 0;
  71. }
  72. static void
  73. nv40_mpeg_context_del(struct nouveau_channel *chan, int engine)
  74. {
  75. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  76. struct nouveau_gpuobj *ctx = chan->engctx[engine];
  77. struct drm_device *dev = chan->dev;
  78. unsigned long flags;
  79. u32 inst = 0x80000000 | (ctx->pinst >> 4);
  80. spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
  81. nv_mask(dev, 0x00b32c, 0x00000001, 0x00000000);
  82. if (nv_rd32(dev, 0x00b318) == inst)
  83. nv_mask(dev, 0x00b318, 0x80000000, 0x00000000);
  84. nv_mask(dev, 0x00b32c, 0x00000001, 0x00000001);
  85. spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
  86. nouveau_gpuobj_ref(NULL, &ctx);
  87. chan->engctx[engine] = NULL;
  88. }
  89. static int
  90. nv31_mpeg_object_new(struct nouveau_channel *chan, int engine,
  91. u32 handle, u16 class)
  92. {
  93. struct drm_device *dev = chan->dev;
  94. struct nouveau_gpuobj *obj = NULL;
  95. int ret;
  96. ret = nouveau_gpuobj_new(dev, chan, 20, 16, NVOBJ_FLAG_ZERO_ALLOC |
  97. NVOBJ_FLAG_ZERO_FREE, &obj);
  98. if (ret)
  99. return ret;
  100. obj->engine = 2;
  101. obj->class = class;
  102. nv_wo32(obj, 0x00, class);
  103. ret = nouveau_ramht_insert(chan, handle, obj);
  104. nouveau_gpuobj_ref(NULL, &obj);
  105. return ret;
  106. }
  107. static int
  108. nv31_mpeg_init(struct drm_device *dev, int engine)
  109. {
  110. struct drm_nouveau_private *dev_priv = dev->dev_private;
  111. struct nv31_mpeg_engine *pmpeg = nv_engine(dev, engine);
  112. int i;
  113. /* VPE init */
  114. nv_mask(dev, 0x000200, 0x00000002, 0x00000000);
  115. nv_mask(dev, 0x000200, 0x00000002, 0x00000002);
  116. nv_wr32(dev, 0x00b0e0, 0x00000020); /* nvidia: rd 0x01, wr 0x20 */
  117. nv_wr32(dev, 0x00b0e8, 0x00000020); /* nvidia: rd 0x01, wr 0x20 */
  118. for (i = 0; i < dev_priv->engine.fb.num_tiles; i++)
  119. pmpeg->base.set_tile_region(dev, i);
  120. /* PMPEG init */
  121. nv_wr32(dev, 0x00b32c, 0x00000000);
  122. nv_wr32(dev, 0x00b314, 0x00000100);
  123. nv_wr32(dev, 0x00b220, nv44_graph_class(dev) ? 0x00000044 : 0x00000031);
  124. nv_wr32(dev, 0x00b300, 0x02001ec1);
  125. nv_mask(dev, 0x00b32c, 0x00000001, 0x00000001);
  126. nv_wr32(dev, 0x00b100, 0xffffffff);
  127. nv_wr32(dev, 0x00b140, 0xffffffff);
  128. if (!nv_wait(dev, 0x00b200, 0x00000001, 0x00000000)) {
  129. NV_ERROR(dev, "PMPEG init: 0x%08x\n", nv_rd32(dev, 0x00b200));
  130. return -EBUSY;
  131. }
  132. return 0;
  133. }
  134. static int
  135. nv31_mpeg_fini(struct drm_device *dev, int engine, bool suspend)
  136. {
  137. /*XXX: context save? */
  138. nv_mask(dev, 0x00b32c, 0x00000001, 0x00000000);
  139. nv_wr32(dev, 0x00b140, 0x00000000);
  140. return 0;
  141. }
  142. static int
  143. nv31_mpeg_mthd_dma(struct nouveau_channel *chan, u32 class, u32 mthd, u32 data)
  144. {
  145. struct drm_device *dev = chan->dev;
  146. u32 inst = data << 4;
  147. u32 dma0 = nv_ri32(dev, inst + 0);
  148. u32 dma1 = nv_ri32(dev, inst + 4);
  149. u32 dma2 = nv_ri32(dev, inst + 8);
  150. u32 base = (dma2 & 0xfffff000) | (dma0 >> 20);
  151. u32 size = dma1 + 1;
  152. /* only allow linear DMA objects */
  153. if (!(dma0 & 0x00002000))
  154. return -EINVAL;
  155. if (mthd == 0x0190) {
  156. /* DMA_CMD */
  157. nv_mask(dev, 0x00b300, 0x00030000, (dma0 & 0x00030000));
  158. nv_wr32(dev, 0x00b334, base);
  159. nv_wr32(dev, 0x00b324, size);
  160. } else
  161. if (mthd == 0x01a0) {
  162. /* DMA_DATA */
  163. nv_mask(dev, 0x00b300, 0x000c0000, (dma0 & 0x00030000) << 2);
  164. nv_wr32(dev, 0x00b360, base);
  165. nv_wr32(dev, 0x00b364, size);
  166. } else {
  167. /* DMA_IMAGE, VRAM only */
  168. if (dma0 & 0x000c0000)
  169. return -EINVAL;
  170. nv_wr32(dev, 0x00b370, base);
  171. nv_wr32(dev, 0x00b374, size);
  172. }
  173. return 0;
  174. }
  175. static int
  176. nv31_mpeg_isr_chid(struct drm_device *dev, u32 inst)
  177. {
  178. struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
  179. struct drm_nouveau_private *dev_priv = dev->dev_private;
  180. struct nouveau_gpuobj *ctx;
  181. unsigned long flags;
  182. int i;
  183. /* hardcode drm channel id on nv3x, so swmthd lookup works */
  184. if (dev_priv->card_type < NV_40)
  185. return 0;
  186. spin_lock_irqsave(&dev_priv->channels.lock, flags);
  187. for (i = 0; i < pfifo->channels; i++) {
  188. if (!dev_priv->channels.ptr[i])
  189. continue;
  190. ctx = dev_priv->channels.ptr[i]->engctx[NVOBJ_ENGINE_MPEG];
  191. if (ctx && ctx->pinst == inst)
  192. break;
  193. }
  194. spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
  195. return i;
  196. }
  197. static void
  198. nv31_vpe_set_tile_region(struct drm_device *dev, int i)
  199. {
  200. struct drm_nouveau_private *dev_priv = dev->dev_private;
  201. struct nouveau_tile_reg *tile = &dev_priv->tile.reg[i];
  202. nv_wr32(dev, 0x00b008 + (i * 0x10), tile->pitch);
  203. nv_wr32(dev, 0x00b004 + (i * 0x10), tile->limit);
  204. nv_wr32(dev, 0x00b000 + (i * 0x10), tile->addr);
  205. }
  206. static void
  207. nv31_mpeg_isr(struct drm_device *dev)
  208. {
  209. u32 inst = (nv_rd32(dev, 0x00b318) & 0x000fffff) << 4;
  210. u32 chid = nv31_mpeg_isr_chid(dev, inst);
  211. u32 stat = nv_rd32(dev, 0x00b100);
  212. u32 type = nv_rd32(dev, 0x00b230);
  213. u32 mthd = nv_rd32(dev, 0x00b234);
  214. u32 data = nv_rd32(dev, 0x00b238);
  215. u32 show = stat;
  216. if (stat & 0x01000000) {
  217. /* happens on initial binding of the object */
  218. if (type == 0x00000020 && mthd == 0x0000) {
  219. nv_mask(dev, 0x00b308, 0x00000000, 0x00000000);
  220. show &= ~0x01000000;
  221. }
  222. if (type == 0x00000010) {
  223. if (!nouveau_gpuobj_mthd_call2(dev, chid, 0x3174, mthd, data))
  224. show &= ~0x01000000;
  225. }
  226. }
  227. nv_wr32(dev, 0x00b100, stat);
  228. nv_wr32(dev, 0x00b230, 0x00000001);
  229. if (show && nouveau_ratelimit()) {
  230. NV_INFO(dev, "PMPEG: Ch %d [0x%08x] 0x%08x 0x%08x 0x%08x 0x%08x\n",
  231. chid, inst, stat, type, mthd, data);
  232. }
  233. }
  234. static void
  235. nv31_vpe_isr(struct drm_device *dev)
  236. {
  237. if (nv_rd32(dev, 0x00b100))
  238. nv31_mpeg_isr(dev);
  239. if (nv_rd32(dev, 0x00b800)) {
  240. u32 stat = nv_rd32(dev, 0x00b800);
  241. NV_INFO(dev, "PMSRCH: 0x%08x\n", stat);
  242. nv_wr32(dev, 0xb800, stat);
  243. }
  244. }
  245. static void
  246. nv31_mpeg_destroy(struct drm_device *dev, int engine)
  247. {
  248. struct nv31_mpeg_engine *pmpeg = nv_engine(dev, engine);
  249. nouveau_irq_unregister(dev, 0);
  250. NVOBJ_ENGINE_DEL(dev, MPEG);
  251. kfree(pmpeg);
  252. }
  253. int
  254. nv31_mpeg_create(struct drm_device *dev)
  255. {
  256. struct drm_nouveau_private *dev_priv = dev->dev_private;
  257. struct nv31_mpeg_engine *pmpeg;
  258. pmpeg = kzalloc(sizeof(*pmpeg), GFP_KERNEL);
  259. if (!pmpeg)
  260. return -ENOMEM;
  261. atomic_set(&pmpeg->refcount, 0);
  262. pmpeg->base.destroy = nv31_mpeg_destroy;
  263. pmpeg->base.init = nv31_mpeg_init;
  264. pmpeg->base.fini = nv31_mpeg_fini;
  265. if (dev_priv->card_type < NV_40) {
  266. pmpeg->base.context_new = nv31_mpeg_context_new;
  267. pmpeg->base.context_del = nv31_mpeg_context_del;
  268. } else {
  269. pmpeg->base.context_new = nv40_mpeg_context_new;
  270. pmpeg->base.context_del = nv40_mpeg_context_del;
  271. }
  272. pmpeg->base.object_new = nv31_mpeg_object_new;
  273. /* ISR vector, PMC_ENABLE bit, and TILE regs are shared between
  274. * all VPE engines, for this driver's purposes the PMPEG engine
  275. * will be treated as the "master" and handle the global VPE
  276. * bits too
  277. */
  278. pmpeg->base.set_tile_region = nv31_vpe_set_tile_region;
  279. nouveau_irq_register(dev, 0, nv31_vpe_isr);
  280. NVOBJ_ENGINE_ADD(dev, MPEG, &pmpeg->base);
  281. NVOBJ_CLASS(dev, 0x3174, MPEG);
  282. NVOBJ_MTHD (dev, 0x3174, 0x0190, nv31_mpeg_mthd_dma);
  283. NVOBJ_MTHD (dev, 0x3174, 0x01a0, nv31_mpeg_mthd_dma);
  284. NVOBJ_MTHD (dev, 0x3174, 0x01b0, nv31_mpeg_mthd_dma);
  285. #if 0
  286. NVOBJ_ENGINE_ADD(dev, ME, &pme->base);
  287. NVOBJ_CLASS(dev, 0x4075, ME);
  288. #endif
  289. return 0;
  290. }