nv31_mpeg.c 9.3 KB

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