summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/nouveau/nvkm/engine/fifo/runl.c
blob: 454a481a0aef3edb5a5b4656683abde466eb77be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * Copyright 2021 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
#include "runl.h"
#include "cgrp.h"
#include "chan.h"
#include "chid.h"
#include "priv.h"
#include "runq.h"

#include <core/gpuobj.h>
#include <subdev/timer.h>
#include <subdev/top.h>

static struct nvkm_cgrp *
nvkm_engn_cgrp_get(struct nvkm_engn *engn, unsigned long *pirqflags)
{
	struct nvkm_cgrp *cgrp = NULL;
	struct nvkm_chan *chan;
	bool cgid;
	int id;

	id = engn->func->cxid(engn, &cgid);
	if (id < 0)
		return NULL;

	if (!cgid) {
		chan = nvkm_runl_chan_get_chid(engn->runl, id, pirqflags);
		if (chan)
			cgrp = chan->cgrp;
	} else {
		cgrp = nvkm_runl_cgrp_get_cgid(engn->runl, id, pirqflags);
	}

	WARN_ON(!cgrp);
	return cgrp;
}

static void
nvkm_runl_rc(struct nvkm_runl *runl)
{
	struct nvkm_fifo *fifo = runl->fifo;
	struct nvkm_cgrp *cgrp, *gtmp;
	struct nvkm_chan *chan, *ctmp;
	struct nvkm_engn *engn;
	unsigned long flags;
	int rc, state, i;
	bool reset;

	/* Runlist is blocked before scheduling recovery - fetch count. */
	BUG_ON(!mutex_is_locked(&runl->mutex));
	rc = atomic_xchg(&runl->rc_pending, 0);
	if (!rc)
		return;

	/* Look for channel groups flagged for RC. */
	nvkm_runl_foreach_cgrp_safe(cgrp, gtmp, runl) {
		state = atomic_cmpxchg(&cgrp->rc, NVKM_CGRP_RC_PENDING, NVKM_CGRP_RC_RUNNING);
		if (state == NVKM_CGRP_RC_PENDING) {
			/* Disable all channels in them, and remove from runlist. */
			nvkm_cgrp_foreach_chan_safe(chan, ctmp, cgrp) {
				nvkm_chan_error(chan, false);
				nvkm_chan_remove_locked(chan);
			}
		}
	}

	/* On GPUs with runlist preempt, wait for PBDMA(s) servicing runlist to go idle. */
	if (runl->func->preempt) {
		for (i = 0; i < runl->runq_nr; i++) {
			struct nvkm_runq *runq = runl->runq[i];

			if (runq) {
				nvkm_msec(fifo->engine.subdev.device, 2000,
					if (runq->func->idle(runq))
						break;
				);
			}
		}
	}

	/* Look for engines that are still on flagged channel groups - reset them. */
	nvkm_runl_foreach_engn_cond(engn, runl, engn->func->cxid) {
		cgrp = nvkm_engn_cgrp_get(engn, &flags);
		if (!cgrp) {
			ENGN_DEBUG(engn, "cxid not valid");
			continue;
		}

		reset = atomic_read(&cgrp->rc) == NVKM_CGRP_RC_RUNNING;
		nvkm_cgrp_put(&cgrp, flags);
		if (!reset) {
			ENGN_DEBUG(engn, "cxid not in recovery");
			continue;
		}

		ENGN_DEBUG(engn, "resetting...");
		/*TODO: can we do something less of a potential catastrophe on failure? */
		WARN_ON(nvkm_engine_reset(engn->engine));
	}

	/* Submit runlist update, and clear any remaining exception state. */
	runl->func->update(runl);
	if (runl->func->fault_clear)
		runl->func->fault_clear(runl);

	/* Unblock runlist processing. */
	while (rc--)
		nvkm_runl_allow(runl);
	runl->func->wait(runl);
}

static void
nvkm_runl_rc_runl(struct nvkm_runl *runl)
{
	RUNL_ERROR(runl, "rc scheduled");

	nvkm_runl_block(runl);
	if (runl->func->preempt)
		runl->func->preempt(runl);

	atomic_inc(&runl->rc_pending);
	schedule_work(&runl->work);
}

void
nvkm_runl_rc_cgrp(struct nvkm_cgrp *cgrp)
{
	if (atomic_cmpxchg(&cgrp->rc, NVKM_CGRP_RC_NONE, NVKM_CGRP_RC_PENDING) != NVKM_CGRP_RC_NONE)
		return;

	CGRP_ERROR(cgrp, "rc scheduled");
	nvkm_runl_rc_runl(cgrp->runl);
}

