i915_gem.c revision 1.71 1 /* $NetBSD: i915_gem.c,v 1.71 2021/12/19 12:25:27 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.71 2021/12/19 12:25:27 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 char *vaddr;
756 int ret;
757
758 vaddr = kmap(page);
759
760 if (needs_clflush_before)
761 drm_clflush_virt_range(vaddr + offset, len);
762
763 ret = __copy_from_user(vaddr + offset, user_data, len);
764 if (!ret && needs_clflush_after)
765 drm_clflush_virt_range(vaddr + offset, len);
766
767 kunmap(page);
768
769 return ret ? -EFAULT : 0;
770 }
771
772 static int
773 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
774 const struct drm_i915_gem_pwrite *args)
775 {
776 unsigned int partial_cacheline_write;
777 unsigned int needs_clflush;
778 unsigned int offset, idx;
779 struct dma_fence *fence;
780 void __user *user_data;
781 u64 remain;
782 int ret;
783
784 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
785 if (ret)
786 return ret;
787
788 fence = i915_gem_object_lock_fence(obj);
789 i915_gem_object_finish_access(obj);
790 if (!fence)
791 return -ENOMEM;
792
793 /* If we don't overwrite a cacheline completely we need to be
794 * careful to have up-to-date data by first clflushing. Don't
795 * overcomplicate things and flush the entire patch.
796 */
797 partial_cacheline_write = 0;
798 if (needs_clflush & CLFLUSH_BEFORE)
799 #ifdef __NetBSD__
800 partial_cacheline_write = cpu_info_primary.ci_cflush_lsize - 1;
801 #else
802 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
803 #endif
804
805 user_data = u64_to_user_ptr(args->data_ptr);
806 remain = args->size;
807 offset = offset_in_page(args->offset);
808 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
809 struct page *page = i915_gem_object_get_page(obj, idx);
810 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
811
812 ret = shmem_pwrite(page, offset, length, user_data,
813 (offset | length) & partial_cacheline_write,
814 needs_clflush & CLFLUSH_AFTER);
815 if (ret)
816 break;
817
818 remain -= length;
819 user_data += length;
820 offset = 0;
821 }
822
823 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
824 i915_gem_object_unlock_fence(obj, fence);
825
826 return ret;
827 }
828
829 /**
830 * Writes data to the object referenced by handle.
831 * @dev: drm device
832 * @data: ioctl data blob
833 * @file: drm file
834 *
835 * On error, the contents of the buffer that were to be modified are undefined.
836 */
837 int
838 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
839 struct drm_file *file)
840 {
841 struct drm_i915_gem_pwrite *args = data;
842 struct drm_i915_gem_object *obj;
843 int ret;
844
845 if (args->size == 0)
846 return 0;
847
848 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
849 return -EFAULT;
850
851 obj = i915_gem_object_lookup(file, args->handle);
852 if (!obj)
853 return -ENOENT;
854
855 /* Bounds check destination. */
856 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
857 ret = -EINVAL;
858 goto err;
859 }
860
861 /* Writes not allowed into this read-only object */
862 if (i915_gem_object_is_readonly(obj)) {
863 ret = -EINVAL;
864 goto err;
865 }
866
867 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
868
869 ret = -ENODEV;
870 if (obj->ops->pwrite)
871 ret = obj->ops->pwrite(obj, args);
872 if (ret != -ENODEV)
873 goto err;
874
875 ret = i915_gem_object_wait(obj,
876 I915_WAIT_INTERRUPTIBLE |
877 I915_WAIT_ALL,
878 MAX_SCHEDULE_TIMEOUT);
879 if (ret)
880 goto err;
881
882 ret = i915_gem_object_pin_pages(obj);
883 if (ret)
884 goto err;
885
886 ret = -EFAULT;
887 /* We can only do the GTT pwrite on untiled buffers, as otherwise
888 * it would end up going through the fenced access, and we'll get
889 * different detiling behavior between reading and writing.
890 * pread/pwrite currently are reading and writing from the CPU
891 * perspective, requiring manual detiling by the client.
892 */
893 if (!i915_gem_object_has_struct_page(obj) ||
894 cpu_write_needs_clflush(obj))
895 /* Note that the gtt paths might fail with non-page-backed user
896 * pointers (e.g. gtt mappings when moving data between
897 * textures). Fallback to the shmem path in that case.
898 */
899 ret = i915_gem_gtt_pwrite_fast(obj, args);
900
901 if (ret == -EFAULT || ret == -ENOSPC) {
902 if (i915_gem_object_has_struct_page(obj))
903 ret = i915_gem_shmem_pwrite(obj, args);
904 else
905 ret = i915_gem_phys_pwrite(obj, args, file);
906 }
907
908 i915_gem_object_unpin_pages(obj);
909 err:
910 i915_gem_object_put(obj);
911 return ret;
912 }
913
914 /**
915 * Called when user space has done writes to this buffer
916 * @dev: drm device
917 * @data: ioctl data blob
918 * @file: drm file
919 */
920 int
921 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
922 struct drm_file *file)
923 {
924 struct drm_i915_gem_sw_finish *args = data;
925 struct drm_i915_gem_object *obj;
926
927 obj = i915_gem_object_lookup(file, args->handle);
928 if (!obj)
929 return -ENOENT;
930
931 /*
932 * Proxy objects are barred from CPU access, so there is no
933 * need to ban sw_finish as it is a nop.
934 */
935
936 /* Pinned buffers may be scanout, so flush the cache */
937 i915_gem_object_flush_if_display(obj);
938 i915_gem_object_put(obj);
939
940 return 0;
941 }
942
943 void i915_gem_runtime_suspend(struct drm_i915_private *i915)
944 {
945 struct drm_i915_gem_object *obj, *on;
946 int i;
947
948 /*
949 * Only called during RPM suspend. All users of the userfault_list
950 * must be holding an RPM wakeref to ensure that this can not
951 * run concurrently with themselves (and use the struct_mutex for
952 * protection between themselves).
953 */
954
955 list_for_each_entry_safe(obj, on,
956 &i915->ggtt.userfault_list, userfault_link)
957 __i915_gem_object_release_mmap_gtt(obj);
958
959 /*
960 * The fence will be lost when the device powers down. If any were
961 * in use by hardware (i.e. they are pinned), we should not be powering
962 * down! All other fences will be reacquired by the user upon waking.
963 */
964 for (i = 0; i < i915->ggtt.num_fences; i++) {
965 struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
966
967 /*
968 * Ideally we want to assert that the fence register is not
969 * live at this point (i.e. that no piece of code will be
970 * trying to write through fence + GTT, as that both violates
971 * our tracking of activity and associated locking/barriers,
972 * but also is illegal given that the hw is powered down).
973 *
974 * Previously we used reg->pin_count as a "liveness" indicator.
975 * That is not sufficient, and we need a more fine-grained
976 * tool if we want to have a sanity check here.
977 */
978
979 if (!reg->vma)
980 continue;
981
982 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
983 reg->dirty = true;
984 }
985 }
986
987 struct i915_vma *
988 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
989 const struct i915_ggtt_view *view,
990 u64 size,
991 u64 alignment,
992 u64 flags)
993 {
994 struct drm_i915_private *i915 = to_i915(obj->base.dev);
995 struct i915_ggtt *ggtt = &i915->ggtt;
996 struct i915_vma *vma;
997 int ret;
998
999 if (i915_gem_object_never_bind_ggtt(obj))
1000 return ERR_PTR(-ENODEV);
1001
1002 if (flags & PIN_MAPPABLE &&
1003 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
1004 /*
1005 * If the required space is larger than the available
1006 * aperture, we will not able to find a slot for the
1007 * object and unbinding the object now will be in
1008 * vain. Worse, doing so may cause us to ping-pong
1009 * the object in and out of the Global GTT and
1010 * waste a lot of cycles under the mutex.
1011 */
1012 if (obj->base.size > ggtt->mappable_end)
1013 return ERR_PTR(-E2BIG);
1014
1015 /*
1016 * If NONBLOCK is set the caller is optimistically
1017 * trying to cache the full object within the mappable
1018 * aperture, and *must* have a fallback in place for
1019 * situations where we cannot bind the object. We
1020 * can be a little more lax here and use the fallback
1021 * more often to avoid costly migrations of ourselves
1022 * and other objects within the aperture.
1023 *
1024 * Half-the-aperture is used as a simple heuristic.
1025 * More interesting would to do search for a free
1026 * block prior to making the commitment to unbind.
1027 * That caters for the self-harm case, and with a
1028 * little more heuristics (e.g. NOFAULT, NOEVICT)
1029 * we could try to minimise harm to others.
1030 */
1031 if (flags & PIN_NONBLOCK &&
1032 obj->base.size > ggtt->mappable_end / 2)
1033 return ERR_PTR(-ENOSPC);
1034 }
1035
1036 vma = i915_vma_instance(obj, &ggtt->vm, view);
1037 if (IS_ERR(vma))
1038 return vma;
1039
1040 if (i915_vma_misplaced(vma, size, alignment, flags)) {
1041 if (flags & PIN_NONBLOCK) {
1042 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
1043 return ERR_PTR(-ENOSPC);
1044
1045 if (flags & PIN_MAPPABLE &&
1046 vma->fence_size > ggtt->mappable_end / 2)
1047 return ERR_PTR(-ENOSPC);
1048 }
1049
1050 ret = i915_vma_unbind(vma);
1051 if (ret)
1052 return ERR_PTR(ret);
1053 }
1054
1055 if (vma->fence && !i915_gem_object_is_tiled(obj)) {
1056 mutex_lock(&ggtt->vm.mutex);
1057 ret = i915_vma_revoke_fence(vma);
1058 mutex_unlock(&ggtt->vm.mutex);
1059 if (ret)
1060 return ERR_PTR(ret);
1061 }
1062
1063 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
1064 if (ret)
1065 return ERR_PTR(ret);
1066
1067 return vma;
1068 }
1069
1070 int
1071 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
1072 struct drm_file *file_priv)
1073 {
1074 struct drm_i915_private *i915 = to_i915(dev);
1075 struct drm_i915_gem_madvise *args = data;
1076 struct drm_i915_gem_object *obj;
1077 int err;
1078
1079 switch (args->madv) {
1080 case I915_MADV_DONTNEED:
1081 case I915_MADV_WILLNEED:
1082 break;
1083 default:
1084 return -EINVAL;
1085 }
1086
1087 obj = i915_gem_object_lookup(file_priv, args->handle);
1088 if (!obj)
1089 return -ENOENT;
1090
1091 err = mutex_lock_interruptible(&obj->mm.lock);
1092 if (err)
1093 goto out;
1094
1095 if (i915_gem_object_has_pages(obj) &&
1096 i915_gem_object_is_tiled(obj) &&
1097 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
1098 if (obj->mm.madv == I915_MADV_WILLNEED) {
1099 GEM_BUG_ON(!obj->mm.quirked);
1100 __i915_gem_object_unpin_pages(obj);
1101 obj->mm.quirked = false;
1102 }
1103 if (args->madv == I915_MADV_WILLNEED) {
1104 GEM_BUG_ON(obj->mm.quirked);
1105 __i915_gem_object_pin_pages(obj);
1106 obj->mm.quirked = true;
1107 }
1108 }
1109
1110 if (obj->mm.madv != __I915_MADV_PURGED)
1111 obj->mm.madv = args->madv;
1112
1113 if (i915_gem_object_has_pages(obj)) {
1114 struct list_head *list;
1115
1116 if (i915_gem_object_is_shrinkable(obj)) {
1117 unsigned long flags;
1118
1119 spin_lock_irqsave(&i915->mm.obj_lock, flags);
1120
1121 if (obj->mm.madv != I915_MADV_WILLNEED)
1122 list = &i915->mm.purge_list;
1123 else
1124 list = &i915->mm.shrink_list;
1125 list_move_tail(&obj->mm.link, list);
1126
1127 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
1128 }
1129 }
1130
1131 /* if the object is no longer attached, discard its backing storage */
1132 if (obj->mm.madv == I915_MADV_DONTNEED &&
1133 !i915_gem_object_has_pages(obj))
1134 i915_gem_object_truncate(obj);
1135
1136 args->retained = obj->mm.madv != __I915_MADV_PURGED;
1137 mutex_unlock(&obj->mm.lock);
1138
1139 out:
1140 i915_gem_object_put(obj);
1141 return err;
1142 }
1143
1144 int i915_gem_init(struct drm_i915_private *dev_priv)
1145 {
1146 int ret;
1147
1148 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1149 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
1150 mkwrite_device_info(dev_priv)->page_sizes =
1151 I915_GTT_PAGE_SIZE_4K;
1152
1153 ret = i915_gem_init_userptr(dev_priv);
1154 if (ret)
1155 return ret;
1156
1157 intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1158 intel_wopcm_init(&dev_priv->wopcm);
1159
1160 ret = i915_init_ggtt(dev_priv);
1161 if (ret) {
1162 GEM_BUG_ON(ret == -EIO);
1163 goto err_unlock;
1164 }
1165
1166 /*
1167 * Despite its name intel_init_clock_gating applies both display
1168 * clock gating workarounds; GT mmio workarounds and the occasional
1169 * GT power context workaround. Worse, sometimes it includes a context
1170 * register workaround which we need to apply before we record the
1171 * default HW state for all contexts.
1172 *
1173 * FIXME: break up the workarounds and apply them at the right time!
1174 */
1175 intel_init_clock_gating(dev_priv);
1176
1177 ret = intel_gt_init(&dev_priv->gt);
1178 if (ret)
1179 goto err_unlock;
1180
1181 return 0;
1182
1183 /*
1184 * Unwinding is complicated by that we want to handle -EIO to mean
1185 * disable GPU submission but keep KMS alive. We want to mark the
1186 * HW as irrevisibly wedged, but keep enough state around that the
1187 * driver doesn't explode during runtime.
1188 */
1189 err_unlock:
1190 i915_gem_drain_workqueue(dev_priv);
1191
1192 if (ret != -EIO) {
1193 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1194 i915_gem_cleanup_userptr(dev_priv);
1195 }
1196
1197 if (ret == -EIO) {
1198 /*
1199 * Allow engines or uC initialisation to fail by marking the GPU
1200 * as wedged. But we only want to do this when the GPU is angry,
1201 * for all other failure, such as an allocation failure, bail.
1202 */
1203 if (!intel_gt_is_wedged(&dev_priv->gt)) {
1204 i915_probe_error(dev_priv,
1205 "Failed to initialize GPU, declaring it wedged!\n");
1206 intel_gt_set_wedged(&dev_priv->gt);
1207 }
1208
1209 /* Minimal basic recovery for KMS */
1210 ret = i915_ggtt_enable_hw(dev_priv);
1211 i915_gem_restore_gtt_mappings(dev_priv);
1212 i915_gem_restore_fences(&dev_priv->ggtt);
1213 intel_init_clock_gating(dev_priv);
1214 }
1215
1216 i915_gem_drain_freed_objects(dev_priv);
1217 return ret;
1218 }
1219
1220 void i915_gem_driver_register(struct drm_i915_private *i915)
1221 {
1222 i915_gem_driver_register__shrinker(i915);
1223
1224 intel_engines_driver_register(i915);
1225 }
1226
1227 void i915_gem_driver_unregister(struct drm_i915_private *i915)
1228 {
1229 i915_gem_driver_unregister__shrinker(i915);
1230 }
1231
1232 void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1233 {
1234 intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1235
1236 i915_gem_suspend_late(dev_priv);
1237 intel_gt_driver_remove(&dev_priv->gt);
1238 #ifndef __NetBSD__ /* XXX uabi_engines */
1239 dev_priv->uabi_engines = RB_ROOT;
1240 #endif
1241
1242 /* Flush any outstanding unpin_work. */
1243 i915_gem_drain_workqueue(dev_priv);
1244
1245 i915_gem_drain_freed_objects(dev_priv);
1246 }
1247
1248 void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1249 {
1250 i915_gem_driver_release__contexts(dev_priv);
1251
1252 intel_gt_driver_release(&dev_priv->gt);
1253
1254 intel_wa_list_free(&dev_priv->gt_wa_list);
1255
1256 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1257 i915_gem_cleanup_userptr(dev_priv);
1258
1259 i915_gem_drain_freed_objects(dev_priv);
1260
1261 WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
1262 }
1263
1264 static void i915_gem_init__mm(struct drm_i915_private *i915)
1265 {
1266 spin_lock_init(&i915->mm.obj_lock);
1267
1268 init_llist_head(&i915->mm.free_list);
1269
1270 INIT_LIST_HEAD(&i915->mm.purge_list);
1271 INIT_LIST_HEAD(&i915->mm.shrink_list);
1272
1273 i915_gem_init__objects(i915);
1274 }
1275
1276 void i915_gem_init_early(struct drm_i915_private *dev_priv)
1277 {
1278 i915_gem_init__mm(dev_priv);
1279 i915_gem_init__contexts(dev_priv);
1280
1281 spin_lock_init(&dev_priv->fb_tracking.lock);
1282 }
1283
1284 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1285 {
1286 i915_gem_drain_freed_objects(dev_priv);
1287 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1288 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1289 WARN_ON(dev_priv->mm.shrink_count);
1290 spin_lock_destroy(&dev_priv->fb_tracking.lock);
1291 }
1292
1293 int i915_gem_freeze(struct drm_i915_private *dev_priv)
1294 {
1295 /* Discard all purgeable objects, let userspace recover those as
1296 * required after resuming.
1297 */
1298 i915_gem_shrink_all(dev_priv);
1299
1300 return 0;
1301 }
1302
1303 int i915_gem_freeze_late(struct drm_i915_private *i915)
1304 {
1305 struct drm_i915_gem_object *obj;
1306 intel_wakeref_t wakeref;
1307
1308 /*
1309 * Called just before we write the hibernation image.
1310 *
1311 * We need to update the domain tracking to reflect that the CPU
1312 * will be accessing all the pages to create and restore from the
1313 * hibernation, and so upon restoration those pages will be in the
1314 * CPU domain.
1315 *
1316 * To make sure the hibernation image contains the latest state,
1317 * we update that state just before writing out the image.
1318 *
1319 * To try and reduce the hibernation image, we manually shrink
1320 * the objects as well, see i915_gem_freeze()
1321 */
1322
1323 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1324
1325 i915_gem_shrink(i915, -1UL, NULL, ~0);
1326 i915_gem_drain_freed_objects(i915);
1327
1328 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
1329 i915_gem_object_lock(obj);
1330 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
1331 i915_gem_object_unlock(obj);
1332 }
1333
1334 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1335
1336 return 0;
1337 }
1338
1339 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1340 {
1341 struct drm_i915_file_private *file_priv = file->driver_priv;
1342 struct i915_request *request;
1343
1344 /* Clean up our request list when the client is going away, so that
1345 * later retire_requests won't dereference our soon-to-be-gone
1346 * file_priv.
1347 */
1348 spin_lock(&file_priv->mm.lock);
1349 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1350 request->file_priv = NULL;
1351 spin_unlock(&file_priv->mm.lock);
1352
1353 /*
1354 * XXX This is probably too early -- need to defer with
1355 * callrcu; caller already defers free with kfree_rcu.
1356 */
1357 spin_lock_destroy(&file_priv->mm.lock);
1358 }
1359
1360 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1361 {
1362 struct drm_i915_file_private *file_priv;
1363 int ret;
1364
1365 DRM_DEBUG("\n");
1366
1367 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1368 if (!file_priv)
1369 return -ENOMEM;
1370
1371 file->driver_priv = file_priv;
1372 file_priv->dev_priv = i915;
1373 file_priv->file = file;
1374
1375 spin_lock_init(&file_priv->mm.lock);
1376 INIT_LIST_HEAD(&file_priv->mm.request_list);
1377
1378 file_priv->bsd_engine = -1;
1379 file_priv->hang_timestamp = jiffies;
1380
1381 ret = i915_gem_context_open(i915, file);
1382 if (ret)
1383 kfree(file_priv);
1384
1385 return ret;
1386 }
1387
1388 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1389 #include "selftests/mock_gem_device.c"
1390 #include "selftests/i915_gem.c"
1391 #endif
1392