1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright © 2014-2016 Intel Corporation
5 */
6
7 #include <linux/pagevec.h>
8 #include <linux/shmem_fs.h>
9 #include <linux/swap.h>
10
11 #include <drm/drm_cache.h>
12
13 #include "gem/i915_gem_region.h"
14 #include "i915_drv.h"
15 #include "i915_gem_object.h"
16 #include "i915_gem_tiling.h"
17 #include "i915_gemfs.h"
18 #include "i915_scatterlist.h"
19 #include "i915_trace.h"
20
21 /*
22 * Move folios to appropriate lru and release the batch, decrementing the
23 * ref count of those folios.
24 */
check_release_folio_batch(struct folio_batch * fbatch)25 static void check_release_folio_batch(struct folio_batch *fbatch)
26 {
27 STUB();
28 #ifdef notyet
29 check_move_unevictable_folios(fbatch);
30 __folio_batch_release(fbatch);
31 #endif
32 cond_resched();
33 }
34
shmem_sg_free_table(struct sg_table * st,struct address_space * mapping,bool dirty,bool backup,struct drm_i915_gem_object * obj)35 void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping,
36 bool dirty, bool backup,
37 struct drm_i915_gem_object *obj)
38 {
39 struct sgt_iter sgt_iter;
40 struct folio_batch fbatch;
41 struct folio *last = NULL;
42 struct vm_page *page;
43
44 #ifdef __linux__
45 mapping_clear_unevictable(mapping);
46
47 folio_batch_init(&fbatch);
48 #endif
49 for_each_sgt_page(page, sgt_iter, st) {
50 #ifdef __linux__
51 struct folio *folio = page_folio(page);
52
53 if (folio == last)
54 continue;
55 last = folio;
56 if (dirty)
57 folio_mark_dirty(folio);
58 if (backup)
59 folio_mark_accessed(folio);
60
61 if (!folio_batch_add(&fbatch, folio))
62 check_release_folio_batch(&fbatch);
63 #else
64 if (dirty)
65 set_page_dirty(page);
66 #endif
67 }
68 #ifdef __linux__
69 if (fbatch.nr)
70 check_release_folio_batch(&fbatch);
71 #else
72 uvm_obj_unwire(obj->base.uao, 0, obj->base.size);
73 #endif
74
75 sg_free_table(st);
76 }
77
shmem_sg_alloc_table(struct drm_i915_private * i915,struct sg_table * st,size_t size,struct intel_memory_region * mr,struct address_space * mapping,unsigned int max_segment,struct drm_i915_gem_object * obj)78 int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
79 size_t size, struct intel_memory_region *mr,
80 struct address_space *mapping,
81 unsigned int max_segment,
82 struct drm_i915_gem_object *obj)
83 {
84 unsigned int page_count; /* restricted by sg_alloc_table */
85 unsigned long i;
86 struct scatterlist *sg;
87 unsigned long next_pfn = 0; /* suppress gcc warning */
88 gfp_t noreclaim;
89 int ret;
90 struct pglist plist;
91 struct vm_page *page;
92
93 if (overflows_type(size / PAGE_SIZE, page_count))
94 return -E2BIG;
95
96 page_count = size / PAGE_SIZE;
97 /*
98 * If there's no chance of allocating enough pages for the whole
99 * object, bail early.
100 */
101 if (size > resource_size(&mr->region))
102 return -ENOMEM;
103
104 if (sg_alloc_table(st, page_count, GFP_KERNEL | __GFP_NOWARN))
105 return -ENOMEM;
106 #ifdef __linux__
107
108 /*
109 * Get the list of pages out of our struct file. They'll be pinned
110 * at this point until we release them.
111 *
112 * Fail silently without starting the shrinker
113 */
114 mapping_set_unevictable(mapping);
115 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
116 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
117
118 sg = st->sgl;
119 st->nents = 0;
120 for (i = 0; i < page_count; i++) {
121 struct folio *folio;
122 unsigned long nr_pages;
123 const unsigned int shrink[] = {
124 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND,
125 0,
126 }, *s = shrink;
127 gfp_t gfp = noreclaim;
128
129 do {
130 cond_resched();
131 folio = shmem_read_folio_gfp(mapping, i, gfp);
132 if (!IS_ERR(folio))
133 break;
134
135 if (!*s) {
136 ret = PTR_ERR(folio);
137 goto err_sg;
138 }
139
140 i915_gem_shrink(NULL, i915, 2 * page_count, NULL, *s++);
141
142 /*
143 * We've tried hard to allocate the memory by reaping
144 * our own buffer, now let the real VM do its job and
145 * go down in flames if truly OOM.
146 *
147 * However, since graphics tend to be disposable,
148 * defer the oom here by reporting the ENOMEM back
149 * to userspace.
150 */
151 if (!*s) {
152 /* reclaim and warn, but no oom */
153 gfp = mapping_gfp_mask(mapping);
154
155 /*
156 * Our bo are always dirty and so we require
157 * kswapd to reclaim our pages (direct reclaim
158 * does not effectively begin pageout of our
159 * buffers on its own). However, direct reclaim
160 * only waits for kswapd when under allocation
161 * congestion. So as a result __GFP_RECLAIM is
162 * unreliable and fails to actually reclaim our
163 * dirty pages -- unless you try over and over
164 * again with !__GFP_NORETRY. However, we still
165 * want to fail this allocation rather than
166 * trigger the out-of-memory killer and for
167 * this we want __GFP_RETRY_MAYFAIL.
168 */
169 gfp |= __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
170 }
171 } while (1);
172
173 nr_pages = min_t(unsigned long,
174 folio_nr_pages(folio), page_count - i);
175 if (!i ||
176 sg->length >= max_segment ||
177 folio_pfn(folio) != next_pfn) {
178 if (i)
179 sg = sg_next(sg);
180
181 st->nents++;
182 sg_set_folio(sg, folio, nr_pages * PAGE_SIZE, 0);
183 } else {
184 /* XXX: could overflow? */
185 sg->length += nr_pages * PAGE_SIZE;
186 }
187 next_pfn = folio_pfn(folio) + nr_pages;
188 i += nr_pages - 1;
189
190 /* Check that the i965g/gm workaround works. */
191 GEM_BUG_ON(gfp & __GFP_DMA32 && next_pfn >= 0x00100000UL);
192 }
193 #else
194 sg = st->sgl;
195 st->nents = 0;
196
197 TAILQ_INIT(&plist);
198 if (uvm_obj_wire(obj->base.uao, 0, obj->base.size, &plist)) {
199 sg_free_table(st);
200 kfree(st);
201 return -ENOMEM;
202 }
203
204 i = 0;
205 TAILQ_FOREACH(page, &plist, pageq) {
206 if (i)
207 sg = sg_next(sg);
208 st->nents++;
209 sg_set_page(sg, page, PAGE_SIZE, 0);
210 i++;
211 }
212 #endif
213 if (sg) /* loop terminated early; short sg table */
214 sg_mark_end(sg);
215
216 /* Trim unused sg entries to avoid wasting memory. */
217 i915_sg_trim(st);
218
219 return 0;
220 #ifdef notyet
221 err_sg:
222 sg_mark_end(sg);
223 if (sg != st->sgl) {
224 shmem_sg_free_table(st, mapping, false, false);
225 } else {
226 mapping_clear_unevictable(mapping);
227 sg_free_table(st);
228 }
229
230 /*
231 * shmemfs first checks if there is enough memory to allocate the page
232 * and reports ENOSPC should there be insufficient, along with the usual
233 * ENOMEM for a genuine allocation failure.
234 *
235 * We use ENOSPC in our driver to mean that we have run out of aperture
236 * space and so want to translate the error from shmemfs back to our
237 * usual understanding of ENOMEM.
238 */
239 if (ret == -ENOSPC)
240 ret = -ENOMEM;
241
242 return ret;
243 #endif
244 }
245
shmem_get_pages(struct drm_i915_gem_object * obj)246 static int shmem_get_pages(struct drm_i915_gem_object *obj)
247 {
248 struct drm_i915_private *i915 = to_i915(obj->base.dev);
249 struct intel_memory_region *mem = obj->mm.region;
250 #ifdef __linux__
251 struct address_space *mapping = obj->base.filp->f_mapping;
252 #endif
253 unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
254 struct sg_table *st;
255 struct sgt_iter sgt_iter;
256 struct vm_page *page;
257 int ret;
258
259 /*
260 * Assert that the object is not currently in any GPU domain. As it
261 * wasn't in the GTT, there shouldn't be any way it could have been in
262 * a GPU cache
263 */
264 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
265 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
266
267 rebuild_st:
268 st = kmalloc(sizeof(*st), GFP_KERNEL | __GFP_NOWARN);
269 if (!st)
270 return -ENOMEM;
271
272 #ifdef __linux__
273 ret = shmem_sg_alloc_table(i915, st, obj->base.size, mem, mapping,
274 max_segment);
275 #else
276 ret = shmem_sg_alloc_table(i915, st, obj->base.size, mem, NULL,
277 max_segment, obj);
278 #endif
279 if (ret)
280 goto err_st;
281
282 ret = i915_gem_gtt_prepare_pages(obj, st);
283 if (ret) {
284 /*
285 * DMA remapping failed? One possible cause is that
286 * it could not reserve enough large entries, asking
287 * for PAGE_SIZE chunks instead may be helpful.
288 */
289 if (max_segment > PAGE_SIZE) {
290 #ifdef __linux__
291 for_each_sgt_page(page, sgt_iter, st)
292 put_page(page);
293 #else
294 uvm_obj_unwire(obj->base.uao, 0, obj->base.size);
295 #endif
296 sg_free_table(st);
297 kfree(st);
298
299 max_segment = PAGE_SIZE;
300 goto rebuild_st;
301 } else {
302 dev_warn(i915->drm.dev,
303 "Failed to DMA remap %zu pages\n",
304 obj->base.size >> PAGE_SHIFT);
305 goto err_pages;
306 }
307 }
308
309 if (i915_gem_object_needs_bit17_swizzle(obj))
310 i915_gem_object_do_bit_17_swizzle(obj, st);
311
312 if (i915_gem_object_can_bypass_llc(obj))
313 obj->cache_dirty = true;
314
315 __i915_gem_object_set_pages(obj, st);
316
317 return 0;
318
319 err_pages:
320 #ifdef __linux__
321 shmem_sg_free_table(st, mapping, false, false);
322 #else
323 shmem_sg_free_table(st, NULL, false, false, obj);
324 #endif
325 /*
326 * shmemfs first checks if there is enough memory to allocate the page
327 * and reports ENOSPC should there be insufficient, along with the usual
328 * ENOMEM for a genuine allocation failure.
329 *
330 * We use ENOSPC in our driver to mean that we have run out of aperture
331 * space and so want to translate the error from shmemfs back to our
332 * usual understanding of ENOMEM.
333 */
334 err_st:
335 if (ret == -ENOSPC)
336 ret = -ENOMEM;
337
338 kfree(st);
339
340 return ret;
341 }
342
343 static int
shmem_truncate(struct drm_i915_gem_object * obj)344 shmem_truncate(struct drm_i915_gem_object *obj)
345 {
346 /*
347 * Our goal here is to return as much of the memory as
348 * is possible back to the system as we are called from OOM.
349 * To do this we must instruct the shmfs to drop all of its
350 * backing pages, *now*.
351 */
352 #ifdef __linux__
353 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
354 #else
355 rw_enter(obj->base.uao->vmobjlock, RW_WRITE);
356 obj->base.uao->pgops->pgo_flush(obj->base.uao, 0, obj->base.size,
357 PGO_ALLPAGES | PGO_FREE);
358 rw_exit(obj->base.uao->vmobjlock);
359 #endif
360 obj->mm.madv = __I915_MADV_PURGED;
361 obj->mm.pages = ERR_PTR(-EFAULT);
362
363 return 0;
364 }
365
__shmem_writeback(size_t size,struct address_space * mapping)366 void __shmem_writeback(size_t size, struct address_space *mapping)
367 {
368 STUB();
369 #ifdef notyet
370 struct writeback_control wbc = {
371 .sync_mode = WB_SYNC_NONE,
372 .nr_to_write = SWAP_CLUSTER_MAX,
373 .range_start = 0,
374 .range_end = LLONG_MAX,
375 .for_reclaim = 1,
376 };
377 unsigned long i;
378
379 /*
380 * Leave mmapings intact (GTT will have been revoked on unbinding,
381 * leaving only CPU mmapings around) and add those pages to the LRU
382 * instead of invoking writeback so they are aged and paged out
383 * as normal.
384 */
385
386 /* Begin writeback on each dirty page */
387 for (i = 0; i < size >> PAGE_SHIFT; i++) {
388 struct vm_page *page;
389
390 page = find_lock_page(mapping, i);
391 if (!page)
392 continue;
393
394 if (!page_mapped(page) && clear_page_dirty_for_io(page)) {
395 int ret;
396
397 SetPageReclaim(page);
398 ret = mapping->a_ops->writepage(page, &wbc);
399 if (!PageWriteback(page))
400 ClearPageReclaim(page);
401 if (!ret)
402 goto put;
403 }
404 unlock_page(page);
405 put:
406 put_page(page);
407 }
408 #endif
409 }
410
411 static void
shmem_writeback(struct drm_i915_gem_object * obj)412 shmem_writeback(struct drm_i915_gem_object *obj)
413 {
414 STUB();
415 #ifdef notyet
416 __shmem_writeback(obj->base.size, obj->base.filp->f_mapping);
417 #endif
418 }
419
shmem_shrink(struct drm_i915_gem_object * obj,unsigned int flags)420 static int shmem_shrink(struct drm_i915_gem_object *obj, unsigned int flags)
421 {
422 switch (obj->mm.madv) {
423 case I915_MADV_DONTNEED:
424 return i915_gem_object_truncate(obj);
425 case __I915_MADV_PURGED:
426 return 0;
427 }
428
429 if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK)
430 shmem_writeback(obj);
431
432 return 0;
433 }
434
435 void
__i915_gem_object_release_shmem(struct drm_i915_gem_object * obj,struct sg_table * pages,bool needs_clflush)436 __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
437 struct sg_table *pages,
438 bool needs_clflush)
439 {
440 struct drm_i915_private *i915 = to_i915(obj->base.dev);
441
442 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
443
444 if (obj->mm.madv == I915_MADV_DONTNEED)
445 obj->mm.dirty = false;
446
447 if (needs_clflush &&
448 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
449 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
450 drm_clflush_sg(pages);
451
452 __start_cpu_write(obj);
453 /*
454 * On non-LLC igfx platforms, force the flush-on-acquire if this is ever
455 * swapped-in. Our async flush path is not trust worthy enough yet(and
456 * happens in the wrong order), and with some tricks it's conceivable
457 * for userspace to change the cache-level to I915_CACHE_NONE after the
458 * pages are swapped-in, and since execbuf binds the object before doing
459 * the async flush, we have a race window.
460 */
461 if (!HAS_LLC(i915) && !IS_DGFX(i915))
462 obj->cache_dirty = true;
463 }
464
i915_gem_object_put_pages_shmem(struct drm_i915_gem_object * obj,struct sg_table * pages)465 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, struct sg_table *pages)
466 {
467 __i915_gem_object_release_shmem(obj, pages, true);
468
469 i915_gem_gtt_finish_pages(obj, pages);
470
471 if (i915_gem_object_needs_bit17_swizzle(obj))
472 i915_gem_object_save_bit_17_swizzle(obj, pages);
473
474 #ifdef __linux__
475 shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping,
476 obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED);
477 #else
478 shmem_sg_free_table(pages, NULL,
479 obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED, obj);
480 #endif
481 kfree(pages);
482 obj->mm.dirty = false;
483 }
484
485 static void
shmem_put_pages(struct drm_i915_gem_object * obj,struct sg_table * pages)486 shmem_put_pages(struct drm_i915_gem_object *obj, struct sg_table *pages)
487 {
488 if (likely(i915_gem_object_has_struct_page(obj)))
489 i915_gem_object_put_pages_shmem(obj, pages);
490 else
491 i915_gem_object_put_pages_phys(obj, pages);
492 }
493
494 static int
shmem_pwrite(struct drm_i915_gem_object * obj,const struct drm_i915_gem_pwrite * arg)495 shmem_pwrite(struct drm_i915_gem_object *obj,
496 const struct drm_i915_gem_pwrite *arg)
497 {
498 #ifdef __linux__
499 struct address_space *mapping = obj->base.filp->f_mapping;
500 const struct address_space_operations *aops = mapping->a_ops;
501 #endif
502 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
503 u64 remain;
504 loff_t pos;
505 unsigned int pg;
506
507 /* Caller already validated user args */
508 GEM_BUG_ON(!access_ok(user_data, arg->size));
509
510 if (!i915_gem_object_has_struct_page(obj))
511 return i915_gem_object_pwrite_phys(obj, arg);
512
513 /*
514 * Before we instantiate/pin the backing store for our use, we
515 * can prepopulate the shmemfs filp efficiently using a write into
516 * the pagecache. We avoid the penalty of instantiating all the
517 * pages, important if the user is just writing to a few and never
518 * uses the object on the GPU, and using a direct write into shmemfs
519 * allows it to avoid the cost of retrieving a page (either swapin
520 * or clearing-before-use) before it is overwritten.
521 */
522 if (i915_gem_object_has_pages(obj))
523 return -ENODEV;
524
525 if (obj->mm.madv != I915_MADV_WILLNEED)
526 return -EFAULT;
527
528 /*
529 * Before the pages are instantiated the object is treated as being
530 * in the CPU domain. The pages will be clflushed as required before
531 * use, and we can freely write into the pages directly. If userspace
532 * races pwrite with any other operation; corruption will ensue -
533 * that is userspace's prerogative!
534 */
535
536 remain = arg->size;
537 pos = arg->offset;
538 pg = offset_in_page(pos);
539
540 do {
541 unsigned int len, unwritten;
542 #ifdef __linux__
543 struct folio *folio;
544 #else
545 struct vm_page *page;
546 #endif
547 void *data, *vaddr;
548 int err;
549 char __maybe_unused c;
550
551 len = PAGE_SIZE - pg;
552 if (len > remain)
553 len = remain;
554
555 /* Prefault the user page to reduce potential recursion */
556 err = __get_user(c, user_data);
557 if (err)
558 return err;
559
560 err = __get_user(c, user_data + len - 1);
561 if (err)
562 return err;
563
564 #ifdef __linux__
565 err = aops->write_begin(obj->base.filp, mapping, pos, len,
566 &folio, &data);
567 if (err < 0)
568 return err;
569 #else
570 struct pglist plist;
571 TAILQ_INIT(&plist);
572 if (uvm_obj_wire(obj->base.uao, trunc_page(pos),
573 trunc_page(pos) + PAGE_SIZE, &plist)) {
574 return -ENOMEM;
575 }
576 page = TAILQ_FIRST(&plist);
577 #endif
578
579 #ifdef __linux__
580 vaddr = kmap_local_folio(folio, offset_in_folio(folio, pos));
581 pagefault_disable();
582 unwritten = __copy_from_user_inatomic(vaddr, user_data, len);
583 pagefault_enable();
584 kunmap_local(vaddr);
585 #else
586 vaddr = kmap_atomic(page);
587 unwritten = __copy_from_user_inatomic(vaddr + pg,
588 user_data, len);
589 kunmap_atomic(vaddr);
590 #endif
591
592 #ifdef __linux__
593 err = aops->write_end(obj->base.filp, mapping, pos, len,
594 len - unwritten, folio, data);
595 if (err < 0)
596 return err;
597 #else
598 uvm_obj_unwire(obj->base.uao, trunc_page(pos),
599 trunc_page(pos) + PAGE_SIZE);
600 #endif
601
602 /* We don't handle -EFAULT, leave it to the caller to check */
603 if (unwritten)
604 return -ENODEV;
605
606 remain -= len;
607 user_data += len;
608 pos += len;
609 pg = 0;
610 } while (remain);
611
612 return 0;
613 }
614
615 static int
shmem_pread(struct drm_i915_gem_object * obj,const struct drm_i915_gem_pread * arg)616 shmem_pread(struct drm_i915_gem_object *obj,
617 const struct drm_i915_gem_pread *arg)
618 {
619 if (!i915_gem_object_has_struct_page(obj))
620 return i915_gem_object_pread_phys(obj, arg);
621
622 return -ENODEV;
623 }
624
shmem_release(struct drm_i915_gem_object * obj)625 static void shmem_release(struct drm_i915_gem_object *obj)
626 {
627 if (i915_gem_object_has_struct_page(obj))
628 i915_gem_object_release_memory_region(obj);
629
630 #ifdef __linux__
631 fput(obj->base.filp);
632 #endif
633 }
634
635 const struct drm_i915_gem_object_ops i915_gem_shmem_ops = {
636 .name = "i915_gem_object_shmem",
637 .flags = I915_GEM_OBJECT_IS_SHRINKABLE,
638
639 .get_pages = shmem_get_pages,
640 .put_pages = shmem_put_pages,
641 .truncate = shmem_truncate,
642 .shrink = shmem_shrink,
643
644 .pwrite = shmem_pwrite,
645 .pread = shmem_pread,
646
647 .release = shmem_release,
648 };
649
650 #ifdef __linux__
__create_shmem(struct drm_i915_private * i915,struct drm_gem_object * obj,resource_size_t size)651 static int __create_shmem(struct drm_i915_private *i915,
652 struct drm_gem_object *obj,
653 resource_size_t size)
654 {
655 unsigned long flags = VM_NORESERVE;
656 struct file *filp;
657
658 drm_gem_private_object_init(&i915->drm, obj, size);
659
660 /* XXX: The __shmem_file_setup() function returns -EINVAL if size is
661 * greater than MAX_LFS_FILESIZE.
662 * To handle the same error as other code that returns -E2BIG when
663 * the size is too large, we add a code that returns -E2BIG when the
664 * size is larger than the size that can be handled.
665 * If BITS_PER_LONG is 32, size > MAX_LFS_FILESIZE is always false,
666 * so we only needs to check when BITS_PER_LONG is 64.
667 * If BITS_PER_LONG is 32, E2BIG checks are processed when
668 * i915_gem_object_size_2big() is called before init_object() callback
669 * is called.
670 */
671 if (BITS_PER_LONG == 64 && size > MAX_LFS_FILESIZE)
672 return -E2BIG;
673
674 if (i915->mm.gemfs)
675 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
676 flags);
677 else
678 filp = shmem_file_setup("i915", size, flags);
679 if (IS_ERR(filp))
680 return PTR_ERR(filp);
681
682 obj->filp = filp;
683 return 0;
684 }
685 #endif
686
shmem_object_init(struct intel_memory_region * mem,struct drm_i915_gem_object * obj,resource_size_t offset,resource_size_t size,resource_size_t page_size,unsigned int flags)687 static int shmem_object_init(struct intel_memory_region *mem,
688 struct drm_i915_gem_object *obj,
689 resource_size_t offset,
690 resource_size_t size,
691 resource_size_t page_size,
692 unsigned int flags)
693 {
694 static struct lock_class_key lock_class;
695 struct drm_i915_private *i915 = mem->i915;
696 struct address_space *mapping;
697 unsigned int cache_level;
698 gfp_t mask;
699 int ret;
700
701 #ifdef __linux__
702 ret = __create_shmem(i915, &obj->base, size);
703 #else
704 ret = drm_gem_object_init(&i915->drm, &obj->base, size);
705 #endif
706 if (ret)
707 return ret;
708
709 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
710 if (IS_I965GM(i915) || IS_I965G(i915)) {
711 /* 965gm cannot relocate objects above 4GiB. */
712 mask &= ~__GFP_HIGHMEM;
713 mask |= __GFP_DMA32;
714 }
715
716 #ifdef __linux__
717 mapping = obj->base.filp->f_mapping;
718 mapping_set_gfp_mask(mapping, mask);
719 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
720 #endif
721
722 i915_gem_object_init(obj, &i915_gem_shmem_ops, &lock_class, flags);
723 obj->mem_flags |= I915_BO_FLAG_STRUCT_PAGE;
724 obj->write_domain = I915_GEM_DOMAIN_CPU;
725 obj->read_domains = I915_GEM_DOMAIN_CPU;
726
727 /*
728 * MTL doesn't snoop CPU cache by default for GPU access (namely
729 * 1-way coherency). However some UMD's are currently depending on
730 * that. Make 1-way coherent the default setting for MTL. A follow
731 * up patch will extend the GEM_CREATE uAPI to allow UMD's specify
732 * caching mode at BO creation time
733 */
734 if (HAS_LLC(i915) || (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70)))
735 /* On some devices, we can have the GPU use the LLC (the CPU
736 * cache) for about a 10% performance improvement
737 * compared to uncached. Graphics requests other than
738 * display scanout are coherent with the CPU in
739 * accessing this cache. This means in this mode we
740 * don't need to clflush on the CPU side, and on the
741 * GPU side we only need to flush internal caches to
742 * get data visible to the CPU.
743 *
744 * However, we maintain the display planes as UC, and so
745 * need to rebind when first used as such.
746 */
747 cache_level = I915_CACHE_LLC;
748 else
749 cache_level = I915_CACHE_NONE;
750
751 i915_gem_object_set_cache_coherency(obj, cache_level);
752
753 i915_gem_object_init_memory_region(obj, mem);
754
755 return 0;
756 }
757
758 struct drm_i915_gem_object *
i915_gem_object_create_shmem(struct drm_i915_private * i915,resource_size_t size)759 i915_gem_object_create_shmem(struct drm_i915_private *i915,
760 resource_size_t size)
761 {
762 return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_SMEM],
763 size, 0, 0);
764 }
765
766 /* Allocate a new GEM object and fill it with the supplied data */
767 #ifdef __linux__
768 struct drm_i915_gem_object *
i915_gem_object_create_shmem_from_data(struct drm_i915_private * i915,const void * data,resource_size_t size)769 i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915,
770 const void *data, resource_size_t size)
771 {
772 struct drm_i915_gem_object *obj;
773 struct file *file;
774 const struct address_space_operations *aops;
775 loff_t pos;
776 int err;
777
778 GEM_WARN_ON(IS_DGFX(i915));
779 obj = i915_gem_object_create_shmem(i915, round_up(size, PAGE_SIZE));
780 if (IS_ERR(obj))
781 return obj;
782
783 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
784
785 file = obj->base.filp;
786 aops = file->f_mapping->a_ops;
787 pos = 0;
788 do {
789 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
790 struct folio *folio;
791 void *fsdata;
792
793 err = aops->write_begin(file, file->f_mapping, pos, len,
794 &folio, &fsdata);
795 if (err < 0)
796 goto fail;
797
798 memcpy_to_folio(folio, offset_in_folio(folio, pos), data, len);
799
800 err = aops->write_end(file, file->f_mapping, pos, len, len,
801 folio, fsdata);
802 if (err < 0)
803 goto fail;
804
805 size -= len;
806 data += len;
807 pos += len;
808 } while (size);
809
810 return obj;
811
812 fail:
813 i915_gem_object_put(obj);
814 return ERR_PTR(err);
815 }
816 #else /* !__linux__ */
817 struct drm_i915_gem_object *
i915_gem_object_create_shmem_from_data(struct drm_i915_private * dev_priv,const void * data,resource_size_t size)818 i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
819 const void *data, resource_size_t size)
820 {
821 struct drm_i915_gem_object *obj;
822 struct uvm_object *uao;
823 resource_size_t offset;
824 int err;
825
826 GEM_WARN_ON(IS_DGFX(dev_priv));
827 obj = i915_gem_object_create_shmem(dev_priv, round_up(size, PAGE_SIZE));
828 if (IS_ERR(obj))
829 return obj;
830
831 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
832
833 uao = obj->base.uao;
834 offset = 0;
835 do {
836 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
837 struct vm_page *page;
838 void *pgdata, *vaddr;
839 struct pglist plist;
840
841 TAILQ_INIT(&plist);
842 if (uvm_obj_wire(uao, trunc_page(offset),
843 trunc_page(offset) + PAGE_SIZE, &plist)) {
844 err = -ENOMEM;
845 goto fail;
846 }
847 page = TAILQ_FIRST(&plist);
848
849 vaddr = kmap(page);
850 memcpy(vaddr, data, len);
851 kunmap_va(vaddr);
852
853 uvm_obj_unwire(uao, trunc_page(offset),
854 trunc_page(offset) + PAGE_SIZE);
855
856 size -= len;
857 data += len;
858 offset += len;
859 } while (size);
860
861 return obj;
862
863 fail:
864 i915_gem_object_put(obj);
865 return ERR_PTR(err);
866 }
867 #endif
868
init_shmem(struct intel_memory_region * mem)869 static int init_shmem(struct intel_memory_region *mem)
870 {
871 i915_gemfs_init(mem->i915);
872 intel_memory_region_set_name(mem, "system");
873
874 return 0; /* We have fallback to the kernel mnt if gemfs init failed. */
875 }
876
release_shmem(struct intel_memory_region * mem)877 static int release_shmem(struct intel_memory_region *mem)
878 {
879 i915_gemfs_fini(mem->i915);
880 return 0;
881 }
882
883 static const struct intel_memory_region_ops shmem_region_ops = {
884 .init = init_shmem,
885 .release = release_shmem,
886 .init_object = shmem_object_init,
887 };
888
i915_gem_shmem_setup(struct drm_i915_private * i915,u16 type,u16 instance)889 struct intel_memory_region *i915_gem_shmem_setup(struct drm_i915_private *i915,
890 u16 type, u16 instance)
891 {
892 return intel_memory_region_create(i915, 0,
893 totalram_pages() << PAGE_SHIFT,
894 PAGE_SIZE, 0, 0,
895 type, instance,
896 &shmem_region_ops);
897 }
898
i915_gem_object_is_shmem(const struct drm_i915_gem_object * obj)899 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj)
900 {
901 return obj->ops == &i915_gem_shmem_ops;
902 }
903