void
nvkm_runl_rc_engn(struct nvkm_runl *runl, struct nvkm_engn *engn)
{
	struct nvkm_cgrp *cgrp;
	unsigned long flags;

	/* Lookup channel group currently on engine. */
	cgrp = nvkm_engn_cgrp_get(engn, &flags);
	if (!cgrp) {
		ENGN_DEBUG(engn, "rc skipped, not on channel");
		return;
	}

	nvkm_runl_rc_cgrp(cgrp);
	nvkm_cgrp_put(&cgrp, flags);
}

static void
nvkm_runl_work(struct work_struct *work)
{
	struct nvkm_runl *runl = container_of(work, typeof(*runl), work);

	mutex_lock(&runl->mutex);
	nvkm_runl_rc(runl);
	mutex_unlock(&runl->mutex);

}

struct nvkm_chan *
nvkm_runl_chan_get_inst(struct nvkm_runl *runl, u64 inst, unsigned long *pirqflags)
{
	struct nvkm_chid *chid = runl->chid;
	struct nvkm_chan *chan;
	unsigned long flags;
	int id;

	spin_lock_irqsave(&chid->lock, flags);
	for_each_set_bit(id, chid->used, chid->nr) {
		chan = chid->data[id];
		if (likely(chan)) {
			if (chan->inst->addr == inst) {
				spin_lock(&chan->cgrp->lock);
				*pirqflags = flags;
				spin_unlock(&chid->lock);
				return chan;
			}
		}
	}
	spin_unlock_irqrestore(&chid->lock, flags);
	return NULL;
}

struct nvkm_chan *
nvkm_runl_chan_get_chid(struct nvkm_runl *runl, int id, unsigned long *pirqflags)
{
	struct nvkm_chid *chid = runl->chid;
	struct nvkm_chan *chan;
	unsigned long flags;

	spin_lock_irqsave(&chid->lock, flags);
	if (!WARN_ON(id >= chid->nr)) {
		chan = chid->data[id];
		if (likely(chan)) {
			spin_lock(&chan->cgrp->lock);
			*pirqflags = flags;
			spin_unlock(&chid->lock);
			return chan;
		}
	}
	spin_unlock_irqrestore(&chid->lock, flags);
	return NULL;
}

struct nvkm_cgrp *
nvkm_runl_cgrp_get_cgid(struct nvkm_runl *runl, int id, unsigned long *pirqflags)
{
	struct nvkm_chid *cgid = runl->cgid;
	struct nvkm_cgrp *cgrp;
	unsigned long flags;

	spin_lock_irqsave(&cgid->lock, flags);
	if (!WARN_ON(id >= cgid->nr)) {
		cgrp = cgid->data[id];
		if (likely(cgrp)) {
			spin_lock(&cgrp->lock);
			*pirqflags = flags;
			spin_unlock(&cgid->lock);
			return cgrp;
		}
	}
	spin_unlock_irqrestore(&cgid->lock, flags);
	return NULL;
}

int
nvkm_runl_preempt_wait(struct nvkm_runl *runl)
{
	return nvkm_msec(runl->fifo->engine.subdev.device, runl->fifo->timeout.chan_msec,
		if (!runl->func->preempt_pending(runl))
			break;

		nvkm_runl_rc(runl);
		usleep_range(1, 2);
	) < 0 ? -ETIMEDOUT : 0;
}

bool
nvkm_runl_update_pending(struct nvkm_runl *runl)
{
	if (!runl->func->pending(runl))
		return false;

	nvkm_runl_rc(runl);
	return true;
}

void
nvkm_runl_update_locked(struct nvkm_runl *runl, bool wait)
{
	if (atomic_xchg(&runl->changed, 0) && runl->func->update) {
		runl->func->update(runl);
		if (wait)
			runl->func->wait(runl);
	}
}

void
nvkm_runl_allow(struct nvkm_runl *runl)
{
	struct nvkm_fifo *fifo = runl->fifo;
	unsigned long flags;

	spin_lock_irqsave(&fifo->lock, flags);
	if (!--runl->blocked) {
		RUNL_TRACE(runl, "running");
		runl->func->allow(runl, ~0);
	}
	spin_unlock_irqrestore(&fifo->lock, flags);
}

