Home | History | Annotate | Line # | Download | only in nouveau
      1  1.3  riastrad /*	$NetBSD: nouveau_svm.c,v 1.3 2021/12/19 11:34:44 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2018 Red Hat Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  */
     24  1.1  riastrad #include <sys/cdefs.h>
     25  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_svm.c,v 1.3 2021/12/19 11:34:44 riastradh Exp $");
     26  1.1  riastrad 
     27  1.1  riastrad #include "nouveau_svm.h"
     28  1.1  riastrad #include "nouveau_drv.h"
     29  1.1  riastrad #include "nouveau_chan.h"
     30  1.1  riastrad #include "nouveau_dmem.h"
     31  1.1  riastrad 
     32  1.1  riastrad #include <nvif/notify.h>
     33  1.1  riastrad #include <nvif/object.h>
     34  1.1  riastrad #include <nvif/vmm.h>
     35  1.1  riastrad 
     36  1.1  riastrad #include <nvif/class.h>
     37  1.1  riastrad #include <nvif/clb069.h>
     38  1.1  riastrad #include <nvif/ifc00d.h>
     39  1.1  riastrad 
     40  1.1  riastrad #include <linux/sched/mm.h>
     41  1.1  riastrad #include <linux/sort.h>
     42  1.1  riastrad #include <linux/hmm.h>
     43  1.1  riastrad 
     44  1.1  riastrad struct nouveau_svm {
     45  1.1  riastrad 	struct nouveau_drm *drm;
     46  1.1  riastrad 	struct mutex mutex;
     47  1.1  riastrad 	struct list_head inst;
     48  1.1  riastrad 
     49  1.1  riastrad 	struct nouveau_svm_fault_buffer {
     50  1.1  riastrad 		int id;
     51  1.1  riastrad 		struct nvif_object object;
     52  1.1  riastrad 		u32 entries;
     53  1.1  riastrad 		u32 getaddr;
     54  1.1  riastrad 		u32 putaddr;
     55  1.1  riastrad 		u32 get;
     56  1.1  riastrad 		u32 put;
     57  1.1  riastrad 		struct nvif_notify notify;
     58  1.1  riastrad 
     59  1.1  riastrad 		struct nouveau_svm_fault {
     60  1.1  riastrad 			u64 inst;
     61  1.1  riastrad 			u64 addr;
     62  1.1  riastrad 			u64 time;
     63  1.1  riastrad 			u32 engine;
     64  1.1  riastrad 			u8  gpc;
     65  1.1  riastrad 			u8  hub;
     66  1.1  riastrad 			u8  access;
     67  1.1  riastrad 			u8  client;
     68  1.1  riastrad 			u8  fault;
     69  1.1  riastrad 			struct nouveau_svmm *svmm;
     70  1.1  riastrad 		} **fault;
     71  1.1  riastrad 		int fault_nr;
     72  1.1  riastrad 	} buffer[1];
     73  1.1  riastrad };
     74  1.1  riastrad 
     75  1.1  riastrad #define SVM_DBG(s,f,a...) NV_DEBUG((s)->drm, "svm: "f"\n", ##a)
     76  1.1  riastrad #define SVM_ERR(s,f,a...) NV_WARN((s)->drm, "svm: "f"\n", ##a)
     77  1.1  riastrad 
     78  1.1  riastrad struct nouveau_ivmm {
     79  1.1  riastrad 	struct nouveau_svmm *svmm;
     80  1.1  riastrad 	u64 inst;
     81  1.1  riastrad 	struct list_head head;
     82  1.1  riastrad };
     83  1.1  riastrad 
     84  1.1  riastrad static struct nouveau_ivmm *
     85  1.1  riastrad nouveau_ivmm_find(struct nouveau_svm *svm, u64 inst)
     86  1.1  riastrad {
     87  1.1  riastrad 	struct nouveau_ivmm *ivmm;
     88  1.1  riastrad 	list_for_each_entry(ivmm, &svm->inst, head) {
     89  1.1  riastrad 		if (ivmm->inst == inst)
     90  1.1  riastrad 			return ivmm;
     91  1.1  riastrad 	}
     92  1.1  riastrad 	return NULL;
     93  1.1  riastrad }
     94  1.1  riastrad 
     95  1.1  riastrad struct nouveau_svmm {
     96  1.1  riastrad 	struct mmu_notifier notifier;
     97  1.1  riastrad 	struct nouveau_vmm *vmm;
     98  1.1  riastrad 	struct {
     99  1.1  riastrad 		unsigned long start;
    100  1.1  riastrad 		unsigned long limit;
    101  1.1  riastrad 	} unmanaged;
    102  1.1  riastrad 
    103  1.1  riastrad 	struct mutex mutex;
    104  1.1  riastrad };
    105  1.1  riastrad 
    106  1.1  riastrad #define SVMM_DBG(s,f,a...)                                                     \
    107  1.1  riastrad 	NV_DEBUG((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
    108  1.1  riastrad #define SVMM_ERR(s,f,a...)                                                     \
    109  1.1  riastrad 	NV_WARN((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
    110  1.1  riastrad 
    111  1.1  riastrad int
    112  1.1  riastrad nouveau_svmm_bind(struct drm_device *dev, void *data,
    113  1.1  riastrad 		  struct drm_file *file_priv)
    114  1.1  riastrad {
    115  1.1  riastrad 	struct nouveau_cli *cli = nouveau_cli(file_priv);
    116  1.1  riastrad 	struct drm_nouveau_svm_bind *args = data;
    117  1.1  riastrad 	unsigned target, cmd, priority;
    118  1.1  riastrad 	unsigned long addr, end, size;
    119  1.1  riastrad 	struct mm_struct *mm;
    120  1.1  riastrad 
    121  1.1  riastrad 	args->va_start &= PAGE_MASK;
    122  1.1  riastrad 	args->va_end &= PAGE_MASK;
    123  1.1  riastrad 
    124  1.1  riastrad 	/* Sanity check arguments */
    125  1.1  riastrad 	if (args->reserved0 || args->reserved1)
    126  1.1  riastrad 		return -EINVAL;
    127  1.1  riastrad 	if (args->header & (~NOUVEAU_SVM_BIND_VALID_MASK))
    128  1.1  riastrad 		return -EINVAL;
    129  1.1  riastrad 	if (args->va_start >= args->va_end)
    130  1.1  riastrad 		return -EINVAL;
    131  1.1  riastrad 	if (!args->npages)
    132  1.1  riastrad 		return -EINVAL;
    133  1.1  riastrad 
    134  1.1  riastrad 	cmd = args->header >> NOUVEAU_SVM_BIND_COMMAND_SHIFT;
    135  1.1  riastrad 	cmd &= NOUVEAU_SVM_BIND_COMMAND_MASK;
    136  1.1  riastrad 	switch (cmd) {
    137  1.1  riastrad 	case NOUVEAU_SVM_BIND_COMMAND__MIGRATE:
    138  1.1  riastrad 		break;
    139  1.1  riastrad 	default:
    140  1.1  riastrad 		return -EINVAL;
    141  1.1  riastrad 	}
    142  1.1  riastrad 
    143  1.1  riastrad 	priority = args->header >> NOUVEAU_SVM_BIND_PRIORITY_SHIFT;
    144  1.1  riastrad 	priority &= NOUVEAU_SVM_BIND_PRIORITY_MASK;
    145  1.1  riastrad 
    146  1.1  riastrad 	/* FIXME support CPU target ie all target value < GPU_VRAM */
    147  1.1  riastrad 	target = args->header >> NOUVEAU_SVM_BIND_TARGET_SHIFT;
    148  1.1  riastrad 	target &= NOUVEAU_SVM_BIND_TARGET_MASK;
    149  1.1  riastrad 	switch (target) {
    150  1.1  riastrad 	case NOUVEAU_SVM_BIND_TARGET__GPU_VRAM:
    151  1.1  riastrad 		break;
    152  1.1  riastrad 	default:
    153  1.1  riastrad 		return -EINVAL;
    154  1.1  riastrad 	}
    155  1.1  riastrad 
    156  1.1  riastrad 	/*
    157  1.1  riastrad 	 * FIXME: For now refuse non 0 stride, we need to change the migrate
    158  1.1  riastrad 	 * kernel function to handle stride to avoid to create a mess within
    159  1.1  riastrad 	 * each device driver.
    160  1.1  riastrad 	 */
    161  1.1  riastrad 	if (args->stride)
    162  1.1  riastrad 		return -EINVAL;
    163  1.1  riastrad 
    164  1.1  riastrad 	size = ((unsigned long)args->npages) << PAGE_SHIFT;
    165  1.1  riastrad 	if ((args->va_start + size) <= args->va_start)
    166  1.1  riastrad 		return -EINVAL;
    167  1.1  riastrad 	if ((args->va_start + size) > args->va_end)
    168  1.1  riastrad 		return -EINVAL;
    169  1.1  riastrad 
    170  1.1  riastrad 	/*
    171  1.1  riastrad 	 * Ok we are ask to do something sane, for now we only support migrate
    172  1.1  riastrad 	 * commands but we will add things like memory policy (what to do on
    173  1.1  riastrad 	 * page fault) and maybe some other commands.
    174  1.1  riastrad 	 */
    175  1.1  riastrad 
    176  1.1  riastrad 	mm = get_task_mm(current);
    177  1.1  riastrad 	down_read(&mm->mmap_sem);
    178  1.1  riastrad 
    179  1.1  riastrad 	for (addr = args->va_start, end = args->va_start + size; addr < end;) {
    180  1.1  riastrad 		struct vm_area_struct *vma;
    181  1.1  riastrad 		unsigned long next;
    182  1.1  riastrad 
    183  1.1  riastrad 		vma = find_vma_intersection(mm, addr, end);
    184  1.1  riastrad 		if (!vma)
    185  1.1  riastrad 			break;
    186  1.1  riastrad 
    187  1.1  riastrad 		next = min(vma->vm_end, end);
    188  1.1  riastrad 		/* This is a best effort so we ignore errors */
    189  1.1  riastrad 		nouveau_dmem_migrate_vma(cli->drm, vma, addr, next);
    190  1.1  riastrad 		addr = next;
    191  1.1  riastrad 	}
    192  1.1  riastrad 
    193  1.1  riastrad 	/*
    194  1.1  riastrad 	 * FIXME Return the number of page we have migrated, again we need to
    195  1.1  riastrad 	 * update the migrate API to return that information so that we can
    196  1.1  riastrad 	 * report it to user space.
    197  1.1  riastrad 	 */
    198  1.1  riastrad 	args->result = 0;
    199  1.1  riastrad 
    200  1.1  riastrad 	up_read(&mm->mmap_sem);
    201  1.1  riastrad 	mmput(mm);
    202  1.1  riastrad 
    203  1.1  riastrad 	return 0;
    204  1.1  riastrad }
    205  1.1  riastrad 
    206  1.1  riastrad /* Unlink channel instance from SVMM. */
    207  1.1  riastrad void
    208  1.1  riastrad nouveau_svmm_part(struct nouveau_svmm *svmm, u64 inst)
    209  1.1  riastrad {
    210  1.1  riastrad 	struct nouveau_ivmm *ivmm;
    211  1.1  riastrad 	if (svmm) {
    212  1.1  riastrad 		mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
    213  1.1  riastrad 		ivmm = nouveau_ivmm_find(svmm->vmm->cli->drm->svm, inst);
    214  1.1  riastrad 		if (ivmm) {
    215  1.1  riastrad 			list_del(&ivmm->head);
    216  1.1  riastrad 			kfree(ivmm);
    217  1.1  riastrad 		}
    218  1.1  riastrad 		mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
    219  1.1  riastrad 	}
    220  1.1  riastrad }
    221  1.1  riastrad 
    222  1.1  riastrad /* Link channel instance to SVMM. */
    223  1.1  riastrad int
    224  1.1  riastrad nouveau_svmm_join(struct nouveau_svmm *svmm, u64 inst)
    225  1.1  riastrad {
    226  1.1  riastrad 	struct nouveau_ivmm *ivmm;
    227  1.1  riastrad 	if (svmm) {
    228  1.1  riastrad 		if (!(ivmm = kmalloc(sizeof(*ivmm), GFP_KERNEL)))
    229  1.1  riastrad 			return -ENOMEM;
    230  1.1  riastrad 		ivmm->svmm = svmm;
    231  1.1  riastrad 		ivmm->inst = inst;
    232  1.1  riastrad 
    233  1.1  riastrad 		mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
    234  1.1  riastrad 		list_add(&ivmm->head, &svmm->vmm->cli->drm->svm->inst);
    235  1.1  riastrad 		mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
    236  1.1  riastrad 	}
    237  1.1  riastrad 	return 0;
    238  1.1  riastrad }
    239  1.1  riastrad 
    240  1.1  riastrad /* Invalidate SVMM address-range on GPU. */
    241  1.1  riastrad static void
    242  1.1  riastrad nouveau_svmm_invalidate(struct nouveau_svmm *svmm, u64 start, u64 limit)
    243  1.1  riastrad {
    244  1.1  riastrad 	if (limit > start) {
    245  1.1  riastrad 		bool super = svmm->vmm->vmm.object.client->super;
    246  1.1  riastrad 		svmm->vmm->vmm.object.client->super = true;
    247  1.1  riastrad 		nvif_object_mthd(&svmm->vmm->vmm.object, NVIF_VMM_V0_PFNCLR,
    248  1.1  riastrad 				 &(struct nvif_vmm_pfnclr_v0) {
    249  1.1  riastrad 					.addr = start,
    250  1.1  riastrad 					.size = limit - start,
    251  1.1  riastrad 				 }, sizeof(struct nvif_vmm_pfnclr_v0));
    252  1.1  riastrad 		svmm->vmm->vmm.object.client->super = super;
    253  1.1  riastrad 	}
    254  1.1  riastrad }
    255  1.1  riastrad 
    256  1.1  riastrad static int
    257  1.1  riastrad nouveau_svmm_invalidate_range_start(struct mmu_notifier *mn,
    258  1.1  riastrad 				    const struct mmu_notifier_range *update)
    259  1.1  riastrad {
    260  1.1  riastrad 	struct nouveau_svmm *svmm =
    261  1.1  riastrad 		container_of(mn, struct nouveau_svmm, notifier);
    262  1.1  riastrad 	unsigned long start = update->start;
    263  1.1  riastrad 	unsigned long limit = update->end;
    264  1.1  riastrad 
    265  1.1  riastrad 	if (!mmu_notifier_range_blockable(update))
    266  1.1  riastrad 		return -EAGAIN;
    267  1.1  riastrad 
    268  1.1  riastrad 	SVMM_DBG(svmm, "invalidate %016lx-%016lx", start, limit);
    269  1.1  riastrad 
    270  1.1  riastrad 	mutex_lock(&svmm->mutex);
    271  1.1  riastrad 	if (unlikely(!svmm->vmm))
    272  1.1  riastrad 		goto out;
    273  1.1  riastrad 
    274  1.1  riastrad 	if (limit > svmm->unmanaged.start && start < svmm->unmanaged.limit) {
    275  1.1  riastrad 		if (start < svmm->unmanaged.start) {
    276  1.1  riastrad 			nouveau_svmm_invalidate(svmm, start,
    277  1.1  riastrad 						svmm->unmanaged.limit);
    278  1.1  riastrad 		}
    279  1.1  riastrad 		start = svmm->unmanaged.limit;
    280  1.1  riastrad 	}
    281  1.1  riastrad 
    282  1.1  riastrad 	nouveau_svmm_invalidate(svmm, start, limit);
    283  1.1  riastrad 
    284  1.1  riastrad out:
    285  1.1  riastrad 	mutex_unlock(&svmm->mutex);
    286  1.1  riastrad 	return 0;
    287  1.1  riastrad }
    288  1.1  riastrad 
    289  1.1  riastrad static void nouveau_svmm_free_notifier(struct mmu_notifier *mn)
    290  1.1  riastrad {
    291  1.1  riastrad 	kfree(container_of(mn, struct nouveau_svmm, notifier));
    292  1.1  riastrad }
    293  1.1  riastrad 
    294  1.1  riastrad static const struct mmu_notifier_ops nouveau_mn_ops = {
    295  1.1  riastrad 	.invalidate_range_start = nouveau_svmm_invalidate_range_start,
    296  1.1  riastrad 	.free_notifier = nouveau_svmm_free_notifier,
    297  1.1  riastrad };
    298  1.1  riastrad 
    299  1.1  riastrad void
    300  1.1  riastrad nouveau_svmm_fini(struct nouveau_svmm **psvmm)
    301  1.1  riastrad {
    302  1.1  riastrad 	struct nouveau_svmm *svmm = *psvmm;
    303  1.1  riastrad 	if (svmm) {
    304  1.1  riastrad 		mutex_lock(&svmm->mutex);
    305  1.1  riastrad 		svmm->vmm = NULL;
    306  1.1  riastrad 		mutex_unlock(&svmm->mutex);
    307  1.1  riastrad 		mmu_notifier_put(&svmm->notifier);
    308  1.3  riastrad 		mutex_destroy(&svmm->mutex);
    309  1.1  riastrad 		*psvmm = NULL;
    310  1.1  riastrad 	}
    311  1.1  riastrad }
    312  1.1  riastrad 
    313  1.1  riastrad int
    314  1.1  riastrad nouveau_svmm_init(struct drm_device *dev, void *data,
    315  1.1  riastrad 		  struct drm_file *file_priv)
    316  1.1  riastrad {
    317  1.1  riastrad 	struct nouveau_cli *cli = nouveau_cli(file_priv);
    318  1.1  riastrad 	struct nouveau_svmm *svmm;
    319  1.1  riastrad 	struct drm_nouveau_svm_init *args = data;
    320  1.1  riastrad 	int ret;
    321  1.1  riastrad 
    322  1.1  riastrad 	/* Allocate tracking for SVM-enabled VMM. */
    323  1.1  riastrad 	if (!(svmm = kzalloc(sizeof(*svmm), GFP_KERNEL)))
    324  1.1  riastrad 		return -ENOMEM;
    325  1.1  riastrad 	svmm->vmm = &cli->svm;
    326  1.1  riastrad 	svmm->unmanaged.start = args->unmanaged_addr;
    327  1.1  riastrad 	svmm->unmanaged.limit = args->unmanaged_addr + args->unmanaged_size;
    328  1.1  riastrad 	mutex_init(&svmm->mutex);
    329  1.1  riastrad 
    330  1.1  riastrad 	/* Check that SVM isn't already enabled for the client. */
    331  1.1  riastrad 	mutex_lock(&cli->mutex);
    332  1.1  riastrad 	if (cli->svm.cli) {
    333  1.1  riastrad 		ret = -EBUSY;
    334  1.1  riastrad 		goto out_free;
    335  1.1  riastrad 	}
    336  1.1  riastrad 
    337  1.1  riastrad 	/* Allocate a new GPU VMM that can support SVM (managed by the
    338  1.1  riastrad 	 * client, with replayable faults enabled).
    339  1.1  riastrad 	 *
    340  1.1  riastrad 	 * All future channel/memory allocations will make use of this
    341  1.1  riastrad 	 * VMM instead of the standard one.
    342  1.1  riastrad 	 */
    343  1.1  riastrad 	ret = nvif_vmm_init(&cli->mmu, cli->vmm.vmm.object.oclass, true,
    344  1.1  riastrad 			    args->unmanaged_addr, args->unmanaged_size,
    345  1.1  riastrad 			    &(struct gp100_vmm_v0) {
    346  1.1  riastrad 				.fault_replay = true,
    347  1.1  riastrad 			    }, sizeof(struct gp100_vmm_v0), &cli->svm.vmm);
    348  1.1  riastrad 	if (ret)
    349  1.1  riastrad 		goto out_free;
    350  1.1  riastrad 
    351  1.1  riastrad 	down_write(&current->mm->mmap_sem);
    352  1.1  riastrad 	svmm->notifier.ops = &nouveau_mn_ops;
    353  1.1  riastrad 	ret = __mmu_notifier_register(&svmm->notifier, current->mm);
    354  1.1  riastrad 	if (ret)
    355  1.1  riastrad 		goto out_mm_unlock;
    356  1.1  riastrad 	/* Note, ownership of svmm transfers to mmu_notifier */
    357  1.1  riastrad 
    358  1.1  riastrad 	cli->svm.svmm = svmm;
    359  1.1  riastrad 	cli->svm.cli = cli;
    360  1.1  riastrad 	up_write(&current->mm->mmap_sem);
    361  1.1  riastrad 	mutex_unlock(&cli->mutex);
    362  1.1  riastrad 	return 0;
    363  1.1  riastrad 
    364  1.1  riastrad out_mm_unlock:
    365  1.1  riastrad 	up_write(&current->mm->mmap_sem);
    366  1.1  riastrad out_free:
    367  1.1  riastrad 	mutex_unlock(&cli->mutex);
    368  1.3  riastrad 	mutex_destroy(&svmm->mutex);
    369  1.1  riastrad 	kfree(svmm);
    370  1.1  riastrad 	return ret;
    371  1.1  riastrad }
    372  1.1  riastrad 
    373  1.1  riastrad static const u64
    374  1.1  riastrad nouveau_svm_pfn_flags[HMM_PFN_FLAG_MAX] = {
    375  1.1  riastrad 	[HMM_PFN_VALID         ] = NVIF_VMM_PFNMAP_V0_V,
    376  1.1  riastrad 	[HMM_PFN_WRITE         ] = NVIF_VMM_PFNMAP_V0_W,
    377  1.1  riastrad 	[HMM_PFN_DEVICE_PRIVATE] = NVIF_VMM_PFNMAP_V0_VRAM,
    378  1.1  riastrad };
    379  1.1  riastrad 
    380  1.1  riastrad static const u64
    381  1.1  riastrad nouveau_svm_pfn_values[HMM_PFN_VALUE_MAX] = {
    382  1.1  riastrad 	[HMM_PFN_ERROR  ] = ~NVIF_VMM_PFNMAP_V0_V,
    383  1.1  riastrad 	[HMM_PFN_NONE   ] =  NVIF_VMM_PFNMAP_V0_NONE,
    384  1.1  riastrad 	[HMM_PFN_SPECIAL] = ~NVIF_VMM_PFNMAP_V0_V,
    385  1.1  riastrad };
    386  1.1  riastrad 
    387  1.1  riastrad /* Issue fault replay for GPU to retry accesses that faulted previously. */
    388  1.1  riastrad static void
    389  1.1  riastrad nouveau_svm_fault_replay(struct nouveau_svm *svm)
    390  1.1  riastrad {
    391  1.1  riastrad 	SVM_DBG(svm, "replay");
    392  1.1  riastrad 	WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
    393  1.1  riastrad 				 GP100_VMM_VN_FAULT_REPLAY,
    394  1.1  riastrad 				 &(struct gp100_vmm_fault_replay_vn) {},
    395  1.1  riastrad 				 sizeof(struct gp100_vmm_fault_replay_vn)));
    396  1.1  riastrad }
    397  1.1  riastrad 
    398  1.1  riastrad /* Cancel a replayable fault that could not be handled.
    399  1.1  riastrad  *
    400  1.1  riastrad  * Cancelling the fault will trigger recovery to reset the engine
    401  1.1  riastrad  * and kill the offending channel (ie. GPU SIGSEGV).
    402  1.1  riastrad  */
    403  1.1  riastrad static void
    404  1.1  riastrad nouveau_svm_fault_cancel(struct nouveau_svm *svm,
    405  1.1  riastrad 			 u64 inst, u8 hub, u8 gpc, u8 client)
    406  1.1  riastrad {
    407  1.1  riastrad 	SVM_DBG(svm, "cancel %016llx %d %02x %02x", inst, hub, gpc, client);
    408  1.1  riastrad 	WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
    409  1.1  riastrad 				 GP100_VMM_VN_FAULT_CANCEL,
    410  1.1  riastrad 				 &(struct gp100_vmm_fault_cancel_v0) {
    411  1.1  riastrad 					.hub = hub,
    412  1.1  riastrad 					.gpc = gpc,
    413  1.1  riastrad 					.client = client,
    414  1.1  riastrad 					.inst = inst,
    415  1.1  riastrad 				 }, sizeof(struct gp100_vmm_fault_cancel_v0)));
    416  1.1  riastrad }
    417  1.1  riastrad 
    418  1.1  riastrad static void
    419  1.1  riastrad nouveau_svm_fault_cancel_fault(struct nouveau_svm *svm,
    420  1.1  riastrad 			       struct nouveau_svm_fault *fault)
    421  1.1  riastrad {
    422  1.1  riastrad 	nouveau_svm_fault_cancel(svm, fault->inst,
    423  1.1  riastrad 				      fault->hub,
    424  1.1  riastrad 				      fault->gpc,
    425  1.1  riastrad 				      fault->client);
    426  1.1  riastrad }
    427  1.1  riastrad 
    428  1.1  riastrad static int
    429  1.1  riastrad nouveau_svm_fault_cmp(const void *a, const void *b)
    430  1.1  riastrad {
    431  1.1  riastrad 	const struct nouveau_svm_fault *fa = *(struct nouveau_svm_fault **)a;
    432  1.1  riastrad 	const struct nouveau_svm_fault *fb = *(struct nouveau_svm_fault **)b;
    433  1.1  riastrad 	int ret;
    434  1.1  riastrad 	if ((ret = (s64)fa->inst - fb->inst))
    435  1.1  riastrad 		return ret;
    436  1.1  riastrad 	if ((ret = (s64)fa->addr - fb->addr))
    437  1.1  riastrad 		return ret;
    438  1.1  riastrad 	/*XXX: atomic? */
    439  1.1  riastrad 	return (fa->access == 0 || fa->access == 3) -
    440  1.1  riastrad 	       (fb->access == 0 || fb->access == 3);
    441  1.1  riastrad }
    442  1.1  riastrad 
    443  1.1  riastrad static void
    444  1.1  riastrad nouveau_svm_fault_cache(struct nouveau_svm *svm,
    445  1.1  riastrad 			struct nouveau_svm_fault_buffer *buffer, u32 offset)
    446  1.1  riastrad {
    447  1.1  riastrad 	struct nvif_object *memory = &buffer->object;
    448  1.1  riastrad 	const u32 instlo = nvif_rd32(memory, offset + 0x00);
    449  1.1  riastrad 	const u32 insthi = nvif_rd32(memory, offset + 0x04);
    450  1.1  riastrad 	const u32 addrlo = nvif_rd32(memory, offset + 0x08);
    451  1.1  riastrad 	const u32 addrhi = nvif_rd32(memory, offset + 0x0c);
    452  1.1  riastrad 	const u32 timelo = nvif_rd32(memory, offset + 0x10);
    453  1.1  riastrad 	const u32 timehi = nvif_rd32(memory, offset + 0x14);
    454  1.1  riastrad 	const u32 engine = nvif_rd32(memory, offset + 0x18);
    455  1.1  riastrad 	const u32   info = nvif_rd32(memory, offset + 0x1c);
    456  1.1  riastrad 	const u64   inst = (u64)insthi << 32 | instlo;
    457  1.1  riastrad 	const u8     gpc = (info & 0x1f000000) >> 24;
    458  1.1  riastrad 	const u8     hub = (info & 0x00100000) >> 20;
    459  1.1  riastrad 	const u8  client = (info & 0x00007f00) >> 8;
    460  1.1  riastrad 	struct nouveau_svm_fault *fault;
    461  1.1  riastrad 
    462  1.1  riastrad 	//XXX: i think we're supposed to spin waiting */
    463  1.1  riastrad 	if (WARN_ON(!(info & 0x80000000)))
    464  1.1  riastrad 		return;
    465  1.1  riastrad 
    466  1.1  riastrad 	nvif_mask(memory, offset + 0x1c, 0x80000000, 0x00000000);
    467  1.1  riastrad 
    468  1.1  riastrad 	if (!buffer->fault[buffer->fault_nr]) {
    469  1.1  riastrad 		fault = kmalloc(sizeof(*fault), GFP_KERNEL);
    470  1.1  riastrad 		if (WARN_ON(!fault)) {
    471  1.1  riastrad 			nouveau_svm_fault_cancel(svm, inst, hub, gpc, client);
    472  1.1  riastrad 			return;
    473  1.1  riastrad 		}
    474  1.1  riastrad 		buffer->fault[buffer->fault_nr] = fault;
    475  1.1  riastrad 	}
    476  1.1  riastrad 
    477  1.1  riastrad 	fault = buffer->fault[buffer->fault_nr++];
    478  1.1  riastrad 	fault->inst   = inst;
    479  1.1  riastrad 	fault->addr   = (u64)addrhi << 32 | addrlo;
    480  1.1  riastrad 	fault->time   = (u64)timehi << 32 | timelo;
    481  1.1  riastrad 	fault->engine = engine;
    482  1.1  riastrad 	fault->gpc    = gpc;
    483  1.1  riastrad 	fault->hub    = hub;
    484  1.1  riastrad 	fault->access = (info & 0x000f0000) >> 16;
    485  1.1  riastrad 	fault->client = client;
    486  1.1  riastrad 	fault->fault  = (info & 0x0000001f);
    487  1.1  riastrad 
    488  1.1  riastrad 	SVM_DBG(svm, "fault %016llx %016llx %02x",
    489  1.1  riastrad 		fault->inst, fault->addr, fault->access);
    490  1.1  riastrad }
    491  1.1  riastrad 
    492  1.1  riastrad struct svm_notifier {
    493  1.1  riastrad 	struct mmu_interval_notifier notifier;
    494  1.1  riastrad 	struct nouveau_svmm *svmm;
    495  1.1  riastrad };
    496  1.1  riastrad 
    497  1.1  riastrad static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni,
    498  1.1  riastrad 					 const struct mmu_notifier_range *range,
    499  1.1  riastrad 					 unsigned long cur_seq)
    500  1.1  riastrad {
    501  1.1  riastrad 	struct svm_notifier *sn =
    502  1.1  riastrad 		container_of(mni, struct svm_notifier, notifier);
    503  1.1  riastrad 
    504  1.1  riastrad 	/*
    505  1.1  riastrad 	 * serializes the update to mni->invalidate_seq done by caller and
    506  1.1  riastrad 	 * prevents invalidation of the PTE from progressing while HW is being
    507  1.1  riastrad 	 * programmed. This is very hacky and only works because the normal
    508  1.1  riastrad 	 * notifier that does invalidation is always called after the range
    509  1.1  riastrad 	 * notifier.
    510  1.1  riastrad 	 */
    511  1.1  riastrad 	if (mmu_notifier_range_blockable(range))
    512  1.1  riastrad 		mutex_lock(&sn->svmm->mutex);
    513  1.1  riastrad 	else if (!mutex_trylock(&sn->svmm->mutex))
    514  1.1  riastrad 		return false;
    515  1.1  riastrad 	mmu_interval_set_seq(mni, cur_seq);
    516  1.1  riastrad 	mutex_unlock(&sn->svmm->mutex);
    517  1.1  riastrad 	return true;
    518  1.1  riastrad }
    519  1.1  riastrad 
    520  1.1  riastrad static const struct mmu_interval_notifier_ops nouveau_svm_mni_ops = {
    521  1.1  riastrad 	.invalidate = nouveau_svm_range_invalidate,
    522  1.1  riastrad };
    523  1.1  riastrad 
    524  1.1  riastrad static int nouveau_range_fault(struct nouveau_svmm *svmm,
    525  1.1  riastrad 			       struct nouveau_drm *drm, void *data, u32 size,
    526  1.1  riastrad 			       u64 *pfns, struct svm_notifier *notifier)
    527  1.1  riastrad {
    528  1.1  riastrad 	unsigned long timeout =
    529  1.1  riastrad 		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
    530  1.1  riastrad 	/* Have HMM fault pages within the fault window to the GPU. */
    531  1.1  riastrad 	struct hmm_range range = {
    532  1.1  riastrad 		.notifier = &notifier->notifier,
    533  1.1  riastrad 		.start = notifier->notifier.interval_tree.start,
    534  1.1  riastrad 		.end = notifier->notifier.interval_tree.last + 1,
    535  1.1  riastrad 		.pfns = pfns,
    536  1.1  riastrad 		.flags = nouveau_svm_pfn_flags,
    537  1.1  riastrad 		.values = nouveau_svm_pfn_values,
    538  1.1  riastrad 		.pfn_shift = NVIF_VMM_PFNMAP_V0_ADDR_SHIFT,
    539  1.1  riastrad 	};
    540  1.1  riastrad 	struct mm_struct *mm = notifier->notifier.mm;
    541  1.1  riastrad 	long ret;
    542  1.1  riastrad 
    543  1.1  riastrad 	while (true) {
    544  1.1  riastrad 		if (time_after(jiffies, timeout))
    545  1.1  riastrad 			return -EBUSY;
    546  1.1  riastrad 
    547  1.1  riastrad 		range.notifier_seq = mmu_interval_read_begin(range.notifier);
    548  1.1  riastrad 		range.default_flags = 0;
    549  1.1  riastrad 		range.pfn_flags_mask = -1UL;
    550  1.1  riastrad 		down_read(&mm->mmap_sem);
    551  1.1  riastrad 		ret = hmm_range_fault(&range, 0);
    552  1.1  riastrad 		up_read(&mm->mmap_sem);
    553  1.1  riastrad 		if (ret <= 0) {
    554  1.1  riastrad 			if (ret == 0 || ret == -EBUSY)
    555  1.1  riastrad 				continue;
    556  1.1  riastrad 			return ret;
    557  1.1  riastrad 		}
    558  1.1  riastrad 
    559  1.1  riastrad 		mutex_lock(&svmm->mutex);
    560  1.1  riastrad 		if (mmu_interval_read_retry(range.notifier,
    561  1.1  riastrad 					    range.notifier_seq)) {
    562  1.1  riastrad 			mutex_unlock(&svmm->mutex);
    563  1.1  riastrad 			continue;
    564  1.1  riastrad 		}
    565  1.1  riastrad 		break;
    566  1.1  riastrad 	}
    567  1.1  riastrad 
    568  1.1  riastrad 	nouveau_dmem_convert_pfn(drm, &range);
    569  1.1  riastrad 
    570  1.1  riastrad 	svmm->vmm->vmm.object.client->super = true;
    571  1.1  riastrad 	ret = nvif_object_ioctl(&svmm->vmm->vmm.object, data, size, NULL);
    572  1.1  riastrad 	svmm->vmm->vmm.object.client->super = false;
    573  1.1  riastrad 	mutex_unlock(&svmm->mutex);
    574  1.1  riastrad 
    575  1.1  riastrad 	return ret;
    576  1.1  riastrad }
    577  1.1  riastrad 
    578  1.1  riastrad static int
    579  1.1  riastrad nouveau_svm_fault(struct nvif_notify *notify)
    580  1.1  riastrad {
    581  1.1  riastrad 	struct nouveau_svm_fault_buffer *buffer =
    582  1.1  riastrad 		container_of(notify, typeof(*buffer), notify);
    583  1.1  riastrad 	struct nouveau_svm *svm =
    584  1.1  riastrad 		container_of(buffer, typeof(*svm), buffer[buffer->id]);
    585  1.1  riastrad 	struct nvif_object *device = &svm->drm->client.device.object;
    586  1.1  riastrad 	struct nouveau_svmm *svmm;
    587  1.1  riastrad 	struct {
    588  1.1  riastrad 		struct {
    589  1.1  riastrad 			struct nvif_ioctl_v0 i;
    590  1.1  riastrad 			struct nvif_ioctl_mthd_v0 m;
    591  1.1  riastrad 			struct nvif_vmm_pfnmap_v0 p;
    592  1.1  riastrad 		} i;
    593  1.1  riastrad 		u64 phys[16];
    594  1.1  riastrad 	} args;
    595  1.1  riastrad 	struct vm_area_struct *vma;
    596  1.1  riastrad 	u64 inst, start, limit;
    597  1.1  riastrad 	int fi, fn, pi, fill;
    598  1.1  riastrad 	int replay = 0, ret;
    599  1.1  riastrad 
    600  1.1  riastrad 	/* Parse available fault buffer entries into a cache, and update
    601  1.1  riastrad 	 * the GET pointer so HW can reuse the entries.
    602  1.1  riastrad 	 */
    603  1.1  riastrad 	SVM_DBG(svm, "fault handler");
    604  1.1  riastrad 	if (buffer->get == buffer->put) {
    605  1.1  riastrad 		buffer->put = nvif_rd32(device, buffer->putaddr);
    606  1.1  riastrad 		buffer->get = nvif_rd32(device, buffer->getaddr);
    607  1.1  riastrad 		if (buffer->get == buffer->put)
    608  1.1  riastrad 			return NVIF_NOTIFY_KEEP;
    609  1.1  riastrad 	}
    610  1.1  riastrad 	buffer->fault_nr = 0;
    611  1.1  riastrad 
    612  1.1  riastrad 	SVM_DBG(svm, "get %08x put %08x", buffer->get, buffer->put);
    613  1.1  riastrad 	while (buffer->get != buffer->put) {
    614  1.1  riastrad 		nouveau_svm_fault_cache(svm, buffer, buffer->get * 0x20);
    615  1.1  riastrad 		if (++buffer->get == buffer->entries)
    616  1.1  riastrad 			buffer->get = 0;
    617  1.1  riastrad 	}
    618  1.1  riastrad 	nvif_wr32(device, buffer->getaddr, buffer->get);
    619  1.1  riastrad 	SVM_DBG(svm, "%d fault(s) pending", buffer->fault_nr);
    620  1.1  riastrad 
    621  1.1  riastrad 	/* Sort parsed faults by instance pointer to prevent unnecessary
    622  1.1  riastrad 	 * instance to SVMM translations, followed by address and access
    623  1.1  riastrad 	 * type to reduce the amount of work when handling the faults.
    624  1.1  riastrad 	 */
    625  1.1  riastrad 	sort(buffer->fault, buffer->fault_nr, sizeof(*buffer->fault),
    626  1.1  riastrad 	     nouveau_svm_fault_cmp, NULL);
    627  1.1  riastrad 
    628  1.1  riastrad 	/* Lookup SVMM structure for each unique instance pointer. */
    629  1.1  riastrad 	mutex_lock(&svm->mutex);
    630  1.1  riastrad 	for (fi = 0, svmm = NULL; fi < buffer->fault_nr; fi++) {
    631  1.1  riastrad 		if (!svmm || buffer->fault[fi]->inst != inst) {
    632  1.1  riastrad 			struct nouveau_ivmm *ivmm =
    633  1.1  riastrad 				nouveau_ivmm_find(svm, buffer->fault[fi]->inst);
    634  1.1  riastrad 			svmm = ivmm ? ivmm->svmm : NULL;
    635  1.1  riastrad 			inst = buffer->fault[fi]->inst;
    636  1.1  riastrad 			SVM_DBG(svm, "inst %016llx -> svm-%p", inst, svmm);
    637  1.1  riastrad 		}
    638  1.1  riastrad 		buffer->fault[fi]->svmm = svmm;
    639  1.1  riastrad 	}
    640  1.1  riastrad 	mutex_unlock(&svm->mutex);
    641  1.1  riastrad 
    642  1.1  riastrad 	/* Process list of faults. */
    643  1.1  riastrad 	args.i.i.version = 0;
    644  1.1  riastrad 	args.i.i.type = NVIF_IOCTL_V0_MTHD;
    645  1.1  riastrad 	args.i.m.version = 0;
    646  1.1  riastrad 	args.i.m.method = NVIF_VMM_V0_PFNMAP;
    647  1.1  riastrad 	args.i.p.version = 0;
    648  1.1  riastrad 
    649  1.1  riastrad 	for (fi = 0; fn = fi + 1, fi < buffer->fault_nr; fi = fn) {
    650  1.1  riastrad 		struct svm_notifier notifier;
    651  1.1  riastrad 		struct mm_struct *mm;
    652  1.1  riastrad 
    653  1.1  riastrad 		/* Cancel any faults from non-SVM channels. */
    654  1.1  riastrad 		if (!(svmm = buffer->fault[fi]->svmm)) {
    655  1.1  riastrad 			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
    656  1.1  riastrad 			continue;
    657  1.1  riastrad 		}
    658  1.1  riastrad 		SVMM_DBG(svmm, "addr %016llx", buffer->fault[fi]->addr);
    659  1.1  riastrad 
    660  1.1  riastrad 		/* We try and group handling of faults within a small
    661  1.1  riastrad 		 * window into a single update.
    662  1.1  riastrad 		 */
    663  1.1  riastrad 		start = buffer->fault[fi]->addr;
    664  1.1  riastrad 		limit = start + (ARRAY_SIZE(args.phys) << PAGE_SHIFT);
    665  1.1  riastrad 		if (start < svmm->unmanaged.limit)
    666  1.1  riastrad 			limit = min_t(u64, limit, svmm->unmanaged.start);
    667  1.1  riastrad 		else
    668  1.1  riastrad 		if (limit > svmm->unmanaged.start)
    669  1.1  riastrad 			start = max_t(u64, start, svmm->unmanaged.limit);
    670  1.1  riastrad 		SVMM_DBG(svmm, "wndw %016llx-%016llx", start, limit);
    671  1.1  riastrad 
    672  1.1  riastrad 		mm = svmm->notifier.mm;
    673  1.1  riastrad 		if (!mmget_not_zero(mm)) {
    674  1.1  riastrad 			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
    675  1.1  riastrad 			continue;
    676  1.1  riastrad 		}
    677  1.1  riastrad 
    678  1.1  riastrad 		/* Intersect fault window with the CPU VMA, cancelling
    679  1.1  riastrad 		 * the fault if the address is invalid.
    680  1.1  riastrad 		 */
    681  1.1  riastrad 		down_read(&mm->mmap_sem);
    682  1.1  riastrad 		vma = find_vma_intersection(mm, start, limit);
    683  1.1  riastrad 		if (!vma) {
    684  1.1  riastrad 			SVMM_ERR(svmm, "wndw %016llx-%016llx", start, limit);
    685  1.1  riastrad 			up_read(&mm->mmap_sem);
    686  1.1  riastrad 			mmput(mm);
    687  1.1  riastrad 			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
    688  1.1  riastrad 			continue;
    689  1.1  riastrad 		}
    690  1.1  riastrad 		start = max_t(u64, start, vma->vm_start);
    691  1.1  riastrad 		limit = min_t(u64, limit, vma->vm_end);
    692  1.1  riastrad 		up_read(&mm->mmap_sem);
    693  1.1  riastrad 		SVMM_DBG(svmm, "wndw %016llx-%016llx", start, limit);
    694  1.1  riastrad 
    695  1.1  riastrad 		if (buffer->fault[fi]->addr != start) {
    696  1.1  riastrad 			SVMM_ERR(svmm, "addr %016llx", buffer->fault[fi]->addr);
    697  1.1  riastrad 			mmput(mm);
    698  1.1  riastrad 			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
    699  1.1  riastrad 			continue;
    700  1.1  riastrad 		}
    701  1.1  riastrad 
    702  1.1  riastrad 		/* Prepare the GPU-side update of all pages within the
    703  1.1  riastrad 		 * fault window, determining required pages and access
    704  1.1  riastrad 		 * permissions based on pending faults.
    705  1.1  riastrad 		 */
    706  1.1  riastrad 		args.i.p.page = PAGE_SHIFT;
    707  1.1  riastrad 		args.i.p.addr = start;
    708  1.1  riastrad 		for (fn = fi, pi = 0;;) {
    709  1.1  riastrad 			/* Determine required permissions based on GPU fault
    710  1.1  riastrad 			 * access flags.
    711  1.1  riastrad 			 *XXX: atomic?
    712  1.1  riastrad 			 */
    713  1.1  riastrad 			if (buffer->fault[fn]->access != 0 /* READ. */ &&
    714  1.1  riastrad 			    buffer->fault[fn]->access != 3 /* PREFETCH. */) {
    715  1.1  riastrad 				args.phys[pi++] = NVIF_VMM_PFNMAP_V0_V |
    716  1.1  riastrad 						  NVIF_VMM_PFNMAP_V0_W;
    717  1.1  riastrad 			} else {
    718  1.1  riastrad 				args.phys[pi++] = NVIF_VMM_PFNMAP_V0_V;
    719  1.1  riastrad 			}
    720  1.1  riastrad 			args.i.p.size = pi << PAGE_SHIFT;
    721  1.1  riastrad 
    722  1.1  riastrad 			/* It's okay to skip over duplicate addresses from the
    723  1.1  riastrad 			 * same SVMM as faults are ordered by access type such
    724  1.1  riastrad 			 * that only the first one needs to be handled.
    725  1.1  riastrad 			 *
    726  1.1  riastrad 			 * ie. WRITE faults appear first, thus any handling of
    727  1.1  riastrad 			 * pending READ faults will already be satisfied.
    728  1.1  riastrad 			 */
    729  1.1  riastrad 			while (++fn < buffer->fault_nr &&
    730  1.1  riastrad 			       buffer->fault[fn]->svmm == svmm &&
    731  1.1  riastrad 			       buffer->fault[fn    ]->addr ==
    732  1.1  riastrad 			       buffer->fault[fn - 1]->addr);
    733  1.1  riastrad 
    734  1.1  riastrad 			/* If the next fault is outside the window, or all GPU
    735  1.1  riastrad 			 * faults have been dealt with, we're done here.
    736  1.1  riastrad 			 */
    737  1.1  riastrad 			if (fn >= buffer->fault_nr ||
    738  1.1  riastrad 			    buffer->fault[fn]->svmm != svmm ||
    739  1.1  riastrad 			    buffer->fault[fn]->addr >= limit)
    740  1.1  riastrad 				break;
    741  1.1  riastrad 
    742  1.1  riastrad 			/* Fill in the gap between this fault and the next. */
    743  1.1  riastrad 			fill = (buffer->fault[fn    ]->addr -
    744  1.1  riastrad 				buffer->fault[fn - 1]->addr) >> PAGE_SHIFT;
    745  1.1  riastrad 			while (--fill)
    746  1.1  riastrad 				args.phys[pi++] = NVIF_VMM_PFNMAP_V0_NONE;
    747  1.1  riastrad 		}
    748  1.1  riastrad 
    749  1.1  riastrad 		SVMM_DBG(svmm, "wndw %016llx-%016llx covering %d fault(s)",
    750  1.1  riastrad 			 args.i.p.addr,
    751  1.1  riastrad 			 args.i.p.addr + args.i.p.size, fn - fi);
    752  1.1  riastrad 
    753  1.1  riastrad 		notifier.svmm = svmm;
    754  1.1  riastrad 		ret = mmu_interval_notifier_insert(&notifier.notifier,
    755  1.1  riastrad 						   svmm->notifier.mm,
    756  1.1  riastrad 						   args.i.p.addr, args.i.p.size,
    757  1.1  riastrad 						   &nouveau_svm_mni_ops);
    758  1.1  riastrad 		if (!ret) {
    759  1.1  riastrad 			ret = nouveau_range_fault(
    760  1.1  riastrad 				svmm, svm->drm, &args,
    761  1.1  riastrad 				sizeof(args.i) + pi * sizeof(args.phys[0]),
    762  1.1  riastrad 				args.phys, &notifier);
    763  1.1  riastrad 			mmu_interval_notifier_remove(&notifier.notifier);
    764  1.1  riastrad 		}
    765  1.1  riastrad 		mmput(mm);
    766  1.1  riastrad 
    767  1.1  riastrad 		/* Cancel any faults in the window whose pages didn't manage
    768  1.1  riastrad 		 * to keep their valid bit, or stay writeable when required.
    769  1.1  riastrad 		 *
    770  1.1  riastrad 		 * If handling failed completely, cancel all faults.
    771  1.1  riastrad 		 */
    772  1.1  riastrad 		while (fi < fn) {
    773  1.1  riastrad 			struct nouveau_svm_fault *fault = buffer->fault[fi++];
    774  1.1  riastrad 			pi = (fault->addr - args.i.p.addr) >> PAGE_SHIFT;
    775  1.1  riastrad 			if (ret ||
    776  1.1  riastrad 			     !(args.phys[pi] & NVIF_VMM_PFNMAP_V0_V) ||
    777  1.1  riastrad 			    (!(args.phys[pi] & NVIF_VMM_PFNMAP_V0_W) &&
    778  1.1  riastrad 			     fault->access != 0 && fault->access != 3)) {
    779  1.1  riastrad 				nouveau_svm_fault_cancel_fault(svm, fault);
    780  1.1  riastrad 				continue;
    781  1.1  riastrad 			}
    782  1.1  riastrad 			replay++;
    783  1.1  riastrad 		}
    784  1.1  riastrad 	}
    785  1.1  riastrad 
    786  1.1  riastrad 	/* Issue fault replay to the GPU. */
    787  1.1  riastrad 	if (replay)
    788  1.1  riastrad 		nouveau_svm_fault_replay(svm);
    789  1.1  riastrad 	return NVIF_NOTIFY_KEEP;
    790  1.1  riastrad }
    791  1.1  riastrad 
    792  1.1  riastrad static void
    793  1.1  riastrad nouveau_svm_fault_buffer_fini(struct nouveau_svm *svm, int id)
    794  1.1  riastrad {
    795  1.1  riastrad 	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
    796  1.1  riastrad 	nvif_notify_put(&buffer->notify);
    797  1.1  riastrad }
    798  1.1  riastrad 
    799  1.1  riastrad static int
    800  1.1  riastrad nouveau_svm_fault_buffer_init(struct nouveau_svm *svm, int id)
    801  1.1  riastrad {
    802  1.1  riastrad 	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
    803  1.1  riastrad 	struct nvif_object *device = &svm->drm->client.device.object;
    804  1.1  riastrad 	buffer->get = nvif_rd32(device, buffer->getaddr);
    805  1.1  riastrad 	buffer->put = nvif_rd32(device, buffer->putaddr);
    806  1.1  riastrad 	SVM_DBG(svm, "get %08x put %08x (init)", buffer->get, buffer->put);
    807  1.1  riastrad 	return nvif_notify_get(&buffer->notify);
    808  1.1  riastrad }
    809  1.1  riastrad 
    810  1.1  riastrad static void
    811  1.1  riastrad nouveau_svm_fault_buffer_dtor(struct nouveau_svm *svm, int id)
    812  1.1  riastrad {
    813  1.1  riastrad 	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
    814  1.1  riastrad 	int i;
    815  1.1  riastrad 
    816  1.1  riastrad 	if (buffer->fault) {
    817  1.1  riastrad 		for (i = 0; buffer->fault[i] && i < buffer->entries; i++)
    818  1.1  riastrad 			kfree(buffer->fault[i]);
    819  1.1  riastrad 		kvfree(buffer->fault);
    820  1.1  riastrad 	}
    821  1.1  riastrad 
    822  1.1  riastrad 	nouveau_svm_fault_buffer_fini(svm, id);
    823  1.1  riastrad 
    824  1.1  riastrad 	nvif_notify_fini(&buffer->notify);
    825  1.1  riastrad 	nvif_object_fini(&buffer->object);
    826  1.1  riastrad }
    827  1.1  riastrad 
    828  1.1  riastrad static int
    829  1.1  riastrad nouveau_svm_fault_buffer_ctor(struct nouveau_svm *svm, s32 oclass, int id)
    830  1.1  riastrad {
    831  1.1  riastrad 	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
    832  1.1  riastrad 	struct nouveau_drm *drm = svm->drm;
    833  1.1  riastrad 	struct nvif_object *device = &drm->client.device.object;
    834  1.1  riastrad 	struct nvif_clb069_v0 args = {};
    835  1.1  riastrad 	int ret;
    836  1.1  riastrad 
    837  1.1  riastrad 	buffer->id = id;
    838  1.1  riastrad 
    839  1.1  riastrad 	ret = nvif_object_init(device, 0, oclass, &args, sizeof(args),
    840  1.1  riastrad 			       &buffer->object);
    841  1.1  riastrad 	if (ret < 0) {
    842  1.1  riastrad 		SVM_ERR(svm, "Fault buffer allocation failed: %d", ret);
    843  1.1  riastrad 		return ret;
    844  1.1  riastrad 	}
    845  1.1  riastrad 
    846  1.1  riastrad 	nvif_object_map(&buffer->object, NULL, 0);
    847  1.1  riastrad 	buffer->entries = args.entries;
    848  1.1  riastrad 	buffer->getaddr = args.get;
    849  1.1  riastrad 	buffer->putaddr = args.put;
    850  1.1  riastrad 
    851  1.1  riastrad 	ret = nvif_notify_init(&buffer->object, nouveau_svm_fault, true,
    852  1.1  riastrad 			       NVB069_V0_NTFY_FAULT, NULL, 0, 0,
    853  1.1  riastrad 			       &buffer->notify);
    854  1.1  riastrad 	if (ret)
    855  1.1  riastrad 		return ret;
    856  1.1  riastrad 
    857  1.1  riastrad 	buffer->fault = kvzalloc(sizeof(*buffer->fault) * buffer->entries, GFP_KERNEL);
    858  1.1  riastrad 	if (!buffer->fault)
    859  1.1  riastrad 		return -ENOMEM;
    860  1.1  riastrad 
    861  1.1  riastrad 	return nouveau_svm_fault_buffer_init(svm, id);
    862  1.1  riastrad }
    863  1.1  riastrad 
    864  1.1  riastrad void
    865  1.1  riastrad nouveau_svm_resume(struct nouveau_drm *drm)
    866  1.1  riastrad {
    867  1.1  riastrad 	struct nouveau_svm *svm = drm->svm;
    868  1.1  riastrad 	if (svm)
    869  1.1  riastrad 		nouveau_svm_fault_buffer_init(svm, 0);
    870  1.1  riastrad }
    871  1.1  riastrad 
    872  1.1  riastrad void
    873  1.1  riastrad nouveau_svm_suspend(struct nouveau_drm *drm)
    874  1.1  riastrad {
    875  1.1  riastrad 	struct nouveau_svm *svm = drm->svm;
    876  1.1  riastrad 	if (svm)
    877  1.1  riastrad 		nouveau_svm_fault_buffer_fini(svm, 0);
    878  1.1  riastrad }
    879  1.1  riastrad 
    880  1.1  riastrad void
    881  1.1  riastrad nouveau_svm_fini(struct nouveau_drm *drm)
    882  1.1  riastrad {
    883  1.1  riastrad 	struct nouveau_svm *svm = drm->svm;
    884  1.1  riastrad 	if (svm) {
    885  1.1  riastrad 		nouveau_svm_fault_buffer_dtor(svm, 0);
    886  1.1  riastrad 		kfree(drm->svm);
    887  1.1  riastrad 		drm->svm = NULL;
    888  1.1  riastrad 	}
    889  1.1  riastrad }
    890  1.1  riastrad 
    891  1.1  riastrad void
    892  1.1  riastrad nouveau_svm_init(struct nouveau_drm *drm)
    893  1.1  riastrad {
    894  1.1  riastrad 	static const struct nvif_mclass buffers[] = {
    895  1.1  riastrad 		{   VOLTA_FAULT_BUFFER_A, 0 },
    896  1.1  riastrad 		{ MAXWELL_FAULT_BUFFER_A, 0 },
    897  1.1  riastrad 		{}
    898  1.1  riastrad 	};
    899  1.1  riastrad 	struct nouveau_svm *svm;
    900  1.1  riastrad 	int ret;
    901  1.1  riastrad 
    902  1.1  riastrad 	/* Disable on Volta and newer until channel recovery is fixed,
    903  1.1  riastrad 	 * otherwise clients will have a trivial way to trash the GPU
    904  1.1  riastrad 	 * for everyone.
    905  1.1  riastrad 	 */
    906  1.1  riastrad 	if (drm->client.device.info.family > NV_DEVICE_INFO_V0_PASCAL)
    907  1.1  riastrad 		return;
    908  1.1  riastrad 
    909  1.1  riastrad 	if (!(drm->svm = svm = kzalloc(sizeof(*drm->svm), GFP_KERNEL)))
    910  1.1  riastrad 		return;
    911  1.1  riastrad 
    912  1.1  riastrad 	drm->svm->drm = drm;
    913  1.1  riastrad 	mutex_init(&drm->svm->mutex);
    914  1.1  riastrad 	INIT_LIST_HEAD(&drm->svm->inst);
    915  1.1  riastrad 
    916  1.1  riastrad 	ret = nvif_mclass(&drm->client.device.object, buffers);
    917  1.1  riastrad 	if (ret < 0) {
    918  1.1  riastrad 		SVM_DBG(svm, "No supported fault buffer class");
    919  1.1  riastrad 		nouveau_svm_fini(drm);
    920  1.1  riastrad 		return;
    921  1.1  riastrad 	}
    922  1.1  riastrad 
    923  1.1  riastrad 	ret = nouveau_svm_fault_buffer_ctor(svm, buffers[ret].oclass, 0);
    924  1.1  riastrad 	if (ret) {
    925  1.1  riastrad 		nouveau_svm_fini(drm);
    926  1.1  riastrad 		return;
    927  1.1  riastrad 	}
    928  1.1  riastrad 
    929  1.1  riastrad 	SVM_DBG(svm, "Initialised");
    930  1.1  riastrad }
    931