1/* $NetBSD: nouveau_engine_mpeg_nv31.c,v 1.1.1.1 2014/08/06 12:36:27 riastradh Exp $ */
2
3/*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_engine_mpeg_nv31.c,v 1.1.1.1 2014/08/06 12:36:27 riastradh Exp $");
29
30#include <core/client.h>
31#include <core/os.h>
32#include <core/class.h>
33#include <core/engctx.h>
34#include <core/handle.h>
35
36#include <subdev/fb.h>
37#include <subdev/timer.h>
38#include <subdev/instmem.h>
39
40#include <engine/fifo.h>
41#include <engine/mpeg.h>
42#include <engine/mpeg/nv31.h>
43
44/*******************************************************************************
45 * MPEG object classes
46 ******************************************************************************/
47
48static int
49nv31_mpeg_object_ctor(struct nouveau_object *parent,
50 struct nouveau_object *engine,
51 struct nouveau_oclass *oclass, void *data, u32 size,
52 struct nouveau_object **pobject)
53{
54 struct nouveau_gpuobj *obj;
55 int ret;
56
57 ret = nouveau_gpuobj_create(parent, engine, oclass, 0, parent,
58 20, 16, 0, &obj);
59 *pobject = nv_object(obj);
60 if (ret)
61 return ret;
62
63 nv_wo32(obj, 0x00, nv_mclass(obj));
64 nv_wo32(obj, 0x04, 0x00000000);
65 nv_wo32(obj, 0x08, 0x00000000);
66 nv_wo32(obj, 0x0c, 0x00000000);
67 return 0;
68}
69
70static int
71nv31_mpeg_mthd_dma(struct nouveau_object *object, u32 mthd, void *arg, u32 len)
72{
73 struct nouveau_instmem *imem = nouveau_instmem(object);
74 struct nv31_mpeg_priv *priv = (void *)object->engine;
75 u32 inst = *(u32 *)arg << 4;
76 u32 dma0 = nv_ro32(imem, inst + 0);
77 u32 dma1 = nv_ro32(imem, inst + 4);
78 u32 dma2 = nv_ro32(imem, inst + 8);
79 u32 base = (dma2 & 0xfffff000) | (dma0 >> 20);
80 u32 size = dma1 + 1;
81
82 /* only allow linear DMA objects */
83 if (!(dma0 & 0x00002000))
84 return -EINVAL;
85
86 if (mthd == 0x0190) {
87 /* DMA_CMD */
88 nv_mask(priv, 0x00b300, 0x00010000, (dma0 & 0x00030000) ? 0x00010000 : 0);
89 nv_wr32(priv, 0x00b334, base);
90 nv_wr32(priv, 0x00b324, size);
91 } else
92 if (mthd == 0x01a0) {
93 /* DMA_DATA */
94 nv_mask(priv, 0x00b300, 0x00020000, (dma0 & 0x00030000) ? 0x00020000 : 0);
95 nv_wr32(priv, 0x00b360, base);
96 nv_wr32(priv, 0x00b364, size);
97 } else {
98 /* DMA_IMAGE, VRAM only */
99 if (dma0 & 0x00030000)
100 return -EINVAL;
101
102 nv_wr32(priv, 0x00b370, base);
103 nv_wr32(priv, 0x00b374, size);
104 }
105
106 return 0;
107}
108
109struct nouveau_ofuncs
110nv31_mpeg_ofuncs = {
111 .ctor = nv31_mpeg_object_ctor,
112 .dtor = _nouveau_gpuobj_dtor,
113 .init = _nouveau_gpuobj_init,
114 .fini = _nouveau_gpuobj_fini,
115 .rd32 = _nouveau_gpuobj_rd32,
116 .wr32 = _nouveau_gpuobj_wr32,
117};
118
119static struct nouveau_omthds
120nv31_mpeg_omthds[] = {
121 { 0x0190, 0x0190, nv31_mpeg_mthd_dma },
122 { 0x01a0, 0x01a0, nv31_mpeg_mthd_dma },
123 { 0x01b0, 0x01b0, nv31_mpeg_mthd_dma },
124 {}
125};
126
127struct nouveau_oclass
128nv31_mpeg_sclass[] = {
129 { 0x3174, &nv31_mpeg_ofuncs, nv31_mpeg_omthds },
130 {}
131};
132
133/*******************************************************************************
134 * PMPEG context
135 ******************************************************************************/
136
137static int
138nv31_mpeg_context_ctor(struct nouveau_object *parent,
139 struct nouveau_object *engine,
140 struct nouveau_oclass *oclass, void *data, u32 size,
141 struct nouveau_object **pobject)
142{
143 struct nv31_mpeg_priv *priv = (void *)engine;
144 struct nv31_mpeg_chan *chan;
145 unsigned long flags;
146 int ret;
147
148 ret = nouveau_object_create(parent, engine, oclass, 0, &chan);
149 *pobject = nv_object(chan);
150 if (ret)
151 return ret;
152
153 spin_lock_irqsave(&nv_engine(priv)->lock, flags);
154 if (priv->chan) {
155 spin_unlock_irqrestore(&nv_engine(priv)->lock, flags);
156 nouveau_object_destroy(&chan->base);
157 *pobject = NULL;
158 return -EBUSY;
159 }
160 priv->chan = chan;
161 spin_unlock_irqrestore(&nv_engine(priv)->lock, flags);
162 return 0;
163}
164
165static void
166nv31_mpeg_context_dtor(struct nouveau_object *object)
167{
168 struct nv31_mpeg_priv *priv = (void *)object->engine;
169 struct nv31_mpeg_chan *chan = (void *)object;
170 unsigned long flags;
171
172 spin_lock_irqsave(&nv_engine(priv)->lock, flags);
173 priv->chan = NULL;
174 spin_unlock_irqrestore(&nv_engine(priv)->lock, flags);
175 nouveau_object_destroy(&chan->base);
176}
177
178struct nouveau_oclass
179nv31_mpeg_cclass = {
180 .handle = NV_ENGCTX(MPEG, 0x31),
181 .ofuncs = &(struct nouveau_ofuncs) {
182 .ctor = nv31_mpeg_context_ctor,
183 .dtor = nv31_mpeg_context_dtor,
184 .init = nouveau_object_init,
185 .fini = nouveau_object_fini,
186 },
187};
188
189/*******************************************************************************
190 * PMPEG engine/subdev functions
191 ******************************************************************************/
192
193void
194nv31_mpeg_tile_prog(struct nouveau_engine *engine, int i)
195{
196 struct nouveau_fb_tile *tile = &nouveau_fb(engine)->tile.region[i];
197 struct nv31_mpeg_priv *priv = (void *)engine;
198
199 nv_wr32(priv, 0x00b008 + (i * 0x10), tile->pitch);
200 nv_wr32(priv, 0x00b004 + (i * 0x10), tile->limit);
201 nv_wr32(priv, 0x00b000 + (i * 0x10), tile->addr);
202}
203
204void
205nv31_mpeg_intr(struct nouveau_subdev *subdev)
206{
207 struct nv31_mpeg_priv *priv = (void *)subdev;
208 struct nouveau_fifo *pfifo = nouveau_fifo(subdev);
209 struct nouveau_handle *handle;
210 struct nouveau_object *engctx;
211 u32 stat = nv_rd32(priv, 0x00b100);
212 u32 type = nv_rd32(priv, 0x00b230);
213 u32 mthd = nv_rd32(priv, 0x00b234);
214 u32 data = nv_rd32(priv, 0x00b238);
215 u32 show = stat;
216 unsigned long flags;
217
218 spin_lock_irqsave(&nv_engine(priv)->lock, flags);
219 engctx = nv_object(priv->chan);
220
221 if (stat & 0x01000000) {
222 /* happens on initial binding of the object */
223 if (type == 0x00000020 && mthd == 0x0000) {
224 nv_mask(priv, 0x00b308, 0x00000000, 0x00000000);
225 show &= ~0x01000000;
226 }
227
228 if (type == 0x00000010 && engctx) {
229 handle = nouveau_handle_get_class(engctx, 0x3174);
230 if (handle && !nv_call(handle->object, mthd, data))
231 show &= ~0x01000000;
232 nouveau_handle_put(handle);
233 }
234 }
235
236 nv_wr32(priv, 0x00b100, stat);
237 nv_wr32(priv, 0x00b230, 0x00000001);
238
239 if (show) {
240 nv_error(priv, "ch %d [%s] 0x%08x 0x%08x 0x%08x 0x%08x\n",
241 pfifo->chid(pfifo, engctx),
242 nouveau_client_name(engctx), stat, type, mthd, data);
243 }
244
245 spin_unlock_irqrestore(&nv_engine(priv)->lock, flags);
246}
247
248static int
249nv31_mpeg_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
250 struct nouveau_oclass *oclass, void *data, u32 size,
251 struct nouveau_object **pobject)
252{
253 struct nv31_mpeg_priv *priv;
254 int ret;
255
256 ret = nouveau_mpeg_create(parent, engine, oclass, &priv);
257 *pobject = nv_object(priv);
258 if (ret)
259 return ret;
260
261 nv_subdev(priv)->unit = 0x00000002;
262 nv_subdev(priv)->intr = nv31_mpeg_intr;
263 nv_engine(priv)->cclass = &nv31_mpeg_cclass;
264 nv_engine(priv)->sclass = nv31_mpeg_sclass;
265 nv_engine(priv)->tile_prog = nv31_mpeg_tile_prog;
266 return 0;
267}
268
269int
270nv31_mpeg_init(struct nouveau_object *object)
271{
272 struct nouveau_engine *engine = nv_engine(object);
273 struct nv31_mpeg_priv *priv = (void *)object;
274 struct nouveau_fb *pfb = nouveau_fb(object);
275 int ret, i;
276
277 ret = nouveau_mpeg_init(&priv->base);
278 if (ret)
279 return ret;
280
281 /* VPE init */
282 nv_wr32(priv, 0x00b0e0, 0x00000020); /* nvidia: rd 0x01, wr 0x20 */
283 nv_wr32(priv, 0x00b0e8, 0x00000020); /* nvidia: rd 0x01, wr 0x20 */
284
285 for (i = 0; i < pfb->tile.regions; i++)
286 engine->tile_prog(engine, i);
287
288 /* PMPEG init */
289 nv_wr32(priv, 0x00b32c, 0x00000000);
290 nv_wr32(priv, 0x00b314, 0x00000100);
291 nv_wr32(priv, 0x00b220, 0x00000031);
292 nv_wr32(priv, 0x00b300, 0x02001ec1);
293 nv_mask(priv, 0x00b32c, 0x00000001, 0x00000001);
294
295 nv_wr32(priv, 0x00b100, 0xffffffff);
296 nv_wr32(priv, 0x00b140, 0xffffffff);
297
298 if (!nv_wait(priv, 0x00b200, 0x00000001, 0x00000000)) {
299 nv_error(priv, "timeout 0x%08x\n", nv_rd32(priv, 0x00b200));
300 return -EBUSY;
301 }
302
303 return 0;
304}
305
306struct nouveau_oclass
307nv31_mpeg_oclass = {
308 .handle = NV_ENGINE(MPEG, 0x31),
309 .ofuncs = &(struct nouveau_ofuncs) {
310 .ctor = nv31_mpeg_ctor,
311 .dtor = _nouveau_mpeg_dtor,
312 .init = nv31_mpeg_init,
313 .fini = _nouveau_mpeg_fini,
314 },
315};
316