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
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2026 Intel Corporation
*/
#include <drm/drm_managed.h>
#include <linux/mutex.h>
#include "xe_cpu_bind.h"
#include "xe_device_types.h"
#include "xe_exec_queue.h"
#include "xe_pt.h"
#include "xe_sched_job.h"
#include "xe_trace_bo.h"
#include "xe_vm.h"
/**
* struct xe_cpu_bind - cpu_bind context.
*/
struct xe_cpu_bind {
/** @xe: Xe device */
struct xe_device *xe;
/** @q: Default exec queue used for kernel binds */
struct xe_exec_queue *q;
/** @job_mutex: Timeline mutex for @q. */
struct mutex job_mutex;
};
static bool is_cpu_bind_queue(struct xe_cpu_bind *cpu_bind,
struct xe_exec_queue *q)
{
return cpu_bind->q == q;
}
static void xe_cpu_bind_fini(void *arg)
{
struct xe_cpu_bind *cpu_bind = arg;
mutex_destroy(&cpu_bind->job_mutex);
xe_exec_queue_put(cpu_bind->q);
}
/**
* xe_cpu_bind_init() - Initialize a cpu_bind context
* @xe: &struct xe_device
*
* Return: 0 if successful, negative error code on failure
*/
int xe_cpu_bind_init(struct xe_device *xe)
{
struct xe_cpu_bind *cpu_bind =
drmm_kzalloc(&xe->drm, sizeof(*cpu_bind), GFP_KERNEL);
struct xe_exec_queue *q;
if (!cpu_bind)
return -ENOMEM;
q = xe_exec_queue_create_bind(xe, xe_device_get_root_tile(xe), NULL,
EXEC_QUEUE_FLAG_KERNEL |
EXEC_QUEUE_FLAG_MIGRATE, 0);
if (IS_ERR(q))
return PTR_ERR(q);
cpu_bind->xe = xe;
cpu_bind->q = q;
xe->cpu_bind = cpu_bind;
mutex_init(&cpu_bind->job_mutex);
fs_reclaim_acquire(GFP_KERNEL);
might_lock(&cpu_bind->job_mutex);
fs_reclaim_release(GFP_KERNEL);
return devm_add_action_or_reset(cpu_bind->xe->drm.dev, xe_cpu_bind_fini,
cpu_bind);
}
/**
* xe_cpu_bind_queue() - Get the bind queue from cpu_bind context.
* @cpu_bind: The cpu bind context.
*
* Return: Pointer to bind queue.
*/
struct xe_exec_queue *xe_cpu_bind_queue(struct xe_cpu_bind *cpu_bind)
{
return cpu_bind->q;
}
/**
* xe_cpu_bind_update_pgtables_execute() - Update a VM's PTEs via the CPU
* @vm: The VM being updated
* @tile: The tile being updated
* @ops: The CPU bind PT update ops
* @pt_op: The VM PT update op
* @num_ops: The number of The VM PT update ops
* @force_clear: Force clear operation
*
* Execute the VM PT update ops array which results in a VM's PTEs being updated
* via the CPU.
*/
void
xe_cpu_bind_update_pgtables_execute(struct xe_vm *vm, struct xe_tile *tile,
const struct xe_cpu_bind_pt_update_ops *ops,
struct xe_vm_pgtable_update_op *pt_op,
u32 num_ops, bool force_clear)
{
u32 j, i;
for (j = 0; j < num_ops; ++j, ++pt_op) {
for (i = 0; i < pt_op->num_entries; i++) {
const struct xe_vm_pgtable_update *update =
&pt_op->entries[i];
xe_assert(vm->xe, !iosys_map_is_null(&update->pt_bo->vmap));
if (pt_op->bind && !force_clear)
ops->populate(tile, &update->pt_bo->vmap,
update);
else
ops->clear(vm, tile, &update->pt_bo->vmap,
update);
}
}
trace_xe_vm_cpu_bind(vm);
xe_device_wmb(vm->xe);
}
static struct dma_fence *
xe_cpu_bind_update_pgtables_no_job(struct xe_cpu_bind *cpu_bind,
struct xe_cpu_bind_pt_update *pt_update)
{
const struct xe_cpu_bind_pt_update_ops *ops = pt_update->ops;
struct xe_vm *vm = pt_update->vops->vm;
struct xe_tile *tile;
int err, id;
if (ops->pre_commit) {
pt_update->job = NULL;
err = ops->pre_commit(pt_update);
if (err)
return ERR_PTR(err);
}
for_each_tile(tile, vm->xe, id) {
struct xe_vm_pgtable_update_ops *pt_update_ops =
&pt_update->vops->pt_update_ops[tile->id];
if (!pt_update_ops->pt_job_ops)
continue;
xe_cpu_bind_update_pgtables_execute(vm, tile, ops,
pt_update_ops->pt_job_ops->ops,
pt_update_ops->pt_job_ops->current_op,
false);
}
return dma_fence_get_stub();
}
static struct dma_fence *
xe_cpu_bind_update_pgtables_job(struct xe_cpu_bind *cpu_bind,
struct xe_cpu_bind_pt_update *pt_update)
{
const struct xe_cpu_bind_pt_update_ops *ops = pt_update->ops;
struct xe_exec_queue *q = pt_update->vops->q;
struct xe_device *xe = cpu_bind->xe;
struct xe_sched_job *job;
struct dma_fence *fence;
struct xe_tile *tile;
int err, id;
bool is_cpu_bind = is_cpu_bind_queue(cpu_bind, q);
job = xe_sched_job_create(q, NULL);
if (IS_ERR(job))
return ERR_CAST(job);
xe_assert(xe, job->is_pt_job);
if (ops->pre_commit) {
pt_update->job = job;
err = ops->pre_commit(pt_update);
if (err)
goto err_job;
}
if (is_cpu_bind)
mutex_lock(&cpu_bind->job_mutex);
job->pt_update[0].vm = pt_update->vops->vm;
job->pt_update[0].ops = ops;
for_each_tile(tile, xe, id) {
struct xe_vm_pgtable_update_ops *pt_update_ops =
&pt_update->vops->pt_update_ops[tile->id];
job->pt_update[0].pt_job_ops[tile->id] =
xe_pt_job_ops_get(pt_update_ops->pt_job_ops);
}
xe_sched_job_arm(job);
fence = dma_fence_get(&job->drm.s_fence->finished);
xe_sched_job_push(job);
if (is_cpu_bind)
mutex_unlock(&cpu_bind->job_mutex);
return fence;
err_job:
xe_sched_job_put(job);
return ERR_PTR(err);
}
/**
* xe_cpu_bind_update_pgtables() - Pipelined page-table update
* @cpu_bind: The cpu bind context.
* @pt_update: PT update arguments
*
* Perform a pipelined page-table update. The update descriptors are typically
* built under the same lock critical section as a call to this function. If
* using the default engine for the updates, they will be performed in the
* order they grab the job_mutex. If different engines are used, external
* synchronization is needed for overlapping updates to maintain page-table
* consistency. Note that the meaning of "overlapping" is that the updates
* touch the same page-table, which might be a higher-level page-directory.
* If no pipelining is needed, then updates may be performed by the cpu.
*
* Return: A dma_fence that, when signaled, indicates the update completion.
*/
struct dma_fence *
xe_cpu_bind_update_pgtables(struct xe_cpu_bind *cpu_bind,
struct xe_cpu_bind_pt_update *pt_update)
{
struct dma_fence *fence;
fence = xe_cpu_bind_update_pgtables_no_job(cpu_bind, pt_update);
/* -ETIME indicates a job is needed, anything else is legit error */
if (!IS_ERR(fence) || PTR_ERR(fence) != -ETIME)
return fence;
return xe_cpu_bind_update_pgtables_job(cpu_bind, pt_update);
}
/**
* xe_cpu_bind_job_lock() - Lock cpu_bind job lock
* @cpu_bind: The cpu bind context.
* @q: Queue associated with the operation which requires a lock
*
* Lock the cpu_bind job lock if the queue is a cpu bind queue, otherwise
* assert the VM's dma-resv is held (user queue's have own locking).
*/
void xe_cpu_bind_job_lock(struct xe_cpu_bind *cpu_bind,
struct xe_exec_queue *q)
{
bool is_cpu_bind = is_cpu_bind_queue(cpu_bind, q);
if (is_cpu_bind)
mutex_lock(&cpu_bind->job_mutex);
else
xe_vm_assert_held(q->user_vm); /* User queues VM's should be locked */
}
/**
* xe_cpu_bind_job_unlock() - Unlock cpu_bind job lock
* @cpu_bind: The cpu bind context.
* @q: Queue associated with the operation which requires a lock
*
* Unlock the cpu_bind job lock if the queue is a cpu bind queue, otherwise
* assert the VM's dma-resv is held (user queue's have own locking).
*/
void xe_cpu_bind_job_unlock(struct xe_cpu_bind *cpu_bind,
struct xe_exec_queue *q)
{
bool is_cpu_bind = is_cpu_bind_queue(cpu_bind, q);
if (is_cpu_bind)
mutex_unlock(&cpu_bind->job_mutex);
else
xe_vm_assert_held(q->user_vm); /* User queues VM's should be locked */
}
#if IS_ENABLED(CONFIG_PROVE_LOCKING)
/**
* xe_cpu_bind_job_lock_assert() - Assert cpu_bind job lock held of queue
* @q: cpu bind queue
*/
void xe_cpu_bind_job_lock_assert(struct xe_exec_queue *q)
{
struct xe_device *xe = gt_to_xe(q->gt);
struct xe_cpu_bind *cpu_bind = xe->cpu_bind;
xe_assert(xe, q == cpu_bind->q);
lockdep_assert_held(&cpu_bind->job_mutex);
}
#endif
|