xref: /linux/drivers/gpu/drm/nouveau/nouveau_uvmm.c (revision a7e43c0a1a70e0d71fdda5d315927e1b3bc7e8d4)
1 // SPDX-License-Identifier: MIT
2 
3 /*
4  * Locking:
5  *
6  * The uvmm mutex protects any operations on the GPU VA space provided by the
7  * DRM GPU VA manager.
8  *
9  * The GEMs dma_resv lock protects the GEMs GPUVA list, hence link/unlink of a
10  * mapping to it's backing GEM must be performed under this lock.
11  *
12  * Actual map/unmap operations within the fence signalling critical path are
13  * protected by installing DMA fences to the corresponding GEMs DMA
14  * reservations, such that concurrent BO moves, which itself walk the GEMs GPUVA
15  * list in order to map/unmap it's entries, can't occur concurrently.
16  *
17  * Accessing the DRM_GPUVA_INVALIDATED flag doesn't need any separate
18  * protection, since there are no accesses other than from BO move callbacks
19  * and from the fence signalling critical path, which are already protected by
20  * the corresponding GEMs DMA reservation fence.
21  */
22 
23 #include "nouveau_drv.h"
24 #include "nouveau_gem.h"
25 #include "nouveau_mem.h"
26 #include "nouveau_uvmm.h"
27 
28 #include <nvif/vmm.h>
29 #include <nvif/mem.h>
30 
31 #include <nvif/class.h>
32 #include <nvif/if000c.h>
33 #include <nvif/if900d.h>
34 
35 #define NOUVEAU_VA_SPACE_BITS		47 /* FIXME */
36 #define NOUVEAU_VA_SPACE_START		0x0
37 #define NOUVEAU_VA_SPACE_END		(1ULL << NOUVEAU_VA_SPACE_BITS)
38 
39 #define list_last_op(_ops) list_last_entry(_ops, struct bind_job_op, entry)
40 #define list_prev_op(_op) list_prev_entry(_op, entry)
41 #define list_for_each_op(_op, _ops) list_for_each_entry(_op, _ops, entry)
42 #define list_for_each_op_from_reverse(_op, _ops) \
43 	list_for_each_entry_from_reverse(_op, _ops, entry)
44 #define list_for_each_op_safe(_op, _n, _ops) list_for_each_entry_safe(_op, _n, _ops, entry)
45 
46 enum vm_bind_op {
47 	OP_MAP = DRM_NOUVEAU_VM_BIND_OP_MAP,
48 	OP_UNMAP = DRM_NOUVEAU_VM_BIND_OP_UNMAP,
49 	OP_MAP_SPARSE,
50 	OP_UNMAP_SPARSE,
51 };
52 
53 struct nouveau_uvma_prealloc {
54 	struct nouveau_uvma *map;
55 	struct nouveau_uvma *prev;
56 	struct nouveau_uvma *next;
57 };
58 
59 struct bind_job_op {
60 	struct list_head entry;
61 
62 	enum vm_bind_op op;
63 	u32 flags;
64 
65 	struct drm_gpuvm_bo *vm_bo;
66 
67 	struct {
68 		u64 addr;
69 		u64 range;
70 	} va;
71 
72 	struct {
73 		u32 handle;
74 		u64 offset;
75 		struct drm_gem_object *obj;
76 	} gem;
77 
78 	struct nouveau_uvma_region *reg;
79 	struct nouveau_uvma_prealloc new;
80 	struct drm_gpuva_ops *ops;
81 };
82 
83 struct uvmm_map_args {
84 	struct nouveau_uvma_region *region;
85 	u64 addr;
86 	u64 range;
87 	u8 kind;
88 };
89 
90 static int
91 nouveau_uvmm_vmm_sparse_ref(struct nouveau_uvmm *uvmm,
92 			    u64 addr, u64 range)
93 {
94 	struct nvif_vmm *vmm = &uvmm->vmm.vmm;
95 
96 	return nvif_vmm_raw_sparse(vmm, addr, range, true);
97 }
98 
99 static int
100 nouveau_uvmm_vmm_sparse_unref(struct nouveau_uvmm *uvmm,
101 			      u64 addr, u64 range)
102 {
103 	struct nvif_vmm *vmm = &uvmm->vmm.vmm;
104 
105 	return nvif_vmm_raw_sparse(vmm, addr, range, false);
106 }
107 
108 static int
109 nouveau_uvmm_vmm_get(struct nouveau_uvmm *uvmm,
110 		     u64 addr, u64 range)
111 {
112 	struct nvif_vmm *vmm = &uvmm->vmm.vmm;
113 
114 	return nvif_vmm_raw_get(vmm, addr, range, PAGE_SHIFT);
115 }
116 
117 static int
118 nouveau_uvmm_vmm_put(struct nouveau_uvmm *uvmm,
119 		     u64 addr, u64 range)
120 {
121 	struct nvif_vmm *vmm = &uvmm->vmm.vmm;
122 
123 	return nvif_vmm_raw_put(vmm, addr, range, PAGE_SHIFT);
124 }
125 
126 static int
127 nouveau_uvmm_vmm_unmap(struct nouveau_uvmm *uvmm,
128 		       u64 addr, u64 range, bool sparse)
129 {
130 	struct nvif_vmm *vmm = &uvmm->vmm.vmm;
131 
132 	return nvif_vmm_raw_unmap(vmm, addr, range, PAGE_SHIFT, sparse);
133 }
134 
135 static int
136 nouveau_uvmm_vmm_map(struct nouveau_uvmm *uvmm,
137 		     u64 addr, u64 range,
138 		     u64 bo_offset, u8 kind,
139 		     struct nouveau_mem *mem)
140 {
141 	struct nvif_vmm *vmm = &uvmm->vmm.vmm;
142 	union {
143 		struct gf100_vmm_map_v0 gf100;
144 	} args;
145 	u32 argc = 0;
146 
147 	switch (vmm->object.oclass) {
148 	case NVIF_CLASS_VMM_GF100:
149 	case NVIF_CLASS_VMM_GM200:
150 	case NVIF_CLASS_VMM_GP100:
151 		args.gf100.version = 0;
152 		if (mem->mem.type & NVIF_MEM_VRAM)
153 			args.gf100.vol = 0;
154 		else
155 			args.gf100.vol = 1;
156 		args.gf100.ro = 0;
157 		args.gf100.priv = 0;
158 		args.gf100.kind = kind;
159 		argc = sizeof(args.gf100);
160 		break;
161 	default:
162 		WARN_ON(1);
163 		return -ENOSYS;
164 	}
165 
166 	return nvif_vmm_raw_map(vmm, addr, range, PAGE_SHIFT,
167 				&args, argc,
168 				&mem->mem, bo_offset);
169 }
170 
171 static int
172 nouveau_uvma_region_sparse_unref(struct nouveau_uvma_region *reg)
173 {
174 	u64 addr = reg->va.addr;
175 	u64 range = reg->va.range;
176 
177 	return nouveau_uvmm_vmm_sparse_unref(reg->uvmm, addr, range);
178 }
179 
180 static int
181 nouveau_uvma_vmm_put(struct nouveau_uvma *uvma)
182 {
183 	u64 addr = uvma->va.va.addr;
184 	u64 range = uvma->va.va.range;
185 
186 	return nouveau_uvmm_vmm_put(to_uvmm(uvma), addr, range);
187 }
188 
189 static int
190 nouveau_uvma_map(struct nouveau_uvma *uvma,
191 		 struct nouveau_mem *mem)
192 {
193 	u64 addr = uvma->va.va.addr;
194 	u64 offset = uvma->va.gem.offset;
195 	u64 range = uvma->va.va.range;
196 
197 	return nouveau_uvmm_vmm_map(to_uvmm(uvma), addr, range,
198 				    offset, uvma->kind, mem);
199 }
200 
201 static int
202 nouveau_uvma_unmap(struct nouveau_uvma *uvma)
203 {
204 	u64 addr = uvma->va.va.addr;
205 	u64 range = uvma->va.va.range;
206 	bool sparse = !!uvma->region;
207 
208 	if (drm_gpuva_invalidated(&uvma->va))
209 		return 0;
210 
211 	return nouveau_uvmm_vmm_unmap(to_uvmm(uvma), addr, range, sparse);
212 }
213 
214 static int
215 nouveau_uvma_alloc(struct nouveau_uvma **puvma)
216 {
217 	*puvma = kzalloc(sizeof(**puvma), GFP_KERNEL);
218 	if (!*puvma)
219 		return -ENOMEM;
220 
221 	return 0;
222 }
223 
224 static void
225 nouveau_uvma_free(struct nouveau_uvma *uvma)
226 {
227 	kfree(uvma);
228 }
229 
230 static void
231 nouveau_uvma_gem_get(struct nouveau_uvma *uvma)
232 {
233 	drm_gem_object_get(uvma->va.gem.obj);
234 }
235 
236 static void
237 nouveau_uvma_gem_put(struct nouveau_uvma *uvma)
238 {
239 	drm_gem_object_put(uvma->va.gem.obj);
240 }
241 
242 static int
243 nouveau_uvma_region_alloc(struct nouveau_uvma_region **preg)
244 {
245 	*preg = kzalloc(sizeof(**preg), GFP_KERNEL);
246 	if (!*preg)
247 		return -ENOMEM;
248 
249 	kref_init(&(*preg)->kref);
250 
251 	return 0;
252 }
253 
254 static void
255 nouveau_uvma_region_free(struct kref *kref)
256 {
257 	struct nouveau_uvma_region *reg =
258 		container_of(kref, struct nouveau_uvma_region, kref);
259 
260 	kfree(reg);
261 }
262 
263 static void
264 nouveau_uvma_region_get(struct nouveau_uvma_region *reg)
265 {
266 	kref_get(&reg->kref);
267 }
268 
269 static void
270 nouveau_uvma_region_put(struct nouveau_uvma_region *reg)
271 {
272 	kref_put(&reg->kref, nouveau_uvma_region_free);
273 }
274 
275 static int
276 __nouveau_uvma_region_insert(struct nouveau_uvmm *uvmm,
277 			     struct nouveau_uvma_region *reg)
278 {
279 	u64 addr = reg->va.addr;
280 	u64 range = reg->va.range;
281 	u64 last = addr + range - 1;
282 	MA_STATE(mas, &uvmm->region_mt, addr, addr);
283 
284 	if (unlikely(mas_walk(&mas)))
285 		return -EEXIST;
286 
287 	if (unlikely(mas.last < last))
288 		return -EEXIST;
289 
290 	mas.index = addr;
291 	mas.last = last;
292 
293 	mas_store_gfp(&mas, reg, GFP_KERNEL);
294 
295 	reg->uvmm = uvmm;
296 
297 	return 0;
298 }
299 
300 static int
301 nouveau_uvma_region_insert(struct nouveau_uvmm *uvmm,
302 			   struct nouveau_uvma_region *reg,
303 			   u64 addr, u64 range)
304 {
305 	int ret;
306 
307 	reg->uvmm = uvmm;
308 	reg->va.addr = addr;
309 	reg->va.range = range;
310 
311 	ret = __nouveau_uvma_region_insert(uvmm, reg);
312 	if (ret)
313 		return ret;
314 
315 	return 0;
316 }
317 
318 static void
319 nouveau_uvma_region_remove(struct nouveau_uvma_region *reg)
320 {
321 	struct nouveau_uvmm *uvmm = reg->uvmm;
322 	MA_STATE(mas, &uvmm->region_mt, reg->va.addr, 0);
323 
324 	mas_erase(&mas);
325 }
326 
327 static int
328 nouveau_uvma_region_create(struct nouveau_uvmm *uvmm,
329 			   u64 addr, u64 range)
330 {
331 	struct nouveau_uvma_region *reg;
332 	int ret;
333 
334 	if (!drm_gpuvm_interval_empty(&uvmm->base, addr, range))
335 		return -ENOSPC;
336 
337 	ret = nouveau_uvma_region_alloc(&reg);
338 	if (ret)
339 		return ret;
340 
341 	ret = nouveau_uvma_region_insert(uvmm, reg, addr, range);
342 	if (ret)
343 		goto err_free_region;
344 
345 	ret = nouveau_uvmm_vmm_sparse_ref(uvmm, addr, range);
346 	if (ret)
347 		goto err_region_remove;
348 
349 	return 0;
350 
351 err_region_remove:
352 	nouveau_uvma_region_remove(reg);
353 err_free_region:
354 	nouveau_uvma_region_put(reg);
355 	return ret;
356 }
357 
358 static struct nouveau_uvma_region *
359 nouveau_uvma_region_find_first(struct nouveau_uvmm *uvmm,
360 			       u64 addr, u64 range)
361 {
362 	MA_STATE(mas, &uvmm->region_mt, addr, 0);
363 
364 	return mas_find(&mas, addr + range - 1);
365 }
366 
367 static struct nouveau_uvma_region *
368 nouveau_uvma_region_find(struct nouveau_uvmm *uvmm,
369 			 u64 addr, u64 range)
370 {
371 	struct nouveau_uvma_region *reg;
372 
373 	reg = nouveau_uvma_region_find_first(uvmm, addr, range);
374 	if (!reg)
375 		return NULL;
376 
377 	if (reg->va.addr != addr ||
378 	    reg->va.range != range)
379 		return NULL;
380 
381 	return reg;
382 }
383 
384 static bool
385 nouveau_uvma_region_empty(struct nouveau_uvma_region *reg)
386 {
387 	struct nouveau_uvmm *uvmm = reg->uvmm;
388 
389 	return drm_gpuvm_interval_empty(&uvmm->base,
390 					reg->va.addr,
391 					reg->va.range);
392 }
393 
394 static int
395 __nouveau_uvma_region_destroy(struct nouveau_uvma_region *reg)
396 {
397 	struct nouveau_uvmm *uvmm = reg->uvmm;
398 	u64 addr = reg->va.addr;
399 	u64 range = reg->va.range;
400 
401 	if (!nouveau_uvma_region_empty(reg))
402 		return -EBUSY;
403 
404 	nouveau_uvma_region_remove(reg);
405 	nouveau_uvmm_vmm_sparse_unref(uvmm, addr, range);
406 	nouveau_uvma_region_put(reg);
407 
408 	return 0;
409 }
410 
411 static int
412 nouveau_uvma_region_destroy(struct nouveau_uvmm *uvmm,
413 			    u64 addr, u64 range)
414 {
415 	struct nouveau_uvma_region *reg;
416 
417 	reg = nouveau_uvma_region_find(uvmm, addr, range);
418 	if (!reg)
419 		return -ENOENT;
420 
421 	return __nouveau_uvma_region_destroy(reg);
422 }
423 
424 static void
425 nouveau_uvma_region_dirty(struct nouveau_uvma_region *reg)
426 {
427 
428 	init_completion(&reg->complete);
429 	reg->dirty = true;
430 }
431 
432 static void
433 nouveau_uvma_region_complete(struct nouveau_uvma_region *reg)
434 {
435 	complete_all(&reg->complete);
436 }
437 
438 static void
439 op_map_prepare_unwind(struct nouveau_uvma *uvma)
440 {
441 	nouveau_uvma_gem_put(uvma);
442 	drm_gpuva_remove(&uvma->va);
443 	nouveau_uvma_free(uvma);
444 }
445 
446 static void
447 op_unmap_prepare_unwind(struct drm_gpuva *va)
448 {
449 	drm_gpuva_insert(va->vm, va);
450 }
451 
452 static void
453 nouveau_uvmm_sm_prepare_unwind(struct nouveau_uvmm *uvmm,
454 			       struct nouveau_uvma_prealloc *new,
455 			       struct drm_gpuva_ops *ops,
456 			       struct drm_gpuva_op *last,
457 			       struct uvmm_map_args *args)
458 {
459 	struct drm_gpuva_op *op = last;
460 	u64 vmm_get_start = args ? args->addr : 0;
461 	u64 vmm_get_end = args ? args->addr + args->range : 0;
462 
463 	/* Unwind GPUVA space. */
464 	drm_gpuva_for_each_op_from_reverse(op, ops) {
465 		switch (op->op) {
466 		case DRM_GPUVA_OP_MAP:
467 			op_map_prepare_unwind(new->map);
468 			break;
469 		case DRM_GPUVA_OP_REMAP: {
470 			struct drm_gpuva_op_remap *r = &op->remap;
471 
472 			if (r->next)
473 				op_map_prepare_unwind(new->next);
474 
475 			if (r->prev)
476 				op_map_prepare_unwind(new->prev);
477 
478 			op_unmap_prepare_unwind(r->unmap->va);
479 			break;
480 		}
481 		case DRM_GPUVA_OP_UNMAP:
482 			op_unmap_prepare_unwind(op->unmap.va);
483 			break;
484 		default:
485 			break;
486 		}
487 	}
488 
489 	/* Unmap operation don't allocate page tables, hence skip the following
490 	 * page table unwind.
491 	 */
492 	if (!args)
493 		return;
494 
495 	drm_gpuva_for_each_op(op, ops) {
496 		switch (op->op) {
497 		case DRM_GPUVA_OP_MAP: {
498 			u64 vmm_get_range = vmm_get_end - vmm_get_start;
499 
500 			if (vmm_get_range)
501 				nouveau_uvmm_vmm_put(uvmm, vmm_get_start,
502 						     vmm_get_range);
503 			break;
504 		}
505 		case DRM_GPUVA_OP_REMAP: {
506 			struct drm_gpuva_op_remap *r = &op->remap;
507 			struct drm_gpuva *va = r->unmap->va;
508 			u64 ustart = va->va.addr;
509 			u64 urange = va->va.range;
510 			u64 uend = ustart + urange;
511 
512 			if (r->prev)
513 				vmm_get_start = uend;
514 
515 			if (r->next)
516 				vmm_get_end = ustart;
517 
518 			if (r->prev && r->next)
519 				vmm_get_start = vmm_get_end = 0;
520 
521 			break;
522 		}
523 		case DRM_GPUVA_OP_UNMAP: {
524 			struct drm_gpuva_op_unmap *u = &op->unmap;
525 			struct drm_gpuva *va = u->va;
526 			u64 ustart = va->va.addr;
527 			u64 urange = va->va.range;
528 			u64 uend = ustart + urange;
529 
530 			/* Nothing to do for mappings we merge with. */
531 			if (uend == vmm_get_start ||
532 			    ustart == vmm_get_end)
533 				break;
534 
535 			if (ustart > vmm_get_start) {
536 				u64 vmm_get_range = ustart - vmm_get_start;
537 
538 				nouveau_uvmm_vmm_put(uvmm, vmm_get_start,
539 						     vmm_get_range);
540 			}
541 			vmm_get_start = uend;
542 			break;
543 		}
544 		default:
545 			break;
546 		}
547 
548 		if (op == last)
549 			break;
550 	}
551 }
552 
553 static void
554 nouveau_uvmm_sm_map_prepare_unwind(struct nouveau_uvmm *uvmm,
555 				   struct nouveau_uvma_prealloc *new,
556 				   struct drm_gpuva_ops *ops,
557 				   u64 addr, u64 range)
558 {
559 	struct drm_gpuva_op *last = drm_gpuva_last_op(ops);
560 	struct uvmm_map_args args = {
561 		.addr = addr,
562 		.range = range,
563 	};
564 
565 	nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops, last, &args);
566 }
567 
568 static void
569 nouveau_uvmm_sm_unmap_prepare_unwind(struct nouveau_uvmm *uvmm,
570 				     struct nouveau_uvma_prealloc *new,
571 				     struct drm_gpuva_ops *ops)
572 {
573 	struct drm_gpuva_op *last = drm_gpuva_last_op(ops);
574 
575 	nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops, last, NULL);
576 }
577 
578 static int
579 op_map_prepare(struct nouveau_uvmm *uvmm,
580 	       struct nouveau_uvma **puvma,
581 	       struct drm_gpuva_op_map *op,
582 	       struct uvmm_map_args *args)
583 {
584 	struct nouveau_uvma *uvma;
585 	int ret;
586 
587 	ret = nouveau_uvma_alloc(&uvma);
588 	if (ret)
589 		return ret;
590 
591 	uvma->region = args->region;
592 	uvma->kind = args->kind;
593 
594 	drm_gpuva_map(&uvmm->base, &uvma->va, op);
595 
596 	/* Keep a reference until this uvma is destroyed. */
597 	nouveau_uvma_gem_get(uvma);
598 
599 	*puvma = uvma;
600 	return 0;
601 }
602 
603 static void
604 op_unmap_prepare(struct drm_gpuva_op_unmap *u)
605 {
606 	drm_gpuva_unmap(u);
607 }
608 
609 static int
610 nouveau_uvmm_sm_prepare(struct nouveau_uvmm *uvmm,
611 			struct nouveau_uvma_prealloc *new,
612 			struct drm_gpuva_ops *ops,
613 			struct uvmm_map_args *args)
614 {
615 	struct drm_gpuva_op *op;
616 	u64 vmm_get_start = args ? args->addr : 0;
617 	u64 vmm_get_end = args ? args->addr + args->range : 0;
618 	int ret;
619 
620 	drm_gpuva_for_each_op(op, ops) {
621 		switch (op->op) {
622 		case DRM_GPUVA_OP_MAP: {
623 			u64 vmm_get_range = vmm_get_end - vmm_get_start;
624 
625 			ret = op_map_prepare(uvmm, &new->map, &op->map, args);
626 			if (ret)
627 				goto unwind;
628 
629 			if (args && vmm_get_range) {
630 				ret = nouveau_uvmm_vmm_get(uvmm, vmm_get_start,
631 							   vmm_get_range);
632 				if (ret) {
633 					op_map_prepare_unwind(new->map);
634 					goto unwind;
635 				}
636 			}
637 			break;
638 		}
639 		case DRM_GPUVA_OP_REMAP: {
640 			struct drm_gpuva_op_remap *r = &op->remap;
641 			struct drm_gpuva *va = r->unmap->va;
642 			struct uvmm_map_args remap_args = {
643 				.kind = uvma_from_va(va)->kind,
644 				.region = uvma_from_va(va)->region,
645 			};
646 			u64 ustart = va->va.addr;
647 			u64 urange = va->va.range;
648 			u64 uend = ustart + urange;
649 
650 			op_unmap_prepare(r->unmap);
651 
652 			if (r->prev) {
653 				ret = op_map_prepare(uvmm, &new->prev, r->prev,
654 						     &remap_args);
655 				if (ret)
656 					goto unwind;
657 
658 				if (args)
659 					vmm_get_start = uend;
660 			}
661 
662 			if (r->next) {
663 				ret = op_map_prepare(uvmm, &new->next, r->next,
664 						     &remap_args);
665 				if (ret) {
666 					if (r->prev)
667 						op_map_prepare_unwind(new->prev);
668 					goto unwind;
669 				}
670 
671 				if (args)
672 					vmm_get_end = ustart;
673 			}
674 
675 			if (args && (r->prev && r->next))
676 				vmm_get_start = vmm_get_end = 0;
677 
678 			break;
679 		}
680 		case DRM_GPUVA_OP_UNMAP: {
681 			struct drm_gpuva_op_unmap *u = &op->unmap;
682 			struct drm_gpuva *va = u->va;
683 			u64 ustart = va->va.addr;
684 			u64 urange = va->va.range;
685 			u64 uend = ustart + urange;
686 
687 			op_unmap_prepare(u);
688 
689 			if (!args)
690 				break;
691 
692 			/* Nothing to do for mappings we merge with. */
693 			if (uend == vmm_get_start ||
694 			    ustart == vmm_get_end)
695 				break;
696 
697 			if (ustart > vmm_get_start) {
698 				u64 vmm_get_range = ustart - vmm_get_start;
699 
700 				ret = nouveau_uvmm_vmm_get(uvmm, vmm_get_start,
701 							   vmm_get_range);
702 				if (ret) {
703 					op_unmap_prepare_unwind(va);
704 					goto unwind;
705 				}
706 			}
707 			vmm_get_start = uend;
708 
709 			break;
710 		}
711 		default:
712 			ret = -EINVAL;
713 			goto unwind;
714 		}
715 	}
716 
717 	return 0;
718 
719 unwind:
720 	if (op != drm_gpuva_first_op(ops))
721 		nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops,
722 					       drm_gpuva_prev_op(op),
723 					       args);
724 	return ret;
725 }
726 
727 static int
728 nouveau_uvmm_sm_map_prepare(struct nouveau_uvmm *uvmm,
729 			    struct nouveau_uvma_prealloc *new,
730 			    struct nouveau_uvma_region *region,
731 			    struct drm_gpuva_ops *ops,
732 			    u64 addr, u64 range, u8 kind)
733 {
734 	struct uvmm_map_args args = {
735 		.region = region,
736 		.addr = addr,
737 		.range = range,
738 		.kind = kind,
739 	};
740 
741 	return nouveau_uvmm_sm_prepare(uvmm, new, ops, &args);
742 }
743 
744 static int
745 nouveau_uvmm_sm_unmap_prepare(struct nouveau_uvmm *uvmm,
746 			      struct nouveau_uvma_prealloc *new,
747 			      struct drm_gpuva_ops *ops)
748 {
749 	return nouveau_uvmm_sm_prepare(uvmm, new, ops, NULL);
750 }
751 
752 static struct drm_gem_object *
753 op_gem_obj(struct drm_gpuva_op *op)
754 {
755 	switch (op->op) {
756 	case DRM_GPUVA_OP_MAP:
757 		return op->map.gem.obj;
758 	case DRM_GPUVA_OP_REMAP:
759 		/* Actually, we're looking for the GEMs backing remap.prev and
760 		 * remap.next, but since this is a remap they're identical to
761 		 * the GEM backing the unmapped GPUVA.
762 		 */
763 		return op->remap.unmap->va->gem.obj;
764 	case DRM_GPUVA_OP_UNMAP:
765 		return op->unmap.va->gem.obj;
766 	default:
767 		WARN(1, "Unknown operation.\n");
768 		return NULL;
769 	}
770 }
771 
772 static void
773 op_map(struct nouveau_uvma *uvma)
774 {
775 	struct nouveau_bo *nvbo = nouveau_gem_object(uvma->va.gem.obj);
776 
777 	nouveau_uvma_map(uvma, nouveau_mem(nvbo->bo.resource));
778 }
779 
780 static void
781 op_unmap(struct drm_gpuva_op_unmap *u)
782 {
783 	struct drm_gpuva *va = u->va;
784 	struct nouveau_uvma *uvma = uvma_from_va(va);
785 
786 	/* nouveau_uvma_unmap() does not unmap if backing BO is evicted. */
787 	if (!u->keep)
788 		nouveau_uvma_unmap(uvma);
789 }
790 
791 static void
792 op_unmap_range(struct drm_gpuva_op_unmap *u,
793 	       u64 addr, u64 range)
794 {
795 	struct nouveau_uvma *uvma = uvma_from_va(u->va);
796 	bool sparse = !!uvma->region;
797 
798 	if (!drm_gpuva_invalidated(u->va))
799 		nouveau_uvmm_vmm_unmap(to_uvmm(uvma), addr, range, sparse);
800 }
801 
802 static void
803 op_remap(struct drm_gpuva_op_remap *r,
804 	 struct nouveau_uvma_prealloc *new)
805 {
806 	struct drm_gpuva_op_unmap *u = r->unmap;
807 	struct nouveau_uvma *uvma = uvma_from_va(u->va);
808 	u64 addr = uvma->va.va.addr;
809 	u64 range = uvma->va.va.range;
810 
811 	if (r->prev)
812 		addr = r->prev->va.addr + r->prev->va.range;
813 
814 	if (r->next)
815 		range = r->next->va.addr - addr;
816 
817 	op_unmap_range(u, addr, range);
818 }
819 
820 static int
821 nouveau_uvmm_sm(struct nouveau_uvmm *uvmm,
822 		struct nouveau_uvma_prealloc *new,
823 		struct drm_gpuva_ops *ops)
824 {
825 	struct drm_gpuva_op *op;
826 
827 	drm_gpuva_for_each_op(op, ops) {
828 		switch (op->op) {
829 		case DRM_GPUVA_OP_MAP:
830 			op_map(new->map);
831 			break;
832 		case DRM_GPUVA_OP_REMAP:
833 			op_remap(&op->remap, new);
834 			break;
835 		case DRM_GPUVA_OP_UNMAP:
836 			op_unmap(&op->unmap);
837 			break;
838 		default:
839 			break;
840 		}
841 	}
842 
843 	return 0;
844 }
845 
846 static int
847 nouveau_uvmm_sm_map(struct nouveau_uvmm *uvmm,
848 		    struct nouveau_uvma_prealloc *new,
849 		    struct drm_gpuva_ops *ops)
850 {
851 	return nouveau_uvmm_sm(uvmm, new, ops);
852 }
853 
854 static int
855 nouveau_uvmm_sm_unmap(struct nouveau_uvmm *uvmm,
856 		      struct nouveau_uvma_prealloc *new,
857 		      struct drm_gpuva_ops *ops)
858 {
859 	return nouveau_uvmm_sm(uvmm, new, ops);
860 }
861 
862 static void
863 nouveau_uvmm_sm_cleanup(struct nouveau_uvmm *uvmm,
864 			struct nouveau_uvma_prealloc *new,
865 			struct drm_gpuva_ops *ops, bool unmap)
866 {
867 	struct drm_gpuva_op *op;
868 
869 	drm_gpuva_for_each_op(op, ops) {
870 		switch (op->op) {
871 		case DRM_GPUVA_OP_MAP:
872 			break;
873 		case DRM_GPUVA_OP_REMAP: {
874 			struct drm_gpuva_op_remap *r = &op->remap;
875 			struct drm_gpuva_op_map *p = r->prev;
876 			struct drm_gpuva_op_map *n = r->next;
877 			struct drm_gpuva *va = r->unmap->va;
878 			struct nouveau_uvma *uvma = uvma_from_va(va);
879 
880 			if (unmap) {
881 				u64 addr = va->va.addr;
882 				u64 end = addr + va->va.range;
883 
884 				if (p)
885 					addr = p->va.addr + p->va.range;
886 
887 				if (n)
888 					end = n->va.addr;
889 
890 				nouveau_uvmm_vmm_put(uvmm, addr, end - addr);
891 			}
892 
893 			nouveau_uvma_gem_put(uvma);
894 			nouveau_uvma_free(uvma);
895 			break;
896 		}
897 		case DRM_GPUVA_OP_UNMAP: {
898 			struct drm_gpuva_op_unmap *u = &op->unmap;
899 			struct drm_gpuva *va = u->va;
900 			struct nouveau_uvma *uvma = uvma_from_va(va);
901 
902 			if (unmap)
903 				nouveau_uvma_vmm_put(uvma);
904 
905 			nouveau_uvma_gem_put(uvma);
906 			nouveau_uvma_free(uvma);
907 			break;
908 		}
909 		default:
910 			break;
911 		}
912 	}
913 }
914 
915 static void
916 nouveau_uvmm_sm_map_cleanup(struct nouveau_uvmm *uvmm,
917 			    struct nouveau_uvma_prealloc *new,
918 			    struct drm_gpuva_ops *ops)
919 {
920 	nouveau_uvmm_sm_cleanup(uvmm, new, ops, false);
921 }
922 
923 static void
924 nouveau_uvmm_sm_unmap_cleanup(struct nouveau_uvmm *uvmm,
925 			      struct nouveau_uvma_prealloc *new,
926 			      struct drm_gpuva_ops *ops)
927 {
928 	nouveau_uvmm_sm_cleanup(uvmm, new, ops, true);
929 }
930 
931 static int
932 nouveau_uvmm_validate_range(struct nouveau_uvmm *uvmm, u64 addr, u64 range)
933 {
934 	if (addr & ~PAGE_MASK)
935 		return -EINVAL;
936 
937 	if (range & ~PAGE_MASK)
938 		return -EINVAL;
939 
940 	if (!drm_gpuvm_range_valid(&uvmm->base, addr, range))
941 		return -EINVAL;
942 
943 	return 0;
944 }
945 
946 static int
947 nouveau_uvmm_bind_job_alloc(struct nouveau_uvmm_bind_job **pjob)
948 {
949 	*pjob = kzalloc(sizeof(**pjob), GFP_KERNEL);
950 	if (!*pjob)
951 		return -ENOMEM;
952 
953 	kref_init(&(*pjob)->kref);
954 
955 	return 0;
956 }
957 
958 static void
959 nouveau_uvmm_bind_job_free(struct kref *kref)
960 {
961 	struct nouveau_uvmm_bind_job *job =
962 		container_of(kref, struct nouveau_uvmm_bind_job, kref);
963 
964 	nouveau_job_free(&job->base);
965 	kfree(job);
966 }
967 
968 static void
969 nouveau_uvmm_bind_job_get(struct nouveau_uvmm_bind_job *job)
970 {
971 	kref_get(&job->kref);
972 }
973 
974 static void
975 nouveau_uvmm_bind_job_put(struct nouveau_uvmm_bind_job *job)
976 {
977 	kref_put(&job->kref, nouveau_uvmm_bind_job_free);
978 }
979 
980 static int
981 bind_validate_op(struct nouveau_job *job,
982 		 struct bind_job_op *op)
983 {
984 	struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
985 	struct drm_gem_object *obj = op->gem.obj;
986 
987 	if (op->op == OP_MAP) {
988 		if (op->gem.offset & ~PAGE_MASK)
989 			return -EINVAL;
990 
991 		if (obj->size <= op->gem.offset)
992 			return -EINVAL;
993 
994 		if (op->va.range > (obj->size - op->gem.offset))
995 			return -EINVAL;
996 	}
997 
998 	return nouveau_uvmm_validate_range(uvmm, op->va.addr, op->va.range);
999 }
1000 
1001 static void
1002 bind_validate_map_sparse(struct nouveau_job *job, u64 addr, u64 range)
1003 {
1004 	struct nouveau_uvmm_bind_job *bind_job;
1005 	struct nouveau_sched_entity *entity = job->entity;
1006 	struct bind_job_op *op;
1007 	u64 end = addr + range;
1008 
1009 again:
1010 	spin_lock(&entity->job.list.lock);
1011 	list_for_each_entry(bind_job, &entity->job.list.head, entry) {
1012 		list_for_each_op(op, &bind_job->ops) {
1013 			if (op->op == OP_UNMAP) {
1014 				u64 op_addr = op->va.addr;
1015 				u64 op_end = op_addr + op->va.range;
1016 
1017 				if (!(end <= op_addr || addr >= op_end)) {
1018 					nouveau_uvmm_bind_job_get(bind_job);
1019 					spin_unlock(&entity->job.list.lock);
1020 					wait_for_completion(&bind_job->complete);
1021 					nouveau_uvmm_bind_job_put(bind_job);
1022 					goto again;
1023 				}
1024 			}
1025 		}
1026 	}
1027 	spin_unlock(&entity->job.list.lock);
1028 }
1029 
1030 static int
1031 bind_validate_map_common(struct nouveau_job *job, u64 addr, u64 range,
1032 			 bool sparse)
1033 {
1034 	struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
1035 	struct nouveau_uvma_region *reg;
1036 	u64 reg_addr, reg_end;
1037 	u64 end = addr + range;
1038 
1039 again:
1040 	nouveau_uvmm_lock(uvmm);
1041 	reg = nouveau_uvma_region_find_first(uvmm, addr, range);
1042 	if (!reg) {
1043 		nouveau_uvmm_unlock(uvmm);
1044 		return 0;
1045 	}
1046 
1047 	/* Generally, job submits are serialized, hence only
1048 	 * dirty regions can be modified concurrently.
1049 	 */
1050 	if (reg->dirty) {
1051 		nouveau_uvma_region_get(reg);
1052 		nouveau_uvmm_unlock(uvmm);
1053 		wait_for_completion(&reg->complete);
1054 		nouveau_uvma_region_put(reg);
1055 		goto again;
1056 	}
1057 	nouveau_uvmm_unlock(uvmm);
1058 
1059 	if (sparse)
1060 		return -ENOSPC;
1061 
1062 	reg_addr = reg->va.addr;
1063 	reg_end = reg_addr + reg->va.range;
1064 
1065 	/* Make sure the mapping is either outside of a
1066 	 * region or fully enclosed by a region.
1067 	 */
1068 	if (reg_addr > addr || reg_end < end)
1069 		return -ENOSPC;
1070 
1071 	return 0;
1072 }
1073 
1074 static int
1075 bind_validate_region(struct nouveau_job *job)
1076 {
1077 	struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1078 	struct bind_job_op *op;
1079 	int ret;
1080 
1081 	list_for_each_op(op, &bind_job->ops) {
1082 		u64 op_addr = op->va.addr;
1083 		u64 op_range = op->va.range;
1084 		bool sparse = false;
1085 
1086 		switch (op->op) {
1087 		case OP_MAP_SPARSE:
1088 			sparse = true;
1089 			bind_validate_map_sparse(job, op_addr, op_range);
1090 			fallthrough;
1091 		case OP_MAP:
1092 			ret = bind_validate_map_common(job, op_addr, op_range,
1093 						       sparse);
1094 			if (ret)
1095 				return ret;
1096 			break;
1097 		default:
1098 			break;
1099 		}
1100 	}
1101 
1102 	return 0;
1103 }
1104 
1105 static void
1106 bind_link_gpuvas(struct bind_job_op *bop)
1107 {
1108 	struct nouveau_uvma_prealloc *new = &bop->new;
1109 	struct drm_gpuvm_bo *vm_bo = bop->vm_bo;
1110 	struct drm_gpuva_ops *ops = bop->ops;
1111 	struct drm_gpuva_op *op;
1112 
1113 	drm_gpuva_for_each_op(op, ops) {
1114 		switch (op->op) {
1115 		case DRM_GPUVA_OP_MAP:
1116 			drm_gpuva_link(&new->map->va, vm_bo);
1117 			break;
1118 		case DRM_GPUVA_OP_REMAP: {
1119 			struct drm_gpuva *va = op->remap.unmap->va;
1120 
1121 			if (op->remap.prev)
1122 				drm_gpuva_link(&new->prev->va, va->vm_bo);
1123 			if (op->remap.next)
1124 				drm_gpuva_link(&new->next->va, va->vm_bo);
1125 			drm_gpuva_unlink(va);
1126 			break;
1127 		}
1128 		case DRM_GPUVA_OP_UNMAP:
1129 			drm_gpuva_unlink(op->unmap.va);
1130 			break;
1131 		default:
1132 			break;
1133 		}
1134 	}
1135 }
1136 
1137 static int
1138 nouveau_uvmm_bind_job_submit(struct nouveau_job *job)
1139 {
1140 	struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
1141 	struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1142 	struct nouveau_sched_entity *entity = job->entity;
1143 	struct drm_exec *exec = &job->exec;
1144 	struct bind_job_op *op;
1145 	int ret;
1146 
1147 	list_for_each_op(op, &bind_job->ops) {
1148 		if (op->op == OP_MAP) {
1149 			struct drm_gem_object *obj = op->gem.obj =
1150 				drm_gem_object_lookup(job->file_priv,
1151 						      op->gem.handle);
1152 			if (!obj)
1153 				return -ENOENT;
1154 
1155 			dma_resv_lock(obj->resv, NULL);
1156 			op->vm_bo = drm_gpuvm_bo_obtain(&uvmm->base, obj);
1157 			dma_resv_unlock(obj->resv);
1158 			if (IS_ERR(op->vm_bo))
1159 				return PTR_ERR(op->vm_bo);
1160 		}
1161 
1162 		ret = bind_validate_op(job, op);
1163 		if (ret)
1164 			return ret;
1165 	}
1166 
1167 	/* If a sparse region or mapping overlaps a dirty region, we need to
1168 	 * wait for the region to complete the unbind process. This is due to
1169 	 * how page table management is currently implemented. A future
1170 	 * implementation might change this.
1171 	 */
1172 	ret = bind_validate_region(job);
1173 	if (ret)
1174 		return ret;
1175 
1176 	/* Once we start modifying the GPU VA space we need to keep holding the
1177 	 * uvmm lock until we can't fail anymore. This is due to the set of GPU
1178 	 * VA space changes must appear atomically and we need to be able to
1179 	 * unwind all GPU VA space changes on failure.
1180 	 */
1181 	nouveau_uvmm_lock(uvmm);
1182 	list_for_each_op(op, &bind_job->ops) {
1183 		switch (op->op) {
1184 		case OP_MAP_SPARSE:
1185 			ret = nouveau_uvma_region_create(uvmm,
1186 							 op->va.addr,
1187 							 op->va.range);
1188 			if (ret)
1189 				goto unwind_continue;
1190 
1191 			break;
1192 		case OP_UNMAP_SPARSE:
1193 			op->reg = nouveau_uvma_region_find(uvmm, op->va.addr,
1194 							   op->va.range);
1195 			if (!op->reg || op->reg->dirty) {
1196 				ret = -ENOENT;
1197 				goto unwind_continue;
1198 			}
1199 
1200 			op->ops = drm_gpuvm_sm_unmap_ops_create(&uvmm->base,
1201 								op->va.addr,
1202 								op->va.range);
1203 			if (IS_ERR(op->ops)) {
1204 				ret = PTR_ERR(op->ops);
1205 				goto unwind_continue;
1206 			}
1207 
1208 			ret = nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new,
1209 							    op->ops);
1210 			if (ret) {
1211 				drm_gpuva_ops_free(&uvmm->base, op->ops);
1212 				op->ops = NULL;
1213 				op->reg = NULL;
1214 				goto unwind_continue;
1215 			}
1216 
1217 			nouveau_uvma_region_dirty(op->reg);
1218 
1219 			break;
1220 		case OP_MAP: {
1221 			struct nouveau_uvma_region *reg;
1222 
1223 			reg = nouveau_uvma_region_find_first(uvmm,
1224 							     op->va.addr,
1225 							     op->va.range);
1226 			if (reg) {
1227 				u64 reg_addr = reg->va.addr;
1228 				u64 reg_end = reg_addr + reg->va.range;
1229 				u64 op_addr = op->va.addr;
1230 				u64 op_end = op_addr + op->va.range;
1231 
1232 				if (unlikely(reg->dirty)) {
1233 					ret = -EINVAL;
1234 					goto unwind_continue;
1235 				}
1236 
1237 				/* Make sure the mapping is either outside of a
1238 				 * region or fully enclosed by a region.
1239 				 */
1240 				if (reg_addr > op_addr || reg_end < op_end) {
1241 					ret = -ENOSPC;
1242 					goto unwind_continue;
1243 				}
1244 			}
1245 
1246 			op->ops = drm_gpuvm_sm_map_ops_create(&uvmm->base,
1247 							      op->va.addr,
1248 							      op->va.range,
1249 							      op->gem.obj,
1250 							      op->gem.offset);
1251 			if (IS_ERR(op->ops)) {
1252 				ret = PTR_ERR(op->ops);
1253 				goto unwind_continue;
1254 			}
1255 
1256 			ret = nouveau_uvmm_sm_map_prepare(uvmm, &op->new,
1257 							  reg, op->ops,
1258 							  op->va.addr,
1259 							  op->va.range,
1260 							  op->flags & 0xff);
1261 			if (ret) {
1262 				drm_gpuva_ops_free(&uvmm->base, op->ops);
1263 				op->ops = NULL;
1264 				goto unwind_continue;
1265 			}
1266 
1267 			break;
1268 		}
1269 		case OP_UNMAP:
1270 			op->ops = drm_gpuvm_sm_unmap_ops_create(&uvmm->base,
1271 								op->va.addr,
1272 								op->va.range);
1273 			if (IS_ERR(op->ops)) {
1274 				ret = PTR_ERR(op->ops);
1275 				goto unwind_continue;
1276 			}
1277 
1278 			ret = nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new,
1279 							    op->ops);
1280 			if (ret) {
1281 				drm_gpuva_ops_free(&uvmm->base, op->ops);
1282 				op->ops = NULL;
1283 				goto unwind_continue;
1284 			}
1285 
1286 			break;
1287 		default:
1288 			ret = -EINVAL;
1289 			goto unwind_continue;
1290 		}
1291 	}
1292 
1293 	drm_exec_init(exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
1294 			    DRM_EXEC_IGNORE_DUPLICATES);
1295 	drm_exec_until_all_locked(exec) {
1296 		list_for_each_op(op, &bind_job->ops) {
1297 			struct drm_gpuva_op *va_op;
1298 
1299 			if (IS_ERR_OR_NULL(op->ops))
1300 				continue;
1301 
1302 			drm_gpuva_for_each_op(va_op, op->ops) {
1303 				struct drm_gem_object *obj = op_gem_obj(va_op);
1304 
1305 				if (unlikely(!obj))
1306 					continue;
1307 
1308 				ret = drm_exec_prepare_obj(exec, obj, 1);
1309 				drm_exec_retry_on_contention(exec);
1310 				if (ret) {
1311 					op = list_last_op(&bind_job->ops);
1312 					goto unwind;
1313 				}
1314 			}
1315 		}
1316 	}
1317 
1318 	list_for_each_op(op, &bind_job->ops) {
1319 		struct drm_gpuva_op *va_op;
1320 
1321 		if (IS_ERR_OR_NULL(op->ops))
1322 			continue;
1323 
1324 		drm_gpuva_for_each_op(va_op, op->ops) {
1325 			struct drm_gem_object *obj = op_gem_obj(va_op);
1326 
1327 			if (unlikely(!obj))
1328 				continue;
1329 
1330 			/* Don't validate GEMs backing mappings we're about to
1331 			 * unmap, it's not worth the effort.
1332 			 */
1333 			if (unlikely(va_op->op == DRM_GPUVA_OP_UNMAP))
1334 				continue;
1335 
1336 			ret = nouveau_bo_validate(nouveau_gem_object(obj),
1337 						  true, false);
1338 			if (ret) {
1339 				op = list_last_op(&bind_job->ops);
1340 				goto unwind;
1341 			}
1342 		}
1343 	}
1344 
1345 	/* Link and unlink GPUVAs while holding the dma_resv lock.
1346 	 *
1347 	 * As long as we validate() all GEMs and add fences to all GEMs DMA
1348 	 * reservations backing map and remap operations we can be sure there
1349 	 * won't be any concurrent (in)validations during job execution, hence
1350 	 * we're safe to check drm_gpuva_invalidated() within the fence
1351 	 * signalling critical path without holding a separate lock.
1352 	 *
1353 	 * GPUVAs about to be unmapped are safe as well, since they're unlinked
1354 	 * already.
1355 	 *
1356 	 * GEMs from map and remap operations must be validated before linking
1357 	 * their corresponding mappings to prevent the actual PT update to
1358 	 * happen right away in validate() rather than asynchronously as
1359 	 * intended.
1360 	 *
1361 	 * Note that after linking and unlinking the GPUVAs in this loop this
1362 	 * function cannot fail anymore, hence there is no need for an unwind
1363 	 * path.
1364 	 */
1365 	list_for_each_op(op, &bind_job->ops) {
1366 		switch (op->op) {
1367 		case OP_UNMAP_SPARSE:
1368 		case OP_MAP:
1369 		case OP_UNMAP:
1370 			bind_link_gpuvas(op);
1371 			break;
1372 		default:
1373 			break;
1374 		}
1375 	}
1376 	nouveau_uvmm_unlock(uvmm);
1377 
1378 	spin_lock(&entity->job.list.lock);
1379 	list_add(&bind_job->entry, &entity->job.list.head);
1380 	spin_unlock(&entity->job.list.lock);
1381 
1382 	return 0;
1383 
1384 unwind_continue:
1385 	op = list_prev_op(op);
1386 unwind:
1387 	list_for_each_op_from_reverse(op, &bind_job->ops) {
1388 		switch (op->op) {
1389 		case OP_MAP_SPARSE:
1390 			nouveau_uvma_region_destroy(uvmm, op->va.addr,
1391 						    op->va.range);
1392 			break;
1393 		case OP_UNMAP_SPARSE:
1394 			__nouveau_uvma_region_insert(uvmm, op->reg);
1395 			nouveau_uvmm_sm_unmap_prepare_unwind(uvmm, &op->new,
1396 							     op->ops);
1397 			break;
1398 		case OP_MAP:
1399 			nouveau_uvmm_sm_map_prepare_unwind(uvmm, &op->new,
1400 							   op->ops,
1401 							   op->va.addr,
1402 							   op->va.range);
1403 			break;
1404 		case OP_UNMAP:
1405 			nouveau_uvmm_sm_unmap_prepare_unwind(uvmm, &op->new,
1406 							     op->ops);
1407 			break;
1408 		}
1409 
1410 		drm_gpuva_ops_free(&uvmm->base, op->ops);
1411 		op->ops = NULL;
1412 		op->reg = NULL;
1413 	}
1414 
1415 	nouveau_uvmm_unlock(uvmm);
1416 	drm_exec_fini(exec);
1417 	return ret;
1418 }
1419 
1420 static void
1421 nouveau_uvmm_bind_job_armed_submit(struct nouveau_job *job)
1422 {
1423 	struct drm_exec *exec = &job->exec;
1424 	struct drm_gem_object *obj;
1425 	unsigned long index;
1426 
1427 	drm_exec_for_each_locked_object(exec, index, obj)
1428 		dma_resv_add_fence(obj->resv, job->done_fence, job->resv_usage);
1429 
1430 	drm_exec_fini(exec);
1431 }
1432 
1433 static struct dma_fence *
1434 nouveau_uvmm_bind_job_run(struct nouveau_job *job)
1435 {
1436 	struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1437 	struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
1438 	struct bind_job_op *op;
1439 	int ret = 0;
1440 
1441 	list_for_each_op(op, &bind_job->ops) {
1442 		switch (op->op) {
1443 		case OP_MAP_SPARSE:
1444 			/* noop */
1445 			break;
1446 		case OP_MAP:
1447 			ret = nouveau_uvmm_sm_map(uvmm, &op->new, op->ops);
1448 			if (ret)
1449 				goto out;
1450 			break;
1451 		case OP_UNMAP_SPARSE:
1452 			fallthrough;
1453 		case OP_UNMAP:
1454 			ret = nouveau_uvmm_sm_unmap(uvmm, &op->new, op->ops);
1455 			if (ret)
1456 				goto out;
1457 			break;
1458 		}
1459 	}
1460 
1461 out:
1462 	if (ret)
1463 		NV_PRINTK(err, job->cli, "bind job failed: %d\n", ret);
1464 	return ERR_PTR(ret);
1465 }
1466 
1467 static void
1468 nouveau_uvmm_bind_job_free_work_fn(struct work_struct *work)
1469 {
1470 	struct nouveau_uvmm_bind_job *bind_job =
1471 		container_of(work, struct nouveau_uvmm_bind_job, work);
1472 	struct nouveau_job *job = &bind_job->base;
1473 	struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
1474 	struct nouveau_sched_entity *entity = job->entity;
1475 	struct bind_job_op *op, *next;
1476 
1477 	list_for_each_op(op, &bind_job->ops) {
1478 		struct drm_gem_object *obj = op->gem.obj;
1479 
1480 		/* When nouveau_uvmm_bind_job_submit() fails op->ops and op->reg
1481 		 * will be NULL, hence skip the cleanup.
1482 		 */
1483 		switch (op->op) {
1484 		case OP_MAP_SPARSE:
1485 			/* noop */
1486 			break;
1487 		case OP_UNMAP_SPARSE:
1488 			if (!IS_ERR_OR_NULL(op->ops))
1489 				nouveau_uvmm_sm_unmap_cleanup(uvmm, &op->new,
1490 							      op->ops);
1491 
1492 			if (op->reg) {
1493 				nouveau_uvma_region_sparse_unref(op->reg);
1494 				nouveau_uvmm_lock(uvmm);
1495 				nouveau_uvma_region_remove(op->reg);
1496 				nouveau_uvmm_unlock(uvmm);
1497 				nouveau_uvma_region_complete(op->reg);
1498 				nouveau_uvma_region_put(op->reg);
1499 			}
1500 
1501 			break;
1502 		case OP_MAP:
1503 			if (!IS_ERR_OR_NULL(op->ops))
1504 				nouveau_uvmm_sm_map_cleanup(uvmm, &op->new,
1505 							    op->ops);
1506 			break;
1507 		case OP_UNMAP:
1508 			if (!IS_ERR_OR_NULL(op->ops))
1509 				nouveau_uvmm_sm_unmap_cleanup(uvmm, &op->new,
1510 							      op->ops);
1511 			break;
1512 		}
1513 
1514 		if (!IS_ERR_OR_NULL(op->ops))
1515 			drm_gpuva_ops_free(&uvmm->base, op->ops);
1516 
1517 		if (!IS_ERR_OR_NULL(op->vm_bo)) {
1518 			dma_resv_lock(obj->resv, NULL);
1519 			drm_gpuvm_bo_put(op->vm_bo);
1520 			dma_resv_unlock(obj->resv);
1521 		}
1522 
1523 		if (obj)
1524 			drm_gem_object_put(obj);
1525 	}
1526 
1527 	spin_lock(&entity->job.list.lock);
1528 	list_del(&bind_job->entry);
1529 	spin_unlock(&entity->job.list.lock);
1530 
1531 	complete_all(&bind_job->complete);
1532 	wake_up(&entity->job.wq);
1533 
1534 	/* Remove and free ops after removing the bind job from the job list to
1535 	 * avoid races against bind_validate_map_sparse().
1536 	 */
1537 	list_for_each_op_safe(op, next, &bind_job->ops) {
1538 		list_del(&op->entry);
1539 		kfree(op);
1540 	}
1541 
1542 	nouveau_uvmm_bind_job_put(bind_job);
1543 }
1544 
1545 static void
1546 nouveau_uvmm_bind_job_free_qwork(struct nouveau_job *job)
1547 {
1548 	struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1549 	struct nouveau_sched_entity *entity = job->entity;
1550 
1551 	nouveau_sched_entity_qwork(entity, &bind_job->work);
1552 }
1553 
1554 static struct nouveau_job_ops nouveau_bind_job_ops = {
1555 	.submit = nouveau_uvmm_bind_job_submit,
1556 	.armed_submit = nouveau_uvmm_bind_job_armed_submit,
1557 	.run = nouveau_uvmm_bind_job_run,
1558 	.free = nouveau_uvmm_bind_job_free_qwork,
1559 };
1560 
1561 static int
1562 bind_job_op_from_uop(struct bind_job_op **pop,
1563 		     struct drm_nouveau_vm_bind_op *uop)
1564 {
1565 	struct bind_job_op *op;
1566 
1567 	op = *pop = kzalloc(sizeof(*op), GFP_KERNEL);
1568 	if (!op)
1569 		return -ENOMEM;
1570 
1571 	switch (uop->op) {
1572 	case OP_MAP:
1573 		op->op = uop->flags & DRM_NOUVEAU_VM_BIND_SPARSE ?
1574 			 OP_MAP_SPARSE : OP_MAP;
1575 		break;
1576 	case OP_UNMAP:
1577 		op->op = uop->flags & DRM_NOUVEAU_VM_BIND_SPARSE ?
1578 			 OP_UNMAP_SPARSE : OP_UNMAP;
1579 		break;
1580 	default:
1581 		op->op = uop->op;
1582 		break;
1583 	}
1584 
1585 	op->flags = uop->flags;
1586 	op->va.addr = uop->addr;
1587 	op->va.range = uop->range;
1588 	op->gem.handle = uop->handle;
1589 	op->gem.offset = uop->bo_offset;
1590 
1591 	return 0;
1592 }
1593 
1594 static void
1595 bind_job_ops_free(struct list_head *ops)
1596 {
1597 	struct bind_job_op *op, *next;
1598 
1599 	list_for_each_op_safe(op, next, ops) {
1600 		list_del(&op->entry);
1601 		kfree(op);
1602 	}
1603 }
1604 
1605 static int
1606 nouveau_uvmm_bind_job_init(struct nouveau_uvmm_bind_job **pjob,
1607 			   struct nouveau_uvmm_bind_job_args *__args)
1608 {
1609 	struct nouveau_uvmm_bind_job *job;
1610 	struct nouveau_job_args args = {};
1611 	struct bind_job_op *op;
1612 	int i, ret;
1613 
1614 	ret = nouveau_uvmm_bind_job_alloc(&job);
1615 	if (ret)
1616 		return ret;
1617 
1618 	INIT_LIST_HEAD(&job->ops);
1619 	INIT_LIST_HEAD(&job->entry);
1620 
1621 	for (i = 0; i < __args->op.count; i++) {
1622 		ret = bind_job_op_from_uop(&op, &__args->op.s[i]);
1623 		if (ret)
1624 			goto err_free;
1625 
1626 		list_add_tail(&op->entry, &job->ops);
1627 	}
1628 
1629 	init_completion(&job->complete);
1630 	INIT_WORK(&job->work, nouveau_uvmm_bind_job_free_work_fn);
1631 
1632 	args.sched_entity = __args->sched_entity;
1633 	args.file_priv = __args->file_priv;
1634 
1635 	args.in_sync.count = __args->in_sync.count;
1636 	args.in_sync.s = __args->in_sync.s;
1637 
1638 	args.out_sync.count = __args->out_sync.count;
1639 	args.out_sync.s = __args->out_sync.s;
1640 
1641 	args.sync = !(__args->flags & DRM_NOUVEAU_VM_BIND_RUN_ASYNC);
1642 	args.ops = &nouveau_bind_job_ops;
1643 	args.resv_usage = DMA_RESV_USAGE_BOOKKEEP;
1644 
1645 	ret = nouveau_job_init(&job->base, &args);
1646 	if (ret)
1647 		goto err_free;
1648 
1649 	*pjob = job;
1650 	return 0;
1651 
1652 err_free:
1653 	bind_job_ops_free(&job->ops);
1654 	kfree(job);
1655 	*pjob = NULL;
1656 
1657 	return ret;
1658 }
1659 
1660 static int
1661 nouveau_uvmm_vm_bind(struct nouveau_uvmm_bind_job_args *args)
1662 {
1663 	struct nouveau_uvmm_bind_job *job;
1664 	int ret;
1665 
1666 	ret = nouveau_uvmm_bind_job_init(&job, args);
1667 	if (ret)
1668 		return ret;
1669 
1670 	ret = nouveau_job_submit(&job->base);
1671 	if (ret)
1672 		goto err_job_fini;
1673 
1674 	return 0;
1675 
1676 err_job_fini:
1677 	nouveau_job_fini(&job->base);
1678 	return ret;
1679 }
1680 
1681 static int
1682 nouveau_uvmm_vm_bind_ucopy(struct nouveau_uvmm_bind_job_args *args,
1683 			   struct drm_nouveau_vm_bind *req)
1684 {
1685 	struct drm_nouveau_sync **s;
1686 	u32 inc = req->wait_count;
1687 	u64 ins = req->wait_ptr;
1688 	u32 outc = req->sig_count;
1689 	u64 outs = req->sig_ptr;
1690 	u32 opc = req->op_count;
1691 	u64 ops = req->op_ptr;
1692 	int ret;
1693 
1694 	args->flags = req->flags;
1695 
1696 	if (opc) {
1697 		args->op.count = opc;
1698 		args->op.s = u_memcpya(ops, opc,
1699 				       sizeof(*args->op.s));
1700 		if (IS_ERR(args->op.s))
1701 			return PTR_ERR(args->op.s);
1702 	}
1703 
1704 	if (inc) {
1705 		s = &args->in_sync.s;
1706 
1707 		args->in_sync.count = inc;
1708 		*s = u_memcpya(ins, inc, sizeof(**s));
1709 		if (IS_ERR(*s)) {
1710 			ret = PTR_ERR(*s);
1711 			goto err_free_ops;
1712 		}
1713 	}
1714 
1715 	if (outc) {
1716 		s = &args->out_sync.s;
1717 
1718 		args->out_sync.count = outc;
1719 		*s = u_memcpya(outs, outc, sizeof(**s));
1720 		if (IS_ERR(*s)) {
1721 			ret = PTR_ERR(*s);
1722 			goto err_free_ins;
1723 		}
1724 	}
1725 
1726 	return 0;
1727 
1728 err_free_ops:
1729 	u_free(args->op.s);
1730 err_free_ins:
1731 	u_free(args->in_sync.s);
1732 	return ret;
1733 }
1734 
1735 static void
1736 nouveau_uvmm_vm_bind_ufree(struct nouveau_uvmm_bind_job_args *args)
1737 {
1738 	u_free(args->op.s);
1739 	u_free(args->in_sync.s);
1740 	u_free(args->out_sync.s);
1741 }
1742 
1743 int
1744 nouveau_uvmm_ioctl_vm_bind(struct drm_device *dev,
1745 			   void *data,
1746 			   struct drm_file *file_priv)
1747 {
1748 	struct nouveau_cli *cli = nouveau_cli(file_priv);
1749 	struct nouveau_uvmm_bind_job_args args = {};
1750 	struct drm_nouveau_vm_bind *req = data;
1751 	int ret = 0;
1752 
1753 	if (unlikely(!nouveau_cli_uvmm_locked(cli)))
1754 		return -ENOSYS;
1755 
1756 	ret = nouveau_uvmm_vm_bind_ucopy(&args, req);
1757 	if (ret)
1758 		return ret;
1759 
1760 	args.sched_entity = &cli->sched_entity;
1761 	args.file_priv = file_priv;
1762 
1763 	ret = nouveau_uvmm_vm_bind(&args);
1764 	if (ret)
1765 		goto out_free_args;
1766 
1767 out_free_args:
1768 	nouveau_uvmm_vm_bind_ufree(&args);
1769 	return ret;
1770 }
1771 
1772 void
1773 nouveau_uvmm_bo_map_all(struct nouveau_bo *nvbo, struct nouveau_mem *mem)
1774 {
1775 	struct drm_gem_object *obj = &nvbo->bo.base;
1776 	struct drm_gpuvm_bo *vm_bo;
1777 	struct drm_gpuva *va;
1778 
1779 	dma_resv_assert_held(obj->resv);
1780 
1781 	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
1782 		drm_gpuvm_bo_for_each_va(va, vm_bo) {
1783 			struct nouveau_uvma *uvma = uvma_from_va(va);
1784 
1785 			nouveau_uvma_map(uvma, mem);
1786 			drm_gpuva_invalidate(va, false);
1787 		}
1788 	}
1789 }
1790 
1791 void
1792 nouveau_uvmm_bo_unmap_all(struct nouveau_bo *nvbo)
1793 {
1794 	struct drm_gem_object *obj = &nvbo->bo.base;
1795 	struct drm_gpuvm_bo *vm_bo;
1796 	struct drm_gpuva *va;
1797 
1798 	dma_resv_assert_held(obj->resv);
1799 
1800 	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
1801 		drm_gpuvm_bo_for_each_va(va, vm_bo) {
1802 			struct nouveau_uvma *uvma = uvma_from_va(va);
1803 
1804 			nouveau_uvma_unmap(uvma);
1805 			drm_gpuva_invalidate(va, true);
1806 		}
1807 	}
1808 }
1809 
1810 static void
1811 nouveau_uvmm_free(struct drm_gpuvm *gpuvm)
1812 {
1813 	struct nouveau_uvmm *uvmm = uvmm_from_gpuvm(gpuvm);
1814 
1815 	kfree(uvmm);
1816 }
1817 
1818 static const struct drm_gpuvm_ops gpuvm_ops = {
1819 	.vm_free = nouveau_uvmm_free,
1820 };
1821 
1822 int
1823 nouveau_uvmm_ioctl_vm_init(struct drm_device *dev,
1824 			   void *data,
1825 			   struct drm_file *file_priv)
1826 {
1827 	struct nouveau_uvmm *uvmm;
1828 	struct nouveau_cli *cli = nouveau_cli(file_priv);
1829 	struct drm_device *drm = cli->drm->dev;
1830 	struct drm_gem_object *r_obj;
1831 	struct drm_nouveau_vm_init *init = data;
1832 	u64 kernel_managed_end;
1833 	int ret;
1834 
1835 	if (check_add_overflow(init->kernel_managed_addr,
1836 			       init->kernel_managed_size,
1837 			       &kernel_managed_end))
1838 		return -EINVAL;
1839 
1840 	if (kernel_managed_end > NOUVEAU_VA_SPACE_END)
1841 		return -EINVAL;
1842 
1843 	mutex_lock(&cli->mutex);
1844 
1845 	if (unlikely(cli->uvmm.disabled)) {
1846 		ret = -ENOSYS;
1847 		goto out_unlock;
1848 	}
1849 
1850 	uvmm = kzalloc(sizeof(*uvmm), GFP_KERNEL);
1851 	if (!uvmm) {
1852 		ret = -ENOMEM;
1853 		goto out_unlock;
1854 	}
1855 
1856 	r_obj = drm_gpuvm_resv_object_alloc(drm);
1857 	if (!r_obj) {
1858 		kfree(uvmm);
1859 		ret = -ENOMEM;
1860 		goto out_unlock;
1861 	}
1862 
1863 	mutex_init(&uvmm->mutex);
1864 	mt_init_flags(&uvmm->region_mt, MT_FLAGS_LOCK_EXTERN);
1865 	mt_set_external_lock(&uvmm->region_mt, &uvmm->mutex);
1866 
1867 	drm_gpuvm_init(&uvmm->base, cli->name, 0, drm, r_obj,
1868 		       NOUVEAU_VA_SPACE_START,
1869 		       NOUVEAU_VA_SPACE_END,
1870 		       init->kernel_managed_addr,
1871 		       init->kernel_managed_size,
1872 		       &gpuvm_ops);
1873 	/* GPUVM takes care from here on. */
1874 	drm_gem_object_put(r_obj);
1875 
1876 	ret = nvif_vmm_ctor(&cli->mmu, "uvmm",
1877 			    cli->vmm.vmm.object.oclass, RAW,
1878 			    init->kernel_managed_addr,
1879 			    init->kernel_managed_size,
1880 			    NULL, 0, &uvmm->vmm.vmm);
1881 	if (ret)
1882 		goto out_gpuvm_fini;
1883 
1884 	uvmm->vmm.cli = cli;
1885 	cli->uvmm.ptr = uvmm;
1886 	mutex_unlock(&cli->mutex);
1887 
1888 	return 0;
1889 
1890 out_gpuvm_fini:
1891 	drm_gpuvm_put(&uvmm->base);
1892 out_unlock:
1893 	mutex_unlock(&cli->mutex);
1894 	return ret;
1895 }
1896 
1897 void
1898 nouveau_uvmm_fini(struct nouveau_uvmm *uvmm)
1899 {
1900 	MA_STATE(mas, &uvmm->region_mt, 0, 0);
1901 	struct nouveau_uvma_region *reg;
1902 	struct nouveau_cli *cli = uvmm->vmm.cli;
1903 	struct nouveau_sched_entity *entity = &cli->sched_entity;
1904 	struct drm_gpuva *va, *next;
1905 
1906 	rmb(); /* for list_empty to work without lock */
1907 	wait_event(entity->job.wq, list_empty(&entity->job.list.head));
1908 
1909 	nouveau_uvmm_lock(uvmm);
1910 	drm_gpuvm_for_each_va_safe(va, next, &uvmm->base) {
1911 		struct nouveau_uvma *uvma = uvma_from_va(va);
1912 		struct drm_gem_object *obj = va->gem.obj;
1913 
1914 		if (unlikely(va == &uvmm->base.kernel_alloc_node))
1915 			continue;
1916 
1917 		drm_gpuva_remove(va);
1918 
1919 		dma_resv_lock(obj->resv, NULL);
1920 		drm_gpuva_unlink(va);
1921 		dma_resv_unlock(obj->resv);
1922 
1923 		nouveau_uvma_unmap(uvma);
1924 		nouveau_uvma_vmm_put(uvma);
1925 
1926 		nouveau_uvma_gem_put(uvma);
1927 		nouveau_uvma_free(uvma);
1928 	}
1929 
1930 	mas_for_each(&mas, reg, ULONG_MAX) {
1931 		mas_erase(&mas);
1932 		nouveau_uvma_region_sparse_unref(reg);
1933 		nouveau_uvma_region_put(reg);
1934 	}
1935 
1936 	WARN(!mtree_empty(&uvmm->region_mt),
1937 	     "nouveau_uvma_region tree not empty, potentially leaking memory.");
1938 	__mt_destroy(&uvmm->region_mt);
1939 	nouveau_uvmm_unlock(uvmm);
1940 
1941 	mutex_lock(&cli->mutex);
1942 	nouveau_vmm_fini(&uvmm->vmm);
1943 	drm_gpuvm_put(&uvmm->base);
1944 	mutex_unlock(&cli->mutex);
1945 }
1946