1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2014-2016 Intel Corporation
5  */
6 
7 #include <linux/anon_inodes.h>
8 #include <linux/mman.h>
9 #include <linux/pfn_t.h>
10 #include <linux/sizes.h>
11 
12 #include "gt/intel_gt.h"
13 #include "gt/intel_gt_requests.h"
14 
15 #include "i915_drv.h"
16 #include "i915_gem_gtt.h"
17 #include "i915_gem_ioctls.h"
18 #include "i915_gem_object.h"
19 #include "i915_gem_mman.h"
20 #include "i915_trace.h"
21 #include "i915_user_extensions.h"
22 #include "i915_vma.h"
23 
24 static inline bool
25 __vma_matches(struct vm_area_struct *vma, struct file *filp,
26 	      unsigned long addr, unsigned long size)
27 {
28 	if (vma->vm_file != filp)
29 		return false;
30 
31 	return vma->vm_start == addr &&
32 	       (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size);
33 }
34 
35 /**
36  * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
37  *			 it is mapped to.
38  * @dev: drm device
39  * @data: ioctl data blob
40  * @file: drm file
41  *
42  * While the mapping holds a reference on the contents of the object, it doesn't
43  * imply a ref on the object itself.
44  *
45  * IMPORTANT:
46  *
47  * DRM driver writers who look a this function as an example for how to do GEM
48  * mmap support, please don't implement mmap support like here. The modern way
49  * to implement DRM mmap support is with an mmap offset ioctl (like
50  * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
51  * That way debug tooling like valgrind will understand what's going on, hiding
52  * the mmap call in a driver private ioctl will break that. The i915 driver only
53  * does cpu mmaps this way because we didn't know better.
54  */
55 int
56 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
57 		    struct drm_file *file)
58 {
59 	struct drm_i915_gem_mmap *args = data;
60 	struct drm_i915_gem_object *obj;
61 	unsigned long addr;
62 
63 	if (args->flags & ~(I915_MMAP_WC))
64 		return -EINVAL;
65 
66 	if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
67 		return -ENODEV;
68 
69 	obj = i915_gem_object_lookup(file, args->handle);
70 	if (!obj)
71 		return -ENOENT;
72 
73 	/* prime objects have no backing filp to GEM mmap
74 	 * pages from.
75 	 */
76 	if (!obj->base.filp) {
77 		addr = -ENXIO;
78 		goto err;
79 	}
80 
81 	if (range_overflows(args->offset, args->size, (u64)obj->base.size)) {
82 		addr = -EINVAL;
83 		goto err;
84 	}
85 
86 	addr = vm_mmap(obj->base.filp, 0, args->size,
87 		       PROT_READ | PROT_WRITE, MAP_SHARED,
88 		       args->offset);
89 	if (IS_ERR_VALUE(addr))
90 		goto err;
91 
92 	if (args->flags & I915_MMAP_WC) {
93 		struct mm_struct *mm = current->mm;
94 		struct vm_area_struct *vma;
95 
96 		if (mmap_write_lock_killable(mm)) {
97 			addr = -EINTR;
98 			goto err;
99 		}
100 		vma = find_vma(mm, addr);
101 		if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
102 			vma->vm_page_prot =
103 				pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
104 		else
105 			addr = -ENOMEM;
106 		mmap_write_unlock(mm);
107 		if (IS_ERR_VALUE(addr))
108 			goto err;
109 	}
110 	i915_gem_object_put(obj);
111 
112 	args->addr_ptr = (u64)addr;
113 	return 0;
114 
115 err:
116 	i915_gem_object_put(obj);
117 	return addr;
118 }
119 
120 static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
121 {
122 	return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
123 }
124 
125 /**
126  * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
127  *
128  * A history of the GTT mmap interface:
129  *
130  * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
131  *     aligned and suitable for fencing, and still fit into the available
132  *     mappable space left by the pinned display objects. A classic problem
133  *     we called the page-fault-of-doom where we would ping-pong between
134  *     two objects that could not fit inside the GTT and so the memcpy
135  *     would page one object in at the expense of the other between every
136  *     single byte.
137  *
138  * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
139  *     as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
140  *     object is too large for the available space (or simply too large
141  *     for the mappable aperture!), a view is created instead and faulted
142  *     into userspace. (This view is aligned and sized appropriately for
143  *     fenced access.)
144  *
145  * 2 - Recognise WC as a separate cache domain so that we can flush the
146  *     delayed writes via GTT before performing direct access via WC.
147  *
148  * 3 - Remove implicit set-domain(GTT) and synchronisation on initial
149  *     pagefault; swapin remains transparent.
150  *
151  * 4 - Support multiple fault handlers per object depending on object's
152  *     backing storage (a.k.a. MMAP_OFFSET).
153  *
154  * Restrictions:
155  *
156  *  * snoopable objects cannot be accessed via the GTT. It can cause machine
157  *    hangs on some architectures, corruption on others. An attempt to service
158  *    a GTT page fault from a snoopable object will generate a SIGBUS.
159  *
160  *  * the object must be able to fit into RAM (physical memory, though no
161  *    limited to the mappable aperture).
162  *
163  *
164  * Caveats:
165  *
166  *  * a new GTT page fault will synchronize rendering from the GPU and flush
167  *    all data to system memory. Subsequent access will not be synchronized.
168  *
169  *  * all mappings are revoked on runtime device suspend.
170  *
171  *  * there are only 8, 16 or 32 fence registers to share between all users
172  *    (older machines require fence register for display and blitter access
173  *    as well). Contention of the fence registers will cause the previous users
174  *    to be unmapped and any new access will generate new page faults.
175  *
176  *  * running out of memory while servicing a fault may generate a SIGBUS,
177  *    rather than the expected SIGSEGV.
178  */
179 int i915_gem_mmap_gtt_version(void)
180 {
181 	return 4;
182 }
183 
184 static inline struct i915_ggtt_view
185 compute_partial_view(const struct drm_i915_gem_object *obj,
186 		     pgoff_t page_offset,
187 		     unsigned int chunk)
188 {
189 	struct i915_ggtt_view view;
190 
191 	if (i915_gem_object_is_tiled(obj))
192 		chunk = roundup(chunk, tile_row_pages(obj));
193 
194 	view.type = I915_GGTT_VIEW_PARTIAL;
195 	view.partial.offset = rounddown(page_offset, chunk);
196 	view.partial.size =
197 		min_t(unsigned int, chunk,
198 		      (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
199 
200 	/* If the partial covers the entire object, just create a normal VMA. */
201 	if (chunk >= obj->base.size >> PAGE_SHIFT)
202 		view.type = I915_GGTT_VIEW_NORMAL;
203 
204 	return view;
205 }
206 
207 static vm_fault_t i915_error_to_vmf_fault(int err)
208 {
209 	switch (err) {
210 	default:
211 		WARN_ONCE(err, "unhandled error in %s: %i\n", __func__, err);
212 		fallthrough;
213 	case -EIO: /* shmemfs failure from swap device */
214 	case -EFAULT: /* purged object */
215 	case -ENODEV: /* bad object, how did you get here! */
216 	case -ENXIO: /* unable to access backing store (on device) */
217 		return VM_FAULT_SIGBUS;
218 
219 	case -ENOMEM: /* our allocation failure */
220 		return VM_FAULT_OOM;
221 
222 	case 0:
223 	case -EAGAIN:
224 	case -ENOSPC: /* transient failure to evict? */
225 	case -ERESTARTSYS:
226 	case -EINTR:
227 	case -EBUSY:
228 		/*
229 		 * EBUSY is ok: this just means that another thread
230 		 * already did the job.
231 		 */
232 		return VM_FAULT_NOPAGE;
233 	}
234 }
235 
236 static vm_fault_t vm_fault_cpu(struct vm_fault *vmf)
237 {
238 	struct vm_area_struct *area = vmf->vma;
239 	struct i915_mmap_offset *mmo = area->vm_private_data;
240 	struct drm_i915_gem_object *obj = mmo->obj;
241 	resource_size_t iomap;
242 	int err;
243 
244 	/* Sanity check that we allow writing into this object */
245 	if (unlikely(i915_gem_object_is_readonly(obj) &&
246 		     area->vm_flags & VM_WRITE))
247 		return VM_FAULT_SIGBUS;
248 
249 	if (i915_gem_object_lock_interruptible(obj, NULL))
250 		return VM_FAULT_NOPAGE;
251 
252 	err = i915_gem_object_pin_pages(obj);
253 	if (err)
254 		goto out;
255 
256 	iomap = -1;
257 	if (!i915_gem_object_has_struct_page(obj)) {
258 		iomap = obj->mm.region->iomap.base;
259 		iomap -= obj->mm.region->region.start;
260 	}
261 
262 	/* PTEs are revoked in obj->ops->put_pages() */
263 	err = remap_io_sg(area,
264 			  area->vm_start, area->vm_end - area->vm_start,
265 			  obj->mm.pages->sgl, iomap);
266 
267 	if (area->vm_flags & VM_WRITE) {
268 		GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
269 		obj->mm.dirty = true;
270 	}
271 
272 	i915_gem_object_unpin_pages(obj);
273 
274 out:
275 	i915_gem_object_unlock(obj);
276 	return i915_error_to_vmf_fault(err);
277 }
278 
279 static vm_fault_t vm_fault_gtt(struct vm_fault *vmf)
280 {
281 #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
282 	struct vm_area_struct *area = vmf->vma;
283 	struct i915_mmap_offset *mmo = area->vm_private_data;
284 	struct drm_i915_gem_object *obj = mmo->obj;
285 	struct drm_device *dev = obj->base.dev;
286 	struct drm_i915_private *i915 = to_i915(dev);
287 	struct intel_runtime_pm *rpm = &i915->runtime_pm;
288 	struct i915_ggtt *ggtt = &i915->ggtt;
289 	bool write = area->vm_flags & VM_WRITE;
290 	struct i915_gem_ww_ctx ww;
291 	intel_wakeref_t wakeref;
292 	struct i915_vma *vma;
293 	pgoff_t page_offset;
294 	int srcu;
295 	int ret;
296 
297 	/* We don't use vmf->pgoff since that has the fake offset */
298 	page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
299 
300 	trace_i915_gem_object_fault(obj, page_offset, true, write);
301 
302 	wakeref = intel_runtime_pm_get(rpm);
303 
304 	i915_gem_ww_ctx_init(&ww, true);
305 retry:
306 	ret = i915_gem_object_lock(obj, &ww);
307 	if (ret)
308 		goto err_rpm;
309 
310 	/* Sanity check that we allow writing into this object */
311 	if (i915_gem_object_is_readonly(obj) && write) {
312 		ret = -EFAULT;
313 		goto err_rpm;
314 	}
315 
316 	ret = i915_gem_object_pin_pages(obj);
317 	if (ret)
318 		goto err_rpm;
319 
320 	ret = intel_gt_reset_trylock(ggtt->vm.gt, &srcu);
321 	if (ret)
322 		goto err_pages;
323 
324 	/* Now pin it into the GTT as needed */
325 	vma = i915_gem_object_ggtt_pin_ww(obj, &ww, NULL, 0, 0,
326 					  PIN_MAPPABLE |
327 					  PIN_NONBLOCK /* NOWARN */ |
328 					  PIN_NOEVICT);
329 	if (IS_ERR(vma) && vma != ERR_PTR(-EDEADLK)) {
330 		/* Use a partial view if it is bigger than available space */
331 		struct i915_ggtt_view view =
332 			compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
333 		unsigned int flags;
334 
335 		flags = PIN_MAPPABLE | PIN_NOSEARCH;
336 		if (view.type == I915_GGTT_VIEW_NORMAL)
337 			flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
338 
339 		/*
340 		 * Userspace is now writing through an untracked VMA, abandon
341 		 * all hope that the hardware is able to track future writes.
342 		 */
343 
344 		vma = i915_gem_object_ggtt_pin_ww(obj, &ww, &view, 0, 0, flags);
345 		if (IS_ERR(vma) && vma != ERR_PTR(-EDEADLK)) {
346 			flags = PIN_MAPPABLE;
347 			view.type = I915_GGTT_VIEW_PARTIAL;
348 			vma = i915_gem_object_ggtt_pin_ww(obj, &ww, &view, 0, 0, flags);
349 		}
350 
351 		/* The entire mappable GGTT is pinned? Unexpected! */
352 		GEM_BUG_ON(vma == ERR_PTR(-ENOSPC));
353 	}
354 	if (IS_ERR(vma)) {
355 		ret = PTR_ERR(vma);
356 		goto err_reset;
357 	}
358 
359 	/* Access to snoopable pages through the GTT is incoherent. */
360 	if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(i915)) {
361 		ret = -EFAULT;
362 		goto err_unpin;
363 	}
364 
365 	ret = i915_vma_pin_fence(vma);
366 	if (ret)
367 		goto err_unpin;
368 
369 	/* Finally, remap it using the new GTT offset */
370 	ret = remap_io_mapping(area,
371 			       area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
372 			       (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
373 			       min_t(u64, vma->size, area->vm_end - area->vm_start),
374 			       &ggtt->iomap);
375 	if (ret)
376 		goto err_fence;
377 
378 	assert_rpm_wakelock_held(rpm);
379 
380 	/* Mark as being mmapped into userspace for later revocation */
381 	mutex_lock(&i915->ggtt.vm.mutex);
382 	if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
383 		list_add(&obj->userfault_link, &i915->ggtt.userfault_list);
384 	mutex_unlock(&i915->ggtt.vm.mutex);
385 
386 	/* Track the mmo associated with the fenced vma */
387 	vma->mmo = mmo;
388 
389 	if (IS_ACTIVE(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND))
390 		intel_wakeref_auto(&i915->ggtt.userfault_wakeref,
391 				   msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND));
392 
393 	if (write) {
394 		GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
395 		i915_vma_set_ggtt_write(vma);
396 		obj->mm.dirty = true;
397 	}
398 
399 err_fence:
400 	i915_vma_unpin_fence(vma);
401 err_unpin:
402 	__i915_vma_unpin(vma);
403 err_reset:
404 	intel_gt_reset_unlock(ggtt->vm.gt, srcu);
405 err_pages:
406 	i915_gem_object_unpin_pages(obj);
407 err_rpm:
408 	if (ret == -EDEADLK) {
409 		ret = i915_gem_ww_ctx_backoff(&ww);
410 		if (!ret)
411 			goto retry;
412 	}
413 	i915_gem_ww_ctx_fini(&ww);
414 	intel_runtime_pm_put(rpm, wakeref);
415 	return i915_error_to_vmf_fault(ret);
416 }
417 
418 static int
419 vm_access(struct vm_area_struct *area, unsigned long addr,
420 	  void *buf, int len, int write)
421 {
422 	struct i915_mmap_offset *mmo = area->vm_private_data;
423 	struct drm_i915_gem_object *obj = mmo->obj;
424 	struct i915_gem_ww_ctx ww;
425 	void *vaddr;
426 	int err = 0;
427 
428 	if (i915_gem_object_is_readonly(obj) && write)
429 		return -EACCES;
430 
431 	addr -= area->vm_start;
432 	if (addr >= obj->base.size)
433 		return -EINVAL;
434 
435 	i915_gem_ww_ctx_init(&ww, true);
436 retry:
437 	err = i915_gem_object_lock(obj, &ww);
438 	if (err)
439 		goto out;
440 
441 	/* As this is primarily for debugging, let's focus on simplicity */
442 	vaddr = i915_gem_object_pin_map(obj, I915_MAP_FORCE_WC);
443 	if (IS_ERR(vaddr)) {
444 		err = PTR_ERR(vaddr);
445 		goto out;
446 	}
447 
448 	if (write) {
449 		memcpy(vaddr + addr, buf, len);
450 		__i915_gem_object_flush_map(obj, addr, len);
451 	} else {
452 		memcpy(buf, vaddr + addr, len);
453 	}
454 
455 	i915_gem_object_unpin_map(obj);
456 out:
457 	if (err == -EDEADLK) {
458 		err = i915_gem_ww_ctx_backoff(&ww);
459 		if (!err)
460 			goto retry;
461 	}
462 	i915_gem_ww_ctx_fini(&ww);
463 
464 	if (err)
465 		return err;
466 
467 	return len;
468 }
469 
470 void __i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj)
471 {
472 	struct i915_vma *vma;
473 
474 	GEM_BUG_ON(!obj->userfault_count);
475 
476 	for_each_ggtt_vma(vma, obj)
477 		i915_vma_revoke_mmap(vma);
478 
479 	GEM_BUG_ON(obj->userfault_count);
480 }
481 
482 /*
483  * It is vital that we remove the page mapping if we have mapped a tiled
484  * object through the GTT and then lose the fence register due to
485  * resource pressure. Similarly if the object has been moved out of the
486  * aperture, than pages mapped into userspace must be revoked. Removing the
487  * mapping will then trigger a page fault on the next user access, allowing
488  * fixup by vm_fault_gtt().
489  */
490 void i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj)
491 {
492 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
493 	intel_wakeref_t wakeref;
494 
495 	/*
496 	 * Serialisation between user GTT access and our code depends upon
497 	 * revoking the CPU's PTE whilst the mutex is held. The next user
498 	 * pagefault then has to wait until we release the mutex.
499 	 *
500 	 * Note that RPM complicates somewhat by adding an additional
501 	 * requirement that operations to the GGTT be made holding the RPM
502 	 * wakeref.
503 	 */
504 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
505 	mutex_lock(&i915->ggtt.vm.mutex);
506 
507 	if (!obj->userfault_count)
508 		goto out;
509 
510 	__i915_gem_object_release_mmap_gtt(obj);
511 
512 	/*
513 	 * Ensure that the CPU's PTE are revoked and there are not outstanding
514 	 * memory transactions from userspace before we return. The TLB
515 	 * flushing implied above by changing the PTE above *should* be
516 	 * sufficient, an extra barrier here just provides us with a bit
517 	 * of paranoid documentation about our requirement to serialise
518 	 * memory writes before touching registers / GSM.
519 	 */
520 	wmb();
521 
522 out:
523 	mutex_unlock(&i915->ggtt.vm.mutex);
524 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
525 }
526 
527 void i915_gem_object_release_mmap_offset(struct drm_i915_gem_object *obj)
528 {
529 	struct i915_mmap_offset *mmo, *mn;
530 
531 	spin_lock(&obj->mmo.lock);
532 	rbtree_postorder_for_each_entry_safe(mmo, mn,
533 					     &obj->mmo.offsets, offset) {
534 		/*
535 		 * vma_node_unmap for GTT mmaps handled already in
536 		 * __i915_gem_object_release_mmap_gtt
537 		 */
538 		if (mmo->mmap_type == I915_MMAP_TYPE_GTT)
539 			continue;
540 
541 		spin_unlock(&obj->mmo.lock);
542 		drm_vma_node_unmap(&mmo->vma_node,
543 				   obj->base.dev->anon_inode->i_mapping);
544 		spin_lock(&obj->mmo.lock);
545 	}
546 	spin_unlock(&obj->mmo.lock);
547 }
548 
549 static struct i915_mmap_offset *
550 lookup_mmo(struct drm_i915_gem_object *obj,
551 	   enum i915_mmap_type mmap_type)
552 {
553 	struct rb_node *rb;
554 
555 	spin_lock(&obj->mmo.lock);
556 	rb = obj->mmo.offsets.rb_node;
557 	while (rb) {
558 		struct i915_mmap_offset *mmo =
559 			rb_entry(rb, typeof(*mmo), offset);
560 
561 		if (mmo->mmap_type == mmap_type) {
562 			spin_unlock(&obj->mmo.lock);
563 			return mmo;
564 		}
565 
566 		if (mmo->mmap_type < mmap_type)
567 			rb = rb->rb_right;
568 		else
569 			rb = rb->rb_left;
570 	}
571 	spin_unlock(&obj->mmo.lock);
572 
573 	return NULL;
574 }
575 
576 static struct i915_mmap_offset *
577 insert_mmo(struct drm_i915_gem_object *obj, struct i915_mmap_offset *mmo)
578 {
579 	struct rb_node *rb, **p;
580 
581 	spin_lock(&obj->mmo.lock);
582 	rb = NULL;
583 	p = &obj->mmo.offsets.rb_node;
584 	while (*p) {
585 		struct i915_mmap_offset *pos;
586 
587 		rb = *p;
588 		pos = rb_entry(rb, typeof(*pos), offset);
589 
590 		if (pos->mmap_type == mmo->mmap_type) {
591 			spin_unlock(&obj->mmo.lock);
592 			drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
593 					      &mmo->vma_node);
594 			kfree(mmo);
595 			return pos;
596 		}
597 
598 		if (pos->mmap_type < mmo->mmap_type)
599 			p = &rb->rb_right;
600 		else
601 			p = &rb->rb_left;
602 	}
603 	rb_link_node(&mmo->offset, rb, p);
604 	rb_insert_color(&mmo->offset, &obj->mmo.offsets);
605 	spin_unlock(&obj->mmo.lock);
606 
607 	return mmo;
608 }
609 
610 static struct i915_mmap_offset *
611 mmap_offset_attach(struct drm_i915_gem_object *obj,
612 		   enum i915_mmap_type mmap_type,
613 		   struct drm_file *file)
614 {
615 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
616 	struct i915_mmap_offset *mmo;
617 	int err;
618 
619 	mmo = lookup_mmo(obj, mmap_type);
620 	if (mmo)
621 		goto out;
622 
623 	mmo = kmalloc(sizeof(*mmo), GFP_KERNEL);
624 	if (!mmo)
625 		return ERR_PTR(-ENOMEM);
626 
627 	mmo->obj = obj;
628 	mmo->mmap_type = mmap_type;
629 	drm_vma_node_reset(&mmo->vma_node);
630 
631 	err = drm_vma_offset_add(obj->base.dev->vma_offset_manager,
632 				 &mmo->vma_node, obj->base.size / PAGE_SIZE);
633 	if (likely(!err))
634 		goto insert;
635 
636 	/* Attempt to reap some mmap space from dead objects */
637 	err = intel_gt_retire_requests_timeout(&i915->gt, MAX_SCHEDULE_TIMEOUT);
638 	if (err)
639 		goto err;
640 
641 	i915_gem_drain_freed_objects(i915);
642 	err = drm_vma_offset_add(obj->base.dev->vma_offset_manager,
643 				 &mmo->vma_node, obj->base.size / PAGE_SIZE);
644 	if (err)
645 		goto err;
646 
647 insert:
648 	mmo = insert_mmo(obj, mmo);
649 	GEM_BUG_ON(lookup_mmo(obj, mmap_type) != mmo);
650 out:
651 	if (file)
652 		drm_vma_node_allow(&mmo->vma_node, file);
653 	return mmo;
654 
655 err:
656 	kfree(mmo);
657 	return ERR_PTR(err);
658 }
659 
660 static int
661 __assign_mmap_offset(struct drm_file *file,
662 		     u32 handle,
663 		     enum i915_mmap_type mmap_type,
664 		     u64 *offset)
665 {
666 	struct drm_i915_gem_object *obj;
667 	struct i915_mmap_offset *mmo;
668 	int err;
669 
670 	obj = i915_gem_object_lookup(file, handle);
671 	if (!obj)
672 		return -ENOENT;
673 
674 	if (i915_gem_object_never_mmap(obj)) {
675 		err = -ENODEV;
676 		goto out;
677 	}
678 
679 	if (mmap_type != I915_MMAP_TYPE_GTT &&
680 	    !i915_gem_object_has_struct_page(obj) &&
681 	    !i915_gem_object_type_has(obj, I915_GEM_OBJECT_HAS_IOMEM)) {
682 		err = -ENODEV;
683 		goto out;
684 	}
685 
686 	mmo = mmap_offset_attach(obj, mmap_type, file);
687 	if (IS_ERR(mmo)) {
688 		err = PTR_ERR(mmo);
689 		goto out;
690 	}
691 
692 	*offset = drm_vma_node_offset_addr(&mmo->vma_node);
693 	err = 0;
694 out:
695 	i915_gem_object_put(obj);
696 	return err;
697 }
698 
699 int
700 i915_gem_dumb_mmap_offset(struct drm_file *file,
701 			  struct drm_device *dev,
702 			  u32 handle,
703 			  u64 *offset)
704 {
705 	enum i915_mmap_type mmap_type;
706 
707 	if (boot_cpu_has(X86_FEATURE_PAT))
708 		mmap_type = I915_MMAP_TYPE_WC;
709 	else if (!i915_ggtt_has_aperture(&to_i915(dev)->ggtt))
710 		return -ENODEV;
711 	else
712 		mmap_type = I915_MMAP_TYPE_GTT;
713 
714 	return __assign_mmap_offset(file, handle, mmap_type, offset);
715 }
716 
717 /**
718  * i915_gem_mmap_offset_ioctl - prepare an object for GTT mmap'ing
719  * @dev: DRM device
720  * @data: GTT mapping ioctl data
721  * @file: GEM object info
722  *
723  * Simply returns the fake offset to userspace so it can mmap it.
724  * The mmap call will end up in drm_gem_mmap(), which will set things
725  * up so we can get faults in the handler above.
726  *
727  * The fault handler will take care of binding the object into the GTT
728  * (since it may have been evicted to make room for something), allocating
729  * a fence register, and mapping the appropriate aperture address into
730  * userspace.
731  */
732 int
733 i915_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
734 			   struct drm_file *file)
735 {
736 	struct drm_i915_private *i915 = to_i915(dev);
737 	struct drm_i915_gem_mmap_offset *args = data;
738 	enum i915_mmap_type type;
739 	int err;
740 
741 	/*
742 	 * Historically we failed to check args.pad and args.offset
743 	 * and so we cannot use those fields for user input and we cannot
744 	 * add -EINVAL for them as the ABI is fixed, i.e. old userspace
745 	 * may be feeding in garbage in those fields.
746 	 *
747 	 * if (args->pad) return -EINVAL; is verbotten!
748 	 */
749 
750 	err = i915_user_extensions(u64_to_user_ptr(args->extensions),
751 				   NULL, 0, NULL);
752 	if (err)
753 		return err;
754 
755 	switch (args->flags) {
756 	case I915_MMAP_OFFSET_GTT:
757 		if (!i915_ggtt_has_aperture(&i915->ggtt))
758 			return -ENODEV;
759 		type = I915_MMAP_TYPE_GTT;
760 		break;
761 
762 	case I915_MMAP_OFFSET_WC:
763 		if (!boot_cpu_has(X86_FEATURE_PAT))
764 			return -ENODEV;
765 		type = I915_MMAP_TYPE_WC;
766 		break;
767 
768 	case I915_MMAP_OFFSET_WB:
769 		type = I915_MMAP_TYPE_WB;
770 		break;
771 
772 	case I915_MMAP_OFFSET_UC:
773 		if (!boot_cpu_has(X86_FEATURE_PAT))
774 			return -ENODEV;
775 		type = I915_MMAP_TYPE_UC;
776 		break;
777 
778 	default:
779 		return -EINVAL;
780 	}
781 
782 	return __assign_mmap_offset(file, args->handle, type, &args->offset);
783 }
784 
785 static void vm_open(struct vm_area_struct *vma)
786 {
787 	struct i915_mmap_offset *mmo = vma->vm_private_data;
788 	struct drm_i915_gem_object *obj = mmo->obj;
789 
790 	GEM_BUG_ON(!obj);
791 	i915_gem_object_get(obj);
792 }
793 
794 static void vm_close(struct vm_area_struct *vma)
795 {
796 	struct i915_mmap_offset *mmo = vma->vm_private_data;
797 	struct drm_i915_gem_object *obj = mmo->obj;
798 
799 	GEM_BUG_ON(!obj);
800 	i915_gem_object_put(obj);
801 }
802 
803 static const struct vm_operations_struct vm_ops_gtt = {
804 	.fault = vm_fault_gtt,
805 	.access = vm_access,
806 	.open = vm_open,
807 	.close = vm_close,
808 };
809 
810 static const struct vm_operations_struct vm_ops_cpu = {
811 	.fault = vm_fault_cpu,
812 	.access = vm_access,
813 	.open = vm_open,
814 	.close = vm_close,
815 };
816 
817 static int singleton_release(struct inode *inode, struct file *file)
818 {
819 	struct drm_i915_private *i915 = file->private_data;
820 
821 	cmpxchg(&i915->gem.mmap_singleton, file, NULL);
822 	drm_dev_put(&i915->drm);
823 
824 	return 0;
825 }
826 
827 static const struct file_operations singleton_fops = {
828 	.owner = THIS_MODULE,
829 	.release = singleton_release,
830 };
831 
832 static struct file *mmap_singleton(struct drm_i915_private *i915)
833 {
834 	struct file *file;
835 
836 	rcu_read_lock();
837 	file = READ_ONCE(i915->gem.mmap_singleton);
838 	if (file && !get_file_rcu(file))
839 		file = NULL;
840 	rcu_read_unlock();
841 	if (file)
842 		return file;
843 
844 	file = anon_inode_getfile("i915.gem", &singleton_fops, i915, O_RDWR);
845 	if (IS_ERR(file))
846 		return file;
847 
848 	/* Everyone shares a single global address space */
849 	file->f_mapping = i915->drm.anon_inode->i_mapping;
850 
851 	smp_store_mb(i915->gem.mmap_singleton, file);
852 	drm_dev_get(&i915->drm);
853 
854 	return file;
855 }
856 
857 /*
858  * This overcomes the limitation in drm_gem_mmap's assignment of a
859  * drm_gem_object as the vma->vm_private_data. Since we need to
860  * be able to resolve multiple mmap offsets which could be tied
861  * to a single gem object.
862  */
863 int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
864 {
865 	struct drm_vma_offset_node *node;
866 	struct drm_file *priv = filp->private_data;
867 	struct drm_device *dev = priv->minor->dev;
868 	struct drm_i915_gem_object *obj = NULL;
869 	struct i915_mmap_offset *mmo = NULL;
870 	struct file *anon;
871 
872 	if (drm_dev_is_unplugged(dev))
873 		return -ENODEV;
874 
875 	rcu_read_lock();
876 	drm_vma_offset_lock_lookup(dev->vma_offset_manager);
877 	node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
878 						  vma->vm_pgoff,
879 						  vma_pages(vma));
880 	if (node && drm_vma_node_is_allowed(node, priv)) {
881 		/*
882 		 * Skip 0-refcnted objects as it is in the process of being
883 		 * destroyed and will be invalid when the vma manager lock
884 		 * is released.
885 		 */
886 		mmo = container_of(node, struct i915_mmap_offset, vma_node);
887 		obj = i915_gem_object_get_rcu(mmo->obj);
888 	}
889 	drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
890 	rcu_read_unlock();
891 	if (!obj)
892 		return node ? -EACCES : -EINVAL;
893 
894 	if (i915_gem_object_is_readonly(obj)) {
895 		if (vma->vm_flags & VM_WRITE) {
896 			i915_gem_object_put(obj);
897 			return -EINVAL;
898 		}
899 		vma->vm_flags &= ~VM_MAYWRITE;
900 	}
901 
902 	anon = mmap_singleton(to_i915(dev));
903 	if (IS_ERR(anon)) {
904 		i915_gem_object_put(obj);
905 		return PTR_ERR(anon);
906 	}
907 
908 	vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
909 	vma->vm_private_data = mmo;
910 
911 	/*
912 	 * We keep the ref on mmo->obj, not vm_file, but we require
913 	 * vma->vm_file->f_mapping, see vma_link(), for later revocation.
914 	 * Our userspace is accustomed to having per-file resource cleanup
915 	 * (i.e. contexts, objects and requests) on their close(fd), which
916 	 * requires avoiding extraneous references to their filp, hence why
917 	 * we prefer to use an anonymous file for their mmaps.
918 	 */
919 	vma_set_file(vma, anon);
920 	/* Drop the initial creation reference, the vma is now holding one. */
921 	fput(anon);
922 
923 	switch (mmo->mmap_type) {
924 	case I915_MMAP_TYPE_WC:
925 		vma->vm_page_prot =
926 			pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
927 		vma->vm_ops = &vm_ops_cpu;
928 		break;
929 
930 	case I915_MMAP_TYPE_WB:
931 		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
932 		vma->vm_ops = &vm_ops_cpu;
933 		break;
934 
935 	case I915_MMAP_TYPE_UC:
936 		vma->vm_page_prot =
937 			pgprot_noncached(vm_get_page_prot(vma->vm_flags));
938 		vma->vm_ops = &vm_ops_cpu;
939 		break;
940 
941 	case I915_MMAP_TYPE_GTT:
942 		vma->vm_page_prot =
943 			pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
944 		vma->vm_ops = &vm_ops_gtt;
945 		break;
946 	}
947 	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
948 
949 	return 0;
950 }
951 
952 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
953 #include "selftests/i915_gem_mman.c"
954 #endif
955