blob: eb5455fed1bf7cfc3a57186fc1357725fdffca27 [file] [log] [blame]
Ben Skeggsa02ccc72011-04-04 16:08:24 +10001/*
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
25#include "drmP.h"
26#include "nouveau_drv.h"
Ben Skeggs02a841d2012-07-04 23:44:54 +100027#include <engine/fifo.h>
28#include <core/ramht.h>
Ben Skeggsa02ccc72011-04-04 16:08:24 +100029
Ben Skeggs323dcac2011-06-23 16:21:21 +100030struct nv31_mpeg_engine {
Ben Skeggsa02ccc72011-04-04 16:08:24 +100031 struct nouveau_exec_engine base;
Ben Skeggs52d07332011-06-23 16:44:05 +100032 atomic_t refcount;
Ben Skeggsa02ccc72011-04-04 16:08:24 +100033};
34
Ben Skeggs52d07332011-06-23 16:44:05 +100035
36static int
37nv31_mpeg_context_new(struct nouveau_channel *chan, int engine)
38{
39 struct nv31_mpeg_engine *pmpeg = nv_engine(chan->dev, engine);
40
41 if (!atomic_add_unless(&pmpeg->refcount, 1, 1))
42 return -EBUSY;
43
44 chan->engctx[engine] = (void *)0xdeadcafe;
45 return 0;
46}
47
48static void
49nv31_mpeg_context_del(struct nouveau_channel *chan, int engine)
50{
51 struct nv31_mpeg_engine *pmpeg = nv_engine(chan->dev, engine);
52 atomic_dec(&pmpeg->refcount);
53 chan->engctx[engine] = NULL;
54}
55
Ben Skeggsa02ccc72011-04-04 16:08:24 +100056static int
57nv40_mpeg_context_new(struct nouveau_channel *chan, int engine)
58{
59 struct drm_device *dev = chan->dev;
60 struct drm_nouveau_private *dev_priv = dev->dev_private;
61 struct nouveau_gpuobj *ctx = NULL;
62 unsigned long flags;
63 int ret;
64
65 NV_DEBUG(dev, "ch%d\n", chan->id);
66
67 ret = nouveau_gpuobj_new(dev, NULL, 264 * 4, 16, NVOBJ_FLAG_ZERO_ALLOC |
68 NVOBJ_FLAG_ZERO_FREE, &ctx);
69 if (ret)
70 return ret;
71
72 nv_wo32(ctx, 0x78, 0x02001ec1);
73
74 spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
75 nv_mask(dev, 0x002500, 0x00000001, 0x00000000);
76 if ((nv_rd32(dev, 0x003204) & 0x1f) == chan->id)
77 nv_wr32(dev, 0x00330c, ctx->pinst >> 4);
78 nv_wo32(chan->ramfc, 0x54, ctx->pinst >> 4);
79 nv_mask(dev, 0x002500, 0x00000001, 0x00000001);
80 spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
81
82 chan->engctx[engine] = ctx;
83 return 0;
84}
85
86static void
87nv40_mpeg_context_del(struct nouveau_channel *chan, int engine)
88{
89 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
90 struct nouveau_gpuobj *ctx = chan->engctx[engine];
91 struct drm_device *dev = chan->dev;
92 unsigned long flags;
93 u32 inst = 0x80000000 | (ctx->pinst >> 4);
94
95 spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
96 nv_mask(dev, 0x00b32c, 0x00000001, 0x00000000);
97 if (nv_rd32(dev, 0x00b318) == inst)
98 nv_mask(dev, 0x00b318, 0x80000000, 0x00000000);
99 nv_mask(dev, 0x00b32c, 0x00000001, 0x00000001);
100 spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
101
102 nouveau_gpuobj_ref(NULL, &ctx);
103 chan->engctx[engine] = NULL;
104}
105
106static int
Ben Skeggs323dcac2011-06-23 16:21:21 +1000107nv31_mpeg_object_new(struct nouveau_channel *chan, int engine,
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000108 u32 handle, u16 class)
109{
110 struct drm_device *dev = chan->dev;
111 struct nouveau_gpuobj *obj = NULL;
112 int ret;
113
114 ret = nouveau_gpuobj_new(dev, chan, 20, 16, NVOBJ_FLAG_ZERO_ALLOC |
115 NVOBJ_FLAG_ZERO_FREE, &obj);
116 if (ret)
117 return ret;
118 obj->engine = 2;
119 obj->class = class;
120
121 nv_wo32(obj, 0x00, class);
122
123 ret = nouveau_ramht_insert(chan, handle, obj);
124 nouveau_gpuobj_ref(NULL, &obj);
125 return ret;
126}
127
128static int
Ben Skeggs323dcac2011-06-23 16:21:21 +1000129nv31_mpeg_init(struct drm_device *dev, int engine)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000130{
Ben Skeggs323dcac2011-06-23 16:21:21 +1000131 struct nv31_mpeg_engine *pmpeg = nv_engine(dev, engine);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000132 int i;
133
134 /* VPE init */
135 nv_mask(dev, 0x000200, 0x00000002, 0x00000000);
136 nv_mask(dev, 0x000200, 0x00000002, 0x00000002);
137 nv_wr32(dev, 0x00b0e0, 0x00000020); /* nvidia: rd 0x01, wr 0x20 */
138 nv_wr32(dev, 0x00b0e8, 0x00000020); /* nvidia: rd 0x01, wr 0x20 */
139
Ben Skeggs861d2102012-07-11 19:05:01 +1000140 for (i = 0; i < nvfb_tile_nr(dev); i++)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000141 pmpeg->base.set_tile_region(dev, i);
142
143 /* PMPEG init */
144 nv_wr32(dev, 0x00b32c, 0x00000000);
145 nv_wr32(dev, 0x00b314, 0x00000100);
Ben Skeggs52d07332011-06-23 16:44:05 +1000146 nv_wr32(dev, 0x00b220, nv44_graph_class(dev) ? 0x00000044 : 0x00000031);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000147 nv_wr32(dev, 0x00b300, 0x02001ec1);
148 nv_mask(dev, 0x00b32c, 0x00000001, 0x00000001);
149
150 nv_wr32(dev, 0x00b100, 0xffffffff);
151 nv_wr32(dev, 0x00b140, 0xffffffff);
152
153 if (!nv_wait(dev, 0x00b200, 0x00000001, 0x00000000)) {
154 NV_ERROR(dev, "PMPEG init: 0x%08x\n", nv_rd32(dev, 0x00b200));
155 return -EBUSY;
156 }
157
158 return 0;
159}
160
161static int
Ben Skeggs323dcac2011-06-23 16:21:21 +1000162nv31_mpeg_fini(struct drm_device *dev, int engine, bool suspend)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000163{
164 /*XXX: context save? */
165 nv_mask(dev, 0x00b32c, 0x00000001, 0x00000000);
166 nv_wr32(dev, 0x00b140, 0x00000000);
167 return 0;
168}
169
170static int
Ben Skeggs323dcac2011-06-23 16:21:21 +1000171nv31_mpeg_mthd_dma(struct nouveau_channel *chan, u32 class, u32 mthd, u32 data)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000172{
173 struct drm_device *dev = chan->dev;
174 u32 inst = data << 4;
175 u32 dma0 = nv_ri32(dev, inst + 0);
176 u32 dma1 = nv_ri32(dev, inst + 4);
177 u32 dma2 = nv_ri32(dev, inst + 8);
178 u32 base = (dma2 & 0xfffff000) | (dma0 >> 20);
179 u32 size = dma1 + 1;
180
181 /* only allow linear DMA objects */
182 if (!(dma0 & 0x00002000))
183 return -EINVAL;
184
185 if (mthd == 0x0190) {
186 /* DMA_CMD */
187 nv_mask(dev, 0x00b300, 0x00030000, (dma0 & 0x00030000));
188 nv_wr32(dev, 0x00b334, base);
189 nv_wr32(dev, 0x00b324, size);
190 } else
191 if (mthd == 0x01a0) {
192 /* DMA_DATA */
193 nv_mask(dev, 0x00b300, 0x000c0000, (dma0 & 0x00030000) << 2);
194 nv_wr32(dev, 0x00b360, base);
195 nv_wr32(dev, 0x00b364, size);
196 } else {
197 /* DMA_IMAGE, VRAM only */
198 if (dma0 & 0x000c0000)
199 return -EINVAL;
200
201 nv_wr32(dev, 0x00b370, base);
202 nv_wr32(dev, 0x00b374, size);
203 }
204
205 return 0;
206}
207
208static int
Ben Skeggs323dcac2011-06-23 16:21:21 +1000209nv31_mpeg_isr_chid(struct drm_device *dev, u32 inst)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000210{
Ben Skeggsc420b2d2012-05-01 20:48:08 +1000211 struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000212 struct drm_nouveau_private *dev_priv = dev->dev_private;
213 struct nouveau_gpuobj *ctx;
214 unsigned long flags;
215 int i;
216
Ben Skeggs52d07332011-06-23 16:44:05 +1000217 /* hardcode drm channel id on nv3x, so swmthd lookup works */
218 if (dev_priv->card_type < NV_40)
219 return 0;
220
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000221 spin_lock_irqsave(&dev_priv->channels.lock, flags);
Ben Skeggsc420b2d2012-05-01 20:48:08 +1000222 for (i = 0; i < pfifo->channels; i++) {
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000223 if (!dev_priv->channels.ptr[i])
224 continue;
225
226 ctx = dev_priv->channels.ptr[i]->engctx[NVOBJ_ENGINE_MPEG];
227 if (ctx && ctx->pinst == inst)
228 break;
229 }
230 spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
231 return i;
232}
233
234static void
Ben Skeggs323dcac2011-06-23 16:21:21 +1000235nv31_vpe_set_tile_region(struct drm_device *dev, int i)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000236{
Ben Skeggs861d2102012-07-11 19:05:01 +1000237 struct nouveau_fb_tile *tile = nvfb_tile(dev, i);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000238 nv_wr32(dev, 0x00b008 + (i * 0x10), tile->pitch);
239 nv_wr32(dev, 0x00b004 + (i * 0x10), tile->limit);
240 nv_wr32(dev, 0x00b000 + (i * 0x10), tile->addr);
241}
242
243static void
Ben Skeggs323dcac2011-06-23 16:21:21 +1000244nv31_mpeg_isr(struct drm_device *dev)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000245{
246 u32 inst = (nv_rd32(dev, 0x00b318) & 0x000fffff) << 4;
Ben Skeggs323dcac2011-06-23 16:21:21 +1000247 u32 chid = nv31_mpeg_isr_chid(dev, inst);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000248 u32 stat = nv_rd32(dev, 0x00b100);
249 u32 type = nv_rd32(dev, 0x00b230);
250 u32 mthd = nv_rd32(dev, 0x00b234);
251 u32 data = nv_rd32(dev, 0x00b238);
252 u32 show = stat;
253
254 if (stat & 0x01000000) {
255 /* happens on initial binding of the object */
256 if (type == 0x00000020 && mthd == 0x0000) {
257 nv_mask(dev, 0x00b308, 0x00000000, 0x00000000);
258 show &= ~0x01000000;
259 }
260
261 if (type == 0x00000010) {
262 if (!nouveau_gpuobj_mthd_call2(dev, chid, 0x3174, mthd, data))
263 show &= ~0x01000000;
264 }
265 }
266
267 nv_wr32(dev, 0x00b100, stat);
268 nv_wr32(dev, 0x00b230, 0x00000001);
269
270 if (show && nouveau_ratelimit()) {
271 NV_INFO(dev, "PMPEG: Ch %d [0x%08x] 0x%08x 0x%08x 0x%08x 0x%08x\n",
272 chid, inst, stat, type, mthd, data);
273 }
274}
275
276static void
Ben Skeggs323dcac2011-06-23 16:21:21 +1000277nv31_vpe_isr(struct drm_device *dev)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000278{
279 if (nv_rd32(dev, 0x00b100))
Ben Skeggs323dcac2011-06-23 16:21:21 +1000280 nv31_mpeg_isr(dev);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000281
282 if (nv_rd32(dev, 0x00b800)) {
283 u32 stat = nv_rd32(dev, 0x00b800);
284 NV_INFO(dev, "PMSRCH: 0x%08x\n", stat);
285 nv_wr32(dev, 0xb800, stat);
286 }
287}
288
289static void
Ben Skeggs323dcac2011-06-23 16:21:21 +1000290nv31_mpeg_destroy(struct drm_device *dev, int engine)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000291{
Ben Skeggs323dcac2011-06-23 16:21:21 +1000292 struct nv31_mpeg_engine *pmpeg = nv_engine(dev, engine);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000293
294 nouveau_irq_unregister(dev, 0);
295
296 NVOBJ_ENGINE_DEL(dev, MPEG);
297 kfree(pmpeg);
298}
299
300int
Ben Skeggs323dcac2011-06-23 16:21:21 +1000301nv31_mpeg_create(struct drm_device *dev)
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000302{
Ben Skeggs52d07332011-06-23 16:44:05 +1000303 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs323dcac2011-06-23 16:21:21 +1000304 struct nv31_mpeg_engine *pmpeg;
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000305
306 pmpeg = kzalloc(sizeof(*pmpeg), GFP_KERNEL);
307 if (!pmpeg)
308 return -ENOMEM;
Ben Skeggs52d07332011-06-23 16:44:05 +1000309 atomic_set(&pmpeg->refcount, 0);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000310
Ben Skeggs323dcac2011-06-23 16:21:21 +1000311 pmpeg->base.destroy = nv31_mpeg_destroy;
312 pmpeg->base.init = nv31_mpeg_init;
313 pmpeg->base.fini = nv31_mpeg_fini;
Ben Skeggs52d07332011-06-23 16:44:05 +1000314 if (dev_priv->card_type < NV_40) {
315 pmpeg->base.context_new = nv31_mpeg_context_new;
316 pmpeg->base.context_del = nv31_mpeg_context_del;
317 } else {
318 pmpeg->base.context_new = nv40_mpeg_context_new;
319 pmpeg->base.context_del = nv40_mpeg_context_del;
320 }
Ben Skeggs323dcac2011-06-23 16:21:21 +1000321 pmpeg->base.object_new = nv31_mpeg_object_new;
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000322
323 /* ISR vector, PMC_ENABLE bit, and TILE regs are shared between
324 * all VPE engines, for this driver's purposes the PMPEG engine
325 * will be treated as the "master" and handle the global VPE
326 * bits too
327 */
Ben Skeggs323dcac2011-06-23 16:21:21 +1000328 pmpeg->base.set_tile_region = nv31_vpe_set_tile_region;
329 nouveau_irq_register(dev, 0, nv31_vpe_isr);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000330
331 NVOBJ_ENGINE_ADD(dev, MPEG, &pmpeg->base);
332 NVOBJ_CLASS(dev, 0x3174, MPEG);
Ben Skeggs323dcac2011-06-23 16:21:21 +1000333 NVOBJ_MTHD (dev, 0x3174, 0x0190, nv31_mpeg_mthd_dma);
334 NVOBJ_MTHD (dev, 0x3174, 0x01a0, nv31_mpeg_mthd_dma);
335 NVOBJ_MTHD (dev, 0x3174, 0x01b0, nv31_mpeg_mthd_dma);
Ben Skeggsa02ccc72011-04-04 16:08:24 +1000336
337#if 0
338 NVOBJ_ENGINE_ADD(dev, ME, &pme->base);
339 NVOBJ_CLASS(dev, 0x4075, ME);
340#endif
341 return 0;
342
343}