summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/xe/xe_sched_job.c
blob: eeccc1c318aef12b7efe17bc63b1e33d13fb6abe (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
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2021 Intel Corporation
 */

#include "xe_sched_job.h"

#include <uapi/drm/xe_drm.h>
#include <linux/dma-fence-chain.h>
#include <linux/slab.h>

#include "xe_device.h"
#include "xe_exec_queue.h"
#include "xe_gt.h"
#include "xe_hw_engine_types.h"
#include "xe_hw_fence.h"
#include "xe_lrc.h"
#include "xe_macros.h"
#include "xe_pm.h"
#include "xe_sync_types.h"
#include "xe_trace.h"
#include "xe_vm.h"

static struct kmem_cache *xe_sched_job_slab;
static struct kmem_cache *xe_sched_job_parallel_slab;

int __init xe_sched_job_module_init(void)
{
	xe_sched_job_slab =
		kmem_cache_create("xe_sched_job",
				  sizeof(struct xe_sched_job) +
				  sizeof(struct xe_job_ptrs), 0,
				  SLAB_HWCACHE_ALIGN, NULL);
	if (!xe_sched_job_slab)
		return -ENOMEM;

	xe_sched_job_parallel_slab =
		kmem_cache_create("xe_sched_job_parallel",
				  sizeof(struct xe_sched_job) +
				  sizeof(struct xe_job_ptrs) *
				  XE_HW_ENGINE_MAX_INSTANCE, 0,
				  SLAB_HWCACHE_ALIGN, NULL);
	if (!xe_sched_job_parallel_slab) {
		kmem_cache_destroy(xe_sched_job_slab);
		return -ENOMEM;
	}

	return 0;
}

void xe_sched_job_module_exit(void)
{
	kmem_cache_destroy(xe_sched_job_slab);
	kmem_cache_destroy(xe_sched_job_parallel_slab);
}

static struct xe_sched_job *job_alloc(bool parallel)
{
	return kmem_cache_zalloc(parallel ? xe_sched_job_parallel_slab :
				 xe_sched_job_slab, GFP_KERNEL);
}

bool xe_sched_job_is_migration(struct xe_exec_queue *q)
{
	return q->vm && (q->vm->flags & XE_VM_FLAG_MIGRATION);
}

static void job_free(struct xe_sched_job *job)
{
	struct xe_exec_queue *q = job->q;
	bool is_migration = xe_sched_job_is_migration(q);

	kmem_cache_free(xe_exec_queue_is_parallel(job->q) || is_migration ?
			xe_sched_job_parallel_slab : xe_sched_job_slab, job);
}

static struct xe_device *job_to_xe(struct xe_sched_job *job)
{
	return gt_to_xe(job->q->gt);
}

/* Free unused pre-allocated fences */
static void xe_sched_job_free_fences(struct xe_sched_job *job)
{
	int i;

	for (i = 0; i < job->q->width; ++i) {
		struct xe_job_ptrs *ptrs = &job->ptrs[i];

		if (ptrs->lrc_fence)
			xe_lrc_free_seqno_fence(ptrs->lrc_fence);
		dma_fence_chain_free(ptrs->chain_fence);
	}
}

struct xe_sched_job *xe_sched_job_create(struct xe_exec_queue *q,
					 u64 *batch_addr)
{
	bool is_migration = xe_sched_job_is_migration(q);
	struct xe_sched_job *job;
	int err;
	int i;
	u32 width;

	/* only a kernel context can submit a vm-less job */
	XE_WARN_ON(!q->vm && !(q->flags & EXEC_QUEUE_FLAG_KERNEL));

	job = job_alloc(xe_exec_queue_is_parallel(q) || is_migration);
	if (!job)
		return ERR_PTR(-ENOMEM);

	job->q = q;
	kref_init(&job->refcount);
	xe_exec_queue_get(job->q);

	err = drm_sched_job_init(&job->drm, q->entity, 1, NULL);
	if (err)
		goto err_free;

	for (i = 0; i < q->width; ++i) {
		struct dma_fence *fence = xe_lrc_alloc_seqno_fence();
		struct dma_fence_chain *chain;

		if (IS_ERR(fence)) {
			err = PTR_ERR(fence);
			goto err_sched_job;
		}
		job->ptrs[i].lrc_fence = fence;

		if (i + 1 == q->width)
			continue;

		chain = dma_fence_chain_alloc();
		if (!chain) {
			err = -ENOMEM;
			goto err_sched_job;
		}
		job->ptrs[i].chain_fence = chain;
	}

	width = q->width;
	if (is_migration)
		width = 2;

	for (i = 0; i < width; ++i)
		job->ptrs[i].batch_addr = batch_addr[i];

	xe_pm_runtime_get_noresume(job_to_xe(job));
	trace_xe_sched_job_create(job);
	return job;

err_sched_job:
	xe_sched_job_free_fences(job);
	drm_sched_job_cleanup(&job->drm);
err_free:
	xe_exec_queue_put(q);
	job_free(job);
	return ERR_PTR(err);
}

/**
 * xe_sched_job_destroy - Destroy XE schedule job
 * @ref: reference to XE schedule job
 *
 * Called when ref == 0, drop a reference to job's xe_engine + fence, cleanup
 * base DRM schedule job, and free memory for XE schedule job.
 */
void xe_sched_job_destroy(struct kref *ref)
{
	struct xe_sched_job *job =
		container_of(ref, struct xe_sched_job, refcount);
	struct xe_device *xe = job_to_xe(job);
	struct xe_exec_queue *q = job->q;

	xe_sched_job_free_fences(job);
	dma_fence_put(job->fence);
	drm_sched_job_cleanup(&job->drm);
	job_free(job);
	xe_exec_queue_put(q);
	xe_pm_runtime_put(xe);
}

/* Set the error status under the fence to avoid racing with signaling */
static bool xe_fence_set_error(struct dma_fence *fence, int error)
{
	unsigned long irq_flags;
	bool signaled;

	spin_lock_irqsave(fence->lock, irq_flags);
	signaled = test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
	if (!signaled)
		dma_fence_set_error(fence, error);
	spin_unlock_irqrestore(fence->lock, irq_flags);

	return signaled;
}

void xe_sched_job_set_error(struct xe_sched_job *job, int error)
{
	if (xe_fence_set_error(job->fence, error))
		return;

	if (dma_fence_is_chain(job->fence)) {
		struct dma_fence *iter;

		dma_fence_chain_for_each(iter, job->fence)
			xe_fence_set_error(dma_fence_chain_contained(iter),
					   error);
	}

	trace_xe_sched_job_set_error(job);

	dma_fence_enable_sw_signaling(job->fence);
	xe_hw_fence_irq_run(job->q->fence_irq);
}

bool xe_sched_job_started(struct xe_sched_job *job)
{
	struct xe_lrc *lrc = job->q->lrc[0];

	return !__dma_fence_is_later(xe_sched_job_lrc_seqno(job),
				     xe_lrc_start_seqno(lrc),
				     dma_fence_chain_contained(job->fence)->ops);
}

bool xe_sched_job_completed(struct xe_sched_job *job)
{
	struct xe_lrc *lrc = job->q->lrc[0];

	/*
	 * Can safely check just LRC[0] seqno as that is last seqno written when
	 * parallel handshake is done.
	 */

	return !__dma_fence_is_later(xe_sched_job_lrc_seqno(job),
				     xe_lrc_seqno(lrc),
				     dma_fence_chain_contained(job->fence)->ops);
}

void xe_sched_job_arm(struct xe_sched_job *job)
{
	struct xe_exec_queue *q = job->q;
	struct dma_fence *fence, *prev;
	struct xe_vm *vm = q->vm;
	u64 seqno = 0;
	int i;

	/* Migration and kernel engines have their own locking */
	if (IS_ENABLED(CONFIG_LOCKDEP) &&
	    !(q->flags & (EXEC_QUEUE_FLAG_KERNEL | EXEC_QUEUE_FLAG_VM))) {
		lockdep_assert_held(&q->vm->lock);
		if (!xe_vm_in_lr_mode(q->vm))
			xe_vm_assert_held(q->vm);
	}

	if (vm && !xe_sched_job_is_migration(q) && !xe_vm_in_lr_mode(vm) &&
	    (vm->batch_invalidate_tlb || vm->tlb_flush_seqno != q->tlb_flush_seqno)) {
		xe_vm_assert_held(vm);
		q->tlb_flush_seqno = vm->tlb_flush_seqno;
		job->ring_ops_flush_tlb = true;
	}

	/* Arm the pre-allocated fences */
	for (i = 0; i < q->width; prev = fence, ++i) {
		struct dma_fence_chain *chain;

		fence = job->ptrs[i].lrc_fence;
		xe_lrc_init_seqno_fence(q->lrc[i], fence);
		job->ptrs[i].lrc_fence = NULL;
		if (!i) {
			job->lrc_seqno = fence->seqno;
			continue;
		} else {
			xe_assert(gt_to_xe(q->gt), job->lrc_seqno == fence->seqno);
		}

		chain = job->ptrs[i - 1].chain_fence;
		dma_fence_chain_init(chain, prev, fence, seqno++);
		job->ptrs[i - 1].chain_fence = NULL;
		fence = &chain->base;
	}

	job->fence = fence;
	drm_sched_job_arm(&job->drm);
}

void xe_sched_job_push(struct xe_sched_job *job)
{
	xe_sched_job_get(job);
	trace_xe_sched_job_exec(job);
	drm_sched_entity_push_job(&job->drm);
	xe_sched_job_put(job);
}

/**
 * xe_sched_job_last_fence_add_dep - Add last fence dependency to job
 * @job:job to add the last fence dependency to
 * @vm: virtual memory job belongs to
 *
 * Returns:
 * 0 on success, or an error on failing to expand the array.
 */
int xe_sched_job_last_fence_add_dep(struct xe_sched_job *job, struct xe_vm *vm)
{
	struct dma_fence *fence;

	fence = xe_exec_queue_last_fence_get(job->q, vm);

	return drm_sched_job_add_dependency(&job->drm, fence);
}

/**
 * xe_sched_job_init_user_fence - Initialize user_fence for the job
 * @job: job whose user_fence needs an init
 * @sync: sync to be use to init user_fence
 */
void xe_sched_job_init_user_fence(struct xe_sched_job *job,
				  struct xe_sync_entry *sync)
{
	if (sync->type != DRM_XE_SYNC_TYPE_USER_FENCE)
		return;

	job->user_fence.used = true;
	job->user_fence.addr = sync->addr;
	job->user_fence.value = sync->timeline_value;
}

struct xe_sched_job_snapshot *
xe_sched_job_snapshot_capture(struct xe_sched_job *job)
{
	struct xe_exec_queue *q = job->q;
	struct xe_device *xe = q->gt->tile->xe;
	struct xe_sched_job_snapshot *snapshot;
	size_t len = sizeof(*snapshot) + (sizeof(u64) * q->width);
	u16 i;

	snapshot = kzalloc(len, GFP_ATOMIC);
	if (!snapshot)
		return NULL;

	snapshot->batch_addr_len = q->width;
	for (i = 0; i < q->width; i++)
		snapshot->batch_addr[i] =
			xe_device_uncanonicalize_addr(xe, job->ptrs[i].batch_addr);

	return snapshot;
}

void xe_sched_job_snapshot_free(struct xe_sched_job_snapshot *snapshot)
{
	kfree(snapshot);
}

void
xe_sched_job_snapshot_print(struct xe_sched_job_snapshot *snapshot,
			    struct drm_printer *p)
{
	u16 i;

	if (!snapshot)
		return;

	for (i = 0; i < snapshot->batch_addr_len; i++)
		drm_printf(p, "batch_addr[%u]: 0x%016llx\n", i, snapshot->batch_addr[i]);
}

int xe_sched_job_add_deps(struct xe_sched_job *job, struct dma_resv *resv,
			  enum dma_resv_usage usage)
{
	return drm_sched_job_add_resv_dependencies(&job->drm, resv, usage);
}