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