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
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2021 Intel Corporation
*/
#include "xe_sched_job.h"
#include <linux/dma-fence-array.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_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(u64), 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(u64) *
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);
}
struct xe_sched_job *xe_sched_job_create(struct xe_exec_queue *q,
u64 *batch_addr)
{
struct xe_sched_job *job;
struct dma_fence **fences;
bool is_migration = xe_sched_job_is_migration(q);
int err;
int i, j;
u32 width;
/* only a kernel context can submit a vm-less job */
XE_WARN_ON(!q->vm && !(q->flags & EXEC_QUEUE_FLAG_KERNEL));
/* Migration and kernel engines have their own locking */
if (!(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);
}
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;
if (!xe_exec_queue_is_parallel(q)) {
job->fence = xe_lrc_create_seqno_fence(q->lrc);
if (IS_ERR(job->fence)) {
err = PTR_ERR(job->fence);
goto err_sched_job;
}
} else {
struct dma_fence_array *cf;
fences = kmalloc_array(q->width, sizeof(*fences), GFP_KERNEL);
if (!fences) {
err = -ENOMEM;
goto err_sched_job;
}
for (j = 0; j < q->width; ++j) {
fences[j] = xe_lrc_create_seqno_fence(q->lrc + j);
if (IS_ERR(fences[j])) {
err = PTR_ERR(fences[j]);
goto err_fences;
}
}
cf = dma_fence_array_create(q->width, fences,
q->parallel.composite_fence_ctx,
q->parallel.composite_fence_seqno++,
false);
if (!cf) {
--q->parallel.composite_fence_seqno;
err = -ENOMEM;
goto err_fences;
}
/* Sanity check */
for (j = 0; j < q->width; ++j)
xe_assert(job_to_xe(job), cf->base.seqno == fences[j]->seqno);
job->fence = &cf->base;
}
width = q->width;
if (is_migration)
width = 2;
for (i = 0; i < width; ++i)
job->batch_addr[i] = batch_addr[i];
/* All other jobs require a VM to be open which has a ref */
if (unlikely(q->flags & EXEC_QUEUE_FLAG_KERNEL))
xe_device_mem_access_get(job_to_xe(job));
xe_device_assert_mem_access(job_to_xe(job));
trace_xe_sched_job_create(job);
return job;
err_fences:
for (j = j - 1; j >= 0; --j) {
--q->lrc[j].fence_ctx.next_seqno;
dma_fence_put(fences[j]);
}
kfree(fences);
err_sched_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);
if (unlikely(job->q->flags & EXEC_QUEUE_FLAG_KERNEL))
xe_device_mem_access_put(job_to_xe(job));
xe_exec_queue_put(job->q);
dma_fence_put(job->fence);
drm_sched_job_cleanup(&job->drm);
job_free(job);
}
void xe_sched_job_set_error(struct xe_sched_job *job, int error)
{
if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &job->fence->flags))
return;
dma_fence_set_error(job->fence, error);
if (dma_fence_is_array(job->fence)) {
struct dma_fence_array *array =
to_dma_fence_array(job->fence);
struct dma_fence **child = array->fences;
unsigned int nchild = array->num_fences;
do {
struct dma_fence *current_fence = *child++;
if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
¤t_fence->flags))
continue;
dma_fence_set_error(current_fence, error);
} while (--nchild);
}
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;
return !__dma_fence_is_later(xe_sched_job_seqno(job),
xe_lrc_start_seqno(lrc),
job->fence->ops);
}
bool xe_sched_job_completed(struct xe_sched_job *job)
{
struct xe_lrc *lrc = job->q->lrc;
/*
* 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_seqno(job), xe_lrc_seqno(lrc),
job->fence->ops);
}
void xe_sched_job_arm(struct xe_sched_job *job)
{
struct xe_exec_queue *q = job->q;
struct xe_vm *vm = 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;
}
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);
}
|