1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /*
3 * Copyright (c) 2022 Red Hat.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Danilo Krummrich <dakr@redhat.com>
25 *
26 */
27
28 #include <drm/drm_gpuvm.h>
29
30 #include <linux/export.h>
31 #include <linux/interval_tree_generic.h>
32 #include <linux/mm.h>
33
34 /**
35 * DOC: Overview
36 *
37 * The DRM GPU VA Manager, represented by struct drm_gpuvm keeps track of a
38 * GPU's virtual address (VA) space and manages the corresponding virtual
39 * mappings represented by &drm_gpuva objects. It also keeps track of the
40 * mapping's backing &drm_gem_object buffers.
41 *
42 * &drm_gem_object buffers maintain a list of &drm_gpuva objects representing
43 * all existing GPU VA mappings using this &drm_gem_object as backing buffer.
44 *
45 * GPU VAs can be flagged as sparse, such that drivers may use GPU VAs to also
46 * keep track of sparse PTEs in order to support Vulkan 'Sparse Resources'.
47 *
48 * The GPU VA manager internally uses a rb-tree to manage the
49 * &drm_gpuva mappings within a GPU's virtual address space.
50 *
51 * The &drm_gpuvm structure contains a special &drm_gpuva representing the
52 * portion of VA space reserved by the kernel. This node is initialized together
53 * with the GPU VA manager instance and removed when the GPU VA manager is
54 * destroyed.
55 *
56 * In a typical application drivers would embed struct drm_gpuvm and
57 * struct drm_gpuva within their own driver specific structures, there won't be
58 * any memory allocations of its own nor memory allocations of &drm_gpuva
59 * entries.
60 *
61 * The data structures needed to store &drm_gpuvas within the &drm_gpuvm are
62 * contained within struct drm_gpuva already. Hence, for inserting &drm_gpuva
63 * entries from within dma-fence signalling critical sections it is enough to
64 * pre-allocate the &drm_gpuva structures.
65 *
66 * &drm_gem_objects which are private to a single VM can share a common
67 * &dma_resv in order to improve locking efficiency (e.g. with &drm_exec).
68 * For this purpose drivers must pass a &drm_gem_object to drm_gpuvm_init(), in
69 * the following called 'resv object', which serves as the container of the
70 * GPUVM's shared &dma_resv. This resv object can be a driver specific
71 * &drm_gem_object, such as the &drm_gem_object containing the root page table,
72 * but it can also be a 'dummy' object, which can be allocated with
73 * drm_gpuvm_resv_object_alloc().
74 *
75 * In order to connect a struct drm_gpuva to its backing &drm_gem_object each
76 * &drm_gem_object maintains a list of &drm_gpuvm_bo structures, and each
77 * &drm_gpuvm_bo contains a list of &drm_gpuva structures.
78 *
79 * A &drm_gpuvm_bo is an abstraction that represents a combination of a
80 * &drm_gpuvm and a &drm_gem_object. Every such combination should be unique.
81 * This is ensured by the API through drm_gpuvm_bo_obtain() and
82 * drm_gpuvm_bo_obtain_prealloc() which first look into the corresponding
83 * &drm_gem_object list of &drm_gpuvm_bos for an existing instance of this
84 * particular combination. If not present, a new instance is created and linked
85 * to the &drm_gem_object.
86 *
87 * &drm_gpuvm_bo structures, since unique for a given &drm_gpuvm, are also used
88 * as entry for the &drm_gpuvm's lists of external and evicted objects. Those
89 * lists are maintained in order to accelerate locking of dma-resv locks and
90 * validation of evicted objects bound in a &drm_gpuvm. For instance, all
91 * &drm_gem_object's &dma_resv of a given &drm_gpuvm can be locked by calling
92 * drm_gpuvm_exec_lock(). Once locked drivers can call drm_gpuvm_validate() in
93 * order to validate all evicted &drm_gem_objects. It is also possible to lock
94 * additional &drm_gem_objects by providing the corresponding parameters to
95 * drm_gpuvm_exec_lock() as well as open code the &drm_exec loop while making
96 * use of helper functions such as drm_gpuvm_prepare_range() or
97 * drm_gpuvm_prepare_objects().
98 *
99 * Every bound &drm_gem_object is treated as external object when its &dma_resv
100 * structure is different than the &drm_gpuvm's common &dma_resv structure.
101 */
102
103 /**
104 * DOC: Split and Merge
105 *
106 * Besides its capability to manage and represent a GPU VA space, the
107 * GPU VA manager also provides functions to let the &drm_gpuvm calculate a
108 * sequence of operations to satisfy a given map or unmap request.
109 *
110 * Therefore the DRM GPU VA manager provides an algorithm implementing splitting
111 * and merging of existing GPU VA mappings with the ones that are requested to
112 * be mapped or unmapped. This feature is required by the Vulkan API to
113 * implement Vulkan 'Sparse Memory Bindings' - drivers UAPIs often refer to this
114 * as VM BIND.
115 *
116 * Drivers can call drm_gpuvm_sm_map() to receive a sequence of callbacks
117 * containing map, unmap and remap operations for a given newly requested
118 * mapping. The sequence of callbacks represents the set of operations to
119 * execute in order to integrate the new mapping cleanly into the current state
120 * of the GPU VA space.
121 *
122 * Depending on how the new GPU VA mapping intersects with the existing mappings
123 * of the GPU VA space the &drm_gpuvm_ops callbacks contain an arbitrary amount
124 * of unmap operations, a maximum of two remap operations and a single map
125 * operation. The caller might receive no callback at all if no operation is
126 * required, e.g. if the requested mapping already exists in the exact same way.
127 *
128 * The single map operation represents the original map operation requested by
129 * the caller.
130 *
131 * &drm_gpuva_op_unmap contains a 'keep' field, which indicates whether the
132 * &drm_gpuva to unmap is physically contiguous with the original mapping
133 * request. Optionally, if 'keep' is set, drivers may keep the actual page table
134 * entries for this &drm_gpuva, adding the missing page table entries only and
135 * update the &drm_gpuvm's view of things accordingly.
136 *
137 * Drivers may do the same optimization, namely delta page table updates, also
138 * for remap operations. This is possible since &drm_gpuva_op_remap consists of
139 * one unmap operation and one or two map operations, such that drivers can
140 * derive the page table update delta accordingly.
141 *
142 * Note that there can't be more than two existing mappings to split up, one at
143 * the beginning and one at the end of the new mapping, hence there is a
144 * maximum of two remap operations.
145 *
146 * Analogous to drm_gpuvm_sm_map() drm_gpuvm_sm_unmap() uses &drm_gpuvm_ops to
147 * call back into the driver in order to unmap a range of GPU VA space. The
148 * logic behind this function is way simpler though: For all existing mappings
149 * enclosed by the given range unmap operations are created. For mappings which
150 * are only partially located within the given range, remap operations are
151 * created such that those mappings are split up and re-mapped partially.
152 *
153 * As an alternative to drm_gpuvm_sm_map() and drm_gpuvm_sm_unmap(),
154 * drm_gpuvm_sm_map_ops_create() and drm_gpuvm_sm_unmap_ops_create() can be used
155 * to directly obtain an instance of struct drm_gpuva_ops containing a list of
156 * &drm_gpuva_op, which can be iterated with drm_gpuva_for_each_op(). This list
157 * contains the &drm_gpuva_ops analogous to the callbacks one would receive when
158 * calling drm_gpuvm_sm_map() or drm_gpuvm_sm_unmap(). While this way requires
159 * more memory (to allocate the &drm_gpuva_ops), it provides drivers a way to
160 * iterate the &drm_gpuva_op multiple times, e.g. once in a context where memory
161 * allocations are possible (e.g. to allocate GPU page tables) and once in the
162 * dma-fence signalling critical path.
163 *
164 * To update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert() and
165 * drm_gpuva_remove() may be used. These functions can safely be used from
166 * &drm_gpuvm_ops callbacks originating from drm_gpuvm_sm_map() or
167 * drm_gpuvm_sm_unmap(). However, it might be more convenient to use the
168 * provided helper functions drm_gpuva_map(), drm_gpuva_remap() and
169 * drm_gpuva_unmap() instead.
170 *
171 * The following diagram depicts the basic relationships of existing GPU VA
172 * mappings, a newly requested mapping and the resulting mappings as implemented
173 * by drm_gpuvm_sm_map() - it doesn't cover any arbitrary combinations of these.
174 *
175 * 1) Requested mapping is identical. Replace it, but indicate the backing PTEs
176 * could be kept.
177 *
178 * ::
179 *
180 * 0 a 1
181 * old: |-----------| (bo_offset=n)
182 *
183 * 0 a 1
184 * req: |-----------| (bo_offset=n)
185 *
186 * 0 a 1
187 * new: |-----------| (bo_offset=n)
188 *
189 *
190 * 2) Requested mapping is identical, except for the BO offset, hence replace
191 * the mapping.
192 *
193 * ::
194 *
195 * 0 a 1
196 * old: |-----------| (bo_offset=n)
197 *
198 * 0 a 1
199 * req: |-----------| (bo_offset=m)
200 *
201 * 0 a 1
202 * new: |-----------| (bo_offset=m)
203 *
204 *
205 * 3) Requested mapping is identical, except for the backing BO, hence replace
206 * the mapping.
207 *
208 * ::
209 *
210 * 0 a 1
211 * old: |-----------| (bo_offset=n)
212 *
213 * 0 b 1
214 * req: |-----------| (bo_offset=n)
215 *
216 * 0 b 1
217 * new: |-----------| (bo_offset=n)
218 *
219 *
220 * 4) Existent mapping is a left aligned subset of the requested one, hence
221 * replace the existing one.
222 *
223 * ::
224 *
225 * 0 a 1
226 * old: |-----| (bo_offset=n)
227 *
228 * 0 a 2
229 * req: |-----------| (bo_offset=n)
230 *
231 * 0 a 2
232 * new: |-----------| (bo_offset=n)
233 *
234 * .. note::
235 * We expect to see the same result for a request with a different BO
236 * and/or non-contiguous BO offset.
237 *
238 *
239 * 5) Requested mapping's range is a left aligned subset of the existing one,
240 * but backed by a different BO. Hence, map the requested mapping and split
241 * the existing one adjusting its BO offset.
242 *
243 * ::
244 *
245 * 0 a 2
246 * old: |-----------| (bo_offset=n)
247 *
248 * 0 b 1
249 * req: |-----| (bo_offset=n)
250 *
251 * 0 b 1 a' 2
252 * new: |-----|-----| (b.bo_offset=n, a.bo_offset=n+1)
253 *
254 * .. note::
255 * We expect to see the same result for a request with a different BO
256 * and/or non-contiguous BO offset.
257 *
258 *
259 * 6) Existent mapping is a superset of the requested mapping. Split it up, but
260 * indicate that the backing PTEs could be kept.
261 *
262 * ::
263 *
264 * 0 a 2
265 * old: |-----------| (bo_offset=n)
266 *
267 * 0 a 1
268 * req: |-----| (bo_offset=n)
269 *
270 * 0 a 1 a' 2
271 * new: |-----|-----| (a.bo_offset=n, a'.bo_offset=n+1)
272 *
273 *
274 * 7) Requested mapping's range is a right aligned subset of the existing one,
275 * but backed by a different BO. Hence, map the requested mapping and split
276 * the existing one, without adjusting the BO offset.
277 *
278 * ::
279 *
280 * 0 a 2
281 * old: |-----------| (bo_offset=n)
282 *
283 * 1 b 2
284 * req: |-----| (bo_offset=m)
285 *
286 * 0 a 1 b 2
287 * new: |-----|-----| (a.bo_offset=n,b.bo_offset=m)
288 *
289 *
290 * 8) Existent mapping is a superset of the requested mapping. Split it up, but
291 * indicate that the backing PTEs could be kept.
292 *
293 * ::
294 *
295 * 0 a 2
296 * old: |-----------| (bo_offset=n)
297 *
298 * 1 a 2
299 * req: |-----| (bo_offset=n+1)
300 *
301 * 0 a' 1 a 2
302 * new: |-----|-----| (a'.bo_offset=n, a.bo_offset=n+1)
303 *
304 *
305 * 9) Existent mapping is overlapped at the end by the requested mapping backed
306 * by a different BO. Hence, map the requested mapping and split up the
307 * existing one, without adjusting the BO offset.
308 *
309 * ::
310 *
311 * 0 a 2
312 * old: |-----------| (bo_offset=n)
313 *
314 * 1 b 3
315 * req: |-----------| (bo_offset=m)
316 *
317 * 0 a 1 b 3
318 * new: |-----|-----------| (a.bo_offset=n,b.bo_offset=m)
319 *
320 *
321 * 10) Existent mapping is overlapped by the requested mapping, both having the
322 * same backing BO with a contiguous offset. Indicate the backing PTEs of
323 * the old mapping could be kept.
324 *
325 * ::
326 *
327 * 0 a 2
328 * old: |-----------| (bo_offset=n)
329 *
330 * 1 a 3
331 * req: |-----------| (bo_offset=n+1)
332 *
333 * 0 a' 1 a 3
334 * new: |-----|-----------| (a'.bo_offset=n, a.bo_offset=n+1)
335 *
336 *
337 * 11) Requested mapping's range is a centered subset of the existing one
338 * having a different backing BO. Hence, map the requested mapping and split
339 * up the existing one in two mappings, adjusting the BO offset of the right
340 * one accordingly.
341 *
342 * ::
343 *
344 * 0 a 3
345 * old: |-----------------| (bo_offset=n)
346 *
347 * 1 b 2
348 * req: |-----| (bo_offset=m)
349 *
350 * 0 a 1 b 2 a' 3
351 * new: |-----|-----|-----| (a.bo_offset=n,b.bo_offset=m,a'.bo_offset=n+2)
352 *
353 *
354 * 12) Requested mapping is a contiguous subset of the existing one. Split it
355 * up, but indicate that the backing PTEs could be kept.
356 *
357 * ::
358 *
359 * 0 a 3
360 * old: |-----------------| (bo_offset=n)
361 *
362 * 1 a 2
363 * req: |-----| (bo_offset=n+1)
364 *
365 * 0 a' 1 a 2 a'' 3
366 * old: |-----|-----|-----| (a'.bo_offset=n, a.bo_offset=n+1, a''.bo_offset=n+2)
367 *
368 *
369 * 13) Existent mapping is a right aligned subset of the requested one, hence
370 * replace the existing one.
371 *
372 * ::
373 *
374 * 1 a 2
375 * old: |-----| (bo_offset=n+1)
376 *
377 * 0 a 2
378 * req: |-----------| (bo_offset=n)
379 *
380 * 0 a 2
381 * new: |-----------| (bo_offset=n)
382 *
383 * .. note::
384 * We expect to see the same result for a request with a different bo
385 * and/or non-contiguous bo_offset.
386 *
387 *
388 * 14) Existent mapping is a centered subset of the requested one, hence
389 * replace the existing one.
390 *
391 * ::
392 *
393 * 1 a 2
394 * old: |-----| (bo_offset=n+1)
395 *
396 * 0 a 3
397 * req: |----------------| (bo_offset=n)
398 *
399 * 0 a 3
400 * new: |----------------| (bo_offset=n)
401 *
402 * .. note::
403 * We expect to see the same result for a request with a different bo
404 * and/or non-contiguous bo_offset.
405 *
406 *
407 * 15) Existent mappings is overlapped at the beginning by the requested mapping
408 * backed by a different BO. Hence, map the requested mapping and split up
409 * the existing one, adjusting its BO offset accordingly.
410 *
411 * ::
412 *
413 * 1 a 3
414 * old: |-----------| (bo_offset=n)
415 *
416 * 0 b 2
417 * req: |-----------| (bo_offset=m)
418 *
419 * 0 b 2 a' 3
420 * new: |-----------|-----| (b.bo_offset=m,a.bo_offset=n+2)
421 */
422
423 /**
424 * DOC: Madvise Logic - Splitting and Traversal
425 *
426 * This logic handles GPU VA range updates by generating remap and map operations
427 * without performing unmaps or merging existing mappings.
428 *
429 * 1) The requested range lies entirely within a single drm_gpuva. The logic splits
430 * the existing mapping at the start and end boundaries and inserts a new map.
431 *
432 * ::
433 * a start end b
434 * pre: |-----------------------|
435 * drm_gpuva1
436 *
437 * a start end b
438 * new: |-----|=========|-------|
439 * remap map remap
440 *
441 * one REMAP and one MAP : Same behaviour as SPLIT and MERGE
442 *
443 * 2) The requested range spans multiple drm_gpuva regions. The logic traverses
444 * across boundaries, remapping the start and end segments, and inserting two
445 * map operations to cover the full range.
446 *
447 * :: a start b c end d
448 * pre: |------------------|--------------|------------------|
449 * drm_gpuva1 drm_gpuva2 drm_gpuva3
450 *
451 * a start b c end d
452 * new: |-------|==========|--------------|========|---------|
453 * remap1 map1 drm_gpuva2 map2 remap2
454 *
455 * two REMAPS and two MAPS
456 *
457 * 3) Either start or end lies within a drm_gpuva. A single remap and map operation
458 * are generated to update the affected portion.
459 *
460 *
461 * :: a/start b c end d
462 * pre: |------------------|--------------|------------------|
463 * drm_gpuva1 drm_gpuva2 drm_gpuva3
464 *
465 * a/start b c end d
466 * new: |------------------|--------------|========|---------|
467 * drm_gpuva1 drm_gpuva2 map1 remap1
468 *
469 * :: a start b c/end d
470 * pre: |------------------|--------------|------------------|
471 * drm_gpuva1 drm_gpuva2 drm_gpuva3
472 *
473 * a start b c/end d
474 * new: |-------|==========|--------------|------------------|
475 * remap1 map1 drm_gpuva2 drm_gpuva3
476 *
477 * one REMAP and one MAP
478 *
479 * 4) Both start and end align with existing drm_gpuva boundaries. No operations
480 * are needed as the range is already covered.
481 *
482 * 5) No existing drm_gpuvas. No operations.
483 *
484 * Unlike drm_gpuvm_sm_map_ops_create, this logic avoids unmaps and merging,
485 * focusing solely on remap and map operations for efficient traversal and update.
486 */
487
488 /**
489 * DOC: Locking
490 *
491 * In terms of managing &drm_gpuva entries DRM GPUVM does not take care of
492 * locking itself, it is the drivers responsibility to take care about locking.
493 * Drivers might want to protect the following operations: inserting, removing
494 * and iterating &drm_gpuva objects as well as generating all kinds of
495 * operations, such as split / merge or prefetch.
496 *
497 * DRM GPUVM also does not take care of the locking of the backing
498 * &drm_gem_object buffers GPU VA lists and &drm_gpuvm_bo abstractions by
499 * itself; drivers are responsible to enforce mutual exclusion using either the
500 * GEMs dma_resv lock or the GEMs gpuva.lock mutex.
501 *
502 * However, DRM GPUVM contains lockdep checks to ensure callers of its API hold
503 * the corresponding lock whenever the &drm_gem_objects GPU VA list is accessed
504 * by functions such as drm_gpuva_link() or drm_gpuva_unlink(), but also
505 * drm_gpuvm_bo_obtain() and drm_gpuvm_bo_put().
506 *
507 * The latter is required since on creation and destruction of a &drm_gpuvm_bo
508 * the &drm_gpuvm_bo is attached / removed from the &drm_gem_objects gpuva list.
509 * Subsequent calls to drm_gpuvm_bo_obtain() for the same &drm_gpuvm and
510 * &drm_gem_object must be able to observe previous creations and destructions
511 * of &drm_gpuvm_bos in order to keep instances unique.
512 *
513 * The &drm_gpuvm's lists for keeping track of external and evicted objects are
514 * protected against concurrent insertion / removal and iteration internally.
515 *
516 * However, drivers still need ensure to protect concurrent calls to functions
517 * iterating those lists, namely drm_gpuvm_prepare_objects() and
518 * drm_gpuvm_validate().
519 *
520 * Alternatively, drivers can set the &DRM_GPUVM_RESV_PROTECTED flag to indicate
521 * that the corresponding &dma_resv locks are held in order to protect the
522 * lists. If &DRM_GPUVM_RESV_PROTECTED is set, internal locking is disabled and
523 * the corresponding lockdep checks are enabled. This is an optimization for
524 * drivers which are capable of taking the corresponding &dma_resv locks and
525 * hence do not require internal locking.
526 */
527
528 /**
529 * DOC: Examples
530 *
531 * This section gives two examples on how to let the DRM GPUVA Manager generate
532 * &drm_gpuva_op in order to satisfy a given map or unmap request and how to
533 * make use of them.
534 *
535 * The below code is strictly limited to illustrate the generic usage pattern.
536 * To maintain simplicity, it doesn't make use of any abstractions for common
537 * code, different (asynchronous) stages with fence signalling critical paths,
538 * any other helpers or error handling in terms of freeing memory and dropping
539 * previously taken locks.
540 *
541 * 1) Obtain a list of &drm_gpuva_op to create a new mapping::
542 *
543 * // Allocates a new &drm_gpuva.
544 * struct drm_gpuva * driver_gpuva_alloc(void);
545 *
546 * // Typically drivers would embed the &drm_gpuvm and &drm_gpuva
547 * // structure in individual driver structures and lock the dma-resv with
548 * // drm_exec or similar helpers.
549 * int driver_mapping_create(struct drm_gpuvm *gpuvm,
550 * u64 addr, u64 range,
551 * struct drm_gem_object *obj, u64 offset)
552 * {
553 * struct drm_gpuvm_map_req map_req = {
554 * .map.va.addr = addr,
555 * .map.va.range = range,
556 * .map.gem.obj = obj,
557 * .map.gem.offset = offset,
558 * };
559 * struct drm_gpuva_ops *ops;
560 * struct drm_gpuva_op *op
561 * struct drm_gpuvm_bo *vm_bo;
562 *
563 * driver_lock_va_space();
564 * ops = drm_gpuvm_sm_map_ops_create(gpuvm, &map_req);
565 * if (IS_ERR(ops))
566 * return PTR_ERR(ops);
567 *
568 * vm_bo = drm_gpuvm_bo_obtain(gpuvm, obj);
569 * if (IS_ERR(vm_bo))
570 * return PTR_ERR(vm_bo);
571 *
572 * drm_gpuva_for_each_op(op, ops) {
573 * struct drm_gpuva *va;
574 *
575 * switch (op->op) {
576 * case DRM_GPUVA_OP_MAP:
577 * va = driver_gpuva_alloc();
578 * if (!va)
579 * ; // unwind previous VA space updates,
580 * // free memory and unlock
581 *
582 * driver_vm_map();
583 * drm_gpuva_map(gpuvm, va, &op->map);
584 * drm_gpuva_link(va, vm_bo);
585 *
586 * break;
587 * case DRM_GPUVA_OP_REMAP: {
588 * struct drm_gpuva *prev = NULL, *next = NULL;
589 *
590 * va = op->remap.unmap->va;
591 *
592 * if (op->remap.prev) {
593 * prev = driver_gpuva_alloc();
594 * if (!prev)
595 * ; // unwind previous VA space
596 * // updates, free memory and
597 * // unlock
598 * }
599 *
600 * if (op->remap.next) {
601 * next = driver_gpuva_alloc();
602 * if (!next)
603 * ; // unwind previous VA space
604 * // updates, free memory and
605 * // unlock
606 * }
607 *
608 * driver_vm_remap();
609 * drm_gpuva_remap(prev, next, &op->remap);
610 *
611 * if (prev)
612 * drm_gpuva_link(prev, va->vm_bo);
613 * if (next)
614 * drm_gpuva_link(next, va->vm_bo);
615 * drm_gpuva_unlink(va);
616 *
617 * break;
618 * }
619 * case DRM_GPUVA_OP_UNMAP:
620 * va = op->unmap->va;
621 *
622 * driver_vm_unmap();
623 * drm_gpuva_unlink(va);
624 * drm_gpuva_unmap(&op->unmap);
625 *
626 * break;
627 * default:
628 * break;
629 * }
630 * }
631 * drm_gpuvm_bo_put(vm_bo);
632 * driver_unlock_va_space();
633 *
634 * return 0;
635 * }
636 *
637 * 2) Receive a callback for each &drm_gpuva_op to create a new mapping::
638 *
639 * struct driver_context {
640 * struct drm_gpuvm *gpuvm;
641 * struct drm_gpuvm_bo *vm_bo;
642 * struct drm_gpuva *new_va;
643 * struct drm_gpuva *prev_va;
644 * struct drm_gpuva *next_va;
645 * };
646 *
647 * // ops to pass to drm_gpuvm_init()
648 * static const struct drm_gpuvm_ops driver_gpuvm_ops = {
649 * .sm_step_map = driver_gpuva_map,
650 * .sm_step_remap = driver_gpuva_remap,
651 * .sm_step_unmap = driver_gpuva_unmap,
652 * };
653 *
654 * // Typically drivers would embed the &drm_gpuvm and &drm_gpuva
655 * // structure in individual driver structures and lock the dma-resv with
656 * // drm_exec or similar helpers.
657 * int driver_mapping_create(struct drm_gpuvm *gpuvm,
658 * u64 addr, u64 range,
659 * struct drm_gem_object *obj, u64 offset)
660 * {
661 * struct driver_context ctx;
662 * struct drm_gpuvm_bo *vm_bo;
663 * struct drm_gpuva_ops *ops;
664 * struct drm_gpuva_op *op;
665 * int ret = 0;
666 *
667 * ctx.gpuvm = gpuvm;
668 *
669 * ctx.new_va = kzalloc(sizeof(*ctx.new_va), GFP_KERNEL);
670 * ctx.prev_va = kzalloc(sizeof(*ctx.prev_va), GFP_KERNEL);
671 * ctx.next_va = kzalloc(sizeof(*ctx.next_va), GFP_KERNEL);
672 * ctx.vm_bo = drm_gpuvm_bo_create(gpuvm, obj);
673 * if (!ctx.new_va || !ctx.prev_va || !ctx.next_va || !vm_bo) {
674 * ret = -ENOMEM;
675 * goto out;
676 * }
677 *
678 * // Typically protected with a driver specific GEM gpuva lock
679 * // used in the fence signaling path for drm_gpuva_link() and
680 * // drm_gpuva_unlink(), hence pre-allocate.
681 * ctx.vm_bo = drm_gpuvm_bo_obtain_prealloc(ctx.vm_bo);
682 *
683 * driver_lock_va_space();
684 * ret = drm_gpuvm_sm_map(gpuvm, &ctx, addr, range, obj, offset);
685 * driver_unlock_va_space();
686 *
687 * out:
688 * drm_gpuvm_bo_put(ctx.vm_bo);
689 * kfree(ctx.new_va);
690 * kfree(ctx.prev_va);
691 * kfree(ctx.next_va);
692 * return ret;
693 * }
694 *
695 * int driver_gpuva_map(struct drm_gpuva_op *op, void *__ctx)
696 * {
697 * struct driver_context *ctx = __ctx;
698 *
699 * drm_gpuva_map(ctx->vm, ctx->new_va, &op->map);
700 *
701 * drm_gpuva_link(ctx->new_va, ctx->vm_bo);
702 *
703 * // prevent the new GPUVA from being freed in
704 * // driver_mapping_create()
705 * ctx->new_va = NULL;
706 *
707 * return 0;
708 * }
709 *
710 * int driver_gpuva_remap(struct drm_gpuva_op *op, void *__ctx)
711 * {
712 * struct driver_context *ctx = __ctx;
713 * struct drm_gpuva *va = op->remap.unmap->va;
714 *
715 * drm_gpuva_remap(ctx->prev_va, ctx->next_va, &op->remap);
716 *
717 * if (op->remap.prev) {
718 * drm_gpuva_link(ctx->prev_va, va->vm_bo);
719 * ctx->prev_va = NULL;
720 * }
721 *
722 * if (op->remap.next) {
723 * drm_gpuva_link(ctx->next_va, va->vm_bo);
724 * ctx->next_va = NULL;
725 * }
726 *
727 * drm_gpuva_unlink(va);
728 * kfree(va);
729 *
730 * return 0;
731 * }
732 *
733 * int driver_gpuva_unmap(struct drm_gpuva_op *op, void *__ctx)
734 * {
735 * drm_gpuva_unlink(op->unmap.va);
736 * drm_gpuva_unmap(&op->unmap);
737 * kfree(op->unmap.va);
738 *
739 * return 0;
740 * }
741 */
742
743 /**
744 * get_next_vm_bo_from_list() - get the next vm_bo element
745 * @__gpuvm: the &drm_gpuvm
746 * @__list_name: the name of the list we're iterating on
747 * @__local_list: a pointer to the local list used to store already iterated items
748 * @__prev_vm_bo: the previous element we got from get_next_vm_bo_from_list()
749 *
750 * This helper is here to provide lockless list iteration. Lockless as in, the
751 * iterator releases the lock immediately after picking the first element from
752 * the list, so list insertion and deletion can happen concurrently.
753 *
754 * Elements popped from the original list are kept in a local list, so removal
755 * and is_empty checks can still happen while we're iterating the list.
756 */
757 #define get_next_vm_bo_from_list(__gpuvm, __list_name, __local_list, __prev_vm_bo) \
758 ({ \
759 struct drm_gpuvm_bo *__vm_bo = NULL; \
760 \
761 drm_gpuvm_bo_put(__prev_vm_bo); \
762 \
763 spin_lock(&(__gpuvm)->__list_name.lock); \
764 if (!(__gpuvm)->__list_name.local_list) \
765 (__gpuvm)->__list_name.local_list = __local_list; \
766 else \
767 drm_WARN_ON((__gpuvm)->drm, \
768 (__gpuvm)->__list_name.local_list != __local_list); \
769 \
770 while (!list_empty(&(__gpuvm)->__list_name.list)) { \
771 __vm_bo = list_first_entry(&(__gpuvm)->__list_name.list, \
772 struct drm_gpuvm_bo, \
773 list.entry.__list_name); \
774 if (kref_get_unless_zero(&__vm_bo->kref)) { \
775 list_move_tail(&(__vm_bo)->list.entry.__list_name, \
776 __local_list); \
777 break; \
778 } else { \
779 list_del_init(&(__vm_bo)->list.entry.__list_name); \
780 __vm_bo = NULL; \
781 } \
782 } \
783 spin_unlock(&(__gpuvm)->__list_name.lock); \
784 \
785 __vm_bo; \
786 })
787
788 /**
789 * for_each_vm_bo_in_list() - internal vm_bo list iterator
790 * @__gpuvm: the &drm_gpuvm
791 * @__list_name: the name of the list we're iterating on
792 * @__local_list: a pointer to the local list used to store already iterated items
793 * @__vm_bo: the struct drm_gpuvm_bo to assign in each iteration step
794 *
795 * This helper is here to provide lockless list iteration. Lockless as in, the
796 * iterator releases the lock immediately after picking the first element from the
797 * list, hence list insertion and deletion can happen concurrently.
798 *
799 * It is not allowed to re-assign the vm_bo pointer from inside this loop.
800 *
801 * Typical use:
802 *
803 * struct drm_gpuvm_bo *vm_bo;
804 * LIST_HEAD(my_local_list);
805 *
806 * ret = 0;
807 * for_each_vm_bo_in_list(gpuvm, <list_name>, &my_local_list, vm_bo) {
808 * ret = do_something_with_vm_bo(..., vm_bo);
809 * if (ret)
810 * break;
811 * }
812 * // Drop ref in case we break out of the loop.
813 * drm_gpuvm_bo_put(vm_bo);
814 * restore_vm_bo_list(gpuvm, <list_name>, &my_local_list);
815 *
816 *
817 * Only used for internal list iterations, not meant to be exposed to the outside
818 * world.
819 */
820 #define for_each_vm_bo_in_list(__gpuvm, __list_name, __local_list, __vm_bo) \
821 for (__vm_bo = get_next_vm_bo_from_list(__gpuvm, __list_name, \
822 __local_list, NULL); \
823 __vm_bo; \
824 __vm_bo = get_next_vm_bo_from_list(__gpuvm, __list_name, \
825 __local_list, __vm_bo))
826
827 static void
__restore_vm_bo_list(struct drm_gpuvm * gpuvm,spinlock_t * lock,struct list_head * list,struct list_head ** local_list)828 __restore_vm_bo_list(struct drm_gpuvm *gpuvm, spinlock_t *lock,
829 struct list_head *list, struct list_head **local_list)
830 {
831 /* Merge back the two lists, moving local list elements to the
832 * head to preserve previous ordering, in case it matters.
833 */
834 spin_lock(lock);
835 if (*local_list) {
836 list_splice(*local_list, list);
837 *local_list = NULL;
838 }
839 spin_unlock(lock);
840 }
841
842 /**
843 * restore_vm_bo_list() - move vm_bo elements back to their original list
844 * @__gpuvm: the &drm_gpuvm
845 * @__list_name: the name of the list we're iterating on
846 *
847 * When we're done iterating a vm_bo list, we should call restore_vm_bo_list()
848 * to restore the original state and let new iterations take place.
849 */
850 #define restore_vm_bo_list(__gpuvm, __list_name) \
851 __restore_vm_bo_list((__gpuvm), &(__gpuvm)->__list_name.lock, \
852 &(__gpuvm)->__list_name.list, \
853 &(__gpuvm)->__list_name.local_list)
854
855 static void
cond_spin_lock(spinlock_t * lock,bool cond)856 cond_spin_lock(spinlock_t *lock, bool cond)
857 {
858 if (cond)
859 spin_lock(lock);
860 }
861
862 static void
cond_spin_unlock(spinlock_t * lock,bool cond)863 cond_spin_unlock(spinlock_t *lock, bool cond)
864 {
865 if (cond)
866 spin_unlock(lock);
867 }
868
869 static void
__drm_gpuvm_bo_list_add(struct drm_gpuvm * gpuvm,spinlock_t * lock,struct list_head * entry,struct list_head * list)870 __drm_gpuvm_bo_list_add(struct drm_gpuvm *gpuvm, spinlock_t *lock,
871 struct list_head *entry, struct list_head *list)
872 {
873 cond_spin_lock(lock, !!lock);
874 if (list_empty(entry))
875 list_add_tail(entry, list);
876 cond_spin_unlock(lock, !!lock);
877 }
878
879 /**
880 * drm_gpuvm_bo_list_add() - insert a vm_bo into the given list
881 * @__vm_bo: the &drm_gpuvm_bo
882 * @__list_name: the name of the list to insert into
883 * @__lock: whether to lock with the internal spinlock
884 *
885 * Inserts the given @__vm_bo into the list specified by @__list_name.
886 */
887 #define drm_gpuvm_bo_list_add(__vm_bo, __list_name, __lock) \
888 __drm_gpuvm_bo_list_add((__vm_bo)->vm, \
889 __lock ? &(__vm_bo)->vm->__list_name.lock : \
890 NULL, \
891 &(__vm_bo)->list.entry.__list_name, \
892 &(__vm_bo)->vm->__list_name.list)
893
894 static void
__drm_gpuvm_bo_list_del(struct drm_gpuvm * gpuvm,spinlock_t * lock,struct list_head * entry,bool init)895 __drm_gpuvm_bo_list_del(struct drm_gpuvm *gpuvm, spinlock_t *lock,
896 struct list_head *entry, bool init)
897 {
898 cond_spin_lock(lock, !!lock);
899 if (init) {
900 if (!list_empty(entry))
901 list_del_init(entry);
902 } else {
903 list_del(entry);
904 }
905 cond_spin_unlock(lock, !!lock);
906 }
907
908 /**
909 * drm_gpuvm_bo_list_del_init() - remove a vm_bo from the given list
910 * @__vm_bo: the &drm_gpuvm_bo
911 * @__list_name: the name of the list to insert into
912 * @__lock: whether to lock with the internal spinlock
913 *
914 * Removes the given @__vm_bo from the list specified by @__list_name.
915 */
916 #define drm_gpuvm_bo_list_del_init(__vm_bo, __list_name, __lock) \
917 __drm_gpuvm_bo_list_del((__vm_bo)->vm, \
918 __lock ? &(__vm_bo)->vm->__list_name.lock : \
919 NULL, \
920 &(__vm_bo)->list.entry.__list_name, \
921 true)
922
923 /**
924 * drm_gpuvm_bo_list_del() - remove a vm_bo from the given list
925 * @__vm_bo: the &drm_gpuvm_bo
926 * @__list_name: the name of the list to insert into
927 * @__lock: whether to lock with the internal spinlock
928 *
929 * Removes the given @__vm_bo from the list specified by @__list_name.
930 */
931 #define drm_gpuvm_bo_list_del(__vm_bo, __list_name, __lock) \
932 __drm_gpuvm_bo_list_del((__vm_bo)->vm, \
933 __lock ? &(__vm_bo)->vm->__list_name.lock : \
934 NULL, \
935 &(__vm_bo)->list.entry.__list_name, \
936 false)
937
938 #define to_drm_gpuva(__node) container_of((__node), struct drm_gpuva, rb.node)
939
940 #define GPUVA_START(node) ((node)->va.addr)
941 #define GPUVA_LAST(node) ((node)->va.addr + (node)->va.range - 1)
942
943 /* We do not actually use drm_gpuva_it_next(), tell the compiler to not complain
944 * about this.
945 */
946 INTERVAL_TREE_DEFINE(struct drm_gpuva, rb.node, u64, rb.__subtree_last,
947 GPUVA_START, GPUVA_LAST, static __maybe_unused,
948 drm_gpuva_it)
949
950 static int __drm_gpuva_insert(struct drm_gpuvm *gpuvm,
951 struct drm_gpuva *va);
952 static void __drm_gpuva_remove(struct drm_gpuva *va);
953
954 static bool
drm_gpuvm_check_overflow(u64 addr,u64 range)955 drm_gpuvm_check_overflow(u64 addr, u64 range)
956 {
957 u64 end;
958
959 return check_add_overflow(addr, range, &end);
960 }
961
962 static bool
drm_gpuvm_warn_check_overflow(struct drm_gpuvm * gpuvm,u64 addr,u64 range)963 drm_gpuvm_warn_check_overflow(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
964 {
965 return drm_WARN(gpuvm->drm, drm_gpuvm_check_overflow(addr, range),
966 "GPUVA address limited to %zu bytes.\n", sizeof(addr));
967 }
968
969 static bool
drm_gpuvm_in_mm_range(struct drm_gpuvm * gpuvm,u64 addr,u64 range)970 drm_gpuvm_in_mm_range(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
971 {
972 u64 end = addr + range;
973 u64 mm_start = gpuvm->mm_start;
974 u64 mm_end = mm_start + gpuvm->mm_range;
975
976 return addr >= mm_start && end <= mm_end;
977 }
978
979 static bool
drm_gpuvm_in_kernel_node(struct drm_gpuvm * gpuvm,u64 addr,u64 range)980 drm_gpuvm_in_kernel_node(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
981 {
982 u64 end = addr + range;
983 u64 kstart = gpuvm->kernel_alloc_node.va.addr;
984 u64 krange = gpuvm->kernel_alloc_node.va.range;
985 u64 kend = kstart + krange;
986
987 return krange && addr < kend && kstart < end;
988 }
989
990 /**
991 * drm_gpuvm_range_valid() - checks whether the given range is valid for the
992 * given &drm_gpuvm
993 * @gpuvm: the GPUVM to check the range for
994 * @addr: the base address
995 * @range: the range starting from the base address
996 *
997 * Checks whether the range is within the GPUVM's managed boundaries.
998 *
999 * Returns: true for a valid range, false otherwise
1000 */
1001 bool
drm_gpuvm_range_valid(struct drm_gpuvm * gpuvm,u64 addr,u64 range)1002 drm_gpuvm_range_valid(struct drm_gpuvm *gpuvm,
1003 u64 addr, u64 range)
1004 {
1005 return !drm_gpuvm_check_overflow(addr, range) &&
1006 drm_gpuvm_in_mm_range(gpuvm, addr, range) &&
1007 !drm_gpuvm_in_kernel_node(gpuvm, addr, range);
1008 }
1009 EXPORT_SYMBOL_GPL(drm_gpuvm_range_valid);
1010
1011 static void
drm_gpuvm_gem_object_free(struct drm_gem_object * obj)1012 drm_gpuvm_gem_object_free(struct drm_gem_object *obj)
1013 {
1014 drm_gem_object_release(obj);
1015 kfree(obj);
1016 }
1017
1018 static const struct drm_gem_object_funcs drm_gpuvm_object_funcs = {
1019 .free = drm_gpuvm_gem_object_free,
1020 };
1021
1022 /**
1023 * drm_gpuvm_resv_object_alloc() - allocate a dummy &drm_gem_object
1024 * @drm: the drivers &drm_device
1025 *
1026 * Allocates a dummy &drm_gem_object which can be passed to drm_gpuvm_init() in
1027 * order to serve as root GEM object providing the &drm_resv shared across
1028 * &drm_gem_objects local to a single GPUVM.
1029 *
1030 * Returns: the &drm_gem_object on success, NULL on failure
1031 */
1032 struct drm_gem_object *
drm_gpuvm_resv_object_alloc(struct drm_device * drm)1033 drm_gpuvm_resv_object_alloc(struct drm_device *drm)
1034 {
1035 struct drm_gem_object *obj;
1036
1037 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
1038 if (!obj)
1039 return NULL;
1040
1041 obj->funcs = &drm_gpuvm_object_funcs;
1042 drm_gem_private_object_init(drm, obj, 0);
1043
1044 return obj;
1045 }
1046 EXPORT_SYMBOL_GPL(drm_gpuvm_resv_object_alloc);
1047
1048 /**
1049 * drm_gpuvm_init() - initialize a &drm_gpuvm
1050 * @gpuvm: pointer to the &drm_gpuvm to initialize
1051 * @name: the name of the GPU VA space
1052 * @flags: the &drm_gpuvm_flags for this GPUVM
1053 * @drm: the &drm_device this VM resides in
1054 * @r_obj: the resv &drm_gem_object providing the GPUVM's common &dma_resv
1055 * @start_offset: the start offset of the GPU VA space
1056 * @range: the size of the GPU VA space
1057 * @reserve_offset: the start of the kernel reserved GPU VA area
1058 * @reserve_range: the size of the kernel reserved GPU VA area
1059 * @ops: &drm_gpuvm_ops called on &drm_gpuvm_sm_map / &drm_gpuvm_sm_unmap
1060 *
1061 * The &drm_gpuvm must be initialized with this function before use.
1062 *
1063 * Note that @gpuvm must be cleared to 0 before calling this function. The given
1064 * &name is expected to be managed by the surrounding driver structures.
1065 */
1066 void
drm_gpuvm_init(struct drm_gpuvm * gpuvm,const char * name,enum drm_gpuvm_flags flags,struct drm_device * drm,struct drm_gem_object * r_obj,u64 start_offset,u64 range,u64 reserve_offset,u64 reserve_range,const struct drm_gpuvm_ops * ops)1067 drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name,
1068 enum drm_gpuvm_flags flags,
1069 struct drm_device *drm,
1070 struct drm_gem_object *r_obj,
1071 u64 start_offset, u64 range,
1072 u64 reserve_offset, u64 reserve_range,
1073 const struct drm_gpuvm_ops *ops)
1074 {
1075 gpuvm->rb.tree = RB_ROOT_CACHED;
1076 INIT_LIST_HEAD(&gpuvm->rb.list);
1077
1078 INIT_LIST_HEAD(&gpuvm->extobj.list);
1079 spin_lock_init(&gpuvm->extobj.lock);
1080
1081 INIT_LIST_HEAD(&gpuvm->evict.list);
1082 spin_lock_init(&gpuvm->evict.lock);
1083
1084 kref_init(&gpuvm->kref);
1085
1086 gpuvm->name = name ? name : "unknown";
1087 gpuvm->flags = flags;
1088 gpuvm->ops = ops;
1089 gpuvm->drm = drm;
1090 gpuvm->r_obj = r_obj;
1091
1092 drm_gem_object_get(r_obj);
1093
1094 drm_gpuvm_warn_check_overflow(gpuvm, start_offset, range);
1095 gpuvm->mm_start = start_offset;
1096 gpuvm->mm_range = range;
1097
1098 memset(&gpuvm->kernel_alloc_node, 0, sizeof(struct drm_gpuva));
1099 if (reserve_range) {
1100 gpuvm->kernel_alloc_node.va.addr = reserve_offset;
1101 gpuvm->kernel_alloc_node.va.range = reserve_range;
1102
1103 if (likely(!drm_gpuvm_warn_check_overflow(gpuvm, reserve_offset,
1104 reserve_range)))
1105 __drm_gpuva_insert(gpuvm, &gpuvm->kernel_alloc_node);
1106 }
1107 }
1108 EXPORT_SYMBOL_GPL(drm_gpuvm_init);
1109
1110 static void
drm_gpuvm_fini(struct drm_gpuvm * gpuvm)1111 drm_gpuvm_fini(struct drm_gpuvm *gpuvm)
1112 {
1113 gpuvm->name = NULL;
1114
1115 if (gpuvm->kernel_alloc_node.va.range)
1116 __drm_gpuva_remove(&gpuvm->kernel_alloc_node);
1117
1118 drm_WARN(gpuvm->drm, !RB_EMPTY_ROOT(&gpuvm->rb.tree.rb_root),
1119 "GPUVA tree is not empty, potentially leaking memory.\n");
1120
1121 drm_WARN(gpuvm->drm, !list_empty(&gpuvm->extobj.list),
1122 "Extobj list should be empty.\n");
1123 drm_WARN(gpuvm->drm, !list_empty(&gpuvm->evict.list),
1124 "Evict list should be empty.\n");
1125
1126 drm_gem_object_put(gpuvm->r_obj);
1127 }
1128
1129 static void
drm_gpuvm_free(struct kref * kref)1130 drm_gpuvm_free(struct kref *kref)
1131 {
1132 struct drm_gpuvm *gpuvm = container_of(kref, struct drm_gpuvm, kref);
1133
1134 drm_gpuvm_fini(gpuvm);
1135
1136 if (drm_WARN_ON(gpuvm->drm, !gpuvm->ops->vm_free))
1137 return;
1138
1139 gpuvm->ops->vm_free(gpuvm);
1140 }
1141
1142 /**
1143 * drm_gpuvm_put() - drop a struct drm_gpuvm reference
1144 * @gpuvm: the &drm_gpuvm to release the reference of
1145 *
1146 * This releases a reference to @gpuvm.
1147 *
1148 * This function may be called from atomic context.
1149 */
1150 void
drm_gpuvm_put(struct drm_gpuvm * gpuvm)1151 drm_gpuvm_put(struct drm_gpuvm *gpuvm)
1152 {
1153 if (gpuvm)
1154 kref_put(&gpuvm->kref, drm_gpuvm_free);
1155 }
1156 EXPORT_SYMBOL_GPL(drm_gpuvm_put);
1157
1158 static int
exec_prepare_obj(struct drm_exec * exec,struct drm_gem_object * obj,unsigned int num_fences)1159 exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
1160 unsigned int num_fences)
1161 {
1162 return num_fences ? drm_exec_prepare_obj(exec, obj, num_fences) :
1163 drm_exec_lock_obj(exec, obj);
1164 }
1165
1166 /**
1167 * drm_gpuvm_prepare_vm() - prepare the GPUVMs common dma-resv
1168 * @gpuvm: the &drm_gpuvm
1169 * @exec: the &drm_exec context
1170 * @num_fences: the amount of &dma_fences to reserve
1171 *
1172 * Calls drm_exec_prepare_obj() for the GPUVMs dummy &drm_gem_object; if
1173 * @num_fences is zero drm_exec_lock_obj() is called instead.
1174 *
1175 * Using this function directly, it is the drivers responsibility to call
1176 * drm_exec_init() and drm_exec_fini() accordingly.
1177 *
1178 * Returns: 0 on success, negative error code on failure.
1179 */
1180 int
drm_gpuvm_prepare_vm(struct drm_gpuvm * gpuvm,struct drm_exec * exec,unsigned int num_fences)1181 drm_gpuvm_prepare_vm(struct drm_gpuvm *gpuvm,
1182 struct drm_exec *exec,
1183 unsigned int num_fences)
1184 {
1185 return exec_prepare_obj(exec, gpuvm->r_obj, num_fences);
1186 }
1187 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_vm);
1188
1189 static int
__drm_gpuvm_prepare_objects(struct drm_gpuvm * gpuvm,struct drm_exec * exec,unsigned int num_fences)1190 __drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm,
1191 struct drm_exec *exec,
1192 unsigned int num_fences)
1193 {
1194 struct drm_gpuvm_bo *vm_bo;
1195 LIST_HEAD(extobjs);
1196 int ret = 0;
1197
1198 for_each_vm_bo_in_list(gpuvm, extobj, &extobjs, vm_bo) {
1199 ret = exec_prepare_obj(exec, vm_bo->obj, num_fences);
1200 if (ret)
1201 break;
1202 }
1203 /* Drop ref in case we break out of the loop. */
1204 drm_gpuvm_bo_put(vm_bo);
1205 restore_vm_bo_list(gpuvm, extobj);
1206
1207 return ret;
1208 }
1209
1210 static int
drm_gpuvm_prepare_objects_locked(struct drm_gpuvm * gpuvm,struct drm_exec * exec,unsigned int num_fences)1211 drm_gpuvm_prepare_objects_locked(struct drm_gpuvm *gpuvm,
1212 struct drm_exec *exec,
1213 unsigned int num_fences)
1214 {
1215 struct drm_gpuvm_bo *vm_bo;
1216 int ret = 0;
1217
1218 drm_gpuvm_resv_assert_held(gpuvm);
1219 list_for_each_entry(vm_bo, &gpuvm->extobj.list, list.entry.extobj) {
1220 ret = exec_prepare_obj(exec, vm_bo->obj, num_fences);
1221 if (ret)
1222 break;
1223
1224 if (vm_bo->evicted)
1225 drm_gpuvm_bo_list_add(vm_bo, evict, false);
1226 }
1227
1228 return ret;
1229 }
1230
1231 /**
1232 * drm_gpuvm_prepare_objects() - prepare all associated BOs
1233 * @gpuvm: the &drm_gpuvm
1234 * @exec: the &drm_exec locking context
1235 * @num_fences: the amount of &dma_fences to reserve
1236 *
1237 * Calls drm_exec_prepare_obj() for all &drm_gem_objects the given
1238 * &drm_gpuvm contains mappings of; if @num_fences is zero drm_exec_lock_obj()
1239 * is called instead.
1240 *
1241 * Using this function directly, it is the drivers responsibility to call
1242 * drm_exec_init() and drm_exec_fini() accordingly.
1243 *
1244 * Note: This function is safe against concurrent insertion and removal of
1245 * external objects, however it is not safe against concurrent usage itself.
1246 *
1247 * Drivers need to make sure to protect this case with either an outer VM lock
1248 * or by calling drm_gpuvm_prepare_vm() before this function within the
1249 * drm_exec_until_all_locked() loop, such that the GPUVM's dma-resv lock ensures
1250 * mutual exclusion.
1251 *
1252 * Returns: 0 on success, negative error code on failure.
1253 */
1254 int
drm_gpuvm_prepare_objects(struct drm_gpuvm * gpuvm,struct drm_exec * exec,unsigned int num_fences)1255 drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm,
1256 struct drm_exec *exec,
1257 unsigned int num_fences)
1258 {
1259 if (drm_gpuvm_resv_protected(gpuvm))
1260 return drm_gpuvm_prepare_objects_locked(gpuvm, exec,
1261 num_fences);
1262 else
1263 return __drm_gpuvm_prepare_objects(gpuvm, exec, num_fences);
1264 }
1265 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_objects);
1266
1267 /**
1268 * drm_gpuvm_prepare_range() - prepare all BOs mapped within a given range
1269 * @gpuvm: the &drm_gpuvm
1270 * @exec: the &drm_exec locking context
1271 * @addr: the start address within the VA space
1272 * @range: the range to iterate within the VA space
1273 * @num_fences: the amount of &dma_fences to reserve
1274 *
1275 * Calls drm_exec_prepare_obj() for all &drm_gem_objects mapped between @addr
1276 * and @addr + @range; if @num_fences is zero drm_exec_lock_obj() is called
1277 * instead.
1278 *
1279 * Returns: 0 on success, negative error code on failure.
1280 */
1281 int
drm_gpuvm_prepare_range(struct drm_gpuvm * gpuvm,struct drm_exec * exec,u64 addr,u64 range,unsigned int num_fences)1282 drm_gpuvm_prepare_range(struct drm_gpuvm *gpuvm, struct drm_exec *exec,
1283 u64 addr, u64 range, unsigned int num_fences)
1284 {
1285 struct drm_gpuva *va;
1286 u64 end = addr + range;
1287 int ret;
1288
1289 drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) {
1290 struct drm_gem_object *obj = va->gem.obj;
1291
1292 ret = exec_prepare_obj(exec, obj, num_fences);
1293 if (ret)
1294 return ret;
1295 }
1296
1297 return 0;
1298 }
1299 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_range);
1300
1301 /**
1302 * drm_gpuvm_exec_lock() - lock all dma-resv of all associated BOs
1303 * @vm_exec: the &drm_gpuvm_exec wrapper
1304 *
1305 * Acquires all dma-resv locks of all &drm_gem_objects the given
1306 * &drm_gpuvm contains mappings of.
1307 *
1308 * Additionally, when calling this function with struct drm_gpuvm_exec::extra
1309 * being set the driver receives the given @fn callback to lock additional
1310 * dma-resv in the context of the &drm_gpuvm_exec instance. Typically, drivers
1311 * would call drm_exec_prepare_obj() from within this callback.
1312 *
1313 * Returns: 0 on success, negative error code on failure.
1314 */
1315 int
drm_gpuvm_exec_lock(struct drm_gpuvm_exec * vm_exec)1316 drm_gpuvm_exec_lock(struct drm_gpuvm_exec *vm_exec)
1317 {
1318 struct drm_gpuvm *gpuvm = vm_exec->vm;
1319 struct drm_exec *exec = &vm_exec->exec;
1320 unsigned int num_fences = vm_exec->num_fences;
1321 int ret;
1322
1323 drm_exec_init(exec, vm_exec->flags, 0);
1324
1325 drm_exec_until_all_locked(exec) {
1326 ret = drm_gpuvm_prepare_vm(gpuvm, exec, num_fences);
1327 drm_exec_retry_on_contention(exec);
1328 if (ret)
1329 goto err;
1330
1331 ret = drm_gpuvm_prepare_objects(gpuvm, exec, num_fences);
1332 drm_exec_retry_on_contention(exec);
1333 if (ret)
1334 goto err;
1335
1336 if (vm_exec->extra.fn) {
1337 ret = vm_exec->extra.fn(vm_exec);
1338 drm_exec_retry_on_contention(exec);
1339 if (ret)
1340 goto err;
1341 }
1342 }
1343
1344 return 0;
1345
1346 err:
1347 drm_exec_fini(exec);
1348 return ret;
1349 }
1350 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock);
1351
1352 static int
fn_lock_array(struct drm_gpuvm_exec * vm_exec)1353 fn_lock_array(struct drm_gpuvm_exec *vm_exec)
1354 {
1355 struct {
1356 struct drm_gem_object **objs;
1357 unsigned int num_objs;
1358 } *args = vm_exec->extra.priv;
1359
1360 return drm_exec_prepare_array(&vm_exec->exec, args->objs,
1361 args->num_objs, vm_exec->num_fences);
1362 }
1363
1364 /**
1365 * drm_gpuvm_exec_lock_array() - lock all dma-resv of all associated BOs
1366 * @vm_exec: the &drm_gpuvm_exec wrapper
1367 * @objs: additional &drm_gem_objects to lock
1368 * @num_objs: the number of additional &drm_gem_objects to lock
1369 *
1370 * Acquires all dma-resv locks of all &drm_gem_objects the given &drm_gpuvm
1371 * contains mappings of, plus the ones given through @objs.
1372 *
1373 * Returns: 0 on success, negative error code on failure.
1374 */
1375 int
drm_gpuvm_exec_lock_array(struct drm_gpuvm_exec * vm_exec,struct drm_gem_object ** objs,unsigned int num_objs)1376 drm_gpuvm_exec_lock_array(struct drm_gpuvm_exec *vm_exec,
1377 struct drm_gem_object **objs,
1378 unsigned int num_objs)
1379 {
1380 struct {
1381 struct drm_gem_object **objs;
1382 unsigned int num_objs;
1383 } args;
1384
1385 args.objs = objs;
1386 args.num_objs = num_objs;
1387
1388 vm_exec->extra.fn = fn_lock_array;
1389 vm_exec->extra.priv = &args;
1390
1391 return drm_gpuvm_exec_lock(vm_exec);
1392 }
1393 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock_array);
1394
1395 /**
1396 * drm_gpuvm_exec_lock_range() - prepare all BOs mapped within a given range
1397 * @vm_exec: the &drm_gpuvm_exec wrapper
1398 * @addr: the start address within the VA space
1399 * @range: the range to iterate within the VA space
1400 *
1401 * Acquires all dma-resv locks of all &drm_gem_objects mapped between @addr and
1402 * @addr + @range.
1403 *
1404 * Returns: 0 on success, negative error code on failure.
1405 */
1406 int
drm_gpuvm_exec_lock_range(struct drm_gpuvm_exec * vm_exec,u64 addr,u64 range)1407 drm_gpuvm_exec_lock_range(struct drm_gpuvm_exec *vm_exec,
1408 u64 addr, u64 range)
1409 {
1410 struct drm_gpuvm *gpuvm = vm_exec->vm;
1411 struct drm_exec *exec = &vm_exec->exec;
1412 int ret;
1413
1414 drm_exec_init(exec, vm_exec->flags, 0);
1415
1416 drm_exec_until_all_locked(exec) {
1417 ret = drm_gpuvm_prepare_range(gpuvm, exec, addr, range,
1418 vm_exec->num_fences);
1419 drm_exec_retry_on_contention(exec);
1420 if (ret)
1421 goto err;
1422 }
1423
1424 return ret;
1425
1426 err:
1427 drm_exec_fini(exec);
1428 return ret;
1429 }
1430 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock_range);
1431
1432 static int
__drm_gpuvm_validate(struct drm_gpuvm * gpuvm,struct drm_exec * exec)1433 __drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
1434 {
1435 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1436 struct drm_gpuvm_bo *vm_bo;
1437 LIST_HEAD(evict);
1438 int ret = 0;
1439
1440 for_each_vm_bo_in_list(gpuvm, evict, &evict, vm_bo) {
1441 ret = ops->vm_bo_validate(vm_bo, exec);
1442 if (ret)
1443 break;
1444 }
1445 /* Drop ref in case we break out of the loop. */
1446 drm_gpuvm_bo_put(vm_bo);
1447 restore_vm_bo_list(gpuvm, evict);
1448
1449 return ret;
1450 }
1451
1452 static int
drm_gpuvm_validate_locked(struct drm_gpuvm * gpuvm,struct drm_exec * exec)1453 drm_gpuvm_validate_locked(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
1454 {
1455 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1456 struct drm_gpuvm_bo *vm_bo, *next;
1457 int ret = 0;
1458
1459 drm_gpuvm_resv_assert_held(gpuvm);
1460
1461 list_for_each_entry_safe(vm_bo, next, &gpuvm->evict.list,
1462 list.entry.evict) {
1463 ret = ops->vm_bo_validate(vm_bo, exec);
1464 if (ret)
1465 break;
1466
1467 dma_resv_assert_held(vm_bo->obj->resv);
1468 if (!vm_bo->evicted)
1469 drm_gpuvm_bo_list_del_init(vm_bo, evict, false);
1470 }
1471
1472 return ret;
1473 }
1474
1475 /**
1476 * drm_gpuvm_validate() - validate all BOs marked as evicted
1477 * @gpuvm: the &drm_gpuvm to validate evicted BOs
1478 * @exec: the &drm_exec instance used for locking the GPUVM
1479 *
1480 * Calls the &drm_gpuvm_ops::vm_bo_validate callback for all evicted buffer
1481 * objects being mapped in the given &drm_gpuvm.
1482 *
1483 * Returns: 0 on success, negative error code on failure.
1484 */
1485 int
drm_gpuvm_validate(struct drm_gpuvm * gpuvm,struct drm_exec * exec)1486 drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
1487 {
1488 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1489
1490 if (unlikely(!ops || !ops->vm_bo_validate))
1491 return -EOPNOTSUPP;
1492
1493 if (drm_gpuvm_resv_protected(gpuvm))
1494 return drm_gpuvm_validate_locked(gpuvm, exec);
1495 else
1496 return __drm_gpuvm_validate(gpuvm, exec);
1497 }
1498 EXPORT_SYMBOL_GPL(drm_gpuvm_validate);
1499
1500 /**
1501 * drm_gpuvm_resv_add_fence - add fence to private and all extobj
1502 * dma-resv
1503 * @gpuvm: the &drm_gpuvm to add a fence to
1504 * @exec: the &drm_exec locking context
1505 * @fence: fence to add
1506 * @private_usage: private dma-resv usage
1507 * @extobj_usage: extobj dma-resv usage
1508 */
1509 void
drm_gpuvm_resv_add_fence(struct drm_gpuvm * gpuvm,struct drm_exec * exec,struct dma_fence * fence,enum dma_resv_usage private_usage,enum dma_resv_usage extobj_usage)1510 drm_gpuvm_resv_add_fence(struct drm_gpuvm *gpuvm,
1511 struct drm_exec *exec,
1512 struct dma_fence *fence,
1513 enum dma_resv_usage private_usage,
1514 enum dma_resv_usage extobj_usage)
1515 {
1516 struct drm_gem_object *obj;
1517 unsigned long index;
1518
1519 drm_exec_for_each_locked_object(exec, index, obj) {
1520 dma_resv_assert_held(obj->resv);
1521 dma_resv_add_fence(obj->resv, fence,
1522 drm_gpuvm_is_extobj(gpuvm, obj) ?
1523 extobj_usage : private_usage);
1524 }
1525 }
1526 EXPORT_SYMBOL_GPL(drm_gpuvm_resv_add_fence);
1527
1528 /**
1529 * drm_gpuvm_bo_create() - create a new instance of struct drm_gpuvm_bo
1530 * @gpuvm: The &drm_gpuvm the @obj is mapped in.
1531 * @obj: The &drm_gem_object being mapped in the @gpuvm.
1532 *
1533 * If provided by the driver, this function uses the &drm_gpuvm_ops
1534 * vm_bo_alloc() callback to allocate.
1535 *
1536 * Returns: a pointer to the &drm_gpuvm_bo on success, NULL on failure
1537 */
1538 struct drm_gpuvm_bo *
drm_gpuvm_bo_create(struct drm_gpuvm * gpuvm,struct drm_gem_object * obj)1539 drm_gpuvm_bo_create(struct drm_gpuvm *gpuvm,
1540 struct drm_gem_object *obj)
1541 {
1542 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1543 struct drm_gpuvm_bo *vm_bo;
1544
1545 if (ops && ops->vm_bo_alloc)
1546 vm_bo = ops->vm_bo_alloc();
1547 else
1548 vm_bo = kzalloc(sizeof(*vm_bo), GFP_KERNEL);
1549
1550 if (unlikely(!vm_bo))
1551 return NULL;
1552
1553 vm_bo->vm = drm_gpuvm_get(gpuvm);
1554 vm_bo->obj = obj;
1555 drm_gem_object_get(obj);
1556
1557 kref_init(&vm_bo->kref);
1558 INIT_LIST_HEAD(&vm_bo->list.gpuva);
1559 INIT_LIST_HEAD(&vm_bo->list.entry.gem);
1560
1561 INIT_LIST_HEAD(&vm_bo->list.entry.extobj);
1562 INIT_LIST_HEAD(&vm_bo->list.entry.evict);
1563
1564 return vm_bo;
1565 }
1566 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_create);
1567
1568 static void
drm_gpuvm_bo_destroy(struct kref * kref)1569 drm_gpuvm_bo_destroy(struct kref *kref)
1570 {
1571 struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo,
1572 kref);
1573 struct drm_gpuvm *gpuvm = vm_bo->vm;
1574 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1575 struct drm_gem_object *obj = vm_bo->obj;
1576 bool lock = !drm_gpuvm_resv_protected(gpuvm);
1577
1578 if (!lock)
1579 drm_gpuvm_resv_assert_held(gpuvm);
1580
1581 drm_gpuvm_bo_list_del(vm_bo, extobj, lock);
1582 drm_gpuvm_bo_list_del(vm_bo, evict, lock);
1583
1584 drm_gem_gpuva_assert_lock_held(gpuvm, obj);
1585 list_del(&vm_bo->list.entry.gem);
1586
1587 if (ops && ops->vm_bo_free)
1588 ops->vm_bo_free(vm_bo);
1589 else
1590 kfree(vm_bo);
1591
1592 drm_gpuvm_put(gpuvm);
1593 drm_gem_object_put(obj);
1594 }
1595
1596 /**
1597 * drm_gpuvm_bo_put() - drop a struct drm_gpuvm_bo reference
1598 * @vm_bo: the &drm_gpuvm_bo to release the reference of
1599 *
1600 * This releases a reference to @vm_bo.
1601 *
1602 * If the reference count drops to zero, the &gpuvm_bo is destroyed, which
1603 * includes removing it from the GEMs gpuva list. Hence, if a call to this
1604 * function can potentially let the reference count drop to zero the caller must
1605 * hold the lock that the GEM uses for its gpuva list (either the GEM's
1606 * dma-resv or gpuva.lock mutex).
1607 *
1608 * This function may only be called from non-atomic context.
1609 *
1610 * Returns: true if vm_bo was destroyed, false otherwise.
1611 */
1612 bool
drm_gpuvm_bo_put(struct drm_gpuvm_bo * vm_bo)1613 drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo)
1614 {
1615 might_sleep();
1616
1617 if (vm_bo)
1618 return !!kref_put(&vm_bo->kref, drm_gpuvm_bo_destroy);
1619
1620 return false;
1621 }
1622 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_put);
1623
1624 static struct drm_gpuvm_bo *
__drm_gpuvm_bo_find(struct drm_gpuvm * gpuvm,struct drm_gem_object * obj)1625 __drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm,
1626 struct drm_gem_object *obj)
1627 {
1628 struct drm_gpuvm_bo *vm_bo;
1629
1630 drm_gem_gpuva_assert_lock_held(gpuvm, obj);
1631 drm_gem_for_each_gpuvm_bo(vm_bo, obj)
1632 if (vm_bo->vm == gpuvm)
1633 return vm_bo;
1634
1635 return NULL;
1636 }
1637
1638 /**
1639 * drm_gpuvm_bo_find() - find the &drm_gpuvm_bo for the given
1640 * &drm_gpuvm and &drm_gem_object
1641 * @gpuvm: The &drm_gpuvm the @obj is mapped in.
1642 * @obj: The &drm_gem_object being mapped in the @gpuvm.
1643 *
1644 * Find the &drm_gpuvm_bo representing the combination of the given
1645 * &drm_gpuvm and &drm_gem_object. If found, increases the reference
1646 * count of the &drm_gpuvm_bo accordingly.
1647 *
1648 * Returns: a pointer to the &drm_gpuvm_bo on success, NULL on failure
1649 */
1650 struct drm_gpuvm_bo *
drm_gpuvm_bo_find(struct drm_gpuvm * gpuvm,struct drm_gem_object * obj)1651 drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm,
1652 struct drm_gem_object *obj)
1653 {
1654 struct drm_gpuvm_bo *vm_bo = __drm_gpuvm_bo_find(gpuvm, obj);
1655
1656 return vm_bo ? drm_gpuvm_bo_get(vm_bo) : NULL;
1657 }
1658 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_find);
1659
1660 /**
1661 * drm_gpuvm_bo_obtain() - obtains an instance of the &drm_gpuvm_bo for the
1662 * given &drm_gpuvm and &drm_gem_object
1663 * @gpuvm: The &drm_gpuvm the @obj is mapped in.
1664 * @obj: The &drm_gem_object being mapped in the @gpuvm.
1665 *
1666 * Find the &drm_gpuvm_bo representing the combination of the given
1667 * &drm_gpuvm and &drm_gem_object. If found, increases the reference
1668 * count of the &drm_gpuvm_bo accordingly. If not found, allocates a new
1669 * &drm_gpuvm_bo.
1670 *
1671 * A new &drm_gpuvm_bo is added to the GEMs gpuva list.
1672 *
1673 * Returns: a pointer to the &drm_gpuvm_bo on success, an ERR_PTR on failure
1674 */
1675 struct drm_gpuvm_bo *
drm_gpuvm_bo_obtain(struct drm_gpuvm * gpuvm,struct drm_gem_object * obj)1676 drm_gpuvm_bo_obtain(struct drm_gpuvm *gpuvm,
1677 struct drm_gem_object *obj)
1678 {
1679 struct drm_gpuvm_bo *vm_bo;
1680
1681 vm_bo = drm_gpuvm_bo_find(gpuvm, obj);
1682 if (vm_bo)
1683 return vm_bo;
1684
1685 vm_bo = drm_gpuvm_bo_create(gpuvm, obj);
1686 if (!vm_bo)
1687 return ERR_PTR(-ENOMEM);
1688
1689 drm_gem_gpuva_assert_lock_held(gpuvm, obj);
1690 list_add_tail(&vm_bo->list.entry.gem, &obj->gpuva.list);
1691
1692 return vm_bo;
1693 }
1694 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_obtain);
1695
1696 /**
1697 * drm_gpuvm_bo_obtain_prealloc() - obtains an instance of the &drm_gpuvm_bo
1698 * for the given &drm_gpuvm and &drm_gem_object
1699 * @__vm_bo: A pre-allocated struct drm_gpuvm_bo.
1700 *
1701 * Find the &drm_gpuvm_bo representing the combination of the given
1702 * &drm_gpuvm and &drm_gem_object. If found, increases the reference
1703 * count of the found &drm_gpuvm_bo accordingly, while the @__vm_bo reference
1704 * count is decreased. If not found @__vm_bo is returned without further
1705 * increase of the reference count.
1706 *
1707 * A new &drm_gpuvm_bo is added to the GEMs gpuva list.
1708 *
1709 * Returns: a pointer to the found &drm_gpuvm_bo or @__vm_bo if no existing
1710 * &drm_gpuvm_bo was found
1711 */
1712 struct drm_gpuvm_bo *
drm_gpuvm_bo_obtain_prealloc(struct drm_gpuvm_bo * __vm_bo)1713 drm_gpuvm_bo_obtain_prealloc(struct drm_gpuvm_bo *__vm_bo)
1714 {
1715 struct drm_gpuvm *gpuvm = __vm_bo->vm;
1716 struct drm_gem_object *obj = __vm_bo->obj;
1717 struct drm_gpuvm_bo *vm_bo;
1718
1719 vm_bo = drm_gpuvm_bo_find(gpuvm, obj);
1720 if (vm_bo) {
1721 drm_gpuvm_bo_put(__vm_bo);
1722 return vm_bo;
1723 }
1724
1725 drm_gem_gpuva_assert_lock_held(gpuvm, obj);
1726 list_add_tail(&__vm_bo->list.entry.gem, &obj->gpuva.list);
1727
1728 return __vm_bo;
1729 }
1730 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_obtain_prealloc);
1731
1732 /**
1733 * drm_gpuvm_bo_extobj_add() - adds the &drm_gpuvm_bo to its &drm_gpuvm's
1734 * extobj list
1735 * @vm_bo: The &drm_gpuvm_bo to add to its &drm_gpuvm's the extobj list.
1736 *
1737 * Adds the given @vm_bo to its &drm_gpuvm's extobj list if not on the list
1738 * already and if the corresponding &drm_gem_object is an external object,
1739 * actually.
1740 */
1741 void
drm_gpuvm_bo_extobj_add(struct drm_gpuvm_bo * vm_bo)1742 drm_gpuvm_bo_extobj_add(struct drm_gpuvm_bo *vm_bo)
1743 {
1744 struct drm_gpuvm *gpuvm = vm_bo->vm;
1745 bool lock = !drm_gpuvm_resv_protected(gpuvm);
1746
1747 if (!lock)
1748 drm_gpuvm_resv_assert_held(gpuvm);
1749
1750 if (drm_gpuvm_is_extobj(gpuvm, vm_bo->obj))
1751 drm_gpuvm_bo_list_add(vm_bo, extobj, lock);
1752 }
1753 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_extobj_add);
1754
1755 /**
1756 * drm_gpuvm_bo_evict() - add / remove a &drm_gpuvm_bo to / from the &drm_gpuvms
1757 * evicted list
1758 * @vm_bo: the &drm_gpuvm_bo to add or remove
1759 * @evict: indicates whether the object is evicted
1760 *
1761 * Adds a &drm_gpuvm_bo to or removes it from the &drm_gpuvm's evicted list.
1762 */
1763 void
drm_gpuvm_bo_evict(struct drm_gpuvm_bo * vm_bo,bool evict)1764 drm_gpuvm_bo_evict(struct drm_gpuvm_bo *vm_bo, bool evict)
1765 {
1766 struct drm_gpuvm *gpuvm = vm_bo->vm;
1767 struct drm_gem_object *obj = vm_bo->obj;
1768 bool lock = !drm_gpuvm_resv_protected(gpuvm);
1769
1770 dma_resv_assert_held(obj->resv);
1771 vm_bo->evicted = evict;
1772
1773 /* Can't add external objects to the evicted list directly if not using
1774 * internal spinlocks, since in this case the evicted list is protected
1775 * with the VM's common dma-resv lock.
1776 */
1777 if (drm_gpuvm_is_extobj(gpuvm, obj) && !lock)
1778 return;
1779
1780 if (evict)
1781 drm_gpuvm_bo_list_add(vm_bo, evict, lock);
1782 else
1783 drm_gpuvm_bo_list_del_init(vm_bo, evict, lock);
1784 }
1785 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_evict);
1786
1787 static int
__drm_gpuva_insert(struct drm_gpuvm * gpuvm,struct drm_gpuva * va)1788 __drm_gpuva_insert(struct drm_gpuvm *gpuvm,
1789 struct drm_gpuva *va)
1790 {
1791 struct rb_node *node;
1792 struct list_head *head;
1793
1794 if (drm_gpuva_it_iter_first(&gpuvm->rb.tree,
1795 GPUVA_START(va),
1796 GPUVA_LAST(va)))
1797 return -EEXIST;
1798
1799 va->vm = gpuvm;
1800
1801 drm_gpuva_it_insert(va, &gpuvm->rb.tree);
1802
1803 node = rb_prev(&va->rb.node);
1804 if (node)
1805 head = &(to_drm_gpuva(node))->rb.entry;
1806 else
1807 head = &gpuvm->rb.list;
1808
1809 list_add(&va->rb.entry, head);
1810
1811 return 0;
1812 }
1813
1814 /**
1815 * drm_gpuva_insert() - insert a &drm_gpuva
1816 * @gpuvm: the &drm_gpuvm to insert the &drm_gpuva in
1817 * @va: the &drm_gpuva to insert
1818 *
1819 * Insert a &drm_gpuva with a given address and range into a
1820 * &drm_gpuvm.
1821 *
1822 * It is safe to use this function using the safe versions of iterating the GPU
1823 * VA space, such as drm_gpuvm_for_each_va_safe() and
1824 * drm_gpuvm_for_each_va_range_safe().
1825 *
1826 * Returns: 0 on success, negative error code on failure.
1827 */
1828 int
drm_gpuva_insert(struct drm_gpuvm * gpuvm,struct drm_gpuva * va)1829 drm_gpuva_insert(struct drm_gpuvm *gpuvm,
1830 struct drm_gpuva *va)
1831 {
1832 u64 addr = va->va.addr;
1833 u64 range = va->va.range;
1834 int ret;
1835
1836 if (unlikely(!drm_gpuvm_range_valid(gpuvm, addr, range)))
1837 return -EINVAL;
1838
1839 ret = __drm_gpuva_insert(gpuvm, va);
1840 if (likely(!ret))
1841 /* Take a reference of the GPUVM for the successfully inserted
1842 * drm_gpuva. We can't take the reference in
1843 * __drm_gpuva_insert() itself, since we don't want to increse
1844 * the reference count for the GPUVM's kernel_alloc_node.
1845 */
1846 drm_gpuvm_get(gpuvm);
1847
1848 return ret;
1849 }
1850 EXPORT_SYMBOL_GPL(drm_gpuva_insert);
1851
1852 static void
__drm_gpuva_remove(struct drm_gpuva * va)1853 __drm_gpuva_remove(struct drm_gpuva *va)
1854 {
1855 drm_gpuva_it_remove(va, &va->vm->rb.tree);
1856 list_del_init(&va->rb.entry);
1857 }
1858
1859 /**
1860 * drm_gpuva_remove() - remove a &drm_gpuva
1861 * @va: the &drm_gpuva to remove
1862 *
1863 * This removes the given &va from the underlying tree.
1864 *
1865 * It is safe to use this function using the safe versions of iterating the GPU
1866 * VA space, such as drm_gpuvm_for_each_va_safe() and
1867 * drm_gpuvm_for_each_va_range_safe().
1868 */
1869 void
drm_gpuva_remove(struct drm_gpuva * va)1870 drm_gpuva_remove(struct drm_gpuva *va)
1871 {
1872 struct drm_gpuvm *gpuvm = va->vm;
1873
1874 if (unlikely(va == &gpuvm->kernel_alloc_node)) {
1875 drm_WARN(gpuvm->drm, 1,
1876 "Can't destroy kernel reserved node.\n");
1877 return;
1878 }
1879
1880 __drm_gpuva_remove(va);
1881 drm_gpuvm_put(va->vm);
1882 }
1883 EXPORT_SYMBOL_GPL(drm_gpuva_remove);
1884
1885 /**
1886 * drm_gpuva_link() - link a &drm_gpuva
1887 * @va: the &drm_gpuva to link
1888 * @vm_bo: the &drm_gpuvm_bo to add the &drm_gpuva to
1889 *
1890 * This adds the given &va to the GPU VA list of the &drm_gpuvm_bo and the
1891 * &drm_gpuvm_bo to the &drm_gem_object it is associated with.
1892 *
1893 * For every &drm_gpuva entry added to the &drm_gpuvm_bo an additional
1894 * reference of the latter is taken.
1895 *
1896 * This function expects the caller to protect the GEM's GPUVA list against
1897 * concurrent access using either the GEM's dma-resv or gpuva.lock mutex.
1898 */
1899 void
drm_gpuva_link(struct drm_gpuva * va,struct drm_gpuvm_bo * vm_bo)1900 drm_gpuva_link(struct drm_gpuva *va, struct drm_gpuvm_bo *vm_bo)
1901 {
1902 struct drm_gem_object *obj = va->gem.obj;
1903 struct drm_gpuvm *gpuvm = va->vm;
1904
1905 if (unlikely(!obj))
1906 return;
1907
1908 drm_WARN_ON(gpuvm->drm, obj != vm_bo->obj);
1909
1910 va->vm_bo = drm_gpuvm_bo_get(vm_bo);
1911
1912 drm_gem_gpuva_assert_lock_held(gpuvm, obj);
1913 list_add_tail(&va->gem.entry, &vm_bo->list.gpuva);
1914 }
1915 EXPORT_SYMBOL_GPL(drm_gpuva_link);
1916
1917 /**
1918 * drm_gpuva_unlink() - unlink a &drm_gpuva
1919 * @va: the &drm_gpuva to unlink
1920 *
1921 * This removes the given &va from the GPU VA list of the &drm_gem_object it is
1922 * associated with.
1923 *
1924 * This removes the given &va from the GPU VA list of the &drm_gpuvm_bo and
1925 * the &drm_gpuvm_bo from the &drm_gem_object it is associated with in case
1926 * this call unlinks the last &drm_gpuva from the &drm_gpuvm_bo.
1927 *
1928 * For every &drm_gpuva entry removed from the &drm_gpuvm_bo a reference of
1929 * the latter is dropped.
1930 *
1931 * This function expects the caller to protect the GEM's GPUVA list against
1932 * concurrent access using either the GEM's dma-resv or gpuva.lock mutex.
1933 */
1934 void
drm_gpuva_unlink(struct drm_gpuva * va)1935 drm_gpuva_unlink(struct drm_gpuva *va)
1936 {
1937 struct drm_gem_object *obj = va->gem.obj;
1938 struct drm_gpuvm_bo *vm_bo = va->vm_bo;
1939
1940 if (unlikely(!obj))
1941 return;
1942
1943 drm_gem_gpuva_assert_lock_held(va->vm, obj);
1944 list_del_init(&va->gem.entry);
1945
1946 va->vm_bo = NULL;
1947 drm_gpuvm_bo_put(vm_bo);
1948 }
1949 EXPORT_SYMBOL_GPL(drm_gpuva_unlink);
1950
1951 /**
1952 * drm_gpuva_find_first() - find the first &drm_gpuva in the given range
1953 * @gpuvm: the &drm_gpuvm to search in
1954 * @addr: the &drm_gpuvas address
1955 * @range: the &drm_gpuvas range
1956 *
1957 * Returns: the first &drm_gpuva within the given range
1958 */
1959 struct drm_gpuva *
drm_gpuva_find_first(struct drm_gpuvm * gpuvm,u64 addr,u64 range)1960 drm_gpuva_find_first(struct drm_gpuvm *gpuvm,
1961 u64 addr, u64 range)
1962 {
1963 u64 last = addr + range - 1;
1964
1965 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, addr, last);
1966 }
1967 EXPORT_SYMBOL_GPL(drm_gpuva_find_first);
1968
1969 /**
1970 * drm_gpuva_find() - find a &drm_gpuva
1971 * @gpuvm: the &drm_gpuvm to search in
1972 * @addr: the &drm_gpuvas address
1973 * @range: the &drm_gpuvas range
1974 *
1975 * Returns: the &drm_gpuva at a given &addr and with a given &range
1976 */
1977 struct drm_gpuva *
drm_gpuva_find(struct drm_gpuvm * gpuvm,u64 addr,u64 range)1978 drm_gpuva_find(struct drm_gpuvm *gpuvm,
1979 u64 addr, u64 range)
1980 {
1981 struct drm_gpuva *va;
1982
1983 va = drm_gpuva_find_first(gpuvm, addr, range);
1984 if (!va)
1985 goto out;
1986
1987 if (va->va.addr != addr ||
1988 va->va.range != range)
1989 goto out;
1990
1991 return va;
1992
1993 out:
1994 return NULL;
1995 }
1996 EXPORT_SYMBOL_GPL(drm_gpuva_find);
1997
1998 /**
1999 * drm_gpuva_find_prev() - find the &drm_gpuva before the given address
2000 * @gpuvm: the &drm_gpuvm to search in
2001 * @start: the given GPU VA's start address
2002 *
2003 * Find the adjacent &drm_gpuva before the GPU VA with given &start address.
2004 *
2005 * Note that if there is any free space between the GPU VA mappings no mapping
2006 * is returned.
2007 *
2008 * Returns: a pointer to the found &drm_gpuva or NULL if none was found
2009 */
2010 struct drm_gpuva *
drm_gpuva_find_prev(struct drm_gpuvm * gpuvm,u64 start)2011 drm_gpuva_find_prev(struct drm_gpuvm *gpuvm, u64 start)
2012 {
2013 if (!drm_gpuvm_range_valid(gpuvm, start - 1, 1))
2014 return NULL;
2015
2016 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, start - 1, start);
2017 }
2018 EXPORT_SYMBOL_GPL(drm_gpuva_find_prev);
2019
2020 /**
2021 * drm_gpuva_find_next() - find the &drm_gpuva after the given address
2022 * @gpuvm: the &drm_gpuvm to search in
2023 * @end: the given GPU VA's end address
2024 *
2025 * Find the adjacent &drm_gpuva after the GPU VA with given &end address.
2026 *
2027 * Note that if there is any free space between the GPU VA mappings no mapping
2028 * is returned.
2029 *
2030 * Returns: a pointer to the found &drm_gpuva or NULL if none was found
2031 */
2032 struct drm_gpuva *
drm_gpuva_find_next(struct drm_gpuvm * gpuvm,u64 end)2033 drm_gpuva_find_next(struct drm_gpuvm *gpuvm, u64 end)
2034 {
2035 if (!drm_gpuvm_range_valid(gpuvm, end, 1))
2036 return NULL;
2037
2038 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, end, end + 1);
2039 }
2040 EXPORT_SYMBOL_GPL(drm_gpuva_find_next);
2041
2042 /**
2043 * drm_gpuvm_interval_empty() - indicate whether a given interval of the VA space
2044 * is empty
2045 * @gpuvm: the &drm_gpuvm to check the range for
2046 * @addr: the start address of the range
2047 * @range: the range of the interval
2048 *
2049 * Returns: true if the interval is empty, false otherwise
2050 */
2051 bool
drm_gpuvm_interval_empty(struct drm_gpuvm * gpuvm,u64 addr,u64 range)2052 drm_gpuvm_interval_empty(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
2053 {
2054 return !drm_gpuva_find_first(gpuvm, addr, range);
2055 }
2056 EXPORT_SYMBOL_GPL(drm_gpuvm_interval_empty);
2057
2058 /**
2059 * drm_gpuva_map() - helper to insert a &drm_gpuva according to a
2060 * &drm_gpuva_op_map
2061 * @gpuvm: the &drm_gpuvm
2062 * @va: the &drm_gpuva to insert
2063 * @op: the &drm_gpuva_op_map to initialize @va with
2064 *
2065 * Initializes the @va from the @op and inserts it into the given @gpuvm.
2066 */
2067 void
drm_gpuva_map(struct drm_gpuvm * gpuvm,struct drm_gpuva * va,struct drm_gpuva_op_map * op)2068 drm_gpuva_map(struct drm_gpuvm *gpuvm,
2069 struct drm_gpuva *va,
2070 struct drm_gpuva_op_map *op)
2071 {
2072 drm_gpuva_init_from_op(va, op);
2073 drm_gpuva_insert(gpuvm, va);
2074 }
2075 EXPORT_SYMBOL_GPL(drm_gpuva_map);
2076
2077 /**
2078 * drm_gpuva_remap() - helper to remap a &drm_gpuva according to a
2079 * &drm_gpuva_op_remap
2080 * @prev: the &drm_gpuva to remap when keeping the start of a mapping
2081 * @next: the &drm_gpuva to remap when keeping the end of a mapping
2082 * @op: the &drm_gpuva_op_remap to initialize @prev and @next with
2083 *
2084 * Removes the currently mapped &drm_gpuva and remaps it using @prev and/or
2085 * @next.
2086 */
2087 void
drm_gpuva_remap(struct drm_gpuva * prev,struct drm_gpuva * next,struct drm_gpuva_op_remap * op)2088 drm_gpuva_remap(struct drm_gpuva *prev,
2089 struct drm_gpuva *next,
2090 struct drm_gpuva_op_remap *op)
2091 {
2092 struct drm_gpuva *va = op->unmap->va;
2093 struct drm_gpuvm *gpuvm = va->vm;
2094
2095 drm_gpuva_remove(va);
2096
2097 if (op->prev) {
2098 drm_gpuva_init_from_op(prev, op->prev);
2099 drm_gpuva_insert(gpuvm, prev);
2100 }
2101
2102 if (op->next) {
2103 drm_gpuva_init_from_op(next, op->next);
2104 drm_gpuva_insert(gpuvm, next);
2105 }
2106 }
2107 EXPORT_SYMBOL_GPL(drm_gpuva_remap);
2108
2109 /**
2110 * drm_gpuva_unmap() - helper to remove a &drm_gpuva according to a
2111 * &drm_gpuva_op_unmap
2112 * @op: the &drm_gpuva_op_unmap specifying the &drm_gpuva to remove
2113 *
2114 * Removes the &drm_gpuva associated with the &drm_gpuva_op_unmap.
2115 */
2116 void
drm_gpuva_unmap(struct drm_gpuva_op_unmap * op)2117 drm_gpuva_unmap(struct drm_gpuva_op_unmap *op)
2118 {
2119 drm_gpuva_remove(op->va);
2120 }
2121 EXPORT_SYMBOL_GPL(drm_gpuva_unmap);
2122
2123 static int
op_map_cb(const struct drm_gpuvm_ops * fn,void * priv,const struct drm_gpuvm_map_req * req)2124 op_map_cb(const struct drm_gpuvm_ops *fn, void *priv,
2125 const struct drm_gpuvm_map_req *req)
2126 {
2127 struct drm_gpuva_op op = {};
2128
2129 if (!req)
2130 return 0;
2131
2132 op.op = DRM_GPUVA_OP_MAP;
2133 op.map.va.addr = req->map.va.addr;
2134 op.map.va.range = req->map.va.range;
2135 op.map.gem.obj = req->map.gem.obj;
2136 op.map.gem.offset = req->map.gem.offset;
2137
2138 return fn->sm_step_map(&op, priv);
2139 }
2140
2141 static int
op_remap_cb(const struct drm_gpuvm_ops * fn,void * priv,struct drm_gpuva_op_map * prev,struct drm_gpuva_op_map * next,struct drm_gpuva_op_unmap * unmap)2142 op_remap_cb(const struct drm_gpuvm_ops *fn, void *priv,
2143 struct drm_gpuva_op_map *prev,
2144 struct drm_gpuva_op_map *next,
2145 struct drm_gpuva_op_unmap *unmap)
2146 {
2147 struct drm_gpuva_op op = {};
2148 struct drm_gpuva_op_remap *r;
2149
2150 op.op = DRM_GPUVA_OP_REMAP;
2151 r = &op.remap;
2152 r->prev = prev;
2153 r->next = next;
2154 r->unmap = unmap;
2155
2156 return fn->sm_step_remap(&op, priv);
2157 }
2158
2159 static int
op_unmap_cb(const struct drm_gpuvm_ops * fn,void * priv,struct drm_gpuva * va,bool merge,bool madvise)2160 op_unmap_cb(const struct drm_gpuvm_ops *fn, void *priv,
2161 struct drm_gpuva *va, bool merge, bool madvise)
2162 {
2163 struct drm_gpuva_op op = {};
2164
2165 if (madvise)
2166 return 0;
2167
2168 op.op = DRM_GPUVA_OP_UNMAP;
2169 op.unmap.va = va;
2170 op.unmap.keep = merge;
2171
2172 return fn->sm_step_unmap(&op, priv);
2173 }
2174
2175 static int
__drm_gpuvm_sm_map(struct drm_gpuvm * gpuvm,const struct drm_gpuvm_ops * ops,void * priv,const struct drm_gpuvm_map_req * req,bool madvise)2176 __drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm,
2177 const struct drm_gpuvm_ops *ops, void *priv,
2178 const struct drm_gpuvm_map_req *req,
2179 bool madvise)
2180 {
2181 struct drm_gem_object *req_obj = req->map.gem.obj;
2182 const struct drm_gpuvm_map_req *op_map = madvise ? NULL : req;
2183 struct drm_gpuva *va, *next;
2184 u64 req_offset = req->map.gem.offset;
2185 u64 req_range = req->map.va.range;
2186 u64 req_addr = req->map.va.addr;
2187 u64 req_end = req_addr + req_range;
2188 int ret;
2189
2190 if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range)))
2191 return -EINVAL;
2192
2193 drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) {
2194 struct drm_gem_object *obj = va->gem.obj;
2195 u64 offset = va->gem.offset;
2196 u64 addr = va->va.addr;
2197 u64 range = va->va.range;
2198 u64 end = addr + range;
2199 bool merge = !!va->gem.obj;
2200
2201 if (madvise && obj)
2202 continue;
2203
2204 if (addr == req_addr) {
2205 merge &= obj == req_obj &&
2206 offset == req_offset;
2207
2208 if (end == req_end) {
2209 ret = op_unmap_cb(ops, priv, va, merge, madvise);
2210 if (ret)
2211 return ret;
2212 break;
2213 }
2214
2215 if (end < req_end) {
2216 ret = op_unmap_cb(ops, priv, va, merge, madvise);
2217 if (ret)
2218 return ret;
2219 continue;
2220 }
2221
2222 if (end > req_end) {
2223 struct drm_gpuva_op_map n = {
2224 .va.addr = req_end,
2225 .va.range = range - req_range,
2226 .gem.obj = obj,
2227 .gem.offset = offset + req_range,
2228 };
2229 struct drm_gpuva_op_unmap u = {
2230 .va = va,
2231 .keep = merge,
2232 };
2233
2234 ret = op_remap_cb(ops, priv, NULL, &n, &u);
2235 if (ret)
2236 return ret;
2237
2238 if (madvise)
2239 op_map = req;
2240 break;
2241 }
2242 } else if (addr < req_addr) {
2243 u64 ls_range = req_addr - addr;
2244 struct drm_gpuva_op_map p = {
2245 .va.addr = addr,
2246 .va.range = ls_range,
2247 .gem.obj = obj,
2248 .gem.offset = offset,
2249 };
2250 struct drm_gpuva_op_unmap u = { .va = va };
2251
2252 merge &= obj == req_obj &&
2253 offset + ls_range == req_offset;
2254 u.keep = merge;
2255
2256 if (end == req_end) {
2257 ret = op_remap_cb(ops, priv, &p, NULL, &u);
2258 if (ret)
2259 return ret;
2260
2261 if (madvise)
2262 op_map = req;
2263 break;
2264 }
2265
2266 if (end < req_end) {
2267 ret = op_remap_cb(ops, priv, &p, NULL, &u);
2268 if (ret)
2269 return ret;
2270
2271 if (madvise) {
2272 struct drm_gpuvm_map_req map_req = {
2273 .map.va.addr = req_addr,
2274 .map.va.range = end - req_addr,
2275 };
2276
2277 ret = op_map_cb(ops, priv, &map_req);
2278 if (ret)
2279 return ret;
2280 }
2281
2282 continue;
2283 }
2284
2285 if (end > req_end) {
2286 struct drm_gpuva_op_map n = {
2287 .va.addr = req_end,
2288 .va.range = end - req_end,
2289 .gem.obj = obj,
2290 .gem.offset = offset + ls_range +
2291 req_range,
2292 };
2293
2294 ret = op_remap_cb(ops, priv, &p, &n, &u);
2295 if (ret)
2296 return ret;
2297
2298 if (madvise)
2299 op_map = req;
2300 break;
2301 }
2302 } else if (addr > req_addr) {
2303 merge &= obj == req_obj &&
2304 offset == req_offset +
2305 (addr - req_addr);
2306
2307 if (end == req_end) {
2308 ret = op_unmap_cb(ops, priv, va, merge, madvise);
2309 if (ret)
2310 return ret;
2311
2312 break;
2313 }
2314
2315 if (end < req_end) {
2316 ret = op_unmap_cb(ops, priv, va, merge, madvise);
2317 if (ret)
2318 return ret;
2319
2320 continue;
2321 }
2322
2323 if (end > req_end) {
2324 struct drm_gpuva_op_map n = {
2325 .va.addr = req_end,
2326 .va.range = end - req_end,
2327 .gem.obj = obj,
2328 .gem.offset = offset + req_end - addr,
2329 };
2330 struct drm_gpuva_op_unmap u = {
2331 .va = va,
2332 .keep = merge,
2333 };
2334
2335 ret = op_remap_cb(ops, priv, NULL, &n, &u);
2336 if (ret)
2337 return ret;
2338
2339 if (madvise) {
2340 struct drm_gpuvm_map_req map_req = {
2341 .map.va.addr = addr,
2342 .map.va.range = req_end - addr,
2343 };
2344
2345 return op_map_cb(ops, priv, &map_req);
2346 }
2347 break;
2348 }
2349 }
2350 }
2351 return op_map_cb(ops, priv, op_map);
2352 }
2353
2354 static int
__drm_gpuvm_sm_unmap(struct drm_gpuvm * gpuvm,const struct drm_gpuvm_ops * ops,void * priv,u64 req_addr,u64 req_range)2355 __drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm,
2356 const struct drm_gpuvm_ops *ops, void *priv,
2357 u64 req_addr, u64 req_range)
2358 {
2359 struct drm_gpuva *va, *next;
2360 u64 req_end = req_addr + req_range;
2361 int ret;
2362
2363 if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range)))
2364 return -EINVAL;
2365
2366 drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) {
2367 struct drm_gpuva_op_map prev = {}, next = {};
2368 bool prev_split = false, next_split = false;
2369 struct drm_gem_object *obj = va->gem.obj;
2370 u64 offset = va->gem.offset;
2371 u64 addr = va->va.addr;
2372 u64 range = va->va.range;
2373 u64 end = addr + range;
2374
2375 if (addr < req_addr) {
2376 prev.va.addr = addr;
2377 prev.va.range = req_addr - addr;
2378 prev.gem.obj = obj;
2379 prev.gem.offset = offset;
2380
2381 prev_split = true;
2382 }
2383
2384 if (end > req_end) {
2385 next.va.addr = req_end;
2386 next.va.range = end - req_end;
2387 next.gem.obj = obj;
2388 next.gem.offset = offset + (req_end - addr);
2389
2390 next_split = true;
2391 }
2392
2393 if (prev_split || next_split) {
2394 struct drm_gpuva_op_unmap unmap = { .va = va };
2395
2396 ret = op_remap_cb(ops, priv,
2397 prev_split ? &prev : NULL,
2398 next_split ? &next : NULL,
2399 &unmap);
2400 if (ret)
2401 return ret;
2402 } else {
2403 ret = op_unmap_cb(ops, priv, va, false, false);
2404 if (ret)
2405 return ret;
2406 }
2407 }
2408
2409 return 0;
2410 }
2411
2412 /**
2413 * drm_gpuvm_sm_map() - calls the &drm_gpuva_op split/merge steps
2414 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2415 * @priv: pointer to a driver private data structure
2416 * @req: ptr to struct drm_gpuvm_map_req
2417 *
2418 * This function iterates the given range of the GPU VA space. It utilizes the
2419 * &drm_gpuvm_ops to call back into the driver providing the split and merge
2420 * steps.
2421 *
2422 * Drivers may use these callbacks to update the GPU VA space right away within
2423 * the callback. In case the driver decides to copy and store the operations for
2424 * later processing neither this function nor &drm_gpuvm_sm_unmap is allowed to
2425 * be called before the &drm_gpuvm's view of the GPU VA space was
2426 * updated with the previous set of operations. To update the
2427 * &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
2428 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
2429 * used.
2430 *
2431 * A sequence of callbacks can contain map, unmap and remap operations, but
2432 * the sequence of callbacks might also be empty if no operation is required,
2433 * e.g. if the requested mapping already exists in the exact same way.
2434 *
2435 * There can be an arbitrary amount of unmap operations, a maximum of two remap
2436 * operations and a single map operation. The latter one represents the original
2437 * map operation requested by the caller.
2438 *
2439 * Returns: 0 on success or a negative error code
2440 */
2441 int
drm_gpuvm_sm_map(struct drm_gpuvm * gpuvm,void * priv,const struct drm_gpuvm_map_req * req)2442 drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,
2443 const struct drm_gpuvm_map_req *req)
2444 {
2445 const struct drm_gpuvm_ops *ops = gpuvm->ops;
2446
2447 if (unlikely(!(ops && ops->sm_step_map &&
2448 ops->sm_step_remap &&
2449 ops->sm_step_unmap)))
2450 return -EINVAL;
2451
2452 return __drm_gpuvm_sm_map(gpuvm, ops, priv, req, false);
2453 }
2454 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map);
2455
2456 /**
2457 * drm_gpuvm_sm_unmap() - calls the &drm_gpuva_ops to split on unmap
2458 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2459 * @priv: pointer to a driver private data structure
2460 * @req_addr: the start address of the range to unmap
2461 * @req_range: the range of the mappings to unmap
2462 *
2463 * This function iterates the given range of the GPU VA space. It utilizes the
2464 * &drm_gpuvm_ops to call back into the driver providing the operations to
2465 * unmap and, if required, split existing mappings.
2466 *
2467 * Drivers may use these callbacks to update the GPU VA space right away within
2468 * the callback. In case the driver decides to copy and store the operations for
2469 * later processing neither this function nor &drm_gpuvm_sm_map is allowed to be
2470 * called before the &drm_gpuvm's view of the GPU VA space was updated
2471 * with the previous set of operations. To update the &drm_gpuvm's view
2472 * of the GPU VA space drm_gpuva_insert(), drm_gpuva_destroy_locked() and/or
2473 * drm_gpuva_destroy_unlocked() should be used.
2474 *
2475 * A sequence of callbacks can contain unmap and remap operations, depending on
2476 * whether there are actual overlapping mappings to split.
2477 *
2478 * There can be an arbitrary amount of unmap operations and a maximum of two
2479 * remap operations.
2480 *
2481 * Returns: 0 on success or a negative error code
2482 */
2483 int
drm_gpuvm_sm_unmap(struct drm_gpuvm * gpuvm,void * priv,u64 req_addr,u64 req_range)2484 drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv,
2485 u64 req_addr, u64 req_range)
2486 {
2487 const struct drm_gpuvm_ops *ops = gpuvm->ops;
2488
2489 if (unlikely(!(ops && ops->sm_step_remap &&
2490 ops->sm_step_unmap)))
2491 return -EINVAL;
2492
2493 return __drm_gpuvm_sm_unmap(gpuvm, ops, priv,
2494 req_addr, req_range);
2495 }
2496 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap);
2497
2498 static int
drm_gpuva_sm_step_lock(struct drm_gpuva_op * op,void * priv)2499 drm_gpuva_sm_step_lock(struct drm_gpuva_op *op, void *priv)
2500 {
2501 struct drm_exec *exec = priv;
2502
2503 switch (op->op) {
2504 case DRM_GPUVA_OP_REMAP:
2505 if (op->remap.unmap->va->gem.obj)
2506 return drm_exec_lock_obj(exec, op->remap.unmap->va->gem.obj);
2507 return 0;
2508 case DRM_GPUVA_OP_UNMAP:
2509 if (op->unmap.va->gem.obj)
2510 return drm_exec_lock_obj(exec, op->unmap.va->gem.obj);
2511 return 0;
2512 default:
2513 return 0;
2514 }
2515 }
2516
2517 static const struct drm_gpuvm_ops lock_ops = {
2518 .sm_step_map = drm_gpuva_sm_step_lock,
2519 .sm_step_remap = drm_gpuva_sm_step_lock,
2520 .sm_step_unmap = drm_gpuva_sm_step_lock,
2521 };
2522
2523 /**
2524 * drm_gpuvm_sm_map_exec_lock() - locks the objects touched by a drm_gpuvm_sm_map()
2525 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2526 * @exec: the &drm_exec locking context
2527 * @num_fences: for newly mapped objects, the # of fences to reserve
2528 * @req: ptr to drm_gpuvm_map_req struct
2529 *
2530 * This function locks (drm_exec_lock_obj()) objects that will be unmapped/
2531 * remapped, and locks+prepares (drm_exec_prepare_object()) objects that
2532 * will be newly mapped.
2533 *
2534 * The expected usage is::
2535 *
2536 * vm_bind {
2537 * struct drm_exec exec;
2538 *
2539 * // IGNORE_DUPLICATES is required, INTERRUPTIBLE_WAIT is recommended:
2540 * drm_exec_init(&exec, IGNORE_DUPLICATES | INTERRUPTIBLE_WAIT, 0);
2541 *
2542 * drm_exec_until_all_locked (&exec) {
2543 * for_each_vm_bind_operation {
2544 * switch (op->op) {
2545 * case DRIVER_OP_UNMAP:
2546 * ret = drm_gpuvm_sm_unmap_exec_lock(gpuvm, &exec, op->addr, op->range);
2547 * break;
2548 * case DRIVER_OP_MAP:
2549 * ret = drm_gpuvm_sm_map_exec_lock(gpuvm, &exec, num_fences, &req);
2550 * break;
2551 * }
2552 *
2553 * drm_exec_retry_on_contention(&exec);
2554 * if (ret)
2555 * return ret;
2556 * }
2557 * }
2558 * }
2559 *
2560 * This enables all locking to be performed before the driver begins modifying
2561 * the VM. This is safe to do in the case of overlapping DRIVER_VM_BIND_OPs,
2562 * where an earlier op can alter the sequence of steps generated for a later
2563 * op, because the later altered step will involve the same GEM object(s)
2564 * already seen in the earlier locking step. For example:
2565 *
2566 * 1) An earlier driver DRIVER_OP_UNMAP op removes the need for a
2567 * DRM_GPUVA_OP_REMAP/UNMAP step. This is safe because we've already
2568 * locked the GEM object in the earlier DRIVER_OP_UNMAP op.
2569 *
2570 * 2) An earlier DRIVER_OP_MAP op overlaps with a later DRIVER_OP_MAP/UNMAP
2571 * op, introducing a DRM_GPUVA_OP_REMAP/UNMAP that wouldn't have been
2572 * required without the earlier DRIVER_OP_MAP. This is safe because we've
2573 * already locked the GEM object in the earlier DRIVER_OP_MAP step.
2574 *
2575 * Returns: 0 on success or a negative error code
2576 */
2577 int
drm_gpuvm_sm_map_exec_lock(struct drm_gpuvm * gpuvm,struct drm_exec * exec,unsigned int num_fences,struct drm_gpuvm_map_req * req)2578 drm_gpuvm_sm_map_exec_lock(struct drm_gpuvm *gpuvm,
2579 struct drm_exec *exec, unsigned int num_fences,
2580 struct drm_gpuvm_map_req *req)
2581 {
2582 struct drm_gem_object *req_obj = req->map.gem.obj;
2583
2584 if (req_obj) {
2585 int ret = drm_exec_prepare_obj(exec, req_obj, num_fences);
2586 if (ret)
2587 return ret;
2588 }
2589
2590 return __drm_gpuvm_sm_map(gpuvm, &lock_ops, exec, req, false);
2591
2592 }
2593 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map_exec_lock);
2594
2595 /**
2596 * drm_gpuvm_sm_unmap_exec_lock() - locks the objects touched by drm_gpuvm_sm_unmap()
2597 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2598 * @exec: the &drm_exec locking context
2599 * @req_addr: the start address of the range to unmap
2600 * @req_range: the range of the mappings to unmap
2601 *
2602 * This function locks (drm_exec_lock_obj()) objects that will be unmapped/
2603 * remapped by drm_gpuvm_sm_unmap().
2604 *
2605 * See drm_gpuvm_sm_map_exec_lock() for expected usage.
2606 *
2607 * Returns: 0 on success or a negative error code
2608 */
2609 int
drm_gpuvm_sm_unmap_exec_lock(struct drm_gpuvm * gpuvm,struct drm_exec * exec,u64 req_addr,u64 req_range)2610 drm_gpuvm_sm_unmap_exec_lock(struct drm_gpuvm *gpuvm, struct drm_exec *exec,
2611 u64 req_addr, u64 req_range)
2612 {
2613 return __drm_gpuvm_sm_unmap(gpuvm, &lock_ops, exec,
2614 req_addr, req_range);
2615 }
2616 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap_exec_lock);
2617
2618 static struct drm_gpuva_op *
gpuva_op_alloc(struct drm_gpuvm * gpuvm)2619 gpuva_op_alloc(struct drm_gpuvm *gpuvm)
2620 {
2621 const struct drm_gpuvm_ops *fn = gpuvm->ops;
2622 struct drm_gpuva_op *op;
2623
2624 if (fn && fn->op_alloc)
2625 op = fn->op_alloc();
2626 else
2627 op = kzalloc(sizeof(*op), GFP_KERNEL);
2628
2629 if (unlikely(!op))
2630 return NULL;
2631
2632 return op;
2633 }
2634
2635 static void
gpuva_op_free(struct drm_gpuvm * gpuvm,struct drm_gpuva_op * op)2636 gpuva_op_free(struct drm_gpuvm *gpuvm,
2637 struct drm_gpuva_op *op)
2638 {
2639 const struct drm_gpuvm_ops *fn = gpuvm->ops;
2640
2641 if (fn && fn->op_free)
2642 fn->op_free(op);
2643 else
2644 kfree(op);
2645 }
2646
2647 static int
drm_gpuva_sm_step(struct drm_gpuva_op * __op,void * priv)2648 drm_gpuva_sm_step(struct drm_gpuva_op *__op,
2649 void *priv)
2650 {
2651 struct {
2652 struct drm_gpuvm *vm;
2653 struct drm_gpuva_ops *ops;
2654 } *args = priv;
2655 struct drm_gpuvm *gpuvm = args->vm;
2656 struct drm_gpuva_ops *ops = args->ops;
2657 struct drm_gpuva_op *op;
2658
2659 op = gpuva_op_alloc(gpuvm);
2660 if (unlikely(!op))
2661 goto err;
2662
2663 memcpy(op, __op, sizeof(*op));
2664
2665 if (op->op == DRM_GPUVA_OP_REMAP) {
2666 struct drm_gpuva_op_remap *__r = &__op->remap;
2667 struct drm_gpuva_op_remap *r = &op->remap;
2668
2669 r->unmap = kmemdup(__r->unmap, sizeof(*r->unmap),
2670 GFP_KERNEL);
2671 if (unlikely(!r->unmap))
2672 goto err_free_op;
2673
2674 if (__r->prev) {
2675 r->prev = kmemdup(__r->prev, sizeof(*r->prev),
2676 GFP_KERNEL);
2677 if (unlikely(!r->prev))
2678 goto err_free_unmap;
2679 }
2680
2681 if (__r->next) {
2682 r->next = kmemdup(__r->next, sizeof(*r->next),
2683 GFP_KERNEL);
2684 if (unlikely(!r->next))
2685 goto err_free_prev;
2686 }
2687 }
2688
2689 list_add_tail(&op->entry, &ops->list);
2690
2691 return 0;
2692
2693 err_free_unmap:
2694 kfree(op->remap.unmap);
2695 err_free_prev:
2696 kfree(op->remap.prev);
2697 err_free_op:
2698 gpuva_op_free(gpuvm, op);
2699 err:
2700 return -ENOMEM;
2701 }
2702
2703 static const struct drm_gpuvm_ops gpuvm_list_ops = {
2704 .sm_step_map = drm_gpuva_sm_step,
2705 .sm_step_remap = drm_gpuva_sm_step,
2706 .sm_step_unmap = drm_gpuva_sm_step,
2707 };
2708
2709 static struct drm_gpuva_ops *
__drm_gpuvm_sm_map_ops_create(struct drm_gpuvm * gpuvm,const struct drm_gpuvm_map_req * req,bool madvise)2710 __drm_gpuvm_sm_map_ops_create(struct drm_gpuvm *gpuvm,
2711 const struct drm_gpuvm_map_req *req,
2712 bool madvise)
2713 {
2714 struct drm_gpuva_ops *ops;
2715 struct {
2716 struct drm_gpuvm *vm;
2717 struct drm_gpuva_ops *ops;
2718 } args;
2719 int ret;
2720
2721 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
2722 if (unlikely(!ops))
2723 return ERR_PTR(-ENOMEM);
2724
2725 INIT_LIST_HEAD(&ops->list);
2726
2727 args.vm = gpuvm;
2728 args.ops = ops;
2729
2730 ret = __drm_gpuvm_sm_map(gpuvm, &gpuvm_list_ops, &args, req, madvise);
2731 if (ret)
2732 goto err_free_ops;
2733
2734 return ops;
2735
2736 err_free_ops:
2737 drm_gpuva_ops_free(gpuvm, ops);
2738 return ERR_PTR(ret);
2739 }
2740
2741 /**
2742 * drm_gpuvm_sm_map_ops_create() - creates the &drm_gpuva_ops to split and merge
2743 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2744 * @req: map request arguments
2745 *
2746 * This function creates a list of operations to perform splitting and merging
2747 * of existing mapping(s) with the newly requested one.
2748 *
2749 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
2750 * in the given order. It can contain map, unmap and remap operations, but it
2751 * also can be empty if no operation is required, e.g. if the requested mapping
2752 * already exists in the exact same way.
2753 *
2754 * There can be an arbitrary amount of unmap operations, a maximum of two remap
2755 * operations and a single map operation. The latter one represents the original
2756 * map operation requested by the caller.
2757 *
2758 * Note that before calling this function again with another mapping request it
2759 * is necessary to update the &drm_gpuvm's view of the GPU VA space. The
2760 * previously obtained operations must be either processed or abandoned. To
2761 * update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
2762 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
2763 * used.
2764 *
2765 * After the caller finished processing the returned &drm_gpuva_ops, they must
2766 * be freed with &drm_gpuva_ops_free.
2767 *
2768 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2769 */
2770 struct drm_gpuva_ops *
drm_gpuvm_sm_map_ops_create(struct drm_gpuvm * gpuvm,const struct drm_gpuvm_map_req * req)2771 drm_gpuvm_sm_map_ops_create(struct drm_gpuvm *gpuvm,
2772 const struct drm_gpuvm_map_req *req)
2773 {
2774 return __drm_gpuvm_sm_map_ops_create(gpuvm, req, false);
2775 }
2776 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map_ops_create);
2777
2778 /**
2779 * drm_gpuvm_madvise_ops_create() - creates the &drm_gpuva_ops to split
2780 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2781 * @req: map request arguments
2782 *
2783 * This function creates a list of operations to perform splitting
2784 * of existent mapping(s) at start or end, based on the request map.
2785 *
2786 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
2787 * in the given order. It can contain map and remap operations, but it
2788 * also can be empty if no operation is required, e.g. if the requested mapping
2789 * already exists is the exact same way.
2790 *
2791 * There will be no unmap operations, a maximum of two remap operations and two
2792 * map operations. The two map operations correspond to: one from start to the
2793 * end of drm_gpuvaX, and another from the start of drm_gpuvaY to end.
2794 *
2795 * Note that before calling this function again with another mapping request it
2796 * is necessary to update the &drm_gpuvm's view of the GPU VA space. The
2797 * previously obtained operations must be either processed or abandoned. To
2798 * update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
2799 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
2800 * used.
2801 *
2802 * After the caller finished processing the returned &drm_gpuva_ops, they must
2803 * be freed with &drm_gpuva_ops_free.
2804 *
2805 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2806 */
2807 struct drm_gpuva_ops *
drm_gpuvm_madvise_ops_create(struct drm_gpuvm * gpuvm,const struct drm_gpuvm_map_req * req)2808 drm_gpuvm_madvise_ops_create(struct drm_gpuvm *gpuvm,
2809 const struct drm_gpuvm_map_req *req)
2810 {
2811 return __drm_gpuvm_sm_map_ops_create(gpuvm, req, true);
2812 }
2813 EXPORT_SYMBOL_GPL(drm_gpuvm_madvise_ops_create);
2814
2815 /**
2816 * drm_gpuvm_sm_unmap_ops_create() - creates the &drm_gpuva_ops to split on
2817 * unmap
2818 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2819 * @req_addr: the start address of the range to unmap
2820 * @req_range: the range of the mappings to unmap
2821 *
2822 * This function creates a list of operations to perform unmapping and, if
2823 * required, splitting of the mappings overlapping the unmap range.
2824 *
2825 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
2826 * in the given order. It can contain unmap and remap operations, depending on
2827 * whether there are actual overlapping mappings to split.
2828 *
2829 * There can be an arbitrary amount of unmap operations and a maximum of two
2830 * remap operations.
2831 *
2832 * Note that before calling this function again with another range to unmap it
2833 * is necessary to update the &drm_gpuvm's view of the GPU VA space. The
2834 * previously obtained operations must be processed or abandoned. To update the
2835 * &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
2836 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
2837 * used.
2838 *
2839 * After the caller finished processing the returned &drm_gpuva_ops, they must
2840 * be freed with &drm_gpuva_ops_free.
2841 *
2842 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2843 */
2844 struct drm_gpuva_ops *
drm_gpuvm_sm_unmap_ops_create(struct drm_gpuvm * gpuvm,u64 req_addr,u64 req_range)2845 drm_gpuvm_sm_unmap_ops_create(struct drm_gpuvm *gpuvm,
2846 u64 req_addr, u64 req_range)
2847 {
2848 struct drm_gpuva_ops *ops;
2849 struct {
2850 struct drm_gpuvm *vm;
2851 struct drm_gpuva_ops *ops;
2852 } args;
2853 int ret;
2854
2855 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
2856 if (unlikely(!ops))
2857 return ERR_PTR(-ENOMEM);
2858
2859 INIT_LIST_HEAD(&ops->list);
2860
2861 args.vm = gpuvm;
2862 args.ops = ops;
2863
2864 ret = __drm_gpuvm_sm_unmap(gpuvm, &gpuvm_list_ops, &args,
2865 req_addr, req_range);
2866 if (ret)
2867 goto err_free_ops;
2868
2869 return ops;
2870
2871 err_free_ops:
2872 drm_gpuva_ops_free(gpuvm, ops);
2873 return ERR_PTR(ret);
2874 }
2875 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap_ops_create);
2876
2877 /**
2878 * drm_gpuvm_prefetch_ops_create() - creates the &drm_gpuva_ops to prefetch
2879 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2880 * @addr: the start address of the range to prefetch
2881 * @range: the range of the mappings to prefetch
2882 *
2883 * This function creates a list of operations to perform prefetching.
2884 *
2885 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
2886 * in the given order. It can contain prefetch operations.
2887 *
2888 * There can be an arbitrary amount of prefetch operations.
2889 *
2890 * After the caller finished processing the returned &drm_gpuva_ops, they must
2891 * be freed with &drm_gpuva_ops_free.
2892 *
2893 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2894 */
2895 struct drm_gpuva_ops *
drm_gpuvm_prefetch_ops_create(struct drm_gpuvm * gpuvm,u64 addr,u64 range)2896 drm_gpuvm_prefetch_ops_create(struct drm_gpuvm *gpuvm,
2897 u64 addr, u64 range)
2898 {
2899 struct drm_gpuva_ops *ops;
2900 struct drm_gpuva_op *op;
2901 struct drm_gpuva *va;
2902 u64 end = addr + range;
2903 int ret;
2904
2905 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
2906 if (!ops)
2907 return ERR_PTR(-ENOMEM);
2908
2909 INIT_LIST_HEAD(&ops->list);
2910
2911 drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) {
2912 op = gpuva_op_alloc(gpuvm);
2913 if (!op) {
2914 ret = -ENOMEM;
2915 goto err_free_ops;
2916 }
2917
2918 op->op = DRM_GPUVA_OP_PREFETCH;
2919 op->prefetch.va = va;
2920 list_add_tail(&op->entry, &ops->list);
2921 }
2922
2923 return ops;
2924
2925 err_free_ops:
2926 drm_gpuva_ops_free(gpuvm, ops);
2927 return ERR_PTR(ret);
2928 }
2929 EXPORT_SYMBOL_GPL(drm_gpuvm_prefetch_ops_create);
2930
2931 /**
2932 * drm_gpuvm_bo_unmap_ops_create() - creates the &drm_gpuva_ops to unmap a GEM
2933 * @vm_bo: the &drm_gpuvm_bo abstraction
2934 *
2935 * This function creates a list of operations to perform unmapping for every
2936 * GPUVA attached to a GEM.
2937 *
2938 * The list can be iterated with &drm_gpuva_for_each_op and consists out of an
2939 * arbitrary amount of unmap operations.
2940 *
2941 * After the caller finished processing the returned &drm_gpuva_ops, they must
2942 * be freed with &drm_gpuva_ops_free.
2943 *
2944 * This function expects the caller to protect the GEM's GPUVA list against
2945 * concurrent access using either the GEM's dma-resv or gpuva.lock mutex.
2946 *
2947 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2948 */
2949 struct drm_gpuva_ops *
drm_gpuvm_bo_unmap_ops_create(struct drm_gpuvm_bo * vm_bo)2950 drm_gpuvm_bo_unmap_ops_create(struct drm_gpuvm_bo *vm_bo)
2951 {
2952 struct drm_gpuva_ops *ops;
2953 struct drm_gpuva_op *op;
2954 struct drm_gpuva *va;
2955 int ret;
2956
2957 drm_gem_gpuva_assert_lock_held(vm_bo->vm, vm_bo->obj);
2958
2959 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
2960 if (!ops)
2961 return ERR_PTR(-ENOMEM);
2962
2963 INIT_LIST_HEAD(&ops->list);
2964
2965 drm_gpuvm_bo_for_each_va(va, vm_bo) {
2966 op = gpuva_op_alloc(vm_bo->vm);
2967 if (!op) {
2968 ret = -ENOMEM;
2969 goto err_free_ops;
2970 }
2971
2972 op->op = DRM_GPUVA_OP_UNMAP;
2973 op->unmap.va = va;
2974 list_add_tail(&op->entry, &ops->list);
2975 }
2976
2977 return ops;
2978
2979 err_free_ops:
2980 drm_gpuva_ops_free(vm_bo->vm, ops);
2981 return ERR_PTR(ret);
2982 }
2983 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_unmap_ops_create);
2984
2985 /**
2986 * drm_gpuva_ops_free() - free the given &drm_gpuva_ops
2987 * @gpuvm: the &drm_gpuvm the ops were created for
2988 * @ops: the &drm_gpuva_ops to free
2989 *
2990 * Frees the given &drm_gpuva_ops structure including all the ops associated
2991 * with it.
2992 */
2993 void
drm_gpuva_ops_free(struct drm_gpuvm * gpuvm,struct drm_gpuva_ops * ops)2994 drm_gpuva_ops_free(struct drm_gpuvm *gpuvm,
2995 struct drm_gpuva_ops *ops)
2996 {
2997 struct drm_gpuva_op *op, *next;
2998
2999 drm_gpuva_for_each_op_safe(op, next, ops) {
3000 list_del(&op->entry);
3001
3002 if (op->op == DRM_GPUVA_OP_REMAP) {
3003 kfree(op->remap.prev);
3004 kfree(op->remap.next);
3005 kfree(op->remap.unmap);
3006 }
3007
3008 gpuva_op_free(gpuvm, op);
3009 }
3010
3011 kfree(ops);
3012 }
3013 EXPORT_SYMBOL_GPL(drm_gpuva_ops_free);
3014
3015 MODULE_DESCRIPTION("DRM GPUVM");
3016 MODULE_LICENSE("GPL");
3017