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>
88475355fSChris Wilson #include <linux/swap.h>
98475355fSChris Wilson 
10da1184cdSMatthew Auld #include "gem/i915_gem_region.h"
118475355fSChris Wilson #include "i915_drv.h"
12da1184cdSMatthew Auld #include "i915_gemfs.h"
138475355fSChris Wilson #include "i915_gem_object.h"
1437d63f8fSChris Wilson #include "i915_scatterlist.h"
15a09d9a80SJani Nikula #include "i915_trace.h"
168475355fSChris Wilson 
178475355fSChris Wilson /*
188475355fSChris Wilson  * Move pages to appropriate lru and release the pagevec, decrementing the
198475355fSChris Wilson  * ref count of those pages.
208475355fSChris Wilson  */
218475355fSChris Wilson static void check_release_pagevec(struct pagevec *pvec)
228475355fSChris Wilson {
238475355fSChris Wilson 	check_move_unevictable_pages(pvec);
248475355fSChris Wilson 	__pagevec_release(pvec);
258475355fSChris Wilson 	cond_resched();
268475355fSChris Wilson }
278475355fSChris Wilson 
28*f05b985eSThomas Hellström static void shmem_free_st(struct sg_table *st, struct address_space *mapping,
29*f05b985eSThomas Hellström 			  bool dirty, bool backup)
308475355fSChris Wilson {
31*f05b985eSThomas Hellström 	struct sgt_iter sgt_iter;
32*f05b985eSThomas Hellström 	struct pagevec pvec;
33*f05b985eSThomas Hellström 	struct page *page;
34*f05b985eSThomas Hellström 
35*f05b985eSThomas Hellström 	mapping_clear_unevictable(mapping);
36*f05b985eSThomas Hellström 
37*f05b985eSThomas Hellström 	pagevec_init(&pvec);
38*f05b985eSThomas Hellström 	for_each_sgt_page(page, sgt_iter, st) {
39*f05b985eSThomas Hellström 		if (dirty)
40*f05b985eSThomas Hellström 			set_page_dirty(page);
41*f05b985eSThomas Hellström 
42*f05b985eSThomas Hellström 		if (backup)
43*f05b985eSThomas Hellström 			mark_page_accessed(page);
44*f05b985eSThomas Hellström 
45*f05b985eSThomas Hellström 		if (!pagevec_add(&pvec, page))
46*f05b985eSThomas Hellström 			check_release_pagevec(&pvec);
47*f05b985eSThomas Hellström 	}
48*f05b985eSThomas Hellström 	if (pagevec_count(&pvec))
49*f05b985eSThomas Hellström 		check_release_pagevec(&pvec);
50*f05b985eSThomas Hellström 
51*f05b985eSThomas Hellström 	sg_free_table(st);
52*f05b985eSThomas Hellström 	kfree(st);
53*f05b985eSThomas Hellström }
54*f05b985eSThomas Hellström 
55*f05b985eSThomas Hellström static struct sg_table *shmem_alloc_st(struct drm_i915_private *i915,
56*f05b985eSThomas Hellström 				       size_t size, struct intel_memory_region *mr,
57*f05b985eSThomas Hellström 				       struct address_space *mapping,
58*f05b985eSThomas Hellström 				       unsigned int max_segment)
59*f05b985eSThomas Hellström {
60*f05b985eSThomas Hellström 	const unsigned long page_count = size / PAGE_SIZE;
618475355fSChris Wilson 	unsigned long i;
628475355fSChris Wilson 	struct sg_table *st;
638475355fSChris Wilson 	struct scatterlist *sg;
648475355fSChris Wilson 	struct page *page;
658475355fSChris Wilson 	unsigned long last_pfn = 0;	/* suppress gcc warning */
668475355fSChris Wilson 	gfp_t noreclaim;
678475355fSChris Wilson 	int ret;
688475355fSChris Wilson 
698475355fSChris Wilson 	/*
708475355fSChris Wilson 	 * If there's no chance of allocating enough pages for the whole
718475355fSChris Wilson 	 * object, bail early.
728475355fSChris Wilson 	 */
73*f05b985eSThomas Hellström 	if (size > resource_size(&mr->region))
74*f05b985eSThomas Hellström 		return ERR_PTR(-ENOMEM);
758475355fSChris Wilson 
768475355fSChris Wilson 	st = kmalloc(sizeof(*st), GFP_KERNEL);
778475355fSChris Wilson 	if (!st)
78*f05b985eSThomas Hellström 		return ERR_PTR(-ENOMEM);
798475355fSChris Wilson 
808475355fSChris Wilson 	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
818475355fSChris Wilson 		kfree(st);
82*f05b985eSThomas Hellström 		return ERR_PTR(-ENOMEM);
838475355fSChris Wilson 	}
848475355fSChris Wilson 
858475355fSChris Wilson 	/*
868475355fSChris Wilson 	 * Get the list of pages out of our struct file.  They'll be pinned
878475355fSChris Wilson 	 * at this point until we release them.
888475355fSChris Wilson 	 *
898475355fSChris Wilson 	 * Fail silently without starting the shrinker
908475355fSChris Wilson 	 */
918475355fSChris Wilson 	mapping_set_unevictable(mapping);
928475355fSChris Wilson 	noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
938475355fSChris Wilson 	noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
948475355fSChris Wilson 
958475355fSChris Wilson 	sg = st->sgl;
968475355fSChris Wilson 	st->nents = 0;
978475355fSChris Wilson 	for (i = 0; i < page_count; i++) {
988475355fSChris Wilson 		const unsigned int shrink[] = {
993b4fa964SChris Wilson 			I915_SHRINK_BOUND | I915_SHRINK_UNBOUND,
1008475355fSChris Wilson 			0,
1018475355fSChris Wilson 		}, *s = shrink;
1028475355fSChris Wilson 		gfp_t gfp = noreclaim;
1038475355fSChris Wilson 
1048475355fSChris Wilson 		do {
1058475355fSChris Wilson 			cond_resched();
1068475355fSChris Wilson 			page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1078475355fSChris Wilson 			if (!IS_ERR(page))
1088475355fSChris Wilson 				break;
1098475355fSChris Wilson 
1108475355fSChris Wilson 			if (!*s) {
1118475355fSChris Wilson 				ret = PTR_ERR(page);
1128475355fSChris Wilson 				goto err_sg;
1138475355fSChris Wilson 			}
1148475355fSChris Wilson 
115cf41a8f1SMaarten Lankhorst 			i915_gem_shrink(NULL, i915, 2 * page_count, NULL, *s++);
1168475355fSChris Wilson 
1178475355fSChris Wilson 			/*
1188475355fSChris Wilson 			 * We've tried hard to allocate the memory by reaping
1198475355fSChris Wilson 			 * our own buffer, now let the real VM do its job and
1208475355fSChris Wilson 			 * go down in flames if truly OOM.
1218475355fSChris Wilson 			 *
1228475355fSChris Wilson 			 * However, since graphics tend to be disposable,
1238475355fSChris Wilson 			 * defer the oom here by reporting the ENOMEM back
1248475355fSChris Wilson 			 * to userspace.
1258475355fSChris Wilson 			 */
1268475355fSChris Wilson 			if (!*s) {
1278475355fSChris Wilson 				/* reclaim and warn, but no oom */
1288475355fSChris Wilson 				gfp = mapping_gfp_mask(mapping);
1298475355fSChris Wilson 
1308475355fSChris Wilson 				/*
1318475355fSChris Wilson 				 * Our bo are always dirty and so we require
1328475355fSChris Wilson 				 * kswapd to reclaim our pages (direct reclaim
1338475355fSChris Wilson 				 * does not effectively begin pageout of our
1348475355fSChris Wilson 				 * buffers on its own). However, direct reclaim
1358475355fSChris Wilson 				 * only waits for kswapd when under allocation
1368475355fSChris Wilson 				 * congestion. So as a result __GFP_RECLAIM is
1378475355fSChris Wilson 				 * unreliable and fails to actually reclaim our
1388475355fSChris Wilson 				 * dirty pages -- unless you try over and over
1398475355fSChris Wilson 				 * again with !__GFP_NORETRY. However, we still
1408475355fSChris Wilson 				 * want to fail this allocation rather than
1418475355fSChris Wilson 				 * trigger the out-of-memory killer and for
1428475355fSChris Wilson 				 * this we want __GFP_RETRY_MAYFAIL.
1438475355fSChris Wilson 				 */
1448475355fSChris Wilson 				gfp |= __GFP_RETRY_MAYFAIL;
1458475355fSChris Wilson 			}
1468475355fSChris Wilson 		} while (1);
1478475355fSChris Wilson 
1488475355fSChris Wilson 		if (!i ||
1498475355fSChris Wilson 		    sg->length >= max_segment ||
1508475355fSChris Wilson 		    page_to_pfn(page) != last_pfn + 1) {
151*f05b985eSThomas Hellström 			if (i)
1528475355fSChris Wilson 				sg = sg_next(sg);
153*f05b985eSThomas Hellström 
1548475355fSChris Wilson 			st->nents++;
1558475355fSChris Wilson 			sg_set_page(sg, page, PAGE_SIZE, 0);
1568475355fSChris Wilson 		} else {
1578475355fSChris Wilson 			sg->length += PAGE_SIZE;
1588475355fSChris Wilson 		}
1598475355fSChris Wilson 		last_pfn = page_to_pfn(page);
1608475355fSChris Wilson 
1618475355fSChris Wilson 		/* Check that the i965g/gm workaround works. */
162ea97c4caSChris Wilson 		GEM_BUG_ON(gfp & __GFP_DMA32 && last_pfn >= 0x00100000UL);
1638475355fSChris Wilson 	}
164*f05b985eSThomas Hellström 	if (sg) /* loop terminated early; short sg table */
1658475355fSChris Wilson 		sg_mark_end(sg);
1668475355fSChris Wilson 
1678475355fSChris Wilson 	/* Trim unused sg entries to avoid wasting memory. */
1688475355fSChris Wilson 	i915_sg_trim(st);
1698475355fSChris Wilson 
170*f05b985eSThomas Hellström 	return st;
171*f05b985eSThomas Hellström err_sg:
172*f05b985eSThomas Hellström 	sg_mark_end(sg);
173*f05b985eSThomas Hellström 	if (sg != st->sgl) {
174*f05b985eSThomas Hellström 		shmem_free_st(st, mapping, false, false);
175*f05b985eSThomas Hellström 	} else {
176*f05b985eSThomas Hellström 		mapping_clear_unevictable(mapping);
177*f05b985eSThomas Hellström 		sg_free_table(st);
178*f05b985eSThomas Hellström 		kfree(st);
179*f05b985eSThomas Hellström 	}
180*f05b985eSThomas Hellström 
181*f05b985eSThomas Hellström 	/*
182*f05b985eSThomas Hellström 	 * shmemfs first checks if there is enough memory to allocate the page
183*f05b985eSThomas Hellström 	 * and reports ENOSPC should there be insufficient, along with the usual
184*f05b985eSThomas Hellström 	 * ENOMEM for a genuine allocation failure.
185*f05b985eSThomas Hellström 	 *
186*f05b985eSThomas Hellström 	 * We use ENOSPC in our driver to mean that we have run out of aperture
187*f05b985eSThomas Hellström 	 * space and so want to translate the error from shmemfs back to our
188*f05b985eSThomas Hellström 	 * usual understanding of ENOMEM.
189*f05b985eSThomas Hellström 	 */
190*f05b985eSThomas Hellström 	if (ret == -ENOSPC)
191*f05b985eSThomas Hellström 		ret = -ENOMEM;
192*f05b985eSThomas Hellström 
193*f05b985eSThomas Hellström 	return ERR_PTR(ret);
194*f05b985eSThomas Hellström }
195*f05b985eSThomas Hellström 
196*f05b985eSThomas Hellström static int shmem_get_pages(struct drm_i915_gem_object *obj)
197*f05b985eSThomas Hellström {
198*f05b985eSThomas Hellström 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
199*f05b985eSThomas Hellström 	struct intel_memory_region *mem = obj->mm.region;
200*f05b985eSThomas Hellström 	struct address_space *mapping = obj->base.filp->f_mapping;
201*f05b985eSThomas Hellström 	const unsigned long page_count = obj->base.size / PAGE_SIZE;
202*f05b985eSThomas Hellström 	unsigned int max_segment = i915_sg_segment_size();
203*f05b985eSThomas Hellström 	struct sg_table *st;
204*f05b985eSThomas Hellström 	struct sgt_iter sgt_iter;
205*f05b985eSThomas Hellström 	struct page *page;
206*f05b985eSThomas Hellström 	int ret;
207*f05b985eSThomas Hellström 
208*f05b985eSThomas Hellström 	/*
209*f05b985eSThomas Hellström 	 * Assert that the object is not currently in any GPU domain. As it
210*f05b985eSThomas Hellström 	 * wasn't in the GTT, there shouldn't be any way it could have been in
211*f05b985eSThomas Hellström 	 * a GPU cache
212*f05b985eSThomas Hellström 	 */
213*f05b985eSThomas Hellström 	GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
214*f05b985eSThomas Hellström 	GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
215*f05b985eSThomas Hellström 
216*f05b985eSThomas Hellström rebuild_st:
217*f05b985eSThomas Hellström 	st = shmem_alloc_st(i915, obj->base.size, mem, mapping, max_segment);
218*f05b985eSThomas Hellström 	if (IS_ERR(st)) {
219*f05b985eSThomas Hellström 		ret = PTR_ERR(st);
220*f05b985eSThomas Hellström 		goto err_st;
221*f05b985eSThomas Hellström 	}
222*f05b985eSThomas Hellström 
2238475355fSChris Wilson 	ret = i915_gem_gtt_prepare_pages(obj, st);
2248475355fSChris Wilson 	if (ret) {
2258475355fSChris Wilson 		/*
2268475355fSChris Wilson 		 * DMA remapping failed? One possible cause is that
2278475355fSChris Wilson 		 * it could not reserve enough large entries, asking
2288475355fSChris Wilson 		 * for PAGE_SIZE chunks instead may be helpful.
2298475355fSChris Wilson 		 */
2308475355fSChris Wilson 		if (max_segment > PAGE_SIZE) {
2318475355fSChris Wilson 			for_each_sgt_page(page, sgt_iter, st)
2328475355fSChris Wilson 				put_page(page);
2338475355fSChris Wilson 			sg_free_table(st);
234*f05b985eSThomas Hellström 			kfree(st);
2358475355fSChris Wilson 
2368475355fSChris Wilson 			max_segment = PAGE_SIZE;
2378475355fSChris Wilson 			goto rebuild_st;
2388475355fSChris Wilson 		} else {
2398ff5446aSThomas Zimmermann 			dev_warn(i915->drm.dev,
2408475355fSChris Wilson 				 "Failed to DMA remap %lu pages\n",
2418475355fSChris Wilson 				 page_count);
2428475355fSChris Wilson 			goto err_pages;
2438475355fSChris Wilson 		}
2448475355fSChris Wilson 	}
2458475355fSChris Wilson 
2468475355fSChris Wilson 	if (i915_gem_object_needs_bit17_swizzle(obj))
2478475355fSChris Wilson 		i915_gem_object_do_bit_17_swizzle(obj, st);
2488475355fSChris Wilson 
24930f1dccdSMatthew Auld 	if (i915_gem_object_can_bypass_llc(obj))
25013d29c82SMatthew Auld 		obj->cache_dirty = true;
25113d29c82SMatthew Auld 
252*f05b985eSThomas Hellström 	__i915_gem_object_set_pages(obj, st, i915_sg_dma_sizes(st->sgl));
2538475355fSChris Wilson 
2548475355fSChris Wilson 	return 0;
2558475355fSChris Wilson 
2568475355fSChris Wilson err_pages:
257*f05b985eSThomas Hellström 	shmem_free_st(st, mapping, false, false);
2588475355fSChris Wilson 	/*
2598475355fSChris Wilson 	 * shmemfs first checks if there is enough memory to allocate the page
2608475355fSChris Wilson 	 * and reports ENOSPC should there be insufficient, along with the usual
2618475355fSChris Wilson 	 * ENOMEM for a genuine allocation failure.
2628475355fSChris Wilson 	 *
2638475355fSChris Wilson 	 * We use ENOSPC in our driver to mean that we have run out of aperture
2648475355fSChris Wilson 	 * space and so want to translate the error from shmemfs back to our
2658475355fSChris Wilson 	 * usual understanding of ENOMEM.
2668475355fSChris Wilson 	 */
267*f05b985eSThomas Hellström err_st:
2688475355fSChris Wilson 	if (ret == -ENOSPC)
2698475355fSChris Wilson 		ret = -ENOMEM;
2708475355fSChris Wilson 
2718475355fSChris Wilson 	return ret;
2728475355fSChris Wilson }
2738475355fSChris Wilson 
274f033428dSChris Wilson static void
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);
286f033428dSChris Wilson }
287f033428dSChris Wilson 
288*f05b985eSThomas Hellström static void __shmem_writeback(size_t size, struct address_space *mapping)
289f033428dSChris Wilson {
290f033428dSChris Wilson 	struct writeback_control wbc = {
291f033428dSChris Wilson 		.sync_mode = WB_SYNC_NONE,
292f033428dSChris Wilson 		.nr_to_write = SWAP_CLUSTER_MAX,
293f033428dSChris Wilson 		.range_start = 0,
294f033428dSChris Wilson 		.range_end = LLONG_MAX,
295f033428dSChris Wilson 		.for_reclaim = 1,
296f033428dSChris Wilson 	};
297f033428dSChris Wilson 	unsigned long i;
298f033428dSChris Wilson 
299f033428dSChris Wilson 	/*
300f033428dSChris Wilson 	 * Leave mmapings intact (GTT will have been revoked on unbinding,
301f033428dSChris Wilson 	 * leaving only CPU mmapings around) and add those pages to the LRU
302f033428dSChris Wilson 	 * instead of invoking writeback so they are aged and paged out
303f033428dSChris Wilson 	 * as normal.
304f033428dSChris Wilson 	 */
305f033428dSChris Wilson 
306f033428dSChris Wilson 	/* Begin writeback on each dirty page */
307*f05b985eSThomas Hellström 	for (i = 0; i < size >> PAGE_SHIFT; i++) {
308f033428dSChris Wilson 		struct page *page;
309f033428dSChris Wilson 
3109dfc8ff3SMatthew Wilcox (Oracle) 		page = find_lock_page(mapping, i);
3119dfc8ff3SMatthew Wilcox (Oracle) 		if (!page)
312f033428dSChris Wilson 			continue;
313f033428dSChris Wilson 
314f033428dSChris Wilson 		if (!page_mapped(page) && clear_page_dirty_for_io(page)) {
315f033428dSChris Wilson 			int ret;
316f033428dSChris Wilson 
317f033428dSChris Wilson 			SetPageReclaim(page);
318f033428dSChris Wilson 			ret = mapping->a_ops->writepage(page, &wbc);
319f033428dSChris Wilson 			if (!PageWriteback(page))
320f033428dSChris Wilson 				ClearPageReclaim(page);
321f033428dSChris Wilson 			if (!ret)
322f033428dSChris Wilson 				goto put;
323f033428dSChris Wilson 		}
324f033428dSChris Wilson 		unlock_page(page);
325f033428dSChris Wilson put:
326f033428dSChris Wilson 		put_page(page);
327f033428dSChris Wilson 	}
328f033428dSChris Wilson }
329f033428dSChris Wilson 
330*f05b985eSThomas Hellström static void
331*f05b985eSThomas Hellström shmem_writeback(struct drm_i915_gem_object *obj)
332*f05b985eSThomas Hellström {
333*f05b985eSThomas Hellström 	__shmem_writeback(obj->base.size, obj->base.filp->f_mapping);
334*f05b985eSThomas Hellström }
335*f05b985eSThomas Hellström 
3368475355fSChris Wilson void
3378475355fSChris Wilson __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
3388475355fSChris Wilson 				struct sg_table *pages,
3398475355fSChris Wilson 				bool needs_clflush)
3408475355fSChris Wilson {
341d70af579SMatthew Auld 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
342d70af579SMatthew Auld 
3438475355fSChris Wilson 	GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
3448475355fSChris Wilson 
3458475355fSChris Wilson 	if (obj->mm.madv == I915_MADV_DONTNEED)
3468475355fSChris Wilson 		obj->mm.dirty = false;
3478475355fSChris Wilson 
3488475355fSChris Wilson 	if (needs_clflush &&
3498475355fSChris Wilson 	    (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
3508475355fSChris Wilson 	    !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
3518475355fSChris Wilson 		drm_clflush_sg(pages);
3528475355fSChris Wilson 
3538475355fSChris Wilson 	__start_cpu_write(obj);
354d70af579SMatthew Auld 	/*
355d70af579SMatthew Auld 	 * On non-LLC platforms, force the flush-on-acquire if this is ever
356d70af579SMatthew Auld 	 * swapped-in. Our async flush path is not trust worthy enough yet(and
357d70af579SMatthew Auld 	 * happens in the wrong order), and with some tricks it's conceivable
358d70af579SMatthew Auld 	 * for userspace to change the cache-level to I915_CACHE_NONE after the
359d70af579SMatthew Auld 	 * pages are swapped-in, and since execbuf binds the object before doing
360d70af579SMatthew Auld 	 * the async flush, we have a race window.
361d70af579SMatthew Auld 	 */
362d70af579SMatthew Auld 	if (!HAS_LLC(i915))
363d70af579SMatthew Auld 		obj->cache_dirty = true;
3648475355fSChris Wilson }
3658475355fSChris Wilson 
366a85fffe3SMaarten Lankhorst void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj, struct sg_table *pages)
3678475355fSChris Wilson {
3688475355fSChris Wilson 	__i915_gem_object_release_shmem(obj, pages, true);
3698475355fSChris Wilson 
3708475355fSChris Wilson 	i915_gem_gtt_finish_pages(obj, pages);
3718475355fSChris Wilson 
3728475355fSChris Wilson 	if (i915_gem_object_needs_bit17_swizzle(obj))
3738475355fSChris Wilson 		i915_gem_object_save_bit_17_swizzle(obj, pages);
3748475355fSChris Wilson 
375*f05b985eSThomas Hellström 	shmem_free_st(pages, file_inode(obj->base.filp)->i_mapping,
376*f05b985eSThomas Hellström 		      obj->mm.dirty, obj->mm.madv == I915_MADV_WILLNEED);
3778475355fSChris Wilson 	obj->mm.dirty = false;
3788475355fSChris Wilson }
3798475355fSChris Wilson 
380a85fffe3SMaarten Lankhorst static void
381a85fffe3SMaarten Lankhorst shmem_put_pages(struct drm_i915_gem_object *obj, struct sg_table *pages)
382a85fffe3SMaarten Lankhorst {
383a85fffe3SMaarten Lankhorst 	if (likely(i915_gem_object_has_struct_page(obj)))
384a85fffe3SMaarten Lankhorst 		i915_gem_object_put_pages_shmem(obj, pages);
385a85fffe3SMaarten Lankhorst 	else
386a85fffe3SMaarten Lankhorst 		i915_gem_object_put_pages_phys(obj, pages);
387a85fffe3SMaarten Lankhorst }
388a85fffe3SMaarten Lankhorst 
3898475355fSChris Wilson static int
3908475355fSChris Wilson shmem_pwrite(struct drm_i915_gem_object *obj,
3918475355fSChris Wilson 	     const struct drm_i915_gem_pwrite *arg)
3928475355fSChris Wilson {
3938475355fSChris Wilson 	struct address_space *mapping = obj->base.filp->f_mapping;
3948475355fSChris Wilson 	char __user *user_data = u64_to_user_ptr(arg->data_ptr);
3958475355fSChris Wilson 	u64 remain, offset;
3968475355fSChris Wilson 	unsigned int pg;
3978475355fSChris Wilson 
3988475355fSChris Wilson 	/* Caller already validated user args */
3998475355fSChris Wilson 	GEM_BUG_ON(!access_ok(user_data, arg->size));
4008475355fSChris Wilson 
401a6117097SMaarten Lankhorst 	if (!i915_gem_object_has_struct_page(obj))
402a6117097SMaarten Lankhorst 		return i915_gem_object_pwrite_phys(obj, arg);
403a6117097SMaarten Lankhorst 
4048475355fSChris Wilson 	/*
4058475355fSChris Wilson 	 * Before we instantiate/pin the backing store for our use, we
4068475355fSChris Wilson 	 * can prepopulate the shmemfs filp efficiently using a write into
4078475355fSChris Wilson 	 * the pagecache. We avoid the penalty of instantiating all the
4088475355fSChris Wilson 	 * pages, important if the user is just writing to a few and never
4098475355fSChris Wilson 	 * uses the object on the GPU, and using a direct write into shmemfs
4108475355fSChris Wilson 	 * allows it to avoid the cost of retrieving a page (either swapin
4118475355fSChris Wilson 	 * or clearing-before-use) before it is overwritten.
4128475355fSChris Wilson 	 */
4138475355fSChris Wilson 	if (i915_gem_object_has_pages(obj))
4148475355fSChris Wilson 		return -ENODEV;
4158475355fSChris Wilson 
4168475355fSChris Wilson 	if (obj->mm.madv != I915_MADV_WILLNEED)
4178475355fSChris Wilson 		return -EFAULT;
4188475355fSChris Wilson 
4198475355fSChris Wilson 	/*
4208475355fSChris Wilson 	 * Before the pages are instantiated the object is treated as being
4218475355fSChris Wilson 	 * in the CPU domain. The pages will be clflushed as required before
4228475355fSChris Wilson 	 * use, and we can freely write into the pages directly. If userspace
4238475355fSChris Wilson 	 * races pwrite with any other operation; corruption will ensue -
4248475355fSChris Wilson 	 * that is userspace's prerogative!
4258475355fSChris Wilson 	 */
4268475355fSChris Wilson 
4278475355fSChris Wilson 	remain = arg->size;
4288475355fSChris Wilson 	offset = arg->offset;
4298475355fSChris Wilson 	pg = offset_in_page(offset);
4308475355fSChris Wilson 
4318475355fSChris Wilson 	do {
4328475355fSChris Wilson 		unsigned int len, unwritten;
4338475355fSChris Wilson 		struct page *page;
4348475355fSChris Wilson 		void *data, *vaddr;
4358475355fSChris Wilson 		int err;
4368475355fSChris Wilson 		char c;
4378475355fSChris Wilson 
4388475355fSChris Wilson 		len = PAGE_SIZE - pg;
4398475355fSChris Wilson 		if (len > remain)
4408475355fSChris Wilson 			len = remain;
4418475355fSChris Wilson 
4428475355fSChris Wilson 		/* Prefault the user page to reduce potential recursion */
4438475355fSChris Wilson 		err = __get_user(c, user_data);
4448475355fSChris Wilson 		if (err)
4458475355fSChris Wilson 			return err;
4468475355fSChris Wilson 
4478475355fSChris Wilson 		err = __get_user(c, user_data + len - 1);
4488475355fSChris Wilson 		if (err)
4498475355fSChris Wilson 			return err;
4508475355fSChris Wilson 
4518475355fSChris Wilson 		err = pagecache_write_begin(obj->base.filp, mapping,
4528475355fSChris Wilson 					    offset, len, 0,
4538475355fSChris Wilson 					    &page, &data);
4548475355fSChris Wilson 		if (err < 0)
4558475355fSChris Wilson 			return err;
4568475355fSChris Wilson 
4578475355fSChris Wilson 		vaddr = kmap_atomic(page);
4588475355fSChris Wilson 		unwritten = __copy_from_user_inatomic(vaddr + pg,
4598475355fSChris Wilson 						      user_data,
4608475355fSChris Wilson 						      len);
4618475355fSChris Wilson 		kunmap_atomic(vaddr);
4628475355fSChris Wilson 
4638475355fSChris Wilson 		err = pagecache_write_end(obj->base.filp, mapping,
4648475355fSChris Wilson 					  offset, len, len - unwritten,
4658475355fSChris Wilson 					  page, data);
4668475355fSChris Wilson 		if (err < 0)
4678475355fSChris Wilson 			return err;
4688475355fSChris Wilson 
4698475355fSChris Wilson 		/* We don't handle -EFAULT, leave it to the caller to check */
4708475355fSChris Wilson 		if (unwritten)
4718475355fSChris Wilson 			return -ENODEV;
4728475355fSChris Wilson 
4738475355fSChris Wilson 		remain -= len;
4748475355fSChris Wilson 		user_data += len;
4758475355fSChris Wilson 		offset += len;
4768475355fSChris Wilson 		pg = 0;
4778475355fSChris Wilson 	} while (remain);
4788475355fSChris Wilson 
4798475355fSChris Wilson 	return 0;
4808475355fSChris Wilson }
4818475355fSChris Wilson 
482a6117097SMaarten Lankhorst static int
483a6117097SMaarten Lankhorst shmem_pread(struct drm_i915_gem_object *obj,
484a6117097SMaarten Lankhorst 	    const struct drm_i915_gem_pread *arg)
485a6117097SMaarten Lankhorst {
486a6117097SMaarten Lankhorst 	if (!i915_gem_object_has_struct_page(obj))
487a6117097SMaarten Lankhorst 		return i915_gem_object_pread_phys(obj, arg);
488a6117097SMaarten Lankhorst 
489a6117097SMaarten Lankhorst 	return -ENODEV;
490a6117097SMaarten Lankhorst }
491a6117097SMaarten Lankhorst 
4920c159ffeSChris Wilson static void shmem_release(struct drm_i915_gem_object *obj)
4930c159ffeSChris Wilson {
4940ff37575SThomas Hellström 	if (i915_gem_object_has_struct_page(obj))
495da1184cdSMatthew Auld 		i915_gem_object_release_memory_region(obj);
496da1184cdSMatthew Auld 
4970c159ffeSChris Wilson 	fput(obj->base.filp);
4980c159ffeSChris Wilson }
4990c159ffeSChris Wilson 
5008475355fSChris Wilson const struct drm_i915_gem_object_ops i915_gem_shmem_ops = {
5017d192daaSChris Wilson 	.name = "i915_gem_object_shmem",
502c471748dSMaarten Lankhorst 	.flags = I915_GEM_OBJECT_IS_SHRINKABLE,
5038475355fSChris Wilson 
5048475355fSChris Wilson 	.get_pages = shmem_get_pages,
5058475355fSChris Wilson 	.put_pages = shmem_put_pages,
506f033428dSChris Wilson 	.truncate = shmem_truncate,
507f033428dSChris Wilson 	.writeback = shmem_writeback,
5088475355fSChris Wilson 
5098475355fSChris Wilson 	.pwrite = shmem_pwrite,
510a6117097SMaarten Lankhorst 	.pread = shmem_pread,
5110c159ffeSChris Wilson 
5120c159ffeSChris Wilson 	.release = shmem_release,
5138475355fSChris Wilson };
5148475355fSChris Wilson 
515da1184cdSMatthew Auld static int __create_shmem(struct drm_i915_private *i915,
5168475355fSChris Wilson 			  struct drm_gem_object *obj,
517da1184cdSMatthew Auld 			  resource_size_t size)
5188475355fSChris Wilson {
5198475355fSChris Wilson 	unsigned long flags = VM_NORESERVE;
5208475355fSChris Wilson 	struct file *filp;
5218475355fSChris Wilson 
5228475355fSChris Wilson 	drm_gem_private_object_init(&i915->drm, obj, size);
5238475355fSChris Wilson 
5248475355fSChris Wilson 	if (i915->mm.gemfs)
5258475355fSChris Wilson 		filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
5268475355fSChris Wilson 						 flags);
5278475355fSChris Wilson 	else
5288475355fSChris Wilson 		filp = shmem_file_setup("i915", size, flags);
5298475355fSChris Wilson 	if (IS_ERR(filp))
5308475355fSChris Wilson 		return PTR_ERR(filp);
5318475355fSChris Wilson 
5328475355fSChris Wilson 	obj->filp = filp;
5338475355fSChris Wilson 	return 0;
5348475355fSChris Wilson }
5358475355fSChris Wilson 
53697d55396SMatthew Auld static int shmem_object_init(struct intel_memory_region *mem,
53797d55396SMatthew Auld 			     struct drm_i915_gem_object *obj,
538da1184cdSMatthew Auld 			     resource_size_t size,
539d22632c8SMatthew Auld 			     resource_size_t page_size,
540da1184cdSMatthew Auld 			     unsigned int flags)
5418475355fSChris Wilson {
5427867d709SChris Wilson 	static struct lock_class_key lock_class;
543da1184cdSMatthew Auld 	struct drm_i915_private *i915 = mem->i915;
5448475355fSChris Wilson 	struct address_space *mapping;
5458475355fSChris Wilson 	unsigned int cache_level;
5468475355fSChris Wilson 	gfp_t mask;
5478475355fSChris Wilson 	int ret;
5488475355fSChris Wilson 
549da1184cdSMatthew Auld 	ret = __create_shmem(i915, &obj->base, size);
5508475355fSChris Wilson 	if (ret)
55197d55396SMatthew Auld 		return ret;
5528475355fSChris Wilson 
5538475355fSChris Wilson 	mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
5548475355fSChris Wilson 	if (IS_I965GM(i915) || IS_I965G(i915)) {
5558475355fSChris Wilson 		/* 965gm cannot relocate objects above 4GiB. */
5568475355fSChris Wilson 		mask &= ~__GFP_HIGHMEM;
5578475355fSChris Wilson 		mask |= __GFP_DMA32;
5588475355fSChris Wilson 	}
5598475355fSChris Wilson 
5608475355fSChris Wilson 	mapping = obj->base.filp->f_mapping;
5618475355fSChris Wilson 	mapping_set_gfp_mask(mapping, mask);
5628475355fSChris Wilson 	GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
5638475355fSChris Wilson 
5640ff37575SThomas Hellström 	i915_gem_object_init(obj, &i915_gem_shmem_ops, &lock_class, 0);
5650ff37575SThomas Hellström 	obj->mem_flags |= I915_BO_FLAG_STRUCT_PAGE;
5668475355fSChris Wilson 	obj->write_domain = I915_GEM_DOMAIN_CPU;
5678475355fSChris Wilson 	obj->read_domains = I915_GEM_DOMAIN_CPU;
5688475355fSChris Wilson 
5698475355fSChris Wilson 	if (HAS_LLC(i915))
5708475355fSChris Wilson 		/* On some devices, we can have the GPU use the LLC (the CPU
5718475355fSChris Wilson 		 * cache) for about a 10% performance improvement
5728475355fSChris Wilson 		 * compared to uncached.  Graphics requests other than
5738475355fSChris Wilson 		 * display scanout are coherent with the CPU in
5748475355fSChris Wilson 		 * accessing this cache.  This means in this mode we
5758475355fSChris Wilson 		 * don't need to clflush on the CPU side, and on the
5768475355fSChris Wilson 		 * GPU side we only need to flush internal caches to
5778475355fSChris Wilson 		 * get data visible to the CPU.
5788475355fSChris Wilson 		 *
5798475355fSChris Wilson 		 * However, we maintain the display planes as UC, and so
5808475355fSChris Wilson 		 * need to rebind when first used as such.
5818475355fSChris Wilson 		 */
5828475355fSChris Wilson 		cache_level = I915_CACHE_LLC;
5838475355fSChris Wilson 	else
5848475355fSChris Wilson 		cache_level = I915_CACHE_NONE;
5858475355fSChris Wilson 
5868475355fSChris Wilson 	i915_gem_object_set_cache_coherency(obj, cache_level);
5878475355fSChris Wilson 
588c471748dSMaarten Lankhorst 	i915_gem_object_init_memory_region(obj, mem);
5898475355fSChris Wilson 
59097d55396SMatthew Auld 	return 0;
5918475355fSChris Wilson }
5928475355fSChris Wilson 
593da1184cdSMatthew Auld struct drm_i915_gem_object *
594da1184cdSMatthew Auld i915_gem_object_create_shmem(struct drm_i915_private *i915,
595da1184cdSMatthew Auld 			     resource_size_t size)
596da1184cdSMatthew Auld {
597da1184cdSMatthew Auld 	return i915_gem_object_create_region(i915->mm.regions[INTEL_REGION_SMEM],
598d22632c8SMatthew Auld 					     size, 0, 0);
599da1184cdSMatthew Auld }
600da1184cdSMatthew Auld 
6018475355fSChris Wilson /* Allocate a new GEM object and fill it with the supplied data */
6028475355fSChris Wilson struct drm_i915_gem_object *
6038475355fSChris Wilson i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
604da1184cdSMatthew Auld 				       const void *data, resource_size_t size)
6058475355fSChris Wilson {
6068475355fSChris Wilson 	struct drm_i915_gem_object *obj;
6078475355fSChris Wilson 	struct file *file;
608da1184cdSMatthew Auld 	resource_size_t offset;
6098475355fSChris Wilson 	int err;
6108475355fSChris Wilson 
61132b7cf51SThomas Hellström 	GEM_WARN_ON(IS_DGFX(dev_priv));
6128475355fSChris Wilson 	obj = i915_gem_object_create_shmem(dev_priv, round_up(size, PAGE_SIZE));
6138475355fSChris Wilson 	if (IS_ERR(obj))
6148475355fSChris Wilson 		return obj;
6158475355fSChris Wilson 
6168475355fSChris Wilson 	GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
6178475355fSChris Wilson 
6188475355fSChris Wilson 	file = obj->base.filp;
6198475355fSChris Wilson 	offset = 0;
6208475355fSChris Wilson 	do {
6218475355fSChris Wilson 		unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
6228475355fSChris Wilson 		struct page *page;
6238475355fSChris Wilson 		void *pgdata, *vaddr;
6248475355fSChris Wilson 
6258475355fSChris Wilson 		err = pagecache_write_begin(file, file->f_mapping,
6268475355fSChris Wilson 					    offset, len, 0,
6278475355fSChris Wilson 					    &page, &pgdata);
6288475355fSChris Wilson 		if (err < 0)
6298475355fSChris Wilson 			goto fail;
6308475355fSChris Wilson 
6318475355fSChris Wilson 		vaddr = kmap(page);
6328475355fSChris Wilson 		memcpy(vaddr, data, len);
6338475355fSChris Wilson 		kunmap(page);
6348475355fSChris Wilson 
6358475355fSChris Wilson 		err = pagecache_write_end(file, file->f_mapping,
6368475355fSChris Wilson 					  offset, len, len,
6378475355fSChris Wilson 					  page, pgdata);
6388475355fSChris Wilson 		if (err < 0)
6398475355fSChris Wilson 			goto fail;
6408475355fSChris Wilson 
6418475355fSChris Wilson 		size -= len;
6428475355fSChris Wilson 		data += len;
6438475355fSChris Wilson 		offset += len;
6448475355fSChris Wilson 	} while (size);
6458475355fSChris Wilson 
6468475355fSChris Wilson 	return obj;
6478475355fSChris Wilson 
6488475355fSChris Wilson fail:
6498475355fSChris Wilson 	i915_gem_object_put(obj);
6508475355fSChris Wilson 	return ERR_PTR(err);
6518475355fSChris Wilson }
652da1184cdSMatthew Auld 
653da1184cdSMatthew Auld static int init_shmem(struct intel_memory_region *mem)
654da1184cdSMatthew Auld {
655da1184cdSMatthew Auld 	int err;
656da1184cdSMatthew Auld 
657da1184cdSMatthew Auld 	err = i915_gemfs_init(mem->i915);
658da1184cdSMatthew Auld 	if (err) {
659da1184cdSMatthew Auld 		DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n",
660da1184cdSMatthew Auld 			 err);
661da1184cdSMatthew Auld 	}
662da1184cdSMatthew Auld 
66338f1cb68SLukasz Fiedorowicz 	intel_memory_region_set_name(mem, "system");
66438f1cb68SLukasz Fiedorowicz 
665da1184cdSMatthew Auld 	return 0; /* Don't error, we can simply fallback to the kernel mnt */
666da1184cdSMatthew Auld }
667da1184cdSMatthew Auld 
668da1184cdSMatthew Auld static void release_shmem(struct intel_memory_region *mem)
669da1184cdSMatthew Auld {
670da1184cdSMatthew Auld 	i915_gemfs_fini(mem->i915);
671da1184cdSMatthew Auld }
672da1184cdSMatthew Auld 
673da1184cdSMatthew Auld static const struct intel_memory_region_ops shmem_region_ops = {
674da1184cdSMatthew Auld 	.init = init_shmem,
675da1184cdSMatthew Auld 	.release = release_shmem,
67697d55396SMatthew Auld 	.init_object = shmem_object_init,
677da1184cdSMatthew Auld };
678da1184cdSMatthew Auld 
679d1487389SThomas Hellström struct intel_memory_region *i915_gem_shmem_setup(struct drm_i915_private *i915,
680d1487389SThomas Hellström 						 u16 type, u16 instance)
681da1184cdSMatthew Auld {
682da1184cdSMatthew Auld 	return intel_memory_region_create(i915, 0,
683da1184cdSMatthew Auld 					  totalram_pages() << PAGE_SHIFT,
684da1184cdSMatthew Auld 					  PAGE_SIZE, 0,
685d1487389SThomas Hellström 					  type, instance,
686da1184cdSMatthew Auld 					  &shmem_region_ops);
687da1184cdSMatthew Auld }
68841a9c75dSChris Wilson 
68941a9c75dSChris Wilson bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj)
69041a9c75dSChris Wilson {
69141a9c75dSChris Wilson 	return obj->ops == &i915_gem_shmem_ops;
69241a9c75dSChris Wilson }
693