i915_gem.c revision 1.65 1 /* $NetBSD: i915_gem.c,v 1.65 2021/12/19 01:50:47 riastradh Exp $ */
2
3 /*
4 * Copyright 2008-2015 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 * Authors:
26 * Eric Anholt <eric (at) anholt.net>
27 *
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: i915_gem.c,v 1.65 2021/12/19 01:50:47 riastradh Exp $");
32
33 #ifdef __NetBSD__
34 #if 0 /* XXX uvmhist option? */
35 #include "opt_uvmhist.h"
36 #endif
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40
41 #include <uvm/uvm.h>
42 #include <uvm/uvm_extern.h>
43 #include <uvm/uvm_fault.h>
44 #include <uvm/uvm_page.h>
45 #include <uvm/uvm_pmap.h>
46 #include <uvm/uvm_prot.h>
47
48 #include <drm/bus_dma_hacks.h>
49 #endif
50
51 #include <drm/drm_vma_manager.h>
52 #include <drm/i915_drm.h>
53 #include <linux/dma-fence-array.h>
54 #include <linux/kthread.h>
55 #include <linux/dma-resv.h>
56 #include <linux/shmem_fs.h>
57 #include <linux/slab.h>
58 #include <linux/stop_machine.h>
59 #include <linux/swap.h>
60 #include <linux/pci.h>
61 #include <linux/dma-buf.h>
62 #include <linux/mman.h>
63 #include <linux/nbsd-namespace.h>
64
65 #include "display/intel_display.h"
66 #include "display/intel_frontbuffer.h"
67
68 #include "gem/i915_gem_clflush.h"
69 #include "gem/i915_gem_context.h"
70 #include "gem/i915_gem_ioctls.h"
71 #include "gem/i915_gem_mman.h"
72 #include "gem/i915_gem_region.h"
73 #include "gt/intel_engine_user.h"
74 #include "gt/intel_gt.h"
75 #include "gt/intel_gt_pm.h"
76 #include "gt/intel_workarounds.h"
77
78 #include "i915_drv.h"
79 #include "i915_trace.h"
80 #include "i915_vgpu.h"
81
82 #include "intel_pm.h"
83
84 static int
85 insert_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node, u32 size)
86 {
87 int err;
88
89 err = mutex_lock_interruptible(&ggtt->vm.mutex);
90 if (err)
91 return err;
92
93 memset(node, 0, sizeof(*node));
94 err = drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
95 size, 0, I915_COLOR_UNEVICTABLE,
96 0, ggtt->mappable_end,
97 DRM_MM_INSERT_LOW);
98
99 mutex_unlock(&ggtt->vm.mutex);
100
101 return err;
102 }
103
104 static void
105 remove_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node)
106 {
107 mutex_lock(&ggtt->vm.mutex);
108 drm_mm_remove_node(node);
109 mutex_unlock(&ggtt->vm.mutex);
110 }
111
112 int
113 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
114 struct drm_file *file)
115 {
116 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
117 struct drm_i915_gem_get_aperture *args = data;
118 struct i915_vma *vma;
119 u64 pinned;
120
121 if (mutex_lock_interruptible(&ggtt->vm.mutex))
122 return -EINTR;
123
124 pinned = ggtt->vm.reserved;
125 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
126 if (i915_vma_is_pinned(vma))
127 pinned += vma->node.size;
128
129 mutex_unlock(&ggtt->vm.mutex);
130
131 args->aper_size = ggtt->vm.total;
132 args->aper_available_size = args->aper_size - pinned;
133
134 return 0;
135 }
136
137 int i915_gem_object_unbind(struct drm_i915_gem_object *obj,
138 unsigned long flags)
139 {
140 struct intel_runtime_pm *rpm = &to_i915(obj->base.dev)->runtime_pm;
141 LIST_HEAD(still_in_list);
142 intel_wakeref_t wakeref;
143 struct i915_vma *vma;
144 int ret;
145
146 if (!atomic_read(&obj->bind_count))
147 return 0;
148
149 /*
150 * As some machines use ACPI to handle runtime-resume callbacks, and
151 * ACPI is quite kmalloc happy, we cannot resume beneath the vm->mutex
152 * as they are required by the shrinker. Ergo, we wake the device up
153 * first just in case.
154 */
155 wakeref = intel_runtime_pm_get(rpm);
156
157 try_again:
158 ret = 0;
159 spin_lock(&obj->vma.lock);
160 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
161 struct i915_vma,
162 obj_link))) {
163 struct i915_address_space *vm = vma->vm;
164
165 list_move_tail(&vma->obj_link, &still_in_list);
166 if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK))
167 continue;
168
169 ret = -EAGAIN;
170 if (!i915_vm_tryopen(vm))
171 break;
172
173 /* Prevent vma being freed by i915_vma_parked as we unbind */
174 vma = __i915_vma_get(vma);
175 spin_unlock(&obj->vma.lock);
176
177 if (vma) {
178 ret = -EBUSY;
179 if (flags & I915_GEM_OBJECT_UNBIND_ACTIVE ||
180 !i915_vma_is_active(vma))
181 ret = i915_vma_unbind(vma);
182
183 __i915_vma_put(vma);
184 }
185
186 i915_vm_close(vm);
187 spin_lock(&obj->vma.lock);
188 }
189 list_splice_init(&still_in_list, &obj->vma.list);
190 spin_unlock(&obj->vma.lock);
191
192 if (ret == -EAGAIN && flags & I915_GEM_OBJECT_UNBIND_BARRIER) {
193 rcu_barrier(); /* flush the i915_vm_release() */
194 goto try_again;
195 }
196
197 intel_runtime_pm_put(rpm, wakeref);
198
199 return ret;
200 }
201
202 static int
203 i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
204 struct drm_i915_gem_pwrite *args,
205 struct drm_file *file)
206 {
207 void *vaddr = sg_page(obj->mm.pages->sgl) + args->offset;
208 char __user *user_data = u64_to_user_ptr(args->data_ptr);
209
210 /*
211 * We manually control the domain here and pretend that it
212 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
213 */
214 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
215
216 if (copy_from_user(vaddr, user_data, args->size))
217 return -EFAULT;
218
219 drm_clflush_virt_range(vaddr, args->size);
220 intel_gt_chipset_flush(&to_i915(obj->base.dev)->gt);
221
222 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
223 return 0;
224 }
225
226 static int
227 i915_gem_create(struct drm_file *file,
228 struct intel_memory_region *mr,
229 u64 *size_p,
230 u32 *handle_p)
231 {
232 struct drm_i915_gem_object *obj;
233 u32 handle;
234 u64 size;
235 int ret;
236
237 GEM_BUG_ON(!is_power_of_2(mr->min_page_size));
238 size = round_up(*size_p, mr->min_page_size);
239 if (size == 0)
240 return -EINVAL;
241
242 /* For most of the ABI (e.g. mmap) we think in system pages */
243 GEM_BUG_ON(!IS_ALIGNED(size, PAGE_SIZE));
244
245 /* Allocate the new object */
246 obj = i915_gem_object_create_region(mr, size, 0);
247 if (IS_ERR(obj))
248 return PTR_ERR(obj);
249
250 ret = drm_gem_handle_create(file, &obj->base, &handle);
251 /* drop reference from allocate - handle holds it now */
252 i915_gem_object_put(obj);
253 if (ret)
254 return ret;
255
256 *handle_p = handle;
257 *size_p = size;
258 return 0;
259 }
260
261 int
262 i915_gem_dumb_create(struct drm_file *file,
263 struct drm_device *dev,
264 struct drm_mode_create_dumb *args)
265 {
266
267 enum intel_memory_type mem_type;
268 int cpp = DIV_ROUND_UP(args->bpp, 8);
269 u32 format;
270
271 switch (cpp) {
272 case 1:
273 format = DRM_FORMAT_C8;
274 break;
275 case 2:
276 format = DRM_FORMAT_RGB565;
277 break;
278 case 4:
279 format = DRM_FORMAT_XRGB8888;
280 break;
281 default:
282 return -EINVAL;
283 }
284
285 /* have to work out size/pitch and return them */
286 args->pitch = round_up(args->width * cpp, 64);
287
288 /* align stride to page size so that we can remap */
289 if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
290 DRM_FORMAT_MOD_LINEAR))
291 args->pitch = round_up(args->pitch, 4096);
292
293 if (args->pitch < args->width)
294 return -EINVAL;
295
296 args->size = mul_u32_u32(args->pitch, args->height);
297
298 mem_type = INTEL_MEMORY_SYSTEM;
299 if (HAS_LMEM(to_i915(dev)))
300 mem_type = INTEL_MEMORY_LOCAL;
301
302 return i915_gem_create(file,
303 intel_memory_region_by_type(to_i915(dev),
304 mem_type),
305 &args->size, &args->handle);
306 }
307
308 /**
309 * Creates a new mm object and returns a handle to it.
310 * @dev: drm device pointer
311 * @data: ioctl data blob
312 * @file: drm file pointer
313 */
314 int
315 i915_gem_create_ioctl(struct drm_device *dev, void *data,
316 struct drm_file *file)
317 {
318 struct drm_i915_private *i915 = to_i915(dev);
319 struct drm_i915_gem_create *args = data;
320
321 i915_gem_flush_free_objects(i915);
322
323 return i915_gem_create(file,
324 intel_memory_region_by_type(i915,
325 INTEL_MEMORY_SYSTEM),
326 &args->size, &args->handle);
327 }
328
329 static int
330 shmem_pread(struct page *page, int offset, int len, char __user *user_data,
331 bool needs_clflush)
332 {
333 char *vaddr;
334 int ret;
335
336 vaddr = kmap(page);
337
338 if (needs_clflush)
339 drm_clflush_virt_range(vaddr + offset, len);
340
341 ret = __copy_to_user(user_data, vaddr + offset, len);
342
343 kunmap(page);
344
345 return ret ? -EFAULT : 0;
346 }
347
348 static int
349 i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
350 struct drm_i915_gem_pread *args)
351 {
352 unsigned int needs_clflush;
353 unsigned int idx, offset;
354 struct dma_fence *fence;
355 char __user *user_data;
356 u64 remain;
357 int ret;
358
359 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
360
361 if (ret)
362 return ret;
363
364 fence = i915_gem_object_lock_fence(obj);
365 i915_gem_object_finish_access(obj);
366 if (!fence)
367 return -ENOMEM;
368
369 remain = args->size;
370 user_data = u64_to_user_ptr(args->data_ptr);
371 offset = offset_in_page(args->offset);
372 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
373 struct page *page = i915_gem_object_get_page(obj, idx);
374 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
375
376 ret = shmem_pread(page, offset, length, user_data,
377 needs_clflush);
378 if (ret)
379 break;
380
381 remain -= length;
382 user_data += length;
383 offset = 0;
384 }
385
386 i915_gem_object_unlock_fence(obj, fence);
387 return ret;
388 }
389
390 static inline bool
391 gtt_user_read(struct io_mapping *mapping,
392 loff_t base, int offset,
393 char __user *user_data, int length)
394 {
395 void __iomem *vaddr;
396 unsigned long unwritten;
397
398 /* We can use the cpu mem copy function because this is X86. */
399 vaddr = io_mapping_map_atomic_wc(mapping, base);
400 unwritten = __copy_to_user_inatomic(user_data,
401 (void __force *)vaddr + offset,
402 length);
403 io_mapping_unmap_atomic(vaddr);
404 if (unwritten) {
405 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
406 unwritten = copy_to_user(user_data,
407 (void __force *)vaddr + offset,
408 length);
409 io_mapping_unmap(vaddr);
410 }
411 return unwritten;
412 }
413
414 static int
415 i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
416 const struct drm_i915_gem_pread *args)
417 {
418 struct drm_i915_private *i915 = to_i915(obj->base.dev);
419 struct i915_ggtt *ggtt = &i915->ggtt;
420 intel_wakeref_t wakeref;
421 struct drm_mm_node node;
422 struct dma_fence *fence;
423 void __user *user_data;
424 struct i915_vma *vma;
425 u64 remain, offset;
426 int ret;
427
428 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
429 vma = ERR_PTR(-ENODEV);
430 if (!i915_gem_object_is_tiled(obj))
431 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
432 PIN_MAPPABLE |
433 PIN_NONBLOCK /* NOWARN */ |
434 PIN_NOEVICT);
435 if (!IS_ERR(vma)) {
436 node.start = i915_ggtt_offset(vma);
437 node.flags = 0;
438 } else {
439 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
440 if (ret)
441 goto out_rpm;
442 GEM_BUG_ON(!drm_mm_node_allocated(&node));
443 }
444
445 ret = i915_gem_object_lock_interruptible(obj);
446 if (ret)
447 goto out_unpin;
448
449 ret = i915_gem_object_set_to_gtt_domain(obj, false);
450 if (ret) {
451 i915_gem_object_unlock(obj);
452 goto out_unpin;
453 }
454
455 fence = i915_gem_object_lock_fence(obj);
456 i915_gem_object_unlock(obj);
457 if (!fence) {
458 ret = -ENOMEM;
459 goto out_unpin;
460 }
461
462 user_data = u64_to_user_ptr(args->data_ptr);
463 remain = args->size;
464 offset = args->offset;
465
466 while (remain > 0) {
467 /* Operation in this page
468 *
469 * page_base = page offset within aperture
470 * page_offset = offset within page
471 * page_length = bytes to copy for this page
472 */
473 u32 page_base = node.start;
474 unsigned page_offset = offset_in_page(offset);
475 unsigned page_length = PAGE_SIZE - page_offset;
476 page_length = remain < page_length ? remain : page_length;
477 if (drm_mm_node_allocated(&node)) {
478 ggtt->vm.insert_page(&ggtt->vm,
479 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
480 node.start, I915_CACHE_NONE, 0);
481 } else {
482 page_base += offset & PAGE_MASK;
483 }
484
485 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
486 user_data, page_length)) {
487 ret = -EFAULT;
488 break;
489 }
490
491 remain -= page_length;
492 user_data += page_length;
493 offset += page_length;
494 }
495
496 i915_gem_object_unlock_fence(obj, fence);
497 out_unpin:
498 if (drm_mm_node_allocated(&node)) {
499 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
500 remove_mappable_node(ggtt, &node);
501 } else {
502 i915_vma_unpin(vma);
503 }
504 out_rpm:
505 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
506 return ret;
507 }
508
509 /**
510 * Reads data from the object referenced by handle.
511 * @dev: drm device pointer
512 * @data: ioctl data blob
513 * @file: drm file pointer
514 *
515 * On error, the contents of *data are undefined.
516 */
517 int
518 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
519 struct drm_file *file)
520 {
521 struct drm_i915_gem_pread *args = data;
522 struct drm_i915_gem_object *obj;
523 int ret;
524
525 if (args->size == 0)
526 return 0;
527
528 if (!access_ok(u64_to_user_ptr(args->data_ptr),
529 args->size))
530 return -EFAULT;
531
532 obj = i915_gem_object_lookup(file, args->handle);
533 if (!obj)
534 return -ENOENT;
535
536 /* Bounds check source. */
537 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
538 ret = -EINVAL;
539 goto out;
540 }
541
542 trace_i915_gem_object_pread(obj, args->offset, args->size);
543
544 ret = i915_gem_object_wait(obj,
545 I915_WAIT_INTERRUPTIBLE,
546 MAX_SCHEDULE_TIMEOUT);
547 if (ret)
548 goto out;
549
550 ret = i915_gem_object_pin_pages(obj);
551 if (ret)
552 goto out;
553
554 ret = i915_gem_shmem_pread(obj, args);
555 if (ret == -EFAULT || ret == -ENODEV)
556 ret = i915_gem_gtt_pread(obj, args);
557
558 i915_gem_object_unpin_pages(obj);
559 out:
560 i915_gem_object_put(obj);
561 return ret;
562 }
563
564 /* This is the fast write path which cannot handle
565 * page faults in the source data
566 */
567
568 static inline bool
569 ggtt_write(struct io_mapping *mapping,
570 loff_t base, int offset,
571 char __user *user_data, int length)
572 {
573 void __iomem *vaddr;
574 unsigned long unwritten;
575
576 /* We can use the cpu mem copy function because this is X86. */
577 vaddr = io_mapping_map_atomic_wc(mapping, base);
578 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
579 user_data, length);
580 io_mapping_unmap_atomic(vaddr);
581 if (unwritten) {
582 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
583 unwritten = copy_from_user((void __force *)vaddr + offset,
584 user_data, length);
585 io_mapping_unmap(vaddr);
586 }
587
588 return unwritten;
589 }
590
591 /**
592 * This is the fast pwrite path, where we copy the data directly from the
593 * user into the GTT, uncached.
594 * @obj: i915 GEM object
595 * @args: pwrite arguments structure
596 */
597 static int
598 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
599 const struct drm_i915_gem_pwrite *args)
600 {
601 struct drm_i915_private *i915 = to_i915(obj->base.dev);
602 struct i915_ggtt *ggtt = &i915->ggtt;
603 struct intel_runtime_pm *rpm = &i915->runtime_pm;
604 intel_wakeref_t wakeref;
605 struct drm_mm_node node;
606 struct dma_fence *fence;
607 struct i915_vma *vma;
608 u64 remain, offset;
609 void __user *user_data;
610 int ret;
611
612 if (i915_gem_object_has_struct_page(obj)) {
613 /*
614 * Avoid waking the device up if we can fallback, as
615 * waking/resuming is very slow (worst-case 10-100 ms
616 * depending on PCI sleeps and our own resume time).
617 * This easily dwarfs any performance advantage from
618 * using the cache bypass of indirect GGTT access.
619 */
620 wakeref = intel_runtime_pm_get_if_in_use(rpm);
621 if (!wakeref)
622 return -EFAULT;
623 } else {
624 /* No backing pages, no fallback, we must force GGTT access */
625 wakeref = intel_runtime_pm_get(rpm);
626 }
627
628 vma = ERR_PTR(-ENODEV);
629 if (!i915_gem_object_is_tiled(obj))
630 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
631 PIN_MAPPABLE |
632 PIN_NONBLOCK /* NOWARN */ |
633 PIN_NOEVICT);
634 if (!IS_ERR(vma)) {
635 node.start = i915_ggtt_offset(vma);
636 node.flags = 0;
637 } else {
638 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
639 if (ret)
640 goto out_rpm;
641 GEM_BUG_ON(!drm_mm_node_allocated(&node));
642 }
643
644 ret = i915_gem_object_lock_interruptible(obj);
645 if (ret)
646 goto out_unpin;
647
648 ret = i915_gem_object_set_to_gtt_domain(obj, true);
649 if (ret) {
650 i915_gem_object_unlock(obj);
651 goto out_unpin;
652 }
653
654 fence = i915_gem_object_lock_fence(obj);
655 i915_gem_object_unlock(obj);
656 if (!fence) {
657 ret = -ENOMEM;
658 goto out_unpin;
659 }
660
661 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
662
663 user_data = u64_to_user_ptr(args->data_ptr);
664 offset = args->offset;
665 remain = args->size;
666 while (remain) {
667 /* Operation in this page
668 *
669 * page_base = page offset within aperture
670 * page_offset = offset within page
671 * page_length = bytes to copy for this page
672 */
673 u32 page_base = node.start;
674 unsigned int page_offset = offset_in_page(offset);
675 unsigned int page_length = PAGE_SIZE - page_offset;
676 page_length = remain < page_length ? remain : page_length;
677 if (drm_mm_node_allocated(&node)) {
678 /* flush the write before we modify the GGTT */
679 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
680 ggtt->vm.insert_page(&ggtt->vm,
681 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
682 node.start, I915_CACHE_NONE, 0);
683 wmb(); /* flush modifications to the GGTT (insert_page) */
684 } else {
685 page_base += offset & PAGE_MASK;
686 }
687 /* If we get a fault while copying data, then (presumably) our
688 * source page isn't available. Return the error and we'll
689 * retry in the slow path.
690 * If the object is non-shmem backed, we retry again with the
691 * path that handles page fault.
692 */
693 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
694 user_data, page_length)) {
695 ret = -EFAULT;
696 break;
697 }
698
699 remain -= page_length;
700 user_data += page_length;
701 offset += page_length;
702 }
703
704 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
705 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
706
707 i915_gem_object_unlock_fence(obj, fence);
708 out_unpin:
709 if (drm_mm_node_allocated(&node)) {
710 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
711 remove_mappable_node(ggtt, &node);
712 } else {
713 i915_vma_unpin(vma);
714 }
715 out_rpm:
716 intel_runtime_pm_put(rpm, wakeref);
717 return ret;
718 }
719
720 /* Per-page copy function for the shmem pwrite fastpath.
721 * Flushes invalid cachelines before writing to the target if
722 * needs_clflush_before is set and flushes out any written cachelines after
723 * writing if needs_clflush is set.
724 */
725 static int
726 shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
727 bool needs_clflush_before,
728 bool needs_clflush_after)
729 {
730 #ifdef __NetBSD__
731 return -EFAULT;
732 #else
733 char *vaddr;
734 int ret;
735
736 vaddr = kmap(page);
737
738 if (needs_clflush_before)
739 drm_clflush_virt_range(vaddr + offset, len);
740
741 ret = __copy_from_user(vaddr + offset, user_data, len);
742 if (!ret && needs_clflush_after)
743 drm_clflush_virt_range(vaddr + offset, len);
744
745 kunmap(page);
746
747 return ret ? -EFAULT : 0;
748 #endif
749 }
750
751 static int
752 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
753 const struct drm_i915_gem_pwrite *args)
754 {
755 unsigned int partial_cacheline_write;
756 unsigned int needs_clflush;
757 unsigned int offset, idx;
758 struct dma_fence *fence;
759 void __user *user_data;
760 u64 remain;
761 int ret;
762
763 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
764 if (ret)
765 return ret;
766
767 fence = i915_gem_object_lock_fence(obj);
768 i915_gem_object_finish_access(obj);
769 if (!fence)
770 return -ENOMEM;
771
772 /* If we don't overwrite a cacheline completely we need to be
773 * careful to have up-to-date data by first clflushing. Don't
774 * overcomplicate things and flush the entire patch.
775 */
776 partial_cacheline_write = 0;
777 if (needs_clflush & CLFLUSH_BEFORE)
778 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
779
780 user_data = u64_to_user_ptr(args->data_ptr);
781 remain = args->size;
782 offset = offset_in_page(args->offset);
783 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
784 struct page *page = i915_gem_object_get_page(obj, idx);
785 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
786
787 ret = shmem_pwrite(page, offset, length, user_data,
788 (offset | length) & partial_cacheline_write,
789 needs_clflush & CLFLUSH_AFTER);
790 if (ret)
791 break;
792
793 remain -= length;
794 user_data += length;
795 offset = 0;
796 }
797
798 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
799 i915_gem_object_unlock_fence(obj, fence);
800
801 return ret;
802 }
803
804 /**
805 * Writes data to the object referenced by handle.
806 * @dev: drm device
807 * @data: ioctl data blob
808 * @file: drm file
809 *
810 * On error, the contents of the buffer that were to be modified are undefined.
811 */
812 int
813 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
814 struct drm_file *file)
815 {
816 struct drm_i915_gem_pwrite *args = data;
817 struct drm_i915_gem_object *obj;
818 int ret;
819
820 if (args->size == 0)
821 return 0;
822
823 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
824 return -EFAULT;
825
826 obj = i915_gem_object_lookup(file, args->handle);
827 if (!obj)
828 return -ENOENT;
829
830 /* Bounds check destination. */
831 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
832 ret = -EINVAL;
833 goto err;
834 }
835
836 /* Writes not allowed into this read-only object */
837 if (i915_gem_object_is_readonly(obj)) {
838 ret = -EINVAL;
839 goto err;
840 }
841
842 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
843
844 ret = -ENODEV;
845 if (obj->ops->pwrite)
846 ret = obj->ops->pwrite(obj, args);
847 if (ret != -ENODEV)
848 goto err;
849
850 ret = i915_gem_object_wait(obj,
851 I915_WAIT_INTERRUPTIBLE |
852 I915_WAIT_ALL,
853 MAX_SCHEDULE_TIMEOUT);
854 if (ret)
855 goto err;
856
857 ret = i915_gem_object_pin_pages(obj);
858 if (ret)
859 goto err;
860
861 ret = -EFAULT;
862 /* We can only do the GTT pwrite on untiled buffers, as otherwise
863 * it would end up going through the fenced access, and we'll get
864 * different detiling behavior between reading and writing.
865 * pread/pwrite currently are reading and writing from the CPU
866 * perspective, requiring manual detiling by the client.
867 */
868 if (!i915_gem_object_has_struct_page(obj) ||
869 cpu_write_needs_clflush(obj))
870 /* Note that the gtt paths might fail with non-page-backed user
871 * pointers (e.g. gtt mappings when moving data between
872 * textures). Fallback to the shmem path in that case.
873 */
874 ret = i915_gem_gtt_pwrite_fast(obj, args);
875
876 if (ret == -EFAULT || ret == -ENOSPC) {
877 if (i915_gem_object_has_struct_page(obj))
878 ret = i915_gem_shmem_pwrite(obj, args);
879 else
880 ret = i915_gem_phys_pwrite(obj, args, file);
881 }
882
883 i915_gem_object_unpin_pages(obj);
884 err:
885 i915_gem_object_put(obj);
886 return ret;
887 }
888
889 /**
890 * Called when user space has done writes to this buffer
891 * @dev: drm device
892 * @data: ioctl data blob
893 * @file: drm file
894 */
895 int
896 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
897 struct drm_file *file)
898 {
899 struct drm_i915_gem_sw_finish *args = data;
900 struct drm_i915_gem_object *obj;
901
902 obj = i915_gem_object_lookup(file, args->handle);
903 if (!obj)
904 return -ENOENT;
905
906 /*
907 * Proxy objects are barred from CPU access, so there is no
908 * need to ban sw_finish as it is a nop.
909 */
910
911 /* Pinned buffers may be scanout, so flush the cache */
912 i915_gem_object_flush_if_display(obj);
913 i915_gem_object_put(obj);
914
915 return 0;
916 }
917
918 void i915_gem_runtime_suspend(struct drm_i915_private *i915)
919 {
920 struct drm_i915_gem_object *obj, *on;
921 int i;
922
923 /*
924 * Only called during RPM suspend. All users of the userfault_list
925 * must be holding an RPM wakeref to ensure that this can not
926 * run concurrently with themselves (and use the struct_mutex for
927 * protection between themselves).
928 */
929
930 list_for_each_entry_safe(obj, on,
931 &i915->ggtt.userfault_list, userfault_link)
932 __i915_gem_object_release_mmap_gtt(obj);
933
934 /*
935 * The fence will be lost when the device powers down. If any were
936 * in use by hardware (i.e. they are pinned), we should not be powering
937 * down! All other fences will be reacquired by the user upon waking.
938 */
939 for (i = 0; i < i915->ggtt.num_fences; i++) {
940 struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
941
942 /*
943 * Ideally we want to assert that the fence register is not
944 * live at this point (i.e. that no piece of code will be
945 * trying to write through fence + GTT, as that both violates
946 * our tracking of activity and associated locking/barriers,
947 * but also is illegal given that the hw is powered down).
948 *
949 * Previously we used reg->pin_count as a "liveness" indicator.
950 * That is not sufficient, and we need a more fine-grained
951 * tool if we want to have a sanity check here.
952 */
953
954 if (!reg->vma)
955 continue;
956
957 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
958 reg->dirty = true;
959 }
960 }
961
962 struct i915_vma *
963 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
964 const struct i915_ggtt_view *view,
965 u64 size,
966 u64 alignment,
967 u64 flags)
968 {
969 struct drm_i915_private *i915 = to_i915(obj->base.dev);
970 struct i915_ggtt *ggtt = &i915->ggtt;
971 struct i915_vma *vma;
972 int ret;
973
974 if (i915_gem_object_never_bind_ggtt(obj))
975 return ERR_PTR(-ENODEV);
976
977 if (flags & PIN_MAPPABLE &&
978 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
979 /*
980 * If the required space is larger than the available
981 * aperture, we will not able to find a slot for the
982 * object and unbinding the object now will be in
983 * vain. Worse, doing so may cause us to ping-pong
984 * the object in and out of the Global GTT and
985 * waste a lot of cycles under the mutex.
986 */
987 if (obj->base.size > ggtt->mappable_end)
988 return ERR_PTR(-E2BIG);
989
990 /*
991 * If NONBLOCK is set the caller is optimistically
992 * trying to cache the full object within the mappable
993 * aperture, and *must* have a fallback in place for
994 * situations where we cannot bind the object. We
995 * can be a little more lax here and use the fallback
996 * more often to avoid costly migrations of ourselves
997 * and other objects within the aperture.
998 *
999 * Half-the-aperture is used as a simple heuristic.
1000 * More interesting would to do search for a free
1001 * block prior to making the commitment to unbind.
1002 * That caters for the self-harm case, and with a
1003 * little more heuristics (e.g. NOFAULT, NOEVICT)
1004 * we could try to minimise harm to others.
1005 */
1006 if (flags & PIN_NONBLOCK &&
1007 obj->base.size > ggtt->mappable_end / 2)
1008 return ERR_PTR(-ENOSPC);
1009 }
1010
1011 vma = i915_vma_instance(obj, &ggtt->vm, view);
1012 if (IS_ERR(vma))
1013 return vma;
1014
1015 if (i915_vma_misplaced(vma, size, alignment, flags)) {
1016 if (flags & PIN_NONBLOCK) {
1017 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
1018 return ERR_PTR(-ENOSPC);
1019
1020 if (flags & PIN_MAPPABLE &&
1021 vma->fence_size > ggtt->mappable_end / 2)
1022 return ERR_PTR(-ENOSPC);
1023 }
1024
1025 ret = i915_vma_unbind(vma);
1026 if (ret)
1027 return ERR_PTR(ret);
1028 }
1029
1030 if (vma->fence && !i915_gem_object_is_tiled(obj)) {
1031 mutex_lock(&ggtt->vm.mutex);
1032 ret = i915_vma_revoke_fence(vma);
1033 mutex_unlock(&ggtt->vm.mutex);
1034 if (ret)
1035 return ERR_PTR(ret);
1036 }
1037
1038 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
1039 if (ret)
1040 return ERR_PTR(ret);
1041
1042 return vma;
1043 }
1044
1045 int
1046 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
1047 struct drm_file *file_priv)
1048 {
1049 struct drm_i915_private *i915 = to_i915(dev);
1050 struct drm_i915_gem_madvise *args = data;
1051 struct drm_i915_gem_object *obj;
1052 int err;
1053
1054 switch (args->madv) {
1055 case I915_MADV_DONTNEED:
1056 case I915_MADV_WILLNEED:
1057 break;
1058 default:
1059 return -EINVAL;
1060 }
1061
1062 obj = i915_gem_object_lookup(file_priv, args->handle);
1063 if (!obj)
1064 return -ENOENT;
1065
1066 err = mutex_lock_interruptible(&obj->mm.lock);
1067 if (err)
1068 goto out;
1069
1070 if (i915_gem_object_has_pages(obj) &&
1071 i915_gem_object_is_tiled(obj) &&
1072 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
1073 if (obj->mm.madv == I915_MADV_WILLNEED) {
1074 GEM_BUG_ON(!obj->mm.quirked);
1075 __i915_gem_object_unpin_pages(obj);
1076 obj->mm.quirked = false;
1077 }
1078 if (args->madv == I915_MADV_WILLNEED) {
1079 GEM_BUG_ON(obj->mm.quirked);
1080 __i915_gem_object_pin_pages(obj);
1081 obj->mm.quirked = true;
1082 }
1083 }
1084
1085 if (obj->mm.madv != __I915_MADV_PURGED)
1086 obj->mm.madv = args->madv;
1087
1088 if (i915_gem_object_has_pages(obj)) {
1089 struct list_head *list;
1090
1091 if (i915_gem_object_is_shrinkable(obj)) {
1092 unsigned long flags;
1093
1094 spin_lock_irqsave(&i915->mm.obj_lock, flags);
1095
1096 if (obj->mm.madv != I915_MADV_WILLNEED)
1097 list = &i915->mm.purge_list;
1098 else
1099 list = &i915->mm.shrink_list;
1100 list_move_tail(&obj->mm.link, list);
1101
1102 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
1103 }
1104 }
1105
1106 /* if the object is no longer attached, discard its backing storage */
1107 if (obj->mm.madv == I915_MADV_DONTNEED &&
1108 !i915_gem_object_has_pages(obj))
1109 i915_gem_object_truncate(obj);
1110
1111 args->retained = obj->mm.madv != __I915_MADV_PURGED;
1112 mutex_unlock(&obj->mm.lock);
1113
1114 out:
1115 i915_gem_object_put(obj);
1116 return err;
1117 }
1118
1119 int i915_gem_init(struct drm_i915_private *dev_priv)
1120 {
1121 int ret;
1122
1123 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1124 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
1125 mkwrite_device_info(dev_priv)->page_sizes =
1126 I915_GTT_PAGE_SIZE_4K;
1127
1128 ret = i915_gem_init_userptr(dev_priv);
1129 if (ret)
1130 return ret;
1131
1132 intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1133 intel_wopcm_init(&dev_priv->wopcm);
1134
1135 ret = i915_init_ggtt(dev_priv);
1136 if (ret) {
1137 GEM_BUG_ON(ret == -EIO);
1138 goto err_unlock;
1139 }
1140
1141 /*
1142 * Despite its name intel_init_clock_gating applies both display
1143 * clock gating workarounds; GT mmio workarounds and the occasional
1144 * GT power context workaround. Worse, sometimes it includes a context
1145 * register workaround which we need to apply before we record the
1146 * default HW state for all contexts.
1147 *
1148 * FIXME: break up the workarounds and apply them at the right time!
1149 */
1150 intel_init_clock_gating(dev_priv);
1151
1152 ret = intel_gt_init(&dev_priv->gt);
1153 if (ret)
1154 goto err_unlock;
1155
1156 return 0;
1157
1158 /*
1159 * Unwinding is complicated by that we want to handle -EIO to mean
1160 * disable GPU submission but keep KMS alive. We want to mark the
1161 * HW as irrevisibly wedged, but keep enough state around that the
1162 * driver doesn't explode during runtime.
1163 */
1164 err_unlock:
1165 i915_gem_drain_workqueue(dev_priv);
1166
1167 if (ret != -EIO) {
1168 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1169 i915_gem_cleanup_userptr(dev_priv);
1170 }
1171
1172 if (ret == -EIO) {
1173 /*
1174 * Allow engines or uC initialisation to fail by marking the GPU
1175 * as wedged. But we only want to do this when the GPU is angry,
1176 * for all other failure, such as an allocation failure, bail.
1177 */
1178 if (!intel_gt_is_wedged(&dev_priv->gt)) {
1179 i915_probe_error(dev_priv,
1180 "Failed to initialize GPU, declaring it wedged!\n");
1181 intel_gt_set_wedged(&dev_priv->gt);
1182 }
1183
1184 /* Minimal basic recovery for KMS */
1185 ret = i915_ggtt_enable_hw(dev_priv);
1186 i915_gem_restore_gtt_mappings(dev_priv);
1187 i915_gem_restore_fences(&dev_priv->ggtt);
1188 intel_init_clock_gating(dev_priv);
1189 }
1190
1191 i915_gem_drain_freed_objects(dev_priv);
1192 return ret;
1193 }
1194
1195 void i915_gem_driver_register(struct drm_i915_private *i915)
1196 {
1197 i915_gem_driver_register__shrinker(i915);
1198
1199 intel_engines_driver_register(i915);
1200 }
1201
1202 void i915_gem_driver_unregister(struct drm_i915_private *i915)
1203 {
1204 i915_gem_driver_unregister__shrinker(i915);
1205 }
1206
1207 void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1208 {
1209 intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1210
1211 i915_gem_suspend_late(dev_priv);
1212 intel_gt_driver_remove(&dev_priv->gt);
1213 dev_priv->uabi_engines = RB_ROOT;
1214
1215 /* Flush any outstanding unpin_work. */
1216 i915_gem_drain_workqueue(dev_priv);
1217
1218 i915_gem_drain_freed_objects(dev_priv);
1219 }
1220
1221 void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1222 {
1223 i915_gem_driver_release__contexts(dev_priv);
1224
1225 intel_gt_driver_release(&dev_priv->gt);
1226
1227 intel_wa_list_free(&dev_priv->gt_wa_list);
1228
1229 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1230 i915_gem_cleanup_userptr(dev_priv);
1231
1232 i915_gem_drain_freed_objects(dev_priv);
1233
1234 WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
1235 }
1236
1237 static void i915_gem_init__mm(struct drm_i915_private *i915)
1238 {
1239 spin_lock_init(&i915->mm.obj_lock);
1240
1241 init_llist_head(&i915->mm.free_list);
1242
1243 INIT_LIST_HEAD(&i915->mm.purge_list);
1244 INIT_LIST_HEAD(&i915->mm.shrink_list);
1245
1246 i915_gem_init__objects(i915);
1247 }
1248
1249 void i915_gem_init_early(struct drm_i915_private *dev_priv)
1250 {
1251 i915_gem_init__mm(dev_priv);
1252 i915_gem_init__contexts(dev_priv);
1253
1254 spin_lock_init(&dev_priv->fb_tracking.lock);
1255 }
1256
1257 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1258 {
1259 i915_gem_drain_freed_objects(dev_priv);
1260 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1261 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1262 WARN_ON(dev_priv->mm.shrink_count);
1263 }
1264
1265 int i915_gem_freeze(struct drm_i915_private *dev_priv)
1266 {
1267 /* Discard all purgeable objects, let userspace recover those as
1268 * required after resuming.
1269 */
1270 i915_gem_shrink_all(dev_priv);
1271
1272 return 0;
1273 }
1274
1275 int i915_gem_freeze_late(struct drm_i915_private *i915)
1276 {
1277 struct drm_i915_gem_object *obj;
1278 intel_wakeref_t wakeref;
1279
1280 /*
1281 * Called just before we write the hibernation image.
1282 *
1283 * We need to update the domain tracking to reflect that the CPU
1284 * will be accessing all the pages to create and restore from the
1285 * hibernation, and so upon restoration those pages will be in the
1286 * CPU domain.
1287 *
1288 * To make sure the hibernation image contains the latest state,
1289 * we update that state just before writing out the image.
1290 *
1291 * To try and reduce the hibernation image, we manually shrink
1292 * the objects as well, see i915_gem_freeze()
1293 */
1294
1295 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1296
1297 i915_gem_shrink(i915, -1UL, NULL, ~0);
1298 i915_gem_drain_freed_objects(i915);
1299
1300 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
1301 i915_gem_object_lock(obj);
1302 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
1303 i915_gem_object_unlock(obj);
1304 }
1305
1306 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1307
1308 return 0;
1309 }
1310
1311 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1312 {
1313 struct drm_i915_file_private *file_priv = file->driver_priv;
1314 struct i915_request *request;
1315
1316 /* Clean up our request list when the client is going away, so that
1317 * later retire_requests won't dereference our soon-to-be-gone
1318 * file_priv.
1319 */
1320 spin_lock(&file_priv->mm.lock);
1321 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1322 request->file_priv = NULL;
1323 spin_unlock(&file_priv->mm.lock);
1324 }
1325
1326 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1327 {
1328 struct drm_i915_file_private *file_priv;
1329 int ret;
1330
1331 DRM_DEBUG("\n");
1332
1333 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1334 if (!file_priv)
1335 return -ENOMEM;
1336
1337 file->driver_priv = file_priv;
1338 file_priv->dev_priv = i915;
1339 file_priv->file = file;
1340
1341 spin_lock_init(&file_priv->mm.lock);
1342 INIT_LIST_HEAD(&file_priv->mm.request_list);
1343
1344 file_priv->bsd_engine = -1;
1345 file_priv->hang_timestamp = jiffies;
1346
1347 ret = i915_gem_context_open(i915, file);
1348 if (ret)
1349 kfree(file_priv);
1350
1351 return ret;
1352 }
1353
1354 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1355 #include "selftests/mock_gem_device.c"
1356 #include "selftests/i915_gem.c"
1357 #endif
1358