1 // SPDX-License-Identifier: MIT
6 * The uvmm mutex protects any operations on the GPU VA space provided by the
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.
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.
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.
23 #include "nouveau_drv.h"
24 #include "nouveau_gem.h"
25 #include "nouveau_mem.h"
26 #include "nouveau_uvmm.h"
31 #include <nvif/class.h>
32 #include <nvif/if000c.h>
33 #include <nvif/if900d.h>
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)
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)
47 OP_MAP = DRM_NOUVEAU_VM_BIND_OP_MAP,
48 OP_UNMAP = DRM_NOUVEAU_VM_BIND_OP_UNMAP,
53 struct nouveau_uvma_prealloc {
54 struct nouveau_uvma *map;
55 struct nouveau_uvma *prev;
56 struct nouveau_uvma *next;
60 struct list_head entry;
65 struct drm_gpuvm_bo *vm_bo;
75 struct drm_gem_object *obj;
78 struct nouveau_uvma_region *reg;
79 struct nouveau_uvma_prealloc new;
80 struct drm_gpuva_ops *ops;
83 struct uvmm_map_args {
84 struct nouveau_uvma_region *region;
91 nouveau_uvmm_vmm_sparse_ref(struct nouveau_uvmm *uvmm,
94 struct nvif_vmm *vmm = &uvmm->vmm.vmm;
96 return nvif_vmm_raw_sparse(vmm, addr, range, true);
100 nouveau_uvmm_vmm_sparse_unref(struct nouveau_uvmm *uvmm,
103 struct nvif_vmm *vmm = &uvmm->vmm.vmm;
105 return nvif_vmm_raw_sparse(vmm, addr, range, false);
109 nouveau_uvmm_vmm_get(struct nouveau_uvmm *uvmm,
112 struct nvif_vmm *vmm = &uvmm->vmm.vmm;
114 return nvif_vmm_raw_get(vmm, addr, range, PAGE_SHIFT);
118 nouveau_uvmm_vmm_put(struct nouveau_uvmm *uvmm,
121 struct nvif_vmm *vmm = &uvmm->vmm.vmm;
123 return nvif_vmm_raw_put(vmm, addr, range, PAGE_SHIFT);
127 nouveau_uvmm_vmm_unmap(struct nouveau_uvmm *uvmm,
128 u64 addr, u64 range, bool sparse)
130 struct nvif_vmm *vmm = &uvmm->vmm.vmm;
132 return nvif_vmm_raw_unmap(vmm, addr, range, PAGE_SHIFT, sparse);
136 nouveau_uvmm_vmm_map(struct nouveau_uvmm *uvmm,
138 u64 bo_offset, u8 kind,
139 struct nouveau_mem *mem)
141 struct nvif_vmm *vmm = &uvmm->vmm.vmm;
143 struct gf100_vmm_map_v0 gf100;
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)
158 args.gf100.kind = kind;
159 argc = sizeof(args.gf100);
166 return nvif_vmm_raw_map(vmm, addr, range, PAGE_SHIFT,
168 &mem->mem, bo_offset);
172 nouveau_uvma_region_sparse_unref(struct nouveau_uvma_region *reg)
174 u64 addr = reg->va.addr;
175 u64 range = reg->va.range;
177 return nouveau_uvmm_vmm_sparse_unref(reg->uvmm, addr, range);
181 nouveau_uvma_vmm_put(struct nouveau_uvma *uvma)
183 u64 addr = uvma->va.va.addr;
184 u64 range = uvma->va.va.range;
186 return nouveau_uvmm_vmm_put(to_uvmm(uvma), addr, range);
190 nouveau_uvma_map(struct nouveau_uvma *uvma,
191 struct nouveau_mem *mem)
193 u64 addr = uvma->va.va.addr;
194 u64 offset = uvma->va.gem.offset;
195 u64 range = uvma->va.va.range;
197 return nouveau_uvmm_vmm_map(to_uvmm(uvma), addr, range,
198 offset, uvma->kind, mem);
202 nouveau_uvma_unmap(struct nouveau_uvma *uvma)
204 u64 addr = uvma->va.va.addr;
205 u64 range = uvma->va.va.range;
206 bool sparse = !!uvma->region;
208 if (drm_gpuva_invalidated(&uvma->va))
211 return nouveau_uvmm_vmm_unmap(to_uvmm(uvma), addr, range, sparse);
215 nouveau_uvma_alloc(struct nouveau_uvma **puvma)
217 *puvma = kzalloc(sizeof(**puvma), GFP_KERNEL);
225 nouveau_uvma_free(struct nouveau_uvma *uvma)
231 nouveau_uvma_gem_get(struct nouveau_uvma *uvma)
233 drm_gem_object_get(uvma->va.gem.obj);
237 nouveau_uvma_gem_put(struct nouveau_uvma *uvma)
239 drm_gem_object_put(uvma->va.gem.obj);
243 nouveau_uvma_region_alloc(struct nouveau_uvma_region **preg)
245 *preg = kzalloc(sizeof(**preg), GFP_KERNEL);
249 kref_init(&(*preg)->kref);
255 nouveau_uvma_region_free(struct kref *kref)
257 struct nouveau_uvma_region *reg =
258 container_of(kref, struct nouveau_uvma_region, kref);
264 nouveau_uvma_region_get(struct nouveau_uvma_region *reg)
266 kref_get(®->kref);
270 nouveau_uvma_region_put(struct nouveau_uvma_region *reg)
272 kref_put(®->kref, nouveau_uvma_region_free);
276 __nouveau_uvma_region_insert(struct nouveau_uvmm *uvmm,
277 struct nouveau_uvma_region *reg)
279 u64 addr = reg->va.addr;
280 u64 range = reg->va.range;
281 u64 last = addr + range - 1;
282 MA_STATE(mas, &uvmm->region_mt, addr, addr);
284 if (unlikely(mas_walk(&mas)))
287 if (unlikely(mas.last < last))
293 mas_store_gfp(&mas, reg, GFP_KERNEL);
301 nouveau_uvma_region_insert(struct nouveau_uvmm *uvmm,
302 struct nouveau_uvma_region *reg,
309 reg->va.range = range;
311 ret = __nouveau_uvma_region_insert(uvmm, reg);
319 nouveau_uvma_region_remove(struct nouveau_uvma_region *reg)
321 struct nouveau_uvmm *uvmm = reg->uvmm;
322 MA_STATE(mas, &uvmm->region_mt, reg->va.addr, 0);
328 nouveau_uvma_region_create(struct nouveau_uvmm *uvmm,
331 struct nouveau_uvma_region *reg;
334 if (!drm_gpuvm_interval_empty(&uvmm->base, addr, range))
337 ret = nouveau_uvma_region_alloc(®);
341 ret = nouveau_uvma_region_insert(uvmm, reg, addr, range);
343 goto err_free_region;
345 ret = nouveau_uvmm_vmm_sparse_ref(uvmm, addr, range);
347 goto err_region_remove;
352 nouveau_uvma_region_remove(reg);
354 nouveau_uvma_region_put(reg);
358 static struct nouveau_uvma_region *
359 nouveau_uvma_region_find_first(struct nouveau_uvmm *uvmm,
362 MA_STATE(mas, &uvmm->region_mt, addr, 0);
364 return mas_find(&mas, addr + range - 1);
367 static struct nouveau_uvma_region *
368 nouveau_uvma_region_find(struct nouveau_uvmm *uvmm,
371 struct nouveau_uvma_region *reg;
373 reg = nouveau_uvma_region_find_first(uvmm, addr, range);
377 if (reg->va.addr != addr ||
378 reg->va.range != range)
385 nouveau_uvma_region_empty(struct nouveau_uvma_region *reg)
387 struct nouveau_uvmm *uvmm = reg->uvmm;
389 return drm_gpuvm_interval_empty(&uvmm->base,
395 __nouveau_uvma_region_destroy(struct nouveau_uvma_region *reg)
397 struct nouveau_uvmm *uvmm = reg->uvmm;
398 u64 addr = reg->va.addr;
399 u64 range = reg->va.range;
401 if (!nouveau_uvma_region_empty(reg))
404 nouveau_uvma_region_remove(reg);
405 nouveau_uvmm_vmm_sparse_unref(uvmm, addr, range);
406 nouveau_uvma_region_put(reg);
412 nouveau_uvma_region_destroy(struct nouveau_uvmm *uvmm,
415 struct nouveau_uvma_region *reg;
417 reg = nouveau_uvma_region_find(uvmm, addr, range);
421 return __nouveau_uvma_region_destroy(reg);
425 nouveau_uvma_region_dirty(struct nouveau_uvma_region *reg)
428 init_completion(®->complete);
433 nouveau_uvma_region_complete(struct nouveau_uvma_region *reg)
435 complete_all(®->complete);
439 op_map_prepare_unwind(struct nouveau_uvma *uvma)
441 struct drm_gpuva *va = &uvma->va;
442 nouveau_uvma_gem_put(uvma);
443 drm_gpuva_remove(va);
444 nouveau_uvma_free(uvma);
448 op_unmap_prepare_unwind(struct drm_gpuva *va)
450 drm_gpuva_insert(va->vm, va);
454 nouveau_uvmm_sm_prepare_unwind(struct nouveau_uvmm *uvmm,
455 struct nouveau_uvma_prealloc *new,
456 struct drm_gpuva_ops *ops,
457 struct drm_gpuva_op *last,
458 struct uvmm_map_args *args)
460 struct drm_gpuva_op *op = last;
461 u64 vmm_get_start = args ? args->addr : 0;
462 u64 vmm_get_end = args ? args->addr + args->range : 0;
464 /* Unwind GPUVA space. */
465 drm_gpuva_for_each_op_from_reverse(op, ops) {
467 case DRM_GPUVA_OP_MAP:
468 op_map_prepare_unwind(new->map);
470 case DRM_GPUVA_OP_REMAP: {
471 struct drm_gpuva_op_remap *r = &op->remap;
472 struct drm_gpuva *va = r->unmap->va;
475 op_map_prepare_unwind(new->next);
478 op_map_prepare_unwind(new->prev);
480 op_unmap_prepare_unwind(va);
483 case DRM_GPUVA_OP_UNMAP:
484 op_unmap_prepare_unwind(op->unmap.va);
491 /* Unmap operation don't allocate page tables, hence skip the following
497 drm_gpuva_for_each_op(op, ops) {
499 case DRM_GPUVA_OP_MAP: {
500 u64 vmm_get_range = vmm_get_end - vmm_get_start;
503 nouveau_uvmm_vmm_put(uvmm, vmm_get_start,
507 case DRM_GPUVA_OP_REMAP: {
508 struct drm_gpuva_op_remap *r = &op->remap;
509 struct drm_gpuva *va = r->unmap->va;
510 u64 ustart = va->va.addr;
511 u64 urange = va->va.range;
512 u64 uend = ustart + urange;
515 vmm_get_start = uend;
518 vmm_get_end = ustart;
520 if (r->prev && r->next)
521 vmm_get_start = vmm_get_end = 0;
525 case DRM_GPUVA_OP_UNMAP: {
526 struct drm_gpuva_op_unmap *u = &op->unmap;
527 struct drm_gpuva *va = u->va;
528 u64 ustart = va->va.addr;
529 u64 urange = va->va.range;
530 u64 uend = ustart + urange;
532 /* Nothing to do for mappings we merge with. */
533 if (uend == vmm_get_start ||
534 ustart == vmm_get_end)
537 if (ustart > vmm_get_start) {
538 u64 vmm_get_range = ustart - vmm_get_start;
540 nouveau_uvmm_vmm_put(uvmm, vmm_get_start,
543 vmm_get_start = uend;
556 nouveau_uvmm_sm_map_prepare_unwind(struct nouveau_uvmm *uvmm,
557 struct nouveau_uvma_prealloc *new,
558 struct drm_gpuva_ops *ops,
561 struct drm_gpuva_op *last = drm_gpuva_last_op(ops);
562 struct uvmm_map_args args = {
567 nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops, last, &args);
571 nouveau_uvmm_sm_unmap_prepare_unwind(struct nouveau_uvmm *uvmm,
572 struct nouveau_uvma_prealloc *new,
573 struct drm_gpuva_ops *ops)
575 struct drm_gpuva_op *last = drm_gpuva_last_op(ops);
577 nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops, last, NULL);
581 op_map_prepare(struct nouveau_uvmm *uvmm,
582 struct nouveau_uvma **puvma,
583 struct drm_gpuva_op_map *op,
584 struct uvmm_map_args *args)
586 struct nouveau_uvma *uvma;
589 ret = nouveau_uvma_alloc(&uvma);
593 uvma->region = args->region;
594 uvma->kind = args->kind;
596 drm_gpuva_map(&uvmm->base, &uvma->va, op);
598 /* Keep a reference until this uvma is destroyed. */
599 nouveau_uvma_gem_get(uvma);
606 op_unmap_prepare(struct drm_gpuva_op_unmap *u)
612 * Note: @args should not be NULL when calling for a map operation.
615 nouveau_uvmm_sm_prepare(struct nouveau_uvmm *uvmm,
616 struct nouveau_uvma_prealloc *new,
617 struct drm_gpuva_ops *ops,
618 struct uvmm_map_args *args)
620 struct drm_gpuva_op *op;
621 u64 vmm_get_start = args ? args->addr : 0;
622 u64 vmm_get_end = args ? args->addr + args->range : 0;
625 drm_gpuva_for_each_op(op, ops) {
627 case DRM_GPUVA_OP_MAP: {
628 u64 vmm_get_range = vmm_get_end - vmm_get_start;
630 ret = op_map_prepare(uvmm, &new->map, &op->map, args);
635 ret = nouveau_uvmm_vmm_get(uvmm, vmm_get_start,
638 op_map_prepare_unwind(new->map);
645 case DRM_GPUVA_OP_REMAP: {
646 struct drm_gpuva_op_remap *r = &op->remap;
647 struct drm_gpuva *va = r->unmap->va;
648 struct uvmm_map_args remap_args = {
649 .kind = uvma_from_va(va)->kind,
650 .region = uvma_from_va(va)->region,
652 u64 ustart = va->va.addr;
653 u64 urange = va->va.range;
654 u64 uend = ustart + urange;
656 op_unmap_prepare(r->unmap);
659 ret = op_map_prepare(uvmm, &new->prev, r->prev,
665 vmm_get_start = uend;
669 ret = op_map_prepare(uvmm, &new->next, r->next,
673 op_map_prepare_unwind(new->prev);
678 vmm_get_end = ustart;
681 if (args && (r->prev && r->next))
682 vmm_get_start = vmm_get_end = 0;
686 case DRM_GPUVA_OP_UNMAP: {
687 struct drm_gpuva_op_unmap *u = &op->unmap;
688 struct drm_gpuva *va = u->va;
689 u64 ustart = va->va.addr;
690 u64 urange = va->va.range;
691 u64 uend = ustart + urange;
698 /* Nothing to do for mappings we merge with. */
699 if (uend == vmm_get_start ||
700 ustart == vmm_get_end)
703 if (ustart > vmm_get_start) {
704 u64 vmm_get_range = ustart - vmm_get_start;
706 ret = nouveau_uvmm_vmm_get(uvmm, vmm_get_start,
709 op_unmap_prepare_unwind(va);
713 vmm_get_start = uend;
726 if (op != drm_gpuva_first_op(ops))
727 nouveau_uvmm_sm_prepare_unwind(uvmm, new, ops,
728 drm_gpuva_prev_op(op),
734 nouveau_uvmm_sm_map_prepare(struct nouveau_uvmm *uvmm,
735 struct nouveau_uvma_prealloc *new,
736 struct nouveau_uvma_region *region,
737 struct drm_gpuva_ops *ops,
738 u64 addr, u64 range, u8 kind)
740 struct uvmm_map_args args = {
747 return nouveau_uvmm_sm_prepare(uvmm, new, ops, &args);
751 nouveau_uvmm_sm_unmap_prepare(struct nouveau_uvmm *uvmm,
752 struct nouveau_uvma_prealloc *new,
753 struct drm_gpuva_ops *ops)
755 return nouveau_uvmm_sm_prepare(uvmm, new, ops, NULL);
758 static struct drm_gem_object *
759 op_gem_obj(struct drm_gpuva_op *op)
762 case DRM_GPUVA_OP_MAP:
763 return op->map.gem.obj;
764 case DRM_GPUVA_OP_REMAP:
765 /* Actually, we're looking for the GEMs backing remap.prev and
766 * remap.next, but since this is a remap they're identical to
767 * the GEM backing the unmapped GPUVA.
769 return op->remap.unmap->va->gem.obj;
770 case DRM_GPUVA_OP_UNMAP:
771 return op->unmap.va->gem.obj;
773 WARN(1, "Unknown operation.\n");
779 op_map(struct nouveau_uvma *uvma)
781 struct nouveau_bo *nvbo = nouveau_gem_object(uvma->va.gem.obj);
783 nouveau_uvma_map(uvma, nouveau_mem(nvbo->bo.resource));
787 op_unmap(struct drm_gpuva_op_unmap *u)
789 struct drm_gpuva *va = u->va;
790 struct nouveau_uvma *uvma = uvma_from_va(va);
792 /* nouveau_uvma_unmap() does not unmap if backing BO is evicted. */
794 nouveau_uvma_unmap(uvma);
798 op_unmap_range(struct drm_gpuva_op_unmap *u,
801 struct nouveau_uvma *uvma = uvma_from_va(u->va);
802 bool sparse = !!uvma->region;
804 if (!drm_gpuva_invalidated(u->va))
805 nouveau_uvmm_vmm_unmap(to_uvmm(uvma), addr, range, sparse);
809 op_remap(struct drm_gpuva_op_remap *r,
810 struct nouveau_uvma_prealloc *new)
812 struct drm_gpuva_op_unmap *u = r->unmap;
813 struct nouveau_uvma *uvma = uvma_from_va(u->va);
814 u64 addr = uvma->va.va.addr;
815 u64 end = uvma->va.va.addr + uvma->va.va.range;
818 addr = r->prev->va.addr + r->prev->va.range;
821 end = r->next->va.addr;
823 op_unmap_range(u, addr, end - addr);
827 nouveau_uvmm_sm(struct nouveau_uvmm *uvmm,
828 struct nouveau_uvma_prealloc *new,
829 struct drm_gpuva_ops *ops)
831 struct drm_gpuva_op *op;
833 drm_gpuva_for_each_op(op, ops) {
835 case DRM_GPUVA_OP_MAP:
838 case DRM_GPUVA_OP_REMAP:
839 op_remap(&op->remap, new);
841 case DRM_GPUVA_OP_UNMAP:
842 op_unmap(&op->unmap);
853 nouveau_uvmm_sm_map(struct nouveau_uvmm *uvmm,
854 struct nouveau_uvma_prealloc *new,
855 struct drm_gpuva_ops *ops)
857 return nouveau_uvmm_sm(uvmm, new, ops);
861 nouveau_uvmm_sm_unmap(struct nouveau_uvmm *uvmm,
862 struct nouveau_uvma_prealloc *new,
863 struct drm_gpuva_ops *ops)
865 return nouveau_uvmm_sm(uvmm, new, ops);
869 nouveau_uvmm_sm_cleanup(struct nouveau_uvmm *uvmm,
870 struct nouveau_uvma_prealloc *new,
871 struct drm_gpuva_ops *ops, bool unmap)
873 struct drm_gpuva_op *op;
875 drm_gpuva_for_each_op(op, ops) {
877 case DRM_GPUVA_OP_MAP:
879 case DRM_GPUVA_OP_REMAP: {
880 struct drm_gpuva_op_remap *r = &op->remap;
881 struct drm_gpuva_op_map *p = r->prev;
882 struct drm_gpuva_op_map *n = r->next;
883 struct drm_gpuva *va = r->unmap->va;
884 struct nouveau_uvma *uvma = uvma_from_va(va);
887 u64 addr = va->va.addr;
888 u64 end = addr + va->va.range;
891 addr = p->va.addr + p->va.range;
896 nouveau_uvmm_vmm_put(uvmm, addr, end - addr);
899 nouveau_uvma_gem_put(uvma);
900 nouveau_uvma_free(uvma);
903 case DRM_GPUVA_OP_UNMAP: {
904 struct drm_gpuva_op_unmap *u = &op->unmap;
905 struct drm_gpuva *va = u->va;
906 struct nouveau_uvma *uvma = uvma_from_va(va);
909 nouveau_uvma_vmm_put(uvma);
911 nouveau_uvma_gem_put(uvma);
912 nouveau_uvma_free(uvma);
922 nouveau_uvmm_sm_map_cleanup(struct nouveau_uvmm *uvmm,
923 struct nouveau_uvma_prealloc *new,
924 struct drm_gpuva_ops *ops)
926 nouveau_uvmm_sm_cleanup(uvmm, new, ops, false);
930 nouveau_uvmm_sm_unmap_cleanup(struct nouveau_uvmm *uvmm,
931 struct nouveau_uvma_prealloc *new,
932 struct drm_gpuva_ops *ops)
934 nouveau_uvmm_sm_cleanup(uvmm, new, ops, true);
938 nouveau_uvmm_validate_range(struct nouveau_uvmm *uvmm, u64 addr, u64 range)
940 if (addr & ~PAGE_MASK)
943 if (range & ~PAGE_MASK)
946 if (!drm_gpuvm_range_valid(&uvmm->base, addr, range))
953 nouveau_uvmm_bind_job_alloc(struct nouveau_uvmm_bind_job **pjob)
955 *pjob = kzalloc(sizeof(**pjob), GFP_KERNEL);
959 kref_init(&(*pjob)->kref);
965 nouveau_uvmm_bind_job_free(struct kref *kref)
967 struct nouveau_uvmm_bind_job *job =
968 container_of(kref, struct nouveau_uvmm_bind_job, kref);
969 struct bind_job_op *op, *next;
971 list_for_each_op_safe(op, next, &job->ops) {
972 list_del(&op->entry);
976 nouveau_job_free(&job->base);
981 nouveau_uvmm_bind_job_get(struct nouveau_uvmm_bind_job *job)
983 kref_get(&job->kref);
987 nouveau_uvmm_bind_job_put(struct nouveau_uvmm_bind_job *job)
989 kref_put(&job->kref, nouveau_uvmm_bind_job_free);
993 bind_validate_op(struct nouveau_job *job,
994 struct bind_job_op *op)
996 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
997 struct drm_gem_object *obj = op->gem.obj;
999 if (op->op == OP_MAP) {
1000 if (op->gem.offset & ~PAGE_MASK)
1003 if (obj->size <= op->gem.offset)
1006 if (op->va.range > (obj->size - op->gem.offset))
1010 return nouveau_uvmm_validate_range(uvmm, op->va.addr, op->va.range);
1014 bind_validate_map_sparse(struct nouveau_job *job, u64 addr, u64 range)
1016 struct nouveau_sched *sched = job->sched;
1017 struct nouveau_job *__job;
1018 struct bind_job_op *op;
1019 u64 end = addr + range;
1022 spin_lock(&sched->job.list.lock);
1023 list_for_each_entry(__job, &sched->job.list.head, entry) {
1024 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(__job);
1026 list_for_each_op(op, &bind_job->ops) {
1027 if (op->op == OP_UNMAP) {
1028 u64 op_addr = op->va.addr;
1029 u64 op_end = op_addr + op->va.range;
1031 if (!(end <= op_addr || addr >= op_end)) {
1032 nouveau_uvmm_bind_job_get(bind_job);
1033 spin_unlock(&sched->job.list.lock);
1034 wait_for_completion(&bind_job->complete);
1035 nouveau_uvmm_bind_job_put(bind_job);
1041 spin_unlock(&sched->job.list.lock);
1045 bind_validate_map_common(struct nouveau_job *job, u64 addr, u64 range,
1048 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
1049 struct nouveau_uvma_region *reg;
1050 u64 reg_addr, reg_end;
1051 u64 end = addr + range;
1054 nouveau_uvmm_lock(uvmm);
1055 reg = nouveau_uvma_region_find_first(uvmm, addr, range);
1057 nouveau_uvmm_unlock(uvmm);
1061 /* Generally, job submits are serialized, hence only
1062 * dirty regions can be modified concurrently.
1065 nouveau_uvma_region_get(reg);
1066 nouveau_uvmm_unlock(uvmm);
1067 wait_for_completion(®->complete);
1068 nouveau_uvma_region_put(reg);
1071 nouveau_uvmm_unlock(uvmm);
1076 reg_addr = reg->va.addr;
1077 reg_end = reg_addr + reg->va.range;
1079 /* Make sure the mapping is either outside of a
1080 * region or fully enclosed by a region.
1082 if (reg_addr > addr || reg_end < end)
1089 bind_validate_region(struct nouveau_job *job)
1091 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1092 struct bind_job_op *op;
1095 list_for_each_op(op, &bind_job->ops) {
1096 u64 op_addr = op->va.addr;
1097 u64 op_range = op->va.range;
1098 bool sparse = false;
1103 bind_validate_map_sparse(job, op_addr, op_range);
1106 ret = bind_validate_map_common(job, op_addr, op_range,
1120 bind_link_gpuvas(struct bind_job_op *bop)
1122 struct nouveau_uvma_prealloc *new = &bop->new;
1123 struct drm_gpuvm_bo *vm_bo = bop->vm_bo;
1124 struct drm_gpuva_ops *ops = bop->ops;
1125 struct drm_gpuva_op *op;
1127 drm_gpuva_for_each_op(op, ops) {
1129 case DRM_GPUVA_OP_MAP:
1130 drm_gpuva_link(&new->map->va, vm_bo);
1132 case DRM_GPUVA_OP_REMAP: {
1133 struct drm_gpuva *va = op->remap.unmap->va;
1136 drm_gpuva_link(&new->prev->va, va->vm_bo);
1138 drm_gpuva_link(&new->next->va, va->vm_bo);
1139 drm_gpuva_unlink(va);
1142 case DRM_GPUVA_OP_UNMAP:
1143 drm_gpuva_unlink(op->unmap.va);
1152 bind_lock_validate(struct nouveau_job *job, struct drm_exec *exec,
1153 unsigned int num_fences)
1155 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1156 struct bind_job_op *op;
1159 list_for_each_op(op, &bind_job->ops) {
1160 struct drm_gpuva_op *va_op;
1165 drm_gpuva_for_each_op(va_op, op->ops) {
1166 struct drm_gem_object *obj = op_gem_obj(va_op);
1171 ret = drm_exec_prepare_obj(exec, obj, num_fences);
1175 /* Don't validate GEMs backing mappings we're about to
1176 * unmap, it's not worth the effort.
1178 if (va_op->op == DRM_GPUVA_OP_UNMAP)
1181 ret = nouveau_bo_validate(nouveau_gem_object(obj),
1192 nouveau_uvmm_bind_job_submit(struct nouveau_job *job,
1193 struct drm_gpuvm_exec *vme)
1195 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
1196 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1197 struct drm_exec *exec = &vme->exec;
1198 struct bind_job_op *op;
1201 list_for_each_op(op, &bind_job->ops) {
1202 if (op->op == OP_MAP) {
1203 struct drm_gem_object *obj = op->gem.obj =
1204 drm_gem_object_lookup(job->file_priv,
1209 dma_resv_lock(obj->resv, NULL);
1210 op->vm_bo = drm_gpuvm_bo_obtain(&uvmm->base, obj);
1211 dma_resv_unlock(obj->resv);
1212 if (IS_ERR(op->vm_bo))
1213 return PTR_ERR(op->vm_bo);
1215 drm_gpuvm_bo_extobj_add(op->vm_bo);
1218 ret = bind_validate_op(job, op);
1223 /* If a sparse region or mapping overlaps a dirty region, we need to
1224 * wait for the region to complete the unbind process. This is due to
1225 * how page table management is currently implemented. A future
1226 * implementation might change this.
1228 ret = bind_validate_region(job);
1232 /* Once we start modifying the GPU VA space we need to keep holding the
1233 * uvmm lock until we can't fail anymore. This is due to the set of GPU
1234 * VA space changes must appear atomically and we need to be able to
1235 * unwind all GPU VA space changes on failure.
1237 nouveau_uvmm_lock(uvmm);
1239 list_for_each_op(op, &bind_job->ops) {
1242 ret = nouveau_uvma_region_create(uvmm,
1246 goto unwind_continue;
1249 case OP_UNMAP_SPARSE:
1250 op->reg = nouveau_uvma_region_find(uvmm, op->va.addr,
1252 if (!op->reg || op->reg->dirty) {
1254 goto unwind_continue;
1257 op->ops = drm_gpuvm_sm_unmap_ops_create(&uvmm->base,
1260 if (IS_ERR(op->ops)) {
1261 ret = PTR_ERR(op->ops);
1262 goto unwind_continue;
1265 ret = nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new,
1268 drm_gpuva_ops_free(&uvmm->base, op->ops);
1271 goto unwind_continue;
1274 nouveau_uvma_region_dirty(op->reg);
1278 struct nouveau_uvma_region *reg;
1280 reg = nouveau_uvma_region_find_first(uvmm,
1284 u64 reg_addr = reg->va.addr;
1285 u64 reg_end = reg_addr + reg->va.range;
1286 u64 op_addr = op->va.addr;
1287 u64 op_end = op_addr + op->va.range;
1289 if (unlikely(reg->dirty)) {
1291 goto unwind_continue;
1294 /* Make sure the mapping is either outside of a
1295 * region or fully enclosed by a region.
1297 if (reg_addr > op_addr || reg_end < op_end) {
1299 goto unwind_continue;
1303 op->ops = drm_gpuvm_sm_map_ops_create(&uvmm->base,
1308 if (IS_ERR(op->ops)) {
1309 ret = PTR_ERR(op->ops);
1310 goto unwind_continue;
1313 ret = nouveau_uvmm_sm_map_prepare(uvmm, &op->new,
1319 drm_gpuva_ops_free(&uvmm->base, op->ops);
1321 goto unwind_continue;
1327 op->ops = drm_gpuvm_sm_unmap_ops_create(&uvmm->base,
1330 if (IS_ERR(op->ops)) {
1331 ret = PTR_ERR(op->ops);
1332 goto unwind_continue;
1335 ret = nouveau_uvmm_sm_unmap_prepare(uvmm, &op->new,
1338 drm_gpuva_ops_free(&uvmm->base, op->ops);
1340 goto unwind_continue;
1346 goto unwind_continue;
1350 drm_exec_init(exec, vme->flags, 0);
1351 drm_exec_until_all_locked(exec) {
1352 ret = bind_lock_validate(job, exec, vme->num_fences);
1353 drm_exec_retry_on_contention(exec);
1355 op = list_last_op(&bind_job->ops);
1360 /* Link and unlink GPUVAs while holding the dma_resv lock.
1362 * As long as we validate() all GEMs and add fences to all GEMs DMA
1363 * reservations backing map and remap operations we can be sure there
1364 * won't be any concurrent (in)validations during job execution, hence
1365 * we're safe to check drm_gpuva_invalidated() within the fence
1366 * signalling critical path without holding a separate lock.
1368 * GPUVAs about to be unmapped are safe as well, since they're unlinked
1371 * GEMs from map and remap operations must be validated before linking
1372 * their corresponding mappings to prevent the actual PT update to
1373 * happen right away in validate() rather than asynchronously as
1376 * Note that after linking and unlinking the GPUVAs in this loop this
1377 * function cannot fail anymore, hence there is no need for an unwind
1380 list_for_each_op(op, &bind_job->ops) {
1382 case OP_UNMAP_SPARSE:
1385 bind_link_gpuvas(op);
1391 nouveau_uvmm_unlock(uvmm);
1396 op = list_prev_op(op);
1398 list_for_each_op_from_reverse(op, &bind_job->ops) {
1401 nouveau_uvma_region_destroy(uvmm, op->va.addr,
1404 case OP_UNMAP_SPARSE:
1405 __nouveau_uvma_region_insert(uvmm, op->reg);
1406 nouveau_uvmm_sm_unmap_prepare_unwind(uvmm, &op->new,
1410 nouveau_uvmm_sm_map_prepare_unwind(uvmm, &op->new,
1416 nouveau_uvmm_sm_unmap_prepare_unwind(uvmm, &op->new,
1421 drm_gpuva_ops_free(&uvmm->base, op->ops);
1426 nouveau_uvmm_unlock(uvmm);
1427 drm_gpuvm_exec_unlock(vme);
1432 nouveau_uvmm_bind_job_armed_submit(struct nouveau_job *job,
1433 struct drm_gpuvm_exec *vme)
1435 drm_gpuvm_exec_resv_add_fence(vme, job->done_fence,
1436 job->resv_usage, job->resv_usage);
1437 drm_gpuvm_exec_unlock(vme);
1440 static struct dma_fence *
1441 nouveau_uvmm_bind_job_run(struct nouveau_job *job)
1443 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1444 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
1445 struct bind_job_op *op;
1448 list_for_each_op(op, &bind_job->ops) {
1454 ret = nouveau_uvmm_sm_map(uvmm, &op->new, op->ops);
1458 case OP_UNMAP_SPARSE:
1461 ret = nouveau_uvmm_sm_unmap(uvmm, &op->new, op->ops);
1470 NV_PRINTK(err, job->cli, "bind job failed: %d\n", ret);
1471 return ERR_PTR(ret);
1475 nouveau_uvmm_bind_job_cleanup(struct nouveau_job *job)
1477 struct nouveau_uvmm_bind_job *bind_job = to_uvmm_bind_job(job);
1478 struct nouveau_uvmm *uvmm = nouveau_cli_uvmm(job->cli);
1479 struct bind_job_op *op;
1481 list_for_each_op(op, &bind_job->ops) {
1482 struct drm_gem_object *obj = op->gem.obj;
1484 /* When nouveau_uvmm_bind_job_submit() fails op->ops and op->reg
1485 * will be NULL, hence skip the cleanup.
1491 case OP_UNMAP_SPARSE:
1492 if (!IS_ERR_OR_NULL(op->ops))
1493 nouveau_uvmm_sm_unmap_cleanup(uvmm, &op->new,
1497 nouveau_uvma_region_sparse_unref(op->reg);
1498 nouveau_uvmm_lock(uvmm);
1499 nouveau_uvma_region_remove(op->reg);
1500 nouveau_uvmm_unlock(uvmm);
1501 nouveau_uvma_region_complete(op->reg);
1502 nouveau_uvma_region_put(op->reg);
1507 if (!IS_ERR_OR_NULL(op->ops))
1508 nouveau_uvmm_sm_map_cleanup(uvmm, &op->new,
1512 if (!IS_ERR_OR_NULL(op->ops))
1513 nouveau_uvmm_sm_unmap_cleanup(uvmm, &op->new,
1518 if (!IS_ERR_OR_NULL(op->ops))
1519 drm_gpuva_ops_free(&uvmm->base, op->ops);
1521 if (!IS_ERR_OR_NULL(op->vm_bo)) {
1522 dma_resv_lock(obj->resv, NULL);
1523 drm_gpuvm_bo_put(op->vm_bo);
1524 dma_resv_unlock(obj->resv);
1528 drm_gem_object_put(obj);
1531 nouveau_job_done(job);
1532 complete_all(&bind_job->complete);
1534 nouveau_uvmm_bind_job_put(bind_job);
1537 static struct nouveau_job_ops nouveau_bind_job_ops = {
1538 .submit = nouveau_uvmm_bind_job_submit,
1539 .armed_submit = nouveau_uvmm_bind_job_armed_submit,
1540 .run = nouveau_uvmm_bind_job_run,
1541 .free = nouveau_uvmm_bind_job_cleanup,
1545 bind_job_op_from_uop(struct bind_job_op **pop,
1546 struct drm_nouveau_vm_bind_op *uop)
1548 struct bind_job_op *op;
1550 op = *pop = kzalloc(sizeof(*op), GFP_KERNEL);
1556 op->op = uop->flags & DRM_NOUVEAU_VM_BIND_SPARSE ?
1557 OP_MAP_SPARSE : OP_MAP;
1560 op->op = uop->flags & DRM_NOUVEAU_VM_BIND_SPARSE ?
1561 OP_UNMAP_SPARSE : OP_UNMAP;
1568 op->flags = uop->flags;
1569 op->va.addr = uop->addr;
1570 op->va.range = uop->range;
1571 op->gem.handle = uop->handle;
1572 op->gem.offset = uop->bo_offset;
1578 bind_job_ops_free(struct list_head *ops)
1580 struct bind_job_op *op, *next;
1582 list_for_each_op_safe(op, next, ops) {
1583 list_del(&op->entry);
1589 nouveau_uvmm_bind_job_init(struct nouveau_uvmm_bind_job **pjob,
1590 struct nouveau_uvmm_bind_job_args *__args)
1592 struct nouveau_uvmm_bind_job *job;
1593 struct nouveau_job_args args = {};
1594 struct bind_job_op *op;
1597 ret = nouveau_uvmm_bind_job_alloc(&job);
1601 INIT_LIST_HEAD(&job->ops);
1603 for (i = 0; i < __args->op.count; i++) {
1604 ret = bind_job_op_from_uop(&op, &__args->op.s[i]);
1608 list_add_tail(&op->entry, &job->ops);
1611 init_completion(&job->complete);
1613 args.file_priv = __args->file_priv;
1615 args.sched = __args->sched;
1618 args.in_sync.count = __args->in_sync.count;
1619 args.in_sync.s = __args->in_sync.s;
1621 args.out_sync.count = __args->out_sync.count;
1622 args.out_sync.s = __args->out_sync.s;
1624 args.sync = !(__args->flags & DRM_NOUVEAU_VM_BIND_RUN_ASYNC);
1625 args.ops = &nouveau_bind_job_ops;
1626 args.resv_usage = DMA_RESV_USAGE_BOOKKEEP;
1628 ret = nouveau_job_init(&job->base, &args);
1636 bind_job_ops_free(&job->ops);
1644 nouveau_uvmm_vm_bind(struct nouveau_uvmm_bind_job_args *args)
1646 struct nouveau_uvmm_bind_job *job;
1649 ret = nouveau_uvmm_bind_job_init(&job, args);
1653 ret = nouveau_job_submit(&job->base);
1660 nouveau_job_fini(&job->base);
1665 nouveau_uvmm_vm_bind_ucopy(struct nouveau_uvmm_bind_job_args *args,
1666 struct drm_nouveau_vm_bind *req)
1668 struct drm_nouveau_sync **s;
1669 u32 inc = req->wait_count;
1670 u64 ins = req->wait_ptr;
1671 u32 outc = req->sig_count;
1672 u64 outs = req->sig_ptr;
1673 u32 opc = req->op_count;
1674 u64 ops = req->op_ptr;
1677 args->flags = req->flags;
1680 args->op.count = opc;
1681 args->op.s = u_memcpya(ops, opc,
1682 sizeof(*args->op.s));
1683 if (IS_ERR(args->op.s))
1684 return PTR_ERR(args->op.s);
1688 s = &args->in_sync.s;
1690 args->in_sync.count = inc;
1691 *s = u_memcpya(ins, inc, sizeof(**s));
1699 s = &args->out_sync.s;
1701 args->out_sync.count = outc;
1702 *s = u_memcpya(outs, outc, sizeof(**s));
1714 u_free(args->in_sync.s);
1719 nouveau_uvmm_vm_bind_ufree(struct nouveau_uvmm_bind_job_args *args)
1722 u_free(args->in_sync.s);
1723 u_free(args->out_sync.s);
1727 nouveau_uvmm_ioctl_vm_bind(struct drm_device *dev,
1729 struct drm_file *file_priv)
1731 struct nouveau_cli *cli = nouveau_cli(file_priv);
1732 struct nouveau_uvmm_bind_job_args args = {};
1733 struct drm_nouveau_vm_bind *req = data;
1736 if (unlikely(!nouveau_cli_uvmm_locked(cli)))
1739 ret = nouveau_uvmm_vm_bind_ucopy(&args, req);
1743 args.sched = cli->sched;
1744 args.file_priv = file_priv;
1746 ret = nouveau_uvmm_vm_bind(&args);
1751 nouveau_uvmm_vm_bind_ufree(&args);
1756 nouveau_uvmm_bo_map_all(struct nouveau_bo *nvbo, struct nouveau_mem *mem)
1758 struct drm_gem_object *obj = &nvbo->bo.base;
1759 struct drm_gpuvm_bo *vm_bo;
1760 struct drm_gpuva *va;
1762 dma_resv_assert_held(obj->resv);
1764 drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
1765 drm_gpuvm_bo_for_each_va(va, vm_bo) {
1766 struct nouveau_uvma *uvma = uvma_from_va(va);
1768 nouveau_uvma_map(uvma, mem);
1769 drm_gpuva_invalidate(va, false);
1775 nouveau_uvmm_bo_unmap_all(struct nouveau_bo *nvbo)
1777 struct drm_gem_object *obj = &nvbo->bo.base;
1778 struct drm_gpuvm_bo *vm_bo;
1779 struct drm_gpuva *va;
1781 dma_resv_assert_held(obj->resv);
1783 drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
1784 drm_gpuvm_bo_for_each_va(va, vm_bo) {
1785 struct nouveau_uvma *uvma = uvma_from_va(va);
1787 nouveau_uvma_unmap(uvma);
1788 drm_gpuva_invalidate(va, true);
1794 nouveau_uvmm_free(struct drm_gpuvm *gpuvm)
1796 struct nouveau_uvmm *uvmm = uvmm_from_gpuvm(gpuvm);
1802 nouveau_uvmm_bo_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec)
1804 struct nouveau_bo *nvbo = nouveau_gem_object(vm_bo->obj);
1806 return nouveau_bo_validate(nvbo, true, false);
1809 static const struct drm_gpuvm_ops gpuvm_ops = {
1810 .vm_free = nouveau_uvmm_free,
1811 .vm_bo_validate = nouveau_uvmm_bo_validate,
1815 nouveau_uvmm_ioctl_vm_init(struct drm_device *dev,
1817 struct drm_file *file_priv)
1819 struct nouveau_uvmm *uvmm;
1820 struct nouveau_cli *cli = nouveau_cli(file_priv);
1821 struct drm_device *drm = cli->drm->dev;
1822 struct drm_gem_object *r_obj;
1823 struct drm_nouveau_vm_init *init = data;
1824 u64 kernel_managed_end;
1827 if (check_add_overflow(init->kernel_managed_addr,
1828 init->kernel_managed_size,
1829 &kernel_managed_end))
1832 if (kernel_managed_end > NOUVEAU_VA_SPACE_END)
1835 mutex_lock(&cli->mutex);
1837 if (unlikely(cli->uvmm.disabled)) {
1842 uvmm = kzalloc(sizeof(*uvmm), GFP_KERNEL);
1848 r_obj = drm_gpuvm_resv_object_alloc(drm);
1855 mutex_init(&uvmm->mutex);
1856 mt_init_flags(&uvmm->region_mt, MT_FLAGS_LOCK_EXTERN);
1857 mt_set_external_lock(&uvmm->region_mt, &uvmm->mutex);
1859 drm_gpuvm_init(&uvmm->base, cli->name, 0, drm, r_obj,
1860 NOUVEAU_VA_SPACE_START,
1861 NOUVEAU_VA_SPACE_END,
1862 init->kernel_managed_addr,
1863 init->kernel_managed_size,
1865 /* GPUVM takes care from here on. */
1866 drm_gem_object_put(r_obj);
1868 ret = nvif_vmm_ctor(&cli->mmu, "uvmm",
1869 cli->vmm.vmm.object.oclass, RAW,
1870 init->kernel_managed_addr,
1871 init->kernel_managed_size,
1872 NULL, 0, &uvmm->vmm.vmm);
1874 goto out_gpuvm_fini;
1876 uvmm->vmm.cli = cli;
1877 cli->uvmm.ptr = uvmm;
1878 mutex_unlock(&cli->mutex);
1883 drm_gpuvm_put(&uvmm->base);
1885 mutex_unlock(&cli->mutex);
1890 nouveau_uvmm_fini(struct nouveau_uvmm *uvmm)
1892 MA_STATE(mas, &uvmm->region_mt, 0, 0);
1893 struct nouveau_uvma_region *reg;
1894 struct nouveau_cli *cli = uvmm->vmm.cli;
1895 struct drm_gpuva *va, *next;
1897 nouveau_uvmm_lock(uvmm);
1898 drm_gpuvm_for_each_va_safe(va, next, &uvmm->base) {
1899 struct nouveau_uvma *uvma = uvma_from_va(va);
1900 struct drm_gem_object *obj = va->gem.obj;
1902 if (unlikely(va == &uvmm->base.kernel_alloc_node))
1905 drm_gpuva_remove(va);
1907 dma_resv_lock(obj->resv, NULL);
1908 drm_gpuva_unlink(va);
1909 dma_resv_unlock(obj->resv);
1911 nouveau_uvma_unmap(uvma);
1912 nouveau_uvma_vmm_put(uvma);
1914 nouveau_uvma_gem_put(uvma);
1915 nouveau_uvma_free(uvma);
1918 mas_for_each(&mas, reg, ULONG_MAX) {
1920 nouveau_uvma_region_sparse_unref(reg);
1921 nouveau_uvma_region_put(reg);
1924 WARN(!mtree_empty(&uvmm->region_mt),
1925 "nouveau_uvma_region tree not empty, potentially leaking memory.");
1926 __mt_destroy(&uvmm->region_mt);
1927 nouveau_uvmm_unlock(uvmm);
1929 mutex_lock(&cli->mutex);
1930 nouveau_vmm_fini(&uvmm->vmm);
1931 drm_gpuvm_put(&uvmm->base);
1932 mutex_unlock(&cli->mutex);