]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/nouveau/core/engine/fifo/nvc0.c
Merge branches 'acpi-scan', 'acpi-utils' and 'acpi-pm'
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / core / engine / fifo / nvc0.c
CommitLineData
4b223eef 1/*
ebb945a9 2 * Copyright 2012 Red Hat Inc.
4b223eef
BS
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
ebb945a9
BS
25#include <core/client.h>
26#include <core/handle.h>
27#include <core/namedb.h>
28#include <core/gpuobj.h>
29#include <core/engctx.h>
9bd2ddba 30#include <core/event.h>
bbf8906b
BS
31#include <nvif/unpack.h>
32#include <nvif/class.h>
ebb945a9 33#include <core/enum.h>
4b223eef 34
ebb945a9
BS
35#include <subdev/timer.h>
36#include <subdev/bar.h>
52225551 37#include <subdev/fb.h>
ebb945a9 38#include <subdev/vm.h>
b2b09938 39
ebb945a9
BS
40#include <engine/dmaobj.h>
41#include <engine/fifo.h>
b2b09938
BS
42
43struct nvc0_fifo_priv {
ebb945a9 44 struct nouveau_fifo base;
24e8341e
BS
45
46 struct work_struct fault;
47 u64 mask;
48
a07d0e76
BS
49 struct {
50 struct nouveau_gpuobj *mem[2];
51 int active;
52 wait_queue_head_t wait;
53 } runlist;
24e8341e 54
9da226f6
BS
55 struct {
56 struct nouveau_gpuobj *mem;
57 struct nouveau_vma bar;
58 } user;
ec9c0883 59 int spoon_nr;
b2b09938
BS
60};
61
ebb945a9
BS
62struct nvc0_fifo_base {
63 struct nouveau_fifo_base base;
64 struct nouveau_gpuobj *pgd;
65 struct nouveau_vm *vm;
66};
67
b2b09938 68struct nvc0_fifo_chan {
c420b2dc 69 struct nouveau_fifo_chan base;
e2822b7a
BS
70 enum {
71 STOPPED,
72 RUNNING,
73 KILLED
74 } state;
b2b09938
BS
75};
76
ebb945a9
BS
77/*******************************************************************************
78 * FIFO channel objects
79 ******************************************************************************/
80
b2b09938 81static void
0357466d 82nvc0_fifo_runlist_update(struct nvc0_fifo_priv *priv)
b2b09938 83{
ebb945a9 84 struct nouveau_bar *bar = nouveau_bar(priv);
b2b09938
BS
85 struct nouveau_gpuobj *cur;
86 int i, p;
87
fadb1719 88 mutex_lock(&nv_subdev(priv)->mutex);
a07d0e76
BS
89 cur = priv->runlist.mem[priv->runlist.active];
90 priv->runlist.active = !priv->runlist.active;
b2b09938
BS
91
92 for (i = 0, p = 0; i < 128; i++) {
e2822b7a
BS
93 struct nvc0_fifo_chan *chan = (void *)priv->base.channel[i];
94 if (chan && chan->state == RUNNING) {
95 nv_wo32(cur, p + 0, i);
96 nv_wo32(cur, p + 4, 0x00000004);
97 p += 8;
98 }
b2b09938 99 }
ebb945a9 100 bar->flush(bar);
b2b09938 101
ebb945a9
BS
102 nv_wr32(priv, 0x002270, cur->addr >> 12);
103 nv_wr32(priv, 0x002274, 0x01f00000 | (p >> 3));
e2822b7a 104
3cf6290a
BS
105 if (wait_event_timeout(priv->runlist.wait,
106 !(nv_rd32(priv, 0x00227c) & 0x00100000),
107 msecs_to_jiffies(2000)) == 0)
108 nv_error(priv, "runlist update timeout\n");
fadb1719 109 mutex_unlock(&nv_subdev(priv)->mutex);
b2b09938 110}
4b223eef 111
c420b2dc 112static int
ebb945a9
BS
113nvc0_fifo_context_attach(struct nouveau_object *parent,
114 struct nouveau_object *object)
4b223eef 115{
ebb945a9
BS
116 struct nouveau_bar *bar = nouveau_bar(parent);
117 struct nvc0_fifo_base *base = (void *)parent->parent;
118 struct nouveau_engctx *ectx = (void *)object;
119 u32 addr;
120 int ret;
b2b09938 121
ebb945a9
BS
122 switch (nv_engidx(object->engine)) {
123 case NVDEV_ENGINE_SW : return 0;
124 case NVDEV_ENGINE_GR : addr = 0x0210; break;
125 case NVDEV_ENGINE_COPY0: addr = 0x0230; break;
126 case NVDEV_ENGINE_COPY1: addr = 0x0240; break;
23c14ed2
ML
127 case NVDEV_ENGINE_BSP : addr = 0x0270; break;
128 case NVDEV_ENGINE_VP : addr = 0x0250; break;
129 case NVDEV_ENGINE_PPP : addr = 0x0260; break;
ebb945a9
BS
130 default:
131 return -EINVAL;
132 }
b2b09938 133
ebb945a9
BS
134 if (!ectx->vma.node) {
135 ret = nouveau_gpuobj_map_vm(nv_gpuobj(ectx), base->vm,
136 NV_MEM_ACCESS_RW, &ectx->vma);
137 if (ret)
138 return ret;
4c2d4222
BS
139
140 nv_engctx(ectx)->addr = nv_gpuobj(base)->addr >> 12;
b2b09938
BS
141 }
142
ebb945a9
BS
143 nv_wo32(base, addr + 0x00, lower_32_bits(ectx->vma.offset) | 4);
144 nv_wo32(base, addr + 0x04, upper_32_bits(ectx->vma.offset));
145 bar->flush(bar);
146 return 0;
4b223eef
BS
147}
148
ebb945a9
BS
149static int
150nvc0_fifo_context_detach(struct nouveau_object *parent, bool suspend,
151 struct nouveau_object *object)
4b223eef 152{
ebb945a9
BS
153 struct nouveau_bar *bar = nouveau_bar(parent);
154 struct nvc0_fifo_priv *priv = (void *)parent->engine;
155 struct nvc0_fifo_base *base = (void *)parent->parent;
156 struct nvc0_fifo_chan *chan = (void *)parent;
157 u32 addr;
158
159 switch (nv_engidx(object->engine)) {
160 case NVDEV_ENGINE_SW : return 0;
161 case NVDEV_ENGINE_GR : addr = 0x0210; break;
162 case NVDEV_ENGINE_COPY0: addr = 0x0230; break;
163 case NVDEV_ENGINE_COPY1: addr = 0x0240; break;
23c14ed2
ML
164 case NVDEV_ENGINE_BSP : addr = 0x0270; break;
165 case NVDEV_ENGINE_VP : addr = 0x0250; break;
166 case NVDEV_ENGINE_PPP : addr = 0x0260; break;
ebb945a9
BS
167 default:
168 return -EINVAL;
b2b09938
BS
169 }
170
ebb945a9
BS
171 nv_wr32(priv, 0x002634, chan->base.chid);
172 if (!nv_wait(priv, 0x002634, 0xffffffff, chan->base.chid)) {
93260d3c
MS
173 nv_error(priv, "channel %d [%s] kick timeout\n",
174 chan->base.chid, nouveau_client_name(chan));
ebb945a9
BS
175 if (suspend)
176 return -EBUSY;
177 }
178
edc260d0
BS
179 nv_wo32(base, addr + 0x00, 0x00000000);
180 nv_wo32(base, addr + 0x04, 0x00000000);
181 bar->flush(bar);
ebb945a9 182 return 0;
b2b09938
BS
183}
184
185static int
ebb945a9
BS
186nvc0_fifo_chan_ctor(struct nouveau_object *parent,
187 struct nouveau_object *engine,
188 struct nouveau_oclass *oclass, void *data, u32 size,
189 struct nouveau_object **pobject)
4b223eef 190{
bbf8906b
BS
191 union {
192 struct nv50_channel_gpfifo_v0 v0;
193 } *args = data;
ebb945a9
BS
194 struct nouveau_bar *bar = nouveau_bar(parent);
195 struct nvc0_fifo_priv *priv = (void *)engine;
196 struct nvc0_fifo_base *base = (void *)parent;
197 struct nvc0_fifo_chan *chan;
ebb945a9
BS
198 u64 usermem, ioffset, ilength;
199 int ret, i;
b2b09938 200
bbf8906b
BS
201 nv_ioctl(parent, "create channel gpfifo size %d\n", size);
202 if (nvif_unpack(args->v0, 0, 0, false)) {
203 nv_ioctl(parent, "create channel gpfifo vers %d pushbuf %08x "
204 "ioffset %016llx ilength %08x\n",
205 args->v0.version, args->v0.pushbuf, args->v0.ioffset,
206 args->v0.ilength);
207 } else
208 return ret;
ebb945a9
BS
209
210 ret = nouveau_fifo_channel_create(parent, engine, oclass, 1,
211 priv->user.bar.offset, 0x1000,
bbf8906b 212 args->v0.pushbuf,
507ceb15
MP
213 (1ULL << NVDEV_ENGINE_SW) |
214 (1ULL << NVDEV_ENGINE_GR) |
215 (1ULL << NVDEV_ENGINE_COPY0) |
216 (1ULL << NVDEV_ENGINE_COPY1) |
217 (1ULL << NVDEV_ENGINE_BSP) |
218 (1ULL << NVDEV_ENGINE_VP) |
219 (1ULL << NVDEV_ENGINE_PPP), &chan);
ebb945a9
BS
220 *pobject = nv_object(chan);
221 if (ret)
222 return ret;
223
bbf8906b
BS
224 args->v0.chid = chan->base.chid;
225
ebb945a9
BS
226 nv_parent(chan)->context_attach = nvc0_fifo_context_attach;
227 nv_parent(chan)->context_detach = nvc0_fifo_context_detach;
228
229 usermem = chan->base.chid * 0x1000;
bbf8906b
BS
230 ioffset = args->v0.ioffset;
231 ilength = order_base_2(args->v0.ilength / 8);
ebb945a9
BS
232
233 for (i = 0; i < 0x1000; i += 4)
234 nv_wo32(priv->user.mem, usermem + i, 0x00000000);
235
236 nv_wo32(base, 0x08, lower_32_bits(priv->user.mem->addr + usermem));
237 nv_wo32(base, 0x0c, upper_32_bits(priv->user.mem->addr + usermem));
238 nv_wo32(base, 0x10, 0x0000face);
239 nv_wo32(base, 0x30, 0xfffff902);
240 nv_wo32(base, 0x48, lower_32_bits(ioffset));
241 nv_wo32(base, 0x4c, upper_32_bits(ioffset) | (ilength << 16));
242 nv_wo32(base, 0x54, 0x00000002);
243 nv_wo32(base, 0x84, 0x20400000);
244 nv_wo32(base, 0x94, 0x30000001);
245 nv_wo32(base, 0x9c, 0x00000100);
246 nv_wo32(base, 0xa4, 0x1f1f1f1f);
247 nv_wo32(base, 0xa8, 0x1f1f1f1f);
248 nv_wo32(base, 0xac, 0x0000001f);
249 nv_wo32(base, 0xb8, 0xf8000000);
250 nv_wo32(base, 0xf8, 0x10003080); /* 0x002310 */
251 nv_wo32(base, 0xfc, 0x10000010); /* 0x002350 */
252 bar->flush(bar);
253 return 0;
254}
b2b09938 255
ebb945a9
BS
256static int
257nvc0_fifo_chan_init(struct nouveau_object *object)
258{
259 struct nouveau_gpuobj *base = nv_gpuobj(object->parent);
260 struct nvc0_fifo_priv *priv = (void *)object->engine;
261 struct nvc0_fifo_chan *chan = (void *)object;
262 u32 chid = chan->base.chid;
263 int ret;
ec9c0883 264
ebb945a9
BS
265 ret = nouveau_fifo_channel_init(&chan->base);
266 if (ret)
267 return ret;
b2b09938 268
ebb945a9 269 nv_wr32(priv, 0x003000 + (chid * 8), 0xc0000000 | base->addr >> 12);
e2822b7a
BS
270
271 if (chan->state == STOPPED && (chan->state = RUNNING) == RUNNING) {
272 nv_wr32(priv, 0x003004 + (chid * 8), 0x001f0001);
273 nvc0_fifo_runlist_update(priv);
274 }
275
ebb945a9
BS
276 return 0;
277}
b2b09938 278
e99bf010
BS
279static void nvc0_fifo_intr_engine(struct nvc0_fifo_priv *priv);
280
ebb945a9
BS
281static int
282nvc0_fifo_chan_fini(struct nouveau_object *object, bool suspend)
283{
284 struct nvc0_fifo_priv *priv = (void *)object->engine;
285 struct nvc0_fifo_chan *chan = (void *)object;
286 u32 chid = chan->base.chid;
b2b09938 287
e2822b7a
BS
288 if (chan->state == RUNNING && (chan->state = STOPPED) == STOPPED) {
289 nv_mask(priv, 0x003004 + (chid * 8), 0x00000001, 0x00000000);
290 nvc0_fifo_runlist_update(priv);
291 }
e99bf010
BS
292
293 nvc0_fifo_intr_engine(priv);
294
ebb945a9 295 nv_wr32(priv, 0x003000 + (chid * 8), 0x00000000);
ebb945a9
BS
296 return nouveau_fifo_channel_fini(&chan->base, suspend);
297}
0638df42 298
ebb945a9
BS
299static struct nouveau_ofuncs
300nvc0_fifo_ofuncs = {
301 .ctor = nvc0_fifo_chan_ctor,
302 .dtor = _nouveau_fifo_channel_dtor,
303 .init = nvc0_fifo_chan_init,
304 .fini = nvc0_fifo_chan_fini,
6c6ae061 305 .map = _nouveau_fifo_channel_map,
ebb945a9
BS
306 .rd32 = _nouveau_fifo_channel_rd32,
307 .wr32 = _nouveau_fifo_channel_wr32,
867920f8 308 .ntfy = _nouveau_fifo_channel_ntfy
ebb945a9 309};
0638df42 310
ebb945a9
BS
311static struct nouveau_oclass
312nvc0_fifo_sclass[] = {
bbf8906b 313 { FERMI_CHANNEL_GPFIFO, &nvc0_fifo_ofuncs },
ebb945a9
BS
314 {}
315};
316
317/*******************************************************************************
318 * FIFO context - instmem heap and vm setup
319 ******************************************************************************/
4b223eef 320
c420b2dc 321static int
ebb945a9
BS
322nvc0_fifo_context_ctor(struct nouveau_object *parent,
323 struct nouveau_object *engine,
324 struct nouveau_oclass *oclass, void *data, u32 size,
325 struct nouveau_object **pobject)
c420b2dc 326{
ebb945a9
BS
327 struct nvc0_fifo_base *base;
328 int ret;
c420b2dc 329
ebb945a9
BS
330 ret = nouveau_fifo_context_create(parent, engine, oclass, NULL, 0x1000,
331 0x1000, NVOBJ_FLAG_ZERO_ALLOC |
332 NVOBJ_FLAG_HEAP, &base);
333 *pobject = nv_object(base);
334 if (ret)
335 return ret;
c420b2dc 336
f50c8054
BS
337 ret = nouveau_gpuobj_new(nv_object(base), NULL, 0x10000, 0x1000, 0,
338 &base->pgd);
ebb945a9
BS
339 if (ret)
340 return ret;
341
342 nv_wo32(base, 0x0200, lower_32_bits(base->pgd->addr));
343 nv_wo32(base, 0x0204, upper_32_bits(base->pgd->addr));
344 nv_wo32(base, 0x0208, 0xffffffff);
345 nv_wo32(base, 0x020c, 0x000000ff);
346
347 ret = nouveau_vm_ref(nouveau_client(parent)->vm, &base->vm, base->pgd);
348 if (ret)
349 return ret;
c420b2dc 350
c420b2dc
BS
351 return 0;
352}
353
ebb945a9
BS
354static void
355nvc0_fifo_context_dtor(struct nouveau_object *object)
356{
357 struct nvc0_fifo_base *base = (void *)object;
358 nouveau_vm_ref(NULL, &base->vm, base->pgd);
359 nouveau_gpuobj_ref(NULL, &base->pgd);
360 nouveau_fifo_context_destroy(&base->base);
361}
362
363static struct nouveau_oclass
364nvc0_fifo_cclass = {
365 .handle = NV_ENGCTX(FIFO, 0xc0),
366 .ofuncs = &(struct nouveau_ofuncs) {
367 .ctor = nvc0_fifo_context_ctor,
368 .dtor = nvc0_fifo_context_dtor,
369 .init = _nouveau_fifo_context_init,
370 .fini = _nouveau_fifo_context_fini,
371 .rd32 = _nouveau_fifo_context_rd32,
372 .wr32 = _nouveau_fifo_context_wr32,
373 },
374};
375
376/*******************************************************************************
377 * PFIFO engine
378 ******************************************************************************/
c420b2dc 379
24e8341e
BS
380static inline int
381nvc0_fifo_engidx(struct nvc0_fifo_priv *priv, u32 engn)
382{
383 switch (engn) {
384 case NVDEV_ENGINE_GR : engn = 0; break;
385 case NVDEV_ENGINE_BSP : engn = 1; break;
386 case NVDEV_ENGINE_PPP : engn = 2; break;
387 case NVDEV_ENGINE_VP : engn = 3; break;
388 case NVDEV_ENGINE_COPY0: engn = 4; break;
389 case NVDEV_ENGINE_COPY1: engn = 5; break;
390 default:
391 return -1;
392 }
393
394 return engn;
395}
396
397static inline struct nouveau_engine *
398nvc0_fifo_engine(struct nvc0_fifo_priv *priv, u32 engn)
399{
400 switch (engn) {
401 case 0: engn = NVDEV_ENGINE_GR; break;
402 case 1: engn = NVDEV_ENGINE_BSP; break;
403 case 2: engn = NVDEV_ENGINE_PPP; break;
404 case 3: engn = NVDEV_ENGINE_VP; break;
405 case 4: engn = NVDEV_ENGINE_COPY0; break;
406 case 5: engn = NVDEV_ENGINE_COPY1; break;
407 default:
408 return NULL;
409 }
410
411 return nouveau_engine(priv, engn);
412}
413
414static void
415nvc0_fifo_recover_work(struct work_struct *work)
416{
417 struct nvc0_fifo_priv *priv = container_of(work, typeof(*priv), fault);
418 struct nouveau_object *engine;
419 unsigned long flags;
420 u32 engn, engm = 0;
421 u64 mask, todo;
422
423 spin_lock_irqsave(&priv->base.lock, flags);
424 mask = priv->mask;
425 priv->mask = 0ULL;
426 spin_unlock_irqrestore(&priv->base.lock, flags);
427
428 for (todo = mask; engn = __ffs64(todo), todo; todo &= ~(1 << engn))
429 engm |= 1 << nvc0_fifo_engidx(priv, engn);
430 nv_mask(priv, 0x002630, engm, engm);
431
432 for (todo = mask; engn = __ffs64(todo), todo; todo &= ~(1 << engn)) {
433 if ((engine = (void *)nouveau_engine(priv, engn))) {
434 nv_ofuncs(engine)->fini(engine, false);
435 WARN_ON(nv_ofuncs(engine)->init(engine));
436 }
437 }
438
439 nvc0_fifo_runlist_update(priv);
440 nv_wr32(priv, 0x00262c, engm);
441 nv_mask(priv, 0x002630, engm, 0x00000000);
442}
443
444static void
445nvc0_fifo_recover(struct nvc0_fifo_priv *priv, struct nouveau_engine *engine,
446 struct nvc0_fifo_chan *chan)
447{
448 struct nouveau_object *engobj = nv_object(engine);
449 u32 chid = chan->base.chid;
450 unsigned long flags;
451
452 nv_error(priv, "%s engine fault on channel %d, recovering...\n",
453 nv_subdev(engine)->name, chid);
454
455 nv_mask(priv, 0x003004 + (chid * 0x08), 0x00000001, 0x00000000);
456 chan->state = KILLED;
457
458 spin_lock_irqsave(&priv->base.lock, flags);
459 priv->mask |= 1ULL << nv_engidx(engobj);
460 spin_unlock_irqrestore(&priv->base.lock, flags);
461 schedule_work(&priv->fault);
462}
463
083c2142
BS
464static int
465nvc0_fifo_swmthd(struct nvc0_fifo_priv *priv, u32 chid, u32 mthd, u32 data)
466{
467 struct nvc0_fifo_chan *chan = NULL;
468 struct nouveau_handle *bind;
469 unsigned long flags;
470 int ret = -EINVAL;
471
472 spin_lock_irqsave(&priv->base.lock, flags);
473 if (likely(chid >= priv->base.min && chid <= priv->base.max))
474 chan = (void *)priv->base.channel[chid];
475 if (unlikely(!chan))
476 goto out;
477
478 bind = nouveau_namedb_get_class(nv_namedb(chan), 0x906e);
479 if (likely(bind)) {
480 if (!mthd || !nv_call(bind->object, mthd, data))
481 ret = 0;
482 nouveau_namedb_put(bind);
483 }
484
485out:
486 spin_unlock_irqrestore(&priv->base.lock, flags);
487 return ret;
488}
489
40476539
BS
490static const struct nouveau_enum
491nvc0_fifo_sched_reason[] = {
492 { 0x0a, "CTXSW_TIMEOUT" },
493 {}
494};
495
61fdf620
BS
496static void
497nvc0_fifo_intr_sched_ctxsw(struct nvc0_fifo_priv *priv)
498{
499 struct nouveau_engine *engine;
500 struct nvc0_fifo_chan *chan;
501 u32 engn;
502
503 for (engn = 0; engn < 6; engn++) {
504 u32 stat = nv_rd32(priv, 0x002640 + (engn * 0x04));
505 u32 busy = (stat & 0x80000000);
506 u32 save = (stat & 0x00100000); /* maybe? */
507 u32 unk0 = (stat & 0x00040000);
508 u32 unk1 = (stat & 0x00001000);
509 u32 chid = (stat & 0x0000007f);
510 (void)save;
511
512 if (busy && unk0 && unk1) {
513 if (!(chan = (void *)priv->base.channel[chid]))
514 continue;
515 if (!(engine = nvc0_fifo_engine(priv, engn)))
516 continue;
517 nvc0_fifo_recover(priv, engine, chan);
518 }
519 }
520}
521
40476539
BS
522static void
523nvc0_fifo_intr_sched(struct nvc0_fifo_priv *priv)
524{
525 u32 intr = nv_rd32(priv, 0x00254c);
526 u32 code = intr & 0x000000ff;
527 const struct nouveau_enum *en;
528 char enunk[6] = "";
529
530 en = nouveau_enum_find(nvc0_fifo_sched_reason, code);
531 if (!en)
532 snprintf(enunk, sizeof(enunk), "UNK%02x", code);
533
534 nv_error(priv, "SCHED_ERROR [ %s ]\n", en ? en->name : enunk);
61fdf620
BS
535
536 switch (code) {
537 case 0x0a:
538 nvc0_fifo_intr_sched_ctxsw(priv);
539 break;
540 default:
541 break;
542 }
40476539
BS
543}
544
d439a5ac
BS
545static const struct nouveau_enum
546nvc0_fifo_fault_engine[] = {
93260d3c 547 { 0x00, "PGRAPH", NULL, NVDEV_ENGINE_GR },
33f8c6d0
BS
548 { 0x03, "PEEPHOLE", NULL, NVDEV_ENGINE_IFB },
549 { 0x04, "BAR1", NULL, NVDEV_SUBDEV_BAR },
550 { 0x05, "BAR3", NULL, NVDEV_SUBDEV_INSTMEM },
93260d3c
MS
551 { 0x07, "PFIFO", NULL, NVDEV_ENGINE_FIFO },
552 { 0x10, "PBSP", NULL, NVDEV_ENGINE_BSP },
553 { 0x11, "PPPP", NULL, NVDEV_ENGINE_PPP },
7a313473 554 { 0x13, "PCOUNTER" },
93260d3c
MS
555 { 0x14, "PVP", NULL, NVDEV_ENGINE_VP },
556 { 0x15, "PCOPY0", NULL, NVDEV_ENGINE_COPY0 },
557 { 0x16, "PCOPY1", NULL, NVDEV_ENGINE_COPY1 },
7a313473 558 { 0x17, "PDAEMON" },
b2b09938
BS
559 {}
560};
561
d439a5ac
BS
562static const struct nouveau_enum
563nvc0_fifo_fault_reason[] = {
e2966632
BS
564 { 0x00, "PT_NOT_PRESENT" },
565 { 0x01, "PT_TOO_SHORT" },
566 { 0x02, "PAGE_NOT_PRESENT" },
567 { 0x03, "VM_LIMIT_EXCEEDED" },
568 { 0x04, "NO_CHANNEL" },
569 { 0x05, "PAGE_SYSTEM_ONLY" },
570 { 0x06, "PAGE_READ_ONLY" },
571 { 0x0a, "COMPRESSED_SYSRAM" },
572 { 0x0c, "INVALID_STORAGE_TYPE" },
b2b09938
BS
573 {}
574};
575
d439a5ac
BS
576static const struct nouveau_enum
577nvc0_fifo_fault_hubclient[] = {
7795bee0
BS
578 { 0x01, "PCOPY0" },
579 { 0x02, "PCOPY1" },
580 { 0x04, "DISPATCH" },
581 { 0x05, "CTXCTL" },
582 { 0x06, "PFIFO" },
583 { 0x07, "BAR_READ" },
584 { 0x08, "BAR_WRITE" },
585 { 0x0b, "PVP" },
586 { 0x0c, "PPPP" },
587 { 0x0d, "PBSP" },
588 { 0x11, "PCOUNTER" },
589 { 0x12, "PDAEMON" },
590 { 0x14, "CCACHE" },
591 { 0x15, "CCACHE_POST" },
592 {}
593};
594
d439a5ac
BS
595static const struct nouveau_enum
596nvc0_fifo_fault_gpcclient[] = {
7795bee0
BS
597 { 0x01, "TEX" },
598 { 0x0c, "ESETUP" },
599 { 0x0e, "CTXCTL" },
600 { 0x0f, "PROP" },
601 {}
602};
603
b2b09938 604static void
d439a5ac 605nvc0_fifo_intr_fault(struct nvc0_fifo_priv *priv, int unit)
b2b09938 606{
b3ccd34d
BS
607 u32 inst = nv_rd32(priv, 0x002800 + (unit * 0x10));
608 u32 valo = nv_rd32(priv, 0x002804 + (unit * 0x10));
609 u32 vahi = nv_rd32(priv, 0x002808 + (unit * 0x10));
610 u32 stat = nv_rd32(priv, 0x00280c + (unit * 0x10));
d439a5ac 611 u32 gpc = (stat & 0x1f000000) >> 24;
7795bee0 612 u32 client = (stat & 0x00001f00) >> 8;
d439a5ac
BS
613 u32 write = (stat & 0x00000080);
614 u32 hub = (stat & 0x00000040);
615 u32 reason = (stat & 0x0000000f);
24e8341e
BS
616 struct nouveau_object *engctx = NULL, *object;
617 struct nouveau_engine *engine = NULL;
d439a5ac
BS
618 const struct nouveau_enum *er, *eu, *ec;
619 char erunk[6] = "";
620 char euunk[6] = "";
621 char ecunk[6] = "";
622 char gpcid[3] = "";
b2b09938 623
d439a5ac
BS
624 er = nouveau_enum_find(nvc0_fifo_fault_reason, reason);
625 if (!er)
626 snprintf(erunk, sizeof(erunk), "UNK%02X", reason);
627
628 eu = nouveau_enum_find(nvc0_fifo_fault_engine, unit);
629 if (eu) {
33f8c6d0
BS
630 switch (eu->data2) {
631 case NVDEV_SUBDEV_BAR:
632 nv_mask(priv, 0x001704, 0x00000000, 0x00000000);
633 break;
634 case NVDEV_SUBDEV_INSTMEM:
635 nv_mask(priv, 0x001714, 0x00000000, 0x00000000);
636 break;
637 case NVDEV_ENGINE_IFB:
638 nv_mask(priv, 0x001718, 0x00000000, 0x00000000);
639 break;
640 default:
d439a5ac
BS
641 engine = nouveau_engine(priv, eu->data2);
642 if (engine)
643 engctx = nouveau_engctx_get(engine, inst);
33f8c6d0 644 break;
d439a5ac 645 }
7795bee0 646 } else {
d439a5ac 647 snprintf(euunk, sizeof(euunk), "UNK%02x", unit);
7795bee0 648 }
93260d3c 649
d439a5ac
BS
650 if (hub) {
651 ec = nouveau_enum_find(nvc0_fifo_fault_hubclient, client);
652 } else {
653 ec = nouveau_enum_find(nvc0_fifo_fault_gpcclient, client);
654 snprintf(gpcid, sizeof(gpcid), "%d", gpc);
93260d3c 655 }
d439a5ac
BS
656
657 if (!ec)
658 snprintf(ecunk, sizeof(ecunk), "UNK%02x", client);
659
660 nv_error(priv, "%s fault at 0x%010llx [%s] from %s/%s%s%s%s on "
661 "channel 0x%010llx [%s]\n", write ? "write" : "read",
662 (u64)vahi << 32 | valo, er ? er->name : erunk,
663 eu ? eu->name : euunk, hub ? "" : "GPC", gpcid, hub ? "" : "/",
664 ec ? ec->name : ecunk, (u64)inst << 12,
665 nouveau_client_name(engctx));
93260d3c 666
24e8341e
BS
667 object = engctx;
668 while (object) {
669 switch (nv_mclass(object)) {
bbf8906b 670 case FERMI_CHANNEL_GPFIFO:
24e8341e
BS
671 nvc0_fifo_recover(priv, engine, (void *)object);
672 break;
673 }
674 object = object->parent;
675 }
676
93260d3c 677 nouveau_engctx_put(engctx);
b2b09938
BS
678}
679
083c2142
BS
680static const struct nouveau_bitfield
681nvc0_fifo_pbdma_intr[] = {
682/* { 0x00008000, "" } seen with null ib push */
683 { 0x00200000, "ILLEGAL_MTHD" },
684 { 0x00800000, "EMPTY_SUBC" },
685 {}
686};
d5316e25 687
b2b09938 688static void
083c2142 689nvc0_fifo_intr_pbdma(struct nvc0_fifo_priv *priv, int unit)
b2b09938 690{
ebb945a9
BS
691 u32 stat = nv_rd32(priv, 0x040108 + (unit * 0x2000));
692 u32 addr = nv_rd32(priv, 0x0400c0 + (unit * 0x2000));
693 u32 data = nv_rd32(priv, 0x0400c4 + (unit * 0x2000));
694 u32 chid = nv_rd32(priv, 0x040120 + (unit * 0x2000)) & 0x7f;
695 u32 subc = (addr & 0x00070000) >> 16;
b2b09938 696 u32 mthd = (addr & 0x00003ffc);
d5316e25 697 u32 show = stat;
b2b09938 698
ebb945a9
BS
699 if (stat & 0x00800000) {
700 if (!nvc0_fifo_swmthd(priv, chid, mthd, data))
701 show &= ~0x00800000;
702 }
703
d5316e25 704 if (show) {
0357466d
BS
705 nv_error(priv, "PBDMA%d:", unit);
706 nouveau_bitfield_print(nvc0_fifo_pbdma_intr, show);
f533da10 707 pr_cont("\n");
93260d3c 708 nv_error(priv,
0357466d 709 "PBDMA%d: ch %d [%s] subc %d mthd 0x%04x data 0x%08x\n",
93260d3c
MS
710 unit, chid,
711 nouveau_client_name_for_fifo_chid(&priv->base, chid),
712 subc, mthd, data);
d5316e25 713 }
b2b09938 714
ebb945a9
BS
715 nv_wr32(priv, 0x0400c0 + (unit * 0x2000), 0x80600008);
716 nv_wr32(priv, 0x040108 + (unit * 0x2000), stat);
b2b09938
BS
717}
718
a07d0e76
BS
719static void
720nvc0_fifo_intr_runlist(struct nvc0_fifo_priv *priv)
721{
722 u32 intr = nv_rd32(priv, 0x002a00);
723
724 if (intr & 0x10000000) {
725 wake_up(&priv->runlist.wait);
726 nv_wr32(priv, 0x002a00, 0x10000000);
727 intr &= ~0x10000000;
728 }
729
730 if (intr) {
731 nv_error(priv, "RUNLIST 0x%08x\n", intr);
732 nv_wr32(priv, 0x002a00, intr);
733 }
734}
735
e99bf010
BS
736static void
737nvc0_fifo_intr_engine_unit(struct nvc0_fifo_priv *priv, int engn)
738{
739 u32 intr = nv_rd32(priv, 0x0025a8 + (engn * 0x04));
740 u32 inte = nv_rd32(priv, 0x002628);
741 u32 unkn;
742
19a10828
BS
743 nv_wr32(priv, 0x0025a8 + (engn * 0x04), intr);
744
e99bf010
BS
745 for (unkn = 0; unkn < 8; unkn++) {
746 u32 ints = (intr >> (unkn * 0x04)) & inte;
747 if (ints & 0x1) {
867920f8 748 nouveau_fifo_uevent(&priv->base);
e99bf010
BS
749 ints &= ~1;
750 }
751 if (ints) {
752 nv_error(priv, "ENGINE %d %d %01x", engn, unkn, ints);
753 nv_mask(priv, 0x002628, ints, 0);
754 }
755 }
e99bf010
BS
756}
757
758static void
759nvc0_fifo_intr_engine(struct nvc0_fifo_priv *priv)
760{
761 u32 mask = nv_rd32(priv, 0x0025a4);
762 while (mask) {
763 u32 unit = __ffs(mask);
764 nvc0_fifo_intr_engine_unit(priv, unit);
765 mask &= ~(1 << unit);
766 }
767}
768
b2b09938 769static void
ebb945a9 770nvc0_fifo_intr(struct nouveau_subdev *subdev)
b2b09938 771{
ebb945a9
BS
772 struct nvc0_fifo_priv *priv = (void *)subdev;
773 u32 mask = nv_rd32(priv, 0x002140);
774 u32 stat = nv_rd32(priv, 0x002100) & mask;
b2b09938 775
32256c87
BS
776 if (stat & 0x00000001) {
777 u32 intr = nv_rd32(priv, 0x00252c);
778 nv_warn(priv, "INTR 0x00000001: 0x%08x\n", intr);
779 nv_wr32(priv, 0x002100, 0x00000001);
780 stat &= ~0x00000001;
781 }
782
cc8cd647 783 if (stat & 0x00000100) {
40476539 784 nvc0_fifo_intr_sched(priv);
ebb945a9 785 nv_wr32(priv, 0x002100, 0x00000100);
cc8cd647
BS
786 stat &= ~0x00000100;
787 }
788
32256c87
BS
789 if (stat & 0x00010000) {
790 u32 intr = nv_rd32(priv, 0x00256c);
791 nv_warn(priv, "INTR 0x00010000: 0x%08x\n", intr);
792 nv_wr32(priv, 0x002100, 0x00010000);
793 stat &= ~0x00010000;
794 }
795
796 if (stat & 0x01000000) {
797 u32 intr = nv_rd32(priv, 0x00258c);
798 nv_warn(priv, "INTR 0x01000000: 0x%08x\n", intr);
799 nv_wr32(priv, 0x002100, 0x01000000);
800 stat &= ~0x01000000;
801 }
802
b2b09938 803 if (stat & 0x10000000) {
d439a5ac
BS
804 u32 mask = nv_rd32(priv, 0x00259c);
805 while (mask) {
806 u32 unit = __ffs(mask);
807 nvc0_fifo_intr_fault(priv, unit);
808 nv_wr32(priv, 0x00259c, (1 << unit));
809 mask &= ~(1 << unit);
b2b09938 810 }
b2b09938
BS
811 stat &= ~0x10000000;
812 }
813
814 if (stat & 0x20000000) {
083c2142
BS
815 u32 mask = nv_rd32(priv, 0x0025a0);
816 while (mask) {
817 u32 unit = __ffs(mask);
818 nvc0_fifo_intr_pbdma(priv, unit);
819 nv_wr32(priv, 0x0025a0, (1 << unit));
820 mask &= ~(1 << unit);
b2b09938 821 }
b2b09938
BS
822 stat &= ~0x20000000;
823 }
824
cc8cd647 825 if (stat & 0x40000000) {
a07d0e76 826 nvc0_fifo_intr_runlist(priv);
cc8cd647
BS
827 stat &= ~0x40000000;
828 }
829
32256c87 830 if (stat & 0x80000000) {
e99bf010 831 nvc0_fifo_intr_engine(priv);
32256c87
BS
832 stat &= ~0x80000000;
833 }
834
b2b09938 835 if (stat) {
22a7a27b
BS
836 nv_error(priv, "INTR 0x%08x\n", stat);
837 nv_mask(priv, 0x002140, stat, 0x00000000);
ebb945a9 838 nv_wr32(priv, 0x002100, stat);
b2b09938 839 }
b2b09938 840}
c420b2dc 841
9bd2ddba 842static void
79ca2770 843nvc0_fifo_uevent_init(struct nvkm_event *event, int type, int index)
9bd2ddba 844{
79ca2770
BS
845 struct nouveau_fifo *fifo = container_of(event, typeof(*fifo), uevent);
846 nv_mask(fifo, 0x002140, 0x80000000, 0x80000000);
9bd2ddba
BS
847}
848
849static void
79ca2770 850nvc0_fifo_uevent_fini(struct nvkm_event *event, int type, int index)
9bd2ddba 851{
79ca2770
BS
852 struct nouveau_fifo *fifo = container_of(event, typeof(*fifo), uevent);
853 nv_mask(fifo, 0x002140, 0x80000000, 0x00000000);
9bd2ddba
BS
854}
855
79ca2770
BS
856static const struct nvkm_event_func
857nvc0_fifo_uevent_func = {
867920f8 858 .ctor = nouveau_fifo_uevent_ctor,
79ca2770
BS
859 .init = nvc0_fifo_uevent_init,
860 .fini = nvc0_fifo_uevent_fini,
861};
862
ebb945a9
BS
863static int
864nvc0_fifo_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
865 struct nouveau_oclass *oclass, void *data, u32 size,
866 struct nouveau_object **pobject)
867{
868 struct nvc0_fifo_priv *priv;
869 int ret;
870
871 ret = nouveau_fifo_create(parent, engine, oclass, 0, 127, &priv);
872 *pobject = nv_object(priv);
873 if (ret)
874 return ret;
875
24e8341e
BS
876 INIT_WORK(&priv->fault, nvc0_fifo_recover_work);
877
f50c8054 878 ret = nouveau_gpuobj_new(nv_object(priv), NULL, 0x1000, 0x1000, 0,
a07d0e76 879 &priv->runlist.mem[0]);
ebb945a9
BS
880 if (ret)
881 return ret;
882
f50c8054 883 ret = nouveau_gpuobj_new(nv_object(priv), NULL, 0x1000, 0x1000, 0,
a07d0e76 884 &priv->runlist.mem[1]);
ebb945a9
BS
885 if (ret)
886 return ret;
887
a07d0e76
BS
888 init_waitqueue_head(&priv->runlist.wait);
889
f50c8054 890 ret = nouveau_gpuobj_new(nv_object(priv), NULL, 128 * 0x1000, 0x1000, 0,
ebb945a9
BS
891 &priv->user.mem);
892 if (ret)
893 return ret;
894
895 ret = nouveau_gpuobj_map(priv->user.mem, NV_MEM_ACCESS_RW,
896 &priv->user.bar);
897 if (ret)
898 return ret;
899
79ca2770
BS
900 ret = nvkm_event_init(&nvc0_fifo_uevent_func, 1, 1, &priv->base.uevent);
901 if (ret)
902 return ret;
9bd2ddba 903
ebb945a9
BS
904 nv_subdev(priv)->unit = 0x00000100;
905 nv_subdev(priv)->intr = nvc0_fifo_intr;
906 nv_engine(priv)->cclass = &nvc0_fifo_cclass;
907 nv_engine(priv)->sclass = nvc0_fifo_sclass;
908 return 0;
909}
910
c420b2dc 911static void
ebb945a9 912nvc0_fifo_dtor(struct nouveau_object *object)
c420b2dc 913{
ebb945a9 914 struct nvc0_fifo_priv *priv = (void *)object;
c420b2dc 915
18c9b959 916 nouveau_gpuobj_unmap(&priv->user.bar);
9da226f6 917 nouveau_gpuobj_ref(NULL, &priv->user.mem);
a07d0e76
BS
918 nouveau_gpuobj_ref(NULL, &priv->runlist.mem[0]);
919 nouveau_gpuobj_ref(NULL, &priv->runlist.mem[1]);
c420b2dc 920
ebb945a9 921 nouveau_fifo_destroy(&priv->base);
c420b2dc
BS
922}
923
ebb945a9
BS
924static int
925nvc0_fifo_init(struct nouveau_object *object)
c420b2dc 926{
ebb945a9
BS
927 struct nvc0_fifo_priv *priv = (void *)object;
928 int ret, i;
c420b2dc 929
ebb945a9
BS
930 ret = nouveau_fifo_init(&priv->base);
931 if (ret)
932 return ret;
c420b2dc 933
ebb945a9
BS
934 nv_wr32(priv, 0x000204, 0xffffffff);
935 nv_wr32(priv, 0x002204, 0xffffffff);
c420b2dc 936
ebb945a9 937 priv->spoon_nr = hweight32(nv_rd32(priv, 0x002204));
0357466d 938 nv_debug(priv, "%d PBDMA unit(s)\n", priv->spoon_nr);
c420b2dc 939
0357466d 940 /* assign engines to PBDMAs */
ebb945a9
BS
941 if (priv->spoon_nr >= 3) {
942 nv_wr32(priv, 0x002208, ~(1 << 0)); /* PGRAPH */
943 nv_wr32(priv, 0x00220c, ~(1 << 1)); /* PVP */
944 nv_wr32(priv, 0x002210, ~(1 << 1)); /* PPP */
945 nv_wr32(priv, 0x002214, ~(1 << 1)); /* PBSP */
946 nv_wr32(priv, 0x002218, ~(1 << 2)); /* PCE0 */
947 nv_wr32(priv, 0x00221c, ~(1 << 1)); /* PCE1 */
948 }
c420b2dc 949
0357466d 950 /* PBDMA[n] */
ebb945a9
BS
951 for (i = 0; i < priv->spoon_nr; i++) {
952 nv_mask(priv, 0x04013c + (i * 0x2000), 0x10000100, 0x00000000);
953 nv_wr32(priv, 0x040108 + (i * 0x2000), 0xffffffff); /* INTR */
954 nv_wr32(priv, 0x04010c + (i * 0x2000), 0xfffffeff); /* INTREN */
955 }
c420b2dc 956
ebb945a9
BS
957 nv_mask(priv, 0x002200, 0x00000001, 0x00000001);
958 nv_wr32(priv, 0x002254, 0x10000000 | priv->user.bar.offset >> 12);
9da226f6 959
ebb945a9 960 nv_wr32(priv, 0x002100, 0xffffffff);
a07d0e76 961 nv_wr32(priv, 0x002140, 0x7fffffff);
e99bf010 962 nv_wr32(priv, 0x002628, 0x00000001); /* ENGINE_INTR_EN */
ebb945a9 963 return 0;
c420b2dc 964}
ebb945a9 965
16c4f227
BS
966struct nouveau_oclass *
967nvc0_fifo_oclass = &(struct nouveau_oclass) {
ebb945a9
BS
968 .handle = NV_ENGINE(FIFO, 0xc0),
969 .ofuncs = &(struct nouveau_ofuncs) {
970 .ctor = nvc0_fifo_ctor,
971 .dtor = nvc0_fifo_dtor,
972 .init = nvc0_fifo_init,
973 .fini = _nouveau_fifo_fini,
974 },
975};