HID: picolcd: sanity check report size in raw_event() callback
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / core / engine / mpeg / nv31.c
1 /*
2  * Copyright 2012 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 <core/client.h>
26 #include <core/os.h>
27 #include <core/class.h>
28 #include <core/engctx.h>
29 #include <core/handle.h>
30
31 #include <subdev/fb.h>
32 #include <subdev/timer.h>
33 #include <subdev/instmem.h>
34
35 #include <engine/fifo.h>
36 #include <engine/mpeg.h>
37 #include <engine/mpeg/nv31.h>
38
39 /*******************************************************************************
40  * MPEG object classes
41  ******************************************************************************/
42
43 static int
44 nv31_mpeg_object_ctor(struct nouveau_object *parent,
45                       struct nouveau_object *engine,
46                       struct nouveau_oclass *oclass, void *data, u32 size,
47                       struct nouveau_object **pobject)
48 {
49         struct nouveau_gpuobj *obj;
50         int ret;
51
52         ret = nouveau_gpuobj_create(parent, engine, oclass, 0, parent,
53                                     20, 16, 0, &obj);
54         *pobject = nv_object(obj);
55         if (ret)
56                 return ret;
57
58         nv_wo32(obj, 0x00, nv_mclass(obj));
59         nv_wo32(obj, 0x04, 0x00000000);
60         nv_wo32(obj, 0x08, 0x00000000);
61         nv_wo32(obj, 0x0c, 0x00000000);
62         return 0;
63 }
64
65 static int
66 nv31_mpeg_mthd_dma(struct nouveau_object *object, u32 mthd, void *arg, u32 len)
67 {
68         struct nouveau_instmem *imem = nouveau_instmem(object);
69         struct nv31_mpeg_priv *priv = (void *)object->engine;
70         u32 inst = *(u32 *)arg << 4;
71         u32 dma0 = nv_ro32(imem, inst + 0);
72         u32 dma1 = nv_ro32(imem, inst + 4);
73         u32 dma2 = nv_ro32(imem, inst + 8);
74         u32 base = (dma2 & 0xfffff000) | (dma0 >> 20);
75         u32 size = dma1 + 1;
76
77         /* only allow linear DMA objects */
78         if (!(dma0 & 0x00002000))
79                 return -EINVAL;
80
81         if (mthd == 0x0190) {
82                 /* DMA_CMD */
83                 nv_mask(priv, 0x00b300, 0x00010000, (dma0 & 0x00030000) ? 0x00010000 : 0);
84                 nv_wr32(priv, 0x00b334, base);
85                 nv_wr32(priv, 0x00b324, size);
86         } else
87         if (mthd == 0x01a0) {
88                 /* DMA_DATA */
89                 nv_mask(priv, 0x00b300, 0x00020000, (dma0 & 0x00030000) ? 0x00020000 : 0);
90                 nv_wr32(priv, 0x00b360, base);
91                 nv_wr32(priv, 0x00b364, size);
92         } else {
93                 /* DMA_IMAGE, VRAM only */
94                 if (dma0 & 0x00030000)
95                         return -EINVAL;
96
97                 nv_wr32(priv, 0x00b370, base);
98                 nv_wr32(priv, 0x00b374, size);
99         }
100
101         return 0;
102 }
103
104 struct nouveau_ofuncs
105 nv31_mpeg_ofuncs = {
106         .ctor = nv31_mpeg_object_ctor,
107         .dtor = _nouveau_gpuobj_dtor,
108         .init = _nouveau_gpuobj_init,
109         .fini = _nouveau_gpuobj_fini,
110         .rd32 = _nouveau_gpuobj_rd32,
111         .wr32 = _nouveau_gpuobj_wr32,
112 };
113
114 static struct nouveau_omthds
115 nv31_mpeg_omthds[] = {
116         { 0x0190, 0x0190, nv31_mpeg_mthd_dma },
117         { 0x01a0, 0x01a0, nv31_mpeg_mthd_dma },
118         { 0x01b0, 0x01b0, nv31_mpeg_mthd_dma },
119         {}
120 };
121
122 struct nouveau_oclass
123 nv31_mpeg_sclass[] = {
124         { 0x3174, &nv31_mpeg_ofuncs, nv31_mpeg_omthds },
125         {}
126 };
127
128 /*******************************************************************************
129  * PMPEG context
130  ******************************************************************************/
131
132 static int
133 nv31_mpeg_context_ctor(struct nouveau_object *parent,
134                        struct nouveau_object *engine,
135                        struct nouveau_oclass *oclass, void *data, u32 size,
136                        struct nouveau_object **pobject)
137 {
138         struct nv31_mpeg_priv *priv = (void *)engine;
139         struct nv31_mpeg_chan *chan;
140         unsigned long flags;
141         int ret;
142
143         ret = nouveau_object_create(parent, engine, oclass, 0, &chan);
144         *pobject = nv_object(chan);
145         if (ret)
146                 return ret;
147
148         spin_lock_irqsave(&nv_engine(priv)->lock, flags);
149         if (priv->chan) {
150                 spin_unlock_irqrestore(&nv_engine(priv)->lock, flags);
151                 nouveau_object_destroy(&chan->base);
152                 *pobject = NULL;
153                 return -EBUSY;
154         }
155         priv->chan = chan;
156         spin_unlock_irqrestore(&nv_engine(priv)->lock, flags);
157         return 0;
158 }
159
160 static void
161 nv31_mpeg_context_dtor(struct nouveau_object *object)
162 {
163         struct nv31_mpeg_priv *priv = (void *)object->engine;
164         struct nv31_mpeg_chan *chan = (void *)object;
165         unsigned long flags;
166
167         spin_lock_irqsave(&nv_engine(priv)->lock, flags);
168         priv->chan = NULL;
169         spin_unlock_irqrestore(&nv_engine(priv)->lock, flags);
170         nouveau_object_destroy(&chan->base);
171 }
172
173 struct nouveau_oclass
174 nv31_mpeg_cclass = {
175         .handle = NV_ENGCTX(MPEG, 0x31),
176         .ofuncs = &(struct nouveau_ofuncs) {
177                 .ctor = nv31_mpeg_context_ctor,
178                 .dtor = nv31_mpeg_context_dtor,
179                 .init = nouveau_object_init,
180                 .fini = nouveau_object_fini,
181         },
182 };
183
184 /*******************************************************************************
185  * PMPEG engine/subdev functions
186  ******************************************************************************/
187
188 void
189 nv31_mpeg_tile_prog(struct nouveau_engine *engine, int i)
190 {
191         struct nouveau_fb_tile *tile = &nouveau_fb(engine)->tile.region[i];
192         struct nv31_mpeg_priv *priv = (void *)engine;
193
194         nv_wr32(priv, 0x00b008 + (i * 0x10), tile->pitch);
195         nv_wr32(priv, 0x00b004 + (i * 0x10), tile->limit);
196         nv_wr32(priv, 0x00b000 + (i * 0x10), tile->addr);
197 }
198
199 void
200 nv31_mpeg_intr(struct nouveau_subdev *subdev)
201 {
202         struct nv31_mpeg_priv *priv = (void *)subdev;
203         struct nouveau_fifo *pfifo = nouveau_fifo(subdev);
204         struct nouveau_handle *handle;
205         struct nouveau_object *engctx;
206         u32 stat = nv_rd32(priv, 0x00b100);
207         u32 type = nv_rd32(priv, 0x00b230);
208         u32 mthd = nv_rd32(priv, 0x00b234);
209         u32 data = nv_rd32(priv, 0x00b238);
210         u32 show = stat;
211         unsigned long flags;
212
213         spin_lock_irqsave(&nv_engine(priv)->lock, flags);
214         engctx = nv_object(priv->chan);
215
216         if (stat & 0x01000000) {
217                 /* happens on initial binding of the object */
218                 if (type == 0x00000020 && mthd == 0x0000) {
219                         nv_mask(priv, 0x00b308, 0x00000000, 0x00000000);
220                         show &= ~0x01000000;
221                 }
222
223                 if (type == 0x00000010 && engctx) {
224                         handle = nouveau_handle_get_class(engctx, 0x3174);
225                         if (handle && !nv_call(handle->object, mthd, data))
226                                 show &= ~0x01000000;
227                         nouveau_handle_put(handle);
228                 }
229         }
230
231         nv_wr32(priv, 0x00b100, stat);
232         nv_wr32(priv, 0x00b230, 0x00000001);
233
234         if (show) {
235                 nv_error(priv, "ch %d [%s] 0x%08x 0x%08x 0x%08x 0x%08x\n",
236                          pfifo->chid(pfifo, engctx),
237                          nouveau_client_name(engctx), stat, type, mthd, data);
238         }
239
240         spin_unlock_irqrestore(&nv_engine(priv)->lock, flags);
241 }
242
243 static int
244 nv31_mpeg_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
245                struct nouveau_oclass *oclass, void *data, u32 size,
246                struct nouveau_object **pobject)
247 {
248         struct nv31_mpeg_priv *priv;
249         int ret;
250
251         ret = nouveau_mpeg_create(parent, engine, oclass, &priv);
252         *pobject = nv_object(priv);
253         if (ret)
254                 return ret;
255
256         nv_subdev(priv)->unit = 0x00000002;
257         nv_subdev(priv)->intr = nv31_mpeg_intr;
258         nv_engine(priv)->cclass = &nv31_mpeg_cclass;
259         nv_engine(priv)->sclass = nv31_mpeg_sclass;
260         nv_engine(priv)->tile_prog = nv31_mpeg_tile_prog;
261         return 0;
262 }
263
264 int
265 nv31_mpeg_init(struct nouveau_object *object)
266 {
267         struct nouveau_engine *engine = nv_engine(object);
268         struct nv31_mpeg_priv *priv = (void *)object;
269         struct nouveau_fb *pfb = nouveau_fb(object);
270         int ret, i;
271
272         ret = nouveau_mpeg_init(&priv->base);
273         if (ret)
274                 return ret;
275
276         /* VPE init */
277         nv_wr32(priv, 0x00b0e0, 0x00000020); /* nvidia: rd 0x01, wr 0x20 */
278         nv_wr32(priv, 0x00b0e8, 0x00000020); /* nvidia: rd 0x01, wr 0x20 */
279
280         for (i = 0; i < pfb->tile.regions; i++)
281                 engine->tile_prog(engine, i);
282
283         /* PMPEG init */
284         nv_wr32(priv, 0x00b32c, 0x00000000);
285         nv_wr32(priv, 0x00b314, 0x00000100);
286         nv_wr32(priv, 0x00b220, 0x00000031);
287         nv_wr32(priv, 0x00b300, 0x02001ec1);
288         nv_mask(priv, 0x00b32c, 0x00000001, 0x00000001);
289
290         nv_wr32(priv, 0x00b100, 0xffffffff);
291         nv_wr32(priv, 0x00b140, 0xffffffff);
292
293         if (!nv_wait(priv, 0x00b200, 0x00000001, 0x00000000)) {
294                 nv_error(priv, "timeout 0x%08x\n", nv_rd32(priv, 0x00b200));
295                 return -EBUSY;
296         }
297
298         return 0;
299 }
300
301 struct nouveau_oclass
302 nv31_mpeg_oclass = {
303         .handle = NV_ENGINE(MPEG, 0x31),
304         .ofuncs = &(struct nouveau_ofuncs) {
305                 .ctor = nv31_mpeg_ctor,
306                 .dtor = _nouveau_mpeg_dtor,
307                 .init = nv31_mpeg_init,
308                 .fini = _nouveau_mpeg_fini,
309         },
310 };