void
nvkm_runl_block(struct nvkm_runl *runl)
{
	struct nvkm_fifo *fifo = runl->fifo;
	unsigned long flags;

	spin_lock_irqsave(&fifo->lock, flags);
	if (!runl->blocked++) {
		RUNL_TRACE(runl, "stopped");
		runl->func->block(runl, ~0);
	}
	spin_unlock_irqrestore(&fifo->lock, flags);
}

void
nvkm_runl_fini(struct nvkm_runl *runl)
{
	if (runl->func->fini)
		runl->func->fini(runl);

	flush_work(&runl->work);
}

void
nvkm_runl_del(struct nvkm_runl *runl)
{
	struct nvkm_engn *engn, *engt;

	nvkm_memory_unref(&runl->mem);

	list_for_each_entry_safe(engn, engt, &runl->engns, head) {
		list_del(&engn->head);
		kfree(engn);
	}

	nvkm_chid_unref(&runl->chid);
	nvkm_chid_unref(&runl->cgid);

	list_del(&runl->head);
	mutex_destroy(&runl->mutex);
	kfree(runl);
}

struct nvkm_engn *
nvkm_runl_add(struct nvkm_runl *runl, int engi, const struct nvkm_engn_func *func,
	      enum nvkm_subdev_type type, int inst)
{
	struct nvkm_fifo *fifo = runl->fifo;
	struct nvkm_device *device = fifo->engine.subdev.device;
	struct nvkm_engine *engine;
	struct nvkm_engn *engn;

	engine = nvkm_device_engine(device, type, inst);
	if (!engine) {
		RUNL_DEBUG(runl, "engn %d.%d[%s] not found", engi, inst, nvkm_subdev_type[type]);
		return NULL;
	}

	if (!(engn = kzalloc(sizeof(*engn), GFP_KERNEL)))
		return NULL;

	engn->func = func;
	engn->runl = runl;
	engn->id = engi;
	engn->engine = engine;
	engn->fault = -1;
	list_add_tail(&engn->head, &runl->engns);

	/* Lookup MMU engine ID for fault handling. */
	if (device->top)
		engn->fault = nvkm_top_fault_id(device, engine->subdev.type, engine->subdev.inst);

	if (engn->fault < 0 && fifo->func->mmu_fault) {
		const struct nvkm_enum *map = fifo->func->mmu_fault->engine;

		while (map->name) {
			if (map->data2 == engine->subdev.type && map->inst == engine->subdev.inst) {
				engn->fault = map->value;
				break;
			}
			map++;
		}
	}

	return engn;
}

struct nvkm_runl *
nvkm_runl_get(struct nvkm_fifo *fifo, int runi, u32 addr)
{
	struct nvkm_runl *runl;

	nvkm_runl_foreach(runl, fifo) {
		if ((runi >= 0 && runl->id == runi) || (runi < 0 && runl->addr == addr))
			return runl;
	}

	return NULL;
}

struct nvkm_runl *
nvkm_runl_new(struct nvkm_fifo *fifo, int runi, u32 addr, int id_nr)
{
	struct nvkm_subdev *subdev = &fifo->engine.subdev;
	struct nvkm_runl *runl;
	int ret;

	if (!(runl = kzalloc(sizeof(*runl), GFP_KERNEL)))
		return ERR_PTR(-ENOMEM);

	runl->func = fifo->func->runl;
	runl->fifo = fifo;
	runl->id = runi;
	runl->addr = addr;
	INIT_LIST_HEAD(&runl->engns);
	INIT_LIST_HEAD(&runl->cgrps);
	atomic_set(&runl->changed, 0);
	mutex_init(&runl->mutex);
	INIT_WORK(&runl->work, nvkm_runl_work);
	atomic_set(&runl->rc_triggered, 0);
	atomic_set(&runl->rc_pending, 0);
	list_add_tail(&runl->head, &fifo->runls);

	if (!fifo->chid) {
		if ((ret = nvkm_chid_new(&nvkm_chan_event, subdev, id_nr, 0, id_nr, &runl->cgid)) ||
		    (ret = nvkm_chid_new(&nvkm_chan_event, subdev, id_nr, 0, id_nr, &runl->chid))) {
			RUNL_ERROR(runl, "cgid/chid: %d", ret);
			nvkm_runl_del(runl);
			return ERR_PTR(ret);
		}
	} else {
		runl->cgid = nvkm_chid_ref(fifo->cgid);
		runl->chid = nvkm_chid_ref(fifo->chid);
	}

	return runl;
}