2 * Copyright 2018 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 #include "nouveau_svm.h"
23 #include "nouveau_drv.h"
24 #include "nouveau_chan.h"
25 #include "nouveau_dmem.h"
27 #include <nvif/notify.h>
28 #include <nvif/object.h>
31 #include <nvif/class.h>
32 #include <nvif/clb069.h>
33 #include <nvif/ifc00d.h>
35 #include <linux/sched/mm.h>
36 #include <linux/sort.h>
37 #include <linux/hmm.h>
40 struct nouveau_drm *drm;
42 struct list_head inst;
44 struct nouveau_svm_fault_buffer {
46 struct nvif_object object;
52 struct nvif_notify notify;
54 struct nouveau_svm_fault {
64 struct nouveau_svmm *svmm;
70 #define SVM_DBG(s,f,a...) NV_DEBUG((s)->drm, "svm: "f"\n", ##a)
71 #define SVM_ERR(s,f,a...) NV_WARN((s)->drm, "svm: "f"\n", ##a)
74 struct nouveau_svmm *svmm;
76 struct list_head head;
79 static struct nouveau_ivmm *
80 nouveau_ivmm_find(struct nouveau_svm *svm, u64 inst)
82 struct nouveau_ivmm *ivmm;
83 list_for_each_entry(ivmm, &svm->inst, head) {
84 if (ivmm->inst == inst)
91 struct mmu_notifier notifier;
92 struct nouveau_vmm *vmm;
101 #define SVMM_DBG(s,f,a...) \
102 NV_DEBUG((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
103 #define SVMM_ERR(s,f,a...) \
104 NV_WARN((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
107 nouveau_svmm_bind(struct drm_device *dev, void *data,
108 struct drm_file *file_priv)
110 struct nouveau_cli *cli = nouveau_cli(file_priv);
111 struct drm_nouveau_svm_bind *args = data;
112 unsigned target, cmd, priority;
113 unsigned long addr, end, size;
114 struct mm_struct *mm;
116 args->va_start &= PAGE_MASK;
117 args->va_end &= PAGE_MASK;
119 /* Sanity check arguments */
120 if (args->reserved0 || args->reserved1)
122 if (args->header & (~NOUVEAU_SVM_BIND_VALID_MASK))
124 if (args->va_start >= args->va_end)
129 cmd = args->header >> NOUVEAU_SVM_BIND_COMMAND_SHIFT;
130 cmd &= NOUVEAU_SVM_BIND_COMMAND_MASK;
132 case NOUVEAU_SVM_BIND_COMMAND__MIGRATE:
138 priority = args->header >> NOUVEAU_SVM_BIND_PRIORITY_SHIFT;
139 priority &= NOUVEAU_SVM_BIND_PRIORITY_MASK;
141 /* FIXME support CPU target ie all target value < GPU_VRAM */
142 target = args->header >> NOUVEAU_SVM_BIND_TARGET_SHIFT;
143 target &= NOUVEAU_SVM_BIND_TARGET_MASK;
145 case NOUVEAU_SVM_BIND_TARGET__GPU_VRAM:
152 * FIXME: For now refuse non 0 stride, we need to change the migrate
153 * kernel function to handle stride to avoid to create a mess within
154 * each device driver.
159 size = ((unsigned long)args->npages) << PAGE_SHIFT;
160 if ((args->va_start + size) <= args->va_start)
162 if ((args->va_start + size) > args->va_end)
166 * Ok we are ask to do something sane, for now we only support migrate
167 * commands but we will add things like memory policy (what to do on
168 * page fault) and maybe some other commands.
171 mm = get_task_mm(current);
172 down_read(&mm->mmap_sem);
174 if (!cli->svm.svmm) {
175 up_read(&mm->mmap_sem);
179 for (addr = args->va_start, end = args->va_start + size; addr < end;) {
180 struct vm_area_struct *vma;
183 vma = find_vma_intersection(mm, addr, end);
187 addr = max(addr, vma->vm_start);
188 next = min(vma->vm_end, end);
189 /* This is a best effort so we ignore errors */
190 nouveau_dmem_migrate_vma(cli->drm, vma, addr, next);
195 * FIXME Return the number of page we have migrated, again we need to
196 * update the migrate API to return that information so that we can
197 * report it to user space.
201 up_read(&mm->mmap_sem);
207 /* Unlink channel instance from SVMM. */
209 nouveau_svmm_part(struct nouveau_svmm *svmm, u64 inst)
211 struct nouveau_ivmm *ivmm;
213 mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
214 ivmm = nouveau_ivmm_find(svmm->vmm->cli->drm->svm, inst);
216 list_del(&ivmm->head);
219 mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
223 /* Link channel instance to SVMM. */
225 nouveau_svmm_join(struct nouveau_svmm *svmm, u64 inst)
227 struct nouveau_ivmm *ivmm;
229 if (!(ivmm = kmalloc(sizeof(*ivmm), GFP_KERNEL)))
234 mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
235 list_add(&ivmm->head, &svmm->vmm->cli->drm->svm->inst);
236 mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
241 /* Invalidate SVMM address-range on GPU. */
243 nouveau_svmm_invalidate(struct nouveau_svmm *svmm, u64 start, u64 limit)
246 bool super = svmm->vmm->vmm.object.client->super;
247 svmm->vmm->vmm.object.client->super = true;
248 nvif_object_mthd(&svmm->vmm->vmm.object, NVIF_VMM_V0_PFNCLR,
249 &(struct nvif_vmm_pfnclr_v0) {
251 .size = limit - start,
252 }, sizeof(struct nvif_vmm_pfnclr_v0));
253 svmm->vmm->vmm.object.client->super = super;
258 nouveau_svmm_invalidate_range_start(struct mmu_notifier *mn,
259 const struct mmu_notifier_range *update)
261 struct nouveau_svmm *svmm =
262 container_of(mn, struct nouveau_svmm, notifier);
263 unsigned long start = update->start;
264 unsigned long limit = update->end;
266 if (!mmu_notifier_range_blockable(update))
269 SVMM_DBG(svmm, "invalidate %016lx-%016lx", start, limit);
271 mutex_lock(&svmm->mutex);
272 if (unlikely(!svmm->vmm))
275 if (limit > svmm->unmanaged.start && start < svmm->unmanaged.limit) {
276 if (start < svmm->unmanaged.start) {
277 nouveau_svmm_invalidate(svmm, start,
278 svmm->unmanaged.limit);
280 start = svmm->unmanaged.limit;
283 nouveau_svmm_invalidate(svmm, start, limit);
286 mutex_unlock(&svmm->mutex);
290 static void nouveau_svmm_free_notifier(struct mmu_notifier *mn)
292 kfree(container_of(mn, struct nouveau_svmm, notifier));
295 static const struct mmu_notifier_ops nouveau_mn_ops = {
296 .invalidate_range_start = nouveau_svmm_invalidate_range_start,
297 .free_notifier = nouveau_svmm_free_notifier,
301 nouveau_svmm_fini(struct nouveau_svmm **psvmm)
303 struct nouveau_svmm *svmm = *psvmm;
305 mutex_lock(&svmm->mutex);
307 mutex_unlock(&svmm->mutex);
308 mmu_notifier_put(&svmm->notifier);
314 nouveau_svmm_init(struct drm_device *dev, void *data,
315 struct drm_file *file_priv)
317 struct nouveau_cli *cli = nouveau_cli(file_priv);
318 struct nouveau_svmm *svmm;
319 struct drm_nouveau_svm_init *args = data;
322 /* Allocate tracking for SVM-enabled VMM. */
323 if (!(svmm = kzalloc(sizeof(*svmm), GFP_KERNEL)))
325 svmm->vmm = &cli->svm;
326 svmm->unmanaged.start = args->unmanaged_addr;
327 svmm->unmanaged.limit = args->unmanaged_addr + args->unmanaged_size;
328 mutex_init(&svmm->mutex);
330 /* Check that SVM isn't already enabled for the client. */
331 mutex_lock(&cli->mutex);
337 /* Allocate a new GPU VMM that can support SVM (managed by the
338 * client, with replayable faults enabled).
340 * All future channel/memory allocations will make use of this
341 * VMM instead of the standard one.
343 ret = nvif_vmm_init(&cli->mmu, cli->vmm.vmm.object.oclass, true,
344 args->unmanaged_addr, args->unmanaged_size,
345 &(struct gp100_vmm_v0) {
346 .fault_replay = true,
347 }, sizeof(struct gp100_vmm_v0), &cli->svm.vmm);
351 down_write(¤t->mm->mmap_sem);
352 svmm->notifier.ops = &nouveau_mn_ops;
353 ret = __mmu_notifier_register(&svmm->notifier, current->mm);
356 /* Note, ownership of svmm transfers to mmu_notifier */
358 cli->svm.svmm = svmm;
360 up_write(¤t->mm->mmap_sem);
361 mutex_unlock(&cli->mutex);
365 up_write(¤t->mm->mmap_sem);
367 mutex_unlock(&cli->mutex);
373 nouveau_svm_pfn_flags[HMM_PFN_FLAG_MAX] = {
374 [HMM_PFN_VALID ] = NVIF_VMM_PFNMAP_V0_V,
375 [HMM_PFN_WRITE ] = NVIF_VMM_PFNMAP_V0_W,
379 nouveau_svm_pfn_values[HMM_PFN_VALUE_MAX] = {
380 [HMM_PFN_ERROR ] = ~NVIF_VMM_PFNMAP_V0_V,
381 [HMM_PFN_NONE ] = NVIF_VMM_PFNMAP_V0_NONE,
382 [HMM_PFN_SPECIAL] = ~NVIF_VMM_PFNMAP_V0_V,
385 /* Issue fault replay for GPU to retry accesses that faulted previously. */
387 nouveau_svm_fault_replay(struct nouveau_svm *svm)
389 SVM_DBG(svm, "replay");
390 WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
391 GP100_VMM_VN_FAULT_REPLAY,
392 &(struct gp100_vmm_fault_replay_vn) {},
393 sizeof(struct gp100_vmm_fault_replay_vn)));
396 /* Cancel a replayable fault that could not be handled.
398 * Cancelling the fault will trigger recovery to reset the engine
399 * and kill the offending channel (ie. GPU SIGSEGV).
402 nouveau_svm_fault_cancel(struct nouveau_svm *svm,
403 u64 inst, u8 hub, u8 gpc, u8 client)
405 SVM_DBG(svm, "cancel %016llx %d %02x %02x", inst, hub, gpc, client);
406 WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
407 GP100_VMM_VN_FAULT_CANCEL,
408 &(struct gp100_vmm_fault_cancel_v0) {
413 }, sizeof(struct gp100_vmm_fault_cancel_v0)));
417 nouveau_svm_fault_cancel_fault(struct nouveau_svm *svm,
418 struct nouveau_svm_fault *fault)
420 nouveau_svm_fault_cancel(svm, fault->inst,
427 nouveau_svm_fault_cmp(const void *a, const void *b)
429 const struct nouveau_svm_fault *fa = *(struct nouveau_svm_fault **)a;
430 const struct nouveau_svm_fault *fb = *(struct nouveau_svm_fault **)b;
432 if ((ret = (s64)fa->inst - fb->inst))
434 if ((ret = (s64)fa->addr - fb->addr))
437 return (fa->access == 0 || fa->access == 3) -
438 (fb->access == 0 || fb->access == 3);
442 nouveau_svm_fault_cache(struct nouveau_svm *svm,
443 struct nouveau_svm_fault_buffer *buffer, u32 offset)
445 struct nvif_object *memory = &buffer->object;
446 const u32 instlo = nvif_rd32(memory, offset + 0x00);
447 const u32 insthi = nvif_rd32(memory, offset + 0x04);
448 const u32 addrlo = nvif_rd32(memory, offset + 0x08);
449 const u32 addrhi = nvif_rd32(memory, offset + 0x0c);
450 const u32 timelo = nvif_rd32(memory, offset + 0x10);
451 const u32 timehi = nvif_rd32(memory, offset + 0x14);
452 const u32 engine = nvif_rd32(memory, offset + 0x18);
453 const u32 info = nvif_rd32(memory, offset + 0x1c);
454 const u64 inst = (u64)insthi << 32 | instlo;
455 const u8 gpc = (info & 0x1f000000) >> 24;
456 const u8 hub = (info & 0x00100000) >> 20;
457 const u8 client = (info & 0x00007f00) >> 8;
458 struct nouveau_svm_fault *fault;
460 //XXX: i think we're supposed to spin waiting */
461 if (WARN_ON(!(info & 0x80000000)))
464 nvif_mask(memory, offset + 0x1c, 0x80000000, 0x00000000);
466 if (!buffer->fault[buffer->fault_nr]) {
467 fault = kmalloc(sizeof(*fault), GFP_KERNEL);
468 if (WARN_ON(!fault)) {
469 nouveau_svm_fault_cancel(svm, inst, hub, gpc, client);
472 buffer->fault[buffer->fault_nr] = fault;
475 fault = buffer->fault[buffer->fault_nr++];
477 fault->addr = (u64)addrhi << 32 | addrlo;
478 fault->time = (u64)timehi << 32 | timelo;
479 fault->engine = engine;
482 fault->access = (info & 0x000f0000) >> 16;
483 fault->client = client;
484 fault->fault = (info & 0x0000001f);
486 SVM_DBG(svm, "fault %016llx %016llx %02x",
487 fault->inst, fault->addr, fault->access);
490 struct svm_notifier {
491 struct mmu_interval_notifier notifier;
492 struct nouveau_svmm *svmm;
495 static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni,
496 const struct mmu_notifier_range *range,
497 unsigned long cur_seq)
499 struct svm_notifier *sn =
500 container_of(mni, struct svm_notifier, notifier);
503 * serializes the update to mni->invalidate_seq done by caller and
504 * prevents invalidation of the PTE from progressing while HW is being
505 * programmed. This is very hacky and only works because the normal
506 * notifier that does invalidation is always called after the range
509 if (mmu_notifier_range_blockable(range))
510 mutex_lock(&sn->svmm->mutex);
511 else if (!mutex_trylock(&sn->svmm->mutex))
513 mmu_interval_set_seq(mni, cur_seq);
514 mutex_unlock(&sn->svmm->mutex);
518 static const struct mmu_interval_notifier_ops nouveau_svm_mni_ops = {
519 .invalidate = nouveau_svm_range_invalidate,
522 static int nouveau_range_fault(struct nouveau_svmm *svmm,
523 struct nouveau_drm *drm, void *data, u32 size,
524 u64 *pfns, struct svm_notifier *notifier)
526 unsigned long timeout =
527 jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
528 /* Have HMM fault pages within the fault window to the GPU. */
529 struct hmm_range range = {
530 .notifier = ¬ifier->notifier,
531 .start = notifier->notifier.interval_tree.start,
532 .end = notifier->notifier.interval_tree.last + 1,
534 .flags = nouveau_svm_pfn_flags,
535 .values = nouveau_svm_pfn_values,
536 .pfn_shift = NVIF_VMM_PFNMAP_V0_ADDR_SHIFT,
538 struct mm_struct *mm = notifier->notifier.mm;
542 if (time_after(jiffies, timeout))
545 range.notifier_seq = mmu_interval_read_begin(range.notifier);
546 range.default_flags = 0;
547 range.pfn_flags_mask = -1UL;
548 down_read(&mm->mmap_sem);
549 ret = hmm_range_fault(&range);
550 up_read(&mm->mmap_sem);
552 if (ret == 0 || ret == -EBUSY)
557 mutex_lock(&svmm->mutex);
558 if (mmu_interval_read_retry(range.notifier,
559 range.notifier_seq)) {
560 mutex_unlock(&svmm->mutex);
566 nouveau_dmem_convert_pfn(drm, &range);
568 svmm->vmm->vmm.object.client->super = true;
569 ret = nvif_object_ioctl(&svmm->vmm->vmm.object, data, size, NULL);
570 svmm->vmm->vmm.object.client->super = false;
571 mutex_unlock(&svmm->mutex);
577 nouveau_svm_fault(struct nvif_notify *notify)
579 struct nouveau_svm_fault_buffer *buffer =
580 container_of(notify, typeof(*buffer), notify);
581 struct nouveau_svm *svm =
582 container_of(buffer, typeof(*svm), buffer[buffer->id]);
583 struct nvif_object *device = &svm->drm->client.device.object;
584 struct nouveau_svmm *svmm;
587 struct nvif_ioctl_v0 i;
588 struct nvif_ioctl_mthd_v0 m;
589 struct nvif_vmm_pfnmap_v0 p;
593 struct vm_area_struct *vma;
594 u64 inst, start, limit;
595 int fi, fn, pi, fill;
598 /* Parse available fault buffer entries into a cache, and update
599 * the GET pointer so HW can reuse the entries.
601 SVM_DBG(svm, "fault handler");
602 if (buffer->get == buffer->put) {
603 buffer->put = nvif_rd32(device, buffer->putaddr);
604 buffer->get = nvif_rd32(device, buffer->getaddr);
605 if (buffer->get == buffer->put)
606 return NVIF_NOTIFY_KEEP;
608 buffer->fault_nr = 0;
610 SVM_DBG(svm, "get %08x put %08x", buffer->get, buffer->put);
611 while (buffer->get != buffer->put) {
612 nouveau_svm_fault_cache(svm, buffer, buffer->get * 0x20);
613 if (++buffer->get == buffer->entries)
616 nvif_wr32(device, buffer->getaddr, buffer->get);
617 SVM_DBG(svm, "%d fault(s) pending", buffer->fault_nr);
619 /* Sort parsed faults by instance pointer to prevent unnecessary
620 * instance to SVMM translations, followed by address and access
621 * type to reduce the amount of work when handling the faults.
623 sort(buffer->fault, buffer->fault_nr, sizeof(*buffer->fault),
624 nouveau_svm_fault_cmp, NULL);
626 /* Lookup SVMM structure for each unique instance pointer. */
627 mutex_lock(&svm->mutex);
628 for (fi = 0, svmm = NULL; fi < buffer->fault_nr; fi++) {
629 if (!svmm || buffer->fault[fi]->inst != inst) {
630 struct nouveau_ivmm *ivmm =
631 nouveau_ivmm_find(svm, buffer->fault[fi]->inst);
632 svmm = ivmm ? ivmm->svmm : NULL;
633 inst = buffer->fault[fi]->inst;
634 SVM_DBG(svm, "inst %016llx -> svm-%p", inst, svmm);
636 buffer->fault[fi]->svmm = svmm;
638 mutex_unlock(&svm->mutex);
640 /* Process list of faults. */
641 args.i.i.version = 0;
642 args.i.i.type = NVIF_IOCTL_V0_MTHD;
643 args.i.m.version = 0;
644 args.i.m.method = NVIF_VMM_V0_PFNMAP;
645 args.i.p.version = 0;
647 for (fi = 0; fn = fi + 1, fi < buffer->fault_nr; fi = fn) {
648 struct svm_notifier notifier;
649 struct mm_struct *mm;
651 /* Cancel any faults from non-SVM channels. */
652 if (!(svmm = buffer->fault[fi]->svmm)) {
653 nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
656 SVMM_DBG(svmm, "addr %016llx", buffer->fault[fi]->addr);
658 /* We try and group handling of faults within a small
659 * window into a single update.
661 start = buffer->fault[fi]->addr;
662 limit = start + (ARRAY_SIZE(args.phys) << PAGE_SHIFT);
663 if (start < svmm->unmanaged.limit)
664 limit = min_t(u64, limit, svmm->unmanaged.start);
665 SVMM_DBG(svmm, "wndw %016llx-%016llx", start, limit);
667 mm = svmm->notifier.mm;
668 if (!mmget_not_zero(mm)) {
669 nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
673 /* Intersect fault window with the CPU VMA, cancelling
674 * the fault if the address is invalid.
676 down_read(&mm->mmap_sem);
677 vma = find_vma_intersection(mm, start, limit);
679 SVMM_ERR(svmm, "wndw %016llx-%016llx", start, limit);
680 up_read(&mm->mmap_sem);
682 nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
685 start = max_t(u64, start, vma->vm_start);
686 limit = min_t(u64, limit, vma->vm_end);
687 up_read(&mm->mmap_sem);
688 SVMM_DBG(svmm, "wndw %016llx-%016llx", start, limit);
690 if (buffer->fault[fi]->addr != start) {
691 SVMM_ERR(svmm, "addr %016llx", buffer->fault[fi]->addr);
693 nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
697 /* Prepare the GPU-side update of all pages within the
698 * fault window, determining required pages and access
699 * permissions based on pending faults.
701 args.i.p.page = PAGE_SHIFT;
702 args.i.p.addr = start;
703 for (fn = fi, pi = 0;;) {
704 /* Determine required permissions based on GPU fault
708 if (buffer->fault[fn]->access != 0 /* READ. */ &&
709 buffer->fault[fn]->access != 3 /* PREFETCH. */) {
710 args.phys[pi++] = NVIF_VMM_PFNMAP_V0_V |
711 NVIF_VMM_PFNMAP_V0_W;
713 args.phys[pi++] = NVIF_VMM_PFNMAP_V0_V;
715 args.i.p.size = pi << PAGE_SHIFT;
717 /* It's okay to skip over duplicate addresses from the
718 * same SVMM as faults are ordered by access type such
719 * that only the first one needs to be handled.
721 * ie. WRITE faults appear first, thus any handling of
722 * pending READ faults will already be satisfied.
724 while (++fn < buffer->fault_nr &&
725 buffer->fault[fn]->svmm == svmm &&
726 buffer->fault[fn ]->addr ==
727 buffer->fault[fn - 1]->addr);
729 /* If the next fault is outside the window, or all GPU
730 * faults have been dealt with, we're done here.
732 if (fn >= buffer->fault_nr ||
733 buffer->fault[fn]->svmm != svmm ||
734 buffer->fault[fn]->addr >= limit)
737 /* Fill in the gap between this fault and the next. */
738 fill = (buffer->fault[fn ]->addr -
739 buffer->fault[fn - 1]->addr) >> PAGE_SHIFT;
741 args.phys[pi++] = NVIF_VMM_PFNMAP_V0_NONE;
744 SVMM_DBG(svmm, "wndw %016llx-%016llx covering %d fault(s)",
746 args.i.p.addr + args.i.p.size, fn - fi);
748 notifier.svmm = svmm;
749 ret = mmu_interval_notifier_insert(¬ifier.notifier,
751 args.i.p.addr, args.i.p.size,
752 &nouveau_svm_mni_ops);
754 ret = nouveau_range_fault(
755 svmm, svm->drm, &args,
756 sizeof(args.i) + pi * sizeof(args.phys[0]),
757 args.phys, ¬ifier);
758 mmu_interval_notifier_remove(¬ifier.notifier);
762 /* Cancel any faults in the window whose pages didn't manage
763 * to keep their valid bit, or stay writeable when required.
765 * If handling failed completely, cancel all faults.
768 struct nouveau_svm_fault *fault = buffer->fault[fi++];
769 pi = (fault->addr - args.i.p.addr) >> PAGE_SHIFT;
771 !(args.phys[pi] & NVIF_VMM_PFNMAP_V0_V) ||
772 (!(args.phys[pi] & NVIF_VMM_PFNMAP_V0_W) &&
773 fault->access != 0 && fault->access != 3)) {
774 nouveau_svm_fault_cancel_fault(svm, fault);
781 /* Issue fault replay to the GPU. */
783 nouveau_svm_fault_replay(svm);
784 return NVIF_NOTIFY_KEEP;
788 nouveau_svm_fault_buffer_fini(struct nouveau_svm *svm, int id)
790 struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
791 nvif_notify_put(&buffer->notify);
795 nouveau_svm_fault_buffer_init(struct nouveau_svm *svm, int id)
797 struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
798 struct nvif_object *device = &svm->drm->client.device.object;
799 buffer->get = nvif_rd32(device, buffer->getaddr);
800 buffer->put = nvif_rd32(device, buffer->putaddr);
801 SVM_DBG(svm, "get %08x put %08x (init)", buffer->get, buffer->put);
802 return nvif_notify_get(&buffer->notify);
806 nouveau_svm_fault_buffer_dtor(struct nouveau_svm *svm, int id)
808 struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
812 for (i = 0; buffer->fault[i] && i < buffer->entries; i++)
813 kfree(buffer->fault[i]);
814 kvfree(buffer->fault);
817 nouveau_svm_fault_buffer_fini(svm, id);
819 nvif_notify_fini(&buffer->notify);
820 nvif_object_fini(&buffer->object);
824 nouveau_svm_fault_buffer_ctor(struct nouveau_svm *svm, s32 oclass, int id)
826 struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
827 struct nouveau_drm *drm = svm->drm;
828 struct nvif_object *device = &drm->client.device.object;
829 struct nvif_clb069_v0 args = {};
834 ret = nvif_object_init(device, 0, oclass, &args, sizeof(args),
837 SVM_ERR(svm, "Fault buffer allocation failed: %d", ret);
841 nvif_object_map(&buffer->object, NULL, 0);
842 buffer->entries = args.entries;
843 buffer->getaddr = args.get;
844 buffer->putaddr = args.put;
846 ret = nvif_notify_init(&buffer->object, nouveau_svm_fault, true,
847 NVB069_V0_NTFY_FAULT, NULL, 0, 0,
852 buffer->fault = kvzalloc(sizeof(*buffer->fault) * buffer->entries, GFP_KERNEL);
856 return nouveau_svm_fault_buffer_init(svm, id);
860 nouveau_svm_resume(struct nouveau_drm *drm)
862 struct nouveau_svm *svm = drm->svm;
864 nouveau_svm_fault_buffer_init(svm, 0);
868 nouveau_svm_suspend(struct nouveau_drm *drm)
870 struct nouveau_svm *svm = drm->svm;
872 nouveau_svm_fault_buffer_fini(svm, 0);
876 nouveau_svm_fini(struct nouveau_drm *drm)
878 struct nouveau_svm *svm = drm->svm;
880 nouveau_svm_fault_buffer_dtor(svm, 0);
887 nouveau_svm_init(struct nouveau_drm *drm)
889 static const struct nvif_mclass buffers[] = {
890 { VOLTA_FAULT_BUFFER_A, 0 },
891 { MAXWELL_FAULT_BUFFER_A, 0 },
894 struct nouveau_svm *svm;
897 /* Disable on Volta and newer until channel recovery is fixed,
898 * otherwise clients will have a trivial way to trash the GPU
901 if (drm->client.device.info.family > NV_DEVICE_INFO_V0_PASCAL)
904 if (!(drm->svm = svm = kzalloc(sizeof(*drm->svm), GFP_KERNEL)))
908 mutex_init(&drm->svm->mutex);
909 INIT_LIST_HEAD(&drm->svm->inst);
911 ret = nvif_mclass(&drm->client.device.object, buffers);
913 SVM_DBG(svm, "No supported fault buffer class");
914 nouveau_svm_fini(drm);
918 ret = nouveau_svm_fault_buffer_ctor(svm, buffers[ret].oclass, 0);
920 nouveau_svm_fini(drm);
924 SVM_DBG(svm, "Initialised");