18475355fSChris Wilson /*
28475355fSChris Wilson  * SPDX-License-Identifier: MIT
38475355fSChris Wilson  *
48475355fSChris Wilson  * Copyright © 2014-2016 Intel Corporation
58475355fSChris Wilson  */
68475355fSChris Wilson 
78475355fSChris Wilson #include <linux/pagevec.h>
882508de2SJani Nikula #include <linux/shmem_fs.h>
98475355fSChris Wilson #include <linux/swap.h>
108475355fSChris Wilson 
115f2ec909SJani Nikula #include <drm/drm_cache.h>
125f2ec909SJani Nikula 
13da1184cdSMatthew Auld #include "gem/i915_gem_region.h"
148475355fSChris Wilson #include "i915_drv.h"
158475355fSChris Wilson #include "i915_gem_object.h"
160438fd1aSJani Nikula #include "i915_gem_tiling.h"
170438fd1aSJani Nikula #include "i915_gemfs.h"
1837d63f8fSChris Wilson #include "i915_scatterlist.h"
19a09d9a80SJani Nikula #include "i915_trace.h"
208475355fSChris Wilson 
218475355fSChris Wilson /*
228475355fSChris Wilson  * Move pages to appropriate lru and release the pagevec, decrementing the
238475355fSChris Wilson  * ref count of those pages.
248475355fSChris Wilson  */
258475355fSChris Wilson static void check_release_pagevec(struct pagevec *pvec)
268475355fSChris Wilson {
278475355fSChris Wilson 	check_move_unevictable_pages(pvec);
288475355fSChris Wilson 	__pagevec_release(pvec);
298475355fSChris Wilson 	cond_resched();
308475355fSChris Wilson }
318475355fSChris Wilson 
32cad7109aSThomas Hellström void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping,
33f05b985eSThomas Hellström 			 bool dirty, bool backup)
348475355fSChris Wilson {
35f05b985eSThomas Hellström 	struct sgt_iter sgt_iter;
36f05b985eSThomas Hellström 	struct pagevec pvec;
37f05b985eSThomas Hellström 	struct page *page;
38f05b985eSThomas Hellström 
39f05b985eSThomas Hellström 	mapping_clear_unevictable(mapping);
40f05b985eSThomas Hellström 
41f05b985eSThomas Hellström 	pagevec_init(&pvec);
42f05b985eSThomas Hellström 	for_each_sgt_page(page, sgt_iter, st) {
43f05b985eSThomas Hellström 		if (dirty)
44f05b985eSThomas Hellström 			set_page_dirty(page);
45f05b985eSThomas Hellström 
46f05b985eSThomas Hellström 		if (backup)
47f05b985eSThomas Hellström 			mark_page_accessed(page);
48f05b985eSThomas Hellström 
49f05b985eSThomas Hellström 		if (!pagevec_add(&pvec, page))
50f05b985eSThomas Hellström 			check_release_pagevec(&pvec);
51f05b985eSThomas Hellström 	}
52f05b985eSThomas Hellström 	if (pagevec_count(&pvec))
53f05b985eSThomas Hellström 		check_release_pagevec(&pvec);
54f05b985eSThomas Hellström 
55f05b985eSThomas Hellström 	sg_free_table(st);
56f05b985eSThomas Hellström }
57f05b985eSThomas Hellström 
58cad7109aSThomas Hellström int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
59f05b985eSThomas Hellström 			 size_t size, struct intel_memory_region *mr,
60f05b985eSThomas Hellström 			 struct address_space *mapping,
61f05b985eSThomas Hellström 			 unsigned int max_segment)
62f05b985eSThomas Hellström {
63f05b985eSThomas Hellström 	const unsigned long page_count = size / PAGE_SIZE;
648475355fSChris Wilson 	unsigned long i;
658475355fSChris Wilson 	struct scatterlist *sg;
668475355fSChris Wilson 	struct page *page;
678475355fSChris Wilson 	unsigned long last_pfn = 0;	/* suppress gcc warning */
688475355fSChris Wilson 	gfp_t noreclaim;
698475355fSChris Wilson 	int ret;
708475355fSChris Wilson 
718475355fSChris Wilson 	/*
728475355fSChris Wilson 	 * If there's no chance of allocating enough pages for the whole
738475355fSChris Wilson 	 * object, bail early.
748475355fSChris Wilson 	 */
75f05b985eSThomas Hellström 	if (size > resource_size(&mr->region))
76cad7109aSThomas Hellström 		return -ENOMEM;
778475355fSChris Wilson 
78a8c18becSChris Wilson 	if (sg_alloc_table(st, page_count, GFP_KERNEL | __GFP_NOWARN))
79cad7109aSThomas Hellström 		return -ENOMEM;
808475355fSChris Wilson 
818475355fSChris Wilson 	/*
828475355fSChris Wilson 	 * Get the list of pages out of our struct file.  They'll be pinned
838475355fSChris Wilson 	 * at this point until we release them.
848475355fSChris Wilson 	 *
858475355fSChris Wilson 	 * Fail silently without starting the shrinker
868475355fSChris Wilson 	 */
878475355fSChris Wilson 	mapping_set_unevictable(mapping);
888475355fSChris Wilson 	noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
898475355fSChris Wilson 	noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
908475355fSChris Wilson 
918475355fSChris Wilson 	sg = st->sgl;
928475355fSChris Wilson 	st->nents = 0;
938475355fSChris Wilson 	for (i = 0; i < page_count; i++) {
948475355fSChris Wilson 		const unsigned int shrink[] = {
953b4fa964SChris Wilson 			I915_SHRINK_BOUND | I915_SHRINK_UNBOUND,
968475355fSChris Wilson 			0,
978475355fSChris Wilson 		}, *s = shrink;
988475355fSChris Wilson 		gfp_t gfp = noreclaim;
998475355fSChris Wilson 
1008475355fSChris Wilson 		do {
1018475355fSChris Wilson 			cond_resched();
1028475355fSChris Wilson 			page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1038475355fSChris Wilson 			if (!IS_ERR(page))
1048475355fSChris Wilson 				break;
1058475355fSChris Wilson 
1068475355fSChris Wilson 			if (!*s) {
1078475355fSChris Wilson 				ret = PTR_ERR(page);
1088475355fSChris Wilson 				goto err_sg;
1098475355fSChris Wilson 			}
1108475355fSChris Wilson 
111cf41a8f1SMaarten Lankhorst 			i915_gem_shrink(NULL, i915, 2 * page_count, NULL, *s++);
1128475355fSChris Wilson 
1138475355fSChris Wilson 			/*
1148475355fSChris Wilson 			 * We've tried hard to allocate the memory by reaping
1158475355fSChris Wilson 			 * our own buffer, now let the real VM do its job and
1168475355fSChris Wilson 			 * go down in flames if truly OOM.
1178475355fSChris Wilson 			 *
1188475355fSChris Wilson 			 * However, since graphics tend to be disposable,
1198475355fSChris Wilson 			 * defer the oom here by reporting the ENOMEM back
1208475355fSChris Wilson 			 * to userspace.
1218475355fSChris Wilson 			 */
1228475355fSChris Wilson 			if (!*s) {
1238475355fSChris Wilson 				/* reclaim and warn, but no oom */
1248475355fSChris Wilson 				gfp = mapping_gfp_mask(mapping);
1258475355fSChris Wilson 
1268475355fSChris Wilson 				/*
1278475355fSChris Wilson 				 * Our bo are always dirty and so we require
1288475355fSChris Wilson 				 * kswapd to reclaim our pages (direct reclaim
1298475355fSChris Wilson 				 * does not effectively begin pageout of our
1308475355fSChris Wilson 				 * buffers on its own). However, direct reclaim
1318475355fSChris Wilson 				 * only waits for kswapd when under allocation
1328475355fSChris Wilson 				 * congestion. So as a result __GFP_RECLAIM is
1338475355fSChris Wilson 				 * unreliable and fails to actually reclaim our
1348475355fSChris Wilson 				 * dirty pages -- unless you try over and over
1358475355fSChris Wilson 				 * again with !__GFP_NORETRY. However, we still
1368475355fSChris Wilson 				 * want to fail this allocation rather than
1378475355fSChris Wilson 				 * trigger the out-of-memory killer and for
1388475355fSChris Wilson 				 * this we want __GFP_RETRY_MAYFAIL.
1398475355fSChris Wilson 				 */
140a8c18becSChris Wilson 				gfp |= __GFP_RETRY_MAYFAIL | __GFP_NOWARN;
1418475355fSChris Wilson 			}
1428475355fSChris Wilson 		} while (1);
1438475355fSChris Wilson 
1448475355fSChris Wilson 		if (!i ||
1458475355fSChris Wilson 		    sg->length >= max_segment ||
1468475355fSChris Wilson 		    page_to_pfn(page) != last_pfn + 1) {
147f05b985eSThomas Hellström 			if (i)
1488475355fSChris Wilson 				sg = sg_next(sg);
149f05b985eSThomas Hellström 
1508475355fSChris Wilson 			st->nents++;
1518475355fSChris Wilson 			sg_set_page(sg, page, PAGE_SIZE, 0);
1528475355fSChris Wilson 		} else {
1538475355fSChris Wilson 			sg->length += PAGE_SIZE;
1548475355fSChris Wilson 		}
1558475355fSChris Wilson 		last_pfn = page_to_pfn(page);
1568475355fSChris Wilson 
1578475355fSChris Wilson 		/* Check that the i965g/gm workaround works. */
158ea97c4caSChris Wilson 		GEM_BUG_ON(gfp & __GFP_DMA32 && last_pfn >= 0x00100000UL);
1598475355fSChris Wilson 	}
160f05b985eSThomas Hellström 	if (sg) /* loop terminated early; short sg table */
1618475355fSChris Wilson 		sg_mark_end(sg);
1628475355fSChris Wilson 
1638475355fSChris Wilson 	/* Trim unused sg entries to avoid wasting memory. */
1648475355fSChris Wilson 	i915_sg_trim(st);
1658475355fSChris Wilson 
166cad7109aSThomas Hellström 	return 0;
167f05b985eSThomas Hellström err_sg:
168f05b985eSThomas Hellström 	sg_mark_end(sg);
169f05b985eSThomas Hellström 	if (sg != st->sgl) {
170cad7109aSThomas Hellström 		shmem_sg_free_table(st, mapping, false, false);
171f05b985eSThomas Hellström 	} else {
172f05b985eSThomas Hellström 		mapping_clear_unevictable(mapping);
173f05b985eSThomas Hellström 		sg_free_table(st);
174f05b985eSThomas Hellström 	}
175f05b985eSThomas Hellström 
176f05b985eSThomas Hellström 	/*
177f05b985eSThomas Hellström 	 * shmemfs first checks if there is enough memory to allocate the page
178f05b985eSThomas Hellström 	 * and reports ENOSPC should there be insufficient, along with the usual
179f05b985eSThomas Hellström 	 * ENOMEM for a genuine allocation failure.
180f05b985eSThomas Hellström 	 *
181f05b985eSThomas Hellström 	 * We use ENOSPC in our driver to mean that we have run out of aperture
182f05b985eSThomas Hellström 	 * space and so want to translate the error from shmemfs back to our
183f05b985eSThomas Hellström 	 * usual understanding of ENOMEM.
184f05b985eSThomas Hellström 	 */
185f05b985eSThomas Hellström 	if (ret == -ENOSPC)
186f05b985eSThomas Hellström 		ret = -ENOMEM;
187f05b985eSThomas Hellström 
188cad7109aSThomas Hellström 	return ret;
189f05b985eSThomas Hellström }
190f05b985eSThomas Hellström 
191f05b985eSThomas Hellström static int shmem_get_pages(struct drm_i915_gem_object *obj)
192f05b985eSThomas Hellström {
193f05b985eSThomas Hellström 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
194f05b985eSThomas Hellström 	struct intel_memory_region *mem = obj->mm.region;
195f05b985eSThomas Hellström 	struct address_space *mapping = obj->base.filp->f_mapping;
196f05b985eSThomas Hellström 	const unsigned long page_count = obj->base.size / PAGE_SIZE;
197*78a07fe7SRobert Beckett 	unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
198f05b985eSThomas Hellström 	struct sg_table *st;
199f05b985eSThomas Hellström 	struct sgt_iter sgt_iter;
200f05b985eSThomas Hellström 	struct page *page;
201f05b985eSThomas Hellström 	int ret;
202f05b985eSThomas Hellström 
203f05b985eSThomas Hellström 	/*
204f05b985eSThomas Hellström 	 * Assert that the object is not currently in any GPU domain. As it
205f05b985eSThomas Hellström 	 * wasn't in the GTT, there shouldn't be any way it could have been in
206f05b985eSThomas Hellström 	 * a GPU cache
207f05b985eSThomas Hellström 	 */
208f05b985eSThomas Hellström 	GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
209f05b985eSThomas Hellström 	GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
210f05b985eSThomas Hellström 
211f05b985eSThomas Hellström rebuild_st:
212a8c18becSChris Wilson 	st = kmalloc(sizeof(*st), GFP_KERNEL | __GFP_NOWARN);
213cad7109aSThomas Hellström 	if (!st)
214cad7109aSThomas Hellström 		return -ENOMEM;
215cad7109aSThomas Hellström 
216cad7109aSThomas Hellström 	ret = shmem_sg_alloc_table(i915, st, obj->base.size, mem, mapping,
217cad7109aSThomas Hellström 				   max_segment);
218cad7109aSThomas Hellström 	if (ret)
219f05b985eSThomas Hellström 		goto err_st;
220f05b985eSThomas Hellström 
2218475355fSChris Wilson 	ret = i915_gem_gtt_prepare_pages(obj, st);
2228475355fSChris Wilson 	if (ret) {
2238475355fSChris Wilson 		/*
2248475355fSChris Wilson 		 * DMA remapping failed? One possible cause is that
2258475355fSChris Wilson 		 * it could not reserve enough large entries, asking
2268475355fSChris Wilson 		 * for PAGE_SIZE chunks instead may be helpful.
2278475355fSChris Wilson 		 */
2288475355fSChris Wilson 		if (max_segment > PAGE_SIZE) {
2298475355fSChris Wilson 			for_each_sgt_page(page, sgt_iter, st)
2308475355fSChris Wilson 				put_page(page);
2318475355fSChris Wilson 			sg_free_table(st);
232f05b985eSThomas Hellström 			kfree(st);
2338475355fSChris Wilson 
2348475355fSChris Wilson 			max_segment = PAGE_SIZE;
2358475355fSChris Wilson 			goto rebuild_st;
2368475355fSChris Wilson 		} else {
2378ff5446aSThomas Zimmermann 			dev_warn(i915->drm.dev,
2388475355fSChris Wilson 				 "Failed to DMA remap %lu pages\n",
2398475355fSChris Wilson 				 page_count);
2408475355fSChris Wilson 			goto err_pages;
2418475355fSChris Wilson 		}
2428475355fSChris Wilson 	}
2438475355fSChris Wilson 
2448475355fSChris Wilson 	if (i915_gem_object_needs_bit17_swizzle(obj))
2458475355fSChris Wilson 		i915_gem_object_do_bit_17_swizzle(obj, st);
2468475355fSChris Wilson 
24730f1dccdSMatthew Auld 	if (i915_gem_object_can_bypass_llc(obj))
24813d29c82SMatthew Auld 		obj->cache_dirty = true;
24913d29c82SMatthew Auld 
250f05b985eSThomas Hellström 	__i915_gem_object_set_pages(obj, st, i915_sg_dma_sizes(st->sgl));
2518475355fSChris Wilson 
2528475355fSChris Wilson 	return 0;
2538475355fSChris Wilson 
2548475355fSChris Wilson err_pages:
255cad7109aSThomas Hellström 	shmem_sg_free_table(st, mapping, false, false);
2568475355fSChris Wilson 	/*
2578475355fSChris Wilson 	 * shmemfs first checks if there is enough memory to allocate the page
2588475355fSChris Wilson 	 * and reports ENOSPC should there be insufficient, along with the usual
2598475355fSChris Wilson 	 * ENOMEM for a genuine allocation failure.
2608475355fSChris Wilson 	 *
2618475355fSChris Wilson 	 * We use ENOSPC in our driver to mean that we have run out of aperture
2628475355fSChris Wilson 	 * space and so want to translate the error from shmemfs back to our
2638475355fSChris Wilson 	 * usual understanding of ENOMEM.
2648475355fSChris Wilson 	 */
265f05b985eSThomas Hellström err_st:
2668475355fSChris Wilson 	if (ret == -ENOSPC)
2678475355fSChris Wilson 		ret = -ENOMEM;
2688475355fSChris Wilson 
269cad7109aSThomas Hellström 	kfree(st);
270cad7109aSThomas Hellström 
2718475355fSChris Wilson 	return ret;
2728475355fSChris Wilson }
2738475355fSChris Wilson 
2747ae03459SMatthew Auld static int
275f033428dSChris Wilson shmem_truncate(struct drm_i915_gem_object *obj)
276f033428dSChris Wilson {
277f033428dSChris Wilson 	/*
278f033428dSChris Wilson 	 * Our goal here is to return as much of the memory as
279f033428dSChris Wilson 	 * is possible back to the system as we are called from OOM.
280f033428dSChris Wilson 	 * To do this we must instruct the shmfs to drop all of its
281f033428dSChris Wilson 	 * backing pages, *now*.
282f033428dSChris Wilson 	 */
283f033428dSChris Wilson 	shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
284f033428dSChris Wilson 	obj->mm.madv = __I915_MADV_PURGED;
285f033428dSChris Wilson 	obj->mm.pages = ERR_PTR(-EFAULT);
2867ae03459SMatthew Auld 
2877ae03459SMatthew Auld 	return 0;
288f033428dSChris Wilson }
289f033428dSChris Wilson 
2907ae03459SMatthew Auld void __shmem_writeback(size_t size, struct address_space *mapping)
291f033428dSChris Wilson {
292f033428dSChris Wilson 	struct writeback_control wbc = {
293f033428dSChris Wilson 		.sync_mode = WB_SYNC_NONE,
294f033428dSChris Wilson 		.nr_to_write = SWAP_CLUSTER_MAX,
295f033428dSChris Wilson 		.range_start = 0,
296f033428dSChris Wilson 		.range_end = LLONG_MAX,
297f033428dSChris Wilson 		.for_reclaim = 1,
298f033428dSChris Wilson 	};
299f033428dSChris Wilson 	unsigned long i;
300f033428dSChris Wilson 
301f033428dSChris Wilson 	/*
302f033428dSChris Wilson 	 * Leave mmapings intact (GTT will have been revoked on unbinding,
303f033428dSChris Wilson 	 * leaving only CPU mmapings around) and add those pages to the LRU
304f033428dSChris Wilson 	 * instead of invoking writeback so they are aged and paged out
305f033428dSChris Wilson 	 * as normal.
306f033428dSChris Wilson 	 */
307f033428dSChris Wilson 
308f033428dSChris Wilson 	/* Begin writeback on each dirty page */
309f05b985eSThomas Hellström 	for (i = 0; i < size >> PAGE_SHIFT; i++) {
310f033428dSChris Wilson 		struct page *page;
311f033428dSChris Wilson 
3129dfc8ff3SMatthew Wilcox (Oracle) 		page = find_lock_page(mapping, i);
3139dfc8ff3SMatthew Wilcox (Oracle) 		if (!page)
314f033428dSChris Wilson 			continue;
315f033428dSChris Wilson 
316f033428dSChris Wilson 		if (!page_mapped(page) && clear_page_dirty_for_io(page)) {
317f033428dSChris Wilson 			int ret;
318f033428dSChris Wilson 
319f033428dSChris Wilson 			SetPageReclaim(page);
320f033428dSChris Wilson 			ret = mapping->a_ops->writepage(page, &wbc);
321f033428dSChris Wilson 			if (!PageWriteback(page))
322f033428dSChris Wilson 				ClearPageReclaim(page);
323f033428dSChris Wilson 			if (!ret)
324f033428dSChris Wilson 				goto put;
325f033428dSChris Wilson 		}
326f033428dSChris Wilson 		unlock_page(page);
327f033428dSChris Wilson put:
328f033428dSChris Wilson 		put_page(page);
329f033428dSChris Wilson 	}
330f033428dSChris Wilson }
331f033428dSChris Wilson 
332f05b985eSThomas Hellström static void
333f05b985eSThomas Hellström shmem_writeback(struct drm_i915_gem_object *obj)
334f05b985eSThomas Hellström {
335f05b985eSThomas Hellström 	__shmem_writeback(obj->base.size, obj->base.filp->f_mapping);
336f05b985eSThomas Hellström }
337f05b985eSThomas Hellström 
338ffa3fe08SMatthew Auld static int shmem_shrink(struct drm_i915_gem_object *obj, unsigned int flags)
33993544177SMatthew Auld {
34093544177SMatthew Auld 	switch (obj->mm.madv) {
34193544177SMatthew Auld 	case I915_MADV_DONTNEED:
34293544177SMatthew Auld 		return i915_gem_object_truncate(obj);
34393544177SMatthew Auld 	case __I915_MADV_PURGED:
34493544177SMatthew Auld 		return 0;
34593544177SMatthew Auld 	}
34693544177SMatthew Auld 
347ffa3fe08SMatthew Auld 	if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK)
34893544177SMatthew Auld 		shmem_writeback(obj);
34993544177SMatthew Auld 
35093544177SMatthew Auld 	return 0;
35193544177SMatthew Auld }
35293544177SMatthew Auld 
3538475355fSChris Wilson void
3548475355fSChris Wilson __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
3558475355fSChris Wilson 				struct sg_table *pages,
3568475355fSChris Wilson 				bool needs_clflush)
3578475355fSChris Wilson {
358d70af579SMatthew Auld 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
359d70af579SMatthew Auld 
3608475355fSChris Wilson 	GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
3618475355fSChris Wilson 
3628475355fSChris Wilson 	if (obj->mm.madv == I915_MADV_DONTNEED)
3638475355fSChris Wilson 		obj->mm.dirty = false;
3648475355fSChris Wilson 
3658475355fSChris Wilson 	if (needs_clflush &&
3668475355fSChris Wilson 	    (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
3678475355fSChris Wilson 	    !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
3688475355fSChris Wilson 		drm_clflush_sg(pages);
3698475355fSChris Wilson 
3708475355fSChris Wilson 	__start_cpu_write(obj);
371d70af579SMatthew Auld 	/*
372d70af579SMatthew Auld 	 * On non-LLC platforms, force the flush-on-acquire if this is ever
373d70af579SMatthew Auld 	 * swapped-in. Our async flush path is not trust worthy enough yet(and
374d70af579SMatthew Auld 	 * happens in the wrong order), and with some tricks it's conceivable
375d70af579SMatthew Auld 	 * for userspace to change the cache-level to I915_CACHE_NONE after the
376d70af579SMatthew Auld 	 * pages are swapped-in, and since execbuf binds the object before doing
377d70af579SMatthew Auld 	 * the async flush, we have a race window.
378d70af579SMatthew Auld 	 */
379d70af579SMatthew Auld 	if (!HAS_LLC(i915))
380d70af579SMatthew Auld 		obj->cache_dirty = true;
3818475355fSChris Wilson }
3828475355fSChris Wilson 
383a85fffe3SMaarten Lankhorst void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, struct sg_table *pages)
3848475355fSChris Wilson {
3858475355fSChris Wilson 	__i915_gem_object_release_shmem(obj, pages, true);
3868475355fSChris Wilson 
3878475355fSChris Wilson 	i915_gem_gtt_finish_pages(obj, pages);
3888475355fSChris Wilson 
3898475355fSChris Wilson 	if (i915_gem_object_needs_bit17_swizzle(obj))
3908475355fSChris Wilson 		i915_gem_object_save_bit_17_swizzle(obj, pages);
3918475355fSChris Wilson 
392cad7109aSThomas Hellström 	shmem_sg_free_table(pages, file_inode(obj->base.filp)->i_mapping,
393f05b985eSThomas Hellström 			    obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED);
394cad7109aSThomas Hellström 	kfree(pages);
3958475355fSChris Wilson 	obj->mm.dirty = false;
3968475355fSChris Wilson }
3978475355fSChris Wilson 
398a85fffe3SMaarten Lankhorst static void
399a85fffe3SMaarten Lankhorst shmem_put_pages(struct drm_i915_gem_object *obj, struct sg_table *pages)
400a85fffe3SMaarten Lankhorst {
401a85fffe3SMaarten Lankhorst 	if (likely(i915_gem_object_has_struct_page(obj)))
402a85fffe3SMaarten Lankhorst 		i915_gem_object_put_pages_shmem(obj, pages);
403a85fffe3SMaarten Lankhorst 	else
404a85fffe3SMaarten Lankhorst 		i915_gem_object_put_pages_phys(obj, pages);
405a85fffe3SMaarten Lankhorst }
406a85fffe3SMaarten Lankhorst 
4078475355fSChris Wilson static int
4088475355fSChris Wilson shmem_pwrite(struct drm_i915_gem_object *obj,
4098475355fSChris Wilson 	     const struct drm_i915_gem_pwrite *arg)
4108475355fSChris Wilson {
4118475355fSChris Wilson 	struct address_space *mapping = obj->base.filp->f_mapping;
412c5edd542SMatthew Wilcox (Oracle) 	const struct address_space_operations *aops = mapping->a_ops;
4138475355fSChris Wilson 	char __user *user_data = u64_to_user_ptr(arg->data_ptr);
4148475355fSChris Wilson 	u64 remain, offset;
4158475355fSChris Wilson 	unsigned int pg;
4168475355fSChris Wilson 
4178475355fSChris Wilson 	/* Caller already validated user args */
4188475355fSChris Wilson 	GEM_BUG_ON(!access_ok(user_data, arg->size));
4198475355fSChris Wilson 
420a6117097SMaarten Lankhorst 	if (!i915_gem_object_has_struct_page(obj))
421a6117097SMaarten Lankhorst 		return i915_gem_object_pwrite_phys(obj, arg);
422a6117097SMaarten Lankhorst 
4238475355fSChris Wilson 	/*
4248475355fSChris Wilson 	 * Before we instantiate/pin the backing store for our use, we
4258475355fSChris Wilson 	 * can prepopulate the shmemfs filp efficiently using a write into
4268475355fSChris Wilson 	 * the pagecache. We avoid the penalty of instantiating all the
4278475355fSChris Wilson 	 * pages, important if the user is just writing to a few and never
4288475355fSChris Wilson 	 * uses the object on the GPU, and using a direct write into shmemfs
4298475355fSChris Wilson 	 * allows it to avoid the cost of retrieving a page (either swapin
4308475355fSChris Wilson 	 * or clearing-before-use) before it is overwritten.
4318475355fSChris Wilson 	 */
4328475355fSChris Wilson 	if (i915_gem_object_has_pages(obj))
4338475355fSChris Wilson 		return -ENODEV;
4348475355fSChris Wilson 
4358475355fSChris Wilson 	if (obj->mm.madv != I915_MADV_WILLNEED)
4368475355fSChris Wilson 		return -EFAULT;
4378475355fSChris Wilson 
4388475355fSChris Wilson 	/*
4398475355fSChris Wilson 	 * Before the pages are instantiated the object is treated as being
4408475355fSChris Wilson 	 * in the CPU domain. The pages will be clflushed as required before
4418475355fSChris Wilson 	 * use, and we can freely write into the pages directly. If userspace
4428475355fSChris Wilson 	 * races pwrite with any other operation; corruption will ensue -
4438475355fSChris Wilson 	 * that is userspace's prerogative!
4448475355fSChris Wilson 	 */
4458475355fSChris Wilson 
4468475355fSChris Wilson 	remain = arg->size;
4478475355fSChris Wilson 	offset = arg->offset;
4488475355fSChris Wilson 	pg = offset_in_page(offset);
4498475355fSChris Wilson 
4508475355fSChris Wilson 	do {
4518475355fSChris Wilson 		unsigned int len, unwritten;
4528475355fSChris Wilson 		struct page *page;
4538475355fSChris Wilson 		void *data, *vaddr;
4548475355fSChris Wilson 		int err;
4558475355fSChris Wilson 		char c;
4568475355fSChris Wilson 
4578475355fSChris Wilson 		len = PAGE_SIZE - pg;
4588475355fSChris Wilson 		if (len > remain)
4598475355fSChris Wilson 			len = remain;
4608475355fSChris Wilson 
4618475355fSChris Wilson 		/* Prefault the user page to reduce potential recursion */
4628475355fSChris Wilson 		err = __get_user(c, user_data);
4638475355fSChris Wilson 		if (err)
4648475355fSChris Wilson 			return err;
4658475355fSChris Wilson 
4668475355fSChris Wilson 		err = __get_user(c, user_data + len - 1);
4678475355fSChris Wilson 		if (err)
4688475355fSChris Wilson 			return err;
4698475355fSChris Wilson 
470c5edd542SMatthew Wilcox (Oracle) 		err = aops->write_begin(obj->base.filp, mapping, offset, len,
4718475355fSChris Wilson 					&page, &data);
4728475355fSChris Wilson 		if (err < 0)
4738475355fSChris Wilson 			return err;
4748475355fSChris Wilson 
4758475355fSChris Wilson 		vaddr = kmap_atomic(page);
4768475355fSChris Wilson 		unwritten = __copy_from_user_inatomic(vaddr + pg,
4778475355fSChris Wilson 						      user_data,
4788475355fSChris Wilson 						      len);
4798475355fSChris Wilson 		kunmap_atomic(vaddr);
4808475355fSChris Wilson 
481c5edd542SMatthew Wilcox (Oracle) 		err = aops->write_end(obj->base.filp, mapping, offset, len,
482c5edd542SMatthew Wilcox (Oracle) 				      len - unwritten, page, data);
4838475355fSChris Wilson 		if (err < 0)
4848475355fSChris Wilson 			return err;
4858475355fSChris Wilson 
4868475355fSChris Wilson 		/* We don't handle -EFAULT, leave it to the caller to check */
4878475355fSChris Wilson 		if (unwritten)
4888475355fSChris Wilson 			return -ENODEV;
4898475355fSChris Wilson 
4908475355fSChris Wilson 		remain -= len;
4918475355fSChris Wilson 		user_data += len;
4928475355fSChris Wilson 		offset += len;
4938475355fSChris Wilson 		pg = 0;
4948475355fSChris Wilson 	} while (remain);
4958475355fSChris Wilson 
4968475355fSChris Wilson 	return 0;
4978475355fSChris Wilson }
4988475355fSChris Wilson 
499a6117097SMaarten Lankhorst static int
500a6117097SMaarten Lankhorst shmem_pread(struct drm_i915_gem_object *obj,
501a6117097SMaarten Lankhorst 	    const struct drm_i915_gem_pread *arg)
502a6117097SMaarten Lankhorst {
503a6117097SMaarten Lankhorst 	if (!i915_gem_object_has_struct_page(obj))
504a6117097SMaarten Lankhorst 		return i915_gem_object_pread_phys(obj, arg);
505a6117097SMaarten Lankhorst 
506a6117097SMaarten Lankhorst 	return -ENODEV;
507a6117097SMaarten Lankhorst }
508a6117097SMaarten Lankhorst 
5090c159ffeSChris Wilson static void shmem_release(struct drm_i915_gem_object *obj)
5100c159ffeSChris Wilson {
5110ff37575SThomas Hellström 	if (i915_gem_object_has_struct_page(obj))
512da1184cdSMatthew Auld 		i915_gem_object_release_memory_region(obj);
513da1184cdSMatthew Auld 
5140c159ffeSChris Wilson 	fput(obj->base.filp);
5150c159ffeSChris Wilson }
5160c159ffeSChris Wilson 
5178475355fSChris Wilson const struct drm_i915_gem_object_ops i915_gem_shmem_ops = {
5187d192daaSChris Wilson 	.name = "i915_gem_object_shmem",
519c471748dSMaarten Lankhorst 	.flags = I915_GEM_OBJECT_IS_SHRINKABLE,
5208475355fSChris Wilson 
5218475355fSChris Wilson 	.get_pages = shmem_get_pages,
5228475355fSChris Wilson 	.put_pages = shmem_put_pages,
523f033428dSChris Wilson 	.truncate = shmem_truncate,
524ffa3fe08SMatthew Auld 	.shrink = shmem_shrink,
5258475355fSChris Wilson 
5268475355fSChris Wilson 	.pwrite = shmem_pwrite,
527a6117097SMaarten Lankhorst 	.pread = shmem_pread,
5280c159ffeSChris Wilson 
5290c159ffeSChris Wilson 	.release = shmem_release,
5308475355fSChris Wilson };
5318475355fSChris Wilson 
532da1184cdSMatthew Auld static int __create_shmem(struct drm_i915_private *i915,
5338475355fSChris Wilson 			  struct drm_gem_object *obj,
534da1184cdSMatthew Auld 			  resource_size_t size)
5358475355fSChris Wilson {
5368475355fSChris Wilson 	unsigned long flags = VM_NORESERVE;
5378475355fSChris Wilson 	struct file *filp;
5388475355fSChris Wilson 
5398475355fSChris Wilson 	drm_gem_private_object_init(&i915->drm, obj, size);
5408475355fSChris Wilson 
5418475355fSChris Wilson 	if (i915->mm.gemfs)
5428475355fSChris Wilson 		filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
5438475355fSChris Wilson 						 flags);
5448475355fSChris Wilson 	else
5458475355fSChris Wilson 		filp = shmem_file_setup("i915", size, flags);
5468475355fSChris Wilson 	if (IS_ERR(filp))
5478475355fSChris Wilson 		return PTR_ERR(filp);
5488475355fSChris Wilson 
5498475355fSChris Wilson 	obj->filp = filp;
5508475355fSChris Wilson 	return 0;
5518475355fSChris Wilson }
5528475355fSChris Wilson 
55397d55396SMatthew Auld static int shmem_object_init(struct intel_memory_region *mem,
55497d55396SMatthew Auld 			     struct drm_i915_gem_object *obj,
5559b78b5daSMatthew Auld 			     resource_size_t offset,
556da1184cdSMatthew Auld 			     resource_size_t size,
557d22632c8SMatthew Auld 			     resource_size_t page_size,
558da1184cdSMatthew Auld 			     unsigned int flags)
5598475355fSChris Wilson {
5607867d709SChris Wilson 	static struct lock_class_key lock_class;
561da1184cdSMatthew Auld 	struct drm_i915_private *i915 = mem->i915;
5628475355fSChris Wilson 	struct address_space *mapping;
5638475355fSChris Wilson 	unsigned int cache_level;
5648475355fSChris Wilson 	gfp_t mask;
5658475355fSChris Wilson 	int ret;
5668475355fSChris Wilson 
567da1184cdSMatthew Auld 	ret = __create_shmem(i915, &obj->base, size);
5688475355fSChris Wilson 	if (ret)
56997d55396SMatthew Auld 		return ret;
5708475355fSChris Wilson 
5718475355fSChris Wilson 	mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
5728475355fSChris Wilson 	if (IS_I965GM(i915) || IS_I965G(i915)) {
5738475355fSChris Wilson 		/* 965gm cannot relocate objects above 4GiB. */
5748475355fSChris Wilson 		mask &= ~__GFP_HIGHMEM;
5758475355fSChris Wilson 		mask |= __GFP_DMA32;
5768475355fSChris Wilson 	}
5778475355fSChris Wilson 
5788475355fSChris Wilson 	mapping = obj->base.filp->f_mapping;
5798475355fSChris Wilson 	mapping_set_gfp_mask(mapping, mask);
5808475355fSChris Wilson 	GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
5818475355fSChris Wilson 
5820ff37575SThomas Hellström 	i915_gem_object_init(obj, &i915_gem_shmem_ops, &lock_class, 0);
5830ff37575SThomas Hellström 	obj->mem_flags |= I915_BO_FLAG_STRUCT_PAGE;
5848475355fSChris Wilson 	obj->write_domain = I915_GEM_DOMAIN_CPU;
5858475355fSChris Wilson 	obj->read_domains = I915_GEM_DOMAIN_CPU;
5868475355fSChris Wilson 
5878475355fSChris Wilson 	if (HAS_LLC(i915))
5888475355fSChris Wilson 		/* On some devices, we can have the GPU use the LLC (the CPU
5898475355fSChris Wilson 		 * cache) for about a 10% performance improvement
5908475355fSChris Wilson 		 * compared to uncached.  Graphics requests other than
5918475355fSChris Wilson 		 * display scanout are coherent with the CPU in
5928475355fSChris Wilson 		 * accessing this cache.  This means in this mode we
5938475355fSChris Wilson 		 * don't need to clflush on the CPU side, and on the
5948475355fSChris Wilson 		 * GPU side we only need to flush internal caches to
5958475355fSChris Wilson 		 * get data visible to the CPU.
5968475355fSChris Wilson 		 *
5978475355fSChris Wilson 		 * However, we maintain the display planes as UC, and so
5988475355fSChris Wilson 		 * need to rebind when first used as such.
5998475355fSChris Wilson 		 */
6008475355fSChris Wilson 		cache_level = I915_CACHE_LLC;
6018475355fSChris Wilson 	else
6028475355fSChris Wilson 		cache_level = I915_CACHE_NONE;
6038475355fSChris Wilson 
6048475355fSChris Wilson 	i915_gem_object_set_cache_coherency(obj, cache_level);
6058475355fSChris Wilson 
606c471748dSMaarten Lankhorst 	i915_gem_object_init_memory_region(obj, mem);
6078475355fSChris Wilson 
60897d55396SMatthew Auld 	return 0;
6098475355fSChris Wilson }
6108475355fSChris Wilson 
611da1184cdSMatthew Auld struct drm_i915_gem_object *
612da1184cdSMatthew Auld i915_gem_object_create_shmem(struct drm_i915_private *i915,
613da1184cdSMatthew Auld 			     resource_size_t size)
614da1184cdSMatthew Auld {
615da1184cdSMatthew Auld 	return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_SMEM],
616d22632c8SMatthew Auld 					     size, 0, 0);
617da1184cdSMatthew Auld }
618da1184cdSMatthew Auld 
6198475355fSChris Wilson /* Allocate a new GEM object and fill it with the supplied data */
6208475355fSChris Wilson struct drm_i915_gem_object *
6218475355fSChris Wilson i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
622da1184cdSMatthew Auld 				       const void *data, resource_size_t size)
6238475355fSChris Wilson {
6248475355fSChris Wilson 	struct drm_i915_gem_object *obj;
6258475355fSChris Wilson 	struct file *file;
626c5edd542SMatthew Wilcox (Oracle) 	const struct address_space_operations *aops;
627da1184cdSMatthew Auld 	resource_size_t offset;
6288475355fSChris Wilson 	int err;
6298475355fSChris Wilson 
63032b7cf51SThomas Hellström 	GEM_WARN_ON(IS_DGFX(dev_priv));
6318475355fSChris Wilson 	obj = i915_gem_object_create_shmem(dev_priv, round_up(size, PAGE_SIZE));
6328475355fSChris Wilson 	if (IS_ERR(obj))
6338475355fSChris Wilson 		return obj;
6348475355fSChris Wilson 
6358475355fSChris Wilson 	GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
6368475355fSChris Wilson 
6378475355fSChris Wilson 	file = obj->base.filp;
638c5edd542SMatthew Wilcox (Oracle) 	aops = file->f_mapping->a_ops;
6398475355fSChris Wilson 	offset = 0;
6408475355fSChris Wilson 	do {
6418475355fSChris Wilson 		unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
6428475355fSChris Wilson 		struct page *page;
6438475355fSChris Wilson 		void *pgdata, *vaddr;
6448475355fSChris Wilson 
645c5edd542SMatthew Wilcox (Oracle) 		err = aops->write_begin(file, file->f_mapping, offset, len,
6468475355fSChris Wilson 					&page, &pgdata);
6478475355fSChris Wilson 		if (err < 0)
6488475355fSChris Wilson 			goto fail;
6498475355fSChris Wilson 
6508475355fSChris Wilson 		vaddr = kmap(page);
6518475355fSChris Wilson 		memcpy(vaddr, data, len);
6528475355fSChris Wilson 		kunmap(page);
6538475355fSChris Wilson 
654c5edd542SMatthew Wilcox (Oracle) 		err = aops->write_end(file, file->f_mapping, offset, len, len,
6558475355fSChris Wilson 				      page, pgdata);
6568475355fSChris Wilson 		if (err < 0)
6578475355fSChris Wilson 			goto fail;
6588475355fSChris Wilson 
6598475355fSChris Wilson 		size -= len;
6608475355fSChris Wilson 		data += len;
6618475355fSChris Wilson 		offset += len;
6628475355fSChris Wilson 	} while (size);
6638475355fSChris Wilson 
6648475355fSChris Wilson 	return obj;
6658475355fSChris Wilson 
6668475355fSChris Wilson fail:
6678475355fSChris Wilson 	i915_gem_object_put(obj);
6688475355fSChris Wilson 	return ERR_PTR(err);
6698475355fSChris Wilson }
670da1184cdSMatthew Auld 
671da1184cdSMatthew Auld static int init_shmem(struct intel_memory_region *mem)
672da1184cdSMatthew Auld {
673b499914eSTvrtko Ursulin 	i915_gemfs_init(mem->i915);
67438f1cb68SLukasz Fiedorowicz 	intel_memory_region_set_name(mem, "system");
67538f1cb68SLukasz Fiedorowicz 
676b499914eSTvrtko Ursulin 	return 0; /* We have fallback to the kernel mnt if gemfs init failed. */
677da1184cdSMatthew Auld }
678da1184cdSMatthew Auld 
6798b1f7f92SThomas Hellström static int release_shmem(struct intel_memory_region *mem)
680da1184cdSMatthew Auld {
681da1184cdSMatthew Auld 	i915_gemfs_fini(mem->i915);
6828b1f7f92SThomas Hellström 	return 0;
683da1184cdSMatthew Auld }
684da1184cdSMatthew Auld 
685da1184cdSMatthew Auld static const struct intel_memory_region_ops shmem_region_ops = {
686da1184cdSMatthew Auld 	.init = init_shmem,
687da1184cdSMatthew Auld 	.release = release_shmem,
68897d55396SMatthew Auld 	.init_object = shmem_object_init,
689da1184cdSMatthew Auld };
690da1184cdSMatthew Auld 
691d1487389SThomas Hellström struct intel_memory_region *i915_gem_shmem_setup(struct drm_i915_private *i915,
692d1487389SThomas Hellström 						 u16 type, u16 instance)
693da1184cdSMatthew Auld {
694da1184cdSMatthew Auld 	return intel_memory_region_create(i915, 0,
695da1184cdSMatthew Auld 					  totalram_pages() << PAGE_SHIFT,
696235582caSMatthew Auld 					  PAGE_SIZE, 0, 0,
697d1487389SThomas Hellström 					  type, instance,
698da1184cdSMatthew Auld 					  &shmem_region_ops);
699da1184cdSMatthew Auld }
70041a9c75dSChris Wilson 
70141a9c75dSChris Wilson bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj)
70241a9c75dSChris Wilson {
70341a9c75dSChris Wilson 	return obj->ops == &i915_gem_shmem_ops;
70441a9c75dSChris Wilson }
705