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