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