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