Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

Need help to get security updates for your Linux BSP?
Loading...
v6.13.7
   1/*
   2 * Copyright 2018 Red Hat Inc.
   3 *
   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:
  10 *
  11 * The above copyright notice and this permission notice shall be included in
  12 * all copies or substantial portions of the Software.
  13 *
  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.
  21 */
  22#include "nouveau_svm.h"
  23#include "nouveau_drv.h"
  24#include "nouveau_chan.h"
  25#include "nouveau_dmem.h"
  26
  27#include <nvif/event.h>
  28#include <nvif/object.h>
  29#include <nvif/vmm.h>
  30
  31#include <nvif/class.h>
  32#include <nvif/clb069.h>
  33#include <nvif/ifc00d.h>
  34
  35#include <linux/sched/mm.h>
  36#include <linux/sort.h>
  37#include <linux/hmm.h>
  38#include <linux/memremap.h>
  39#include <linux/rmap.h>
  40
  41struct nouveau_svm {
  42	struct nouveau_drm *drm;
  43	struct mutex mutex;
  44	struct list_head inst;
  45
  46	struct nouveau_svm_fault_buffer {
  47		int id;
  48		struct nvif_object object;
  49		u32 entries;
  50		u32 getaddr;
  51		u32 putaddr;
  52		u32 get;
  53		u32 put;
  54		struct nvif_event notify;
  55		struct work_struct work;
  56
  57		struct nouveau_svm_fault {
  58			u64 inst;
  59			u64 addr;
  60			u64 time;
  61			u32 engine;
  62			u8  gpc;
  63			u8  hub;
  64			u8  access;
  65			u8  client;
  66			u8  fault;
  67			struct nouveau_svmm *svmm;
  68		} **fault;
  69		int fault_nr;
  70	} buffer[];
  71};
  72
  73#define FAULT_ACCESS_READ 0
  74#define FAULT_ACCESS_WRITE 1
  75#define FAULT_ACCESS_ATOMIC 2
  76#define FAULT_ACCESS_PREFETCH 3
  77
  78#define SVM_DBG(s,f,a...) NV_DEBUG((s)->drm, "svm: "f"\n", ##a)
  79#define SVM_ERR(s,f,a...) NV_WARN((s)->drm, "svm: "f"\n", ##a)
  80
  81struct nouveau_pfnmap_args {
  82	struct nvif_ioctl_v0 i;
  83	struct nvif_ioctl_mthd_v0 m;
  84	struct nvif_vmm_pfnmap_v0 p;
  85};
  86
  87struct nouveau_ivmm {
  88	struct nouveau_svmm *svmm;
  89	u64 inst;
  90	struct list_head head;
  91};
  92
  93static struct nouveau_ivmm *
  94nouveau_ivmm_find(struct nouveau_svm *svm, u64 inst)
  95{
  96	struct nouveau_ivmm *ivmm;
  97	list_for_each_entry(ivmm, &svm->inst, head) {
  98		if (ivmm->inst == inst)
  99			return ivmm;
 100	}
 101	return NULL;
 102}
 103
 
 
 
 
 
 
 
 
 
 
 
 
 
 104#define SVMM_DBG(s,f,a...)                                                     \
 105	NV_DEBUG((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
 106#define SVMM_ERR(s,f,a...)                                                     \
 107	NV_WARN((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
 108
 109int
 110nouveau_svmm_bind(struct drm_device *dev, void *data,
 111		  struct drm_file *file_priv)
 112{
 113	struct nouveau_cli *cli = nouveau_cli(file_priv);
 114	struct drm_nouveau_svm_bind *args = data;
 115	unsigned target, cmd;
 116	unsigned long addr, end;
 117	struct mm_struct *mm;
 118
 119	args->va_start &= PAGE_MASK;
 120	args->va_end = ALIGN(args->va_end, PAGE_SIZE);
 121
 122	/* Sanity check arguments */
 123	if (args->reserved0 || args->reserved1)
 124		return -EINVAL;
 125	if (args->header & (~NOUVEAU_SVM_BIND_VALID_MASK))
 126		return -EINVAL;
 127	if (args->va_start >= args->va_end)
 128		return -EINVAL;
 
 
 129
 130	cmd = args->header >> NOUVEAU_SVM_BIND_COMMAND_SHIFT;
 131	cmd &= NOUVEAU_SVM_BIND_COMMAND_MASK;
 132	switch (cmd) {
 133	case NOUVEAU_SVM_BIND_COMMAND__MIGRATE:
 134		break;
 135	default:
 136		return -EINVAL;
 137	}
 138
 
 
 
 139	/* FIXME support CPU target ie all target value < GPU_VRAM */
 140	target = args->header >> NOUVEAU_SVM_BIND_TARGET_SHIFT;
 141	target &= NOUVEAU_SVM_BIND_TARGET_MASK;
 142	switch (target) {
 143	case NOUVEAU_SVM_BIND_TARGET__GPU_VRAM:
 144		break;
 145	default:
 146		return -EINVAL;
 147	}
 148
 149	/*
 150	 * FIXME: For now refuse non 0 stride, we need to change the migrate
 151	 * kernel function to handle stride to avoid to create a mess within
 152	 * each device driver.
 153	 */
 154	if (args->stride)
 155		return -EINVAL;
 156
 
 
 
 
 
 
 157	/*
 158	 * Ok we are ask to do something sane, for now we only support migrate
 159	 * commands but we will add things like memory policy (what to do on
 160	 * page fault) and maybe some other commands.
 161	 */
 162
 163	mm = get_task_mm(current);
 164	if (!mm) {
 165		return -EINVAL;
 166	}
 167	mmap_read_lock(mm);
 168
 169	if (!cli->svm.svmm) {
 170		mmap_read_unlock(mm);
 171		mmput(mm);
 172		return -EINVAL;
 173	}
 174
 175	for (addr = args->va_start, end = args->va_end; addr < end;) {
 176		struct vm_area_struct *vma;
 177		unsigned long next;
 178
 179		vma = find_vma_intersection(mm, addr, end);
 180		if (!vma)
 181			break;
 182
 183		addr = max(addr, vma->vm_start);
 184		next = min(vma->vm_end, end);
 185		/* This is a best effort so we ignore errors */
 186		nouveau_dmem_migrate_vma(cli->drm, cli->svm.svmm, vma, addr,
 187					 next);
 188		addr = next;
 189	}
 190
 191	/*
 192	 * FIXME Return the number of page we have migrated, again we need to
 193	 * update the migrate API to return that information so that we can
 194	 * report it to user space.
 195	 */
 196	args->result = 0;
 197
 198	mmap_read_unlock(mm);
 199	mmput(mm);
 200
 201	return 0;
 202}
 203
 204/* Unlink channel instance from SVMM. */
 205void
 206nouveau_svmm_part(struct nouveau_svmm *svmm, u64 inst)
 207{
 208	struct nouveau_ivmm *ivmm;
 209	if (svmm) {
 210		mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
 211		ivmm = nouveau_ivmm_find(svmm->vmm->cli->drm->svm, inst);
 212		if (ivmm) {
 213			list_del(&ivmm->head);
 214			kfree(ivmm);
 215		}
 216		mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
 217	}
 218}
 219
 220/* Link channel instance to SVMM. */
 221int
 222nouveau_svmm_join(struct nouveau_svmm *svmm, u64 inst)
 223{
 224	struct nouveau_ivmm *ivmm;
 225	if (svmm) {
 226		if (!(ivmm = kmalloc(sizeof(*ivmm), GFP_KERNEL)))
 227			return -ENOMEM;
 228		ivmm->svmm = svmm;
 229		ivmm->inst = inst;
 230
 231		mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
 232		list_add(&ivmm->head, &svmm->vmm->cli->drm->svm->inst);
 233		mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
 234	}
 235	return 0;
 236}
 237
 238/* Invalidate SVMM address-range on GPU. */
 239void
 240nouveau_svmm_invalidate(struct nouveau_svmm *svmm, u64 start, u64 limit)
 241{
 242	if (limit > start) {
 
 
 243		nvif_object_mthd(&svmm->vmm->vmm.object, NVIF_VMM_V0_PFNCLR,
 244				 &(struct nvif_vmm_pfnclr_v0) {
 245					.addr = start,
 246					.size = limit - start,
 247				 }, sizeof(struct nvif_vmm_pfnclr_v0));
 
 248	}
 249}
 250
 251static int
 252nouveau_svmm_invalidate_range_start(struct mmu_notifier *mn,
 253				    const struct mmu_notifier_range *update)
 254{
 255	struct nouveau_svmm *svmm =
 256		container_of(mn, struct nouveau_svmm, notifier);
 257	unsigned long start = update->start;
 258	unsigned long limit = update->end;
 259
 260	if (!mmu_notifier_range_blockable(update))
 261		return -EAGAIN;
 262
 263	SVMM_DBG(svmm, "invalidate %016lx-%016lx", start, limit);
 264
 265	mutex_lock(&svmm->mutex);
 266	if (unlikely(!svmm->vmm))
 267		goto out;
 268
 269	/*
 270	 * Ignore invalidation callbacks for device private pages since
 271	 * the invalidation is handled as part of the migration process.
 272	 */
 273	if (update->event == MMU_NOTIFY_MIGRATE &&
 274	    update->owner == svmm->vmm->cli->drm->dev)
 275		goto out;
 276
 277	if (limit > svmm->unmanaged.start && start < svmm->unmanaged.limit) {
 278		if (start < svmm->unmanaged.start) {
 279			nouveau_svmm_invalidate(svmm, start,
 280						svmm->unmanaged.limit);
 281		}
 282		start = svmm->unmanaged.limit;
 283	}
 284
 285	nouveau_svmm_invalidate(svmm, start, limit);
 286
 287out:
 288	mutex_unlock(&svmm->mutex);
 289	return 0;
 290}
 291
 292static void nouveau_svmm_free_notifier(struct mmu_notifier *mn)
 
 293{
 294	kfree(container_of(mn, struct nouveau_svmm, notifier));
 295}
 296
 297static const struct mmu_notifier_ops nouveau_mn_ops = {
 298	.invalidate_range_start = nouveau_svmm_invalidate_range_start,
 299	.free_notifier = nouveau_svmm_free_notifier,
 
 300};
 301
 302void
 303nouveau_svmm_fini(struct nouveau_svmm **psvmm)
 304{
 305	struct nouveau_svmm *svmm = *psvmm;
 306	if (svmm) {
 307		mutex_lock(&svmm->mutex);
 308		svmm->vmm = NULL;
 309		mutex_unlock(&svmm->mutex);
 310		mmu_notifier_put(&svmm->notifier);
 311		*psvmm = NULL;
 312	}
 313}
 314
 315int
 316nouveau_svmm_init(struct drm_device *dev, void *data,
 317		  struct drm_file *file_priv)
 318{
 319	struct nouveau_cli *cli = nouveau_cli(file_priv);
 320	struct nouveau_svmm *svmm;
 321	struct drm_nouveau_svm_init *args = data;
 322	int ret;
 323
 324	/* We need to fail if svm is disabled */
 325	if (!cli->drm->svm)
 326		return -ENOSYS;
 327
 328	/* Allocate tracking for SVM-enabled VMM. */
 329	if (!(svmm = kzalloc(sizeof(*svmm), GFP_KERNEL)))
 330		return -ENOMEM;
 331	svmm->vmm = &cli->svm;
 332	svmm->unmanaged.start = args->unmanaged_addr;
 333	svmm->unmanaged.limit = args->unmanaged_addr + args->unmanaged_size;
 334	mutex_init(&svmm->mutex);
 335
 336	/* Check that SVM isn't already enabled for the client. */
 337	mutex_lock(&cli->mutex);
 338	if (cli->svm.cli) {
 339		ret = -EBUSY;
 340		goto out_free;
 341	}
 342
 343	/* Allocate a new GPU VMM that can support SVM (managed by the
 344	 * client, with replayable faults enabled).
 345	 *
 346	 * All future channel/memory allocations will make use of this
 347	 * VMM instead of the standard one.
 348	 */
 349	ret = nvif_vmm_ctor(&cli->mmu, "svmVmm",
 350			    cli->vmm.vmm.object.oclass, MANAGED,
 351			    args->unmanaged_addr, args->unmanaged_size,
 352			    &(struct gp100_vmm_v0) {
 353				.fault_replay = true,
 354			    }, sizeof(struct gp100_vmm_v0), &cli->svm.vmm);
 355	if (ret)
 356		goto out_free;
 357
 358	mmap_write_lock(current->mm);
 359	svmm->notifier.ops = &nouveau_mn_ops;
 360	ret = __mmu_notifier_register(&svmm->notifier, current->mm);
 361	if (ret)
 362		goto out_mm_unlock;
 363	/* Note, ownership of svmm transfers to mmu_notifier */
 364
 365	cli->svm.svmm = svmm;
 366	cli->svm.cli = cli;
 367	mmap_write_unlock(current->mm);
 368	mutex_unlock(&cli->mutex);
 369	return 0;
 370
 371out_mm_unlock:
 372	mmap_write_unlock(current->mm);
 373out_free:
 374	mutex_unlock(&cli->mutex);
 375	kfree(svmm);
 376	return ret;
 377}
 378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 379/* Issue fault replay for GPU to retry accesses that faulted previously. */
 380static void
 381nouveau_svm_fault_replay(struct nouveau_svm *svm)
 382{
 383	SVM_DBG(svm, "replay");
 384	WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
 385				 GP100_VMM_VN_FAULT_REPLAY,
 386				 &(struct gp100_vmm_fault_replay_vn) {},
 387				 sizeof(struct gp100_vmm_fault_replay_vn)));
 388}
 389
 390/* Cancel a replayable fault that could not be handled.
 391 *
 392 * Cancelling the fault will trigger recovery to reset the engine
 393 * and kill the offending channel (ie. GPU SIGSEGV).
 394 */
 395static void
 396nouveau_svm_fault_cancel(struct nouveau_svm *svm,
 397			 u64 inst, u8 hub, u8 gpc, u8 client)
 398{
 399	SVM_DBG(svm, "cancel %016llx %d %02x %02x", inst, hub, gpc, client);
 400	WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
 401				 GP100_VMM_VN_FAULT_CANCEL,
 402				 &(struct gp100_vmm_fault_cancel_v0) {
 403					.hub = hub,
 404					.gpc = gpc,
 405					.client = client,
 406					.inst = inst,
 407				 }, sizeof(struct gp100_vmm_fault_cancel_v0)));
 408}
 409
 410static void
 411nouveau_svm_fault_cancel_fault(struct nouveau_svm *svm,
 412			       struct nouveau_svm_fault *fault)
 413{
 414	nouveau_svm_fault_cancel(svm, fault->inst,
 415				      fault->hub,
 416				      fault->gpc,
 417				      fault->client);
 418}
 419
 420static int
 421nouveau_svm_fault_priority(u8 fault)
 422{
 423	switch (fault) {
 424	case FAULT_ACCESS_PREFETCH:
 425		return 0;
 426	case FAULT_ACCESS_READ:
 427		return 1;
 428	case FAULT_ACCESS_WRITE:
 429		return 2;
 430	case FAULT_ACCESS_ATOMIC:
 431		return 3;
 432	default:
 433		WARN_ON_ONCE(1);
 434		return -1;
 435	}
 436}
 437
 438static int
 439nouveau_svm_fault_cmp(const void *a, const void *b)
 440{
 441	const struct nouveau_svm_fault *fa = *(struct nouveau_svm_fault **)a;
 442	const struct nouveau_svm_fault *fb = *(struct nouveau_svm_fault **)b;
 443	int ret;
 444	if ((ret = (s64)fa->inst - fb->inst))
 445		return ret;
 446	if ((ret = (s64)fa->addr - fb->addr))
 447		return ret;
 448	return nouveau_svm_fault_priority(fa->access) -
 449		nouveau_svm_fault_priority(fb->access);
 
 450}
 451
 452static void
 453nouveau_svm_fault_cache(struct nouveau_svm *svm,
 454			struct nouveau_svm_fault_buffer *buffer, u32 offset)
 455{
 456	struct nvif_object *memory = &buffer->object;
 457	const u32 instlo = nvif_rd32(memory, offset + 0x00);
 458	const u32 insthi = nvif_rd32(memory, offset + 0x04);
 459	const u32 addrlo = nvif_rd32(memory, offset + 0x08);
 460	const u32 addrhi = nvif_rd32(memory, offset + 0x0c);
 461	const u32 timelo = nvif_rd32(memory, offset + 0x10);
 462	const u32 timehi = nvif_rd32(memory, offset + 0x14);
 463	const u32 engine = nvif_rd32(memory, offset + 0x18);
 464	const u32   info = nvif_rd32(memory, offset + 0x1c);
 465	const u64   inst = (u64)insthi << 32 | instlo;
 466	const u8     gpc = (info & 0x1f000000) >> 24;
 467	const u8     hub = (info & 0x00100000) >> 20;
 468	const u8  client = (info & 0x00007f00) >> 8;
 469	struct nouveau_svm_fault *fault;
 470
 471	//XXX: i think we're supposed to spin waiting */
 472	if (WARN_ON(!(info & 0x80000000)))
 473		return;
 474
 475	nvif_mask(memory, offset + 0x1c, 0x80000000, 0x00000000);
 476
 477	if (!buffer->fault[buffer->fault_nr]) {
 478		fault = kmalloc(sizeof(*fault), GFP_KERNEL);
 479		if (WARN_ON(!fault)) {
 480			nouveau_svm_fault_cancel(svm, inst, hub, gpc, client);
 481			return;
 482		}
 483		buffer->fault[buffer->fault_nr] = fault;
 484	}
 485
 486	fault = buffer->fault[buffer->fault_nr++];
 487	fault->inst   = inst;
 488	fault->addr   = (u64)addrhi << 32 | addrlo;
 489	fault->time   = (u64)timehi << 32 | timelo;
 490	fault->engine = engine;
 491	fault->gpc    = gpc;
 492	fault->hub    = hub;
 493	fault->access = (info & 0x000f0000) >> 16;
 494	fault->client = client;
 495	fault->fault  = (info & 0x0000001f);
 496
 497	SVM_DBG(svm, "fault %016llx %016llx %02x",
 498		fault->inst, fault->addr, fault->access);
 499}
 500
 501struct svm_notifier {
 502	struct mmu_interval_notifier notifier;
 503	struct nouveau_svmm *svmm;
 504};
 505
 506static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni,
 507					 const struct mmu_notifier_range *range,
 508					 unsigned long cur_seq)
 509{
 510	struct svm_notifier *sn =
 511		container_of(mni, struct svm_notifier, notifier);
 512
 513	if (range->event == MMU_NOTIFY_EXCLUSIVE &&
 514	    range->owner == sn->svmm->vmm->cli->drm->dev)
 515		return true;
 516
 517	/*
 518	 * serializes the update to mni->invalidate_seq done by caller and
 519	 * prevents invalidation of the PTE from progressing while HW is being
 520	 * programmed. This is very hacky and only works because the normal
 521	 * notifier that does invalidation is always called after the range
 522	 * notifier.
 523	 */
 524	if (mmu_notifier_range_blockable(range))
 525		mutex_lock(&sn->svmm->mutex);
 526	else if (!mutex_trylock(&sn->svmm->mutex))
 527		return false;
 528	mmu_interval_set_seq(mni, cur_seq);
 529	mutex_unlock(&sn->svmm->mutex);
 530	return true;
 531}
 532
 533static const struct mmu_interval_notifier_ops nouveau_svm_mni_ops = {
 534	.invalidate = nouveau_svm_range_invalidate,
 535};
 536
 537static void nouveau_hmm_convert_pfn(struct nouveau_drm *drm,
 538				    struct hmm_range *range,
 539				    struct nouveau_pfnmap_args *args)
 540{
 541	struct page *page;
 542
 543	/*
 544	 * The address prepared here is passed through nvif_object_ioctl()
 545	 * to an eventual DMA map in something like gp100_vmm_pgt_pfn()
 546	 *
 547	 * This is all just encoding the internal hmm representation into a
 548	 * different nouveau internal representation.
 549	 */
 550	if (!(range->hmm_pfns[0] & HMM_PFN_VALID)) {
 551		args->p.phys[0] = 0;
 552		return;
 553	}
 554
 555	page = hmm_pfn_to_page(range->hmm_pfns[0]);
 556	/*
 557	 * Only map compound pages to the GPU if the CPU is also mapping the
 558	 * page as a compound page. Otherwise, the PTE protections might not be
 559	 * consistent (e.g., CPU only maps part of a compound page).
 560	 * Note that the underlying page might still be larger than the
 561	 * CPU mapping (e.g., a PUD sized compound page partially mapped with
 562	 * a PMD sized page table entry).
 563	 */
 564	if (hmm_pfn_to_map_order(range->hmm_pfns[0])) {
 565		unsigned long addr = args->p.addr;
 566
 567		args->p.page = hmm_pfn_to_map_order(range->hmm_pfns[0]) +
 568				PAGE_SHIFT;
 569		args->p.size = 1UL << args->p.page;
 570		args->p.addr &= ~(args->p.size - 1);
 571		page -= (addr - args->p.addr) >> PAGE_SHIFT;
 572	}
 573	if (is_device_private_page(page))
 574		args->p.phys[0] = nouveau_dmem_page_addr(page) |
 575				NVIF_VMM_PFNMAP_V0_V |
 576				NVIF_VMM_PFNMAP_V0_VRAM;
 577	else
 578		args->p.phys[0] = page_to_phys(page) |
 579				NVIF_VMM_PFNMAP_V0_V |
 580				NVIF_VMM_PFNMAP_V0_HOST;
 581	if (range->hmm_pfns[0] & HMM_PFN_WRITE)
 582		args->p.phys[0] |= NVIF_VMM_PFNMAP_V0_W;
 583}
 584
 585static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm,
 586			       struct nouveau_drm *drm,
 587			       struct nouveau_pfnmap_args *args, u32 size,
 588			       struct svm_notifier *notifier)
 589{
 590	unsigned long timeout =
 591		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
 592	struct mm_struct *mm = svmm->notifier.mm;
 593	struct folio *folio;
 594	struct page *page;
 595	unsigned long start = args->p.addr;
 596	unsigned long notifier_seq;
 597	int ret = 0;
 598
 599	ret = mmu_interval_notifier_insert(&notifier->notifier, mm,
 600					args->p.addr, args->p.size,
 601					&nouveau_svm_mni_ops);
 602	if (ret)
 603		return ret;
 604
 605	while (true) {
 606		if (time_after(jiffies, timeout)) {
 607			ret = -EBUSY;
 608			goto out;
 609		}
 610
 611		notifier_seq = mmu_interval_read_begin(&notifier->notifier);
 612		mmap_read_lock(mm);
 613		ret = make_device_exclusive_range(mm, start, start + PAGE_SIZE,
 614					    &page, drm->dev);
 615		mmap_read_unlock(mm);
 616		if (ret <= 0 || !page) {
 617			ret = -EINVAL;
 618			goto out;
 619		}
 620		folio = page_folio(page);
 621
 622		mutex_lock(&svmm->mutex);
 623		if (!mmu_interval_read_retry(&notifier->notifier,
 624					     notifier_seq))
 625			break;
 626		mutex_unlock(&svmm->mutex);
 627
 628		folio_unlock(folio);
 629		folio_put(folio);
 
 
 630	}
 631
 632	/* Map the page on the GPU. */
 633	args->p.page = 12;
 634	args->p.size = PAGE_SIZE;
 635	args->p.addr = start;
 636	args->p.phys[0] = page_to_phys(page) |
 637		NVIF_VMM_PFNMAP_V0_V |
 638		NVIF_VMM_PFNMAP_V0_W |
 639		NVIF_VMM_PFNMAP_V0_A |
 640		NVIF_VMM_PFNMAP_V0_HOST;
 641
 642	ret = nvif_object_ioctl(&svmm->vmm->vmm.object, args, size, NULL);
 643	mutex_unlock(&svmm->mutex);
 644
 645	folio_unlock(folio);
 646	folio_put(folio);
 647
 648out:
 649	mmu_interval_notifier_remove(&notifier->notifier);
 650	return ret;
 651}
 652
 653static int nouveau_range_fault(struct nouveau_svmm *svmm,
 654			       struct nouveau_drm *drm,
 655			       struct nouveau_pfnmap_args *args, u32 size,
 656			       unsigned long hmm_flags,
 657			       struct svm_notifier *notifier)
 658{
 659	unsigned long timeout =
 660		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
 661	/* Have HMM fault pages within the fault window to the GPU. */
 662	unsigned long hmm_pfns[1];
 663	struct hmm_range range = {
 664		.notifier = &notifier->notifier,
 665		.default_flags = hmm_flags,
 666		.hmm_pfns = hmm_pfns,
 667		.dev_private_owner = drm->dev,
 668	};
 669	struct mm_struct *mm = svmm->notifier.mm;
 670	int ret;
 671
 672	ret = mmu_interval_notifier_insert(&notifier->notifier, mm,
 673					args->p.addr, args->p.size,
 674					&nouveau_svm_mni_ops);
 675	if (ret)
 676		return ret;
 677
 678	range.start = notifier->notifier.interval_tree.start;
 679	range.end = notifier->notifier.interval_tree.last + 1;
 680
 681	while (true) {
 682		if (time_after(jiffies, timeout)) {
 
 683			ret = -EBUSY;
 684			goto out;
 685		}
 686
 687		range.notifier_seq = mmu_interval_read_begin(range.notifier);
 688		mmap_read_lock(mm);
 689		ret = hmm_range_fault(&range);
 690		mmap_read_unlock(mm);
 691		if (ret) {
 692			if (ret == -EBUSY)
 693				continue;
 694			goto out;
 695		}
 696
 697		mutex_lock(&svmm->mutex);
 698		if (mmu_interval_read_retry(range.notifier,
 699					    range.notifier_seq)) {
 700			mutex_unlock(&svmm->mutex);
 701			continue;
 702		}
 703		break;
 704	}
 705
 706	nouveau_hmm_convert_pfn(drm, &range, args);
 707
 708	ret = nvif_object_ioctl(&svmm->vmm->vmm.object, args, size, NULL);
 709	mutex_unlock(&svmm->mutex);
 710
 711out:
 712	mmu_interval_notifier_remove(&notifier->notifier);
 713
 714	return ret;
 715}
 716
 717static void
 718nouveau_svm_fault(struct work_struct *work)
 719{
 720	struct nouveau_svm_fault_buffer *buffer = container_of(work, typeof(*buffer), work);
 721	struct nouveau_svm *svm = container_of(buffer, typeof(*svm), buffer[buffer->id]);
 
 
 722	struct nvif_object *device = &svm->drm->client.device.object;
 723	struct nouveau_svmm *svmm;
 724	struct {
 725		struct nouveau_pfnmap_args i;
 726		u64 phys[1];
 
 
 
 
 727	} args;
 728	unsigned long hmm_flags;
 
 729	u64 inst, start, limit;
 730	int fi, fn;
 731	int replay = 0, atomic = 0, ret;
 732
 733	/* Parse available fault buffer entries into a cache, and update
 734	 * the GET pointer so HW can reuse the entries.
 735	 */
 736	SVM_DBG(svm, "fault handler");
 737	if (buffer->get == buffer->put) {
 738		buffer->put = nvif_rd32(device, buffer->putaddr);
 739		buffer->get = nvif_rd32(device, buffer->getaddr);
 740		if (buffer->get == buffer->put)
 741			return;
 742	}
 743	buffer->fault_nr = 0;
 744
 745	SVM_DBG(svm, "get %08x put %08x", buffer->get, buffer->put);
 746	while (buffer->get != buffer->put) {
 747		nouveau_svm_fault_cache(svm, buffer, buffer->get * 0x20);
 748		if (++buffer->get == buffer->entries)
 749			buffer->get = 0;
 750	}
 751	nvif_wr32(device, buffer->getaddr, buffer->get);
 752	SVM_DBG(svm, "%d fault(s) pending", buffer->fault_nr);
 753
 754	/* Sort parsed faults by instance pointer to prevent unnecessary
 755	 * instance to SVMM translations, followed by address and access
 756	 * type to reduce the amount of work when handling the faults.
 757	 */
 758	sort(buffer->fault, buffer->fault_nr, sizeof(*buffer->fault),
 759	     nouveau_svm_fault_cmp, NULL);
 760
 761	/* Lookup SVMM structure for each unique instance pointer. */
 762	mutex_lock(&svm->mutex);
 763	for (fi = 0, svmm = NULL; fi < buffer->fault_nr; fi++) {
 764		if (!svmm || buffer->fault[fi]->inst != inst) {
 765			struct nouveau_ivmm *ivmm =
 766				nouveau_ivmm_find(svm, buffer->fault[fi]->inst);
 767			svmm = ivmm ? ivmm->svmm : NULL;
 768			inst = buffer->fault[fi]->inst;
 769			SVM_DBG(svm, "inst %016llx -> svm-%p", inst, svmm);
 770		}
 771		buffer->fault[fi]->svmm = svmm;
 772	}
 773	mutex_unlock(&svm->mutex);
 774
 775	/* Process list of faults. */
 776	args.i.i.version = 0;
 777	args.i.i.type = NVIF_IOCTL_V0_MTHD;
 778	args.i.m.version = 0;
 779	args.i.m.method = NVIF_VMM_V0_PFNMAP;
 780	args.i.p.version = 0;
 781
 782	for (fi = 0; fn = fi + 1, fi < buffer->fault_nr; fi = fn) {
 783		struct svm_notifier notifier;
 784		struct mm_struct *mm;
 785
 786		/* Cancel any faults from non-SVM channels. */
 787		if (!(svmm = buffer->fault[fi]->svmm)) {
 788			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
 789			continue;
 790		}
 791		SVMM_DBG(svmm, "addr %016llx", buffer->fault[fi]->addr);
 792
 793		/* We try and group handling of faults within a small
 794		 * window into a single update.
 795		 */
 796		start = buffer->fault[fi]->addr;
 797		limit = start + PAGE_SIZE;
 798		if (start < svmm->unmanaged.limit)
 799			limit = min_t(u64, limit, svmm->unmanaged.start);
 
 
 
 
 800
 801		/*
 802		 * Prepare the GPU-side update of all pages within the
 803		 * fault window, determining required pages and access
 804		 * permissions based on pending faults.
 805		 */
 806		args.i.p.addr = start;
 807		args.i.p.page = PAGE_SHIFT;
 808		args.i.p.size = PAGE_SIZE;
 809		/*
 810		 * Determine required permissions based on GPU fault
 811		 * access flags.
 812		 */
 813		switch (buffer->fault[fi]->access) {
 814		case 0: /* READ. */
 815			hmm_flags = HMM_PFN_REQ_FAULT;
 816			break;
 817		case 2: /* ATOMIC. */
 818			atomic = true;
 819			break;
 820		case 3: /* PREFETCH. */
 821			hmm_flags = 0;
 822			break;
 823		default:
 824			hmm_flags = HMM_PFN_REQ_FAULT | HMM_PFN_REQ_WRITE;
 825			break;
 826		}
 827
 828		mm = svmm->notifier.mm;
 829		if (!mmget_not_zero(mm)) {
 
 
 
 
 830			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
 831			continue;
 832		}
 833
 834		notifier.svmm = svmm;
 835		if (atomic)
 836			ret = nouveau_atomic_range_fault(svmm, svm->drm,
 837							 &args.i, sizeof(args),
 838							 &notifier);
 839		else
 840			ret = nouveau_range_fault(svmm, svm->drm, &args.i,
 841						  sizeof(args), hmm_flags,
 842						  &notifier);
 843		mmput(mm);
 
 
 
 
 
 
 
 
 
 844
 845		limit = args.i.p.addr + args.i.p.size;
 846		for (fn = fi; ++fn < buffer->fault_nr; ) {
 847			/* It's okay to skip over duplicate addresses from the
 848			 * same SVMM as faults are ordered by access type such
 849			 * that only the first one needs to be handled.
 850			 *
 851			 * ie. WRITE faults appear first, thus any handling of
 852			 * pending READ faults will already be satisfied.
 853			 * But if a large page is mapped, make sure subsequent
 854			 * fault addresses have sufficient access permission.
 855			 */
 856			if (buffer->fault[fn]->svmm != svmm ||
 857			    buffer->fault[fn]->addr >= limit ||
 858			    (buffer->fault[fi]->access == FAULT_ACCESS_READ &&
 859			     !(args.phys[0] & NVIF_VMM_PFNMAP_V0_V)) ||
 860			    (buffer->fault[fi]->access != FAULT_ACCESS_READ &&
 861			     buffer->fault[fi]->access != FAULT_ACCESS_PREFETCH &&
 862			     !(args.phys[0] & NVIF_VMM_PFNMAP_V0_W)) ||
 863			    (buffer->fault[fi]->access != FAULT_ACCESS_READ &&
 864			     buffer->fault[fi]->access != FAULT_ACCESS_WRITE &&
 865			     buffer->fault[fi]->access != FAULT_ACCESS_PREFETCH &&
 866			     !(args.phys[0] & NVIF_VMM_PFNMAP_V0_A)))
 867				break;
 868		}
 869
 870		/* If handling failed completely, cancel all faults. */
 871		if (ret) {
 872			while (fi < fn) {
 873				struct nouveau_svm_fault *fault =
 874					buffer->fault[fi++];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 876				nouveau_svm_fault_cancel_fault(svm, fault);
 
 877			}
 878		} else
 879			replay++;
 
 880	}
 881
 882	/* Issue fault replay to the GPU. */
 883	if (replay)
 884		nouveau_svm_fault_replay(svm);
 885}
 886
 887static int
 888nouveau_svm_event(struct nvif_event *event, void *argv, u32 argc)
 889{
 890	struct nouveau_svm_fault_buffer *buffer = container_of(event, typeof(*buffer), notify);
 891
 892	schedule_work(&buffer->work);
 893	return NVIF_EVENT_KEEP;
 894}
 895
 896static struct nouveau_pfnmap_args *
 897nouveau_pfns_to_args(void *pfns)
 898{
 899	return container_of(pfns, struct nouveau_pfnmap_args, p.phys);
 900}
 901
 902u64 *
 903nouveau_pfns_alloc(unsigned long npages)
 904{
 905	struct nouveau_pfnmap_args *args;
 906
 907	args = kzalloc(struct_size(args, p.phys, npages), GFP_KERNEL);
 908	if (!args)
 909		return NULL;
 910
 911	args->i.type = NVIF_IOCTL_V0_MTHD;
 912	args->m.method = NVIF_VMM_V0_PFNMAP;
 913	args->p.page = PAGE_SHIFT;
 914
 915	return args->p.phys;
 916}
 917
 918void
 919nouveau_pfns_free(u64 *pfns)
 920{
 921	struct nouveau_pfnmap_args *args = nouveau_pfns_to_args(pfns);
 922
 923	kfree(args);
 924}
 925
 926void
 927nouveau_pfns_map(struct nouveau_svmm *svmm, struct mm_struct *mm,
 928		 unsigned long addr, u64 *pfns, unsigned long npages)
 929{
 930	struct nouveau_pfnmap_args *args = nouveau_pfns_to_args(pfns);
 931
 932	args->p.addr = addr;
 933	args->p.size = npages << PAGE_SHIFT;
 934
 935	mutex_lock(&svmm->mutex);
 936
 937	nvif_object_ioctl(&svmm->vmm->vmm.object, args,
 938			  struct_size(args, p.phys, npages), NULL);
 939
 940	mutex_unlock(&svmm->mutex);
 941}
 942
 943static void
 944nouveau_svm_fault_buffer_fini(struct nouveau_svm *svm, int id)
 945{
 946	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
 947
 948	nvif_event_block(&buffer->notify);
 949	flush_work(&buffer->work);
 950}
 951
 952static int
 953nouveau_svm_fault_buffer_init(struct nouveau_svm *svm, int id)
 954{
 955	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
 956	struct nvif_object *device = &svm->drm->client.device.object;
 957
 958	buffer->get = nvif_rd32(device, buffer->getaddr);
 959	buffer->put = nvif_rd32(device, buffer->putaddr);
 960	SVM_DBG(svm, "get %08x put %08x (init)", buffer->get, buffer->put);
 961
 962	return nvif_event_allow(&buffer->notify);
 963}
 964
 965static void
 966nouveau_svm_fault_buffer_dtor(struct nouveau_svm *svm, int id)
 967{
 968	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
 969	int i;
 970
 971	if (!nvif_object_constructed(&buffer->object))
 972		return;
 973
 974	nouveau_svm_fault_buffer_fini(svm, id);
 975
 976	if (buffer->fault) {
 977		for (i = 0; buffer->fault[i] && i < buffer->entries; i++)
 978			kfree(buffer->fault[i]);
 979		kvfree(buffer->fault);
 980	}
 981
 982	nvif_event_dtor(&buffer->notify);
 983	nvif_object_dtor(&buffer->object);
 
 
 984}
 985
 986static int
 987nouveau_svm_fault_buffer_ctor(struct nouveau_svm *svm, s32 oclass, int id)
 988{
 989	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
 990	struct nouveau_drm *drm = svm->drm;
 991	struct nvif_object *device = &drm->client.device.object;
 992	struct nvif_clb069_v0 args = {};
 993	int ret;
 994
 995	buffer->id = id;
 996
 997	ret = nvif_object_ctor(device, "svmFaultBuffer", 0, oclass, &args,
 998			       sizeof(args), &buffer->object);
 999	if (ret < 0) {
1000		SVM_ERR(svm, "Fault buffer allocation failed: %d", ret);
1001		return ret;
1002	}
1003
1004	nvif_object_map(&buffer->object, NULL, 0);
1005	buffer->entries = args.entries;
1006	buffer->getaddr = args.get;
1007	buffer->putaddr = args.put;
1008	INIT_WORK(&buffer->work, nouveau_svm_fault);
1009
1010	ret = nvif_event_ctor(&buffer->object, "svmFault", id, nouveau_svm_event, true, NULL, 0,
1011			      &buffer->notify);
 
1012	if (ret)
1013		return ret;
1014
1015	buffer->fault = kvcalloc(buffer->entries, sizeof(*buffer->fault), GFP_KERNEL);
1016	if (!buffer->fault)
1017		return -ENOMEM;
1018
1019	return nouveau_svm_fault_buffer_init(svm, id);
1020}
1021
1022void
1023nouveau_svm_resume(struct nouveau_drm *drm)
1024{
1025	struct nouveau_svm *svm = drm->svm;
1026	if (svm)
1027		nouveau_svm_fault_buffer_init(svm, 0);
1028}
1029
1030void
1031nouveau_svm_suspend(struct nouveau_drm *drm)
1032{
1033	struct nouveau_svm *svm = drm->svm;
1034	if (svm)
1035		nouveau_svm_fault_buffer_fini(svm, 0);
1036}
1037
1038void
1039nouveau_svm_fini(struct nouveau_drm *drm)
1040{
1041	struct nouveau_svm *svm = drm->svm;
1042	if (svm) {
1043		nouveau_svm_fault_buffer_dtor(svm, 0);
1044		kfree(drm->svm);
1045		drm->svm = NULL;
1046	}
1047}
1048
1049void
1050nouveau_svm_init(struct nouveau_drm *drm)
1051{
1052	static const struct nvif_mclass buffers[] = {
1053		{   VOLTA_FAULT_BUFFER_A, 0 },
1054		{ MAXWELL_FAULT_BUFFER_A, 0 },
1055		{}
1056	};
1057	struct nouveau_svm *svm;
1058	int ret;
1059
1060	/* Disable on Volta and newer until channel recovery is fixed,
1061	 * otherwise clients will have a trivial way to trash the GPU
1062	 * for everyone.
1063	 */
1064	if (drm->client.device.info.family > NV_DEVICE_INFO_V0_PASCAL)
1065		return;
1066
1067	drm->svm = svm = kzalloc(struct_size(drm->svm, buffer, 1), GFP_KERNEL);
1068	if (!drm->svm)
1069		return;
1070
1071	drm->svm->drm = drm;
1072	mutex_init(&drm->svm->mutex);
1073	INIT_LIST_HEAD(&drm->svm->inst);
1074
1075	ret = nvif_mclass(&drm->client.device.object, buffers);
1076	if (ret < 0) {
1077		SVM_DBG(svm, "No supported fault buffer class");
1078		nouveau_svm_fini(drm);
1079		return;
1080	}
1081
1082	ret = nouveau_svm_fault_buffer_ctor(svm, buffers[ret].oclass, 0);
1083	if (ret) {
1084		nouveau_svm_fini(drm);
1085		return;
1086	}
1087
1088	SVM_DBG(svm, "Initialised");
1089}
v5.4
  1/*
  2 * Copyright 2018 Red Hat Inc.
  3 *
  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:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 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.
 21 */
 22#include "nouveau_svm.h"
 23#include "nouveau_drv.h"
 24#include "nouveau_chan.h"
 25#include "nouveau_dmem.h"
 26
 27#include <nvif/notify.h>
 28#include <nvif/object.h>
 29#include <nvif/vmm.h>
 30
 31#include <nvif/class.h>
 32#include <nvif/clb069.h>
 33#include <nvif/ifc00d.h>
 34
 35#include <linux/sched/mm.h>
 36#include <linux/sort.h>
 37#include <linux/hmm.h>
 
 
 38
 39struct nouveau_svm {
 40	struct nouveau_drm *drm;
 41	struct mutex mutex;
 42	struct list_head inst;
 43
 44	struct nouveau_svm_fault_buffer {
 45		int id;
 46		struct nvif_object object;
 47		u32 entries;
 48		u32 getaddr;
 49		u32 putaddr;
 50		u32 get;
 51		u32 put;
 52		struct nvif_notify notify;
 
 53
 54		struct nouveau_svm_fault {
 55			u64 inst;
 56			u64 addr;
 57			u64 time;
 58			u32 engine;
 59			u8  gpc;
 60			u8  hub;
 61			u8  access;
 62			u8  client;
 63			u8  fault;
 64			struct nouveau_svmm *svmm;
 65		} **fault;
 66		int fault_nr;
 67	} buffer[1];
 68};
 69
 
 
 
 
 
 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)
 72
 
 
 
 
 
 
 73struct nouveau_ivmm {
 74	struct nouveau_svmm *svmm;
 75	u64 inst;
 76	struct list_head head;
 77};
 78
 79static struct nouveau_ivmm *
 80nouveau_ivmm_find(struct nouveau_svm *svm, u64 inst)
 81{
 82	struct nouveau_ivmm *ivmm;
 83	list_for_each_entry(ivmm, &svm->inst, head) {
 84		if (ivmm->inst == inst)
 85			return ivmm;
 86	}
 87	return NULL;
 88}
 89
 90struct nouveau_svmm {
 91	struct nouveau_vmm *vmm;
 92	struct {
 93		unsigned long start;
 94		unsigned long limit;
 95	} unmanaged;
 96
 97	struct mutex mutex;
 98
 99	struct mm_struct *mm;
100	struct hmm_mirror mirror;
101};
102
103#define SVMM_DBG(s,f,a...)                                                     \
104	NV_DEBUG((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
105#define SVMM_ERR(s,f,a...)                                                     \
106	NV_WARN((s)->vmm->cli->drm, "svm-%p: "f"\n", (s), ##a)
107
108int
109nouveau_svmm_bind(struct drm_device *dev, void *data,
110		  struct drm_file *file_priv)
111{
112	struct nouveau_cli *cli = nouveau_cli(file_priv);
113	struct drm_nouveau_svm_bind *args = data;
114	unsigned target, cmd, priority;
115	unsigned long addr, end, size;
116	struct mm_struct *mm;
117
118	args->va_start &= PAGE_MASK;
119	args->va_end &= PAGE_MASK;
120
121	/* Sanity check arguments */
122	if (args->reserved0 || args->reserved1)
123		return -EINVAL;
124	if (args->header & (~NOUVEAU_SVM_BIND_VALID_MASK))
125		return -EINVAL;
126	if (args->va_start >= args->va_end)
127		return -EINVAL;
128	if (!args->npages)
129		return -EINVAL;
130
131	cmd = args->header >> NOUVEAU_SVM_BIND_COMMAND_SHIFT;
132	cmd &= NOUVEAU_SVM_BIND_COMMAND_MASK;
133	switch (cmd) {
134	case NOUVEAU_SVM_BIND_COMMAND__MIGRATE:
135		break;
136	default:
137		return -EINVAL;
138	}
139
140	priority = args->header >> NOUVEAU_SVM_BIND_PRIORITY_SHIFT;
141	priority &= NOUVEAU_SVM_BIND_PRIORITY_MASK;
142
143	/* FIXME support CPU target ie all target value < GPU_VRAM */
144	target = args->header >> NOUVEAU_SVM_BIND_TARGET_SHIFT;
145	target &= NOUVEAU_SVM_BIND_TARGET_MASK;
146	switch (target) {
147	case NOUVEAU_SVM_BIND_TARGET__GPU_VRAM:
148		break;
149	default:
150		return -EINVAL;
151	}
152
153	/*
154	 * FIXME: For now refuse non 0 stride, we need to change the migrate
155	 * kernel function to handle stride to avoid to create a mess within
156	 * each device driver.
157	 */
158	if (args->stride)
159		return -EINVAL;
160
161	size = ((unsigned long)args->npages) << PAGE_SHIFT;
162	if ((args->va_start + size) <= args->va_start)
163		return -EINVAL;
164	if ((args->va_start + size) > args->va_end)
165		return -EINVAL;
166
167	/*
168	 * Ok we are ask to do something sane, for now we only support migrate
169	 * commands but we will add things like memory policy (what to do on
170	 * page fault) and maybe some other commands.
171	 */
172
173	mm = get_task_mm(current);
174	down_read(&mm->mmap_sem);
 
 
 
175
176	for (addr = args->va_start, end = args->va_start + size; addr < end;) {
 
 
 
 
 
 
177		struct vm_area_struct *vma;
178		unsigned long next;
179
180		vma = find_vma_intersection(mm, addr, end);
181		if (!vma)
182			break;
183
 
184		next = min(vma->vm_end, end);
185		/* This is a best effort so we ignore errors */
186		nouveau_dmem_migrate_vma(cli->drm, vma, addr, next);
 
187		addr = next;
188	}
189
190	/*
191	 * FIXME Return the number of page we have migrated, again we need to
192	 * update the migrate API to return that information so that we can
193	 * report it to user space.
194	 */
195	args->result = 0;
196
197	up_read(&mm->mmap_sem);
198	mmput(mm);
199
200	return 0;
201}
202
203/* Unlink channel instance from SVMM. */
204void
205nouveau_svmm_part(struct nouveau_svmm *svmm, u64 inst)
206{
207	struct nouveau_ivmm *ivmm;
208	if (svmm) {
209		mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
210		ivmm = nouveau_ivmm_find(svmm->vmm->cli->drm->svm, inst);
211		if (ivmm) {
212			list_del(&ivmm->head);
213			kfree(ivmm);
214		}
215		mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
216	}
217}
218
219/* Link channel instance to SVMM. */
220int
221nouveau_svmm_join(struct nouveau_svmm *svmm, u64 inst)
222{
223	struct nouveau_ivmm *ivmm;
224	if (svmm) {
225		if (!(ivmm = kmalloc(sizeof(*ivmm), GFP_KERNEL)))
226			return -ENOMEM;
227		ivmm->svmm = svmm;
228		ivmm->inst = inst;
229
230		mutex_lock(&svmm->vmm->cli->drm->svm->mutex);
231		list_add(&ivmm->head, &svmm->vmm->cli->drm->svm->inst);
232		mutex_unlock(&svmm->vmm->cli->drm->svm->mutex);
233	}
234	return 0;
235}
236
237/* Invalidate SVMM address-range on GPU. */
238static void
239nouveau_svmm_invalidate(struct nouveau_svmm *svmm, u64 start, u64 limit)
240{
241	if (limit > start) {
242		bool super = svmm->vmm->vmm.object.client->super;
243		svmm->vmm->vmm.object.client->super = true;
244		nvif_object_mthd(&svmm->vmm->vmm.object, NVIF_VMM_V0_PFNCLR,
245				 &(struct nvif_vmm_pfnclr_v0) {
246					.addr = start,
247					.size = limit - start,
248				 }, sizeof(struct nvif_vmm_pfnclr_v0));
249		svmm->vmm->vmm.object.client->super = super;
250	}
251}
252
253static int
254nouveau_svmm_sync_cpu_device_pagetables(struct hmm_mirror *mirror,
255					const struct mmu_notifier_range *update)
256{
257	struct nouveau_svmm *svmm = container_of(mirror, typeof(*svmm), mirror);
 
258	unsigned long start = update->start;
259	unsigned long limit = update->end;
260
261	if (!mmu_notifier_range_blockable(update))
262		return -EAGAIN;
263
264	SVMM_DBG(svmm, "invalidate %016lx-%016lx", start, limit);
265
266	mutex_lock(&svmm->mutex);
 
 
 
 
 
 
 
 
 
 
 
267	if (limit > svmm->unmanaged.start && start < svmm->unmanaged.limit) {
268		if (start < svmm->unmanaged.start) {
269			nouveau_svmm_invalidate(svmm, start,
270						svmm->unmanaged.limit);
271		}
272		start = svmm->unmanaged.limit;
273	}
274
275	nouveau_svmm_invalidate(svmm, start, limit);
 
 
276	mutex_unlock(&svmm->mutex);
277	return 0;
278}
279
280static void
281nouveau_svmm_release(struct hmm_mirror *mirror)
282{
 
283}
284
285static const struct hmm_mirror_ops
286nouveau_svmm = {
287	.sync_cpu_device_pagetables = nouveau_svmm_sync_cpu_device_pagetables,
288	.release = nouveau_svmm_release,
289};
290
291void
292nouveau_svmm_fini(struct nouveau_svmm **psvmm)
293{
294	struct nouveau_svmm *svmm = *psvmm;
295	if (svmm) {
296		hmm_mirror_unregister(&svmm->mirror);
297		kfree(*psvmm);
 
 
298		*psvmm = NULL;
299	}
300}
301
302int
303nouveau_svmm_init(struct drm_device *dev, void *data,
304		  struct drm_file *file_priv)
305{
306	struct nouveau_cli *cli = nouveau_cli(file_priv);
307	struct nouveau_svmm *svmm;
308	struct drm_nouveau_svm_init *args = data;
309	int ret;
310
 
 
 
 
311	/* Allocate tracking for SVM-enabled VMM. */
312	if (!(svmm = kzalloc(sizeof(*svmm), GFP_KERNEL)))
313		return -ENOMEM;
314	svmm->vmm = &cli->svm;
315	svmm->unmanaged.start = args->unmanaged_addr;
316	svmm->unmanaged.limit = args->unmanaged_addr + args->unmanaged_size;
317	mutex_init(&svmm->mutex);
318
319	/* Check that SVM isn't already enabled for the client. */
320	mutex_lock(&cli->mutex);
321	if (cli->svm.cli) {
322		ret = -EBUSY;
323		goto done;
324	}
325
326	/* Allocate a new GPU VMM that can support SVM (managed by the
327	 * client, with replayable faults enabled).
328	 *
329	 * All future channel/memory allocations will make use of this
330	 * VMM instead of the standard one.
331	 */
332	ret = nvif_vmm_init(&cli->mmu, cli->vmm.vmm.object.oclass, true,
 
333			    args->unmanaged_addr, args->unmanaged_size,
334			    &(struct gp100_vmm_v0) {
335				.fault_replay = true,
336			    }, sizeof(struct gp100_vmm_v0), &cli->svm.vmm);
337	if (ret)
338		goto done;
339
340	/* Enable HMM mirroring of CPU address-space to VMM. */
341	svmm->mm = get_task_mm(current);
342	down_write(&svmm->mm->mmap_sem);
343	svmm->mirror.ops = &nouveau_svmm;
344	ret = hmm_mirror_register(&svmm->mirror, svmm->mm);
345	if (ret == 0) {
346		cli->svm.svmm = svmm;
347		cli->svm.cli = cli;
348	}
349	up_write(&svmm->mm->mmap_sem);
350	mmput(svmm->mm);
 
351
352done:
353	if (ret)
354		nouveau_svmm_fini(&svmm);
355	mutex_unlock(&cli->mutex);
 
356	return ret;
357}
358
359static const u64
360nouveau_svm_pfn_flags[HMM_PFN_FLAG_MAX] = {
361	[HMM_PFN_VALID         ] = NVIF_VMM_PFNMAP_V0_V,
362	[HMM_PFN_WRITE         ] = NVIF_VMM_PFNMAP_V0_W,
363	[HMM_PFN_DEVICE_PRIVATE] = NVIF_VMM_PFNMAP_V0_VRAM,
364};
365
366static const u64
367nouveau_svm_pfn_values[HMM_PFN_VALUE_MAX] = {
368	[HMM_PFN_ERROR  ] = ~NVIF_VMM_PFNMAP_V0_V,
369	[HMM_PFN_NONE   ] =  NVIF_VMM_PFNMAP_V0_NONE,
370	[HMM_PFN_SPECIAL] = ~NVIF_VMM_PFNMAP_V0_V,
371};
372
373/* Issue fault replay for GPU to retry accesses that faulted previously. */
374static void
375nouveau_svm_fault_replay(struct nouveau_svm *svm)
376{
377	SVM_DBG(svm, "replay");
378	WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
379				 GP100_VMM_VN_FAULT_REPLAY,
380				 &(struct gp100_vmm_fault_replay_vn) {},
381				 sizeof(struct gp100_vmm_fault_replay_vn)));
382}
383
384/* Cancel a replayable fault that could not be handled.
385 *
386 * Cancelling the fault will trigger recovery to reset the engine
387 * and kill the offending channel (ie. GPU SIGSEGV).
388 */
389static void
390nouveau_svm_fault_cancel(struct nouveau_svm *svm,
391			 u64 inst, u8 hub, u8 gpc, u8 client)
392{
393	SVM_DBG(svm, "cancel %016llx %d %02x %02x", inst, hub, gpc, client);
394	WARN_ON(nvif_object_mthd(&svm->drm->client.vmm.vmm.object,
395				 GP100_VMM_VN_FAULT_CANCEL,
396				 &(struct gp100_vmm_fault_cancel_v0) {
397					.hub = hub,
398					.gpc = gpc,
399					.client = client,
400					.inst = inst,
401				 }, sizeof(struct gp100_vmm_fault_cancel_v0)));
402}
403
404static void
405nouveau_svm_fault_cancel_fault(struct nouveau_svm *svm,
406			       struct nouveau_svm_fault *fault)
407{
408	nouveau_svm_fault_cancel(svm, fault->inst,
409				      fault->hub,
410				      fault->gpc,
411				      fault->client);
412}
413
414static int
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415nouveau_svm_fault_cmp(const void *a, const void *b)
416{
417	const struct nouveau_svm_fault *fa = *(struct nouveau_svm_fault **)a;
418	const struct nouveau_svm_fault *fb = *(struct nouveau_svm_fault **)b;
419	int ret;
420	if ((ret = (s64)fa->inst - fb->inst))
421		return ret;
422	if ((ret = (s64)fa->addr - fb->addr))
423		return ret;
424	/*XXX: atomic? */
425	return (fa->access == 0 || fa->access == 3) -
426	       (fb->access == 0 || fb->access == 3);
427}
428
429static void
430nouveau_svm_fault_cache(struct nouveau_svm *svm,
431			struct nouveau_svm_fault_buffer *buffer, u32 offset)
432{
433	struct nvif_object *memory = &buffer->object;
434	const u32 instlo = nvif_rd32(memory, offset + 0x00);
435	const u32 insthi = nvif_rd32(memory, offset + 0x04);
436	const u32 addrlo = nvif_rd32(memory, offset + 0x08);
437	const u32 addrhi = nvif_rd32(memory, offset + 0x0c);
438	const u32 timelo = nvif_rd32(memory, offset + 0x10);
439	const u32 timehi = nvif_rd32(memory, offset + 0x14);
440	const u32 engine = nvif_rd32(memory, offset + 0x18);
441	const u32   info = nvif_rd32(memory, offset + 0x1c);
442	const u64   inst = (u64)insthi << 32 | instlo;
443	const u8     gpc = (info & 0x1f000000) >> 24;
444	const u8     hub = (info & 0x00100000) >> 20;
445	const u8  client = (info & 0x00007f00) >> 8;
446	struct nouveau_svm_fault *fault;
447
448	//XXX: i think we're supposed to spin waiting */
449	if (WARN_ON(!(info & 0x80000000)))
450		return;
451
452	nvif_mask(memory, offset + 0x1c, 0x80000000, 0x00000000);
453
454	if (!buffer->fault[buffer->fault_nr]) {
455		fault = kmalloc(sizeof(*fault), GFP_KERNEL);
456		if (WARN_ON(!fault)) {
457			nouveau_svm_fault_cancel(svm, inst, hub, gpc, client);
458			return;
459		}
460		buffer->fault[buffer->fault_nr] = fault;
461	}
462
463	fault = buffer->fault[buffer->fault_nr++];
464	fault->inst   = inst;
465	fault->addr   = (u64)addrhi << 32 | addrlo;
466	fault->time   = (u64)timehi << 32 | timelo;
467	fault->engine = engine;
468	fault->gpc    = gpc;
469	fault->hub    = hub;
470	fault->access = (info & 0x000f0000) >> 16;
471	fault->client = client;
472	fault->fault  = (info & 0x0000001f);
473
474	SVM_DBG(svm, "fault %016llx %016llx %02x",
475		fault->inst, fault->addr, fault->access);
476}
477
478static inline bool
479nouveau_range_done(struct hmm_range *range)
480{
481	bool ret = hmm_range_valid(range);
 
 
 
 
 
 
 
 
 
 
 
482
483	hmm_range_unregister(range);
484	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
485}
486
487static int
488nouveau_range_fault(struct nouveau_svmm *svmm, struct hmm_range *range)
 
 
 
 
 
489{
490	long ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
491
492	range->default_flags = 0;
493	range->pfn_flags_mask = -1UL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
495	ret = hmm_range_register(range, &svmm->mirror);
496	if (ret) {
497		up_read(&svmm->mm->mmap_sem);
498		return (int)ret;
499	}
500
501	if (!hmm_range_wait_until_valid(range, HMM_RANGE_DEFAULT_TIMEOUT)) {
502		up_read(&svmm->mm->mmap_sem);
503		return -EBUSY;
504	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505
506	ret = hmm_range_fault(range, 0);
507	if (ret <= 0) {
508		if (ret == 0)
509			ret = -EBUSY;
510		up_read(&svmm->mm->mmap_sem);
511		hmm_range_unregister(range);
512		return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513	}
514	return 0;
 
 
 
 
 
 
 
 
 
515}
516
517static int
518nouveau_svm_fault(struct nvif_notify *notify)
519{
520	struct nouveau_svm_fault_buffer *buffer =
521		container_of(notify, typeof(*buffer), notify);
522	struct nouveau_svm *svm =
523		container_of(buffer, typeof(*svm), buffer[buffer->id]);
524	struct nvif_object *device = &svm->drm->client.device.object;
525	struct nouveau_svmm *svmm;
526	struct {
527		struct {
528			struct nvif_ioctl_v0 i;
529			struct nvif_ioctl_mthd_v0 m;
530			struct nvif_vmm_pfnmap_v0 p;
531		} i;
532		u64 phys[16];
533	} args;
534	struct hmm_range range;
535	struct vm_area_struct *vma;
536	u64 inst, start, limit;
537	int fi, fn, pi, fill;
538	int replay = 0, ret;
539
540	/* Parse available fault buffer entries into a cache, and update
541	 * the GET pointer so HW can reuse the entries.
542	 */
543	SVM_DBG(svm, "fault handler");
544	if (buffer->get == buffer->put) {
545		buffer->put = nvif_rd32(device, buffer->putaddr);
546		buffer->get = nvif_rd32(device, buffer->getaddr);
547		if (buffer->get == buffer->put)
548			return NVIF_NOTIFY_KEEP;
549	}
550	buffer->fault_nr = 0;
551
552	SVM_DBG(svm, "get %08x put %08x", buffer->get, buffer->put);
553	while (buffer->get != buffer->put) {
554		nouveau_svm_fault_cache(svm, buffer, buffer->get * 0x20);
555		if (++buffer->get == buffer->entries)
556			buffer->get = 0;
557	}
558	nvif_wr32(device, buffer->getaddr, buffer->get);
559	SVM_DBG(svm, "%d fault(s) pending", buffer->fault_nr);
560
561	/* Sort parsed faults by instance pointer to prevent unnecessary
562	 * instance to SVMM translations, followed by address and access
563	 * type to reduce the amount of work when handling the faults.
564	 */
565	sort(buffer->fault, buffer->fault_nr, sizeof(*buffer->fault),
566	     nouveau_svm_fault_cmp, NULL);
567
568	/* Lookup SVMM structure for each unique instance pointer. */
569	mutex_lock(&svm->mutex);
570	for (fi = 0, svmm = NULL; fi < buffer->fault_nr; fi++) {
571		if (!svmm || buffer->fault[fi]->inst != inst) {
572			struct nouveau_ivmm *ivmm =
573				nouveau_ivmm_find(svm, buffer->fault[fi]->inst);
574			svmm = ivmm ? ivmm->svmm : NULL;
575			inst = buffer->fault[fi]->inst;
576			SVM_DBG(svm, "inst %016llx -> svm-%p", inst, svmm);
577		}
578		buffer->fault[fi]->svmm = svmm;
579	}
580	mutex_unlock(&svm->mutex);
581
582	/* Process list of faults. */
583	args.i.i.version = 0;
584	args.i.i.type = NVIF_IOCTL_V0_MTHD;
585	args.i.m.version = 0;
586	args.i.m.method = NVIF_VMM_V0_PFNMAP;
587	args.i.p.version = 0;
588
589	for (fi = 0; fn = fi + 1, fi < buffer->fault_nr; fi = fn) {
 
 
 
590		/* Cancel any faults from non-SVM channels. */
591		if (!(svmm = buffer->fault[fi]->svmm)) {
592			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
593			continue;
594		}
595		SVMM_DBG(svmm, "addr %016llx", buffer->fault[fi]->addr);
596
597		/* We try and group handling of faults within a small
598		 * window into a single update.
599		 */
600		start = buffer->fault[fi]->addr;
601		limit = start + (ARRAY_SIZE(args.phys) << PAGE_SHIFT);
602		if (start < svmm->unmanaged.limit)
603			limit = min_t(u64, limit, svmm->unmanaged.start);
604		else
605		if (limit > svmm->unmanaged.start)
606			start = max_t(u64, start, svmm->unmanaged.limit);
607		SVMM_DBG(svmm, "wndw %016llx-%016llx", start, limit);
608
609		/* Intersect fault window with the CPU VMA, cancelling
610		 * the fault if the address is invalid.
 
 
611		 */
612		down_read(&svmm->mm->mmap_sem);
613		vma = find_vma_intersection(svmm->mm, start, limit);
614		if (!vma) {
615			SVMM_ERR(svmm, "wndw %016llx-%016llx", start, limit);
616			up_read(&svmm->mm->mmap_sem);
617			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
618			continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
619		}
620		start = max_t(u64, start, vma->vm_start);
621		limit = min_t(u64, limit, vma->vm_end);
622		SVMM_DBG(svmm, "wndw %016llx-%016llx", start, limit);
623
624		if (buffer->fault[fi]->addr != start) {
625			SVMM_ERR(svmm, "addr %016llx", buffer->fault[fi]->addr);
626			up_read(&svmm->mm->mmap_sem);
627			nouveau_svm_fault_cancel_fault(svm, buffer->fault[fi]);
628			continue;
629		}
630
631		/* Prepare the GPU-side update of all pages within the
632		 * fault window, determining required pages and access
633		 * permissions based on pending faults.
634		 */
635		args.i.p.page = PAGE_SHIFT;
636		args.i.p.addr = start;
637		for (fn = fi, pi = 0;;) {
638			/* Determine required permissions based on GPU fault
639			 * access flags.
640			 *XXX: atomic?
641			 */
642			if (buffer->fault[fn]->access != 0 /* READ. */ &&
643			    buffer->fault[fn]->access != 3 /* PREFETCH. */) {
644				args.phys[pi++] = NVIF_VMM_PFNMAP_V0_V |
645						  NVIF_VMM_PFNMAP_V0_W;
646			} else {
647				args.phys[pi++] = NVIF_VMM_PFNMAP_V0_V;
648			}
649			args.i.p.size = pi << PAGE_SHIFT;
650
 
 
651			/* It's okay to skip over duplicate addresses from the
652			 * same SVMM as faults are ordered by access type such
653			 * that only the first one needs to be handled.
654			 *
655			 * ie. WRITE faults appear first, thus any handling of
656			 * pending READ faults will already be satisfied.
 
 
657			 */
658			while (++fn < buffer->fault_nr &&
659			       buffer->fault[fn]->svmm == svmm &&
660			       buffer->fault[fn    ]->addr ==
661			       buffer->fault[fn - 1]->addr);
662
663			/* If the next fault is outside the window, or all GPU
664			 * faults have been dealt with, we're done here.
665			 */
666			if (fn >= buffer->fault_nr ||
667			    buffer->fault[fn]->svmm != svmm ||
668			    buffer->fault[fn]->addr >= limit)
669				break;
 
670
671			/* Fill in the gap between this fault and the next. */
672			fill = (buffer->fault[fn    ]->addr -
673				buffer->fault[fn - 1]->addr) >> PAGE_SHIFT;
674			while (--fill)
675				args.phys[pi++] = NVIF_VMM_PFNMAP_V0_NONE;
676		}
677
678		SVMM_DBG(svmm, "wndw %016llx-%016llx covering %d fault(s)",
679			 args.i.p.addr,
680			 args.i.p.addr + args.i.p.size, fn - fi);
681
682		/* Have HMM fault pages within the fault window to the GPU. */
683		range.start = args.i.p.addr;
684		range.end = args.i.p.addr + args.i.p.size;
685		range.pfns = args.phys;
686		range.flags = nouveau_svm_pfn_flags;
687		range.values = nouveau_svm_pfn_values;
688		range.pfn_shift = NVIF_VMM_PFNMAP_V0_ADDR_SHIFT;
689again:
690		ret = nouveau_range_fault(svmm, &range);
691		if (ret == 0) {
692			mutex_lock(&svmm->mutex);
693			if (!nouveau_range_done(&range)) {
694				mutex_unlock(&svmm->mutex);
695				goto again;
696			}
697
698			nouveau_dmem_convert_pfn(svm->drm, &range);
699
700			svmm->vmm->vmm.object.client->super = true;
701			ret = nvif_object_ioctl(&svmm->vmm->vmm.object,
702						&args, sizeof(args.i) +
703						pi * sizeof(args.phys[0]),
704						NULL);
705			svmm->vmm->vmm.object.client->super = false;
706			mutex_unlock(&svmm->mutex);
707			up_read(&svmm->mm->mmap_sem);
708		}
709
710		/* Cancel any faults in the window whose pages didn't manage
711		 * to keep their valid bit, or stay writeable when required.
712		 *
713		 * If handling failed completely, cancel all faults.
714		 */
715		while (fi < fn) {
716			struct nouveau_svm_fault *fault = buffer->fault[fi++];
717			pi = (fault->addr - range.start) >> PAGE_SHIFT;
718			if (ret ||
719			     !(range.pfns[pi] & NVIF_VMM_PFNMAP_V0_V) ||
720			    (!(range.pfns[pi] & NVIF_VMM_PFNMAP_V0_W) &&
721			     fault->access != 0 && fault->access != 3)) {
722				nouveau_svm_fault_cancel_fault(svm, fault);
723				continue;
724			}
 
725			replay++;
726		}
727	}
728
729	/* Issue fault replay to the GPU. */
730	if (replay)
731		nouveau_svm_fault_replay(svm);
732	return NVIF_NOTIFY_KEEP;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
733}
734
735static void
736nouveau_svm_fault_buffer_fini(struct nouveau_svm *svm, int id)
737{
738	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
739	nvif_notify_put(&buffer->notify);
 
 
740}
741
742static int
743nouveau_svm_fault_buffer_init(struct nouveau_svm *svm, int id)
744{
745	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
746	struct nvif_object *device = &svm->drm->client.device.object;
 
747	buffer->get = nvif_rd32(device, buffer->getaddr);
748	buffer->put = nvif_rd32(device, buffer->putaddr);
749	SVM_DBG(svm, "get %08x put %08x (init)", buffer->get, buffer->put);
750	return nvif_notify_get(&buffer->notify);
 
751}
752
753static void
754nouveau_svm_fault_buffer_dtor(struct nouveau_svm *svm, int id)
755{
756	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
757	int i;
758
 
 
 
 
 
759	if (buffer->fault) {
760		for (i = 0; buffer->fault[i] && i < buffer->entries; i++)
761			kfree(buffer->fault[i]);
762		kvfree(buffer->fault);
763	}
764
765	nouveau_svm_fault_buffer_fini(svm, id);
766
767	nvif_notify_fini(&buffer->notify);
768	nvif_object_fini(&buffer->object);
769}
770
771static int
772nouveau_svm_fault_buffer_ctor(struct nouveau_svm *svm, s32 oclass, int id)
773{
774	struct nouveau_svm_fault_buffer *buffer = &svm->buffer[id];
775	struct nouveau_drm *drm = svm->drm;
776	struct nvif_object *device = &drm->client.device.object;
777	struct nvif_clb069_v0 args = {};
778	int ret;
779
780	buffer->id = id;
781
782	ret = nvif_object_init(device, 0, oclass, &args, sizeof(args),
783			       &buffer->object);
784	if (ret < 0) {
785		SVM_ERR(svm, "Fault buffer allocation failed: %d", ret);
786		return ret;
787	}
788
789	nvif_object_map(&buffer->object, NULL, 0);
790	buffer->entries = args.entries;
791	buffer->getaddr = args.get;
792	buffer->putaddr = args.put;
 
793
794	ret = nvif_notify_init(&buffer->object, nouveau_svm_fault, true,
795			       NVB069_V0_NTFY_FAULT, NULL, 0, 0,
796			       &buffer->notify);
797	if (ret)
798		return ret;
799
800	buffer->fault = kvzalloc(sizeof(*buffer->fault) * buffer->entries, GFP_KERNEL);
801	if (!buffer->fault)
802		return -ENOMEM;
803
804	return nouveau_svm_fault_buffer_init(svm, id);
805}
806
807void
808nouveau_svm_resume(struct nouveau_drm *drm)
809{
810	struct nouveau_svm *svm = drm->svm;
811	if (svm)
812		nouveau_svm_fault_buffer_init(svm, 0);
813}
814
815void
816nouveau_svm_suspend(struct nouveau_drm *drm)
817{
818	struct nouveau_svm *svm = drm->svm;
819	if (svm)
820		nouveau_svm_fault_buffer_fini(svm, 0);
821}
822
823void
824nouveau_svm_fini(struct nouveau_drm *drm)
825{
826	struct nouveau_svm *svm = drm->svm;
827	if (svm) {
828		nouveau_svm_fault_buffer_dtor(svm, 0);
829		kfree(drm->svm);
830		drm->svm = NULL;
831	}
832}
833
834void
835nouveau_svm_init(struct nouveau_drm *drm)
836{
837	static const struct nvif_mclass buffers[] = {
838		{   VOLTA_FAULT_BUFFER_A, 0 },
839		{ MAXWELL_FAULT_BUFFER_A, 0 },
840		{}
841	};
842	struct nouveau_svm *svm;
843	int ret;
844
845	/* Disable on Volta and newer until channel recovery is fixed,
846	 * otherwise clients will have a trivial way to trash the GPU
847	 * for everyone.
848	 */
849	if (drm->client.device.info.family > NV_DEVICE_INFO_V0_PASCAL)
850		return;
851
852	if (!(drm->svm = svm = kzalloc(sizeof(*drm->svm), GFP_KERNEL)))
 
853		return;
854
855	drm->svm->drm = drm;
856	mutex_init(&drm->svm->mutex);
857	INIT_LIST_HEAD(&drm->svm->inst);
858
859	ret = nvif_mclass(&drm->client.device.object, buffers);
860	if (ret < 0) {
861		SVM_DBG(svm, "No supported fault buffer class");
862		nouveau_svm_fini(drm);
863		return;
864	}
865
866	ret = nouveau_svm_fault_buffer_ctor(svm, buffers[ret].oclass, 0);
867	if (ret) {
868		nouveau_svm_fini(drm);
869		return;
870	}
871
872	SVM_DBG(svm, "Initialised");
873}