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