c7b9b34de01b0855f160d7f5f60db47cb9093424
[sfrench/cifs-2.6.git] / drivers / gpu / drm / i915 / gem / i915_gem_mman.c
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2014-2016 Intel Corporation
5  */
6
7 #include <linux/mman.h>
8 #include <linux/sizes.h>
9
10 #include "i915_drv.h"
11 #include "i915_gem_gtt.h"
12 #include "i915_gem_ioctls.h"
13 #include "i915_gem_object.h"
14 #include "i915_vma.h"
15 #include "intel_drv.h"
16
17 static inline bool
18 __vma_matches(struct vm_area_struct *vma, struct file *filp,
19               unsigned long addr, unsigned long size)
20 {
21         if (vma->vm_file != filp)
22                 return false;
23
24         return vma->vm_start == addr &&
25                (vma->vm_end - vma->vm_start) == PAGE_ALIGN(size);
26 }
27
28 /**
29  * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
30  *                       it is mapped to.
31  * @dev: drm device
32  * @data: ioctl data blob
33  * @file: drm file
34  *
35  * While the mapping holds a reference on the contents of the object, it doesn't
36  * imply a ref on the object itself.
37  *
38  * IMPORTANT:
39  *
40  * DRM driver writers who look a this function as an example for how to do GEM
41  * mmap support, please don't implement mmap support like here. The modern way
42  * to implement DRM mmap support is with an mmap offset ioctl (like
43  * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
44  * That way debug tooling like valgrind will understand what's going on, hiding
45  * the mmap call in a driver private ioctl will break that. The i915 driver only
46  * does cpu mmaps this way because we didn't know better.
47  */
48 int
49 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
50                     struct drm_file *file)
51 {
52         struct drm_i915_gem_mmap *args = data;
53         struct drm_i915_gem_object *obj;
54         unsigned long addr;
55
56         if (args->flags & ~(I915_MMAP_WC))
57                 return -EINVAL;
58
59         if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
60                 return -ENODEV;
61
62         obj = i915_gem_object_lookup(file, args->handle);
63         if (!obj)
64                 return -ENOENT;
65
66         /* prime objects have no backing filp to GEM mmap
67          * pages from.
68          */
69         if (!obj->base.filp) {
70                 addr = -ENXIO;
71                 goto err;
72         }
73
74         if (range_overflows(args->offset, args->size, (u64)obj->base.size)) {
75                 addr = -EINVAL;
76                 goto err;
77         }
78
79         addr = vm_mmap(obj->base.filp, 0, args->size,
80                        PROT_READ | PROT_WRITE, MAP_SHARED,
81                        args->offset);
82         if (IS_ERR_VALUE(addr))
83                 goto err;
84
85         if (args->flags & I915_MMAP_WC) {
86                 struct mm_struct *mm = current->mm;
87                 struct vm_area_struct *vma;
88
89                 if (down_write_killable(&mm->mmap_sem)) {
90                         addr = -EINTR;
91                         goto err;
92                 }
93                 vma = find_vma(mm, addr);
94                 if (vma && __vma_matches(vma, obj->base.filp, addr, args->size))
95                         vma->vm_page_prot =
96                                 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
97                 else
98                         addr = -ENOMEM;
99                 up_write(&mm->mmap_sem);
100                 if (IS_ERR_VALUE(addr))
101                         goto err;
102
103                 /* This may race, but that's ok, it only gets set */
104                 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
105         }
106         i915_gem_object_put(obj);
107
108         args->addr_ptr = (u64)addr;
109         return 0;
110
111 err:
112         i915_gem_object_put(obj);
113         return addr;
114 }
115
116 static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj)
117 {
118         return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
119 }
120
121 /**
122  * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
123  *
124  * A history of the GTT mmap interface:
125  *
126  * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
127  *     aligned and suitable for fencing, and still fit into the available
128  *     mappable space left by the pinned display objects. A classic problem
129  *     we called the page-fault-of-doom where we would ping-pong between
130  *     two objects that could not fit inside the GTT and so the memcpy
131  *     would page one object in at the expense of the other between every
132  *     single byte.
133  *
134  * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
135  *     as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
136  *     object is too large for the available space (or simply too large
137  *     for the mappable aperture!), a view is created instead and faulted
138  *     into userspace. (This view is aligned and sized appropriately for
139  *     fenced access.)
140  *
141  * 2 - Recognise WC as a separate cache domain so that we can flush the
142  *     delayed writes via GTT before performing direct access via WC.
143  *
144  * 3 - Remove implicit set-domain(GTT) and synchronisation on initial
145  *     pagefault; swapin remains transparent.
146  *
147  * Restrictions:
148  *
149  *  * snoopable objects cannot be accessed via the GTT. It can cause machine
150  *    hangs on some architectures, corruption on others. An attempt to service
151  *    a GTT page fault from a snoopable object will generate a SIGBUS.
152  *
153  *  * the object must be able to fit into RAM (physical memory, though no
154  *    limited to the mappable aperture).
155  *
156  *
157  * Caveats:
158  *
159  *  * a new GTT page fault will synchronize rendering from the GPU and flush
160  *    all data to system memory. Subsequent access will not be synchronized.
161  *
162  *  * all mappings are revoked on runtime device suspend.
163  *
164  *  * there are only 8, 16 or 32 fence registers to share between all users
165  *    (older machines require fence register for display and blitter access
166  *    as well). Contention of the fence registers will cause the previous users
167  *    to be unmapped and any new access will generate new page faults.
168  *
169  *  * running out of memory while servicing a fault may generate a SIGBUS,
170  *    rather than the expected SIGSEGV.
171  */
172 int i915_gem_mmap_gtt_version(void)
173 {
174         return 3;
175 }
176
177 static inline struct i915_ggtt_view
178 compute_partial_view(const struct drm_i915_gem_object *obj,
179                      pgoff_t page_offset,
180                      unsigned int chunk)
181 {
182         struct i915_ggtt_view view;
183
184         if (i915_gem_object_is_tiled(obj))
185                 chunk = roundup(chunk, tile_row_pages(obj));
186
187         view.type = I915_GGTT_VIEW_PARTIAL;
188         view.partial.offset = rounddown(page_offset, chunk);
189         view.partial.size =
190                 min_t(unsigned int, chunk,
191                       (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
192
193         /* If the partial covers the entire object, just create a normal VMA. */
194         if (chunk >= obj->base.size >> PAGE_SHIFT)
195                 view.type = I915_GGTT_VIEW_NORMAL;
196
197         return view;
198 }
199
200 /**
201  * i915_gem_fault - fault a page into the GTT
202  * @vmf: fault info
203  *
204  * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
205  * from userspace.  The fault handler takes care of binding the object to
206  * the GTT (if needed), allocating and programming a fence register (again,
207  * only if needed based on whether the old reg is still valid or the object
208  * is tiled) and inserting a new PTE into the faulting process.
209  *
210  * Note that the faulting process may involve evicting existing objects
211  * from the GTT and/or fence registers to make room.  So performance may
212  * suffer if the GTT working set is large or there are few fence registers
213  * left.
214  *
215  * The current feature set supported by i915_gem_fault() and thus GTT mmaps
216  * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
217  */
218 vm_fault_t i915_gem_fault(struct vm_fault *vmf)
219 {
220 #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT)
221         struct vm_area_struct *area = vmf->vma;
222         struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
223         struct drm_device *dev = obj->base.dev;
224         struct drm_i915_private *i915 = to_i915(dev);
225         struct i915_ggtt *ggtt = &i915->ggtt;
226         bool write = area->vm_flags & VM_WRITE;
227         intel_wakeref_t wakeref;
228         struct i915_vma *vma;
229         pgoff_t page_offset;
230         int srcu;
231         int ret;
232
233         /* Sanity check that we allow writing into this object */
234         if (i915_gem_object_is_readonly(obj) && write)
235                 return VM_FAULT_SIGBUS;
236
237         /* We don't use vmf->pgoff since that has the fake offset */
238         page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
239
240         trace_i915_gem_object_fault(obj, page_offset, true, write);
241
242         ret = i915_gem_object_pin_pages(obj);
243         if (ret)
244                 goto err;
245
246         wakeref = intel_runtime_pm_get(i915);
247
248         srcu = i915_reset_trylock(i915);
249         if (srcu < 0) {
250                 ret = srcu;
251                 goto err_rpm;
252         }
253
254         ret = i915_mutex_lock_interruptible(dev);
255         if (ret)
256                 goto err_reset;
257
258         /* Access to snoopable pages through the GTT is incoherent. */
259         if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(i915)) {
260                 ret = -EFAULT;
261                 goto err_unlock;
262         }
263
264         /* Now pin it into the GTT as needed */
265         vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
266                                        PIN_MAPPABLE |
267                                        PIN_NONBLOCK |
268                                        PIN_NONFAULT);
269         if (IS_ERR(vma)) {
270                 /* Use a partial view if it is bigger than available space */
271                 struct i915_ggtt_view view =
272                         compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
273                 unsigned int flags;
274
275                 flags = PIN_MAPPABLE;
276                 if (view.type == I915_GGTT_VIEW_NORMAL)
277                         flags |= PIN_NONBLOCK; /* avoid warnings for pinned */
278
279                 /*
280                  * Userspace is now writing through an untracked VMA, abandon
281                  * all hope that the hardware is able to track future writes.
282                  */
283                 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
284
285                 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
286                 if (IS_ERR(vma) && !view.type) {
287                         flags = PIN_MAPPABLE;
288                         view.type = I915_GGTT_VIEW_PARTIAL;
289                         vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags);
290                 }
291         }
292         if (IS_ERR(vma)) {
293                 ret = PTR_ERR(vma);
294                 goto err_unlock;
295         }
296
297         ret = i915_vma_pin_fence(vma);
298         if (ret)
299                 goto err_unpin;
300
301         /* Finally, remap it using the new GTT offset */
302         ret = remap_io_mapping(area,
303                                area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
304                                (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
305                                min_t(u64, vma->size, area->vm_end - area->vm_start),
306                                &ggtt->iomap);
307         if (ret)
308                 goto err_fence;
309
310         /* Mark as being mmapped into userspace for later revocation */
311         assert_rpm_wakelock_held(i915);
312         if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
313                 list_add(&obj->userfault_link, &i915->mm.userfault_list);
314         if (CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND)
315                 intel_wakeref_auto(&i915->mm.userfault_wakeref,
316                                    msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND));
317         GEM_BUG_ON(!obj->userfault_count);
318
319         i915_vma_set_ggtt_write(vma);
320
321 err_fence:
322         i915_vma_unpin_fence(vma);
323 err_unpin:
324         __i915_vma_unpin(vma);
325 err_unlock:
326         mutex_unlock(&dev->struct_mutex);
327 err_reset:
328         i915_reset_unlock(i915, srcu);
329 err_rpm:
330         intel_runtime_pm_put(i915, wakeref);
331         i915_gem_object_unpin_pages(obj);
332 err:
333         switch (ret) {
334         case -EIO:
335                 /*
336                  * We eat errors when the gpu is terminally wedged to avoid
337                  * userspace unduly crashing (gl has no provisions for mmaps to
338                  * fail). But any other -EIO isn't ours (e.g. swap in failure)
339                  * and so needs to be reported.
340                  */
341                 if (!i915_terminally_wedged(i915))
342                         return VM_FAULT_SIGBUS;
343                 /* else: fall through */
344         case -EAGAIN:
345                 /*
346                  * EAGAIN means the gpu is hung and we'll wait for the error
347                  * handler to reset everything when re-faulting in
348                  * i915_mutex_lock_interruptible.
349                  */
350         case 0:
351         case -ERESTARTSYS:
352         case -EINTR:
353         case -EBUSY:
354                 /*
355                  * EBUSY is ok: this just means that another thread
356                  * already did the job.
357                  */
358                 return VM_FAULT_NOPAGE;
359         case -ENOMEM:
360                 return VM_FAULT_OOM;
361         case -ENOSPC:
362         case -EFAULT:
363                 return VM_FAULT_SIGBUS;
364         default:
365                 WARN_ONCE(ret, "unhandled error in %s: %i\n", __func__, ret);
366                 return VM_FAULT_SIGBUS;
367         }
368 }
369
370 void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
371 {
372         struct i915_vma *vma;
373
374         GEM_BUG_ON(!obj->userfault_count);
375
376         obj->userfault_count = 0;
377         list_del(&obj->userfault_link);
378         drm_vma_node_unmap(&obj->base.vma_node,
379                            obj->base.dev->anon_inode->i_mapping);
380
381         for_each_ggtt_vma(vma, obj)
382                 i915_vma_unset_userfault(vma);
383 }
384
385 /**
386  * i915_gem_object_release_mmap - remove physical page mappings
387  * @obj: obj in question
388  *
389  * Preserve the reservation of the mmapping with the DRM core code, but
390  * relinquish ownership of the pages back to the system.
391  *
392  * It is vital that we remove the page mapping if we have mapped a tiled
393  * object through the GTT and then lose the fence register due to
394  * resource pressure. Similarly if the object has been moved out of the
395  * aperture, than pages mapped into userspace must be revoked. Removing the
396  * mapping will then trigger a page fault on the next user access, allowing
397  * fixup by i915_gem_fault().
398  */
399 void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
400 {
401         struct drm_i915_private *i915 = to_i915(obj->base.dev);
402         intel_wakeref_t wakeref;
403
404         /* Serialisation between user GTT access and our code depends upon
405          * revoking the CPU's PTE whilst the mutex is held. The next user
406          * pagefault then has to wait until we release the mutex.
407          *
408          * Note that RPM complicates somewhat by adding an additional
409          * requirement that operations to the GGTT be made holding the RPM
410          * wakeref.
411          */
412         lockdep_assert_held(&i915->drm.struct_mutex);
413         wakeref = intel_runtime_pm_get(i915);
414
415         if (!obj->userfault_count)
416                 goto out;
417
418         __i915_gem_object_release_mmap(obj);
419
420         /* Ensure that the CPU's PTE are revoked and there are not outstanding
421          * memory transactions from userspace before we return. The TLB
422          * flushing implied above by changing the PTE above *should* be
423          * sufficient, an extra barrier here just provides us with a bit
424          * of paranoid documentation about our requirement to serialise
425          * memory writes before touching registers / GSM.
426          */
427         wmb();
428
429 out:
430         intel_runtime_pm_put(i915, wakeref);
431 }
432
433 static int create_mmap_offset(struct drm_i915_gem_object *obj)
434 {
435         struct drm_i915_private *i915 = to_i915(obj->base.dev);
436         int err;
437
438         err = drm_gem_create_mmap_offset(&obj->base);
439         if (likely(!err))
440                 return 0;
441
442         /* Attempt to reap some mmap space from dead objects */
443         do {
444                 err = i915_gem_wait_for_idle(i915,
445                                              I915_WAIT_INTERRUPTIBLE,
446                                              MAX_SCHEDULE_TIMEOUT);
447                 if (err)
448                         break;
449
450                 i915_gem_drain_freed_objects(i915);
451                 err = drm_gem_create_mmap_offset(&obj->base);
452                 if (!err)
453                         break;
454
455         } while (flush_delayed_work(&i915->gem.retire_work));
456
457         return err;
458 }
459
460 int
461 i915_gem_mmap_gtt(struct drm_file *file,
462                   struct drm_device *dev,
463                   u32 handle,
464                   u64 *offset)
465 {
466         struct drm_i915_gem_object *obj;
467         int ret;
468
469         obj = i915_gem_object_lookup(file, handle);
470         if (!obj)
471                 return -ENOENT;
472
473         ret = create_mmap_offset(obj);
474         if (ret == 0)
475                 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
476
477         i915_gem_object_put(obj);
478         return ret;
479 }
480
481 /**
482  * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
483  * @dev: DRM device
484  * @data: GTT mapping ioctl data
485  * @file: GEM object info
486  *
487  * Simply returns the fake offset to userspace so it can mmap it.
488  * The mmap call will end up in drm_gem_mmap(), which will set things
489  * up so we can get faults in the handler above.
490  *
491  * The fault handler will take care of binding the object into the GTT
492  * (since it may have been evicted to make room for something), allocating
493  * a fence register, and mapping the appropriate aperture address into
494  * userspace.
495  */
496 int
497 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
498                         struct drm_file *file)
499 {
500         struct drm_i915_gem_mmap_gtt *args = data;
501
502         return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
503 }
504
505 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
506 #include "selftests/i915_gem_mman.c"
507 #endif