i915_gem.c revision 1.63 1 /* $NetBSD: i915_gem.c,v 1.63 2021/12/19 01:24:25 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.63 2021/12/19 01:24:25 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 = ALIGN(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 #ifdef __NetBSD__ /* ALIGN means something else. */
292 args->pitch = round_up(args->pitch, 4096);
293 #else
294 args->pitch = ALIGN(args->pitch, 4096);
295 #endif
296
297 if (args->pitch < args->width)
298 return -EINVAL;
299
300 args->size = mul_u32_u32(args->pitch, args->height);
301
302 mem_type = INTEL_MEMORY_SYSTEM;
303 if (HAS_LMEM(to_i915(dev)))
304 mem_type = INTEL_MEMORY_LOCAL;
305
306 return i915_gem_create(file,
307 intel_memory_region_by_type(to_i915(dev),
308 mem_type),
309 &args->size, &args->handle);
310 }
311
312 /**
313 * Creates a new mm object and returns a handle to it.
314 * @dev: drm device pointer
315 * @data: ioctl data blob
316 * @file: drm file pointer
317 */
318 int
319 i915_gem_create_ioctl(struct drm_device *dev, void *data,
320 struct drm_file *file)
321 {
322 struct drm_i915_private *i915 = to_i915(dev);
323 struct drm_i915_gem_create *args = data;
324
325 i915_gem_flush_free_objects(i915);
326
327 return i915_gem_create(file,
328 intel_memory_region_by_type(i915,
329 INTEL_MEMORY_SYSTEM),
330 &args->size, &args->handle);
331 }
332
333 static int
334 shmem_pread(struct page *page, int offset, int len, char __user *user_data,
335 bool needs_clflush)
336 {
337 char *vaddr;
338 int ret;
339
340 vaddr = kmap(page);
341
342 if (needs_clflush)
343 drm_clflush_virt_range(vaddr + offset, len);
344
345 ret = __copy_to_user(user_data, vaddr + offset, len);
346
347 kunmap(page);
348
349 return ret ? -EFAULT : 0;
350 }
351
352 static int
353 i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
354 struct drm_i915_gem_pread *args)
355 {
356 unsigned int needs_clflush;
357 unsigned int idx, offset;
358 struct dma_fence *fence;
359 char __user *user_data;
360 u64 remain;
361 int ret;
362
363 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
364
365 if (ret)
366 return ret;
367
368 fence = i915_gem_object_lock_fence(obj);
369 i915_gem_object_finish_access(obj);
370 if (!fence)
371 return -ENOMEM;
372
373 remain = args->size;
374 user_data = u64_to_user_ptr(args->data_ptr);
375 offset = offset_in_page(args->offset);
376 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
377 struct page *page = i915_gem_object_get_page(obj, idx);
378 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
379
380 ret = shmem_pread(page, offset, length, user_data,
381 needs_clflush);
382 if (ret)
383 break;
384
385 remain -= length;
386 user_data += length;
387 offset = 0;
388 }
389
390 i915_gem_object_unlock_fence(obj, fence);
391 return ret;
392 }
393
394 static inline bool
395 gtt_user_read(struct io_mapping *mapping,
396 loff_t base, int offset,
397 char __user *user_data, int length)
398 {
399 void __iomem *vaddr;
400 unsigned long unwritten;
401
402 /* We can use the cpu mem copy function because this is X86. */
403 vaddr = io_mapping_map_atomic_wc(mapping, base);
404 unwritten = __copy_to_user_inatomic(user_data,
405 (void __force *)vaddr + offset,
406 length);
407 io_mapping_unmap_atomic(vaddr);
408 if (unwritten) {
409 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
410 unwritten = copy_to_user(user_data,
411 (void __force *)vaddr + offset,
412 length);
413 io_mapping_unmap(vaddr);
414 }
415 return unwritten;
416 }
417
418 static int
419 i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
420 const struct drm_i915_gem_pread *args)
421 {
422 struct drm_i915_private *i915 = to_i915(obj->base.dev);
423 struct i915_ggtt *ggtt = &i915->ggtt;
424 intel_wakeref_t wakeref;
425 struct drm_mm_node node;
426 struct dma_fence *fence;
427 void __user *user_data;
428 struct i915_vma *vma;
429 u64 remain, offset;
430 int ret;
431
432 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
433 vma = ERR_PTR(-ENODEV);
434 if (!i915_gem_object_is_tiled(obj))
435 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
436 PIN_MAPPABLE |
437 PIN_NONBLOCK /* NOWARN */ |
438 PIN_NOEVICT);
439 if (!IS_ERR(vma)) {
440 node.start = i915_ggtt_offset(vma);
441 node.flags = 0;
442 } else {
443 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
444 if (ret)
445 goto out_rpm;
446 GEM_BUG_ON(!drm_mm_node_allocated(&node));
447 }
448
449 ret = i915_gem_object_lock_interruptible(obj);
450 if (ret)
451 goto out_unpin;
452
453 ret = i915_gem_object_set_to_gtt_domain(obj, false);
454 if (ret) {
455 i915_gem_object_unlock(obj);
456 goto out_unpin;
457 }
458
459 fence = i915_gem_object_lock_fence(obj);
460 i915_gem_object_unlock(obj);
461 if (!fence) {
462 ret = -ENOMEM;
463 goto out_unpin;
464 }
465
466 user_data = u64_to_user_ptr(args->data_ptr);
467 remain = args->size;
468 offset = args->offset;
469
470 while (remain > 0) {
471 /* Operation in this page
472 *
473 * page_base = page offset within aperture
474 * page_offset = offset within page
475 * page_length = bytes to copy for this page
476 */
477 u32 page_base = node.start;
478 unsigned page_offset = offset_in_page(offset);
479 unsigned page_length = PAGE_SIZE - page_offset;
480 page_length = remain < page_length ? remain : page_length;
481 if (drm_mm_node_allocated(&node)) {
482 ggtt->vm.insert_page(&ggtt->vm,
483 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
484 node.start, I915_CACHE_NONE, 0);
485 } else {
486 page_base += offset & PAGE_MASK;
487 }
488
489 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
490 user_data, page_length)) {
491 ret = -EFAULT;
492 break;
493 }
494
495 remain -= page_length;
496 user_data += page_length;
497 offset += page_length;
498 }
499
500 i915_gem_object_unlock_fence(obj, fence);
501 out_unpin:
502 if (drm_mm_node_allocated(&node)) {
503 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
504 remove_mappable_node(ggtt, &node);
505 } else {
506 i915_vma_unpin(vma);
507 }
508 out_rpm:
509 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
510 return ret;
511 }
512
513 /**
514 * Reads data from the object referenced by handle.
515 * @dev: drm device pointer
516 * @data: ioctl data blob
517 * @file: drm file pointer
518 *
519 * On error, the contents of *data are undefined.
520 */
521 int
522 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
523 struct drm_file *file)
524 {
525 struct drm_i915_gem_pread *args = data;
526 struct drm_i915_gem_object *obj;
527 int ret;
528
529 if (args->size == 0)
530 return 0;
531
532 if (!access_ok(u64_to_user_ptr(args->data_ptr),
533 args->size))
534 return -EFAULT;
535
536 obj = i915_gem_object_lookup(file, args->handle);
537 if (!obj)
538 return -ENOENT;
539
540 /* Bounds check source. */
541 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
542 ret = -EINVAL;
543 goto out;
544 }
545
546 trace_i915_gem_object_pread(obj, args->offset, args->size);
547
548 ret = i915_gem_object_wait(obj,
549 I915_WAIT_INTERRUPTIBLE,
550 MAX_SCHEDULE_TIMEOUT);
551 if (ret)
552 goto out;
553
554 ret = i915_gem_object_pin_pages(obj);
555 if (ret)
556 goto out;
557
558 ret = i915_gem_shmem_pread(obj, args);
559 if (ret == -EFAULT || ret == -ENODEV)
560 ret = i915_gem_gtt_pread(obj, args);
561
562 i915_gem_object_unpin_pages(obj);
563 out:
564 i915_gem_object_put(obj);
565 return ret;
566 }
567
568 /* This is the fast write path which cannot handle
569 * page faults in the source data
570 */
571
572 static inline bool
573 ggtt_write(struct io_mapping *mapping,
574 loff_t base, int offset,
575 char __user *user_data, int length)
576 {
577 void __iomem *vaddr;
578 unsigned long unwritten;
579
580 /* We can use the cpu mem copy function because this is X86. */
581 vaddr = io_mapping_map_atomic_wc(mapping, base);
582 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
583 user_data, length);
584 io_mapping_unmap_atomic(vaddr);
585 if (unwritten) {
586 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
587 unwritten = copy_from_user((void __force *)vaddr + offset,
588 user_data, length);
589 io_mapping_unmap(vaddr);
590 }
591
592 return unwritten;
593 }
594
595 /**
596 * This is the fast pwrite path, where we copy the data directly from the
597 * user into the GTT, uncached.
598 * @obj: i915 GEM object
599 * @args: pwrite arguments structure
600 */
601 static int
602 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
603 const struct drm_i915_gem_pwrite *args)
604 {
605 struct drm_i915_private *i915 = to_i915(obj->base.dev);
606 struct i915_ggtt *ggtt = &i915->ggtt;
607 struct intel_runtime_pm *rpm = &i915->runtime_pm;
608 intel_wakeref_t wakeref;
609 struct drm_mm_node node;
610 struct dma_fence *fence;
611 struct i915_vma *vma;
612 u64 remain, offset;
613 void __user *user_data;
614 int ret;
615
616 if (i915_gem_object_has_struct_page(obj)) {
617 /*
618 * Avoid waking the device up if we can fallback, as
619 * waking/resuming is very slow (worst-case 10-100 ms
620 * depending on PCI sleeps and our own resume time).
621 * This easily dwarfs any performance advantage from
622 * using the cache bypass of indirect GGTT access.
623 */
624 wakeref = intel_runtime_pm_get_if_in_use(rpm);
625 if (!wakeref)
626 return -EFAULT;
627 } else {
628 /* No backing pages, no fallback, we must force GGTT access */
629 wakeref = intel_runtime_pm_get(rpm);
630 }
631
632 vma = ERR_PTR(-ENODEV);
633 if (!i915_gem_object_is_tiled(obj))
634 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
635 PIN_MAPPABLE |
636 PIN_NONBLOCK /* NOWARN */ |
637 PIN_NOEVICT);
638 if (!IS_ERR(vma)) {
639 node.start = i915_ggtt_offset(vma);
640 node.flags = 0;
641 } else {
642 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
643 if (ret)
644 goto out_rpm;
645 GEM_BUG_ON(!drm_mm_node_allocated(&node));
646 }
647
648 ret = i915_gem_object_lock_interruptible(obj);
649 if (ret)
650 goto out_unpin;
651
652 ret = i915_gem_object_set_to_gtt_domain(obj, true);
653 if (ret) {
654 i915_gem_object_unlock(obj);
655 goto out_unpin;
656 }
657
658 fence = i915_gem_object_lock_fence(obj);
659 i915_gem_object_unlock(obj);
660 if (!fence) {
661 ret = -ENOMEM;
662 goto out_unpin;
663 }
664
665 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
666
667 user_data = u64_to_user_ptr(args->data_ptr);
668 offset = args->offset;
669 remain = args->size;
670 while (remain) {
671 /* Operation in this page
672 *
673 * page_base = page offset within aperture
674 * page_offset = offset within page
675 * page_length = bytes to copy for this page
676 */
677 u32 page_base = node.start;
678 unsigned int page_offset = offset_in_page(offset);
679 unsigned int page_length = PAGE_SIZE - page_offset;
680 page_length = remain < page_length ? remain : page_length;
681 if (drm_mm_node_allocated(&node)) {
682 /* flush the write before we modify the GGTT */
683 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
684 ggtt->vm.insert_page(&ggtt->vm,
685 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
686 node.start, I915_CACHE_NONE, 0);
687 wmb(); /* flush modifications to the GGTT (insert_page) */
688 } else {
689 page_base += offset & PAGE_MASK;
690 }
691 /* If we get a fault while copying data, then (presumably) our
692 * source page isn't available. Return the error and we'll
693 * retry in the slow path.
694 * If the object is non-shmem backed, we retry again with the
695 * path that handles page fault.
696 */
697 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
698 user_data, page_length)) {
699 ret = -EFAULT;
700 break;
701 }
702
703 remain -= page_length;
704 user_data += page_length;
705 offset += page_length;
706 }
707
708 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
709 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
710
711 i915_gem_object_unlock_fence(obj, fence);
712 out_unpin:
713 if (drm_mm_node_allocated(&node)) {
714 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
715 remove_mappable_node(ggtt, &node);
716 } else {
717 i915_vma_unpin(vma);
718 }
719 out_rpm:
720 intel_runtime_pm_put(rpm, wakeref);
721 return ret;
722 }
723
724 /* Per-page copy function for the shmem pwrite fastpath.
725 * Flushes invalid cachelines before writing to the target if
726 * needs_clflush_before is set and flushes out any written cachelines after
727 * writing if needs_clflush is set.
728 */
729 static int
730 shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
731 bool needs_clflush_before,
732 bool needs_clflush_after)
733 {
734 #ifdef __NetBSD__
735 return -EFAULT;
736 #else
737 char *vaddr;
738 int ret;
739
740 vaddr = kmap(page);
741
742 if (needs_clflush_before)
743 drm_clflush_virt_range(vaddr + offset, len);
744
745 ret = __copy_from_user(vaddr + offset, user_data, len);
746 if (!ret && needs_clflush_after)
747 drm_clflush_virt_range(vaddr + offset, len);
748
749 kunmap(page);
750
751 return ret ? -EFAULT : 0;
752 #endif
753 }
754
755 static int
756 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
757 const struct drm_i915_gem_pwrite *args)
758 {
759 unsigned int partial_cacheline_write;
760 unsigned int needs_clflush;
761 unsigned int offset, idx;
762 struct dma_fence *fence;
763 void __user *user_data;
764 u64 remain;
765 int ret;
766
767 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
768 if (ret)
769 return ret;
770
771 fence = i915_gem_object_lock_fence(obj);
772 i915_gem_object_finish_access(obj);
773 if (!fence)
774 return -ENOMEM;
775
776 /* If we don't overwrite a cacheline completely we need to be
777 * careful to have up-to-date data by first clflushing. Don't
778 * overcomplicate things and flush the entire patch.
779 */
780 partial_cacheline_write = 0;
781 if (needs_clflush & CLFLUSH_BEFORE)
782 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
783
784 user_data = u64_to_user_ptr(args->data_ptr);
785 remain = args->size;
786 offset = offset_in_page(args->offset);
787 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
788 struct page *page = i915_gem_object_get_page(obj, idx);
789 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
790
791 ret = shmem_pwrite(page, offset, length, user_data,
792 (offset | length) & partial_cacheline_write,
793 needs_clflush & CLFLUSH_AFTER);
794 if (ret)
795 break;
796
797 remain -= length;
798 user_data += length;
799 offset = 0;
800 }
801
802 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
803 i915_gem_object_unlock_fence(obj, fence);
804
805 return ret;
806 }
807
808 /**
809 * Writes data to the object referenced by handle.
810 * @dev: drm device
811 * @data: ioctl data blob
812 * @file: drm file
813 *
814 * On error, the contents of the buffer that were to be modified are undefined.
815 */
816 int
817 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
818 struct drm_file *file)
819 {
820 struct drm_i915_gem_pwrite *args = data;
821 struct drm_i915_gem_object *obj;
822 int ret;
823
824 if (args->size == 0)
825 return 0;
826
827 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
828 return -EFAULT;
829
830 obj = i915_gem_object_lookup(file, args->handle);
831 if (!obj)
832 return -ENOENT;
833
834 /* Bounds check destination. */
835 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
836 ret = -EINVAL;
837 goto err;
838 }
839
840 /* Writes not allowed into this read-only object */
841 if (i915_gem_object_is_readonly(obj)) {
842 ret = -EINVAL;
843 goto err;
844 }
845
846 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
847
848 ret = -ENODEV;
849 if (obj->ops->pwrite)
850 ret = obj->ops->pwrite(obj, args);
851 if (ret != -ENODEV)
852 goto err;
853
854 ret = i915_gem_object_wait(obj,
855 I915_WAIT_INTERRUPTIBLE |
856 I915_WAIT_ALL,
857 MAX_SCHEDULE_TIMEOUT);
858 if (ret)
859 goto err;
860
861 ret = i915_gem_object_pin_pages(obj);
862 if (ret)
863 goto err;
864
865 ret = -EFAULT;
866 /* We can only do the GTT pwrite on untiled buffers, as otherwise
867 * it would end up going through the fenced access, and we'll get
868 * different detiling behavior between reading and writing.
869 * pread/pwrite currently are reading and writing from the CPU
870 * perspective, requiring manual detiling by the client.
871 */
872 if (!i915_gem_object_has_struct_page(obj) ||
873 cpu_write_needs_clflush(obj))
874 /* Note that the gtt paths might fail with non-page-backed user
875 * pointers (e.g. gtt mappings when moving data between
876 * textures). Fallback to the shmem path in that case.
877 */
878 ret = i915_gem_gtt_pwrite_fast(obj, args);
879
880 if (ret == -EFAULT || ret == -ENOSPC) {
881 if (i915_gem_object_has_struct_page(obj))
882 ret = i915_gem_shmem_pwrite(obj, args);
883 else
884 ret = i915_gem_phys_pwrite(obj, args, file);
885 }
886
887 i915_gem_object_unpin_pages(obj);
888 err:
889 i915_gem_object_put(obj);
890 return ret;
891 }
892
893 /**
894 * Called when user space has done writes to this buffer
895 * @dev: drm device
896 * @data: ioctl data blob
897 * @file: drm file
898 */
899 int
900 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
901 struct drm_file *file)
902 {
903 struct drm_i915_gem_sw_finish *args = data;
904 struct drm_i915_gem_object *obj;
905
906 obj = i915_gem_object_lookup(file, args->handle);
907 if (!obj)
908 return -ENOENT;
909
910 /*
911 * Proxy objects are barred from CPU access, so there is no
912 * need to ban sw_finish as it is a nop.
913 */
914
915 /* Pinned buffers may be scanout, so flush the cache */
916 i915_gem_object_flush_if_display(obj);
917 i915_gem_object_put(obj);
918
919 return 0;
920 }
921
922 void i915_gem_runtime_suspend(struct drm_i915_private *i915)
923 {
924 struct drm_i915_gem_object *obj, *on;
925 int i;
926
927 /*
928 * Only called during RPM suspend. All users of the userfault_list
929 * must be holding an RPM wakeref to ensure that this can not
930 * run concurrently with themselves (and use the struct_mutex for
931 * protection between themselves).
932 */
933
934 list_for_each_entry_safe(obj, on,
935 &i915->ggtt.userfault_list, userfault_link)
936 __i915_gem_object_release_mmap_gtt(obj);
937
938 /*
939 * The fence will be lost when the device powers down. If any were
940 * in use by hardware (i.e. they are pinned), we should not be powering
941 * down! All other fences will be reacquired by the user upon waking.
942 */
943 for (i = 0; i < i915->ggtt.num_fences; i++) {
944 struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
945
946 /*
947 * Ideally we want to assert that the fence register is not
948 * live at this point (i.e. that no piece of code will be
949 * trying to write through fence + GTT, as that both violates
950 * our tracking of activity and associated locking/barriers,
951 * but also is illegal given that the hw is powered down).
952 *
953 * Previously we used reg->pin_count as a "liveness" indicator.
954 * That is not sufficient, and we need a more fine-grained
955 * tool if we want to have a sanity check here.
956 */
957
958 if (!reg->vma)
959 continue;
960
961 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
962 reg->dirty = true;
963 }
964 }
965
966 struct i915_vma *
967 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
968 const struct i915_ggtt_view *view,
969 u64 size,
970 u64 alignment,
971 u64 flags)
972 {
973 struct drm_i915_private *i915 = to_i915(obj->base.dev);
974 struct i915_ggtt *ggtt = &i915->ggtt;
975 struct i915_vma *vma;
976 int ret;
977
978 if (i915_gem_object_never_bind_ggtt(obj))
979 return ERR_PTR(-ENODEV);
980
981 if (flags & PIN_MAPPABLE &&
982 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
983 /*
984 * If the required space is larger than the available
985 * aperture, we will not able to find a slot for the
986 * object and unbinding the object now will be in
987 * vain. Worse, doing so may cause us to ping-pong
988 * the object in and out of the Global GTT and
989 * waste a lot of cycles under the mutex.
990 */
991 if (obj->base.size > ggtt->mappable_end)
992 return ERR_PTR(-E2BIG);
993
994 /*
995 * If NONBLOCK is set the caller is optimistically
996 * trying to cache the full object within the mappable
997 * aperture, and *must* have a fallback in place for
998 * situations where we cannot bind the object. We
999 * can be a little more lax here and use the fallback
1000 * more often to avoid costly migrations of ourselves
1001 * and other objects within the aperture.
1002 *
1003 * Half-the-aperture is used as a simple heuristic.
1004 * More interesting would to do search for a free
1005 * block prior to making the commitment to unbind.
1006 * That caters for the self-harm case, and with a
1007 * little more heuristics (e.g. NOFAULT, NOEVICT)
1008 * we could try to minimise harm to others.
1009 */
1010 if (flags & PIN_NONBLOCK &&
1011 obj->base.size > ggtt->mappable_end / 2)
1012 return ERR_PTR(-ENOSPC);
1013 }
1014
1015 vma = i915_vma_instance(obj, &ggtt->vm, view);
1016 if (IS_ERR(vma))
1017 return vma;
1018
1019 if (i915_vma_misplaced(vma, size, alignment, flags)) {
1020 if (flags & PIN_NONBLOCK) {
1021 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
1022 return ERR_PTR(-ENOSPC);
1023
1024 if (flags & PIN_MAPPABLE &&
1025 vma->fence_size > ggtt->mappable_end / 2)
1026 return ERR_PTR(-ENOSPC);
1027 }
1028
1029 ret = i915_vma_unbind(vma);
1030 if (ret)
1031 return ERR_PTR(ret);
1032 }
1033
1034 if (vma->fence && !i915_gem_object_is_tiled(obj)) {
1035 mutex_lock(&ggtt->vm.mutex);
1036 ret = i915_vma_revoke_fence(vma);
1037 mutex_unlock(&ggtt->vm.mutex);
1038 if (ret)
1039 return ERR_PTR(ret);
1040 }
1041
1042 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
1043 if (ret)
1044 return ERR_PTR(ret);
1045
1046 return vma;
1047 }
1048
1049 int
1050 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
1051 struct drm_file *file_priv)
1052 {
1053 struct drm_i915_private *i915 = to_i915(dev);
1054 struct drm_i915_gem_madvise *args = data;
1055 struct drm_i915_gem_object *obj;
1056 int err;
1057
1058 switch (args->madv) {
1059 case I915_MADV_DONTNEED:
1060 case I915_MADV_WILLNEED:
1061 break;
1062 default:
1063 return -EINVAL;
1064 }
1065
1066 obj = i915_gem_object_lookup(file_priv, args->handle);
1067 if (!obj)
1068 return -ENOENT;
1069
1070 err = mutex_lock_interruptible(&obj->mm.lock);
1071 if (err)
1072 goto out;
1073
1074 if (i915_gem_object_has_pages(obj) &&
1075 i915_gem_object_is_tiled(obj) &&
1076 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
1077 if (obj->mm.madv == I915_MADV_WILLNEED) {
1078 GEM_BUG_ON(!obj->mm.quirked);
1079 __i915_gem_object_unpin_pages(obj);
1080 obj->mm.quirked = false;
1081 }
1082 if (args->madv == I915_MADV_WILLNEED) {
1083 GEM_BUG_ON(obj->mm.quirked);
1084 __i915_gem_object_pin_pages(obj);
1085 obj->mm.quirked = true;
1086 }
1087 }
1088
1089 if (obj->mm.madv != __I915_MADV_PURGED)
1090 obj->mm.madv = args->madv;
1091
1092 if (i915_gem_object_has_pages(obj)) {
1093 struct list_head *list;
1094
1095 if (i915_gem_object_is_shrinkable(obj)) {
1096 unsigned long flags;
1097
1098 spin_lock_irqsave(&i915->mm.obj_lock, flags);
1099
1100 if (obj->mm.madv != I915_MADV_WILLNEED)
1101 list = &i915->mm.purge_list;
1102 else
1103 list = &i915->mm.shrink_list;
1104 list_move_tail(&obj->mm.link, list);
1105
1106 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
1107 }
1108 }
1109
1110 /* if the object is no longer attached, discard its backing storage */
1111 if (obj->mm.madv == I915_MADV_DONTNEED &&
1112 !i915_gem_object_has_pages(obj))
1113 i915_gem_object_truncate(obj);
1114
1115 args->retained = obj->mm.madv != __I915_MADV_PURGED;
1116 mutex_unlock(&obj->mm.lock);
1117
1118 out:
1119 i915_gem_object_put(obj);
1120 return err;
1121 }
1122
1123 int i915_gem_init(struct drm_i915_private *dev_priv)
1124 {
1125 int ret;
1126
1127 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1128 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
1129 mkwrite_device_info(dev_priv)->page_sizes =
1130 I915_GTT_PAGE_SIZE_4K;
1131
1132 ret = i915_gem_init_userptr(dev_priv);
1133 if (ret)
1134 return ret;
1135
1136 intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1137 intel_wopcm_init(&dev_priv->wopcm);
1138
1139 ret = i915_init_ggtt(dev_priv);
1140 if (ret) {
1141 GEM_BUG_ON(ret == -EIO);
1142 goto err_unlock;
1143 }
1144
1145 /*
1146 * Despite its name intel_init_clock_gating applies both display
1147 * clock gating workarounds; GT mmio workarounds and the occasional
1148 * GT power context workaround. Worse, sometimes it includes a context
1149 * register workaround which we need to apply before we record the
1150 * default HW state for all contexts.
1151 *
1152 * FIXME: break up the workarounds and apply them at the right time!
1153 */
1154 intel_init_clock_gating(dev_priv);
1155
1156 ret = intel_gt_init(&dev_priv->gt);
1157 if (ret)
1158 goto err_unlock;
1159
1160 return 0;
1161
1162 /*
1163 * Unwinding is complicated by that we want to handle -EIO to mean
1164 * disable GPU submission but keep KMS alive. We want to mark the
1165 * HW as irrevisibly wedged, but keep enough state around that the
1166 * driver doesn't explode during runtime.
1167 */
1168 err_unlock:
1169 i915_gem_drain_workqueue(dev_priv);
1170
1171 if (ret != -EIO) {
1172 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1173 i915_gem_cleanup_userptr(dev_priv);
1174 }
1175
1176 if (ret == -EIO) {
1177 /*
1178 * Allow engines or uC initialisation to fail by marking the GPU
1179 * as wedged. But we only want to do this when the GPU is angry,
1180 * for all other failure, such as an allocation failure, bail.
1181 */
1182 if (!intel_gt_is_wedged(&dev_priv->gt)) {
1183 i915_probe_error(dev_priv,
1184 "Failed to initialize GPU, declaring it wedged!\n");
1185 intel_gt_set_wedged(&dev_priv->gt);
1186 }
1187
1188 /* Minimal basic recovery for KMS */
1189 ret = i915_ggtt_enable_hw(dev_priv);
1190 i915_gem_restore_gtt_mappings(dev_priv);
1191 i915_gem_restore_fences(&dev_priv->ggtt);
1192 intel_init_clock_gating(dev_priv);
1193 }
1194
1195 i915_gem_drain_freed_objects(dev_priv);
1196 return ret;
1197 }
1198
1199 void i915_gem_driver_register(struct drm_i915_private *i915)
1200 {
1201 i915_gem_driver_register__shrinker(i915);
1202
1203 intel_engines_driver_register(i915);
1204 }
1205
1206 void i915_gem_driver_unregister(struct drm_i915_private *i915)
1207 {
1208 i915_gem_driver_unregister__shrinker(i915);
1209 }
1210
1211 void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1212 {
1213 intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1214
1215 i915_gem_suspend_late(dev_priv);
1216 intel_gt_driver_remove(&dev_priv->gt);
1217 dev_priv->uabi_engines = RB_ROOT;
1218
1219 /* Flush any outstanding unpin_work. */
1220 i915_gem_drain_workqueue(dev_priv);
1221
1222 i915_gem_drain_freed_objects(dev_priv);
1223 }
1224
1225 void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1226 {
1227 i915_gem_driver_release__contexts(dev_priv);
1228
1229 intel_gt_driver_release(&dev_priv->gt);
1230
1231 intel_wa_list_free(&dev_priv->gt_wa_list);
1232
1233 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1234 i915_gem_cleanup_userptr(dev_priv);
1235
1236 i915_gem_drain_freed_objects(dev_priv);
1237
1238 WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
1239 }
1240
1241 static void i915_gem_init__mm(struct drm_i915_private *i915)
1242 {
1243 spin_lock_init(&i915->mm.obj_lock);
1244
1245 init_llist_head(&i915->mm.free_list);
1246
1247 INIT_LIST_HEAD(&i915->mm.purge_list);
1248 INIT_LIST_HEAD(&i915->mm.shrink_list);
1249
1250 i915_gem_init__objects(i915);
1251 }
1252
1253 void i915_gem_init_early(struct drm_i915_private *dev_priv)
1254 {
1255 i915_gem_init__mm(dev_priv);
1256 i915_gem_init__contexts(dev_priv);
1257
1258 spin_lock_init(&dev_priv->fb_tracking.lock);
1259 }
1260
1261 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1262 {
1263 i915_gem_drain_freed_objects(dev_priv);
1264 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1265 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1266 WARN_ON(dev_priv->mm.shrink_count);
1267 }
1268
1269 int i915_gem_freeze(struct drm_i915_private *dev_priv)
1270 {
1271 /* Discard all purgeable objects, let userspace recover those as
1272 * required after resuming.
1273 */
1274 i915_gem_shrink_all(dev_priv);
1275
1276 return 0;
1277 }
1278
1279 int i915_gem_freeze_late(struct drm_i915_private *i915)
1280 {
1281 struct drm_i915_gem_object *obj;
1282 intel_wakeref_t wakeref;
1283
1284 /*
1285 * Called just before we write the hibernation image.
1286 *
1287 * We need to update the domain tracking to reflect that the CPU
1288 * will be accessing all the pages to create and restore from the
1289 * hibernation, and so upon restoration those pages will be in the
1290 * CPU domain.
1291 *
1292 * To make sure the hibernation image contains the latest state,
1293 * we update that state just before writing out the image.
1294 *
1295 * To try and reduce the hibernation image, we manually shrink
1296 * the objects as well, see i915_gem_freeze()
1297 */
1298
1299 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1300
1301 i915_gem_shrink(i915, -1UL, NULL, ~0);
1302 i915_gem_drain_freed_objects(i915);
1303
1304 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
1305 i915_gem_object_lock(obj);
1306 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
1307 i915_gem_object_unlock(obj);
1308 }
1309
1310 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1311
1312 return 0;
1313 }
1314
1315 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1316 {
1317 struct drm_i915_file_private *file_priv = file->driver_priv;
1318 struct i915_request *request;
1319
1320 /* Clean up our request list when the client is going away, so that
1321 * later retire_requests won't dereference our soon-to-be-gone
1322 * file_priv.
1323 */
1324 spin_lock(&file_priv->mm.lock);
1325 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1326 request->file_priv = NULL;
1327 spin_unlock(&file_priv->mm.lock);
1328 }
1329
1330 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1331 {
1332 struct drm_i915_file_private *file_priv;
1333 int ret;
1334
1335 DRM_DEBUG("\n");
1336
1337 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1338 if (!file_priv)
1339 return -ENOMEM;
1340
1341 file->driver_priv = file_priv;
1342 file_priv->dev_priv = i915;
1343 file_priv->file = file;
1344
1345 spin_lock_init(&file_priv->mm.lock);
1346 INIT_LIST_HEAD(&file_priv->mm.request_list);
1347
1348 file_priv->bsd_engine = -1;
1349 file_priv->hang_timestamp = jiffies;
1350
1351 ret = i915_gem_context_open(i915, file);
1352 if (ret)
1353 kfree(file_priv);
1354
1355 return ret;
1356 }
1357
1358 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1359 #include "selftests/mock_gem_device.c"
1360 #include "selftests/i915_gem.c"
1361 #endif
1362