nv40_mpeg.c 8.4 KB

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