drm/nouveau/fifo/gk104: add sec plumbing
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nvkm / engine / fifo / gpfifogk104.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 #include "changk104.h"
25
26 #include <core/client.h>
27 #include <core/gpuobj.h>
28 #include <subdev/fb.h>
29 #include <subdev/mmu.h>
30 #include <subdev/timer.h>
31
32 #include <nvif/class.h>
33 #include <nvif/cla06f.h>
34 #include <nvif/unpack.h>
35
36 static int
37 gk104_fifo_gpfifo_kick(struct gk104_fifo_chan *chan)
38 {
39         struct gk104_fifo *fifo = chan->fifo;
40         struct nvkm_subdev *subdev = &fifo->base.engine.subdev;
41         struct nvkm_device *device = subdev->device;
42         struct nvkm_client *client = chan->base.object.client;
43
44         nvkm_wr32(device, 0x002634, chan->base.chid);
45         if (nvkm_msec(device, 2000,
46                 if (!(nvkm_rd32(device, 0x002634) & 0x00100000))
47                         break;
48         ) < 0) {
49                 nvkm_error(subdev, "channel %d [%s] kick timeout\n",
50                            chan->base.chid, client->name);
51                 return -EBUSY;
52         }
53
54         return 0;
55 }
56
57 static u32
58 gk104_fifo_gpfifo_engine_addr(struct nvkm_engine *engine)
59 {
60         switch (engine->subdev.index) {
61         case NVKM_ENGINE_SW    :
62         case NVKM_ENGINE_CE0   :
63         case NVKM_ENGINE_CE1   :
64         case NVKM_ENGINE_CE2   : return 0x0000;
65         case NVKM_ENGINE_GR    : return 0x0210;
66         case NVKM_ENGINE_SEC   : return 0x0220;
67         case NVKM_ENGINE_MSPDEC: return 0x0250;
68         case NVKM_ENGINE_MSPPP : return 0x0260;
69         case NVKM_ENGINE_MSVLD : return 0x0270;
70         case NVKM_ENGINE_MSENC : return 0x0290;
71         case NVKM_ENGINE_NVDEC : return 0x02100270;
72         case NVKM_ENGINE_NVENC0: return 0x02100290;
73         case NVKM_ENGINE_NVENC1: return 0x0210;
74         default:
75                 WARN_ON(1);
76                 return 0;
77         }
78 }
79
80 static int
81 gk104_fifo_gpfifo_engine_fini(struct nvkm_fifo_chan *base,
82                               struct nvkm_engine *engine, bool suspend)
83 {
84         struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
85         struct nvkm_gpuobj *inst = chan->base.inst;
86         u32 offset = gk104_fifo_gpfifo_engine_addr(engine);
87         int ret;
88
89         ret = gk104_fifo_gpfifo_kick(chan);
90         if (ret && suspend)
91                 return ret;
92
93         if (offset) {
94                 nvkm_kmap(inst);
95                 nvkm_wo32(inst, (offset & 0xffff) + 0x00, 0x00000000);
96                 nvkm_wo32(inst, (offset & 0xffff) + 0x04, 0x00000000);
97                 if ((offset >>= 16)) {
98                         nvkm_wo32(inst, offset + 0x00, 0x00000000);
99                         nvkm_wo32(inst, offset + 0x04, 0x00000000);
100                 }
101                 nvkm_done(inst);
102         }
103
104         return ret;
105 }
106
107 static int
108 gk104_fifo_gpfifo_engine_init(struct nvkm_fifo_chan *base,
109                               struct nvkm_engine *engine)
110 {
111         struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
112         struct nvkm_gpuobj *inst = chan->base.inst;
113         u32 offset = gk104_fifo_gpfifo_engine_addr(engine);
114
115         if (offset) {
116                 u64   addr = chan->engn[engine->subdev.index].vma.offset;
117                 u32 datalo = lower_32_bits(addr) | 0x00000004;
118                 u32 datahi = upper_32_bits(addr);
119                 nvkm_kmap(inst);
120                 nvkm_wo32(inst, (offset & 0xffff) + 0x00, datalo);
121                 nvkm_wo32(inst, (offset & 0xffff) + 0x04, datahi);
122                 if ((offset >>= 16)) {
123                         nvkm_wo32(inst, offset + 0x00, datalo);
124                         nvkm_wo32(inst, offset + 0x04, datahi);
125                 }
126                 nvkm_done(inst);
127         }
128
129         return 0;
130 }
131
132 static void
133 gk104_fifo_gpfifo_engine_dtor(struct nvkm_fifo_chan *base,
134                               struct nvkm_engine *engine)
135 {
136         struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
137         nvkm_gpuobj_unmap(&chan->engn[engine->subdev.index].vma);
138         nvkm_gpuobj_del(&chan->engn[engine->subdev.index].inst);
139 }
140
141 static int
142 gk104_fifo_gpfifo_engine_ctor(struct nvkm_fifo_chan *base,
143                               struct nvkm_engine *engine,
144                               struct nvkm_object *object)
145 {
146         struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
147         int engn = engine->subdev.index;
148         int ret;
149
150         if (!gk104_fifo_gpfifo_engine_addr(engine))
151                 return 0;
152
153         ret = nvkm_object_bind(object, NULL, 0, &chan->engn[engn].inst);
154         if (ret)
155                 return ret;
156
157         return nvkm_gpuobj_map(chan->engn[engn].inst, chan->vm,
158                                NV_MEM_ACCESS_RW, &chan->engn[engn].vma);
159 }
160
161 static void
162 gk104_fifo_gpfifo_fini(struct nvkm_fifo_chan *base)
163 {
164         struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
165         struct gk104_fifo *fifo = chan->fifo;
166         struct nvkm_device *device = fifo->base.engine.subdev.device;
167         u32 coff = chan->base.chid * 8;
168
169         if (!list_empty(&chan->head)) {
170                 gk104_fifo_runlist_remove(fifo, chan);
171                 nvkm_mask(device, 0x800004 + coff, 0x00000800, 0x00000800);
172                 gk104_fifo_gpfifo_kick(chan);
173                 gk104_fifo_runlist_commit(fifo, chan->runl);
174         }
175
176         nvkm_wr32(device, 0x800000 + coff, 0x00000000);
177 }
178
179 static void
180 gk104_fifo_gpfifo_init(struct nvkm_fifo_chan *base)
181 {
182         struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
183         struct gk104_fifo *fifo = chan->fifo;
184         struct nvkm_device *device = fifo->base.engine.subdev.device;
185         u32 addr = chan->base.inst->addr >> 12;
186         u32 coff = chan->base.chid * 8;
187
188         nvkm_mask(device, 0x800004 + coff, 0x000f0000, chan->runl << 16);
189         nvkm_wr32(device, 0x800000 + coff, 0x80000000 | addr);
190
191         if (list_empty(&chan->head) && !chan->killed) {
192                 gk104_fifo_runlist_insert(fifo, chan);
193                 nvkm_mask(device, 0x800004 + coff, 0x00000400, 0x00000400);
194                 gk104_fifo_runlist_commit(fifo, chan->runl);
195                 nvkm_mask(device, 0x800004 + coff, 0x00000400, 0x00000400);
196         }
197 }
198
199 static void *
200 gk104_fifo_gpfifo_dtor(struct nvkm_fifo_chan *base)
201 {
202         struct gk104_fifo_chan *chan = gk104_fifo_chan(base);
203         nvkm_vm_ref(NULL, &chan->vm, chan->pgd);
204         nvkm_gpuobj_del(&chan->pgd);
205         return chan;
206 }
207
208 static const struct nvkm_fifo_chan_func
209 gk104_fifo_gpfifo_func = {
210         .dtor = gk104_fifo_gpfifo_dtor,
211         .init = gk104_fifo_gpfifo_init,
212         .fini = gk104_fifo_gpfifo_fini,
213         .ntfy = g84_fifo_chan_ntfy,
214         .engine_ctor = gk104_fifo_gpfifo_engine_ctor,
215         .engine_dtor = gk104_fifo_gpfifo_engine_dtor,
216         .engine_init = gk104_fifo_gpfifo_engine_init,
217         .engine_fini = gk104_fifo_gpfifo_engine_fini,
218 };
219
220 struct gk104_fifo_chan_func {
221         u32 engine;
222         u64 subdev;
223 };
224
225 static int
226 gk104_fifo_gpfifo_new_(const struct gk104_fifo_chan_func *func,
227                        struct gk104_fifo *fifo, u32 *engmask, u16 *chid,
228                        u64 vm, u64 ioffset, u64 ilength,
229                        const struct nvkm_oclass *oclass,
230                        struct nvkm_object **pobject)
231 {
232         struct nvkm_device *device = fifo->base.engine.subdev.device;
233         struct gk104_fifo_chan *chan;
234         int runlist = -1, ret = -ENOSYS, i, j;
235         u32 engines = 0, present = 0;
236         u64 subdevs = 0;
237         u64 usermem;
238
239         /* Determine which downstream engines are present */
240         for (i = 0; i < fifo->engine_nr; i++) {
241                 struct nvkm_engine *engine = fifo->engine[i].engine;
242                 if (engine) {
243                         u64 submask = BIT_ULL(engine->subdev.index);
244                         for (j = 0; func[j].subdev; j++) {
245                                 if (func[j].subdev & submask) {
246                                         present |= func[j].engine;
247                                         break;
248                                 }
249                         }
250
251                         if (!func[j].subdev)
252                                 continue;
253
254                         if (runlist < 0 && (*engmask & present))
255                                 runlist = fifo->engine[i].runl;
256                         if (runlist == fifo->engine[i].runl) {
257                                 engines |= func[j].engine;
258                                 subdevs |= func[j].subdev;
259                         }
260                 }
261         }
262
263         /* Just an engine mask query?  All done here! */
264         if (!*engmask) {
265                 *engmask = present;
266                 return nvkm_object_new(oclass, NULL, 0, pobject);
267         }
268
269         /* No runlist?  No supported engines. */
270         *engmask = present;
271         if (runlist < 0)
272                 return -ENODEV;
273         *engmask = engines;
274
275         /* Allocate the channel. */
276         if (!(chan = kzalloc(sizeof(*chan), GFP_KERNEL)))
277                 return -ENOMEM;
278         *pobject = &chan->base.object;
279         chan->fifo = fifo;
280         chan->runl = runlist;
281         INIT_LIST_HEAD(&chan->head);
282
283         ret = nvkm_fifo_chan_ctor(&gk104_fifo_gpfifo_func, &fifo->base,
284                                   0x1000, 0x1000, true, vm, 0, subdevs,
285                                   1, fifo->user.bar.offset, 0x200,
286                                   oclass, &chan->base);
287         if (ret)
288                 return ret;
289
290         *chid = chan->base.chid;
291
292         /* Page directory. */
293         ret = nvkm_gpuobj_new(device, 0x10000, 0x1000, false, NULL, &chan->pgd);
294         if (ret)
295                 return ret;
296
297         nvkm_kmap(chan->base.inst);
298         nvkm_wo32(chan->base.inst, 0x0200, lower_32_bits(chan->pgd->addr));
299         nvkm_wo32(chan->base.inst, 0x0204, upper_32_bits(chan->pgd->addr));
300         nvkm_wo32(chan->base.inst, 0x0208, 0xffffffff);
301         nvkm_wo32(chan->base.inst, 0x020c, 0x000000ff);
302         nvkm_done(chan->base.inst);
303
304         ret = nvkm_vm_ref(chan->base.vm, &chan->vm, chan->pgd);
305         if (ret)
306                 return ret;
307
308         /* Clear channel control registers. */
309         usermem = chan->base.chid * 0x200;
310         ilength = order_base_2(ilength / 8);
311
312         nvkm_kmap(fifo->user.mem);
313         for (i = 0; i < 0x200; i += 4)
314                 nvkm_wo32(fifo->user.mem, usermem + i, 0x00000000);
315         nvkm_done(fifo->user.mem);
316         usermem = nvkm_memory_addr(fifo->user.mem) + usermem;
317
318         /* RAMFC */
319         nvkm_kmap(chan->base.inst);
320         nvkm_wo32(chan->base.inst, 0x08, lower_32_bits(usermem));
321         nvkm_wo32(chan->base.inst, 0x0c, upper_32_bits(usermem));
322         nvkm_wo32(chan->base.inst, 0x10, 0x0000face);
323         nvkm_wo32(chan->base.inst, 0x30, 0xfffff902);
324         nvkm_wo32(chan->base.inst, 0x48, lower_32_bits(ioffset));
325         nvkm_wo32(chan->base.inst, 0x4c, upper_32_bits(ioffset) |
326                                          (ilength << 16));
327         nvkm_wo32(chan->base.inst, 0x84, 0x20400000);
328         nvkm_wo32(chan->base.inst, 0x94, 0x30000001);
329         nvkm_wo32(chan->base.inst, 0x9c, 0x00000100);
330         nvkm_wo32(chan->base.inst, 0xac, 0x0000001f);
331         nvkm_wo32(chan->base.inst, 0xe8, chan->base.chid);
332         nvkm_wo32(chan->base.inst, 0xb8, 0xf8000000);
333         nvkm_wo32(chan->base.inst, 0xf8, 0x10003080); /* 0x002310 */
334         nvkm_wo32(chan->base.inst, 0xfc, 0x10000010); /* 0x002350 */
335         nvkm_done(chan->base.inst);
336         return 0;
337 }
338
339 static const struct gk104_fifo_chan_func
340 gk104_fifo_gpfifo[] = {
341         { NVA06F_V0_ENGINE_SW | NVA06F_V0_ENGINE_GR,
342                 BIT_ULL(NVKM_ENGINE_SW) | BIT_ULL(NVKM_ENGINE_GR)
343         },
344         { NVA06F_V0_ENGINE_SEC   , BIT_ULL(NVKM_ENGINE_SEC   ) },
345         { NVA06F_V0_ENGINE_MSVLD , BIT_ULL(NVKM_ENGINE_MSVLD ) },
346         { NVA06F_V0_ENGINE_MSPDEC, BIT_ULL(NVKM_ENGINE_MSPDEC) },
347         { NVA06F_V0_ENGINE_MSPPP , BIT_ULL(NVKM_ENGINE_MSPPP ) },
348         { NVA06F_V0_ENGINE_MSENC , BIT_ULL(NVKM_ENGINE_MSENC ) },
349         { NVA06F_V0_ENGINE_NVDEC , BIT_ULL(NVKM_ENGINE_NVDEC ) },
350         { NVA06F_V0_ENGINE_NVENC0, BIT_ULL(NVKM_ENGINE_NVENC0) },
351         { NVA06F_V0_ENGINE_NVENC1, BIT_ULL(NVKM_ENGINE_NVENC1) },
352         { NVA06F_V0_ENGINE_CE0   , BIT_ULL(NVKM_ENGINE_CE0   ) },
353         { NVA06F_V0_ENGINE_CE1   , BIT_ULL(NVKM_ENGINE_CE1   ) },
354         { NVA06F_V0_ENGINE_CE2   , BIT_ULL(NVKM_ENGINE_CE2   ) },
355         {}
356 };
357
358 int
359 gk104_fifo_gpfifo_new(struct nvkm_fifo *base, const struct nvkm_oclass *oclass,
360                       void *data, u32 size, struct nvkm_object **pobject)
361 {
362         struct nvkm_object *parent = oclass->parent;
363         union {
364                 struct kepler_channel_gpfifo_a_v0 v0;
365         } *args = data;
366         struct gk104_fifo *fifo = gk104_fifo(base);
367         int ret = -ENOSYS;
368
369         nvif_ioctl(parent, "create channel gpfifo size %d\n", size);
370         if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
371                 nvif_ioctl(parent, "create channel gpfifo vers %d vm %llx "
372                                    "ioffset %016llx ilength %08x engine %08x\n",
373                            args->v0.version, args->v0.vm, args->v0.ioffset,
374                            args->v0.ilength, args->v0.engines);
375                 return gk104_fifo_gpfifo_new_(gk104_fifo_gpfifo, fifo,
376                                               &args->v0.engines,
377                                               &args->v0.chid,
378                                                args->v0.vm,
379                                                args->v0.ioffset,
380                                                args->v0.ilength,
381                                               oclass, pobject);
382
383         }
384
385         return ret;
386 }
387
388 const struct nvkm_fifo_chan_oclass
389 gk104_fifo_gpfifo_oclass = {
390         .base.oclass = KEPLER_CHANNEL_GPFIFO_A,
391         .base.minver = 0,
392         .base.maxver = 0,
393         .ctor = gk104_fifo_gpfifo_new,
394 };