xref: /linux/drivers/gpu/drm/i915/gem/i915_gem_shmem.c (revision 1c9982b4961334c1edb0745a04cabd34bc2de675)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2014-2016 Intel Corporation
4  */
5 
6 #include <linux/pagevec.h>
7 #include <linux/shmem_fs.h>
8 #include <linux/swap.h>
9 #include <linux/uio.h>
10 
11 #include <drm/drm_cache.h>
12 #include <drm/drm_gem.h>
13 #include <drm/drm_print.h>
14 
15 #include "gem/i915_gem_region.h"
16 #include "i915_drv.h"
17 #include "i915_gem_object.h"
18 #include "i915_gem_tiling.h"
19 #include "i915_scatterlist.h"
20 #include "i915_trace.h"
21 #include "i915_utils.h"
22 
23 /*
24  * Move folios to appropriate lru and release the batch, decrementing the
25  * ref count of those folios.
26  */
check_release_folio_batch(struct folio_batch * fbatch)27 static void check_release_folio_batch(struct folio_batch *fbatch)
28 {
29 	check_move_unevictable_folios(fbatch);
30 	__folio_batch_release(fbatch);
31 	cond_resched();
32 }
33 
shmem_sg_free_table(struct sg_table * st,struct address_space * mapping,bool dirty,bool backup)34 void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping,
35 			 bool dirty, bool backup)
36 {
37 	struct sgt_iter sgt_iter;
38 	struct folio_batch fbatch;
39 	struct folio *last = NULL;
40 	struct page *page;
41 
42 	mapping_clear_unevictable(mapping);
43 
44 	folio_batch_init(&fbatch);
45 	for_each_sgt_page(page, sgt_iter, st) {
46 		struct folio *folio = page_folio(page);
47 
48 		if (folio == last)
49 			continue;
50 		last = folio;
51 		if (dirty)
52 			folio_mark_dirty(folio);
53 		if (backup)
54 			folio_mark_accessed(folio);
55 
56 		if (!folio_batch_add(&fbatch, folio))
57 			check_release_folio_batch(&fbatch);
58 	}
59 	if (fbatch.nr)
60 		check_release_folio_batch(&fbatch);
61 
62 	sg_free_table(st);
63 }
64 
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)65 int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
66 			 size_t size, struct intel_memory_region *mr,
67 			 struct address_space *mapping,
68 			 unsigned int max_segment)
69 {
70 	unsigned int page_count; /* restricted by sg_alloc_table */
71 	unsigned long i;
72 	struct scatterlist *sg;
73 	unsigned long next_pfn = 0;	/* suppress gcc warning */
74 	gfp_t noreclaim;
75 	int ret;
76 
77 	if (overflows_type(size / PAGE_SIZE, page_count))
78 		return -E2BIG;
79 
80 	page_count = size / PAGE_SIZE;
81 	/*
82 	 * If there's no chance of allocating enough pages for the whole
83 	 * object, bail early.
84 	 */
85 	if (size > resource_size(&mr->region))
86 		return -ENOMEM;
87 
88 	if (sg_alloc_table(st, page_count, GFP_KERNEL | __GFP_NOWARN))
89 		return -ENOMEM;
90 
91 	/*
92 	 * Get the list of pages out of our struct file.  They'll be pinned
93 	 * at this point until we release them.
94 	 *
95 	 * Fail silently without starting the shrinker
96 	 */
97 	mapping_set_unevictable(mapping);
98 	noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
99 	noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
100 
101 	sg = st->sgl;
102 	st->nents = 0;
103 	for (i = 0; i < page_count; i++) {
104 		struct folio *folio;
105 		unsigned long nr_pages;
106 		const unsigned int shrink[] = {
107 			I915_SHRINK_BOUND | I915_SHRINK_UNBOUND,
108 			0,
109 		}, *s = shrink;
110 		gfp_t gfp = noreclaim;
111 
112 		do {
113 			cond_resched();
114 			folio = shmem_read_folio_gfp(mapping, i, gfp);
115 			if (!IS_ERR(folio))
116 				break;
117 
118 			if (!*s) {
119 				ret = PTR_ERR(folio);
120 				goto err_sg;
121 			}
122 
123 			i915_gem_shrink(NULL, i915, 2 * page_count, NULL, *s++);
124 
125 			/*
126 			 * We've tried hard to allocate the memory by reaping
127 			 * our own buffer, now let the real VM do its job and
128 			 * go down in flames if truly OOM.
129 			 *
130 			 * However, since graphics tend to be disposable,
131 			 * defer the oom here by reporting the ENOMEM back
132 			 * to userspace.
133 			 */
134 			if (!*s) {
135 				/* reclaim and warn, but no oom */
136 				gfp = mapping_gfp_mask(mapping);
137 
138 				/*
139 				 * Our bo are always dirty and so we require
140 				 * kswapd to reclaim our pages (direct reclaim
141 				 * does not effectively begin pageout of our
142 				 * buffers on its own). However, direct reclaim
143 				 * only waits for kswapd when under allocation
144 				 * congestion. So as a result __GFP_RECLAIM is
145 				 * unreliable and fails to actually reclaim our
146 				 * dirty pages -- unless you try over and over
147 				 * again with !__GFP_NORETRY. However, we still
148 				 * want to fail this allocation rather than
149 				 * trigger the out-of-memory killer and for
150 				 * this we want __GFP_RETRY_MAYFAIL.
151 				 */
152 				gfp |= __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
153 			}
154 		} while (1);
155 
156 		nr_pages = min_array(((unsigned long[]) {
157 					folio_nr_pages(folio),
158 					page_count - i,
159 					max_segment / PAGE_SIZE,
160 				      }), 3);
161 
162 		if (!i ||
163 		    sg->length >= max_segment ||
164 		    folio_pfn(folio) != next_pfn) {
165 			if (i)
166 				sg = sg_next(sg);
167 
168 			st->nents++;
169 			sg_set_folio(sg, folio, nr_pages * PAGE_SIZE, 0);
170 		} else {
171 			nr_pages = min_t(unsigned long, nr_pages,
172 					 (max_segment - sg->length) / PAGE_SIZE);
173 
174 			sg->length += nr_pages * PAGE_SIZE;
175 		}
176 		next_pfn = folio_pfn(folio) + nr_pages;
177 		i += nr_pages - 1;
178 
179 		/* Check that the i965g/gm workaround works. */
180 		GEM_BUG_ON(gfp & __GFP_DMA32 && next_pfn >= 0x00100000UL);
181 	}
182 	if (sg) /* loop terminated early; short sg table */
183 		sg_mark_end(sg);
184 
185 	/* Trim unused sg entries to avoid wasting memory. */
186 	i915_sg_trim(st);
187 
188 	return 0;
189 err_sg:
190 	sg_mark_end(sg);
191 	if (sg != st->sgl) {
192 		shmem_sg_free_table(st, mapping, false, false);
193 	} else {
194 		mapping_clear_unevictable(mapping);
195 		sg_free_table(st);
196 	}
197 
198 	/*
199 	 * shmemfs first checks if there is enough memory to allocate the page
200 	 * and reports ENOSPC should there be insufficient, along with the usual
201 	 * ENOMEM for a genuine allocation failure.
202 	 *
203 	 * We use ENOSPC in our driver to mean that we have run out of aperture
204 	 * space and so want to translate the error from shmemfs back to our
205 	 * usual understanding of ENOMEM.
206 	 */
207 	if (ret == -ENOSPC)
208 		ret = -ENOMEM;
209 
210 	return ret;
211 }
212 
shmem_get_pages(struct drm_i915_gem_object * obj)213 static int shmem_get_pages(struct drm_i915_gem_object *obj)
214 {
215 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
216 	struct intel_memory_region *mem = obj->mm.region;
217 	struct address_space *mapping = obj->base.filp->f_mapping;
218 	unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
219 	struct sg_table *st;
220 	int ret;
221 
222 	/*
223 	 * Assert that the object is not currently in any GPU domain. As it
224 	 * wasn't in the GTT, there shouldn't be any way it could have been in
225 	 * a GPU cache
226 	 */
227 	GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
228 	GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
229 
230 rebuild_st:
231 	st = kmalloc_obj(*st, GFP_KERNEL | __GFP_NOWARN);
232 	if (!st)
233 		return -ENOMEM;
234 
235 	ret = shmem_sg_alloc_table(i915, st, obj->base.size, mem, mapping,
236 				   max_segment);
237 	if (ret)
238 		goto err_st;
239 
240 	ret = i915_gem_gtt_prepare_pages(obj, st);
241 	if (ret) {
242 		/*
243 		 * DMA remapping failed? One possible cause is that
244 		 * it could not reserve enough large entries, asking
245 		 * for PAGE_SIZE chunks instead may be helpful.
246 		 */
247 		if (max_segment > PAGE_SIZE) {
248 			shmem_sg_free_table(st, mapping, false, false);
249 			kfree(st);
250 
251 			max_segment = PAGE_SIZE;
252 			goto rebuild_st;
253 		} else {
254 			dev_warn(i915->drm.dev,
255 				 "Failed to DMA remap %zu pages\n",
256 				 obj->base.size >> PAGE_SHIFT);
257 			goto err_pages;
258 		}
259 	}
260 
261 	if (i915_gem_object_needs_bit17_swizzle(obj))
262 		i915_gem_object_do_bit_17_swizzle(obj, st);
263 
264 	if (i915_gem_object_can_bypass_llc(obj))
265 		obj->cache_dirty = true;
266 
267 	__i915_gem_object_set_pages(obj, st);
268 
269 	return 0;
270 
271 err_pages:
272 	shmem_sg_free_table(st, mapping, false, false);
273 	/*
274 	 * shmemfs first checks if there is enough memory to allocate the page
275 	 * and reports ENOSPC should there be insufficient, along with the usual
276 	 * ENOMEM for a genuine allocation failure.
277 	 *
278 	 * We use ENOSPC in our driver to mean that we have run out of aperture
279 	 * space and so want to translate the error from shmemfs back to our
280 	 * usual understanding of ENOMEM.
281 	 */
282 err_st:
283 	if (ret == -ENOSPC)
284 		ret = -ENOMEM;
285 
286 	kfree(st);
287 
288 	return ret;
289 }
290 
291 static int
shmem_truncate(struct drm_i915_gem_object * obj)292 shmem_truncate(struct drm_i915_gem_object *obj)
293 {
294 	/*
295 	 * Our goal here is to return as much of the memory as
296 	 * is possible back to the system as we are called from OOM.
297 	 * To do this we must instruct the shmfs to drop all of its
298 	 * backing pages, *now*.
299 	 */
300 	shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
301 	obj->mm.madv = __I915_MADV_PURGED;
302 	obj->mm.pages = ERR_PTR(-EFAULT);
303 
304 	return 0;
305 }
306 
__shmem_writeback(size_t size,struct address_space * mapping)307 void __shmem_writeback(size_t size, struct address_space *mapping)
308 {
309 	struct writeback_control wbc = {
310 		.sync_mode = WB_SYNC_NONE,
311 		.nr_to_write = SWAP_CLUSTER_MAX,
312 		.range_start = 0,
313 		.range_end = LLONG_MAX,
314 	};
315 	struct folio *folio = NULL;
316 	int error = 0;
317 
318 	/*
319 	 * Leave mmapings intact (GTT will have been revoked on unbinding,
320 	 * leaving only CPU mmapings around) and add those folios to the LRU
321 	 * instead of invoking writeback so they are aged and paged out
322 	 * as normal.
323 	 */
324 	while ((folio = writeback_iter(mapping, &wbc, folio, &error))) {
325 		if (folio_mapped(folio))
326 			folio_redirty_for_writepage(&wbc, folio);
327 		else
328 			error = shmem_writeout(folio, NULL, NULL);
329 	}
330 }
331 
332 static void
shmem_writeback(struct drm_i915_gem_object * obj)333 shmem_writeback(struct drm_i915_gem_object *obj)
334 {
335 	__shmem_writeback(obj->base.size, obj->base.filp->f_mapping);
336 }
337 
shmem_shrink(struct drm_i915_gem_object * obj,unsigned int flags)338 static int shmem_shrink(struct drm_i915_gem_object *obj, unsigned int flags)
339 {
340 	switch (obj->mm.madv) {
341 	case I915_MADV_DONTNEED:
342 		return i915_gem_object_truncate(obj);
343 	case __I915_MADV_PURGED:
344 		return 0;
345 	}
346 
347 	if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK)
348 		shmem_writeback(obj);
349 
350 	return 0;
351 }
352 
353 void
__i915_gem_object_release_shmem(struct drm_i915_gem_object * obj,struct sg_table * pages,bool needs_clflush)354 __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
355 				struct sg_table *pages,
356 				bool needs_clflush)
357 {
358 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
359 
360 	GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
361 
362 	if (obj->mm.madv == I915_MADV_DONTNEED)
363 		obj->mm.dirty = false;
364 
365 	if (needs_clflush &&
366 	    (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
367 	    !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
368 		drm_clflush_sg(pages);
369 
370 	__start_cpu_write(obj);
371 	/*
372 	 * On non-LLC igfx platforms, force the flush-on-acquire if this is ever
373 	 * swapped-in. Our async flush path is not trust worthy enough yet(and
374 	 * happens in the wrong order), and with some tricks it's conceivable
375 	 * for userspace to change the cache-level to I915_CACHE_NONE after the
376 	 * pages are swapped-in, and since execbuf binds the object before doing
377 	 * the async flush, we have a race window.
378 	 */
379 	if (!HAS_LLC(i915) && !IS_DGFX(i915))
380 		obj->cache_dirty = true;
381 }
382 
i915_gem_object_put_pages_shmem(struct drm_i915_gem_object * obj,struct sg_table * pages)383 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, struct sg_table *pages)
384 {
385 	__i915_gem_object_release_shmem(obj, pages, true);
386 
387 	i915_gem_gtt_finish_pages(obj, pages);
388 
389 	if (i915_gem_object_needs_bit17_swizzle(obj))
390 		i915_gem_object_save_bit_17_swizzle(obj, pages);
391 
392 	shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping,
393 			    obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED);
394 	kfree(pages);
395 	obj->mm.dirty = false;
396 }
397 
398 static void
shmem_put_pages(struct drm_i915_gem_object * obj,struct sg_table * pages)399 shmem_put_pages(struct drm_i915_gem_object *obj, struct sg_table *pages)
400 {
401 	if (likely(i915_gem_object_has_struct_page(obj)))
402 		i915_gem_object_put_pages_shmem(obj, pages);
403 	else
404 		i915_gem_object_put_pages_phys(obj, pages);
405 }
406 
407 static int
shmem_pwrite(struct drm_i915_gem_object * obj,const struct drm_i915_gem_pwrite * arg)408 shmem_pwrite(struct drm_i915_gem_object *obj,
409 	     const struct drm_i915_gem_pwrite *arg)
410 {
411 	char __user *user_data = u64_to_user_ptr(arg->data_ptr);
412 	struct file *file = obj->base.filp;
413 	struct kiocb kiocb;
414 	struct iov_iter iter;
415 	ssize_t written;
416 	u64 size = arg->size;
417 
418 	/* Caller already validated user args */
419 	GEM_BUG_ON(!access_ok(user_data, arg->size));
420 
421 	if (!i915_gem_object_has_struct_page(obj))
422 		return i915_gem_object_pwrite_phys(obj, arg);
423 
424 	/*
425 	 * Before we instantiate/pin the backing store for our use, we
426 	 * can prepopulate the shmemfs filp efficiently using a write into
427 	 * the pagecache. We avoid the penalty of instantiating all the
428 	 * pages, important if the user is just writing to a few and never
429 	 * uses the object on the GPU, and using a direct write into shmemfs
430 	 * allows it to avoid the cost of retrieving a page (either swapin
431 	 * or clearing-before-use) before it is overwritten.
432 	 */
433 	if (i915_gem_object_has_pages(obj))
434 		return -ENODEV;
435 
436 	if (obj->mm.madv != I915_MADV_WILLNEED)
437 		return -EFAULT;
438 
439 	if (size > MAX_RW_COUNT)
440 		return -EFBIG;
441 
442 	if (!file->f_op->write_iter)
443 		return -EINVAL;
444 
445 	init_sync_kiocb(&kiocb, file);
446 	kiocb.ki_pos = arg->offset;
447 	iov_iter_ubuf(&iter, ITER_SOURCE, (void __user *)user_data, size);
448 
449 	written = file->f_op->write_iter(&kiocb, &iter);
450 	BUG_ON(written == -EIOCBQUEUED);
451 
452 	/*
453 	 * First, check if write_iter returned a negative error.
454 	 * If the write failed, return the real error code immediately.
455 	 * This prevents it from being overwritten by the short write check below.
456 	 */
457 	if (written < 0)
458 		return written;
459 	/*
460 	 * Check for a short write (written bytes != requested size).
461 	 * Even if some data was written, return -EIO to indicate that the
462 	 * write was not fully completed.
463 	 */
464 	if (written != size)
465 		return -EIO;
466 
467 	return 0;
468 }
469 
470 static int
shmem_pread(struct drm_i915_gem_object * obj,const struct drm_i915_gem_pread * arg)471 shmem_pread(struct drm_i915_gem_object *obj,
472 	    const struct drm_i915_gem_pread *arg)
473 {
474 	if (!i915_gem_object_has_struct_page(obj))
475 		return i915_gem_object_pread_phys(obj, arg);
476 
477 	return -ENODEV;
478 }
479 
shmem_release(struct drm_i915_gem_object * obj)480 static void shmem_release(struct drm_i915_gem_object *obj)
481 {
482 	if (i915_gem_object_has_struct_page(obj))
483 		i915_gem_object_release_memory_region(obj);
484 
485 	fput(obj->base.filp);
486 }
487 
488 const struct drm_i915_gem_object_ops i915_gem_shmem_ops = {
489 	.name = "i915_gem_object_shmem",
490 	.flags = I915_GEM_OBJECT_IS_SHRINKABLE,
491 
492 	.get_pages = shmem_get_pages,
493 	.put_pages = shmem_put_pages,
494 	.truncate = shmem_truncate,
495 	.shrink = shmem_shrink,
496 
497 	.pwrite = shmem_pwrite,
498 	.pread = shmem_pread,
499 
500 	.release = shmem_release,
501 };
502 
__create_shmem(struct drm_i915_private * i915,struct drm_gem_object * obj,resource_size_t size,unsigned int flags)503 static int __create_shmem(struct drm_i915_private *i915,
504 			  struct drm_gem_object *obj,
505 			  resource_size_t size,
506 			  unsigned int flags)
507 {
508 	const vma_flags_t shmem_flags = mk_vma_flags(VMA_NORESERVE_BIT);
509 	struct vfsmount *huge_mnt;
510 	struct file *filp;
511 
512 	drm_gem_private_object_init(&i915->drm, obj, size);
513 
514 	/* XXX: The __shmem_file_setup() function returns -EINVAL if size is
515 	 * greater than MAX_LFS_FILESIZE.
516 	 * To handle the same error as other code that returns -E2BIG when
517 	 * the size is too large, we add a code that returns -E2BIG when the
518 	 * size is larger than the size that can be handled.
519 	 * If BITS_PER_LONG is 32, size > MAX_LFS_FILESIZE is always false,
520 	 * so we only needs to check when BITS_PER_LONG is 64.
521 	 * If BITS_PER_LONG is 32, E2BIG checks are processed when
522 	 * i915_gem_object_size_2big() is called before init_object() callback
523 	 * is called.
524 	 */
525 	if (BITS_PER_LONG == 64 && size > MAX_LFS_FILESIZE)
526 		return -E2BIG;
527 
528 	huge_mnt = drm_gem_get_huge_mnt(&i915->drm);
529 	if (!(flags & I915_BO_ALLOC_NOTHP) && huge_mnt)
530 		filp = shmem_file_setup_with_mnt(huge_mnt, "i915", size,
531 						 shmem_flags);
532 	else
533 		filp = shmem_file_setup("i915", size, shmem_flags);
534 	if (IS_ERR(filp))
535 		return PTR_ERR(filp);
536 
537 	/*
538 	 * Prevent -EFBIG by allowing large writes beyond MAX_NON_LFS on shmem
539 	 * objects by setting O_LARGEFILE.
540 	 */
541 	if (force_o_largefile())
542 		filp->f_flags |= O_LARGEFILE;
543 
544 	obj->filp = filp;
545 	return 0;
546 }
547 
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)548 static int shmem_object_init(struct intel_memory_region *mem,
549 			     struct drm_i915_gem_object *obj,
550 			     resource_size_t offset,
551 			     resource_size_t size,
552 			     resource_size_t page_size,
553 			     unsigned int flags)
554 {
555 	static struct lock_class_key lock_class;
556 	struct drm_i915_private *i915 = mem->i915;
557 	struct address_space *mapping;
558 	unsigned int cache_level;
559 	gfp_t mask;
560 	int ret;
561 
562 	ret = __create_shmem(i915, &obj->base, size, flags);
563 	if (ret)
564 		return ret;
565 
566 	mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
567 	if (IS_I965GM(i915) || IS_I965G(i915)) {
568 		/* 965gm cannot relocate objects above 4GiB. */
569 		mask &= ~__GFP_HIGHMEM;
570 		mask |= __GFP_DMA32;
571 	}
572 
573 	mapping = obj->base.filp->f_mapping;
574 	mapping_set_gfp_mask(mapping, mask);
575 	GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
576 
577 	i915_gem_object_init(obj, &i915_gem_shmem_ops, &lock_class, flags);
578 	obj->mem_flags |= I915_BO_FLAG_STRUCT_PAGE;
579 	obj->write_domain = I915_GEM_DOMAIN_CPU;
580 	obj->read_domains = I915_GEM_DOMAIN_CPU;
581 
582 	/*
583 	 * MTL doesn't snoop CPU cache by default for GPU access (namely
584 	 * 1-way coherency). However some UMD's are currently depending on
585 	 * that. Make 1-way coherent the default setting for MTL. A follow
586 	 * up patch will extend the GEM_CREATE uAPI to allow UMD's specify
587 	 * caching mode at BO creation time
588 	 */
589 	if (HAS_LLC(i915) || (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70)))
590 		/* On some devices, we can have the GPU use the LLC (the CPU
591 		 * cache) for about a 10% performance improvement
592 		 * compared to uncached.  Graphics requests other than
593 		 * display scanout are coherent with the CPU in
594 		 * accessing this cache.  This means in this mode we
595 		 * don't need to clflush on the CPU side, and on the
596 		 * GPU side we only need to flush internal caches to
597 		 * get data visible to the CPU.
598 		 *
599 		 * However, we maintain the display planes as UC, and so
600 		 * need to rebind when first used as such.
601 		 */
602 		cache_level = I915_CACHE_LLC;
603 	else
604 		cache_level = I915_CACHE_NONE;
605 
606 	i915_gem_object_set_cache_coherency(obj, cache_level);
607 
608 	i915_gem_object_init_memory_region(obj, mem);
609 
610 	return 0;
611 }
612 
613 struct drm_i915_gem_object *
i915_gem_object_create_shmem(struct drm_i915_private * i915,resource_size_t size)614 i915_gem_object_create_shmem(struct drm_i915_private *i915,
615 			     resource_size_t size)
616 {
617 	return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_SMEM],
618 					     size, 0, 0);
619 }
620 
621 /* Allocate a new GEM object and fill it with the supplied data */
622 struct drm_i915_gem_object *
i915_gem_object_create_shmem_from_data(struct drm_i915_private * i915,const void * data,resource_size_t size)623 i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915,
624 				       const void *data, resource_size_t size)
625 {
626 	struct drm_i915_gem_object *obj;
627 	struct file *file;
628 	loff_t pos = 0;
629 	ssize_t err;
630 
631 	GEM_WARN_ON(IS_DGFX(i915));
632 	obj = i915_gem_object_create_shmem(i915, round_up(size, PAGE_SIZE));
633 	if (IS_ERR(obj))
634 		return obj;
635 
636 	GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
637 
638 	file = obj->base.filp;
639 	err = kernel_write(file, data, size, &pos);
640 
641 	if (err < 0)
642 		goto fail;
643 
644 	if (err != size) {
645 		err = -EIO;
646 		goto fail;
647 	}
648 
649 	return obj;
650 
651 fail:
652 	i915_gem_object_put(obj);
653 	return ERR_PTR(err);
654 }
655 
init_shmem(struct intel_memory_region * mem)656 static int init_shmem(struct intel_memory_region *mem)
657 {
658 	struct drm_i915_private *i915 = mem->i915;
659 
660 	/*
661 	 * By creating our own shmemfs mountpoint, we can pass in
662 	 * mount flags that better match our usecase.
663 	 *
664 	 * One example, although it is probably better with a per-file
665 	 * control, is selecting huge page allocations ("huge=within_size").
666 	 * However, we only do so on platforms which benefit from it, or to
667 	 * offset the overhead of iommu lookups, where with latter it is a net
668 	 * win even on platforms which would otherwise see some performance
669 	 * regressions such a slow reads issue on Broadwell and Skylake.
670 	 */
671 
672 	if (GRAPHICS_VER(i915) < 11 && !i915_vtd_active(i915))
673 		goto no_thp;
674 
675 	drm_gem_huge_mnt_create(&i915->drm, "within_size");
676 	if (drm_gem_get_huge_mnt(&i915->drm))
677 		drm_info(&i915->drm, "Using Transparent Hugepages\n");
678 	else
679 		drm_notice(&i915->drm,
680 			   "Transparent Hugepage support is recommended for optimal performance%s\n",
681 			   GRAPHICS_VER(i915) >= 11 ? " on this platform!" :
682 						      " when IOMMU is enabled!");
683 
684  no_thp:
685 	intel_memory_region_set_name(mem, "system");
686 
687 	return 0; /* We have fallback to the kernel mnt if huge mnt failed. */
688 }
689 
690 static const struct intel_memory_region_ops shmem_region_ops = {
691 	.init = init_shmem,
692 	.init_object = shmem_object_init,
693 };
694 
i915_gem_shmem_setup(struct drm_i915_private * i915,u16 type,u16 instance)695 struct intel_memory_region *i915_gem_shmem_setup(struct drm_i915_private *i915,
696 						 u16 type, u16 instance)
697 {
698 	return intel_memory_region_create(i915, 0,
699 					  totalram_pages() << PAGE_SHIFT,
700 					  PAGE_SIZE, 0, 0,
701 					  type, instance,
702 					  &shmem_region_ops);
703 }
704 
i915_gem_object_is_shmem(const struct drm_i915_gem_object * obj)705 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj)
706 {
707 	return obj->ops == &i915_gem_shmem_ops;
708 }
709