xref: /dragonfly/sys/dev/drm/i915/i915_gem.c (revision 303e1bae)
1561529b1SFrançois Tigeot /*
2477eb7f9SFrançois Tigeot  * Copyright © 2008-2015 Intel Corporation
3e3adcf8fSFrançois Tigeot  *
4e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10e3adcf8fSFrançois Tigeot  *
11e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13e3adcf8fSFrançois Tigeot  * Software.
14e3adcf8fSFrançois Tigeot  *
15e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e3adcf8fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e3adcf8fSFrançois Tigeot  * IN THE SOFTWARE.
22e3adcf8fSFrançois Tigeot  *
23e3adcf8fSFrançois Tigeot  * Authors:
24e3adcf8fSFrançois Tigeot  *    Eric Anholt <eric@anholt.net>
25e3adcf8fSFrançois Tigeot  *
26e3adcf8fSFrançois Tigeot  */
27e3adcf8fSFrançois Tigeot 
2818e26a6dSFrançois Tigeot #include <drm/drmP.h>
299edbd4a0SFrançois Tigeot #include <drm/drm_vma_manager.h>
305c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
31e3adcf8fSFrançois Tigeot #include "i915_drv.h"
32a85cb24fSFrançois Tigeot #include "i915_gem_clflush.h"
33477eb7f9SFrançois Tigeot #include "i915_vgpu.h"
347256b59bSFrançois Tigeot #include "i915_trace.h"
35e3adcf8fSFrançois Tigeot #include "intel_drv.h"
3671f41f3eSFrançois Tigeot #include "intel_frontbuffer.h"
378621f407SFrançois Tigeot #include "intel_mocs.h"
383f2dd94aSFrançois Tigeot #include "i915_gemfs.h"
394be47400SFrançois Tigeot #include <linux/dma-fence-array.h>
40a85cb24fSFrançois Tigeot #include <linux/kthread.h>
4187df8fc6SFrançois Tigeot #include <linux/reservation.h>
421964046dSFrançois Tigeot #include <linux/shmem_fs.h>
437256b59bSFrançois Tigeot #include <linux/slab.h>
44a85cb24fSFrançois Tigeot #include <linux/stop_machine.h>
45ba023890SFrançois Tigeot #include <linux/swap.h>
467256b59bSFrançois Tigeot #include <linux/pci.h>
47ba7073ceSFrançois Tigeot #include <linux/dma-buf.h>
48a85cb24fSFrançois Tigeot #include <linux/swiotlb.h>
49e3adcf8fSFrançois Tigeot 
5062bba8f6SFrançois Tigeot #include <sys/mman.h>
5162bba8f6SFrançois Tigeot #include <vm/vm_map.h>
5262bba8f6SFrançois Tigeot #include <vm/vm_param.h>
5362bba8f6SFrançois Tigeot 
543f2dd94aSFrançois Tigeot #undef USE_INSERT
553f2dd94aSFrançois Tigeot 
564be47400SFrançois Tigeot static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
577cbd1a46SFrançois Tigeot 
cpu_write_needs_clflush(struct drm_i915_gem_object * obj)589edbd4a0SFrançois Tigeot static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
599edbd4a0SFrançois Tigeot {
603f2dd94aSFrançois Tigeot 	if (obj->cache_dirty)
611487f786SFrançois Tigeot 		return false;
621487f786SFrançois Tigeot 
633f2dd94aSFrançois Tigeot 	if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
649edbd4a0SFrançois Tigeot 		return true;
659edbd4a0SFrançois Tigeot 
663f2dd94aSFrançois Tigeot 	return obj->pin_global; /* currently in use by HW, keep flushed */
679edbd4a0SFrançois Tigeot }
689edbd4a0SFrançois Tigeot 
691487f786SFrançois Tigeot static int
insert_mappable_node(struct i915_ggtt * ggtt,struct drm_mm_node * node,u32 size)704be47400SFrançois Tigeot insert_mappable_node(struct i915_ggtt *ggtt,
711487f786SFrançois Tigeot                      struct drm_mm_node *node, u32 size)
721487f786SFrançois Tigeot {
731487f786SFrançois Tigeot 	memset(node, 0, sizeof(*node));
74a85cb24fSFrançois Tigeot 	return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
75a85cb24fSFrançois Tigeot 					   size, 0, I915_COLOR_UNEVICTABLE,
764be47400SFrançois Tigeot 					   0, ggtt->mappable_end,
77a85cb24fSFrançois Tigeot 					   DRM_MM_INSERT_LOW);
781487f786SFrançois Tigeot }
791487f786SFrançois Tigeot 
801487f786SFrançois Tigeot static void
remove_mappable_node(struct drm_mm_node * node)811487f786SFrançois Tigeot remove_mappable_node(struct drm_mm_node *node)
821487f786SFrançois Tigeot {
831487f786SFrançois Tigeot 	drm_mm_remove_node(node);
841487f786SFrançois Tigeot }
851487f786SFrançois Tigeot 
86e11a51e3SFrançois Tigeot /* some bookkeeping */
i915_gem_info_add_obj(struct drm_i915_private * dev_priv,u64 size)87e11a51e3SFrançois Tigeot static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
884be47400SFrançois Tigeot 				  u64 size)
89e3adcf8fSFrançois Tigeot {
90ec5b6af4SFrançois Tigeot 	lockmgr(&dev_priv->mm.object_stat_lock, LK_EXCLUSIVE);
91e3adcf8fSFrançois Tigeot 	dev_priv->mm.object_count++;
92e3adcf8fSFrançois Tigeot 	dev_priv->mm.object_memory += size;
93ec5b6af4SFrançois Tigeot 	lockmgr(&dev_priv->mm.object_stat_lock, LK_RELEASE);
94e3adcf8fSFrançois Tigeot }
95e3adcf8fSFrançois Tigeot 
i915_gem_info_remove_obj(struct drm_i915_private * dev_priv,u64 size)96e11a51e3SFrançois Tigeot static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
974be47400SFrançois Tigeot 				     u64 size)
98e3adcf8fSFrançois Tigeot {
99ec5b6af4SFrançois Tigeot 	lockmgr(&dev_priv->mm.object_stat_lock, LK_EXCLUSIVE);
100e3adcf8fSFrançois Tigeot 	dev_priv->mm.object_count--;
101e3adcf8fSFrançois Tigeot 	dev_priv->mm.object_memory -= size;
102ec5b6af4SFrançois Tigeot 	lockmgr(&dev_priv->mm.object_stat_lock, LK_RELEASE);
103e3adcf8fSFrançois Tigeot }
104e3adcf8fSFrançois Tigeot 
105e3adcf8fSFrançois Tigeot static int
i915_gem_wait_for_error(struct i915_gpu_error * error)106a2fdbec6SFrançois Tigeot i915_gem_wait_for_error(struct i915_gpu_error *error)
107e3adcf8fSFrançois Tigeot {
108e3adcf8fSFrançois Tigeot 	int ret;
109e3adcf8fSFrançois Tigeot 
1104be47400SFrançois Tigeot 	might_sleep();
1114be47400SFrançois Tigeot 
112901476d5SFrançois Tigeot 	/*
113901476d5SFrançois Tigeot 	 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
114901476d5SFrançois Tigeot 	 * userspace. If it takes that long something really bad is going on and
115901476d5SFrançois Tigeot 	 * we should simply try to bail out and fail as gracefully as possible.
116901476d5SFrançois Tigeot 	 */
117a2fdbec6SFrançois Tigeot 	ret = wait_event_interruptible_timeout(error->reset_queue,
118a85cb24fSFrançois Tigeot 					       !i915_reset_backoff(error),
1194be47400SFrançois Tigeot 					       I915_RESET_TIMEOUT);
120901476d5SFrançois Tigeot 	if (ret == 0) {
121901476d5SFrançois Tigeot 		DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
122901476d5SFrançois Tigeot 		return -EIO;
123901476d5SFrançois Tigeot 	} else if (ret < 0) {
124901476d5SFrançois Tigeot 		return ret;
1258621f407SFrançois Tigeot 	} else {
126d65a337fSFrançois Tigeot 		return 0;
127e3adcf8fSFrançois Tigeot 	}
1288621f407SFrançois Tigeot }
129e3adcf8fSFrançois Tigeot 
i915_mutex_lock_interruptible(struct drm_device * dev)130e11a51e3SFrançois Tigeot int i915_mutex_lock_interruptible(struct drm_device *dev)
131e3adcf8fSFrançois Tigeot {
132303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
133e3adcf8fSFrançois Tigeot 	int ret;
134e3adcf8fSFrançois Tigeot 
135a2fdbec6SFrançois Tigeot 	ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
136d2557f23SFrançois Tigeot 	if (ret)
137d2557f23SFrançois Tigeot 		return ret;
138e3adcf8fSFrançois Tigeot 
139ba55f2f5SFrançois Tigeot 	ret = mutex_lock_interruptible(&dev->struct_mutex);
140e11a51e3SFrançois Tigeot 	if (ret)
141ba55f2f5SFrançois Tigeot 		return ret;
142e3adcf8fSFrançois Tigeot 
143e11a51e3SFrançois Tigeot 	return 0;
144e3adcf8fSFrançois Tigeot }
145e3adcf8fSFrançois Tigeot 
146e3adcf8fSFrançois Tigeot int
i915_gem_get_aperture_ioctl(struct drm_device * dev,void * data,struct drm_file * file)147e3adcf8fSFrançois Tigeot i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
148e3adcf8fSFrançois Tigeot 			    struct drm_file *file)
149e3adcf8fSFrançois Tigeot {
1508621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
1518621f407SFrançois Tigeot 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
152f192107fSFrançois Tigeot 	struct drm_i915_gem_get_aperture *args = data;
153a05eeebfSFrançois Tigeot 	struct i915_vma *vma;
1543f2dd94aSFrançois Tigeot 	u64 pinned;
155e3adcf8fSFrançois Tigeot 
1563f2dd94aSFrançois Tigeot 	pinned = ggtt->base.reserved;
157a2fdbec6SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
158c0e85e96SFrançois Tigeot 	list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
15971f41f3eSFrançois Tigeot 		if (i915_vma_is_pinned(vma))
160a05eeebfSFrançois Tigeot 			pinned += vma->node.size;
161c0e85e96SFrançois Tigeot 	list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
16271f41f3eSFrançois Tigeot 		if (i915_vma_is_pinned(vma))
163a05eeebfSFrançois Tigeot 			pinned += vma->node.size;
164a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
165e3adcf8fSFrançois Tigeot 
1668621f407SFrançois Tigeot 	args->aper_size = ggtt->base.total;
167e3adcf8fSFrançois Tigeot 	args->aper_available_size = args->aper_size - pinned;
168e3adcf8fSFrançois Tigeot 
169f192107fSFrançois Tigeot 	return 0;
170e3adcf8fSFrançois Tigeot }
171e3adcf8fSFrançois Tigeot 
i915_gem_object_get_pages_phys(struct drm_i915_gem_object * obj)1723f2dd94aSFrançois Tigeot static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
173ba55f2f5SFrançois Tigeot {
17472b9d63cSFrançois Tigeot #if 0
1751e12ee3bSFrançois Tigeot 	struct address_space *mapping = obj->base.filp->f_mapping;
17672b9d63cSFrançois Tigeot #else
17772b9d63cSFrançois Tigeot 	vm_object_t vm_obj = obj->base.filp;
17872b9d63cSFrançois Tigeot #endif
1794be47400SFrançois Tigeot 	drm_dma_handle_t *phys;
1802c9916cdSFrançois Tigeot 	struct sg_table *st;
1812c9916cdSFrançois Tigeot 	struct scatterlist *sg;
1824be47400SFrançois Tigeot 	char *vaddr;
1832c9916cdSFrançois Tigeot 	int i;
1843f2dd94aSFrançois Tigeot 	int err;
185ba55f2f5SFrançois Tigeot 
186a85cb24fSFrançois Tigeot 	if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
1873f2dd94aSFrançois Tigeot 		return -EINVAL;
188a85cb24fSFrançois Tigeot 
1894be47400SFrançois Tigeot 	/* Always aligning to the object size, allows a single allocation
1904be47400SFrançois Tigeot 	 * to handle all possible callers, and given typical object sizes,
1914be47400SFrançois Tigeot 	 * the alignment of the buddy allocation will naturally match.
1924be47400SFrançois Tigeot 	 */
1934be47400SFrançois Tigeot 	phys = drm_pci_alloc(obj->base.dev,
1943f2dd94aSFrançois Tigeot 			     roundup_pow_of_two(obj->base.size),
1954be47400SFrançois Tigeot 			     roundup_pow_of_two(obj->base.size));
1964be47400SFrançois Tigeot 	if (!phys)
1973f2dd94aSFrançois Tigeot 		return -ENOMEM;
198ba55f2f5SFrançois Tigeot 
1994be47400SFrançois Tigeot 	vaddr = phys->vaddr;
20072b9d63cSFrançois Tigeot 	VM_OBJECT_LOCK(vm_obj);
2012c9916cdSFrançois Tigeot 	for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
2027ec9f8e5SFrançois Tigeot 		struct page *page;
2032c9916cdSFrançois Tigeot 		char *src;
2042c9916cdSFrançois Tigeot 
20572b9d63cSFrançois Tigeot #if 0
2062c9916cdSFrançois Tigeot 		page = shmem_read_mapping_page(mapping, i);
20772b9d63cSFrançois Tigeot #else
20872b9d63cSFrançois Tigeot 		page = shmem_read_mapping_page(vm_obj, i);
20972b9d63cSFrançois Tigeot #endif
2104be47400SFrançois Tigeot 		if (IS_ERR(page)) {
2113f2dd94aSFrançois Tigeot 			err = PTR_ERR(page);
2124be47400SFrançois Tigeot 			goto err_phys;
2134be47400SFrançois Tigeot 		}
2142c9916cdSFrançois Tigeot 
2152c9916cdSFrançois Tigeot 		src = kmap_atomic(page);
2162c9916cdSFrançois Tigeot 		memcpy(vaddr, src, PAGE_SIZE);
2172c9916cdSFrançois Tigeot 		drm_clflush_virt_range(vaddr, PAGE_SIZE);
2182c9916cdSFrançois Tigeot 		kunmap_atomic(src);
2192c9916cdSFrançois Tigeot 
220c0e85e96SFrançois Tigeot 		put_page(page);
2212c9916cdSFrançois Tigeot 		vaddr += PAGE_SIZE;
2222c9916cdSFrançois Tigeot 	}
22372b9d63cSFrançois Tigeot 	VM_OBJECT_UNLOCK(vm_obj);
2242c9916cdSFrançois Tigeot 
2251487f786SFrançois Tigeot 	i915_gem_chipset_flush(to_i915(obj->base.dev));
2262c9916cdSFrançois Tigeot 
22772b9d63cSFrançois Tigeot 	st = kmalloc(sizeof(*st), M_DRM, GFP_KERNEL);
2284be47400SFrançois Tigeot 	if (!st) {
2293f2dd94aSFrançois Tigeot 		err = -ENOMEM;
2304be47400SFrançois Tigeot 		goto err_phys;
2314be47400SFrançois Tigeot 	}
2322c9916cdSFrançois Tigeot 
2332c9916cdSFrançois Tigeot 	if (sg_alloc_table(st, 1, GFP_KERNEL)) {
2342c9916cdSFrançois Tigeot 		kfree(st);
2353f2dd94aSFrançois Tigeot 		err = -ENOMEM;
2364be47400SFrançois Tigeot 		goto err_phys;
2372c9916cdSFrançois Tigeot 	}
2382c9916cdSFrançois Tigeot 
2392c9916cdSFrançois Tigeot 	sg = st->sgl;
2402c9916cdSFrançois Tigeot 	sg->offset = 0;
2412c9916cdSFrançois Tigeot 	sg->length = obj->base.size;
2422c9916cdSFrançois Tigeot 
2434be47400SFrançois Tigeot 	sg_dma_address(sg) = phys->busaddr;
2442c9916cdSFrançois Tigeot 	sg_dma_len(sg) = obj->base.size;
2452c9916cdSFrançois Tigeot 
2464be47400SFrançois Tigeot 	obj->phys_handle = phys;
2473f2dd94aSFrançois Tigeot 
2483f2dd94aSFrançois Tigeot 	__i915_gem_object_set_pages(obj, st, sg->length);
2493f2dd94aSFrançois Tigeot 
2503f2dd94aSFrançois Tigeot 	return 0;
2514be47400SFrançois Tigeot 
2524be47400SFrançois Tigeot err_phys:
2534be47400SFrançois Tigeot 	drm_pci_free(obj->base.dev, phys);
2543f2dd94aSFrançois Tigeot 
2553f2dd94aSFrançois Tigeot 	return err;
2563f2dd94aSFrançois Tigeot }
2573f2dd94aSFrançois Tigeot 
__start_cpu_write(struct drm_i915_gem_object * obj)2583f2dd94aSFrançois Tigeot static void __start_cpu_write(struct drm_i915_gem_object *obj)
2593f2dd94aSFrançois Tigeot {
2603f2dd94aSFrançois Tigeot 	obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2613f2dd94aSFrançois Tigeot 	obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2623f2dd94aSFrançois Tigeot 	if (cpu_write_needs_clflush(obj))
2633f2dd94aSFrançois Tigeot 		obj->cache_dirty = true;
2642c9916cdSFrançois Tigeot }
2652c9916cdSFrançois Tigeot 
2662c9916cdSFrançois Tigeot static void
__i915_gem_object_release_shmem(struct drm_i915_gem_object * obj,struct sg_table * pages,bool needs_clflush)2674be47400SFrançois Tigeot __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
2684be47400SFrançois Tigeot 				struct sg_table *pages,
2694be47400SFrançois Tigeot 				bool needs_clflush)
2702c9916cdSFrançois Tigeot {
2714be47400SFrançois Tigeot 	GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
2722c9916cdSFrançois Tigeot 
2734be47400SFrançois Tigeot 	if (obj->mm.madv == I915_MADV_DONTNEED)
2744be47400SFrançois Tigeot 		obj->mm.dirty = false;
2752c9916cdSFrançois Tigeot 
2764be47400SFrançois Tigeot 	if (needs_clflush &&
2774be47400SFrançois Tigeot 	    (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
2783f2dd94aSFrançois Tigeot 	    !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
2794be47400SFrançois Tigeot 		drm_clflush_sg(pages);
2804be47400SFrançois Tigeot 
2813f2dd94aSFrançois Tigeot 	__start_cpu_write(obj);
2822c9916cdSFrançois Tigeot }
2832c9916cdSFrançois Tigeot 
2844be47400SFrançois Tigeot static void
i915_gem_object_put_pages_phys(struct drm_i915_gem_object * obj,struct sg_table * pages)2854be47400SFrançois Tigeot i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
2864be47400SFrançois Tigeot 			       struct sg_table *pages)
2874be47400SFrançois Tigeot {
2884be47400SFrançois Tigeot 	__i915_gem_object_release_shmem(obj, pages, false);
2892c9916cdSFrançois Tigeot 
2904be47400SFrançois Tigeot 	if (obj->mm.dirty) {
29172b9d63cSFrançois Tigeot #if 0
2921e12ee3bSFrançois Tigeot 		struct address_space *mapping = obj->base.filp->f_mapping;
29372b9d63cSFrançois Tigeot #else
294a85cb24fSFrançois Tigeot 		vm_object_t vm_obj = obj->base.filp;
29572b9d63cSFrançois Tigeot #endif
2962c9916cdSFrançois Tigeot 		char *vaddr = obj->phys_handle->vaddr;
297ba55f2f5SFrançois Tigeot 		int i;
298ba55f2f5SFrançois Tigeot 
299ba55f2f5SFrançois Tigeot 		for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
3002c9916cdSFrançois Tigeot 			struct page *page;
3012c9916cdSFrançois Tigeot 			char *dst;
3022c9916cdSFrançois Tigeot 
303a85cb24fSFrançois Tigeot 			page = shmem_read_mapping_page(vm_obj, i);
3042c9916cdSFrançois Tigeot 			if (IS_ERR(page))
3052c9916cdSFrançois Tigeot 				continue;
3062c9916cdSFrançois Tigeot 
3072c9916cdSFrançois Tigeot 			dst = kmap_atomic(page);
3082c9916cdSFrançois Tigeot 			drm_clflush_virt_range(vaddr, PAGE_SIZE);
309ba55f2f5SFrançois Tigeot 			memcpy(dst, vaddr, PAGE_SIZE);
310ba55f2f5SFrançois Tigeot 			kunmap_atomic(dst);
311ba55f2f5SFrançois Tigeot 
312ba55f2f5SFrançois Tigeot 			set_page_dirty(page);
3134be47400SFrançois Tigeot 			if (obj->mm.madv == I915_MADV_WILLNEED)
314ba55f2f5SFrançois Tigeot 				mark_page_accessed(page);
315c0e85e96SFrançois Tigeot 			put_page(page);
316ba55f2f5SFrançois Tigeot 			vaddr += PAGE_SIZE;
317ba55f2f5SFrançois Tigeot 		}
3184be47400SFrançois Tigeot 		obj->mm.dirty = false;
319ba55f2f5SFrançois Tigeot 	}
320ba55f2f5SFrançois Tigeot 
3214be47400SFrançois Tigeot 	sg_free_table(pages);
3224be47400SFrançois Tigeot 	kfree(pages);
3234be47400SFrançois Tigeot 
3244be47400SFrançois Tigeot 	drm_pci_free(obj->base.dev, obj->phys_handle);
3252c9916cdSFrançois Tigeot }
3262c9916cdSFrançois Tigeot 
3272c9916cdSFrançois Tigeot static void
i915_gem_object_release_phys(struct drm_i915_gem_object * obj)3282c9916cdSFrançois Tigeot i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
3292c9916cdSFrançois Tigeot {
3304be47400SFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
3312c9916cdSFrançois Tigeot }
3322c9916cdSFrançois Tigeot 
3332c9916cdSFrançois Tigeot static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
3342c9916cdSFrançois Tigeot 	.get_pages = i915_gem_object_get_pages_phys,
3352c9916cdSFrançois Tigeot 	.put_pages = i915_gem_object_put_pages_phys,
3362c9916cdSFrançois Tigeot 	.release = i915_gem_object_release_phys,
337bf017597SFrançois Tigeot };
3382c9916cdSFrançois Tigeot 
339a85cb24fSFrançois Tigeot static const struct drm_i915_gem_object_ops i915_gem_object_ops;
340a85cb24fSFrançois Tigeot 
i915_gem_object_unbind(struct drm_i915_gem_object * obj)3411e12ee3bSFrançois Tigeot int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
3422c9916cdSFrançois Tigeot {
34371f41f3eSFrançois Tigeot 	struct i915_vma *vma;
34471f41f3eSFrançois Tigeot 	LINUX_LIST_HEAD(still_in_list);
3452c9916cdSFrançois Tigeot 	int ret;
3462c9916cdSFrançois Tigeot 
3471e12ee3bSFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
3481e12ee3bSFrançois Tigeot 
3491e12ee3bSFrançois Tigeot 	/* Closed vma are removed from the obj->vma_list - but they may
3501e12ee3bSFrançois Tigeot 	 * still have an active binding on the object. To remove those we
3511e12ee3bSFrançois Tigeot 	 * must wait for all rendering to complete to the object (as unbinding
3521e12ee3bSFrançois Tigeot 	 * must anyway), and retire the requests.
35371f41f3eSFrançois Tigeot 	 */
3543f2dd94aSFrançois Tigeot 	ret = i915_gem_object_set_to_cpu_domain(obj, false);
3551e12ee3bSFrançois Tigeot 	if (ret)
3561e12ee3bSFrançois Tigeot 		return ret;
3571e12ee3bSFrançois Tigeot 
35871f41f3eSFrançois Tigeot 	while ((vma = list_first_entry_or_null(&obj->vma_list,
35971f41f3eSFrançois Tigeot 					       struct i915_vma,
36071f41f3eSFrançois Tigeot 					       obj_link))) {
36171f41f3eSFrançois Tigeot 		list_move_tail(&vma->obj_link, &still_in_list);
36271f41f3eSFrançois Tigeot 		ret = i915_vma_unbind(vma);
36371f41f3eSFrançois Tigeot 		if (ret)
3642c9916cdSFrançois Tigeot 			break;
36571f41f3eSFrançois Tigeot 	}
36671f41f3eSFrançois Tigeot 	list_splice(&still_in_list, &obj->vma_list);
3672c9916cdSFrançois Tigeot 
3682c9916cdSFrançois Tigeot 	return ret;
369ba55f2f5SFrançois Tigeot }
370ba55f2f5SFrançois Tigeot 
3714be47400SFrançois Tigeot static long
i915_gem_object_wait_fence(struct dma_fence * fence,unsigned int flags,long timeout,struct intel_rps_client * rps_client)3724be47400SFrançois Tigeot i915_gem_object_wait_fence(struct dma_fence *fence,
3734be47400SFrançois Tigeot 			   unsigned int flags,
3744be47400SFrançois Tigeot 			   long timeout,
3753f2dd94aSFrançois Tigeot 			   struct intel_rps_client *rps_client)
3764be47400SFrançois Tigeot {
3774be47400SFrançois Tigeot 	struct drm_i915_gem_request *rq;
3784be47400SFrançois Tigeot 
3794be47400SFrançois Tigeot 	BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
3804be47400SFrançois Tigeot 
3814be47400SFrançois Tigeot 	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
3824be47400SFrançois Tigeot 		return timeout;
3834be47400SFrançois Tigeot 
3844be47400SFrançois Tigeot 	if (!dma_fence_is_i915(fence))
3854be47400SFrançois Tigeot 		return dma_fence_wait_timeout(fence,
3864be47400SFrançois Tigeot 					      flags & I915_WAIT_INTERRUPTIBLE,
3874be47400SFrançois Tigeot 					      timeout);
3884be47400SFrançois Tigeot 
3894be47400SFrançois Tigeot 	rq = to_request(fence);
3904be47400SFrançois Tigeot 	if (i915_gem_request_completed(rq))
3914be47400SFrançois Tigeot 		goto out;
3924be47400SFrançois Tigeot 
3934be47400SFrançois Tigeot 	/* This client is about to stall waiting for the GPU. In many cases
3944be47400SFrançois Tigeot 	 * this is undesirable and limits the throughput of the system, as
3954be47400SFrançois Tigeot 	 * many clients cannot continue processing user input/output whilst
3964be47400SFrançois Tigeot 	 * blocked. RPS autotuning may take tens of milliseconds to respond
3974be47400SFrançois Tigeot 	 * to the GPU load and thus incurs additional latency for the client.
3984be47400SFrançois Tigeot 	 * We can circumvent that by promoting the GPU frequency to maximum
3994be47400SFrançois Tigeot 	 * before we wait. This makes the GPU throttle up much more quickly
4004be47400SFrançois Tigeot 	 * (good for benchmarks and user experience, e.g. window animations),
4014be47400SFrançois Tigeot 	 * but at a cost of spending more power processing the workload
4024be47400SFrançois Tigeot 	 * (bad for battery). Not all clients even want their results
4034be47400SFrançois Tigeot 	 * immediately and for them we should just let the GPU select its own
4044be47400SFrançois Tigeot 	 * frequency to maximise efficiency. To prevent a single client from
4054be47400SFrançois Tigeot 	 * forcing the clocks too high for the whole system, we only allow
4064be47400SFrançois Tigeot 	 * each client to waitboost once in a busy period.
4074be47400SFrançois Tigeot 	 */
4083f2dd94aSFrançois Tigeot 	if (rps_client) {
4094be47400SFrançois Tigeot 		if (INTEL_GEN(rq->i915) >= 6)
4103f2dd94aSFrançois Tigeot 			gen6_rps_boost(rq, rps_client);
4114be47400SFrançois Tigeot 		else
4123f2dd94aSFrançois Tigeot 			rps_client = NULL;
4134be47400SFrançois Tigeot 	}
4144be47400SFrançois Tigeot 
4154be47400SFrançois Tigeot 	timeout = i915_wait_request(rq, flags, timeout);
4164be47400SFrançois Tigeot 
4174be47400SFrançois Tigeot out:
4184be47400SFrançois Tigeot 	if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
4194be47400SFrançois Tigeot 		i915_gem_request_retire_upto(rq);
4204be47400SFrançois Tigeot 
4214be47400SFrançois Tigeot 	return timeout;
4224be47400SFrançois Tigeot }
4234be47400SFrançois Tigeot 
4244be47400SFrançois Tigeot static long
i915_gem_object_wait_reservation(struct reservation_object * resv,unsigned int flags,long timeout,struct intel_rps_client * rps_client)4254be47400SFrançois Tigeot i915_gem_object_wait_reservation(struct reservation_object *resv,
4264be47400SFrançois Tigeot 				 unsigned int flags,
4274be47400SFrançois Tigeot 				 long timeout,
4283f2dd94aSFrançois Tigeot 				 struct intel_rps_client *rps_client)
4294be47400SFrançois Tigeot {
430a85cb24fSFrançois Tigeot 	unsigned int seq = __read_seqcount_begin(&resv->seq);
4314be47400SFrançois Tigeot 	struct dma_fence *excl;
432a85cb24fSFrançois Tigeot 	bool prune_fences = false;
4334be47400SFrançois Tigeot 
4344be47400SFrançois Tigeot 	if (flags & I915_WAIT_ALL) {
4354be47400SFrançois Tigeot 		struct dma_fence **shared;
4364be47400SFrançois Tigeot 		unsigned int count, i;
4374be47400SFrançois Tigeot 		int ret;
4384be47400SFrançois Tigeot 
4394be47400SFrançois Tigeot 		ret = reservation_object_get_fences_rcu(resv,
4404be47400SFrançois Tigeot 							&excl, &count, &shared);
4414be47400SFrançois Tigeot 		if (ret)
4424be47400SFrançois Tigeot 			return ret;
4434be47400SFrançois Tigeot 
4444be47400SFrançois Tigeot 		for (i = 0; i < count; i++) {
4454be47400SFrançois Tigeot 			timeout = i915_gem_object_wait_fence(shared[i],
4464be47400SFrançois Tigeot 							     flags, timeout,
4473f2dd94aSFrançois Tigeot 							     rps_client);
4484be47400SFrançois Tigeot 			if (timeout < 0)
4494be47400SFrançois Tigeot 				break;
4504be47400SFrançois Tigeot 
4514be47400SFrançois Tigeot 			dma_fence_put(shared[i]);
4524be47400SFrançois Tigeot 		}
4534be47400SFrançois Tigeot 
4544be47400SFrançois Tigeot 		for (; i < count; i++)
4554be47400SFrançois Tigeot 			dma_fence_put(shared[i]);
4564be47400SFrançois Tigeot 		kfree(shared);
457a85cb24fSFrançois Tigeot 
458a85cb24fSFrançois Tigeot 		prune_fences = count && timeout >= 0;
4594be47400SFrançois Tigeot 	} else {
4604be47400SFrançois Tigeot 		excl = reservation_object_get_excl_rcu(resv);
4614be47400SFrançois Tigeot 	}
4624be47400SFrançois Tigeot 
463a85cb24fSFrançois Tigeot 	if (excl && timeout >= 0) {
4643f2dd94aSFrançois Tigeot 		timeout = i915_gem_object_wait_fence(excl, flags, timeout,
4653f2dd94aSFrançois Tigeot 						     rps_client);
466a85cb24fSFrançois Tigeot 		prune_fences = timeout >= 0;
467a85cb24fSFrançois Tigeot 	}
4684be47400SFrançois Tigeot 
4694be47400SFrançois Tigeot 	dma_fence_put(excl);
4704be47400SFrançois Tigeot 
471a85cb24fSFrançois Tigeot 	/* Oportunistically prune the fences iff we know they have *all* been
472a85cb24fSFrançois Tigeot 	 * signaled and that the reservation object has not been changed (i.e.
473a85cb24fSFrançois Tigeot 	 * no new fences have been added).
474a85cb24fSFrançois Tigeot 	 */
475a85cb24fSFrançois Tigeot 	if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
476a85cb24fSFrançois Tigeot 		if (reservation_object_trylock(resv)) {
477a85cb24fSFrançois Tigeot 			if (!__read_seqcount_retry(&resv->seq, seq))
478a85cb24fSFrançois Tigeot 				reservation_object_add_excl_fence(resv, NULL);
479a85cb24fSFrançois Tigeot 			reservation_object_unlock(resv);
480a85cb24fSFrançois Tigeot 		}
481a85cb24fSFrançois Tigeot 	}
482a85cb24fSFrançois Tigeot 
4834be47400SFrançois Tigeot 	return timeout;
4844be47400SFrançois Tigeot }
4854be47400SFrançois Tigeot 
__fence_set_priority(struct dma_fence * fence,int prio)4864be47400SFrançois Tigeot static void __fence_set_priority(struct dma_fence *fence, int prio)
4874be47400SFrançois Tigeot {
4884be47400SFrançois Tigeot 	struct drm_i915_gem_request *rq;
4894be47400SFrançois Tigeot 	struct intel_engine_cs *engine;
4904be47400SFrançois Tigeot 
4913f2dd94aSFrançois Tigeot 	if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
4924be47400SFrançois Tigeot 		return;
4934be47400SFrançois Tigeot 
4944be47400SFrançois Tigeot 	rq = to_request(fence);
4954be47400SFrançois Tigeot 	engine = rq->engine;
4964be47400SFrançois Tigeot 	if (!engine->schedule)
4974be47400SFrançois Tigeot 		return;
4984be47400SFrançois Tigeot 
4994be47400SFrançois Tigeot 	engine->schedule(rq, prio);
5004be47400SFrançois Tigeot }
5014be47400SFrançois Tigeot 
fence_set_priority(struct dma_fence * fence,int prio)5024be47400SFrançois Tigeot static void fence_set_priority(struct dma_fence *fence, int prio)
5034be47400SFrançois Tigeot {
5044be47400SFrançois Tigeot 	/* Recurse once into a fence-array */
5054be47400SFrançois Tigeot 	if (dma_fence_is_array(fence)) {
5064be47400SFrançois Tigeot 		struct dma_fence_array *array = to_dma_fence_array(fence);
5074be47400SFrançois Tigeot 		int i;
5084be47400SFrançois Tigeot 
5094be47400SFrançois Tigeot 		for (i = 0; i < array->num_fences; i++)
5104be47400SFrançois Tigeot 			__fence_set_priority(array->fences[i], prio);
5114be47400SFrançois Tigeot 	} else {
5124be47400SFrançois Tigeot 		__fence_set_priority(fence, prio);
5134be47400SFrançois Tigeot 	}
5144be47400SFrançois Tigeot }
5154be47400SFrançois Tigeot 
5164be47400SFrançois Tigeot int
i915_gem_object_wait_priority(struct drm_i915_gem_object * obj,unsigned int flags,int prio)5174be47400SFrançois Tigeot i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
5184be47400SFrançois Tigeot 			      unsigned int flags,
5194be47400SFrançois Tigeot 			      int prio)
5204be47400SFrançois Tigeot {
5214be47400SFrançois Tigeot 	struct dma_fence *excl;
5224be47400SFrançois Tigeot 
5234be47400SFrançois Tigeot 	if (flags & I915_WAIT_ALL) {
5244be47400SFrançois Tigeot 		struct dma_fence **shared;
5254be47400SFrançois Tigeot 		unsigned int count, i;
5264be47400SFrançois Tigeot 		int ret;
5274be47400SFrançois Tigeot 
5284be47400SFrançois Tigeot 		ret = reservation_object_get_fences_rcu(obj->resv,
5294be47400SFrançois Tigeot 							&excl, &count, &shared);
5304be47400SFrançois Tigeot 		if (ret)
5314be47400SFrançois Tigeot 			return ret;
5324be47400SFrançois Tigeot 
5334be47400SFrançois Tigeot 		for (i = 0; i < count; i++) {
5344be47400SFrançois Tigeot 			fence_set_priority(shared[i], prio);
5354be47400SFrançois Tigeot 			dma_fence_put(shared[i]);
5364be47400SFrançois Tigeot 		}
5374be47400SFrançois Tigeot 
5384be47400SFrançois Tigeot 		kfree(shared);
5394be47400SFrançois Tigeot 	} else {
5404be47400SFrançois Tigeot 		excl = reservation_object_get_excl_rcu(obj->resv);
5414be47400SFrançois Tigeot 	}
5424be47400SFrançois Tigeot 
5434be47400SFrançois Tigeot 	if (excl) {
5444be47400SFrançois Tigeot 		fence_set_priority(excl, prio);
5454be47400SFrançois Tigeot 		dma_fence_put(excl);
5464be47400SFrançois Tigeot 	}
5474be47400SFrançois Tigeot 	return 0;
5484be47400SFrançois Tigeot }
5494be47400SFrançois Tigeot 
55071f41f3eSFrançois Tigeot /**
5514be47400SFrançois Tigeot  * Waits for rendering to the object to be completed
55271f41f3eSFrançois Tigeot  * @obj: i915 gem object
5534be47400SFrançois Tigeot  * @flags: how to wait (under a lock, for all rendering or just for writes etc)
5544be47400SFrançois Tigeot  * @timeout: how long to wait
5554be47400SFrançois Tigeot  * @rps: client (user process) to charge for any waitboosting
55671f41f3eSFrançois Tigeot  */
55771f41f3eSFrançois Tigeot int
i915_gem_object_wait(struct drm_i915_gem_object * obj,unsigned int flags,long timeout,struct intel_rps_client * rps_client)5584be47400SFrançois Tigeot i915_gem_object_wait(struct drm_i915_gem_object *obj,
5594be47400SFrançois Tigeot 		     unsigned int flags,
5604be47400SFrançois Tigeot 		     long timeout,
5613f2dd94aSFrançois Tigeot 		     struct intel_rps_client *rps_client)
56271f41f3eSFrançois Tigeot {
5634be47400SFrançois Tigeot 	might_sleep();
5644be47400SFrançois Tigeot #if IS_ENABLED(CONFIG_LOCKDEP)
5654be47400SFrançois Tigeot 	GEM_BUG_ON(debug_locks &&
5664be47400SFrançois Tigeot 		   !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
5674be47400SFrançois Tigeot 		   !!(flags & I915_WAIT_LOCKED));
5684be47400SFrançois Tigeot #endif
5694be47400SFrançois Tigeot 	GEM_BUG_ON(timeout < 0);
57071f41f3eSFrançois Tigeot 
5714be47400SFrançois Tigeot 	timeout = i915_gem_object_wait_reservation(obj->resv,
5724be47400SFrançois Tigeot 						   flags, timeout,
5733f2dd94aSFrançois Tigeot 						   rps_client);
5744be47400SFrançois Tigeot 	return timeout < 0 ? timeout : 0;
57571f41f3eSFrançois Tigeot }
57671f41f3eSFrançois Tigeot 
to_rps_client(struct drm_file * file)57771f41f3eSFrançois Tigeot static struct intel_rps_client *to_rps_client(struct drm_file *file)
57871f41f3eSFrançois Tigeot {
57971f41f3eSFrançois Tigeot 	struct drm_i915_file_private *fpriv = file->driver_priv;
58071f41f3eSFrançois Tigeot 
5813f2dd94aSFrançois Tigeot 	return &fpriv->rps_client;
582ba55f2f5SFrançois Tigeot }
583ba55f2f5SFrançois Tigeot 
584ba55f2f5SFrançois Tigeot static int
i915_gem_phys_pwrite(struct drm_i915_gem_object * obj,struct drm_i915_gem_pwrite * args,struct drm_file * file)585ba55f2f5SFrançois Tigeot i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
586ba55f2f5SFrançois Tigeot 		     struct drm_i915_gem_pwrite *args,
5874be47400SFrançois Tigeot 		     struct drm_file *file)
588ba55f2f5SFrançois Tigeot {
5891487f786SFrançois Tigeot 	void *vaddr = obj->phys_handle->vaddr + args->offset;
5908621f407SFrançois Tigeot 	char __user *user_data = u64_to_user_ptr(args->data_ptr);
5912c9916cdSFrançois Tigeot 
5922c9916cdSFrançois Tigeot 	/* We manually control the domain here and pretend that it
5932c9916cdSFrançois Tigeot 	 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
5942c9916cdSFrançois Tigeot 	 */
595a05eeebfSFrançois Tigeot 	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
5964be47400SFrançois Tigeot 	if (copy_from_user(vaddr, user_data, args->size))
5974be47400SFrançois Tigeot 		return -EFAULT;
598ba55f2f5SFrançois Tigeot 
5992c9916cdSFrançois Tigeot 	drm_clflush_virt_range(vaddr, args->size);
6004be47400SFrançois Tigeot 	i915_gem_chipset_flush(to_i915(obj->base.dev));
601477eb7f9SFrançois Tigeot 
602a85cb24fSFrançois Tigeot 	intel_fb_obj_flush(obj, ORIGIN_CPU);
6034be47400SFrançois Tigeot 	return 0;
604ba55f2f5SFrançois Tigeot }
605ba55f2f5SFrançois Tigeot 
i915_gem_object_alloc(struct drm_i915_private * dev_priv)606a85cb24fSFrançois Tigeot void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
6079edbd4a0SFrançois Tigeot {
60865d5e47fSFrançois Tigeot 	return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
6099edbd4a0SFrançois Tigeot }
6109edbd4a0SFrançois Tigeot 
i915_gem_object_free(struct drm_i915_gem_object * obj)6115d0b1887SFrançois Tigeot void i915_gem_object_free(struct drm_i915_gem_object *obj)
6125d0b1887SFrançois Tigeot {
61365d5e47fSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
61465d5e47fSFrançois Tigeot 	kmem_cache_free(dev_priv->objects, obj);
6155d0b1887SFrançois Tigeot }
6165d0b1887SFrançois Tigeot 
617245593daSFrançois Tigeot static int
i915_gem_create(struct drm_file * file,struct drm_i915_private * dev_priv,uint64_t size,uint32_t * handle_p)618d2557f23SFrançois Tigeot i915_gem_create(struct drm_file *file,
619a85cb24fSFrançois Tigeot 		struct drm_i915_private *dev_priv,
620d2557f23SFrançois Tigeot 		uint64_t size,
621e3adcf8fSFrançois Tigeot 		uint32_t *handle_p)
622e3adcf8fSFrançois Tigeot {
623e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
624e3adcf8fSFrançois Tigeot 	int ret;
625d2557f23SFrançois Tigeot 	u32 handle;
626e3adcf8fSFrançois Tigeot 
627e3adcf8fSFrançois Tigeot 	size = roundup(size, PAGE_SIZE);
628e3adcf8fSFrançois Tigeot 	if (size == 0)
629d2557f23SFrançois Tigeot 		return -EINVAL;
630e3adcf8fSFrançois Tigeot 
631d2557f23SFrançois Tigeot 	/* Allocate the new object */
632a85cb24fSFrançois Tigeot 	obj = i915_gem_object_create(dev_priv, size);
6331487f786SFrançois Tigeot 	if (IS_ERR(obj))
6341487f786SFrançois Tigeot 		return PTR_ERR(obj);
635e3adcf8fSFrançois Tigeot 
636e3adcf8fSFrançois Tigeot 	ret = drm_gem_handle_create(file, &obj->base, &handle);
637e3adcf8fSFrançois Tigeot 	/* drop reference from allocate - handle holds it now */
6384be47400SFrançois Tigeot 	i915_gem_object_put(obj);
6399edbd4a0SFrançois Tigeot 	if (ret)
6409edbd4a0SFrançois Tigeot 		return ret;
6417256b59bSFrançois Tigeot 
642e3adcf8fSFrançois Tigeot 	*handle_p = handle;
643d2557f23SFrançois Tigeot 	return 0;
644e3adcf8fSFrançois Tigeot }
645e3adcf8fSFrançois Tigeot 
646e3adcf8fSFrançois Tigeot int
i915_gem_dumb_create(struct drm_file * file,struct drm_device * dev,struct drm_mode_create_dumb * args)647e11a51e3SFrançois Tigeot i915_gem_dumb_create(struct drm_file *file,
648e11a51e3SFrançois Tigeot 		     struct drm_device *dev,
649e3adcf8fSFrançois Tigeot 		     struct drm_mode_create_dumb *args)
650e3adcf8fSFrançois Tigeot {
651e3adcf8fSFrançois Tigeot 	/* have to work out size/pitch and return them */
6529edbd4a0SFrançois Tigeot 	args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
653e3adcf8fSFrançois Tigeot 	args->size = args->pitch * args->height;
654a85cb24fSFrançois Tigeot 	return i915_gem_create(file, to_i915(dev),
655d2557f23SFrançois Tigeot 			       args->size, &args->handle);
656e3adcf8fSFrançois Tigeot }
657e3adcf8fSFrançois Tigeot 
gpu_write_needs_clflush(struct drm_i915_gem_object * obj)6583f2dd94aSFrançois Tigeot static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
6593f2dd94aSFrançois Tigeot {
6603f2dd94aSFrançois Tigeot 	return !(obj->cache_level == I915_CACHE_NONE ||
6613f2dd94aSFrançois Tigeot 		 obj->cache_level == I915_CACHE_WT);
6623f2dd94aSFrançois Tigeot }
6633f2dd94aSFrançois Tigeot 
664e11a51e3SFrançois Tigeot /**
665e11a51e3SFrançois Tigeot  * Creates a new mm object and returns a handle to it.
6661487f786SFrançois Tigeot  * @dev: drm device pointer
6671487f786SFrançois Tigeot  * @data: ioctl data blob
6681487f786SFrançois Tigeot  * @file: drm file pointer
669e11a51e3SFrançois Tigeot  */
670e3adcf8fSFrançois Tigeot int
i915_gem_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file)671e3adcf8fSFrançois Tigeot i915_gem_create_ioctl(struct drm_device *dev, void *data,
672e3adcf8fSFrançois Tigeot 		      struct drm_file *file)
673e3adcf8fSFrançois Tigeot {
674a85cb24fSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
675e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_create *args = data;
676e3adcf8fSFrançois Tigeot 
677a85cb24fSFrançois Tigeot 	i915_gem_flush_free_objects(dev_priv);
6784be47400SFrançois Tigeot 
679a85cb24fSFrançois Tigeot 	return i915_gem_create(file, dev_priv,
680d2557f23SFrançois Tigeot 			       args->size, &args->handle);
681e3adcf8fSFrançois Tigeot }
682e3adcf8fSFrançois Tigeot 
6833f2dd94aSFrançois Tigeot static inline enum fb_op_origin
fb_write_origin(struct drm_i915_gem_object * obj,unsigned int domain)6843f2dd94aSFrançois Tigeot fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
6853f2dd94aSFrançois Tigeot {
6863f2dd94aSFrançois Tigeot 	return (domain == I915_GEM_DOMAIN_GTT ?
6873f2dd94aSFrançois Tigeot 		obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
6883f2dd94aSFrançois Tigeot }
6893f2dd94aSFrançois Tigeot 
6903f2dd94aSFrançois Tigeot static void
flush_write_domain(struct drm_i915_gem_object * obj,unsigned int flush_domains)6913f2dd94aSFrançois Tigeot flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
6923f2dd94aSFrançois Tigeot {
6933f2dd94aSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
6943f2dd94aSFrançois Tigeot 
6953f2dd94aSFrançois Tigeot 	if (!(obj->base.write_domain & flush_domains))
6963f2dd94aSFrançois Tigeot 		return;
6973f2dd94aSFrançois Tigeot 
6983f2dd94aSFrançois Tigeot 	/* No actual flushing is required for the GTT write domain.  Writes
6993f2dd94aSFrançois Tigeot 	 * to it "immediately" go to main memory as far as we know, so there's
7003f2dd94aSFrançois Tigeot 	 * no chipset flush.  It also doesn't land in render cache.
7013f2dd94aSFrançois Tigeot 	 *
7023f2dd94aSFrançois Tigeot 	 * However, we do have to enforce the order so that all writes through
7033f2dd94aSFrançois Tigeot 	 * the GTT land before any writes to the device, such as updates to
7043f2dd94aSFrançois Tigeot 	 * the GATT itself.
7053f2dd94aSFrançois Tigeot 	 *
7063f2dd94aSFrançois Tigeot 	 * We also have to wait a bit for the writes to land from the GTT.
7073f2dd94aSFrançois Tigeot 	 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
7083f2dd94aSFrançois Tigeot 	 * timing. This issue has only been observed when switching quickly
7093f2dd94aSFrançois Tigeot 	 * between GTT writes and CPU reads from inside the kernel on recent hw,
7103f2dd94aSFrançois Tigeot 	 * and it appears to only affect discrete GTT blocks (i.e. on LLC
7113f2dd94aSFrançois Tigeot 	 * system agents we cannot reproduce this behaviour).
7123f2dd94aSFrançois Tigeot 	 */
7133f2dd94aSFrançois Tigeot 	wmb();
7143f2dd94aSFrançois Tigeot 
7153f2dd94aSFrançois Tigeot 	switch (obj->base.write_domain) {
7163f2dd94aSFrançois Tigeot 	case I915_GEM_DOMAIN_GTT:
7173f2dd94aSFrançois Tigeot 		if (!HAS_LLC(dev_priv)) {
7183f2dd94aSFrançois Tigeot 			intel_runtime_pm_get(dev_priv);
7193f2dd94aSFrançois Tigeot 			spin_lock_irq(&dev_priv->uncore.lock);
7203f2dd94aSFrançois Tigeot 			POSTING_READ_FW(RING_HEAD(dev_priv->engine[RCS]->mmio_base));
7213f2dd94aSFrançois Tigeot 			spin_unlock_irq(&dev_priv->uncore.lock);
7223f2dd94aSFrançois Tigeot 			intel_runtime_pm_put(dev_priv);
7233f2dd94aSFrançois Tigeot 		}
7243f2dd94aSFrançois Tigeot 
7253f2dd94aSFrançois Tigeot 		intel_fb_obj_flush(obj,
7263f2dd94aSFrançois Tigeot 				   fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
7273f2dd94aSFrançois Tigeot 		break;
7283f2dd94aSFrançois Tigeot 
7293f2dd94aSFrançois Tigeot 	case I915_GEM_DOMAIN_CPU:
7303f2dd94aSFrançois Tigeot 		i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
7313f2dd94aSFrançois Tigeot 		break;
7323f2dd94aSFrançois Tigeot 
7333f2dd94aSFrançois Tigeot 	case I915_GEM_DOMAIN_RENDER:
7343f2dd94aSFrançois Tigeot 		if (gpu_write_needs_clflush(obj))
7353f2dd94aSFrançois Tigeot 			obj->cache_dirty = true;
7363f2dd94aSFrançois Tigeot 		break;
7373f2dd94aSFrançois Tigeot 	}
7383f2dd94aSFrançois Tigeot 
7393f2dd94aSFrançois Tigeot 	obj->base.write_domain = 0;
7403f2dd94aSFrançois Tigeot }
7413f2dd94aSFrançois Tigeot 
7427256b59bSFrançois Tigeot static inline int
__copy_to_user_swizzled(char __user * cpu_vaddr,const char * gpu_vaddr,int gpu_offset,int length)7437256b59bSFrançois Tigeot __copy_to_user_swizzled(char __user *cpu_vaddr,
7447256b59bSFrançois Tigeot 			const char *gpu_vaddr, int gpu_offset,
7457256b59bSFrançois Tigeot 			int length)
7467256b59bSFrançois Tigeot {
7477256b59bSFrançois Tigeot 	int ret, cpu_offset = 0;
7487256b59bSFrançois Tigeot 
7497256b59bSFrançois Tigeot 	while (length > 0) {
7507256b59bSFrançois Tigeot 		int cacheline_end = ALIGN(gpu_offset + 1, 64);
7517256b59bSFrançois Tigeot 		int this_length = min(cacheline_end - gpu_offset, length);
7527256b59bSFrançois Tigeot 		int swizzled_gpu_offset = gpu_offset ^ 64;
7537256b59bSFrançois Tigeot 
7547256b59bSFrançois Tigeot 		ret = __copy_to_user(cpu_vaddr + cpu_offset,
7557256b59bSFrançois Tigeot 				     gpu_vaddr + swizzled_gpu_offset,
7567256b59bSFrançois Tigeot 				     this_length);
7577256b59bSFrançois Tigeot 		if (ret)
7587256b59bSFrançois Tigeot 			return ret + length;
7597256b59bSFrançois Tigeot 
7607256b59bSFrançois Tigeot 		cpu_offset += this_length;
7617256b59bSFrançois Tigeot 		gpu_offset += this_length;
7627256b59bSFrançois Tigeot 		length -= this_length;
7637256b59bSFrançois Tigeot 	}
7647256b59bSFrançois Tigeot 
7657256b59bSFrançois Tigeot 	return 0;
7667256b59bSFrançois Tigeot }
7677256b59bSFrançois Tigeot 
7687256b59bSFrançois Tigeot static inline int
__copy_from_user_swizzled(char * gpu_vaddr,int gpu_offset,const char __user * cpu_vaddr,int length)7697256b59bSFrançois Tigeot __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
7707256b59bSFrançois Tigeot 			  const char __user *cpu_vaddr,
7717256b59bSFrançois Tigeot 			  int length)
7727256b59bSFrançois Tigeot {
7737256b59bSFrançois Tigeot 	int ret, cpu_offset = 0;
7747256b59bSFrançois Tigeot 
7757256b59bSFrançois Tigeot 	while (length > 0) {
7767256b59bSFrançois Tigeot 		int cacheline_end = ALIGN(gpu_offset + 1, 64);
7777256b59bSFrançois Tigeot 		int this_length = min(cacheline_end - gpu_offset, length);
7787256b59bSFrançois Tigeot 		int swizzled_gpu_offset = gpu_offset ^ 64;
7797256b59bSFrançois Tigeot 
7807256b59bSFrançois Tigeot 		ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
7817256b59bSFrançois Tigeot 				       cpu_vaddr + cpu_offset,
7827256b59bSFrançois Tigeot 				       this_length);
7837256b59bSFrançois Tigeot 		if (ret)
7847256b59bSFrançois Tigeot 			return ret + length;
7857256b59bSFrançois Tigeot 
7867256b59bSFrançois Tigeot 		cpu_offset += this_length;
7877256b59bSFrançois Tigeot 		gpu_offset += this_length;
7887256b59bSFrançois Tigeot 		length -= this_length;
7897256b59bSFrançois Tigeot 	}
7907256b59bSFrançois Tigeot 
7917256b59bSFrançois Tigeot 	return 0;
7927256b59bSFrançois Tigeot }
7937256b59bSFrançois Tigeot 
794ba55f2f5SFrançois Tigeot /*
795ba55f2f5SFrançois Tigeot  * Pins the specified object's pages and synchronizes the object with
796ba55f2f5SFrançois Tigeot  * GPU accesses. Sets needs_clflush to non-zero if the caller should
797ba55f2f5SFrançois Tigeot  * flush the object from the CPU cache.
798ba55f2f5SFrançois Tigeot  */
i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object * obj,unsigned int * needs_clflush)799ba55f2f5SFrançois Tigeot int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
8001e12ee3bSFrançois Tigeot 				    unsigned int *needs_clflush)
801ba55f2f5SFrançois Tigeot {
802ba55f2f5SFrançois Tigeot 	int ret;
803ba55f2f5SFrançois Tigeot 
8044be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
805ba55f2f5SFrançois Tigeot 
8064be47400SFrançois Tigeot 	*needs_clflush = 0;
8071e12ee3bSFrançois Tigeot 	if (!i915_gem_object_has_struct_page(obj))
8081e12ee3bSFrançois Tigeot 		return -ENODEV;
809ba55f2f5SFrançois Tigeot 
8104be47400SFrançois Tigeot 	ret = i915_gem_object_wait(obj,
8114be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE |
8124be47400SFrançois Tigeot 				   I915_WAIT_LOCKED,
8134be47400SFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
8144be47400SFrançois Tigeot 				   NULL);
81587df8fc6SFrançois Tigeot 	if (ret)
81687df8fc6SFrançois Tigeot 		return ret;
81787df8fc6SFrançois Tigeot 
8184be47400SFrançois Tigeot 	ret = i915_gem_object_pin_pages(obj);
819ba55f2f5SFrançois Tigeot 	if (ret)
820ba55f2f5SFrançois Tigeot 		return ret;
821ba55f2f5SFrançois Tigeot 
8223f2dd94aSFrançois Tigeot 	if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
823a85cb24fSFrançois Tigeot 	    !static_cpu_has(X86_FEATURE_CLFLUSH)) {
824a85cb24fSFrançois Tigeot 		ret = i915_gem_object_set_to_cpu_domain(obj, false);
825a85cb24fSFrançois Tigeot 		if (ret)
826a85cb24fSFrançois Tigeot 			goto err_unpin;
827a85cb24fSFrançois Tigeot 		else
828a85cb24fSFrançois Tigeot 			goto out;
829a85cb24fSFrançois Tigeot 	}
830a85cb24fSFrançois Tigeot 
8313f2dd94aSFrançois Tigeot 	flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
8321e12ee3bSFrançois Tigeot 
8331e12ee3bSFrançois Tigeot 	/* If we're not in the cpu read domain, set ourself into the gtt
8341e12ee3bSFrançois Tigeot 	 * read domain and manually flush cachelines (if required). This
8351e12ee3bSFrançois Tigeot 	 * optimizes for the case when the gpu will dirty the data
8361e12ee3bSFrançois Tigeot 	 * anyway again before the next pread happens.
8371e12ee3bSFrançois Tigeot 	 */
8383f2dd94aSFrançois Tigeot 	if (!obj->cache_dirty &&
8393f2dd94aSFrançois Tigeot 	    !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
840a85cb24fSFrançois Tigeot 		*needs_clflush = CLFLUSH_BEFORE;
8411e12ee3bSFrançois Tigeot 
842a85cb24fSFrançois Tigeot out:
8431e12ee3bSFrançois Tigeot 	/* return with the pages pinned */
8441e12ee3bSFrançois Tigeot 	return 0;
8451e12ee3bSFrançois Tigeot 
8461e12ee3bSFrançois Tigeot err_unpin:
8471e12ee3bSFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
8481e12ee3bSFrançois Tigeot 	return ret;
8491e12ee3bSFrançois Tigeot }
8501e12ee3bSFrançois Tigeot 
i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object * obj,unsigned int * needs_clflush)8511e12ee3bSFrançois Tigeot int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
8521e12ee3bSFrançois Tigeot 				     unsigned int *needs_clflush)
8531e12ee3bSFrançois Tigeot {
8541e12ee3bSFrançois Tigeot 	int ret;
8551e12ee3bSFrançois Tigeot 
8564be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
8574be47400SFrançois Tigeot 
8581e12ee3bSFrançois Tigeot 	*needs_clflush = 0;
8591e12ee3bSFrançois Tigeot 	if (!i915_gem_object_has_struct_page(obj))
8601e12ee3bSFrançois Tigeot 		return -ENODEV;
8611e12ee3bSFrançois Tigeot 
8624be47400SFrançois Tigeot 	ret = i915_gem_object_wait(obj,
8634be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE |
8644be47400SFrançois Tigeot 				   I915_WAIT_LOCKED |
8654be47400SFrançois Tigeot 				   I915_WAIT_ALL,
8664be47400SFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
8674be47400SFrançois Tigeot 				   NULL);
8681e12ee3bSFrançois Tigeot 	if (ret)
8691e12ee3bSFrançois Tigeot 		return ret;
8701e12ee3bSFrançois Tigeot 
8714be47400SFrançois Tigeot 	ret = i915_gem_object_pin_pages(obj);
8721e12ee3bSFrançois Tigeot 	if (ret)
8731e12ee3bSFrançois Tigeot 		return ret;
8741e12ee3bSFrançois Tigeot 
8753f2dd94aSFrançois Tigeot 	if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
876a85cb24fSFrançois Tigeot 	    !static_cpu_has(X86_FEATURE_CLFLUSH)) {
877a85cb24fSFrançois Tigeot 		ret = i915_gem_object_set_to_cpu_domain(obj, true);
878a85cb24fSFrançois Tigeot 		if (ret)
879a85cb24fSFrançois Tigeot 			goto err_unpin;
880a85cb24fSFrançois Tigeot 		else
881a85cb24fSFrançois Tigeot 			goto out;
882a85cb24fSFrançois Tigeot 	}
883a85cb24fSFrançois Tigeot 
8843f2dd94aSFrançois Tigeot 	flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
8851e12ee3bSFrançois Tigeot 
8861e12ee3bSFrançois Tigeot 	/* If we're not in the cpu write domain, set ourself into the
8871e12ee3bSFrançois Tigeot 	 * gtt write domain and manually flush cachelines (as required).
8881e12ee3bSFrançois Tigeot 	 * This optimizes for the case when the gpu will use the data
8891e12ee3bSFrançois Tigeot 	 * right away and we therefore have to clflush anyway.
8901e12ee3bSFrançois Tigeot 	 */
8913f2dd94aSFrançois Tigeot 	if (!obj->cache_dirty) {
892a85cb24fSFrançois Tigeot 		*needs_clflush |= CLFLUSH_AFTER;
8931e12ee3bSFrançois Tigeot 
8943f2dd94aSFrançois Tigeot 		/*
8953f2dd94aSFrançois Tigeot 		 * Same trick applies to invalidate partially written
8963f2dd94aSFrançois Tigeot 		 * cachelines read before writing.
8971e12ee3bSFrançois Tigeot 		 */
8981e12ee3bSFrançois Tigeot 		if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
899a85cb24fSFrançois Tigeot 			*needs_clflush |= CLFLUSH_BEFORE;
9003f2dd94aSFrançois Tigeot 	}
9011e12ee3bSFrançois Tigeot 
902a85cb24fSFrançois Tigeot out:
9031e12ee3bSFrançois Tigeot 	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
9044be47400SFrançois Tigeot 	obj->mm.dirty = true;
9051e12ee3bSFrançois Tigeot 	/* return with the pages pinned */
9061e12ee3bSFrançois Tigeot 	return 0;
9071e12ee3bSFrançois Tigeot 
9081e12ee3bSFrançois Tigeot err_unpin:
9091e12ee3bSFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
910ba55f2f5SFrançois Tigeot 	return ret;
911ba55f2f5SFrançois Tigeot }
912ba55f2f5SFrançois Tigeot 
9137256b59bSFrançois Tigeot static void
shmem_clflush_swizzled_range(char * addr,unsigned long length,bool swizzled)9147256b59bSFrançois Tigeot shmem_clflush_swizzled_range(char *addr, unsigned long length,
9157256b59bSFrançois Tigeot 			     bool swizzled)
9167256b59bSFrançois Tigeot {
9177256b59bSFrançois Tigeot 	if (unlikely(swizzled)) {
9187256b59bSFrançois Tigeot 		unsigned long start = (unsigned long) addr;
9197256b59bSFrançois Tigeot 		unsigned long end = (unsigned long) addr + length;
9207256b59bSFrançois Tigeot 
9217256b59bSFrançois Tigeot 		/* For swizzling simply ensure that we always flush both
9227256b59bSFrançois Tigeot 		 * channels. Lame, but simple and it works. Swizzled
9237256b59bSFrançois Tigeot 		 * pwrite/pread is far from a hotpath - current userspace
9247256b59bSFrançois Tigeot 		 * doesn't use it at all. */
9257256b59bSFrançois Tigeot 		start = round_down(start, 128);
9267256b59bSFrançois Tigeot 		end = round_up(end, 128);
9277256b59bSFrançois Tigeot 
9287256b59bSFrançois Tigeot 		drm_clflush_virt_range((void *)start, end - start);
9297256b59bSFrançois Tigeot 	} else {
9307256b59bSFrançois Tigeot 		drm_clflush_virt_range(addr, length);
9317256b59bSFrançois Tigeot 	}
9327256b59bSFrançois Tigeot 
9337256b59bSFrançois Tigeot }
9347256b59bSFrançois Tigeot 
9357256b59bSFrançois Tigeot /* Only difference to the fast-path function is that this can handle bit17
9367256b59bSFrançois Tigeot  * and uses non-atomic copy and kmap functions. */
9377256b59bSFrançois Tigeot static int
shmem_pread_slow(struct page * page,int offset,int length,char __user * user_data,bool page_do_bit17_swizzling,bool needs_clflush)9384be47400SFrançois Tigeot shmem_pread_slow(struct page *page, int offset, int length,
9397256b59bSFrançois Tigeot 		 char __user *user_data,
9407256b59bSFrançois Tigeot 		 bool page_do_bit17_swizzling, bool needs_clflush)
9417256b59bSFrançois Tigeot {
9427256b59bSFrançois Tigeot 	char *vaddr;
9437256b59bSFrançois Tigeot 	int ret;
9447256b59bSFrançois Tigeot 
9457256b59bSFrançois Tigeot 	vaddr = kmap(page);
9467256b59bSFrançois Tigeot 	if (needs_clflush)
9474be47400SFrançois Tigeot 		shmem_clflush_swizzled_range(vaddr + offset, length,
9487256b59bSFrançois Tigeot 					     page_do_bit17_swizzling);
9497256b59bSFrançois Tigeot 
9507256b59bSFrançois Tigeot 	if (page_do_bit17_swizzling)
9514be47400SFrançois Tigeot 		ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
9527256b59bSFrançois Tigeot 	else
9534be47400SFrançois Tigeot 		ret = __copy_to_user(user_data, vaddr + offset, length);
9547256b59bSFrançois Tigeot 	kunmap(page);
9557256b59bSFrançois Tigeot 
9567256b59bSFrançois Tigeot 	return ret ? - EFAULT : 0;
9577256b59bSFrançois Tigeot }
9587256b59bSFrançois Tigeot 
9594be47400SFrançois Tigeot static int
shmem_pread(struct page * page,int offset,int length,char __user * user_data,bool page_do_bit17_swizzling,bool needs_clflush)9604be47400SFrançois Tigeot shmem_pread(struct page *page, int offset, int length, char __user *user_data,
9614be47400SFrançois Tigeot 	    bool page_do_bit17_swizzling, bool needs_clflush)
9621487f786SFrançois Tigeot {
9634be47400SFrançois Tigeot 	int ret;
9644be47400SFrançois Tigeot 
9654be47400SFrançois Tigeot 	ret = -ENODEV;
9664be47400SFrançois Tigeot 	if (!page_do_bit17_swizzling) {
9674be47400SFrançois Tigeot 		char *vaddr = kmap_atomic(page);
9684be47400SFrançois Tigeot 
9694be47400SFrançois Tigeot 		if (needs_clflush)
9704be47400SFrançois Tigeot 			drm_clflush_virt_range(vaddr + offset, length);
9714be47400SFrançois Tigeot 		ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
9724be47400SFrançois Tigeot 		kunmap_atomic(vaddr);
9734be47400SFrançois Tigeot 	}
9744be47400SFrançois Tigeot 	if (ret == 0)
9754be47400SFrançois Tigeot 		return 0;
9764be47400SFrançois Tigeot 
9774be47400SFrançois Tigeot 	return shmem_pread_slow(page, offset, length, user_data,
9784be47400SFrançois Tigeot 				page_do_bit17_swizzling, needs_clflush);
9794be47400SFrançois Tigeot }
9804be47400SFrançois Tigeot 
9814be47400SFrançois Tigeot static int
i915_gem_shmem_pread(struct drm_i915_gem_object * obj,struct drm_i915_gem_pread * args)9824be47400SFrançois Tigeot i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
9834be47400SFrançois Tigeot 		     struct drm_i915_gem_pread *args)
9844be47400SFrançois Tigeot {
9854be47400SFrançois Tigeot 	char __user *user_data;
9864be47400SFrançois Tigeot 	u64 remain;
9874be47400SFrançois Tigeot 	unsigned int obj_do_bit17_swizzling;
9884be47400SFrançois Tigeot 	unsigned int needs_clflush;
9894be47400SFrançois Tigeot 	unsigned int idx, offset;
9904be47400SFrançois Tigeot 	int ret;
9914be47400SFrançois Tigeot 
9924be47400SFrançois Tigeot 	obj_do_bit17_swizzling = 0;
9934be47400SFrançois Tigeot 	if (i915_gem_object_needs_bit17_swizzle(obj))
9944be47400SFrançois Tigeot 		obj_do_bit17_swizzling = BIT(17);
9954be47400SFrançois Tigeot 
9964be47400SFrançois Tigeot 	ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
9974be47400SFrançois Tigeot 	if (ret)
9984be47400SFrançois Tigeot 		return ret;
9994be47400SFrançois Tigeot 
10004be47400SFrançois Tigeot 	ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
10014be47400SFrançois Tigeot 	mutex_unlock(&obj->base.dev->struct_mutex);
10024be47400SFrançois Tigeot 	if (ret)
10034be47400SFrançois Tigeot 		return ret;
10044be47400SFrançois Tigeot 
10054be47400SFrançois Tigeot 	remain = args->size;
10064be47400SFrançois Tigeot 	user_data = u64_to_user_ptr(args->data_ptr);
10074be47400SFrançois Tigeot 	offset = offset_in_page(args->offset);
10084be47400SFrançois Tigeot 	for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
10094be47400SFrançois Tigeot 		struct page *page = i915_gem_object_get_page(obj, idx);
10104be47400SFrançois Tigeot 		int length;
10114be47400SFrançois Tigeot 
10124be47400SFrançois Tigeot 		length = remain;
10134be47400SFrançois Tigeot 		if (offset + length > PAGE_SIZE)
10144be47400SFrançois Tigeot 			length = PAGE_SIZE - offset;
10154be47400SFrançois Tigeot 
10164be47400SFrançois Tigeot 		ret = shmem_pread(page, offset, length, user_data,
10174be47400SFrançois Tigeot 				  page_to_phys(page) & obj_do_bit17_swizzling,
10184be47400SFrançois Tigeot 				  needs_clflush);
10194be47400SFrançois Tigeot 		if (ret)
10204be47400SFrançois Tigeot 			break;
10214be47400SFrançois Tigeot 
10224be47400SFrançois Tigeot 		remain -= length;
10234be47400SFrançois Tigeot 		user_data += length;
10244be47400SFrançois Tigeot 		offset = 0;
10254be47400SFrançois Tigeot 	}
10264be47400SFrançois Tigeot 
10274be47400SFrançois Tigeot 	i915_gem_obj_finish_shmem_access(obj);
10284be47400SFrançois Tigeot 	return ret;
10294be47400SFrançois Tigeot }
10304be47400SFrançois Tigeot 
10314be47400SFrançois Tigeot static inline bool
gtt_user_read(struct io_mapping * mapping,loff_t base,int offset,char __user * user_data,int length)10324be47400SFrançois Tigeot gtt_user_read(struct io_mapping *mapping,
10334be47400SFrançois Tigeot 	      loff_t base, int offset,
10344be47400SFrançois Tigeot 	      char __user *user_data, int length)
10354be47400SFrançois Tigeot {
10363f2dd94aSFrançois Tigeot 	void __iomem *vaddr;
10374be47400SFrançois Tigeot 	unsigned long unwritten;
10381487f786SFrançois Tigeot 
10391487f786SFrançois Tigeot 	/* We can use the cpu mem copy function because this is X86. */
10403f2dd94aSFrançois Tigeot 	vaddr = io_mapping_map_atomic_wc(mapping, base);
10413f2dd94aSFrançois Tigeot 	unwritten = __copy_to_user_inatomic(user_data,
10423f2dd94aSFrançois Tigeot 					    (void __force *)vaddr + offset,
10433f2dd94aSFrançois Tigeot 					    length);
10444be47400SFrançois Tigeot 	io_mapping_unmap_atomic(vaddr);
10454be47400SFrançois Tigeot 	if (unwritten) {
10463f2dd94aSFrançois Tigeot 		vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
10473f2dd94aSFrançois Tigeot 		unwritten = copy_to_user(user_data,
10483f2dd94aSFrançois Tigeot 					 (void __force *)vaddr + offset,
10493f2dd94aSFrançois Tigeot 					 length);
10504be47400SFrançois Tigeot 		io_mapping_unmap(vaddr);
10514be47400SFrançois Tigeot 	}
10521487f786SFrançois Tigeot 	return unwritten;
10531487f786SFrançois Tigeot }
10541487f786SFrançois Tigeot 
10551487f786SFrançois Tigeot static int
i915_gem_gtt_pread(struct drm_i915_gem_object * obj,const struct drm_i915_gem_pread * args)10564be47400SFrançois Tigeot i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
10574be47400SFrançois Tigeot 		   const struct drm_i915_gem_pread *args)
10581487f786SFrançois Tigeot {
10594be47400SFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
10604be47400SFrançois Tigeot 	struct i915_ggtt *ggtt = &i915->ggtt;
10611487f786SFrançois Tigeot 	struct drm_mm_node node;
10624be47400SFrançois Tigeot 	struct i915_vma *vma;
10634be47400SFrançois Tigeot 	void __user *user_data;
10644be47400SFrançois Tigeot 	u64 remain, offset;
10651487f786SFrançois Tigeot 	int ret;
10661487f786SFrançois Tigeot 
10674be47400SFrançois Tigeot 	ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
10684be47400SFrançois Tigeot 	if (ret)
10694be47400SFrançois Tigeot 		return ret;
10704be47400SFrançois Tigeot 
10714be47400SFrançois Tigeot 	intel_runtime_pm_get(i915);
10724be47400SFrançois Tigeot 	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
10733f2dd94aSFrançois Tigeot 				       PIN_MAPPABLE |
10743f2dd94aSFrançois Tigeot 				       PIN_NONFAULT |
10753f2dd94aSFrançois Tigeot 				       PIN_NONBLOCK);
10761e12ee3bSFrançois Tigeot 	if (!IS_ERR(vma)) {
10771e12ee3bSFrançois Tigeot 		node.start = i915_ggtt_offset(vma);
10781e12ee3bSFrançois Tigeot 		node.allocated = false;
10791e12ee3bSFrançois Tigeot 		ret = i915_vma_put_fence(vma);
10801487f786SFrançois Tigeot 		if (ret) {
10811e12ee3bSFrançois Tigeot 			i915_vma_unpin(vma);
10821e12ee3bSFrançois Tigeot 			vma = ERR_PTR(ret);
10831e12ee3bSFrançois Tigeot 		}
10841e12ee3bSFrançois Tigeot 	}
10851e12ee3bSFrançois Tigeot 	if (IS_ERR(vma)) {
10864be47400SFrançois Tigeot 		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
10871487f786SFrançois Tigeot 		if (ret)
10884be47400SFrançois Tigeot 			goto out_unlock;
10894be47400SFrançois Tigeot 		GEM_BUG_ON(!node.allocated);
10901487f786SFrançois Tigeot 	}
10911487f786SFrançois Tigeot 
10921487f786SFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, false);
10931487f786SFrançois Tigeot 	if (ret)
10941487f786SFrançois Tigeot 		goto out_unpin;
10951487f786SFrançois Tigeot 
10964be47400SFrançois Tigeot 	mutex_unlock(&i915->drm.struct_mutex);
10971487f786SFrançois Tigeot 
10984be47400SFrançois Tigeot 	user_data = u64_to_user_ptr(args->data_ptr);
10994be47400SFrançois Tigeot 	remain = args->size;
11004be47400SFrançois Tigeot 	offset = args->offset;
11011487f786SFrançois Tigeot 
11021487f786SFrançois Tigeot 	while (remain > 0) {
11031487f786SFrançois Tigeot 		/* Operation in this page
11041487f786SFrançois Tigeot 		 *
11051487f786SFrançois Tigeot 		 * page_base = page offset within aperture
11061487f786SFrançois Tigeot 		 * page_offset = offset within page
11071487f786SFrançois Tigeot 		 * page_length = bytes to copy for this page
11081487f786SFrançois Tigeot 		 */
11091487f786SFrançois Tigeot 		u32 page_base = node.start;
11101487f786SFrançois Tigeot 		unsigned page_offset = offset_in_page(offset);
11111487f786SFrançois Tigeot 		unsigned page_length = PAGE_SIZE - page_offset;
11121487f786SFrançois Tigeot 		page_length = remain < page_length ? remain : page_length;
11131487f786SFrançois Tigeot 		if (node.allocated) {
11141487f786SFrançois Tigeot 			wmb();
11151487f786SFrançois Tigeot 			ggtt->base.insert_page(&ggtt->base,
11161487f786SFrançois Tigeot 					       i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
11174be47400SFrançois Tigeot 					       node.start, I915_CACHE_NONE, 0);
11181487f786SFrançois Tigeot 			wmb();
11191487f786SFrançois Tigeot 		} else {
11201e12ee3bSFrançois Tigeot 			page_base += offset & LINUX_PAGE_MASK;
11211487f786SFrançois Tigeot 		}
11224be47400SFrançois Tigeot 
11234be47400SFrançois Tigeot 		if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
11244be47400SFrançois Tigeot 				  user_data, page_length)) {
11251487f786SFrançois Tigeot 			ret = -EFAULT;
11261487f786SFrançois Tigeot 			break;
11271487f786SFrançois Tigeot 		}
11281487f786SFrançois Tigeot 
11291487f786SFrançois Tigeot 		remain -= page_length;
11301487f786SFrançois Tigeot 		user_data += page_length;
11311487f786SFrançois Tigeot 		offset += page_length;
11321487f786SFrançois Tigeot 	}
11331487f786SFrançois Tigeot 
11344be47400SFrançois Tigeot 	mutex_lock(&i915->drm.struct_mutex);
11351487f786SFrançois Tigeot out_unpin:
11361487f786SFrançois Tigeot 	if (node.allocated) {
11371487f786SFrançois Tigeot 		wmb();
11381487f786SFrançois Tigeot 		ggtt->base.clear_range(&ggtt->base,
11391e12ee3bSFrançois Tigeot 				       node.start, node.size);
11401487f786SFrançois Tigeot 		remove_mappable_node(&node);
11411487f786SFrançois Tigeot 	} else {
11421e12ee3bSFrançois Tigeot 		i915_vma_unpin(vma);
11431487f786SFrançois Tigeot 	}
11444be47400SFrançois Tigeot out_unlock:
11454be47400SFrançois Tigeot 	intel_runtime_pm_put(i915);
11464be47400SFrançois Tigeot 	mutex_unlock(&i915->drm.struct_mutex);
11477256b59bSFrançois Tigeot 
11487256b59bSFrançois Tigeot 	return ret;
1149d1c259eeSFrançois Tigeot }
1150d1c259eeSFrançois Tigeot 
1151e11a51e3SFrançois Tigeot /**
1152e11a51e3SFrançois Tigeot  * Reads data from the object referenced by handle.
11531487f786SFrançois Tigeot  * @dev: drm device pointer
11541487f786SFrançois Tigeot  * @data: ioctl data blob
11551487f786SFrançois Tigeot  * @file: drm file pointer
1156e11a51e3SFrançois Tigeot  *
1157e11a51e3SFrançois Tigeot  * On error, the contents of *data are undefined.
1158e11a51e3SFrançois Tigeot  */
1159e3adcf8fSFrançois Tigeot int
i915_gem_pread_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1160e11a51e3SFrançois Tigeot i915_gem_pread_ioctl(struct drm_device *dev, void *data,
1161e11a51e3SFrançois Tigeot 		     struct drm_file *file)
1162e3adcf8fSFrançois Tigeot {
1163d2557f23SFrançois Tigeot 	struct drm_i915_gem_pread *args = data;
1164d1c259eeSFrançois Tigeot 	struct drm_i915_gem_object *obj;
11654be47400SFrançois Tigeot 	int ret;
1166e3adcf8fSFrançois Tigeot 
1167d1c259eeSFrançois Tigeot 	if (args->size == 0)
1168d1c259eeSFrançois Tigeot 		return 0;
1169d1c259eeSFrançois Tigeot 
11708621f407SFrançois Tigeot #if 0
11718621f407SFrançois Tigeot 	if (!access_ok(VERIFY_WRITE,
11728621f407SFrançois Tigeot 		       u64_to_user_ptr(args->data_ptr),
11738621f407SFrançois Tigeot 		       args->size))
11748621f407SFrançois Tigeot 		return -EFAULT;
11758621f407SFrançois Tigeot #endif
11768621f407SFrançois Tigeot 
117787df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file, args->handle);
117871f41f3eSFrançois Tigeot 	if (!obj)
117971f41f3eSFrançois Tigeot 		return -ENOENT;
1180d1c259eeSFrançois Tigeot 
1181d1c259eeSFrançois Tigeot 	/* Bounds check source.  */
1182a85cb24fSFrançois Tigeot 	if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
1183d1c259eeSFrançois Tigeot 		ret = -EINVAL;
11844be47400SFrançois Tigeot 		goto out;
1185d1c259eeSFrançois Tigeot 	}
1186d1c259eeSFrançois Tigeot 
1187b15f8132SFrançois Tigeot 	trace_i915_gem_object_pread(obj, args->offset, args->size);
1188b15f8132SFrançois Tigeot 
11894be47400SFrançois Tigeot 	ret = i915_gem_object_wait(obj,
11904be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE,
11914be47400SFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
11924be47400SFrançois Tigeot 				   to_rps_client(file));
119371f41f3eSFrançois Tigeot 	if (ret)
11944be47400SFrançois Tigeot 		goto out;
119571f41f3eSFrançois Tigeot 
11964be47400SFrançois Tigeot 	ret = i915_gem_object_pin_pages(obj);
119771f41f3eSFrançois Tigeot 	if (ret)
11984be47400SFrançois Tigeot 		goto out;
119971f41f3eSFrançois Tigeot 
12004be47400SFrançois Tigeot 	ret = i915_gem_shmem_pread(obj, args);
12014be47400SFrançois Tigeot 	if (ret == -EFAULT || ret == -ENODEV)
12024be47400SFrançois Tigeot 		ret = i915_gem_gtt_pread(obj, args);
1203ba023890SFrançois Tigeot 
12044be47400SFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
12054be47400SFrançois Tigeot out:
120687df8fc6SFrançois Tigeot 	i915_gem_object_put(obj);
1207d1c259eeSFrançois Tigeot 	return ret;
1208d1c259eeSFrançois Tigeot }
1209d1c259eeSFrançois Tigeot 
1210a2fdbec6SFrançois Tigeot /* This is the fast write path which cannot handle
1211a2fdbec6SFrançois Tigeot  * page faults in the source data
1212a2fdbec6SFrançois Tigeot  */
1213a2fdbec6SFrançois Tigeot 
12144be47400SFrançois Tigeot static inline bool
ggtt_write(struct io_mapping * mapping,loff_t base,int offset,char __user * user_data,int length)12154be47400SFrançois Tigeot ggtt_write(struct io_mapping *mapping,
12164be47400SFrançois Tigeot 	   loff_t base, int offset,
12174be47400SFrançois Tigeot 	   char __user *user_data, int length)
1218a2fdbec6SFrançois Tigeot {
12193f2dd94aSFrançois Tigeot 	void __iomem *vaddr;
1220a2fdbec6SFrançois Tigeot 	unsigned long unwritten;
1221a2fdbec6SFrançois Tigeot 
1222a2fdbec6SFrançois Tigeot 	/* We can use the cpu mem copy function because this is X86. */
12233f2dd94aSFrançois Tigeot 	vaddr = io_mapping_map_atomic_wc(mapping, base);
12243f2dd94aSFrançois Tigeot 	unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
1225a2fdbec6SFrançois Tigeot 						      user_data, length);
12264be47400SFrançois Tigeot 	io_mapping_unmap_atomic(vaddr);
12274be47400SFrançois Tigeot 	if (unwritten) {
12283f2dd94aSFrançois Tigeot 		vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
12293f2dd94aSFrançois Tigeot 		unwritten = copy_from_user((void __force *)vaddr + offset,
12303f2dd94aSFrançois Tigeot 					   user_data, length);
12314be47400SFrançois Tigeot 		io_mapping_unmap(vaddr);
12324be47400SFrançois Tigeot 	}
12334be47400SFrançois Tigeot 
1234a2fdbec6SFrançois Tigeot 	return unwritten;
1235a2fdbec6SFrançois Tigeot }
1236d1c259eeSFrançois Tigeot 
12379edbd4a0SFrançois Tigeot /**
12389edbd4a0SFrançois Tigeot  * This is the fast pwrite path, where we copy the data directly from the
12399edbd4a0SFrançois Tigeot  * user into the GTT, uncached.
12404be47400SFrançois Tigeot  * @obj: i915 GEM object
12411487f786SFrançois Tigeot  * @args: pwrite arguments structure
12429edbd4a0SFrançois Tigeot  */
12439edbd4a0SFrançois Tigeot static int
i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object * obj,const struct drm_i915_gem_pwrite * args)12444be47400SFrançois Tigeot i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
12454be47400SFrançois Tigeot 			 const struct drm_i915_gem_pwrite *args)
12469edbd4a0SFrançois Tigeot {
12474be47400SFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
12481487f786SFrançois Tigeot 	struct i915_ggtt *ggtt = &i915->ggtt;
12491487f786SFrançois Tigeot 	struct drm_mm_node node;
12504be47400SFrançois Tigeot 	struct i915_vma *vma;
12514be47400SFrançois Tigeot 	u64 remain, offset;
12524be47400SFrançois Tigeot 	void __user *user_data;
12531487f786SFrançois Tigeot 	int ret;
12541487f786SFrançois Tigeot 
12554be47400SFrançois Tigeot 	ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
12564be47400SFrançois Tigeot 	if (ret)
12574be47400SFrançois Tigeot 		return ret;
12589edbd4a0SFrançois Tigeot 
12593f2dd94aSFrançois Tigeot 	if (i915_gem_object_has_struct_page(obj)) {
12603f2dd94aSFrançois Tigeot 		/*
12613f2dd94aSFrançois Tigeot 		 * Avoid waking the device up if we can fallback, as
12623f2dd94aSFrançois Tigeot 		 * waking/resuming is very slow (worst-case 10-100 ms
12633f2dd94aSFrançois Tigeot 		 * depending on PCI sleeps and our own resume time).
12643f2dd94aSFrançois Tigeot 		 * This easily dwarfs any performance advantage from
12653f2dd94aSFrançois Tigeot 		 * using the cache bypass of indirect GGTT access.
12663f2dd94aSFrançois Tigeot 		 */
12673f2dd94aSFrançois Tigeot 		if (!intel_runtime_pm_get_if_in_use(i915)) {
12683f2dd94aSFrançois Tigeot 			ret = -EFAULT;
12693f2dd94aSFrançois Tigeot 			goto out_unlock;
12703f2dd94aSFrançois Tigeot 		}
12713f2dd94aSFrançois Tigeot 	} else {
12723f2dd94aSFrançois Tigeot 		/* No backing pages, no fallback, we must force GGTT access */
12734be47400SFrançois Tigeot 		intel_runtime_pm_get(i915);
12743f2dd94aSFrançois Tigeot 	}
12753f2dd94aSFrançois Tigeot 
12761e12ee3bSFrançois Tigeot 	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
12773f2dd94aSFrançois Tigeot 				       PIN_MAPPABLE |
12783f2dd94aSFrançois Tigeot 				       PIN_NONFAULT |
12793f2dd94aSFrançois Tigeot 				       PIN_NONBLOCK);
12801e12ee3bSFrançois Tigeot 	if (!IS_ERR(vma)) {
12811e12ee3bSFrançois Tigeot 		node.start = i915_ggtt_offset(vma);
12821e12ee3bSFrançois Tigeot 		node.allocated = false;
12831e12ee3bSFrançois Tigeot 		ret = i915_vma_put_fence(vma);
12841487f786SFrançois Tigeot 		if (ret) {
12851e12ee3bSFrançois Tigeot 			i915_vma_unpin(vma);
12861e12ee3bSFrançois Tigeot 			vma = ERR_PTR(ret);
12871e12ee3bSFrançois Tigeot 		}
12881e12ee3bSFrançois Tigeot 	}
12891e12ee3bSFrançois Tigeot 	if (IS_ERR(vma)) {
12904be47400SFrançois Tigeot 		ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
12919edbd4a0SFrançois Tigeot 		if (ret)
12923f2dd94aSFrançois Tigeot 			goto out_rpm;
12934be47400SFrançois Tigeot 		GEM_BUG_ON(!node.allocated);
12941487f786SFrançois Tigeot 	}
12951487f786SFrançois Tigeot 
12969edbd4a0SFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
12979edbd4a0SFrançois Tigeot 	if (ret)
12989edbd4a0SFrançois Tigeot 		goto out_unpin;
12999edbd4a0SFrançois Tigeot 
13004be47400SFrançois Tigeot 	mutex_unlock(&i915->drm.struct_mutex);
13014be47400SFrançois Tigeot 
13021e12ee3bSFrançois Tigeot 	intel_fb_obj_invalidate(obj, ORIGIN_CPU);
13039edbd4a0SFrançois Tigeot 
13048621f407SFrançois Tigeot 	user_data = u64_to_user_ptr(args->data_ptr);
13051487f786SFrançois Tigeot 	offset = args->offset;
13069edbd4a0SFrançois Tigeot 	remain = args->size;
13071487f786SFrançois Tigeot 	while (remain) {
13089edbd4a0SFrançois Tigeot 		/* Operation in this page
13099edbd4a0SFrançois Tigeot 		 *
13109edbd4a0SFrançois Tigeot 		 * page_base = page offset within aperture
13119edbd4a0SFrançois Tigeot 		 * page_offset = offset within page
13129edbd4a0SFrançois Tigeot 		 * page_length = bytes to copy for this page
13139edbd4a0SFrançois Tigeot 		 */
13141487f786SFrançois Tigeot 		u32 page_base = node.start;
13154be47400SFrançois Tigeot 		unsigned int page_offset = offset_in_page(offset);
13164be47400SFrançois Tigeot 		unsigned int page_length = PAGE_SIZE - page_offset;
13171487f786SFrançois Tigeot 		page_length = remain < page_length ? remain : page_length;
13181487f786SFrançois Tigeot 		if (node.allocated) {
13191487f786SFrançois Tigeot 			wmb(); /* flush the write before we modify the GGTT */
13201487f786SFrançois Tigeot 			ggtt->base.insert_page(&ggtt->base,
13211487f786SFrançois Tigeot 					       i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
13221487f786SFrançois Tigeot 					       node.start, I915_CACHE_NONE, 0);
13231487f786SFrançois Tigeot 			wmb(); /* flush modifications to the GGTT (insert_page) */
13241487f786SFrançois Tigeot 		} else {
13251487f786SFrançois Tigeot 			page_base += offset & LINUX_PAGE_MASK;
13261487f786SFrançois Tigeot 		}
13279edbd4a0SFrançois Tigeot 		/* If we get a fault while copying data, then (presumably) our
13289edbd4a0SFrançois Tigeot 		 * source page isn't available.  Return the error and we'll
13299edbd4a0SFrançois Tigeot 		 * retry in the slow path.
13301487f786SFrançois Tigeot 		 * If the object is non-shmem backed, we retry again with the
13311487f786SFrançois Tigeot 		 * path that handles page fault.
13329edbd4a0SFrançois Tigeot 		 */
13334be47400SFrançois Tigeot 		if (ggtt_write(&ggtt->mappable, page_base, page_offset,
13344be47400SFrançois Tigeot 			       user_data, page_length)) {
13359edbd4a0SFrançois Tigeot 			ret = -EFAULT;
13364be47400SFrançois Tigeot 			break;
13371487f786SFrançois Tigeot 		}
13381487f786SFrançois Tigeot 
13399edbd4a0SFrançois Tigeot 		remain -= page_length;
13409edbd4a0SFrançois Tigeot 		user_data += page_length;
13419edbd4a0SFrançois Tigeot 		offset += page_length;
13429edbd4a0SFrançois Tigeot 	}
1343a85cb24fSFrançois Tigeot 	intel_fb_obj_flush(obj, ORIGIN_CPU);
13444be47400SFrançois Tigeot 
13454be47400SFrançois Tigeot 	mutex_lock(&i915->drm.struct_mutex);
13469edbd4a0SFrançois Tigeot out_unpin:
13471487f786SFrançois Tigeot 	if (node.allocated) {
13481487f786SFrançois Tigeot 		wmb();
13491487f786SFrançois Tigeot 		ggtt->base.clear_range(&ggtt->base,
13501e12ee3bSFrançois Tigeot 				       node.start, node.size);
13511487f786SFrançois Tigeot 		remove_mappable_node(&node);
13521487f786SFrançois Tigeot 	} else {
13531e12ee3bSFrançois Tigeot 		i915_vma_unpin(vma);
13541487f786SFrançois Tigeot 	}
13553f2dd94aSFrançois Tigeot out_rpm:
13564be47400SFrançois Tigeot 	intel_runtime_pm_put(i915);
13573f2dd94aSFrançois Tigeot out_unlock:
13584be47400SFrançois Tigeot 	mutex_unlock(&i915->drm.struct_mutex);
13599edbd4a0SFrançois Tigeot 	return ret;
13609edbd4a0SFrançois Tigeot }
13619edbd4a0SFrançois Tigeot 
13627256b59bSFrançois Tigeot static int
shmem_pwrite_slow(struct page * page,int offset,int length,char __user * user_data,bool page_do_bit17_swizzling,bool needs_clflush_before,bool needs_clflush_after)13634be47400SFrançois Tigeot shmem_pwrite_slow(struct page *page, int offset, int length,
13647256b59bSFrançois Tigeot 		  char __user *user_data,
13657256b59bSFrançois Tigeot 		  bool page_do_bit17_swizzling,
13667256b59bSFrançois Tigeot 		  bool needs_clflush_before,
13677256b59bSFrançois Tigeot 		  bool needs_clflush_after)
13687256b59bSFrançois Tigeot {
13697256b59bSFrançois Tigeot 	char *vaddr;
13707256b59bSFrançois Tigeot 	int ret;
13717256b59bSFrançois Tigeot 
13727256b59bSFrançois Tigeot 	vaddr = kmap(page);
13737256b59bSFrançois Tigeot 	if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
13744be47400SFrançois Tigeot 		shmem_clflush_swizzled_range(vaddr + offset, length,
13757256b59bSFrançois Tigeot 					     page_do_bit17_swizzling);
13767256b59bSFrançois Tigeot 	if (page_do_bit17_swizzling)
13774be47400SFrançois Tigeot 		ret = __copy_from_user_swizzled(vaddr, offset, user_data,
13784be47400SFrançois Tigeot 						length);
13797256b59bSFrançois Tigeot 	else
13804be47400SFrançois Tigeot 		ret = __copy_from_user(vaddr + offset, user_data, length);
13817256b59bSFrançois Tigeot 	if (needs_clflush_after)
13824be47400SFrançois Tigeot 		shmem_clflush_swizzled_range(vaddr + offset, length,
13837256b59bSFrançois Tigeot 					     page_do_bit17_swizzling);
13847256b59bSFrançois Tigeot 	kunmap(page);
13857256b59bSFrançois Tigeot 
13867256b59bSFrançois Tigeot 	return ret ? -EFAULT : 0;
13877256b59bSFrançois Tigeot }
13887256b59bSFrançois Tigeot 
13894be47400SFrançois Tigeot /* Per-page copy function for the shmem pwrite fastpath.
13904be47400SFrançois Tigeot  * Flushes invalid cachelines before writing to the target if
13914be47400SFrançois Tigeot  * needs_clflush_before is set and flushes out any written cachelines after
13924be47400SFrançois Tigeot  * writing if needs_clflush is set.
13934be47400SFrançois Tigeot  */
1394d1c259eeSFrançois Tigeot static int
shmem_pwrite(struct page * page,int offset,int len,char __user * user_data,bool page_do_bit17_swizzling,bool needs_clflush_before,bool needs_clflush_after)13954be47400SFrançois Tigeot shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
13964be47400SFrançois Tigeot 	     bool page_do_bit17_swizzling,
13974be47400SFrançois Tigeot 	     bool needs_clflush_before,
13984be47400SFrançois Tigeot 	     bool needs_clflush_after)
1399d1c259eeSFrançois Tigeot {
14004be47400SFrançois Tigeot 	int ret;
1401d1c259eeSFrançois Tigeot 
14024be47400SFrançois Tigeot 	ret = -ENODEV;
14034be47400SFrançois Tigeot 	if (!page_do_bit17_swizzling) {
14044be47400SFrançois Tigeot 		char *vaddr = kmap_atomic(page);
14054be47400SFrançois Tigeot 
14064be47400SFrançois Tigeot 		if (needs_clflush_before)
14074be47400SFrançois Tigeot 			drm_clflush_virt_range(vaddr + offset, len);
14084be47400SFrançois Tigeot 		ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
14094be47400SFrançois Tigeot 		if (needs_clflush_after)
14104be47400SFrançois Tigeot 			drm_clflush_virt_range(vaddr + offset, len);
14114be47400SFrançois Tigeot 
14124be47400SFrançois Tigeot 		kunmap_atomic(vaddr);
14134be47400SFrançois Tigeot 	}
14144be47400SFrançois Tigeot 	if (ret == 0)
14154be47400SFrançois Tigeot 		return ret;
14164be47400SFrançois Tigeot 
14174be47400SFrançois Tigeot 	return shmem_pwrite_slow(page, offset, len, user_data,
14184be47400SFrançois Tigeot 				 page_do_bit17_swizzling,
14194be47400SFrançois Tigeot 				 needs_clflush_before,
14204be47400SFrançois Tigeot 				 needs_clflush_after);
14214be47400SFrançois Tigeot }
14224be47400SFrançois Tigeot 
14234be47400SFrançois Tigeot static int
i915_gem_shmem_pwrite(struct drm_i915_gem_object * obj,const struct drm_i915_gem_pwrite * args)14244be47400SFrançois Tigeot i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
14254be47400SFrançois Tigeot 		      const struct drm_i915_gem_pwrite *args)
14264be47400SFrançois Tigeot {
14274be47400SFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
14284be47400SFrançois Tigeot 	void __user *user_data;
14294be47400SFrançois Tigeot 	u64 remain;
14304be47400SFrançois Tigeot 	unsigned int obj_do_bit17_swizzling;
14314be47400SFrançois Tigeot 	unsigned int partial_cacheline_write;
14324be47400SFrançois Tigeot 	unsigned int needs_clflush;
14334be47400SFrançois Tigeot 	unsigned int offset, idx;
14344be47400SFrançois Tigeot 	int ret;
1435a85cb24fSFrançois Tigeot #ifdef __DragonFly__
1436a85cb24fSFrançois Tigeot 	vm_object_t vm_obj;
1437a85cb24fSFrançois Tigeot #endif
14384be47400SFrançois Tigeot 
14394be47400SFrançois Tigeot 	ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
14401e12ee3bSFrançois Tigeot 	if (ret)
14411e12ee3bSFrançois Tigeot 		return ret;
1442d1c259eeSFrançois Tigeot 
14434be47400SFrançois Tigeot 	ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
14444be47400SFrançois Tigeot 	mutex_unlock(&i915->drm.struct_mutex);
14454be47400SFrançois Tigeot 	if (ret)
14464be47400SFrançois Tigeot 		return ret;
1447d1c259eeSFrançois Tigeot 
14484be47400SFrançois Tigeot 	obj_do_bit17_swizzling = 0;
14494be47400SFrançois Tigeot 	if (i915_gem_object_needs_bit17_swizzle(obj))
14504be47400SFrançois Tigeot 		obj_do_bit17_swizzling = BIT(17);
14519edbd4a0SFrançois Tigeot 
14529edbd4a0SFrançois Tigeot 	/* If we don't overwrite a cacheline completely we need to be
14539edbd4a0SFrançois Tigeot 	 * careful to have up-to-date data by first clflushing. Don't
14544be47400SFrançois Tigeot 	 * overcomplicate things and flush the entire patch.
14559edbd4a0SFrançois Tigeot 	 */
14564be47400SFrançois Tigeot 	partial_cacheline_write = 0;
14574be47400SFrançois Tigeot 	if (needs_clflush & CLFLUSH_BEFORE)
14584be47400SFrançois Tigeot 		partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
14599edbd4a0SFrançois Tigeot 
14604be47400SFrançois Tigeot 	user_data = u64_to_user_ptr(args->data_ptr);
14614be47400SFrançois Tigeot 	remain = args->size;
14624be47400SFrançois Tigeot 	offset = offset_in_page(args->offset);
1463a85cb24fSFrançois Tigeot #ifdef __DragonFly__
1464a85cb24fSFrançois Tigeot 	vm_obj = obj->base.filp;
1465a85cb24fSFrançois Tigeot 	VM_OBJECT_LOCK(vm_obj);
1466a85cb24fSFrançois Tigeot 	vm_object_pip_add(vm_obj, 1);
1467a85cb24fSFrançois Tigeot #endif
14684be47400SFrançois Tigeot 	for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
14694be47400SFrançois Tigeot 		struct page *page = i915_gem_object_get_page(obj, idx);
14704be47400SFrançois Tigeot 		int length;
14714be47400SFrançois Tigeot 
14724be47400SFrançois Tigeot 		length = remain;
14734be47400SFrançois Tigeot 		if (offset + length > PAGE_SIZE)
14744be47400SFrançois Tigeot 			length = PAGE_SIZE - offset;
14754be47400SFrançois Tigeot 
14764be47400SFrançois Tigeot 		ret = shmem_pwrite(page, offset, length, user_data,
14774be47400SFrançois Tigeot 				   page_to_phys(page) & obj_do_bit17_swizzling,
14784be47400SFrançois Tigeot 				   (offset | length) & partial_cacheline_write,
14794be47400SFrançois Tigeot 				   needs_clflush & CLFLUSH_AFTER);
14804be47400SFrançois Tigeot 		if (ret)
14814be47400SFrançois Tigeot 			break;
14824be47400SFrançois Tigeot 
14834be47400SFrançois Tigeot 		remain -= length;
14844be47400SFrançois Tigeot 		user_data += length;
14854be47400SFrançois Tigeot 		offset = 0;
14864be47400SFrançois Tigeot 	}
1487a85cb24fSFrançois Tigeot #ifdef __DragonFly__
1488a85cb24fSFrançois Tigeot 	if (vm_obj != obj->base.filp) {
1489a85cb24fSFrançois Tigeot 		kprintf("i915_gem_shmem_pwrite: VM_OBJECT CHANGED! %p %p\n",
1490a85cb24fSFrançois Tigeot 			vm_obj, obj->base.filp);
1491a85cb24fSFrançois Tigeot 	}
1492a85cb24fSFrançois Tigeot 	vm_object_pip_wakeup(vm_obj);
1493a85cb24fSFrançois Tigeot 	VM_OBJECT_UNLOCK(vm_obj);
1494a85cb24fSFrançois Tigeot #endif
14959edbd4a0SFrançois Tigeot 
1496a85cb24fSFrançois Tigeot 	intel_fb_obj_flush(obj, ORIGIN_CPU);
14974be47400SFrançois Tigeot 	i915_gem_obj_finish_shmem_access(obj);
14989edbd4a0SFrançois Tigeot 	return ret;
1499e11a51e3SFrançois Tigeot }
1500e3adcf8fSFrançois Tigeot 
1501e11a51e3SFrançois Tigeot /**
1502e11a51e3SFrançois Tigeot  * Writes data to the object referenced by handle.
15031487f786SFrançois Tigeot  * @dev: drm device
15041487f786SFrançois Tigeot  * @data: ioctl data blob
15051487f786SFrançois Tigeot  * @file: drm file
1506e11a51e3SFrançois Tigeot  *
1507e11a51e3SFrançois Tigeot  * On error, the contents of the buffer that were to be modified are undefined.
1508e11a51e3SFrançois Tigeot  */
1509e11a51e3SFrançois Tigeot int
i915_gem_pwrite_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1510e11a51e3SFrançois Tigeot i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1511e11a51e3SFrançois Tigeot 		      struct drm_file *file)
1512e11a51e3SFrançois Tigeot {
1513d2557f23SFrançois Tigeot 	struct drm_i915_gem_pwrite *args = data;
1514d1c259eeSFrançois Tigeot 	struct drm_i915_gem_object *obj;
15159edbd4a0SFrançois Tigeot 	int ret;
1516e3adcf8fSFrançois Tigeot 
1517d1c259eeSFrançois Tigeot 	if (args->size == 0)
1518d1c259eeSFrançois Tigeot 		return 0;
1519d1c259eeSFrançois Tigeot 
15208621f407SFrançois Tigeot #if 0
15218621f407SFrançois Tigeot 	if (!access_ok(VERIFY_READ,
15228621f407SFrançois Tigeot 		       u64_to_user_ptr(args->data_ptr),
15238621f407SFrançois Tigeot 		       args->size))
15248621f407SFrançois Tigeot 		return -EFAULT;
15258621f407SFrançois Tigeot #endif
15268621f407SFrançois Tigeot 
152787df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file, args->handle);
152871f41f3eSFrançois Tigeot 	if (!obj)
152971f41f3eSFrançois Tigeot 		return -ENOENT;
1530d1c259eeSFrançois Tigeot 
1531d1c259eeSFrançois Tigeot 	/* Bounds check destination. */
1532a85cb24fSFrançois Tigeot 	if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
1533d1c259eeSFrançois Tigeot 		ret = -EINVAL;
153471f41f3eSFrançois Tigeot 		goto err;
1535d1c259eeSFrançois Tigeot 	}
1536d1c259eeSFrançois Tigeot 
1537b15f8132SFrançois Tigeot 	trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1538b15f8132SFrançois Tigeot 
1539a85cb24fSFrançois Tigeot 	ret = -ENODEV;
1540a85cb24fSFrançois Tigeot 	if (obj->ops->pwrite)
1541a85cb24fSFrançois Tigeot 		ret = obj->ops->pwrite(obj, args);
1542a85cb24fSFrançois Tigeot 	if (ret != -ENODEV)
1543a85cb24fSFrançois Tigeot 		goto err;
1544a85cb24fSFrançois Tigeot 
15454be47400SFrançois Tigeot 	ret = i915_gem_object_wait(obj,
15464be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE |
15474be47400SFrançois Tigeot 				   I915_WAIT_ALL,
15484be47400SFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
15494be47400SFrançois Tigeot 				   to_rps_client(file));
155071f41f3eSFrançois Tigeot 	if (ret)
155171f41f3eSFrançois Tigeot 		goto err;
155271f41f3eSFrançois Tigeot 
15534be47400SFrançois Tigeot 	ret = i915_gem_object_pin_pages(obj);
155471f41f3eSFrançois Tigeot 	if (ret)
15554be47400SFrançois Tigeot 		goto err;
155671f41f3eSFrançois Tigeot 
15579edbd4a0SFrançois Tigeot 	ret = -EFAULT;
15589edbd4a0SFrançois Tigeot 	/* We can only do the GTT pwrite on untiled buffers, as otherwise
15599edbd4a0SFrançois Tigeot 	 * it would end up going through the fenced access, and we'll get
15609edbd4a0SFrançois Tigeot 	 * different detiling behavior between reading and writing.
15619edbd4a0SFrançois Tigeot 	 * pread/pwrite currently are reading and writing from the CPU
15629edbd4a0SFrançois Tigeot 	 * perspective, requiring manual detiling by the client.
15639edbd4a0SFrançois Tigeot 	 */
15641487f786SFrançois Tigeot 	if (!i915_gem_object_has_struct_page(obj) ||
15654be47400SFrançois Tigeot 	    cpu_write_needs_clflush(obj))
15669edbd4a0SFrançois Tigeot 		/* Note that the gtt paths might fail with non-page-backed user
15679edbd4a0SFrançois Tigeot 		 * pointers (e.g. gtt mappings when moving data between
15684be47400SFrançois Tigeot 		 * textures). Fallback to the shmem path in that case.
15694be47400SFrançois Tigeot 		 */
15704be47400SFrançois Tigeot 		ret = i915_gem_gtt_pwrite_fast(obj, args);
15719edbd4a0SFrançois Tigeot 
1572bf017597SFrançois Tigeot 	if (ret == -EFAULT || ret == -ENOSPC) {
15732c9916cdSFrançois Tigeot 		if (obj->phys_handle)
15742c9916cdSFrançois Tigeot 			ret = i915_gem_phys_pwrite(obj, args, file);
15751487f786SFrançois Tigeot 		else
15764be47400SFrançois Tigeot 			ret = i915_gem_shmem_pwrite(obj, args);
15772c9916cdSFrançois Tigeot 	}
15789edbd4a0SFrançois Tigeot 
15794be47400SFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
158071f41f3eSFrançois Tigeot err:
15814be47400SFrançois Tigeot 	i915_gem_object_put(obj);
1582686a02f1SFrançois Tigeot 	return ret;
158367838cc5SFrançois Tigeot }
158467838cc5SFrançois Tigeot 
i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object * obj)15854be47400SFrançois Tigeot static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
15864be47400SFrançois Tigeot {
15874be47400SFrançois Tigeot 	struct drm_i915_private *i915;
15884be47400SFrançois Tigeot 	struct list_head *list;
15894be47400SFrançois Tigeot 	struct i915_vma *vma;
15904be47400SFrançois Tigeot 
15913f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
15923f2dd94aSFrançois Tigeot 
15934be47400SFrançois Tigeot 	list_for_each_entry(vma, &obj->vma_list, obj_link) {
15944be47400SFrançois Tigeot 		if (!i915_vma_is_ggtt(vma))
1595a85cb24fSFrançois Tigeot 			break;
15964be47400SFrançois Tigeot 
15974be47400SFrançois Tigeot 		if (i915_vma_is_active(vma))
15984be47400SFrançois Tigeot 			continue;
15994be47400SFrançois Tigeot 
16004be47400SFrançois Tigeot 		if (!drm_mm_node_allocated(&vma->node))
16014be47400SFrançois Tigeot 			continue;
16024be47400SFrançois Tigeot 
16034be47400SFrançois Tigeot 		list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
16044be47400SFrançois Tigeot 	}
16054be47400SFrançois Tigeot 
16064be47400SFrançois Tigeot 	i915 = to_i915(obj->base.dev);
16073f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.obj_lock, LK_EXCLUSIVE);
16084be47400SFrançois Tigeot 	list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
16093f2dd94aSFrançois Tigeot 	list_move_tail(&obj->mm.link, list);
16103f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.obj_lock, LK_RELEASE);
16114be47400SFrançois Tigeot }
16124be47400SFrançois Tigeot 
1613686a02f1SFrançois Tigeot /**
1614e11a51e3SFrançois Tigeot  * Called when user space prepares to use an object with the CPU, either
1615e11a51e3SFrançois Tigeot  * through the mmap ioctl's mapping or a GTT mapping.
16161487f786SFrançois Tigeot  * @dev: drm device
16171487f786SFrançois Tigeot  * @data: ioctl data blob
16181487f786SFrançois Tigeot  * @file: drm file
1619e11a51e3SFrançois Tigeot  */
1620e11a51e3SFrançois Tigeot int
i915_gem_set_domain_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1621e11a51e3SFrançois Tigeot i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1622e11a51e3SFrançois Tigeot 			  struct drm_file *file)
1623e11a51e3SFrançois Tigeot {
1624f192107fSFrançois Tigeot 	struct drm_i915_gem_set_domain *args = data;
1625e11a51e3SFrançois Tigeot 	struct drm_i915_gem_object *obj;
1626f192107fSFrançois Tigeot 	uint32_t read_domains = args->read_domains;
1627f192107fSFrançois Tigeot 	uint32_t write_domain = args->write_domain;
16284be47400SFrançois Tigeot 	int err;
1629e11a51e3SFrançois Tigeot 
1630f192107fSFrançois Tigeot 	/* Only handle setting domains to types used by the CPU. */
163171f41f3eSFrançois Tigeot 	if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
1632f192107fSFrançois Tigeot 		return -EINVAL;
1633f192107fSFrançois Tigeot 
1634f192107fSFrançois Tigeot 	/* Having something in the write domain implies it's in the read
1635f192107fSFrançois Tigeot 	 * domain, and only that read domain.  Enforce that in the request.
1636f192107fSFrançois Tigeot 	 */
1637f192107fSFrançois Tigeot 	if (write_domain != 0 && read_domains != write_domain)
1638f192107fSFrançois Tigeot 		return -EINVAL;
1639e11a51e3SFrançois Tigeot 
164087df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file, args->handle);
164171f41f3eSFrançois Tigeot 	if (!obj)
164271f41f3eSFrançois Tigeot 		return -ENOENT;
1643e11a51e3SFrançois Tigeot 
164467838cc5SFrançois Tigeot 	/* Try to flush the object off the GPU without holding the lock.
164567838cc5SFrançois Tigeot 	 * We will repeat the flush holding the lock in the normal manner
164667838cc5SFrançois Tigeot 	 * to catch cases where we are gazumped.
164767838cc5SFrançois Tigeot 	 */
16484be47400SFrançois Tigeot 	err = i915_gem_object_wait(obj,
16494be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE |
16504be47400SFrançois Tigeot 				   (write_domain ? I915_WAIT_ALL : 0),
16514be47400SFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
16524be47400SFrançois Tigeot 				   to_rps_client(file));
16534be47400SFrançois Tigeot 	if (err)
16544be47400SFrançois Tigeot 		goto out;
165571f41f3eSFrançois Tigeot 
16564be47400SFrançois Tigeot 	/* Flush and acquire obj->pages so that we are coherent through
16574be47400SFrançois Tigeot 	 * direct access in memory with previous cached writes through
16584be47400SFrançois Tigeot 	 * shmemfs and that our cache domain tracking remains valid.
16594be47400SFrançois Tigeot 	 * For example, if the obj->filp was moved to swap without us
16604be47400SFrançois Tigeot 	 * being notified and releasing the pages, we would mistakenly
16614be47400SFrançois Tigeot 	 * continue to assume that the obj remained out of the CPU cached
16624be47400SFrançois Tigeot 	 * domain.
16634be47400SFrançois Tigeot 	 */
16644be47400SFrançois Tigeot 	err = i915_gem_object_pin_pages(obj);
16654be47400SFrançois Tigeot 	if (err)
16664be47400SFrançois Tigeot 		goto out;
16674be47400SFrançois Tigeot 
16684be47400SFrançois Tigeot 	err = i915_mutex_lock_interruptible(dev);
16694be47400SFrançois Tigeot 	if (err)
16704be47400SFrançois Tigeot 		goto out_unpin;
167167838cc5SFrançois Tigeot 
16723f2dd94aSFrançois Tigeot 	if (read_domains & I915_GEM_DOMAIN_WC)
16733f2dd94aSFrançois Tigeot 		err = i915_gem_object_set_to_wc_domain(obj, write_domain);
16743f2dd94aSFrançois Tigeot 	else if (read_domains & I915_GEM_DOMAIN_GTT)
16753f2dd94aSFrançois Tigeot 		err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
16762c9916cdSFrançois Tigeot 	else
16773f2dd94aSFrançois Tigeot 		err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
16784be47400SFrançois Tigeot 
16794be47400SFrançois Tigeot 	/* And bump the LRU for this access */
16804be47400SFrançois Tigeot 	i915_gem_object_bump_inactive_ggtt(obj);
16814be47400SFrançois Tigeot 
16824be47400SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1683e11a51e3SFrançois Tigeot 
1684a05eeebfSFrançois Tigeot 	if (write_domain != 0)
16853f2dd94aSFrançois Tigeot 		intel_fb_obj_invalidate(obj,
16863f2dd94aSFrançois Tigeot 					fb_write_origin(obj, write_domain));
1687a05eeebfSFrançois Tigeot 
16884be47400SFrançois Tigeot out_unpin:
16894be47400SFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
16904be47400SFrançois Tigeot out:
169187df8fc6SFrançois Tigeot 	i915_gem_object_put(obj);
16924be47400SFrançois Tigeot 	return err;
1693e11a51e3SFrançois Tigeot }
1694e11a51e3SFrançois Tigeot 
1695e11a51e3SFrançois Tigeot /**
1696e11a51e3SFrançois Tigeot  * Called when user space has done writes to this buffer
16971487f786SFrançois Tigeot  * @dev: drm device
16981487f786SFrançois Tigeot  * @data: ioctl data blob
16991487f786SFrançois Tigeot  * @file: drm file
1700e11a51e3SFrançois Tigeot  */
1701e11a51e3SFrançois Tigeot int
i915_gem_sw_finish_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1702e11a51e3SFrançois Tigeot i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1703e11a51e3SFrançois Tigeot 			 struct drm_file *file)
1704e11a51e3SFrançois Tigeot {
1705686a02f1SFrançois Tigeot 	struct drm_i915_gem_sw_finish *args = data;
1706e11a51e3SFrançois Tigeot 	struct drm_i915_gem_object *obj;
1707c16fa8beSFrançois Tigeot 
170887df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file, args->handle);
170971f41f3eSFrançois Tigeot 	if (!obj)
171071f41f3eSFrançois Tigeot 		return -ENOENT;
1711f192107fSFrançois Tigeot 
1712f192107fSFrançois Tigeot 	/* Pinned buffers may be scanout, so flush the cache */
1713a85cb24fSFrançois Tigeot 	i915_gem_object_flush_if_display(obj);
17144be47400SFrançois Tigeot 	i915_gem_object_put(obj);
1715a85cb24fSFrançois Tigeot 
1716a85cb24fSFrançois Tigeot 	return 0;
1717e11a51e3SFrançois Tigeot }
1718e11a51e3SFrançois Tigeot 
1719*303e1baeSFrançois Tigeot static int
vm_object_map_wc_callback(vm_page_t p,void * data)1720*303e1baeSFrançois Tigeot vm_object_map_wc_callback(vm_page_t p, void *data)
1721*303e1baeSFrançois Tigeot {
1722*303e1baeSFrançois Tigeot 	pmap_page_set_memattr(p, VM_MEMATTR_WRITE_COMBINING);
1723*303e1baeSFrançois Tigeot 
1724*303e1baeSFrançois Tigeot 	return 0;
1725*303e1baeSFrançois Tigeot }
1726*303e1baeSFrançois Tigeot 
1727e11a51e3SFrançois Tigeot /**
17281487f786SFrançois Tigeot  * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
17291487f786SFrançois Tigeot  *			 it is mapped to.
17301487f786SFrançois Tigeot  * @dev: drm device
17311487f786SFrançois Tigeot  * @data: ioctl data blob
17321487f786SFrançois Tigeot  * @file: drm file
1733e11a51e3SFrançois Tigeot  *
1734e11a51e3SFrançois Tigeot  * While the mapping holds a reference on the contents of the object, it doesn't
1735e11a51e3SFrançois Tigeot  * imply a ref on the object itself.
17362c9916cdSFrançois Tigeot  *
17372c9916cdSFrançois Tigeot  * IMPORTANT:
17382c9916cdSFrançois Tigeot  *
17392c9916cdSFrançois Tigeot  * DRM driver writers who look a this function as an example for how to do GEM
17402c9916cdSFrançois Tigeot  * mmap support, please don't implement mmap support like here. The modern way
17412c9916cdSFrançois Tigeot  * to implement DRM mmap support is with an mmap offset ioctl (like
17422c9916cdSFrançois Tigeot  * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
17432c9916cdSFrançois Tigeot  * That way debug tooling like valgrind will understand what's going on, hiding
17442c9916cdSFrançois Tigeot  * the mmap call in a driver private ioctl will break that. The i915 driver only
17452c9916cdSFrançois Tigeot  * does cpu mmaps this way because we didn't know better.
1746e11a51e3SFrançois Tigeot  */
1747e11a51e3SFrançois Tigeot int
i915_gem_mmap_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1748e11a51e3SFrançois Tigeot i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1749e11a51e3SFrançois Tigeot 		    struct drm_file *file)
1750e11a51e3SFrançois Tigeot {
1751c737e47cSFrançois Tigeot 	struct drm_i915_gem_mmap *args = data;
175287df8fc6SFrançois Tigeot 	struct drm_i915_gem_object *obj;
17539edbd4a0SFrançois Tigeot 	unsigned long addr;
1754a85cb24fSFrançois Tigeot #ifdef __DragonFly__
1755c737e47cSFrançois Tigeot 	struct proc *p = curproc;
1756c737e47cSFrançois Tigeot 	vm_map_t map = &p->p_vmspace->vm_map;
1757e11a51e3SFrançois Tigeot 	vm_size_t size;
1758c737e47cSFrançois Tigeot 	int error = 0, rv;
1759*303e1baeSFrançois Tigeot 	struct vm_object *vm_obj;
1760*303e1baeSFrançois Tigeot 	struct rb_vm_page_scan_info info;
1761a85cb24fSFrançois Tigeot #endif
1762e11a51e3SFrançois Tigeot 
17637ec9f8e5SFrançois Tigeot 	if (args->flags & ~(I915_MMAP_WC))
17647ec9f8e5SFrançois Tigeot 		return -EINVAL;
17657ec9f8e5SFrançois Tigeot 
17661487f786SFrançois Tigeot 	if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
17671487f786SFrançois Tigeot 		return -ENODEV;
17681487f786SFrançois Tigeot 
176987df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file, args->handle);
177087df8fc6SFrançois Tigeot 	if (!obj)
1771c737e47cSFrançois Tigeot 		return -ENOENT;
1772c737e47cSFrançois Tigeot 
177371f41f3eSFrançois Tigeot 	/* prime objects have no backing filp to GEM mmap
177471f41f3eSFrançois Tigeot 	 * pages from.
177571f41f3eSFrançois Tigeot 	 */
177671f41f3eSFrançois Tigeot 	if (!obj->base.filp) {
17774be47400SFrançois Tigeot 		i915_gem_object_put(obj);
177871f41f3eSFrançois Tigeot 		return -EINVAL;
177971f41f3eSFrançois Tigeot 	}
178071f41f3eSFrançois Tigeot 
1781*303e1baeSFrançois Tigeot #ifdef __DragonFly__
1782e11a51e3SFrançois Tigeot 	if (args->size == 0)
1783e11a51e3SFrançois Tigeot 		goto out;
1784c737e47cSFrançois Tigeot 
1785e11a51e3SFrançois Tigeot 	size = round_page(args->size);
1786e11a51e3SFrançois Tigeot 	if (map->size + size > p->p_rlimit[RLIMIT_VMEM].rlim_cur) {
17879edbd4a0SFrançois Tigeot 		error = -ENOMEM;
1788e11a51e3SFrançois Tigeot 		goto out;
1789e11a51e3SFrançois Tigeot 	}
1790e11a51e3SFrançois Tigeot 
1791c7b62c42SMatthew Dillon 	/*
1792c7b62c42SMatthew Dillon 	 * Call hint to ensure that NULL is not returned as a valid address
179312b70c84SMatthew Dillon 	 * and to reduce vm_map traversals. XXX causes instability, use a
179412b70c84SMatthew Dillon 	 * fixed low address as the start point instead to avoid the NULL
179512b70c84SMatthew Dillon 	 * return issue.
1796c7b62c42SMatthew Dillon 	 */
179712b70c84SMatthew Dillon 	addr = PAGE_SIZE;
1798eecfe9a4SMatthew Dillon 
1799eecfe9a4SMatthew Dillon 	/*
1800eecfe9a4SMatthew Dillon 	 * Use 256KB alignment.  It is unclear why this matters for a
1801eecfe9a4SMatthew Dillon 	 * virtual address but it appears to fix a number of application/X
1802eecfe9a4SMatthew Dillon 	 * crashes and kms console switching is much faster.
1803eecfe9a4SMatthew Dillon 	 */
180487df8fc6SFrançois Tigeot 	vm_object_hold(obj->base.filp);
180587df8fc6SFrançois Tigeot 	vm_object_reference_locked(obj->base.filp);
180687df8fc6SFrançois Tigeot 	vm_object_drop(obj->base.filp);
1807eecfe9a4SMatthew Dillon 
18087ca4ece0SFrançois Tigeot 	/* Something gets wrong here: fails to mmap 4096 */
180987df8fc6SFrançois Tigeot 	rv = vm_map_find(map, obj->base.filp, NULL,
18100adbcbd6SMatthew Dillon 			 args->offset, &addr, args->size,
1811eecfe9a4SMatthew Dillon 			 256 * 1024, /* align */
1812e11a51e3SFrançois Tigeot 			 TRUE, /* fitit */
18133091de50SMatthew Dillon 			 VM_MAPTYPE_NORMAL, VM_SUBSYS_DRM_GEM,
1814e11a51e3SFrançois Tigeot 			 VM_PROT_READ | VM_PROT_WRITE, /* prot */
1815e11a51e3SFrançois Tigeot 			 VM_PROT_READ | VM_PROT_WRITE, /* max */
1816e11a51e3SFrançois Tigeot 			 MAP_SHARED /* cow */);
1817e11a51e3SFrançois Tigeot 	if (rv != KERN_SUCCESS) {
181887df8fc6SFrançois Tigeot 		vm_object_deallocate(obj->base.filp);
1819e11a51e3SFrançois Tigeot 		error = -vm_mmap_to_errno(rv);
1820e11a51e3SFrançois Tigeot 	} else {
1821e11a51e3SFrançois Tigeot 		args->addr_ptr = (uint64_t)addr;
1822e11a51e3SFrançois Tigeot 	}
1823*303e1baeSFrançois Tigeot #else
1824a85cb24fSFrançois Tigeot 	addr = vm_mmap(obj->base.filp, 0, args->size,
1825a85cb24fSFrançois Tigeot 		       PROT_READ | PROT_WRITE, MAP_SHARED,
1826a85cb24fSFrançois Tigeot 		       args->offset);
1827*303e1baeSFrançois Tigeot #endif	/* __DragonFly__ */
1828*303e1baeSFrançois Tigeot 	if (args->flags & I915_MMAP_WC) {	/* I915_PARAM_MMAP_VERSION */
182971f41f3eSFrançois Tigeot 		struct mm_struct *mm = current->mm;
1830*303e1baeSFrançois Tigeot #if 0
183171f41f3eSFrançois Tigeot 		struct vm_area_struct *vma;
1832*303e1baeSFrançois Tigeot #endif
183371f41f3eSFrançois Tigeot 
183471f41f3eSFrançois Tigeot 		if (down_write_killable(&mm->mmap_sem)) {
18354be47400SFrançois Tigeot 			i915_gem_object_put(obj);
183671f41f3eSFrançois Tigeot 			return -EINTR;
183771f41f3eSFrançois Tigeot 		}
1838*303e1baeSFrançois Tigeot #ifdef __DragonFly__
1839*303e1baeSFrançois Tigeot 		vm_obj = obj->base.filp;
1840*303e1baeSFrançois Tigeot 		vm_object_hold(vm_obj);
1841*303e1baeSFrançois Tigeot 		vm_obj->memattr = pgprot_writecombine(vm_obj->memattr);
1842*303e1baeSFrançois Tigeot 		/* Change attributes of all pages in the mapping here */
1843*303e1baeSFrançois Tigeot 		info.error = 0;
1844*303e1baeSFrançois Tigeot 		info.count = 0;
1845*303e1baeSFrançois Tigeot 		vm_page_rb_tree_RB_SCAN(
1846*303e1baeSFrançois Tigeot 			&vm_obj->rb_memq,		/* *head */
1847*303e1baeSFrançois Tigeot 			NULL, 				/* *scancmp */
1848*303e1baeSFrançois Tigeot 			vm_object_map_wc_callback,	/* *callback */
1849*303e1baeSFrançois Tigeot 			&info				/* *data */
1850*303e1baeSFrançois Tigeot 		);
1851*303e1baeSFrançois Tigeot 		vm_object_drop(vm_obj);
1852*303e1baeSFrançois Tigeot #else
185371f41f3eSFrançois Tigeot 		vma = find_vma(mm, addr);
185471f41f3eSFrançois Tigeot 		if (vma)
185571f41f3eSFrançois Tigeot 			vma->vm_page_prot =
185671f41f3eSFrançois Tigeot 				pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
185771f41f3eSFrançois Tigeot 		else
185871f41f3eSFrançois Tigeot 			addr = -ENOMEM;
1859*303e1baeSFrançois Tigeot #endif	/* __DragonFly__ */
186071f41f3eSFrançois Tigeot 		up_write(&mm->mmap_sem);
186171f41f3eSFrançois Tigeot 
186271f41f3eSFrançois Tigeot 		/* This may race, but that's ok, it only gets set */
18631e12ee3bSFrançois Tigeot 		WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
186471f41f3eSFrançois Tigeot 	}
186571f41f3eSFrançois Tigeot 
1866e11a51e3SFrançois Tigeot out:
18674be47400SFrançois Tigeot 	i915_gem_object_put(obj);
1868a85cb24fSFrançois Tigeot 	if (IS_ERR((void *)addr))
1869a85cb24fSFrançois Tigeot 		return addr;
187071f41f3eSFrançois Tigeot 
187171f41f3eSFrançois Tigeot 	args->addr_ptr = (uint64_t) addr;
187271f41f3eSFrançois Tigeot 
187371f41f3eSFrançois Tigeot 	return 0;
1874e11a51e3SFrançois Tigeot }
1875e11a51e3SFrançois Tigeot 
tile_row_pages(struct drm_i915_gem_object * obj)18761e12ee3bSFrançois Tigeot static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
18771e12ee3bSFrançois Tigeot {
1878a85cb24fSFrançois Tigeot 	return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
18791e12ee3bSFrançois Tigeot }
18801e12ee3bSFrançois Tigeot 
18811e12ee3bSFrançois Tigeot /**
18821e12ee3bSFrançois Tigeot  * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
18831e12ee3bSFrançois Tigeot  *
18841e12ee3bSFrançois Tigeot  * A history of the GTT mmap interface:
18851e12ee3bSFrançois Tigeot  *
18861e12ee3bSFrançois Tigeot  * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
18871e12ee3bSFrançois Tigeot  *     aligned and suitable for fencing, and still fit into the available
18881e12ee3bSFrançois Tigeot  *     mappable space left by the pinned display objects. A classic problem
18891e12ee3bSFrançois Tigeot  *     we called the page-fault-of-doom where we would ping-pong between
18901e12ee3bSFrançois Tigeot  *     two objects that could not fit inside the GTT and so the memcpy
18911e12ee3bSFrançois Tigeot  *     would page one object in at the expense of the other between every
18921e12ee3bSFrançois Tigeot  *     single byte.
18931e12ee3bSFrançois Tigeot  *
18941e12ee3bSFrançois Tigeot  * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
18951e12ee3bSFrançois Tigeot  *     as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
18961e12ee3bSFrançois Tigeot  *     object is too large for the available space (or simply too large
18971e12ee3bSFrançois Tigeot  *     for the mappable aperture!), a view is created instead and faulted
18981e12ee3bSFrançois Tigeot  *     into userspace. (This view is aligned and sized appropriately for
18991e12ee3bSFrançois Tigeot  *     fenced access.)
19001e12ee3bSFrançois Tigeot  *
19013f2dd94aSFrançois Tigeot  * 2 - Recognise WC as a separate cache domain so that we can flush the
19023f2dd94aSFrançois Tigeot  *     delayed writes via GTT before performing direct access via WC.
19033f2dd94aSFrançois Tigeot  *
19041e12ee3bSFrançois Tigeot  * Restrictions:
19051e12ee3bSFrançois Tigeot  *
19061e12ee3bSFrançois Tigeot  *  * snoopable objects cannot be accessed via the GTT. It can cause machine
19071e12ee3bSFrançois Tigeot  *    hangs on some architectures, corruption on others. An attempt to service
19081e12ee3bSFrançois Tigeot  *    a GTT page fault from a snoopable object will generate a SIGBUS.
19091e12ee3bSFrançois Tigeot  *
19101e12ee3bSFrançois Tigeot  *  * the object must be able to fit into RAM (physical memory, though no
19111e12ee3bSFrançois Tigeot  *    limited to the mappable aperture).
19121e12ee3bSFrançois Tigeot  *
19131e12ee3bSFrançois Tigeot  *
19141e12ee3bSFrançois Tigeot  * Caveats:
19151e12ee3bSFrançois Tigeot  *
19161e12ee3bSFrançois Tigeot  *  * a new GTT page fault will synchronize rendering from the GPU and flush
19171e12ee3bSFrançois Tigeot  *    all data to system memory. Subsequent access will not be synchronized.
19181e12ee3bSFrançois Tigeot  *
19191e12ee3bSFrançois Tigeot  *  * all mappings are revoked on runtime device suspend.
19201e12ee3bSFrançois Tigeot  *
19211e12ee3bSFrançois Tigeot  *  * there are only 8, 16 or 32 fence registers to share between all users
19221e12ee3bSFrançois Tigeot  *    (older machines require fence register for display and blitter access
19231e12ee3bSFrançois Tigeot  *    as well). Contention of the fence registers will cause the previous users
19241e12ee3bSFrançois Tigeot  *    to be unmapped and any new access will generate new page faults.
19251e12ee3bSFrançois Tigeot  *
19261e12ee3bSFrançois Tigeot  *  * running out of memory while servicing a fault may generate a SIGBUS,
19271e12ee3bSFrançois Tigeot  *    rather than the expected SIGSEGV.
19281e12ee3bSFrançois Tigeot  */
i915_gem_mmap_gtt_version(void)19291e12ee3bSFrançois Tigeot int i915_gem_mmap_gtt_version(void)
19301e12ee3bSFrançois Tigeot {
19313f2dd94aSFrançois Tigeot 	return 2;
19321e12ee3bSFrançois Tigeot }
19331e12ee3bSFrançois Tigeot 
1934a85cb24fSFrançois Tigeot static inline struct i915_ggtt_view
compute_partial_view(struct drm_i915_gem_object * obj,pgoff_t page_offset,unsigned int chunk)1935a85cb24fSFrançois Tigeot compute_partial_view(struct drm_i915_gem_object *obj,
1936a85cb24fSFrançois Tigeot 		     pgoff_t page_offset,
1937a85cb24fSFrançois Tigeot 		     unsigned int chunk)
1938a85cb24fSFrançois Tigeot {
1939a85cb24fSFrançois Tigeot 	struct i915_ggtt_view view;
1940a85cb24fSFrançois Tigeot 
1941a85cb24fSFrançois Tigeot 	if (i915_gem_object_is_tiled(obj))
1942a85cb24fSFrançois Tigeot 		chunk = roundup(chunk, tile_row_pages(obj));
1943a85cb24fSFrançois Tigeot 
1944a85cb24fSFrançois Tigeot 	view.type = I915_GGTT_VIEW_PARTIAL;
1945a85cb24fSFrançois Tigeot 	view.partial.offset = rounddown(page_offset, chunk);
1946a85cb24fSFrançois Tigeot 	view.partial.size =
1947a85cb24fSFrançois Tigeot 		min_t(unsigned int, chunk,
1948a85cb24fSFrançois Tigeot 		      (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
1949a85cb24fSFrançois Tigeot 
1950a85cb24fSFrançois Tigeot 	/* If the partial covers the entire object, just create a normal VMA. */
1951a85cb24fSFrançois Tigeot 	if (chunk >= obj->base.size >> PAGE_SHIFT)
1952a85cb24fSFrançois Tigeot 		view.type = I915_GGTT_VIEW_NORMAL;
1953a85cb24fSFrançois Tigeot 
1954a85cb24fSFrançois Tigeot 	return view;
1955a85cb24fSFrançois Tigeot }
1956a85cb24fSFrançois Tigeot 
19573f2dd94aSFrançois Tigeot #ifdef __DragonFly__
drm_vma_node_unmap(struct drm_vma_offset_node * node,struct address_space * file_mapping)19583f2dd94aSFrançois Tigeot static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
19593f2dd94aSFrançois Tigeot 				      struct address_space *file_mapping)
19603f2dd94aSFrançois Tigeot {
19613f2dd94aSFrançois Tigeot 	struct drm_i915_gem_object *obj = container_of(
19623f2dd94aSFrançois Tigeot 		node,struct drm_i915_gem_object, base.vma_node);
19633f2dd94aSFrançois Tigeot 	vm_object_t devobj;
19643f2dd94aSFrançois Tigeot 
19653f2dd94aSFrançois Tigeot 	devobj = cdev_pager_lookup(obj);
19663f2dd94aSFrançois Tigeot 	if (devobj != NULL) {
19673f2dd94aSFrançois Tigeot 		VM_OBJECT_LOCK(devobj);
19683f2dd94aSFrançois Tigeot 		vm_object_page_remove(devobj, 0, 0, false);
19693f2dd94aSFrançois Tigeot 		VM_OBJECT_UNLOCK(devobj);
19703f2dd94aSFrançois Tigeot 		vm_object_deallocate(devobj);
19713f2dd94aSFrançois Tigeot 	}
19723f2dd94aSFrançois Tigeot }
19733f2dd94aSFrançois Tigeot #endif
19743f2dd94aSFrançois Tigeot 
__i915_gem_object_release_mmap(struct drm_i915_gem_object * obj)19753f2dd94aSFrançois Tigeot static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
19763f2dd94aSFrançois Tigeot {
19773f2dd94aSFrançois Tigeot 	struct i915_vma *vma;
19783f2dd94aSFrançois Tigeot 
19793f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!obj->userfault_count);
19803f2dd94aSFrançois Tigeot 
19813f2dd94aSFrançois Tigeot 	obj->userfault_count = 0;
19823f2dd94aSFrançois Tigeot 	list_del(&obj->userfault_link);
19833f2dd94aSFrançois Tigeot #ifdef __DragonFly__
19843f2dd94aSFrançois Tigeot 	drm_vma_node_unmap(&obj->base.vma_node, NULL);
19853f2dd94aSFrançois Tigeot #else
19863f2dd94aSFrançois Tigeot 	drm_vma_node_unmap(&obj->base.vma_node,
19873f2dd94aSFrançois Tigeot 			   obj->base.dev->anon_inode->i_mapping);
19883f2dd94aSFrançois Tigeot #endif
19893f2dd94aSFrançois Tigeot 
19903f2dd94aSFrançois Tigeot 	list_for_each_entry(vma, &obj->vma_list, obj_link) {
19913f2dd94aSFrançois Tigeot 		if (!i915_vma_is_ggtt(vma))
19923f2dd94aSFrançois Tigeot 			break;
19933f2dd94aSFrançois Tigeot 
19943f2dd94aSFrançois Tigeot 		i915_vma_unset_userfault(vma);
19953f2dd94aSFrançois Tigeot 	}
19963f2dd94aSFrançois Tigeot }
19973f2dd94aSFrançois Tigeot 
1998e11a51e3SFrançois Tigeot /**
19990b869d8aSFrançois Tigeot  * i915_gem_fault - fault a page into the GTT
200025b95970SMatthew Dillon  *
200125b95970SMatthew Dillon  * vm_obj is locked on entry and expected to be locked on return.
200225b95970SMatthew Dillon  *
20032854a88cSMatthew Dillon  * This is a OBJT_MGTDEVICE object, *mres will be NULL and should be set
20042854a88cSMatthew Dillon  * to the desired vm_page.  The page is not indexed into the vm_obj.
200525b95970SMatthew Dillon  *
200625b95970SMatthew Dillon  * XXX Most GEM calls appear to be interruptable, but we can't hard loop
200725b95970SMatthew Dillon  * in that case.  Release all resources and wait 1 tick before retrying.
200825b95970SMatthew Dillon  * This is a huge problem which needs to be fixed by getting rid of most
200925b95970SMatthew Dillon  * of the interruptability.  The linux code does not retry but does appear
201025b95970SMatthew Dillon  * to have some sort of mechanism (VM_FAULT_NOPAGE ?) for the higher level
201125b95970SMatthew Dillon  * to be able to retry.
201225b95970SMatthew Dillon  *
201325b95970SMatthew Dillon  * --
2014a85cb24fSFrançois Tigeot  * @vma: VMA in question
2015352ff8bdSFrançois Tigeot  * @vmf: fault info
20160b869d8aSFrançois Tigeot  *
20170b869d8aSFrançois Tigeot  * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
20180b869d8aSFrançois Tigeot  * from userspace.  The fault handler takes care of binding the object to
20190b869d8aSFrançois Tigeot  * the GTT (if needed), allocating and programming a fence register (again,
20200b869d8aSFrançois Tigeot  * only if needed based on whether the old reg is still valid or the object
20210b869d8aSFrançois Tigeot  * is tiled) and inserting a new PTE into the faulting process.
20220b869d8aSFrançois Tigeot  *
20230b869d8aSFrançois Tigeot  * Note that the faulting process may involve evicting existing objects
20240b869d8aSFrançois Tigeot  * from the GTT and/or fence registers to make room.  So performance may
20250b869d8aSFrançois Tigeot  * suffer if the GTT working set is large or there are few fence registers
20260b869d8aSFrançois Tigeot  * left.
202725b95970SMatthew Dillon  *
20281e12ee3bSFrançois Tigeot  * The current feature set supported by i915_gem_fault() and thus GTT mmaps
20291e12ee3bSFrançois Tigeot  * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
203025b95970SMatthew Dillon  * vm_obj is locked on entry and expected to be locked on return.  The VM
203125b95970SMatthew Dillon  * pager has placed an anonymous memory page at (obj,offset) which we have
203225b95970SMatthew Dillon  * to replace.
20330b869d8aSFrançois Tigeot  */
20343f2dd94aSFrançois Tigeot // int i915_gem_fault(struct vm_fault *vmf)
i915_gem_fault(vm_object_t vm_obj,vm_ooffset_t offset,int prot,vm_page_t * mres)20359edbd4a0SFrançois Tigeot int i915_gem_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot, vm_page_t *mres)
2036a2fdbec6SFrançois Tigeot {
20371e12ee3bSFrançois Tigeot #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
20383f2dd94aSFrançois Tigeot 	struct vm_area_struct *area;
20399edbd4a0SFrançois Tigeot 	struct drm_i915_gem_object *obj = to_intel_bo(vm_obj->handle);
20409edbd4a0SFrançois Tigeot 	struct drm_device *dev = obj->base.dev;
20418621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
20428621f407SFrançois Tigeot 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
204371f41f3eSFrançois Tigeot 	bool write = !!(prot & VM_PROT_WRITE);
20441e12ee3bSFrançois Tigeot 	struct i915_vma *vma;
2045a85cb24fSFrançois Tigeot 	pgoff_t page_offset;
2046a85cb24fSFrançois Tigeot 	vm_page_t m;
20471e12ee3bSFrançois Tigeot 	unsigned int flags;
204871f41f3eSFrançois Tigeot 	int ret;
20493f2dd94aSFrançois Tigeot #ifdef __DragonFly__
20504be47400SFrançois Tigeot 	int didref = 0;
20513f2dd94aSFrançois Tigeot 	struct vm_area_struct vmas;
20524be47400SFrançois Tigeot 
20534be47400SFrançois Tigeot 	/* Fill-in vm_area_struct */
20543f2dd94aSFrançois Tigeot 	area = &vmas;
20554be47400SFrançois Tigeot 	area->vm_private_data = vm_obj->handle;
20564be47400SFrançois Tigeot 	area->vm_start = 0;
2057a85cb24fSFrançois Tigeot 	area->vm_end = obj->base.size;
20583f2dd94aSFrançois Tigeot 	area->vm_mm = current->mm;
20593f2dd94aSFrançois Tigeot 	// XXX: in Linux, mmap_sem is held on entry of this function
20603f2dd94aSFrançois Tigeot 	// XXX: should that be an exclusive lock ?
20613f2dd94aSFrançois Tigeot 	down_read(&area->vm_mm->mmap_sem);
20623f2dd94aSFrançois Tigeot #endif
20639edbd4a0SFrançois Tigeot 
20649edbd4a0SFrançois Tigeot 	/* We don't use vmf->pgoff since that has the fake offset */
2065a85cb24fSFrançois Tigeot 	page_offset = (unsigned long)offset >> PAGE_SHIFT;
20669edbd4a0SFrançois Tigeot 
2067e24b12cdSMatthew Dillon 	/*
2068e24b12cdSMatthew Dillon 	 * vm_fault() has supplied us with a busied page placeholding
2069e24b12cdSMatthew Dillon 	 * the operation.  This presents a lock order reversal issue
2070e24b12cdSMatthew Dillon 	 * again i915_gem_release_mmap() for our device mutex.
2071e24b12cdSMatthew Dillon 	 *
2072e24b12cdSMatthew Dillon 	 * Deal with the problem by getting rid of the placeholder now,
2073e24b12cdSMatthew Dillon 	 * and then dealing with the potential for a new placeholder when
2074e24b12cdSMatthew Dillon 	 * we try to insert later.
2075e24b12cdSMatthew Dillon 	 */
20762854a88cSMatthew Dillon 	KKASSERT(*mres == NULL);
2077e24b12cdSMatthew Dillon 	m = NULL;
2078e24b12cdSMatthew Dillon 
207925b95970SMatthew Dillon retry:
2080ba55f2f5SFrançois Tigeot 	trace_i915_gem_object_fault(obj, page_offset, true, write);
2081ba55f2f5SFrançois Tigeot 
2082ba55f2f5SFrançois Tigeot 	/* Try to flush the object off the GPU first without holding the lock.
208371f41f3eSFrançois Tigeot 	 * Upon acquiring the lock, we will perform our sanity checks and then
2084ba55f2f5SFrançois Tigeot 	 * repeat the flush holding the lock in the normal manner to catch cases
2085ba55f2f5SFrançois Tigeot 	 * where we are gazumped.
2086ba55f2f5SFrançois Tigeot 	 */
20874be47400SFrançois Tigeot 	ret = i915_gem_object_wait(obj,
20884be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE,
20894be47400SFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
20904be47400SFrançois Tigeot 				   NULL);
2091a85cb24fSFrançois Tigeot 	if (ret)
2092a85cb24fSFrançois Tigeot 		goto err;
20934be47400SFrançois Tigeot 
20944be47400SFrançois Tigeot 	ret = i915_gem_object_pin_pages(obj);
2095ba55f2f5SFrançois Tigeot 	if (ret)
209671f41f3eSFrançois Tigeot 		goto err;
209771f41f3eSFrançois Tigeot 
209871f41f3eSFrançois Tigeot 	intel_runtime_pm_get(dev_priv);
209971f41f3eSFrançois Tigeot 
210071f41f3eSFrançois Tigeot 	ret = i915_mutex_lock_interruptible(dev);
21014be47400SFrançois Tigeot 	if (ret) {
21024be47400SFrançois Tigeot 		if (ret != -EINTR)
21034be47400SFrançois Tigeot 		    kprintf("i915: caught bug(%d) (mutex_lock_inter)\n", ret);
210471f41f3eSFrançois Tigeot 		goto err_rpm;
21054be47400SFrançois Tigeot 	}
2106ba55f2f5SFrançois Tigeot 
2107ba55f2f5SFrançois Tigeot 	/* Access to snoopable pages through the GTT is incoherent. */
21084be47400SFrançois Tigeot 	if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
21094be47400SFrançois Tigeot 		kprintf("i915: caught bug() (cache_level %d %d)\n",
21104be47400SFrançois Tigeot 			(obj->cache_level), !HAS_LLC(dev_priv));
2111ba55f2f5SFrançois Tigeot 		ret = -EFAULT;
211271f41f3eSFrançois Tigeot 		goto err_unlock;
2113ba55f2f5SFrançois Tigeot 	}
2114ba55f2f5SFrançois Tigeot 
21151e12ee3bSFrançois Tigeot 	/* If the object is smaller than a couple of partial vma, it is
21161e12ee3bSFrançois Tigeot 	 * not worth only creating a single partial vma - we may as well
21171e12ee3bSFrançois Tigeot 	 * clear enough space for the full object.
21181e12ee3bSFrançois Tigeot 	 */
21191e12ee3bSFrançois Tigeot 	flags = PIN_MAPPABLE;
21201e12ee3bSFrançois Tigeot 	if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
21211e12ee3bSFrançois Tigeot 		flags |= PIN_NONBLOCK | PIN_NONFAULT;
21221e12ee3bSFrançois Tigeot 
21231e12ee3bSFrançois Tigeot 	/* Now pin it into the GTT as needed */
21241e12ee3bSFrançois Tigeot 	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
21251e12ee3bSFrançois Tigeot 	if (IS_ERR(vma)) {
21261e12ee3bSFrançois Tigeot 		/* Use a partial view if it is bigger than available space */
2127a85cb24fSFrançois Tigeot 		struct i915_ggtt_view view =
2128a85cb24fSFrançois Tigeot 			compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
2129cfbd4176SFrançois Tigeot 
2130a85cb24fSFrançois Tigeot 		kprintf("i915_gem_fault: CHUNKING PASS\n");
21311e12ee3bSFrançois Tigeot 
21321e12ee3bSFrançois Tigeot 		/* Userspace is now writing through an untracked VMA, abandon
21331e12ee3bSFrançois Tigeot 		 * all hope that the hardware is able to track future writes.
21341e12ee3bSFrançois Tigeot 		 */
21351e12ee3bSFrançois Tigeot 		obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
21361e12ee3bSFrançois Tigeot 
21371e12ee3bSFrançois Tigeot 		vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
21381e12ee3bSFrançois Tigeot 	}
21391e12ee3bSFrançois Tigeot 	if (IS_ERR(vma)) {
21404be47400SFrançois Tigeot 		kprintf("i915: caught bug() (VMA error %ld objsize %ld)\n",
21414be47400SFrançois Tigeot 			PTR_ERR(vma), obj->base.size);
21421e12ee3bSFrançois Tigeot 		ret = PTR_ERR(vma);
214371f41f3eSFrançois Tigeot 		goto err_unlock;
21441e12ee3bSFrançois Tigeot 	}
21457f68d5c8SFrançois Tigeot 
21467f68d5c8SFrançois Tigeot 	ret = i915_gem_object_set_to_gtt_domain(obj, write);
21474be47400SFrançois Tigeot 	if (ret) {
21484be47400SFrançois Tigeot 		kprintf("i915: caught bug(%d) (set_to_gtt_dom)\n", ret);
214971f41f3eSFrançois Tigeot 		goto err_unpin;
21504be47400SFrançois Tigeot 	}
21517f68d5c8SFrançois Tigeot 
21523f2dd94aSFrançois Tigeot 	ret = i915_vma_pin_fence(vma);
21534be47400SFrançois Tigeot 	if (ret) {
21543f2dd94aSFrançois Tigeot 		kprintf("i915: caught bug(%d) (vma_pin_fence)\n", ret);
215571f41f3eSFrançois Tigeot 		goto err_unpin;
21564be47400SFrançois Tigeot 	}
21577f68d5c8SFrançois Tigeot 
21583f2dd94aSFrançois Tigeot #ifdef __DragonFly__
215925b95970SMatthew Dillon 	/*
216025b95970SMatthew Dillon 	 * Add a pip count to avoid destruction and certain other
216125b95970SMatthew Dillon 	 * complex operations (such as collapses?) while unlocked.
216225b95970SMatthew Dillon 	 */
2163e9587a4eSFrançois Tigeot 	vm_object_pip_add(vm_obj, 1);
21644be47400SFrançois Tigeot 	didref = 1;
2165a2fdbec6SFrançois Tigeot 
21669edbd4a0SFrançois Tigeot 	ret = 0;
216725b95970SMatthew Dillon 
21683f2dd94aSFrançois Tigeot #if 0
21693f2dd94aSFrançois Tigeot 	/* NO LONGER USED */
21703f2dd94aSFrançois Tigeot 	{
21713f2dd94aSFrançois Tigeot 		vm_page_t m;
21723f2dd94aSFrançois Tigeot 
21733f2dd94aSFrançois Tigeot 		m = vm_page_lookup(vm_obj, OFF_TO_IDX(offset));
21743f2dd94aSFrançois Tigeot 		if (m) {
21753f2dd94aSFrançois Tigeot 			if (vm_page_busy_try(m, false)) {
21763f2dd94aSFrançois Tigeot 				kprintf("i915_gem_fault: BUSY\n");
21773f2dd94aSFrançois Tigeot 				ret = -EINTR;
21783f2dd94aSFrançois Tigeot 				goto err_unpin;
21793f2dd94aSFrançois Tigeot 			}
21803f2dd94aSFrançois Tigeot 		}
21813f2dd94aSFrançois Tigeot 		goto have_page;
21823f2dd94aSFrançois Tigeot 	}
21833f2dd94aSFrançois Tigeot #endif
21849edbd4a0SFrançois Tigeot 
2185cfbd4176SFrançois Tigeot 	/* Finally, remap it using the new GTT offset */
21868621f407SFrançois Tigeot 	m = vm_phys_fictitious_to_vm_page(ggtt->mappable_base +
21873f2dd94aSFrançois Tigeot 			vma->node.start + offset);
21883f2dd94aSFrançois Tigeot 
2189e9587a4eSFrançois Tigeot 	if (m == NULL) {
21904be47400SFrançois Tigeot 		kprintf("i915: caught bug() (phys_fict_to_vm)\n");
2191e9587a4eSFrançois Tigeot 		ret = -EFAULT;
21923f2dd94aSFrançois Tigeot 		goto err_fence;
2193e9587a4eSFrançois Tigeot 	}
219425b95970SMatthew Dillon 	KASSERT((m->flags & PG_FICTITIOUS) != 0, ("not fictitious %p", m));
2195e9587a4eSFrançois Tigeot 	KASSERT(m->wire_count == 1, ("wire_count not 1 %p", m));
2196a2fdbec6SFrançois Tigeot 
219725b95970SMatthew Dillon 	/*
219825b95970SMatthew Dillon 	 * Try to busy the page.  Fails on non-zero return.
219925b95970SMatthew Dillon 	 */
220025b95970SMatthew Dillon 	if (vm_page_busy_try(m, false)) {
2201bc0aa189SMatthew Dillon 		kprintf("i915_gem_fault: BUSY(2)\n");
2202c0d7b1d2SFrançois Tigeot 		ret = -EINTR;
22033f2dd94aSFrançois Tigeot 		goto err_fence;
2204e9587a4eSFrançois Tigeot 	}
2205e9587a4eSFrançois Tigeot 	m->valid = VM_PAGE_BITS_ALL;
2206a2fdbec6SFrançois Tigeot 
22073f2dd94aSFrançois Tigeot #ifdef USE_INSERT
22083f2dd94aSFrançois Tigeot 	/* NO LONGER USED */
22093f2dd94aSFrançois Tigeot 	/*
22103f2dd94aSFrançois Tigeot 	 * This should always work since we already checked via a lookup
22113f2dd94aSFrançois Tigeot 	 * above.
22123f2dd94aSFrançois Tigeot 	 */
22133f2dd94aSFrançois Tigeot 	if (vm_page_insert(m, vm_obj, OFF_TO_IDX(offset)) == FALSE) {
22143f2dd94aSFrançois Tigeot 		kprintf("i915:gem_fault: page %p,%jd already in object\n",
22153f2dd94aSFrançois Tigeot 			vm_obj,
22163f2dd94aSFrançois Tigeot 			OFF_TO_IDX(offset));
22173f2dd94aSFrançois Tigeot 		vm_page_wakeup(m);
22183f2dd94aSFrançois Tigeot 		ret = -EINTR;
22193f2dd94aSFrançois Tigeot 		goto err_unpin;
22203f2dd94aSFrançois Tigeot 	}
22213f2dd94aSFrançois Tigeot have_page:
22223f2dd94aSFrançois Tigeot #endif
22233f2dd94aSFrançois Tigeot 	*mres = m;
2224ef1906eaSFrançois Tigeot 	ret = VM_PAGER_OK;
2225e9587a4eSFrançois Tigeot 
222625b95970SMatthew Dillon 	/*
222725b95970SMatthew Dillon 	 * ALTERNATIVE ERROR RETURN.
222825b95970SMatthew Dillon 	 *
222925b95970SMatthew Dillon 	 * OBJECT EXPECTED TO BE LOCKED.
223025b95970SMatthew Dillon 	 */
22313f2dd94aSFrançois Tigeot #endif
22323f2dd94aSFrançois Tigeot 
22333f2dd94aSFrançois Tigeot 	/* Mark as being mmapped into userspace for later revocation */
22343f2dd94aSFrançois Tigeot 	assert_rpm_wakelock_held(dev_priv);
22353f2dd94aSFrançois Tigeot 	if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
22363f2dd94aSFrançois Tigeot 		list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
22373f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!obj->userfault_count);
22383f2dd94aSFrançois Tigeot 
22393f2dd94aSFrançois Tigeot err_fence:
22403f2dd94aSFrançois Tigeot 	i915_vma_unpin_fence(vma);
224171f41f3eSFrançois Tigeot err_unpin:
22421e12ee3bSFrançois Tigeot 	__i915_vma_unpin(vma);
224371f41f3eSFrançois Tigeot err_unlock:
2244a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
224571f41f3eSFrançois Tigeot err_rpm:
224671f41f3eSFrançois Tigeot 	intel_runtime_pm_put(dev_priv);
22474be47400SFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
224871f41f3eSFrançois Tigeot err:
22499edbd4a0SFrançois Tigeot 	switch (ret) {
22509edbd4a0SFrançois Tigeot 	case -EIO:
225124edb884SFrançois Tigeot 		/*
225224edb884SFrançois Tigeot 		 * We eat errors when the gpu is terminally wedged to avoid
225324edb884SFrançois Tigeot 		 * userspace unduly crashing (gl has no provisions for mmaps to
225424edb884SFrançois Tigeot 		 * fail). But any other -EIO isn't ours (e.g. swap in failure)
225524edb884SFrançois Tigeot 		 * and so needs to be reported.
225624edb884SFrançois Tigeot 		 */
225724edb884SFrançois Tigeot 		if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
2258ba55f2f5SFrançois Tigeot //			ret = VM_FAULT_SIGBUS;
2259ba55f2f5SFrançois Tigeot 			break;
2260ba55f2f5SFrançois Tigeot 		}
22619edbd4a0SFrançois Tigeot 	case -EAGAIN:
2262ba55f2f5SFrançois Tigeot 		/*
2263ba55f2f5SFrançois Tigeot 		 * EAGAIN means the gpu is hung and we'll wait for the error
2264ba55f2f5SFrançois Tigeot 		 * handler to reset everything when re-faulting in
2265ba55f2f5SFrançois Tigeot 		 * i915_mutex_lock_interruptible.
2266ba55f2f5SFrançois Tigeot 		 */
2267ba55f2f5SFrançois Tigeot 	case -ERESTARTSYS:
22689edbd4a0SFrançois Tigeot 	case -EINTR:
2269a85cb24fSFrançois Tigeot #ifdef __DragonFly__
22704be47400SFrançois Tigeot 		if (didref) {
22714be47400SFrançois Tigeot 			kprintf("i915: caught bug(%d) (retry)\n", ret);
22724be47400SFrançois Tigeot 			vm_object_pip_wakeup(vm_obj);
22734be47400SFrançois Tigeot 			didref = 0;
22744be47400SFrançois Tigeot 		}
227525b95970SMatthew Dillon 		VM_OBJECT_UNLOCK(vm_obj);
227625b95970SMatthew Dillon 		int dummy;
227725b95970SMatthew Dillon 		tsleep(&dummy, 0, "delay", 1); /* XXX */
227825b95970SMatthew Dillon 		VM_OBJECT_LOCK(vm_obj);
227925b95970SMatthew Dillon 		goto retry;
22803f2dd94aSFrançois Tigeot 	case VM_PAGER_OK:
22813f2dd94aSFrançois Tigeot 		break;
2282a85cb24fSFrançois Tigeot #endif
22839edbd4a0SFrançois Tigeot 	default:
2284ba55f2f5SFrançois Tigeot 		WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
22859edbd4a0SFrançois Tigeot 		ret = VM_PAGER_ERROR;
228625b95970SMatthew Dillon 		break;
22879edbd4a0SFrançois Tigeot 	}
22889edbd4a0SFrançois Tigeot 
2289a85cb24fSFrançois Tigeot #ifdef __DragonFly__
22904be47400SFrançois Tigeot 	if (didref)
229125b95970SMatthew Dillon 		vm_object_pip_wakeup(vm_obj);
22924be47400SFrançois Tigeot 	else
22934be47400SFrançois Tigeot 		kprintf("i915: caught bug(%d)\n", ret);
22943f2dd94aSFrançois Tigeot 
22953f2dd94aSFrançois Tigeot 	up_read(&area->vm_mm->mmap_sem);
2296a85cb24fSFrançois Tigeot #endif
229725b95970SMatthew Dillon 
22989edbd4a0SFrançois Tigeot 	return ret;
22999edbd4a0SFrançois Tigeot }
23009edbd4a0SFrançois Tigeot 
23010b869d8aSFrançois Tigeot /**
2302e11a51e3SFrançois Tigeot  * i915_gem_release_mmap - remove physical page mappings
2303e11a51e3SFrançois Tigeot  * @obj: obj in question
2304e11a51e3SFrançois Tigeot  *
2305e11a51e3SFrançois Tigeot  * Preserve the reservation of the mmapping with the DRM core code, but
2306e11a51e3SFrançois Tigeot  * relinquish ownership of the pages back to the system.
2307e11a51e3SFrançois Tigeot  *
2308e11a51e3SFrançois Tigeot  * It is vital that we remove the page mapping if we have mapped a tiled
2309e11a51e3SFrançois Tigeot  * object through the GTT and then lose the fence register due to
2310e11a51e3SFrançois Tigeot  * resource pressure. Similarly if the object has been moved out of the
2311e11a51e3SFrançois Tigeot  * aperture, than pages mapped into userspace must be revoked. Removing the
2312e11a51e3SFrançois Tigeot  * mapping will then trigger a page fault on the next user access, allowing
2313e11a51e3SFrançois Tigeot  * fixup by i915_gem_fault().
2314e11a51e3SFrançois Tigeot  */
2315e11a51e3SFrançois Tigeot void
i915_gem_release_mmap(struct drm_i915_gem_object * obj)2316e11a51e3SFrançois Tigeot i915_gem_release_mmap(struct drm_i915_gem_object *obj)
2317e11a51e3SFrançois Tigeot {
23184be47400SFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
2319e11a51e3SFrançois Tigeot 
23208621f407SFrançois Tigeot 	/* Serialisation between user GTT access and our code depends upon
23218621f407SFrançois Tigeot 	 * revoking the CPU's PTE whilst the mutex is held. The next user
23228621f407SFrançois Tigeot 	 * pagefault then has to wait until we release the mutex.
23234be47400SFrançois Tigeot 	 *
23244be47400SFrançois Tigeot 	 * Note that RPM complicates somewhat by adding an additional
23254be47400SFrançois Tigeot 	 * requirement that operations to the GGTT be made holding the RPM
23264be47400SFrançois Tigeot 	 * wakeref.
23278621f407SFrançois Tigeot 	 */
23284be47400SFrançois Tigeot 	lockdep_assert_held(&i915->drm.struct_mutex);
23294be47400SFrançois Tigeot 	intel_runtime_pm_get(i915);
23308621f407SFrançois Tigeot 
23313f2dd94aSFrançois Tigeot 	if (!obj->userfault_count)
23324be47400SFrançois Tigeot 		goto out;
2333e11a51e3SFrançois Tigeot 
23343f2dd94aSFrançois Tigeot 	__i915_gem_object_release_mmap(obj);
2335e11a51e3SFrançois Tigeot 
23368621f407SFrançois Tigeot 	/* Ensure that the CPU's PTE are revoked and there are not outstanding
23378621f407SFrançois Tigeot 	 * memory transactions from userspace before we return. The TLB
23388621f407SFrançois Tigeot 	 * flushing implied above by changing the PTE above *should* be
23398621f407SFrançois Tigeot 	 * sufficient, an extra barrier here just provides us with a bit
23408621f407SFrançois Tigeot 	 * of paranoid documentation about our requirement to serialise
23418621f407SFrançois Tigeot 	 * memory writes before touching registers / GSM.
23428621f407SFrançois Tigeot 	 */
23438621f407SFrançois Tigeot 	wmb();
23448621f407SFrançois Tigeot 
23454be47400SFrançois Tigeot out:
23464be47400SFrançois Tigeot 	intel_runtime_pm_put(i915);
2347e11a51e3SFrançois Tigeot }
2348e11a51e3SFrançois Tigeot 
i915_gem_runtime_suspend(struct drm_i915_private * dev_priv)23494be47400SFrançois Tigeot void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
2350ba55f2f5SFrançois Tigeot {
23514be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj, *on;
23524be47400SFrançois Tigeot 	int i;
2353ba55f2f5SFrançois Tigeot 
23544be47400SFrançois Tigeot 	/*
23554be47400SFrançois Tigeot 	 * Only called during RPM suspend. All users of the userfault_list
23564be47400SFrançois Tigeot 	 * must be holding an RPM wakeref to ensure that this can not
23574be47400SFrançois Tigeot 	 * run concurrently with themselves (and use the struct_mutex for
23584be47400SFrançois Tigeot 	 * protection between themselves).
23594be47400SFrançois Tigeot 	 */
23604be47400SFrançois Tigeot 
23614be47400SFrançois Tigeot 	list_for_each_entry_safe(obj, on,
23623f2dd94aSFrançois Tigeot 				 &dev_priv->mm.userfault_list, userfault_link)
23633f2dd94aSFrançois Tigeot 		__i915_gem_object_release_mmap(obj);
23644be47400SFrançois Tigeot 
23654be47400SFrançois Tigeot 	/* The fence will be lost when the device powers down. If any were
23664be47400SFrançois Tigeot 	 * in use by hardware (i.e. they are pinned), we should not be powering
23674be47400SFrançois Tigeot 	 * down! All other fences will be reacquired by the user upon waking.
23684be47400SFrançois Tigeot 	 */
23694be47400SFrançois Tigeot 	for (i = 0; i < dev_priv->num_fence_regs; i++) {
23704be47400SFrançois Tigeot 		struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
23714be47400SFrançois Tigeot 
23724be47400SFrançois Tigeot 		/* Ideally we want to assert that the fence register is not
23734be47400SFrançois Tigeot 		 * live at this point (i.e. that no piece of code will be
23744be47400SFrançois Tigeot 		 * trying to write through fence + GTT, as that both violates
23754be47400SFrançois Tigeot 		 * our tracking of activity and associated locking/barriers,
23764be47400SFrançois Tigeot 		 * but also is illegal given that the hw is powered down).
23774be47400SFrançois Tigeot 		 *
23784be47400SFrançois Tigeot 		 * Previously we used reg->pin_count as a "liveness" indicator.
23794be47400SFrançois Tigeot 		 * That is not sufficient, and we need a more fine-grained
23804be47400SFrançois Tigeot 		 * tool if we want to have a sanity check here.
23814be47400SFrançois Tigeot 		 */
23824be47400SFrançois Tigeot 
23834be47400SFrançois Tigeot 		if (!reg->vma)
23844be47400SFrançois Tigeot 			continue;
23854be47400SFrançois Tigeot 
23863f2dd94aSFrançois Tigeot 		GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
23874be47400SFrançois Tigeot 		reg->dirty = true;
23884be47400SFrançois Tigeot 	}
2389ba55f2f5SFrançois Tigeot }
2390ba55f2f5SFrançois Tigeot 
i915_gem_object_create_mmap_offset(struct drm_i915_gem_object * obj)23919edbd4a0SFrançois Tigeot static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
23929edbd4a0SFrançois Tigeot {
2393303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
239471f41f3eSFrançois Tigeot 	int err;
23959edbd4a0SFrançois Tigeot 
239671f41f3eSFrançois Tigeot 	err = drm_gem_create_mmap_offset(&obj->base);
2397a85cb24fSFrançois Tigeot 	if (likely(!err))
239871f41f3eSFrançois Tigeot 		return 0;
23999edbd4a0SFrançois Tigeot 
2400a85cb24fSFrançois Tigeot 	/* Attempt to reap some mmap space from dead objects */
2401a85cb24fSFrançois Tigeot 	do {
24021e12ee3bSFrançois Tigeot 		err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
240371f41f3eSFrançois Tigeot 		if (err)
2404a85cb24fSFrançois Tigeot 			break;
24059edbd4a0SFrançois Tigeot 
2406a85cb24fSFrançois Tigeot 		i915_gem_drain_freed_objects(dev_priv);
240771f41f3eSFrançois Tigeot 		err = drm_gem_create_mmap_offset(&obj->base);
2408a85cb24fSFrançois Tigeot 		if (!err)
2409a85cb24fSFrançois Tigeot 			break;
2410a85cb24fSFrançois Tigeot 
2411a85cb24fSFrançois Tigeot 	} while (flush_delayed_work(&dev_priv->gt.retire_work));
24129edbd4a0SFrançois Tigeot 
241371f41f3eSFrançois Tigeot 	return err;
24149edbd4a0SFrançois Tigeot }
24159edbd4a0SFrançois Tigeot 
241687df8fc6SFrançois Tigeot #if 0
24179edbd4a0SFrançois Tigeot static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
24189edbd4a0SFrançois Tigeot {
24199edbd4a0SFrançois Tigeot 	drm_gem_free_mmap_offset(&obj->base);
24209edbd4a0SFrançois Tigeot }
242187df8fc6SFrançois Tigeot #endif
24229edbd4a0SFrançois Tigeot 
2423e11a51e3SFrançois Tigeot int
i915_gem_mmap_gtt(struct drm_file * file,struct drm_device * dev,uint32_t handle,uint64_t * offset)2424e11a51e3SFrançois Tigeot i915_gem_mmap_gtt(struct drm_file *file,
2425e11a51e3SFrançois Tigeot 		  struct drm_device *dev,
2426e11a51e3SFrançois Tigeot 		  uint32_t handle,
2427e11a51e3SFrançois Tigeot 		  uint64_t *offset)
2428e11a51e3SFrançois Tigeot {
2429e11a51e3SFrançois Tigeot 	struct drm_i915_gem_object *obj;
2430e11a51e3SFrançois Tigeot 	int ret;
2431e11a51e3SFrançois Tigeot 
243287df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file, handle);
243371f41f3eSFrançois Tigeot 	if (!obj)
243471f41f3eSFrançois Tigeot 		return -ENOENT;
2435e11a51e3SFrançois Tigeot 
24369edbd4a0SFrançois Tigeot 	ret = i915_gem_object_create_mmap_offset(obj);
243771f41f3eSFrançois Tigeot 	if (ret == 0)
2438e11a51e3SFrançois Tigeot 		*offset = DRM_GEM_MAPPING_OFF(obj->base.map_list.key) |
2439e11a51e3SFrançois Tigeot 		    DRM_GEM_MAPPING_KEY;
24409edbd4a0SFrançois Tigeot 
24414be47400SFrançois Tigeot 	i915_gem_object_put(obj);
2442d2557f23SFrançois Tigeot 	return ret;
2443e11a51e3SFrançois Tigeot }
2444e11a51e3SFrançois Tigeot 
2445e11a51e3SFrançois Tigeot /**
2446e11a51e3SFrançois Tigeot  * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2447e11a51e3SFrançois Tigeot  * @dev: DRM device
2448e11a51e3SFrançois Tigeot  * @data: GTT mapping ioctl data
2449e11a51e3SFrançois Tigeot  * @file: GEM object info
2450e11a51e3SFrançois Tigeot  *
2451e11a51e3SFrançois Tigeot  * Simply returns the fake offset to userspace so it can mmap it.
2452e11a51e3SFrançois Tigeot  * The mmap call will end up in drm_gem_mmap(), which will set things
2453e11a51e3SFrançois Tigeot  * up so we can get faults in the handler above.
2454e11a51e3SFrançois Tigeot  *
2455e11a51e3SFrançois Tigeot  * The fault handler will take care of binding the object into the GTT
2456e11a51e3SFrançois Tigeot  * (since it may have been evicted to make room for something), allocating
2457e11a51e3SFrançois Tigeot  * a fence register, and mapping the appropriate aperture address into
2458e11a51e3SFrançois Tigeot  * userspace.
2459e11a51e3SFrançois Tigeot  */
2460e11a51e3SFrançois Tigeot int
i915_gem_mmap_gtt_ioctl(struct drm_device * dev,void * data,struct drm_file * file)2461e11a51e3SFrançois Tigeot i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2462e11a51e3SFrançois Tigeot 			struct drm_file *file)
2463e11a51e3SFrançois Tigeot {
2464686a02f1SFrançois Tigeot 	struct drm_i915_gem_mmap_gtt *args = data;
2465e11a51e3SFrançois Tigeot 
2466f77dbd6cSFrançois Tigeot 	return i915_gem_mmap_gtt(file, dev, args->handle, (uint64_t *)&args->offset);
2467e11a51e3SFrançois Tigeot }
2468e11a51e3SFrançois Tigeot 
2469e11a51e3SFrançois Tigeot /* Immediately discard the backing storage */
2470e11a51e3SFrançois Tigeot static void
i915_gem_object_truncate(struct drm_i915_gem_object * obj)2471e11a51e3SFrançois Tigeot i915_gem_object_truncate(struct drm_i915_gem_object *obj)
2472e11a51e3SFrançois Tigeot {
2473bf017597SFrançois Tigeot 	vm_object_t vm_obj = obj->base.filp;
2474e11a51e3SFrançois Tigeot 
2475bf017597SFrançois Tigeot 	if (obj->base.filp == NULL)
2476bf017597SFrançois Tigeot 		return;
2477bf017597SFrançois Tigeot 
2478e11a51e3SFrançois Tigeot 	VM_OBJECT_LOCK(vm_obj);
2479e11a51e3SFrançois Tigeot 	vm_object_page_remove(vm_obj, 0, 0, false);
2480e11a51e3SFrançois Tigeot 	VM_OBJECT_UNLOCK(vm_obj);
2481b15f8132SFrançois Tigeot 
2482bf017597SFrançois Tigeot 	/* Our goal here is to return as much of the memory as
2483bf017597SFrançois Tigeot 	 * is possible back to the system as we are called from OOM.
2484bf017597SFrançois Tigeot 	 * To do this we must instruct the shmfs to drop all of its
2485bf017597SFrançois Tigeot 	 * backing pages, *now*.
2486bf017597SFrançois Tigeot 	 */
248787df8fc6SFrançois Tigeot #if 0
248887df8fc6SFrançois Tigeot 	shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
248987df8fc6SFrançois Tigeot #endif
24904be47400SFrançois Tigeot 	obj->mm.madv = __I915_MADV_PURGED;
24914be47400SFrançois Tigeot 	obj->mm.pages = ERR_PTR(-EFAULT);
2492e11a51e3SFrançois Tigeot }
2493e11a51e3SFrançois Tigeot 
2494ba55f2f5SFrançois Tigeot /* Try to discard unwanted pages */
__i915_gem_object_invalidate(struct drm_i915_gem_object * obj)24954be47400SFrançois Tigeot void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
2496e11a51e3SFrançois Tigeot {
2497ba55f2f5SFrançois Tigeot #if 0
2498ba55f2f5SFrançois Tigeot 	struct address_space *mapping;
2499ba55f2f5SFrançois Tigeot #endif
2500ba55f2f5SFrançois Tigeot 
25014be47400SFrançois Tigeot 	lockdep_assert_held(&obj->mm.lock);
25023f2dd94aSFrançois Tigeot 	GEM_BUG_ON(i915_gem_object_has_pages(obj));
25034be47400SFrançois Tigeot 
25044be47400SFrançois Tigeot 	switch (obj->mm.madv) {
2505ba55f2f5SFrançois Tigeot 	case I915_MADV_DONTNEED:
2506ba55f2f5SFrançois Tigeot 		i915_gem_object_truncate(obj);
2507ba55f2f5SFrançois Tigeot 	case __I915_MADV_PURGED:
2508ba55f2f5SFrançois Tigeot 		return;
2509ba55f2f5SFrançois Tigeot 	}
2510ba55f2f5SFrançois Tigeot 
2511ba55f2f5SFrançois Tigeot 	if (obj->base.filp == NULL)
2512ba55f2f5SFrançois Tigeot 		return;
2513ba55f2f5SFrançois Tigeot 
25147ca4ece0SFrançois Tigeot #if 0
2515a85cb24fSFrançois Tigeot 	mapping = obj->base.filp->f_mapping,
25164be47400SFrançois Tigeot 	invalidate_mapping_pages(mapping, 0, (loff_t)-1);
25174be47400SFrançois Tigeot #endif
2518a85cb24fSFrançois Tigeot 	invalidate_mapping_pages(obj->base.filp, 0, (loff_t)-1);
2519e11a51e3SFrançois Tigeot }
2520e11a51e3SFrançois Tigeot 
2521e11a51e3SFrançois Tigeot static void
i915_gem_object_put_pages_gtt(struct drm_i915_gem_object * obj,struct sg_table * pages)25224be47400SFrançois Tigeot i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
25234be47400SFrançois Tigeot 			      struct sg_table *pages)
2524e11a51e3SFrançois Tigeot {
25251487f786SFrançois Tigeot 	struct sgt_iter sgt_iter;
25261487f786SFrançois Tigeot 	struct page *page;
2527e11a51e3SFrançois Tigeot 
25284be47400SFrançois Tigeot 	__i915_gem_object_release_shmem(obj, pages, true);
2529c16fa8beSFrançois Tigeot 
25304be47400SFrançois Tigeot 	i915_gem_gtt_finish_pages(obj, pages);
253119c468b4SFrançois Tigeot 
2532c16fa8beSFrançois Tigeot 	if (i915_gem_object_needs_bit17_swizzle(obj))
25334be47400SFrançois Tigeot 		i915_gem_object_save_bit_17_swizzle(obj, pages);
2534c16fa8beSFrançois Tigeot 
25354be47400SFrançois Tigeot 	for_each_sgt_page(page, sgt_iter, pages) {
25364be47400SFrançois Tigeot 		if (obj->mm.dirty)
25379edbd4a0SFrançois Tigeot 			set_page_dirty(page);
2538c16fa8beSFrançois Tigeot 
25394be47400SFrançois Tigeot 		if (obj->mm.madv == I915_MADV_WILLNEED)
25409edbd4a0SFrançois Tigeot 			mark_page_accessed(page);
2541c16fa8beSFrançois Tigeot 
2542a85cb24fSFrançois Tigeot 		put_page(page);
2543e11a51e3SFrançois Tigeot 	}
25444be47400SFrançois Tigeot 	obj->mm.dirty = false;
2545c16fa8beSFrançois Tigeot 
25464be47400SFrançois Tigeot 	sg_free_table(pages);
25474be47400SFrançois Tigeot 	kfree(pages);
2548e11a51e3SFrançois Tigeot }
2549e11a51e3SFrançois Tigeot 
__i915_gem_object_reset_page_iter(struct drm_i915_gem_object * obj)25504be47400SFrançois Tigeot static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2551a2fdbec6SFrançois Tigeot {
25524be47400SFrançois Tigeot 	struct radix_tree_iter iter;
25533f2dd94aSFrançois Tigeot 	void __rcu **slot;
2554a2fdbec6SFrançois Tigeot 
25553f2dd94aSFrançois Tigeot 	rcu_read_lock();
25564be47400SFrançois Tigeot 	radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
25574be47400SFrançois Tigeot 		radix_tree_delete(&obj->mm.get_page.radix, iter.index);
25583f2dd94aSFrançois Tigeot 	rcu_read_unlock();
25594be47400SFrançois Tigeot }
2560a2fdbec6SFrançois Tigeot 
__i915_gem_object_put_pages(struct drm_i915_gem_object * obj,enum i915_mm_subclass subclass)25614be47400SFrançois Tigeot void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
25624be47400SFrançois Tigeot 				 enum i915_mm_subclass subclass)
25634be47400SFrançois Tigeot {
25643f2dd94aSFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
25654be47400SFrançois Tigeot 	struct sg_table *pages;
25664be47400SFrançois Tigeot 
25674be47400SFrançois Tigeot 	if (i915_gem_object_has_pinned_pages(obj))
25684be47400SFrançois Tigeot 		return;
2569a2fdbec6SFrançois Tigeot 
257071f41f3eSFrançois Tigeot 	GEM_BUG_ON(obj->bind_count);
25713f2dd94aSFrançois Tigeot 	if (!i915_gem_object_has_pages(obj))
25724be47400SFrançois Tigeot 		return;
25734be47400SFrançois Tigeot 
25744be47400SFrançois Tigeot 	/* May be called by shrinker from within get_pages() (on another bo) */
25754be47400SFrançois Tigeot 	mutex_lock_nested(&obj->mm.lock, subclass);
25764be47400SFrançois Tigeot 	if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
25774be47400SFrançois Tigeot 		goto unlock;
25789edbd4a0SFrançois Tigeot 
2579a2fdbec6SFrançois Tigeot 	/* ->put_pages might need to allocate memory for the bit17 swizzle
2580a2fdbec6SFrançois Tigeot 	 * array, hence protect them from being reaped by removing them from gtt
2581a2fdbec6SFrançois Tigeot 	 * lists early. */
25824be47400SFrançois Tigeot 	pages = fetch_and_zero(&obj->mm.pages);
25834be47400SFrançois Tigeot 	GEM_BUG_ON(!pages);
2584a2fdbec6SFrançois Tigeot 
25853f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.obj_lock, LK_EXCLUSIVE);
25863f2dd94aSFrançois Tigeot 	list_del(&obj->mm.link);
25873f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.obj_lock, LK_RELEASE);
25883f2dd94aSFrançois Tigeot 
25894be47400SFrançois Tigeot 	if (obj->mm.mapping) {
25901e12ee3bSFrançois Tigeot 		void *ptr;
25911e12ee3bSFrançois Tigeot 
25923f2dd94aSFrançois Tigeot 		ptr = page_mask_bits(obj->mm.mapping);
25931e12ee3bSFrançois Tigeot 		if (is_vmalloc_addr(ptr))
25941e12ee3bSFrançois Tigeot 			vunmap(ptr);
25958621f407SFrançois Tigeot 		else
25961e12ee3bSFrançois Tigeot 			kunmap(kmap_to_page(ptr));
25971e12ee3bSFrançois Tigeot 
25984be47400SFrançois Tigeot 		obj->mm.mapping = NULL;
25998621f407SFrançois Tigeot 	}
26008621f407SFrançois Tigeot 
26014be47400SFrançois Tigeot 	__i915_gem_object_reset_page_iter(obj);
2602a2fdbec6SFrançois Tigeot 
26034be47400SFrançois Tigeot 	if (!IS_ERR(pages))
26044be47400SFrançois Tigeot 		obj->ops->put_pages(obj, pages);
2605a2fdbec6SFrançois Tigeot 
26063f2dd94aSFrançois Tigeot 	obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
26073f2dd94aSFrançois Tigeot 
26084be47400SFrançois Tigeot unlock:
26094be47400SFrançois Tigeot 	mutex_unlock(&obj->mm.lock);
2610a2fdbec6SFrançois Tigeot }
2611a2fdbec6SFrançois Tigeot 
i915_sg_trim(struct sg_table * orig_st)2612a85cb24fSFrançois Tigeot static bool i915_sg_trim(struct sg_table *orig_st)
26131e12ee3bSFrançois Tigeot {
26144be47400SFrançois Tigeot 	struct sg_table new_st;
26154be47400SFrançois Tigeot 	struct scatterlist *sg, *new_sg;
26164be47400SFrançois Tigeot 	unsigned int i;
26174be47400SFrançois Tigeot 
26184be47400SFrançois Tigeot 	if (orig_st->nents == orig_st->orig_nents)
2619a85cb24fSFrançois Tigeot 		return false;
26204be47400SFrançois Tigeot 
26214be47400SFrançois Tigeot 	if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
2622a85cb24fSFrançois Tigeot 		return false;
26234be47400SFrançois Tigeot 
26244be47400SFrançois Tigeot 	new_sg = new_st.sgl;
26254be47400SFrançois Tigeot 	for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
26264be47400SFrançois Tigeot 		sg_set_page(new_sg, sg_page(sg), sg->length, 0);
26274be47400SFrançois Tigeot 		/* called before being DMA mapped, no need to copy sg->dma_* */
26284be47400SFrançois Tigeot 		new_sg = sg_next(new_sg);
26291e12ee3bSFrançois Tigeot 	}
2630a85cb24fSFrançois Tigeot 	GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
26311e12ee3bSFrançois Tigeot 
26324be47400SFrançois Tigeot 	sg_free_table(orig_st);
26334be47400SFrançois Tigeot 
26344be47400SFrançois Tigeot 	*orig_st = new_st;
2635a85cb24fSFrançois Tigeot 	return true;
26364be47400SFrançois Tigeot }
26374be47400SFrançois Tigeot 
i915_gem_object_get_pages_gtt(struct drm_i915_gem_object * obj)26383f2dd94aSFrançois Tigeot static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
2639e11a51e3SFrançois Tigeot {
2640303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
26414be47400SFrançois Tigeot 	const unsigned long page_count = obj->base.size / PAGE_SIZE;
26424be47400SFrançois Tigeot 	unsigned long i;
2643a85cb24fSFrançois Tigeot 	struct vm_object *mapping;
26447ec9f8e5SFrançois Tigeot 	struct sg_table *st;
26457ec9f8e5SFrançois Tigeot 	struct scatterlist *sg;
26461487f786SFrançois Tigeot 	struct sgt_iter sgt_iter;
2647f0bba3d1SFrançois Tigeot 	struct page *page;
26487ec9f8e5SFrançois Tigeot 	unsigned long last_pfn = 0;	/* suppress gcc warning */
26493f2dd94aSFrançois Tigeot 	unsigned int max_segment = i915_sg_segment_size();
26503f2dd94aSFrançois Tigeot 	unsigned int sg_page_sizes;
2651a85cb24fSFrançois Tigeot 	gfp_t noreclaim;
26527ec9f8e5SFrançois Tigeot 	int ret;
2653e11a51e3SFrançois Tigeot 
26549edbd4a0SFrançois Tigeot 	/* Assert that the object is not currently in any GPU domain. As it
26559edbd4a0SFrançois Tigeot 	 * wasn't in the GTT, there shouldn't be any way it could have been in
26569edbd4a0SFrançois Tigeot 	 * a GPU cache
26579edbd4a0SFrançois Tigeot 	 */
26584be47400SFrançois Tigeot 	GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
26594be47400SFrançois Tigeot 	GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
26609edbd4a0SFrançois Tigeot 
26611487f786SFrançois Tigeot 	st = kmalloc(sizeof(*st), M_DRM, GFP_KERNEL);
26627ec9f8e5SFrançois Tigeot 	if (st == NULL)
26633f2dd94aSFrançois Tigeot 		return -ENOMEM;
26647ec9f8e5SFrançois Tigeot 
26654be47400SFrançois Tigeot rebuild_st:
26667ec9f8e5SFrançois Tigeot 	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
26677ec9f8e5SFrançois Tigeot 		kfree(st);
26683f2dd94aSFrançois Tigeot 		return -ENOMEM;
26697ec9f8e5SFrançois Tigeot 	}
267056c606a8SFrançois Tigeot 
26719edbd4a0SFrançois Tigeot 	/* Get the list of pages out of our struct file.  They'll be pinned
26729edbd4a0SFrançois Tigeot 	 * at this point until we release them.
26739edbd4a0SFrançois Tigeot 	 *
26749edbd4a0SFrançois Tigeot 	 * Fail silently without starting the shrinker
26759edbd4a0SFrançois Tigeot 	 */
2676a85cb24fSFrançois Tigeot #ifdef __DragonFly__
2677a85cb24fSFrançois Tigeot 	mapping = obj->base.filp;
2678a85cb24fSFrançois Tigeot 	VM_OBJECT_LOCK(mapping);
2679a85cb24fSFrançois Tigeot #endif
2680a85cb24fSFrançois Tigeot 	noreclaim = mapping_gfp_constraint(mapping,
2681a85cb24fSFrançois Tigeot 					   ~(__GFP_IO | __GFP_RECLAIM));
2682a85cb24fSFrançois Tigeot 	noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2683a85cb24fSFrançois Tigeot 
26847ec9f8e5SFrançois Tigeot 	sg = st->sgl;
26857ec9f8e5SFrançois Tigeot 	st->nents = 0;
26863f2dd94aSFrançois Tigeot 	sg_page_sizes = 0;
2687e11a51e3SFrançois Tigeot 	for (i = 0; i < page_count; i++) {
2688a85cb24fSFrançois Tigeot 		const unsigned int shrink[] = {
2689a85cb24fSFrançois Tigeot 			I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2690a85cb24fSFrançois Tigeot 			0,
2691a85cb24fSFrançois Tigeot 		}, *s = shrink;
2692a85cb24fSFrançois Tigeot 		gfp_t gfp = noreclaim;
2693a85cb24fSFrançois Tigeot 
2694a85cb24fSFrançois Tigeot 		do {
2695a85cb24fSFrançois Tigeot 			page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2696a85cb24fSFrançois Tigeot 			if (likely(!IS_ERR(page)))
2697a85cb24fSFrançois Tigeot 				break;
2698a85cb24fSFrançois Tigeot 
2699a85cb24fSFrançois Tigeot 			if (!*s) {
270019c468b4SFrançois Tigeot 				ret = PTR_ERR(page);
27014be47400SFrançois Tigeot 				goto err_sg;
2702a2fdbec6SFrançois Tigeot 			}
2703a85cb24fSFrançois Tigeot 
27043f2dd94aSFrançois Tigeot 			i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
2705a85cb24fSFrançois Tigeot 			cond_resched();
2706a85cb24fSFrançois Tigeot 
2707a85cb24fSFrançois Tigeot 			/* We've tried hard to allocate the memory by reaping
2708a85cb24fSFrançois Tigeot 			 * our own buffer, now let the real VM do its job and
2709a85cb24fSFrançois Tigeot 			 * go down in flames if truly OOM.
2710a85cb24fSFrançois Tigeot 			 *
2711a85cb24fSFrançois Tigeot 			 * However, since graphics tend to be disposable,
2712a85cb24fSFrançois Tigeot 			 * defer the oom here by reporting the ENOMEM back
2713a85cb24fSFrançois Tigeot 			 * to userspace.
2714a85cb24fSFrançois Tigeot 			 */
2715a85cb24fSFrançois Tigeot 			if (!*s) {
2716a85cb24fSFrançois Tigeot 				/* reclaim and warn, but no oom */
2717a85cb24fSFrançois Tigeot 				gfp = mapping_gfp_mask(mapping);
2718a85cb24fSFrançois Tigeot 
2719a85cb24fSFrançois Tigeot 				/* Our bo are always dirty and so we require
2720a85cb24fSFrançois Tigeot 				 * kswapd to reclaim our pages (direct reclaim
2721a85cb24fSFrançois Tigeot 				 * does not effectively begin pageout of our
2722a85cb24fSFrançois Tigeot 				 * buffers on its own). However, direct reclaim
2723a85cb24fSFrançois Tigeot 				 * only waits for kswapd when under allocation
2724a85cb24fSFrançois Tigeot 				 * congestion. So as a result __GFP_RECLAIM is
2725a85cb24fSFrançois Tigeot 				 * unreliable and fails to actually reclaim our
2726a85cb24fSFrançois Tigeot 				 * dirty pages -- unless you try over and over
2727a85cb24fSFrançois Tigeot 				 * again with !__GFP_NORETRY. However, we still
2728a85cb24fSFrançois Tigeot 				 * want to fail this allocation rather than
2729a85cb24fSFrançois Tigeot 				 * trigger the out-of-memory killer and for
27303f2dd94aSFrançois Tigeot 				 * this we want __GFP_RETRY_MAYFAIL.
2731a85cb24fSFrançois Tigeot 				 */
27323f2dd94aSFrançois Tigeot 				gfp |= __GFP_RETRY_MAYFAIL;
273319c468b4SFrançois Tigeot 			}
2734a85cb24fSFrançois Tigeot 		} while (1);
2735a85cb24fSFrançois Tigeot 
27361e12ee3bSFrançois Tigeot 		if (!i ||
27371e12ee3bSFrançois Tigeot 		    sg->length >= max_segment ||
27381e12ee3bSFrançois Tigeot 		    page_to_pfn(page) != last_pfn + 1) {
27393f2dd94aSFrançois Tigeot 			if (i) {
27403f2dd94aSFrançois Tigeot 				sg_page_sizes |= sg->length;
27417ec9f8e5SFrançois Tigeot 				sg = sg_next(sg);
27423f2dd94aSFrançois Tigeot 			}
27437ec9f8e5SFrançois Tigeot 			st->nents++;
27447ec9f8e5SFrançois Tigeot 			sg_set_page(sg, page, PAGE_SIZE, 0);
27457ec9f8e5SFrançois Tigeot 		} else {
27467ec9f8e5SFrançois Tigeot 			sg->length += PAGE_SIZE;
27477ec9f8e5SFrançois Tigeot 		}
27487ec9f8e5SFrançois Tigeot 		last_pfn = page_to_pfn(page);
27497ec9f8e5SFrançois Tigeot 
27507ec9f8e5SFrançois Tigeot 		/* Check that the i965g/gm workaround works. */
2751a85cb24fSFrançois Tigeot 		WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
2752e11a51e3SFrançois Tigeot 	}
27533f2dd94aSFrançois Tigeot 	if (sg) { /* loop terminated early; short sg table */
27543f2dd94aSFrançois Tigeot 		sg_page_sizes |= sg->length;
27557ec9f8e5SFrançois Tigeot 		sg_mark_end(sg);
27563f2dd94aSFrançois Tigeot 	}
2757a85cb24fSFrançois Tigeot #ifdef __DragonFly__
2758a85cb24fSFrançois Tigeot 	VM_OBJECT_UNLOCK(mapping);
2759a85cb24fSFrançois Tigeot #endif
27609edbd4a0SFrançois Tigeot 
27614be47400SFrançois Tigeot 	/* Trim unused sg entries to avoid wasting memory. */
27624be47400SFrançois Tigeot 	i915_sg_trim(st);
27634be47400SFrançois Tigeot 
27644be47400SFrançois Tigeot 	ret = i915_gem_gtt_prepare_pages(obj, st);
27654be47400SFrançois Tigeot 	if (ret) {
27664be47400SFrançois Tigeot 		/* DMA remapping failed? One possible cause is that
27674be47400SFrançois Tigeot 		 * it could not reserve enough large entries, asking
27684be47400SFrançois Tigeot 		 * for PAGE_SIZE chunks instead may be helpful.
27694be47400SFrançois Tigeot 		 */
27704be47400SFrançois Tigeot 		if (max_segment > PAGE_SIZE) {
27714be47400SFrançois Tigeot 			for_each_sgt_page(page, sgt_iter, st)
27724be47400SFrançois Tigeot 				put_page(page);
27734be47400SFrançois Tigeot 			sg_free_table(st);
27744be47400SFrançois Tigeot 
27754be47400SFrançois Tigeot 			max_segment = PAGE_SIZE;
27764be47400SFrançois Tigeot 			goto rebuild_st;
27774be47400SFrançois Tigeot 		} else {
27784be47400SFrançois Tigeot 			dev_warn(&dev_priv->drm.pdev->dev,
27794be47400SFrançois Tigeot 				 "Failed to DMA remap %lu pages\n",
27804be47400SFrançois Tigeot 				 page_count);
278119c468b4SFrançois Tigeot 			goto err_pages;
27824be47400SFrançois Tigeot 		}
27834be47400SFrançois Tigeot 	}
278419c468b4SFrançois Tigeot 
2785e11a51e3SFrançois Tigeot 	if (i915_gem_object_needs_bit17_swizzle(obj))
27864be47400SFrançois Tigeot 		i915_gem_object_do_bit_17_swizzle(obj, st);
2787e11a51e3SFrançois Tigeot 
27883f2dd94aSFrançois Tigeot 	__i915_gem_object_set_pages(obj, st, sg_page_sizes);
27893f2dd94aSFrançois Tigeot 
27903f2dd94aSFrançois Tigeot 	return 0;
27912c9916cdSFrançois Tigeot 
27924be47400SFrançois Tigeot err_sg:
279371f41f3eSFrançois Tigeot 	sg_mark_end(sg);
27944be47400SFrançois Tigeot err_pages:
27951487f786SFrançois Tigeot 	for_each_sgt_page(page, sgt_iter, st)
2796a85cb24fSFrançois Tigeot 		put_page(page);
2797a85cb24fSFrançois Tigeot #ifdef __DragonFly__
2798a85cb24fSFrançois Tigeot 	VM_OBJECT_UNLOCK(mapping);
2799a85cb24fSFrançois Tigeot #endif
28007ec9f8e5SFrançois Tigeot 	sg_free_table(st);
28017ec9f8e5SFrançois Tigeot 	kfree(st);
280219c468b4SFrançois Tigeot 
280319c468b4SFrançois Tigeot 	/* shmemfs first checks if there is enough memory to allocate the page
280419c468b4SFrançois Tigeot 	 * and reports ENOSPC should there be insufficient, along with the usual
280519c468b4SFrançois Tigeot 	 * ENOMEM for a genuine allocation failure.
280619c468b4SFrançois Tigeot 	 *
280719c468b4SFrançois Tigeot 	 * We use ENOSPC in our driver to mean that we have run out of aperture
280819c468b4SFrançois Tigeot 	 * space and so want to translate the error from shmemfs back to our
280919c468b4SFrançois Tigeot 	 * usual understanding of ENOMEM.
281019c468b4SFrançois Tigeot 	 */
281119c468b4SFrançois Tigeot 	if (ret == -ENOSPC)
281219c468b4SFrançois Tigeot 		ret = -ENOMEM;
281319c468b4SFrançois Tigeot 
28143f2dd94aSFrançois Tigeot 	return ret;
2815e11a51e3SFrançois Tigeot }
2816e11a51e3SFrançois Tigeot 
__i915_gem_object_set_pages(struct drm_i915_gem_object * obj,struct sg_table * pages,unsigned int sg_page_sizes)28174be47400SFrançois Tigeot void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
28183f2dd94aSFrançois Tigeot 				 struct sg_table *pages,
28193f2dd94aSFrançois Tigeot 				 unsigned int sg_page_sizes)
2820a2fdbec6SFrançois Tigeot {
28213f2dd94aSFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
28223f2dd94aSFrançois Tigeot 	unsigned long supported = INTEL_INFO(i915)->page_sizes;
28233f2dd94aSFrançois Tigeot 	int i;
28243f2dd94aSFrançois Tigeot 
28254be47400SFrançois Tigeot 	lockdep_assert_held(&obj->mm.lock);
2826a2fdbec6SFrançois Tigeot 
28274be47400SFrançois Tigeot 	obj->mm.get_page.sg_pos = pages->sgl;
28284be47400SFrançois Tigeot 	obj->mm.get_page.sg_idx = 0;
2829a2fdbec6SFrançois Tigeot 
28304be47400SFrançois Tigeot 	obj->mm.pages = pages;
28314be47400SFrançois Tigeot 
28324be47400SFrançois Tigeot 	if (i915_gem_object_is_tiled(obj) &&
28333f2dd94aSFrançois Tigeot 	    i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
28344be47400SFrançois Tigeot 		GEM_BUG_ON(obj->mm.quirked);
28354be47400SFrançois Tigeot 		__i915_gem_object_pin_pages(obj);
28364be47400SFrançois Tigeot 		obj->mm.quirked = true;
28374be47400SFrançois Tigeot 	}
28383f2dd94aSFrançois Tigeot 
28393f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!sg_page_sizes);
28403f2dd94aSFrançois Tigeot 	obj->mm.page_sizes.phys = sg_page_sizes;
28413f2dd94aSFrançois Tigeot 
28423f2dd94aSFrançois Tigeot 	/*
28433f2dd94aSFrançois Tigeot 	 * Calculate the supported page-sizes which fit into the given
28443f2dd94aSFrançois Tigeot 	 * sg_page_sizes. This will give us the page-sizes which we may be able
28453f2dd94aSFrançois Tigeot 	 * to use opportunistically when later inserting into the GTT. For
28463f2dd94aSFrançois Tigeot 	 * example if phys=2G, then in theory we should be able to use 1G, 2M,
28473f2dd94aSFrançois Tigeot 	 * 64K or 4K pages, although in practice this will depend on a number of
28483f2dd94aSFrançois Tigeot 	 * other factors.
28493f2dd94aSFrançois Tigeot 	 */
28503f2dd94aSFrançois Tigeot 	obj->mm.page_sizes.sg = 0;
28513f2dd94aSFrançois Tigeot 	for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
28523f2dd94aSFrançois Tigeot 		if (obj->mm.page_sizes.phys & ~0u << i)
28533f2dd94aSFrançois Tigeot 			obj->mm.page_sizes.sg |= BIT(i);
28543f2dd94aSFrançois Tigeot 	}
28553f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
28563f2dd94aSFrançois Tigeot 
28573f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.obj_lock, LK_EXCLUSIVE);
28583f2dd94aSFrançois Tigeot 	list_add(&obj->mm.link, &i915->mm.unbound_list);
28593f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.obj_lock, LK_RELEASE);
28604be47400SFrançois Tigeot }
28614be47400SFrançois Tigeot 
____i915_gem_object_get_pages(struct drm_i915_gem_object * obj)28624be47400SFrançois Tigeot static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
28634be47400SFrançois Tigeot {
28643f2dd94aSFrançois Tigeot 	int err;
28654be47400SFrançois Tigeot 
28664be47400SFrançois Tigeot 	if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2867ba55f2f5SFrançois Tigeot 		DRM_DEBUG("Attempting to obtain a purgeable object\n");
2868ba55f2f5SFrançois Tigeot 		return -EFAULT;
2869a2fdbec6SFrançois Tigeot 	}
2870a2fdbec6SFrançois Tigeot 
28713f2dd94aSFrançois Tigeot 	err = obj->ops->get_pages(obj);
28723f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!err && IS_ERR_OR_NULL(obj->mm.pages));
2873a2fdbec6SFrançois Tigeot 
28743f2dd94aSFrançois Tigeot 	return err;
2875a2fdbec6SFrançois Tigeot }
2876a2fdbec6SFrançois Tigeot 
28774be47400SFrançois Tigeot /* Ensure that the associated pages are gathered from the backing storage
28784be47400SFrançois Tigeot  * and pinned into our object. i915_gem_object_pin_pages() may be called
28794be47400SFrançois Tigeot  * multiple times before they are released by a single call to
28804be47400SFrançois Tigeot  * i915_gem_object_unpin_pages() - once the pages are no longer referenced
28814be47400SFrançois Tigeot  * either as a result of memory pressure (reaping pages under the shrinker)
28824be47400SFrançois Tigeot  * or as the object is itself released.
28834be47400SFrançois Tigeot  */
__i915_gem_object_get_pages(struct drm_i915_gem_object * obj)28844be47400SFrançois Tigeot int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
28854be47400SFrançois Tigeot {
28864be47400SFrançois Tigeot 	int err;
28874be47400SFrançois Tigeot 
28884be47400SFrançois Tigeot 	err = mutex_lock_interruptible(&obj->mm.lock);
28894be47400SFrançois Tigeot 	if (err)
28904be47400SFrançois Tigeot 		return err;
28914be47400SFrançois Tigeot 
28923f2dd94aSFrançois Tigeot 	if (unlikely(!i915_gem_object_has_pages(obj))) {
28933f2dd94aSFrançois Tigeot 		GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
28943f2dd94aSFrançois Tigeot 
28954be47400SFrançois Tigeot 		err = ____i915_gem_object_get_pages(obj);
28964be47400SFrançois Tigeot 		if (err)
28974be47400SFrançois Tigeot 			goto unlock;
28984be47400SFrançois Tigeot 
28994be47400SFrançois Tigeot 		smp_mb__before_atomic();
29004be47400SFrançois Tigeot 	}
29014be47400SFrançois Tigeot 	atomic_inc(&obj->mm.pages_pin_count);
29024be47400SFrançois Tigeot 
29034be47400SFrançois Tigeot unlock:
29044be47400SFrançois Tigeot 	mutex_unlock(&obj->mm.lock);
29054be47400SFrançois Tigeot 	return err;
29064be47400SFrançois Tigeot }
29074be47400SFrançois Tigeot 
29081487f786SFrançois Tigeot /* The 'mapping' part of i915_gem_object_pin_map() below */
i915_gem_object_map(const struct drm_i915_gem_object * obj,enum i915_map_type type)29091e12ee3bSFrançois Tigeot static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
29101e12ee3bSFrançois Tigeot 				 enum i915_map_type type)
29111487f786SFrançois Tigeot {
29121487f786SFrançois Tigeot 	unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
29134be47400SFrançois Tigeot 	struct sg_table *sgt = obj->mm.pages;
29141487f786SFrançois Tigeot 	struct sgt_iter sgt_iter;
29151487f786SFrançois Tigeot 	struct page *page;
29161487f786SFrançois Tigeot 	struct page *stack_pages[32];
29171487f786SFrançois Tigeot 	struct page **pages = stack_pages;
29181487f786SFrançois Tigeot 	unsigned long i = 0;
29191e12ee3bSFrançois Tigeot 	pgprot_t pgprot;
29201487f786SFrançois Tigeot 	void *addr;
29211487f786SFrançois Tigeot 
29221487f786SFrançois Tigeot 	/* A single page can always be kmapped */
29231e12ee3bSFrançois Tigeot 	if (n_pages == 1 && type == I915_MAP_WB)
29241487f786SFrançois Tigeot 		return kmap(sg_page(sgt->sgl));
29251487f786SFrançois Tigeot 
29261487f786SFrançois Tigeot 	if (n_pages > ARRAY_SIZE(stack_pages)) {
29271487f786SFrançois Tigeot 		/* Too big for stack -- allocate temporary array instead */
29283f2dd94aSFrançois Tigeot 		pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
29291487f786SFrançois Tigeot 		if (!pages)
29301487f786SFrançois Tigeot 			return NULL;
29311487f786SFrançois Tigeot 	}
29321487f786SFrançois Tigeot 
29331487f786SFrançois Tigeot 	for_each_sgt_page(page, sgt_iter, sgt)
29341487f786SFrançois Tigeot 		pages[i++] = page;
29351487f786SFrançois Tigeot 
29361487f786SFrançois Tigeot 	/* Check that we have the expected number of pages */
29371487f786SFrançois Tigeot 	GEM_BUG_ON(i != n_pages);
29381487f786SFrançois Tigeot 
29391e12ee3bSFrançois Tigeot 	switch (type) {
29403f2dd94aSFrançois Tigeot 	default:
29413f2dd94aSFrançois Tigeot 		MISSING_CASE(type);
29423f2dd94aSFrançois Tigeot 		/* fallthrough to use PAGE_KERNEL anyway */
29431e12ee3bSFrançois Tigeot 	case I915_MAP_WB:
29441e12ee3bSFrançois Tigeot 		pgprot = PAGE_KERNEL;
29451e12ee3bSFrançois Tigeot 		break;
29461e12ee3bSFrançois Tigeot 	case I915_MAP_WC:
29471e12ee3bSFrançois Tigeot 		pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
29481e12ee3bSFrançois Tigeot 		break;
29491e12ee3bSFrançois Tigeot 	}
29501e12ee3bSFrançois Tigeot 	addr = vmap(pages, n_pages, 0, pgprot);
29511487f786SFrançois Tigeot 
29521487f786SFrançois Tigeot 	if (pages != stack_pages)
29533f2dd94aSFrançois Tigeot 		kvfree(pages);
29541487f786SFrançois Tigeot 
29551487f786SFrançois Tigeot 	return addr;
29561487f786SFrançois Tigeot }
29571487f786SFrançois Tigeot 
29581487f786SFrançois Tigeot /* get, pin, and map the pages of the object into kernel space */
i915_gem_object_pin_map(struct drm_i915_gem_object * obj,enum i915_map_type type)29591e12ee3bSFrançois Tigeot void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
29601e12ee3bSFrançois Tigeot 			      enum i915_map_type type)
29618621f407SFrançois Tigeot {
29621e12ee3bSFrançois Tigeot 	enum i915_map_type has_type;
29631e12ee3bSFrançois Tigeot 	bool pinned;
29641e12ee3bSFrançois Tigeot 	void *ptr;
29658621f407SFrançois Tigeot 	int ret;
29668621f407SFrançois Tigeot 
29671e12ee3bSFrançois Tigeot 	GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
29688621f407SFrançois Tigeot 
29694be47400SFrançois Tigeot 	ret = mutex_lock_interruptible(&obj->mm.lock);
29708621f407SFrançois Tigeot 	if (ret)
29718621f407SFrançois Tigeot 		return ERR_PTR(ret);
29728621f407SFrançois Tigeot 
29733f2dd94aSFrançois Tigeot 	pinned = !(type & I915_MAP_OVERRIDE);
29743f2dd94aSFrançois Tigeot 	type &= ~I915_MAP_OVERRIDE;
29753f2dd94aSFrançois Tigeot 
29764be47400SFrançois Tigeot 	if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
29773f2dd94aSFrançois Tigeot 		if (unlikely(!i915_gem_object_has_pages(obj))) {
29783f2dd94aSFrançois Tigeot 			GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
29793f2dd94aSFrançois Tigeot 
29804be47400SFrançois Tigeot 			ret = ____i915_gem_object_get_pages(obj);
29814be47400SFrançois Tigeot 			if (ret)
29824be47400SFrançois Tigeot 				goto err_unlock;
29838621f407SFrançois Tigeot 
29844be47400SFrançois Tigeot 			smp_mb__before_atomic();
29854be47400SFrançois Tigeot 		}
29864be47400SFrançois Tigeot 		atomic_inc(&obj->mm.pages_pin_count);
29874be47400SFrançois Tigeot 		pinned = false;
29884be47400SFrançois Tigeot 	}
29893f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!i915_gem_object_has_pages(obj));
29904be47400SFrançois Tigeot 
29913f2dd94aSFrançois Tigeot 	ptr = page_unpack_bits(obj->mm.mapping, &has_type);
29921e12ee3bSFrançois Tigeot 	if (ptr && has_type != type) {
29931e12ee3bSFrançois Tigeot 		if (pinned) {
29941e12ee3bSFrançois Tigeot 			ret = -EBUSY;
29954be47400SFrançois Tigeot 			goto err_unpin;
29961e12ee3bSFrançois Tigeot 		}
29971e12ee3bSFrançois Tigeot 
29981e12ee3bSFrançois Tigeot 		if (is_vmalloc_addr(ptr))
29991e12ee3bSFrançois Tigeot 			vunmap(ptr);
30001e12ee3bSFrançois Tigeot 		else
30011e12ee3bSFrançois Tigeot 			kunmap(kmap_to_page(ptr));
30021e12ee3bSFrançois Tigeot 
30034be47400SFrançois Tigeot 		ptr = obj->mm.mapping = NULL;
30041e12ee3bSFrançois Tigeot 	}
30051e12ee3bSFrançois Tigeot 
30061e12ee3bSFrançois Tigeot 	if (!ptr) {
30071e12ee3bSFrançois Tigeot 		ptr = i915_gem_object_map(obj, type);
30081e12ee3bSFrançois Tigeot 		if (!ptr) {
30091e12ee3bSFrançois Tigeot 			ret = -ENOMEM;
30104be47400SFrançois Tigeot 			goto err_unpin;
30111e12ee3bSFrançois Tigeot 		}
30121e12ee3bSFrançois Tigeot 
30133f2dd94aSFrançois Tigeot 		obj->mm.mapping = page_pack_bits(ptr, type);
30141e12ee3bSFrançois Tigeot 	}
30151e12ee3bSFrançois Tigeot 
30164be47400SFrançois Tigeot out_unlock:
30174be47400SFrançois Tigeot 	mutex_unlock(&obj->mm.lock);
30181e12ee3bSFrançois Tigeot 	return ptr;
30191e12ee3bSFrançois Tigeot 
30204be47400SFrançois Tigeot err_unpin:
30214be47400SFrançois Tigeot 	atomic_dec(&obj->mm.pages_pin_count);
30224be47400SFrançois Tigeot err_unlock:
30234be47400SFrançois Tigeot 	ptr = ERR_PTR(ret);
30244be47400SFrançois Tigeot 	goto out_unlock;
3025e3adcf8fSFrançois Tigeot }
3026e3adcf8fSFrançois Tigeot 
3027a85cb24fSFrançois Tigeot static int
i915_gem_object_pwrite_gtt(struct drm_i915_gem_object * obj,const struct drm_i915_gem_pwrite * arg)3028a85cb24fSFrançois Tigeot i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
3029a85cb24fSFrançois Tigeot 			   const struct drm_i915_gem_pwrite *arg)
30305d0b1887SFrançois Tigeot {
3031a85cb24fSFrançois Tigeot #ifndef __DragonFly__
3032a85cb24fSFrançois Tigeot 	struct address_space *mapping = obj->base.filp->f_mapping;
3033a85cb24fSFrançois Tigeot #endif
3034a85cb24fSFrançois Tigeot 	char __user *user_data = u64_to_user_ptr(arg->data_ptr);
3035a85cb24fSFrançois Tigeot 	u64 remain, offset;
3036a85cb24fSFrançois Tigeot 	unsigned int pg;
30375d0b1887SFrançois Tigeot 
3038a85cb24fSFrançois Tigeot 	/* Before we instantiate/pin the backing store for our use, we
3039a85cb24fSFrançois Tigeot 	 * can prepopulate the shmemfs filp efficiently using a write into
3040a85cb24fSFrançois Tigeot 	 * the pagecache. We avoid the penalty of instantiating all the
3041a85cb24fSFrançois Tigeot 	 * pages, important if the user is just writing to a few and never
3042a85cb24fSFrançois Tigeot 	 * uses the object on the GPU, and using a direct write into shmemfs
3043a85cb24fSFrançois Tigeot 	 * allows it to avoid the cost of retrieving a page (either swapin
3044a85cb24fSFrançois Tigeot 	 * or clearing-before-use) before it is overwritten.
3045a85cb24fSFrançois Tigeot 	 */
30463f2dd94aSFrançois Tigeot 	if (i915_gem_object_has_pages(obj))
3047a85cb24fSFrançois Tigeot 		return -ENODEV;
30489edbd4a0SFrançois Tigeot 
30493f2dd94aSFrançois Tigeot 	if (obj->mm.madv != I915_MADV_WILLNEED)
30503f2dd94aSFrançois Tigeot 		return -EFAULT;
30513f2dd94aSFrançois Tigeot 
3052a85cb24fSFrançois Tigeot 	/* Before the pages are instantiated the object is treated as being
3053a85cb24fSFrançois Tigeot 	 * in the CPU domain. The pages will be clflushed as required before
3054a85cb24fSFrançois Tigeot 	 * use, and we can freely write into the pages directly. If userspace
3055a85cb24fSFrançois Tigeot 	 * races pwrite with any other operation; corruption will ensue -
3056a85cb24fSFrançois Tigeot 	 * that is userspace's prerogative!
3057a85cb24fSFrançois Tigeot 	 */
3058a85cb24fSFrançois Tigeot 
3059a85cb24fSFrançois Tigeot 	remain = arg->size;
3060a85cb24fSFrançois Tigeot 	offset = arg->offset;
3061a85cb24fSFrançois Tigeot 	pg = offset_in_page(offset);
3062a85cb24fSFrançois Tigeot 
3063a85cb24fSFrançois Tigeot 	do {
3064a85cb24fSFrançois Tigeot 		unsigned int len, unwritten;
3065a85cb24fSFrançois Tigeot 		struct page *page;
3066a85cb24fSFrançois Tigeot 		void *data, *vaddr;
3067a85cb24fSFrançois Tigeot 		int err;
3068a85cb24fSFrançois Tigeot 
3069a85cb24fSFrançois Tigeot 		len = PAGE_SIZE - pg;
3070a85cb24fSFrançois Tigeot 		if (len > remain)
3071a85cb24fSFrançois Tigeot 			len = remain;
3072a85cb24fSFrançois Tigeot 
3073a85cb24fSFrançois Tigeot #ifndef __DragonFly__
3074a85cb24fSFrançois Tigeot 		err = pagecache_write_begin(obj->base.filp, mapping,
30753f2dd94aSFrançois Tigeot #else
30763f2dd94aSFrançois Tigeot 		err = pagecache_write_begin(obj->base.filp, NULL,
30773f2dd94aSFrançois Tigeot #endif
3078a85cb24fSFrançois Tigeot 					    offset, len, 0,
3079a85cb24fSFrançois Tigeot 					    &page, &data);
3080a85cb24fSFrançois Tigeot 		if (err < 0)
3081a85cb24fSFrançois Tigeot 			return err;
3082a85cb24fSFrançois Tigeot 
3083a85cb24fSFrançois Tigeot 		vaddr = kmap(page);
3084a85cb24fSFrançois Tigeot 		unwritten = copy_from_user(vaddr + pg, user_data, len);
3085a85cb24fSFrançois Tigeot 		kunmap(page);
3086a85cb24fSFrançois Tigeot 
3087a85cb24fSFrançois Tigeot #ifndef __DragonFly__
3088a85cb24fSFrançois Tigeot 		err = pagecache_write_end(obj->base.filp, mapping,
30893f2dd94aSFrançois Tigeot #else
30903f2dd94aSFrançois Tigeot 		err = pagecache_write_end(obj->base.filp, NULL,
30913f2dd94aSFrançois Tigeot #endif
3092a85cb24fSFrançois Tigeot 					  offset, len, len - unwritten,
3093a85cb24fSFrançois Tigeot 					  page, data);
3094a85cb24fSFrançois Tigeot 		if (err < 0)
3095a85cb24fSFrançois Tigeot 			return err;
3096a85cb24fSFrançois Tigeot 
3097a85cb24fSFrançois Tigeot 		if (unwritten)
3098a85cb24fSFrançois Tigeot 			return -EFAULT;
3099a85cb24fSFrançois Tigeot 
3100a85cb24fSFrançois Tigeot 		remain -= len;
3101a85cb24fSFrançois Tigeot 		user_data += len;
3102a85cb24fSFrançois Tigeot 		offset += len;
3103a85cb24fSFrançois Tigeot 		pg = 0;
3104a85cb24fSFrançois Tigeot 	} while (remain);
3105a85cb24fSFrançois Tigeot 
3106a85cb24fSFrançois Tigeot 	return 0;
31079edbd4a0SFrançois Tigeot }
31089edbd4a0SFrançois Tigeot 
ban_context(const struct i915_gem_context * ctx,unsigned int score)31093f2dd94aSFrançois Tigeot static bool ban_context(const struct i915_gem_context *ctx,
31103f2dd94aSFrançois Tigeot 			unsigned int score)
31115d0b1887SFrançois Tigeot {
3112a85cb24fSFrançois Tigeot 	return (i915_gem_context_is_bannable(ctx) &&
31133f2dd94aSFrançois Tigeot 		score >= CONTEXT_SCORE_BAN_THRESHOLD);
31145d0b1887SFrançois Tigeot }
3115a85cb24fSFrançois Tigeot 
i915_gem_context_mark_guilty(struct i915_gem_context * ctx)3116a85cb24fSFrançois Tigeot static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
3117a85cb24fSFrançois Tigeot {
31183f2dd94aSFrançois Tigeot 	unsigned int score;
31193f2dd94aSFrançois Tigeot 	bool banned;
3120a85cb24fSFrançois Tigeot 
31213f2dd94aSFrançois Tigeot 	atomic_inc(&ctx->guilty_count);
31223f2dd94aSFrançois Tigeot 
31233f2dd94aSFrançois Tigeot 	score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
31243f2dd94aSFrançois Tigeot 	banned = ban_context(ctx, score);
3125a85cb24fSFrançois Tigeot 	DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
31263f2dd94aSFrançois Tigeot 			 ctx->name, score, yesno(banned));
31273f2dd94aSFrançois Tigeot 	if (!banned)
3128a85cb24fSFrançois Tigeot 		return;
3129a85cb24fSFrançois Tigeot 
31303f2dd94aSFrançois Tigeot 	i915_gem_context_set_banned(ctx);
31313f2dd94aSFrançois Tigeot 	if (!IS_ERR_OR_NULL(ctx->file_priv)) {
31323f2dd94aSFrançois Tigeot 		atomic_inc(&ctx->file_priv->context_bans);
3133a85cb24fSFrançois Tigeot 		DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
31343f2dd94aSFrançois Tigeot 				 ctx->name, atomic_read(&ctx->file_priv->context_bans));
31353f2dd94aSFrançois Tigeot 	}
3136a85cb24fSFrançois Tigeot }
3137a85cb24fSFrançois Tigeot 
i915_gem_context_mark_innocent(struct i915_gem_context * ctx)3138a85cb24fSFrançois Tigeot static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
3139a85cb24fSFrançois Tigeot {
31403f2dd94aSFrançois Tigeot 	atomic_inc(&ctx->active_count);
31415d0b1887SFrançois Tigeot }
31425d0b1887SFrançois Tigeot 
3143ba55f2f5SFrançois Tigeot struct drm_i915_gem_request *
i915_gem_find_active_request(struct intel_engine_cs * engine)31448621f407SFrançois Tigeot i915_gem_find_active_request(struct intel_engine_cs *engine)
3145e3adcf8fSFrançois Tigeot {
3146a85cb24fSFrançois Tigeot 	struct drm_i915_gem_request *request, *active = NULL;
3147a85cb24fSFrançois Tigeot 	unsigned long flags;
3148e3adcf8fSFrançois Tigeot 
3149303bf270SFrançois Tigeot 	/* We are called by the error capture and reset at a random
3150303bf270SFrançois Tigeot 	 * point in time. In particular, note that neither is crucially
3151303bf270SFrançois Tigeot 	 * ordered with an interrupt. After a hang, the GPU is dead and we
3152303bf270SFrançois Tigeot 	 * assume that no more writes can happen (we waited long enough for
3153303bf270SFrançois Tigeot 	 * all writes that were in transaction to be flushed) - adding an
3154303bf270SFrançois Tigeot 	 * extra delay for a recent interrupt is pointless. Hence, we do
3155303bf270SFrançois Tigeot 	 * not need an engine->irq_seqno_barrier() before the seqno reads.
3156303bf270SFrançois Tigeot 	 */
3157a85cb24fSFrançois Tigeot 	spin_lock_irqsave(&engine->timeline->lock, flags);
31584be47400SFrançois Tigeot 	list_for_each_entry(request, &engine->timeline->requests, link) {
3159a85cb24fSFrançois Tigeot 		if (__i915_gem_request_completed(request,
3160a85cb24fSFrançois Tigeot 						 request->global_seqno))
31619edbd4a0SFrançois Tigeot 			continue;
3162e3adcf8fSFrançois Tigeot 
3163a85cb24fSFrançois Tigeot 		GEM_BUG_ON(request->engine != engine);
3164a85cb24fSFrançois Tigeot 		GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
3165a85cb24fSFrançois Tigeot 				    &request->fence.flags));
3166a85cb24fSFrançois Tigeot 
3167a85cb24fSFrançois Tigeot 		active = request;
3168a85cb24fSFrançois Tigeot 		break;
3169a85cb24fSFrançois Tigeot 	}
3170a85cb24fSFrançois Tigeot 	spin_unlock_irqrestore(&engine->timeline->lock, flags);
3171a85cb24fSFrançois Tigeot 
3172a85cb24fSFrançois Tigeot 	return active;
31739edbd4a0SFrançois Tigeot }
3174ba55f2f5SFrançois Tigeot 
engine_stalled(struct intel_engine_cs * engine)3175a85cb24fSFrançois Tigeot static bool engine_stalled(struct intel_engine_cs *engine)
3176a85cb24fSFrançois Tigeot {
3177a85cb24fSFrançois Tigeot 	if (!engine->hangcheck.stalled)
3178a85cb24fSFrançois Tigeot 		return false;
3179a85cb24fSFrançois Tigeot 
3180a85cb24fSFrançois Tigeot 	/* Check for possible seqno movement after hang declaration */
3181a85cb24fSFrançois Tigeot 	if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
3182a85cb24fSFrançois Tigeot 		DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
3183a85cb24fSFrançois Tigeot 		return false;
3184ba55f2f5SFrançois Tigeot 	}
3185ba55f2f5SFrançois Tigeot 
3186a85cb24fSFrançois Tigeot 	return true;
3187a85cb24fSFrançois Tigeot }
3188a85cb24fSFrançois Tigeot 
31893f2dd94aSFrançois Tigeot /*
31903f2dd94aSFrançois Tigeot  * Ensure irq handler finishes, and not run again.
31913f2dd94aSFrançois Tigeot  * Also return the active request so that we only search for it once.
31923f2dd94aSFrançois Tigeot  */
31933f2dd94aSFrançois Tigeot struct drm_i915_gem_request *
i915_gem_reset_prepare_engine(struct intel_engine_cs * engine)31943f2dd94aSFrançois Tigeot i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
3195a85cb24fSFrançois Tigeot {
31963f2dd94aSFrançois Tigeot 	struct drm_i915_gem_request *request = NULL;
3197a85cb24fSFrançois Tigeot 
31983f2dd94aSFrançois Tigeot 	/*
31993f2dd94aSFrançois Tigeot 	 * During the reset sequence, we must prevent the engine from
32003f2dd94aSFrançois Tigeot 	 * entering RC6. As the context state is undefined until we restart
32013f2dd94aSFrançois Tigeot 	 * the engine, if it does enter RC6 during the reset, the state
32023f2dd94aSFrançois Tigeot 	 * written to the powercontext is undefined and so we may lose
32033f2dd94aSFrançois Tigeot 	 * GPU state upon resume, i.e. fail to restart after a reset.
32043f2dd94aSFrançois Tigeot 	 */
32053f2dd94aSFrançois Tigeot 	intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
3206a85cb24fSFrançois Tigeot 
32073f2dd94aSFrançois Tigeot 	/*
32083f2dd94aSFrançois Tigeot 	 * Prevent the signaler thread from updating the request
3209a85cb24fSFrançois Tigeot 	 * state (by calling dma_fence_signal) as we are processing
3210a85cb24fSFrançois Tigeot 	 * the reset. The write from the GPU of the seqno is
3211a85cb24fSFrançois Tigeot 	 * asynchronous and the signaler thread may see a different
3212a85cb24fSFrançois Tigeot 	 * value to us and declare the request complete, even though
3213a85cb24fSFrançois Tigeot 	 * the reset routine have picked that request as the active
3214a85cb24fSFrançois Tigeot 	 * (incomplete) request. This conflict is not handled
3215a85cb24fSFrançois Tigeot 	 * gracefully!
3216a85cb24fSFrançois Tigeot 	 */
3217a85cb24fSFrançois Tigeot 	kthread_park(engine->breadcrumbs.signaler);
3218a85cb24fSFrançois Tigeot 
32193f2dd94aSFrançois Tigeot 	/*
32203f2dd94aSFrançois Tigeot 	 * Prevent request submission to the hardware until we have
3221a85cb24fSFrançois Tigeot 	 * completed the reset in i915_gem_reset_finish(). If a request
3222a85cb24fSFrançois Tigeot 	 * is completed by one engine, it may then queue a request
3223a85cb24fSFrançois Tigeot 	 * to a second via its engine->irq_tasklet *just* as we are
3224a85cb24fSFrançois Tigeot 	 * calling engine->init_hw() and also writing the ELSP.
3225a85cb24fSFrançois Tigeot 	 * Turning off the engine->irq_tasklet until the reset is over
3226a85cb24fSFrançois Tigeot 	 * prevents the race.
3227a85cb24fSFrançois Tigeot 	 */
32283f2dd94aSFrançois Tigeot 	tasklet_kill(&engine->execlists.irq_tasklet);
32293f2dd94aSFrançois Tigeot 	tasklet_disable(&engine->execlists.irq_tasklet);
3230a85cb24fSFrançois Tigeot 
3231a85cb24fSFrançois Tigeot 	if (engine->irq_seqno_barrier)
3232a85cb24fSFrançois Tigeot 		engine->irq_seqno_barrier(engine);
3233a85cb24fSFrançois Tigeot 
3234a85cb24fSFrançois Tigeot 	request = i915_gem_find_active_request(engine);
3235a85cb24fSFrançois Tigeot 	if (request && request->fence.error == -EIO)
32363f2dd94aSFrançois Tigeot 		request = ERR_PTR(-EIO); /* Previous reset failed! */
32373f2dd94aSFrançois Tigeot 
32383f2dd94aSFrançois Tigeot 	return request;
3239a85cb24fSFrançois Tigeot }
32403f2dd94aSFrançois Tigeot 
i915_gem_reset_prepare(struct drm_i915_private * dev_priv)32413f2dd94aSFrançois Tigeot int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
32423f2dd94aSFrançois Tigeot {
32433f2dd94aSFrançois Tigeot 	struct intel_engine_cs *engine;
32443f2dd94aSFrançois Tigeot 	struct drm_i915_gem_request *request;
32453f2dd94aSFrançois Tigeot 	enum intel_engine_id id;
32463f2dd94aSFrançois Tigeot 	int err = 0;
32473f2dd94aSFrançois Tigeot 
32483f2dd94aSFrançois Tigeot 	for_each_engine(engine, dev_priv, id) {
32493f2dd94aSFrançois Tigeot 		request = i915_gem_reset_prepare_engine(engine);
32503f2dd94aSFrançois Tigeot 		if (IS_ERR(request)) {
32513f2dd94aSFrançois Tigeot 			err = PTR_ERR(request);
32523f2dd94aSFrançois Tigeot 			continue;
32533f2dd94aSFrançois Tigeot 		}
32543f2dd94aSFrançois Tigeot 
32553f2dd94aSFrançois Tigeot 		engine->hangcheck.active_request = request;
3256a85cb24fSFrançois Tigeot 	}
3257a85cb24fSFrançois Tigeot 
3258a85cb24fSFrançois Tigeot 	i915_gem_revoke_fences(dev_priv);
3259a85cb24fSFrançois Tigeot 
3260a85cb24fSFrançois Tigeot 	return err;
3261a85cb24fSFrançois Tigeot }
3262a85cb24fSFrançois Tigeot 
skip_request(struct drm_i915_gem_request * request)3263a85cb24fSFrançois Tigeot static void skip_request(struct drm_i915_gem_request *request)
32641e12ee3bSFrançois Tigeot {
32651e12ee3bSFrançois Tigeot 	void *vaddr = request->ring->vaddr;
32661e12ee3bSFrançois Tigeot 	u32 head;
32671e12ee3bSFrançois Tigeot 
32681e12ee3bSFrançois Tigeot 	/* As this request likely depends on state from the lost
32691e12ee3bSFrançois Tigeot 	 * context, clear out all the user operations leaving the
32701e12ee3bSFrançois Tigeot 	 * breadcrumb at the end (so we get the fence notifications).
32711e12ee3bSFrançois Tigeot 	 */
32721e12ee3bSFrançois Tigeot 	head = request->head;
32731e12ee3bSFrançois Tigeot 	if (request->postfix < head) {
32741e12ee3bSFrançois Tigeot 		memset(vaddr + head, 0, request->ring->size - head);
32751e12ee3bSFrançois Tigeot 		head = 0;
32761e12ee3bSFrançois Tigeot 	}
32771e12ee3bSFrançois Tigeot 	memset(vaddr + head, 0, request->postfix - head);
3278a85cb24fSFrançois Tigeot 
3279a85cb24fSFrançois Tigeot 	dma_fence_set_error(&request->fence, -EIO);
32801e12ee3bSFrançois Tigeot }
32811e12ee3bSFrançois Tigeot 
engine_skip_context(struct drm_i915_gem_request * request)3282a85cb24fSFrançois Tigeot static void engine_skip_context(struct drm_i915_gem_request *request)
3283ba55f2f5SFrançois Tigeot {
3284a85cb24fSFrançois Tigeot 	struct intel_engine_cs *engine = request->engine;
3285a85cb24fSFrançois Tigeot 	struct i915_gem_context *hung_ctx = request->ctx;
32864be47400SFrançois Tigeot 	struct intel_timeline *timeline;
32874be47400SFrançois Tigeot 	unsigned long flags;
3288ba55f2f5SFrançois Tigeot 
3289a85cb24fSFrançois Tigeot 	timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
32904be47400SFrançois Tigeot 
32914be47400SFrançois Tigeot 	spin_lock_irqsave(&engine->timeline->lock, flags);
32924be47400SFrançois Tigeot 	lockmgr(&timeline->lock, LK_EXCLUSIVE);
32934be47400SFrançois Tigeot 
32944be47400SFrançois Tigeot 	list_for_each_entry_continue(request, &engine->timeline->requests, link)
3295a85cb24fSFrançois Tigeot 		if (request->ctx == hung_ctx)
3296a85cb24fSFrançois Tigeot 			skip_request(request);
32974be47400SFrançois Tigeot 
32984be47400SFrançois Tigeot 	list_for_each_entry(request, &timeline->requests, link)
3299a85cb24fSFrançois Tigeot 		skip_request(request);
33004be47400SFrançois Tigeot 
33014be47400SFrançois Tigeot 	lockmgr(&timeline->lock, LK_RELEASE);
33024be47400SFrançois Tigeot 	spin_unlock_irqrestore(&engine->timeline->lock, flags);
3303e3adcf8fSFrançois Tigeot }
3304e3adcf8fSFrançois Tigeot 
33053f2dd94aSFrançois Tigeot /* Returns the request if it was guilty of the hang */
33063f2dd94aSFrançois Tigeot static struct drm_i915_gem_request *
i915_gem_reset_request(struct intel_engine_cs * engine,struct drm_i915_gem_request * request)33073f2dd94aSFrançois Tigeot i915_gem_reset_request(struct intel_engine_cs *engine,
33083f2dd94aSFrançois Tigeot 		       struct drm_i915_gem_request *request)
3309a85cb24fSFrançois Tigeot {
3310a85cb24fSFrançois Tigeot 	/* The guilty request will get skipped on a hung engine.
3311a85cb24fSFrançois Tigeot 	 *
3312a85cb24fSFrançois Tigeot 	 * Users of client default contexts do not rely on logical
3313a85cb24fSFrançois Tigeot 	 * state preserved between batches so it is safe to execute
3314a85cb24fSFrançois Tigeot 	 * queued requests following the hang. Non default contexts
3315a85cb24fSFrançois Tigeot 	 * rely on preserved state, so skipping a batch loses the
3316a85cb24fSFrançois Tigeot 	 * evolution of the state and it needs to be considered corrupted.
3317a85cb24fSFrançois Tigeot 	 * Executing more queued batches on top of corrupted state is
3318a85cb24fSFrançois Tigeot 	 * risky. But we take the risk by trying to advance through
3319a85cb24fSFrançois Tigeot 	 * the queued requests in order to make the client behaviour
3320a85cb24fSFrançois Tigeot 	 * more predictable around resets, by not throwing away random
3321a85cb24fSFrançois Tigeot 	 * amount of batches it has prepared for execution. Sophisticated
3322a85cb24fSFrançois Tigeot 	 * clients can use gem_reset_stats_ioctl and dma fence status
3323a85cb24fSFrançois Tigeot 	 * (exported via sync_file info ioctl on explicit fences) to observe
3324a85cb24fSFrançois Tigeot 	 * when it loses the context state and should rebuild accordingly.
3325a85cb24fSFrançois Tigeot 	 *
3326a85cb24fSFrançois Tigeot 	 * The context ban, and ultimately the client ban, mechanism are safety
3327a85cb24fSFrançois Tigeot 	 * valves if client submission ends up resulting in nothing more than
3328a85cb24fSFrançois Tigeot 	 * subsequent hangs.
3329a85cb24fSFrançois Tigeot 	 */
3330a85cb24fSFrançois Tigeot 
33313f2dd94aSFrançois Tigeot 	if (engine_stalled(engine)) {
3332a85cb24fSFrançois Tigeot 		i915_gem_context_mark_guilty(request->ctx);
3333a85cb24fSFrançois Tigeot 		skip_request(request);
3334a85cb24fSFrançois Tigeot 
3335a85cb24fSFrançois Tigeot 		/* If this context is now banned, skip all pending requests. */
3336a85cb24fSFrançois Tigeot 		if (i915_gem_context_is_banned(request->ctx))
3337a85cb24fSFrançois Tigeot 			engine_skip_context(request);
33383f2dd94aSFrançois Tigeot 	} else {
33393f2dd94aSFrançois Tigeot 		/*
33403f2dd94aSFrançois Tigeot 		 * Since this is not the hung engine, it may have advanced
33413f2dd94aSFrançois Tigeot 		 * since the hang declaration. Double check by refinding
33423f2dd94aSFrançois Tigeot 		 * the active request at the time of the reset.
33433f2dd94aSFrançois Tigeot 		 */
33443f2dd94aSFrançois Tigeot 		request = i915_gem_find_active_request(engine);
33453f2dd94aSFrançois Tigeot 		if (request) {
33463f2dd94aSFrançois Tigeot 			i915_gem_context_mark_innocent(request->ctx);
33473f2dd94aSFrançois Tigeot 			dma_fence_set_error(&request->fence, -EAGAIN);
33483f2dd94aSFrançois Tigeot 
33493f2dd94aSFrançois Tigeot 			/* Rewind the engine to replay the incomplete rq */
33503f2dd94aSFrançois Tigeot 			spin_lock_irq(&engine->timeline->lock);
33513f2dd94aSFrançois Tigeot 			request = list_prev_entry(request, link);
33523f2dd94aSFrançois Tigeot 			if (&request->link == &engine->timeline->requests)
33533f2dd94aSFrançois Tigeot 				request = NULL;
33543f2dd94aSFrançois Tigeot 			spin_unlock_irq(&engine->timeline->lock);
33553f2dd94aSFrançois Tigeot 		}
33563f2dd94aSFrançois Tigeot 	}
33573f2dd94aSFrançois Tigeot 
33583f2dd94aSFrançois Tigeot 	return request;
33593f2dd94aSFrançois Tigeot }
33603f2dd94aSFrançois Tigeot 
i915_gem_reset_engine(struct intel_engine_cs * engine,struct drm_i915_gem_request * request)33613f2dd94aSFrançois Tigeot void i915_gem_reset_engine(struct intel_engine_cs *engine,
33623f2dd94aSFrançois Tigeot 			   struct drm_i915_gem_request *request)
33633f2dd94aSFrançois Tigeot {
33643f2dd94aSFrançois Tigeot 	engine->irq_posted = 0;
33653f2dd94aSFrançois Tigeot 
33663f2dd94aSFrançois Tigeot 	if (request)
33673f2dd94aSFrançois Tigeot 		request = i915_gem_reset_request(engine, request);
33683f2dd94aSFrançois Tigeot 
33693f2dd94aSFrançois Tigeot 	if (request) {
33703f2dd94aSFrançois Tigeot 		DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
33713f2dd94aSFrançois Tigeot 				 engine->name, request->global_seqno);
3372a85cb24fSFrançois Tigeot 	}
3373a85cb24fSFrançois Tigeot 
3374a85cb24fSFrançois Tigeot 	/* Setup the CS to resume from the breadcrumb of the hung request */
3375a85cb24fSFrançois Tigeot 	engine->reset_hw(engine, request);
3376a85cb24fSFrançois Tigeot }
3377a85cb24fSFrançois Tigeot 
i915_gem_reset(struct drm_i915_private * dev_priv)33781e12ee3bSFrançois Tigeot void i915_gem_reset(struct drm_i915_private *dev_priv)
33799edbd4a0SFrançois Tigeot {
33801e12ee3bSFrançois Tigeot 	struct intel_engine_cs *engine;
33811e12ee3bSFrançois Tigeot 	enum intel_engine_id id;
33821e12ee3bSFrançois Tigeot 
33834be47400SFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
33844be47400SFrançois Tigeot 
33851e12ee3bSFrançois Tigeot 	i915_gem_retire_requests(dev_priv);
33861e12ee3bSFrançois Tigeot 
3387a85cb24fSFrançois Tigeot 	for_each_engine(engine, dev_priv, id) {
3388a85cb24fSFrançois Tigeot 		struct i915_gem_context *ctx;
3389a85cb24fSFrançois Tigeot 
33903f2dd94aSFrançois Tigeot 		i915_gem_reset_engine(engine, engine->hangcheck.active_request);
3391a85cb24fSFrançois Tigeot 		ctx = fetch_and_zero(&engine->last_retired_context);
3392a85cb24fSFrançois Tigeot 		if (ctx)
3393a85cb24fSFrançois Tigeot 			engine->context_unpin(engine, ctx);
3394a85cb24fSFrançois Tigeot 	}
33951e12ee3bSFrançois Tigeot 
33964be47400SFrançois Tigeot 	i915_gem_restore_fences(dev_priv);
33971e12ee3bSFrançois Tigeot 
33981e12ee3bSFrançois Tigeot 	if (dev_priv->gt.awake) {
33991e12ee3bSFrançois Tigeot 		intel_sanitize_gt_powersave(dev_priv);
34001e12ee3bSFrançois Tigeot 		intel_enable_gt_powersave(dev_priv);
34011e12ee3bSFrançois Tigeot 		if (INTEL_GEN(dev_priv) >= 6)
34021e12ee3bSFrançois Tigeot 			gen6_rps_busy(dev_priv);
34031e12ee3bSFrançois Tigeot 	}
34041e12ee3bSFrançois Tigeot }
34051e12ee3bSFrançois Tigeot 
i915_gem_reset_finish_engine(struct intel_engine_cs * engine)34063f2dd94aSFrançois Tigeot void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
34073f2dd94aSFrançois Tigeot {
34083f2dd94aSFrançois Tigeot 	tasklet_enable(&engine->execlists.irq_tasklet);
34093f2dd94aSFrançois Tigeot 	kthread_unpark(engine->breadcrumbs.signaler);
34103f2dd94aSFrançois Tigeot 
34113f2dd94aSFrançois Tigeot 	intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
34123f2dd94aSFrançois Tigeot }
34133f2dd94aSFrançois Tigeot 
i915_gem_reset_finish(struct drm_i915_private * dev_priv)3414a85cb24fSFrançois Tigeot void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3415a85cb24fSFrançois Tigeot {
3416a85cb24fSFrançois Tigeot 	struct intel_engine_cs *engine;
3417a85cb24fSFrançois Tigeot 	enum intel_engine_id id;
3418a85cb24fSFrançois Tigeot 
3419a85cb24fSFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
3420a85cb24fSFrançois Tigeot 
3421a85cb24fSFrançois Tigeot 	for_each_engine(engine, dev_priv, id) {
34223f2dd94aSFrançois Tigeot 		engine->hangcheck.active_request = NULL;
34233f2dd94aSFrançois Tigeot 		i915_gem_reset_finish_engine(engine);
3424a85cb24fSFrançois Tigeot 	}
3425a85cb24fSFrançois Tigeot }
3426a85cb24fSFrançois Tigeot 
nop_submit_request(struct drm_i915_gem_request * request)34271e12ee3bSFrançois Tigeot static void nop_submit_request(struct drm_i915_gem_request *request)
34281e12ee3bSFrançois Tigeot {
3429a85cb24fSFrançois Tigeot 	dma_fence_set_error(&request->fence, -EIO);
34303f2dd94aSFrançois Tigeot 
34314be47400SFrançois Tigeot 	i915_gem_request_submit(request);
34321e12ee3bSFrançois Tigeot }
34331e12ee3bSFrançois Tigeot 
nop_complete_submit_request(struct drm_i915_gem_request * request)34343f2dd94aSFrançois Tigeot static void nop_complete_submit_request(struct drm_i915_gem_request *request)
34351e12ee3bSFrançois Tigeot {
3436a85cb24fSFrançois Tigeot 	unsigned long flags;
3437a85cb24fSFrançois Tigeot 
34383f2dd94aSFrançois Tigeot 	dma_fence_set_error(&request->fence, -EIO);
34393f2dd94aSFrançois Tigeot 
34403f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&request->engine->timeline->lock, flags);
34413f2dd94aSFrançois Tigeot 	__i915_gem_request_submit(request);
34423f2dd94aSFrançois Tigeot 	intel_engine_init_global_seqno(request->engine, request->global_seqno);
34433f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
34443f2dd94aSFrançois Tigeot }
34453f2dd94aSFrançois Tigeot 
i915_gem_set_wedged(struct drm_i915_private * i915)34463f2dd94aSFrançois Tigeot void i915_gem_set_wedged(struct drm_i915_private *i915)
34473f2dd94aSFrançois Tigeot {
34483f2dd94aSFrançois Tigeot 	struct intel_engine_cs *engine;
34493f2dd94aSFrançois Tigeot 	enum intel_engine_id id;
34503f2dd94aSFrançois Tigeot 
34513f2dd94aSFrançois Tigeot 	/*
34523f2dd94aSFrançois Tigeot 	 * First, stop submission to hw, but do not yet complete requests by
34533f2dd94aSFrançois Tigeot 	 * rolling the global seqno forward (since this would complete requests
34543f2dd94aSFrançois Tigeot 	 * for which we haven't set the fence error to EIO yet).
3455a85cb24fSFrançois Tigeot 	 */
34563f2dd94aSFrançois Tigeot 	for_each_engine(engine, i915, id)
34571e12ee3bSFrançois Tigeot 		engine->submit_request = nop_submit_request;
34589edbd4a0SFrançois Tigeot 
34593f2dd94aSFrançois Tigeot 	/*
34603f2dd94aSFrançois Tigeot 	 * Make sure no one is running the old callback before we proceed with
34613f2dd94aSFrançois Tigeot 	 * cancelling requests and resetting the completion tracking. Otherwise
34623f2dd94aSFrançois Tigeot 	 * we might submit a request to the hardware which never completes.
34633f2dd94aSFrançois Tigeot 	 */
34643f2dd94aSFrançois Tigeot 	synchronize_rcu();
34653f2dd94aSFrançois Tigeot 
34663f2dd94aSFrançois Tigeot 	for_each_engine(engine, i915, id) {
3467a85cb24fSFrançois Tigeot 		/* Mark all executing requests as skipped */
34683f2dd94aSFrançois Tigeot 		engine->cancel_requests(engine);
34693f2dd94aSFrançois Tigeot 
34703f2dd94aSFrançois Tigeot 		/*
34713f2dd94aSFrançois Tigeot 		 * Only once we've force-cancelled all in-flight requests can we
34723f2dd94aSFrançois Tigeot 		 * start to complete all requests.
34733f2dd94aSFrançois Tigeot 		 */
34743f2dd94aSFrançois Tigeot 		engine->submit_request = nop_complete_submit_request;
34753f2dd94aSFrançois Tigeot 	}
34763f2dd94aSFrançois Tigeot 
34773f2dd94aSFrançois Tigeot 	/*
34783f2dd94aSFrançois Tigeot 	 * Make sure no request can slip through without getting completed by
34793f2dd94aSFrançois Tigeot 	 * either this call here to intel_engine_init_global_seqno, or the one
34803f2dd94aSFrançois Tigeot 	 * in nop_complete_submit_request.
34813f2dd94aSFrançois Tigeot 	 */
34823f2dd94aSFrançois Tigeot 	synchronize_rcu();
34833f2dd94aSFrançois Tigeot 
34843f2dd94aSFrançois Tigeot 	for_each_engine(engine, i915, id) {
34853f2dd94aSFrançois Tigeot 		unsigned long flags;
3486a85cb24fSFrançois Tigeot 
348787df8fc6SFrançois Tigeot 		/* Mark all pending requests as complete so that any concurrent
348887df8fc6SFrançois Tigeot 		 * (lockless) lookup doesn't try and wait upon the request as we
348987df8fc6SFrançois Tigeot 		 * reset it.
349087df8fc6SFrançois Tigeot 		 */
34913f2dd94aSFrançois Tigeot 		spin_lock_irqsave(&engine->timeline->lock, flags);
34924be47400SFrançois Tigeot 		intel_engine_init_global_seqno(engine,
34934be47400SFrançois Tigeot 					       intel_engine_last_submit(engine));
34944be47400SFrançois Tigeot 		spin_unlock_irqrestore(&engine->timeline->lock, flags);
34954be47400SFrançois Tigeot 	}
3496e3adcf8fSFrançois Tigeot 
34973f2dd94aSFrançois Tigeot 	set_bit(I915_WEDGED, &i915->gpu_error.flags);
34983f2dd94aSFrançois Tigeot 	wake_up_all(&i915->gpu_error.reset_queue);
3499a85cb24fSFrançois Tigeot }
3500a85cb24fSFrançois Tigeot 
i915_gem_unset_wedged(struct drm_i915_private * i915)3501a85cb24fSFrançois Tigeot bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3502a85cb24fSFrançois Tigeot {
3503a85cb24fSFrançois Tigeot 	struct i915_gem_timeline *tl;
3504a85cb24fSFrançois Tigeot 	int i;
3505a85cb24fSFrançois Tigeot 
3506a85cb24fSFrançois Tigeot 	lockdep_assert_held(&i915->drm.struct_mutex);
3507a85cb24fSFrançois Tigeot 	if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3508a85cb24fSFrançois Tigeot 		return true;
3509a85cb24fSFrançois Tigeot 
3510a85cb24fSFrançois Tigeot 	/* Before unwedging, make sure that all pending operations
3511a85cb24fSFrançois Tigeot 	 * are flushed and errored out - we may have requests waiting upon
3512a85cb24fSFrançois Tigeot 	 * third party fences. We marked all inflight requests as EIO, and
3513a85cb24fSFrançois Tigeot 	 * every execbuf since returned EIO, for consistency we want all
3514a85cb24fSFrançois Tigeot 	 * the currently pending requests to also be marked as EIO, which
3515a85cb24fSFrançois Tigeot 	 * is done inside our nop_submit_request - and so we must wait.
3516a85cb24fSFrançois Tigeot 	 *
3517a85cb24fSFrançois Tigeot 	 * No more can be submitted until we reset the wedged bit.
3518a85cb24fSFrançois Tigeot 	 */
3519a85cb24fSFrançois Tigeot 	list_for_each_entry(tl, &i915->gt.timelines, link) {
3520a85cb24fSFrançois Tigeot 		for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3521a85cb24fSFrançois Tigeot 			struct drm_i915_gem_request *rq;
3522a85cb24fSFrançois Tigeot 
3523a85cb24fSFrançois Tigeot 			rq = i915_gem_active_peek(&tl->engine[i].last_request,
3524a85cb24fSFrançois Tigeot 						  &i915->drm.struct_mutex);
3525a85cb24fSFrançois Tigeot 			if (!rq)
3526a85cb24fSFrançois Tigeot 				continue;
3527a85cb24fSFrançois Tigeot 
3528a85cb24fSFrançois Tigeot 			/* We can't use our normal waiter as we want to
3529a85cb24fSFrançois Tigeot 			 * avoid recursively trying to handle the current
3530a85cb24fSFrançois Tigeot 			 * reset. The basic dma_fence_default_wait() installs
3531a85cb24fSFrançois Tigeot 			 * a callback for dma_fence_signal(), which is
3532a85cb24fSFrançois Tigeot 			 * triggered by our nop handler (indirectly, the
3533a85cb24fSFrançois Tigeot 			 * callback enables the signaler thread which is
3534a85cb24fSFrançois Tigeot 			 * woken by the nop_submit_request() advancing the seqno
3535a85cb24fSFrançois Tigeot 			 * and when the seqno passes the fence, the signaler
3536a85cb24fSFrançois Tigeot 			 * then signals the fence waking us up).
3537a85cb24fSFrançois Tigeot 			 */
3538a85cb24fSFrançois Tigeot 			if (dma_fence_default_wait(&rq->fence, true,
3539a85cb24fSFrançois Tigeot 						   MAX_SCHEDULE_TIMEOUT) < 0)
3540a85cb24fSFrançois Tigeot 				return false;
3541a85cb24fSFrançois Tigeot 		}
3542a85cb24fSFrançois Tigeot 	}
3543a85cb24fSFrançois Tigeot 
3544a85cb24fSFrançois Tigeot 	/* Undo nop_submit_request. We prevent all new i915 requests from
3545a85cb24fSFrançois Tigeot 	 * being queued (by disallowing execbuf whilst wedged) so having
3546a85cb24fSFrançois Tigeot 	 * waited for all active requests above, we know the system is idle
3547a85cb24fSFrançois Tigeot 	 * and do not have to worry about a thread being inside
3548a85cb24fSFrançois Tigeot 	 * engine->submit_request() as we swap over. So unlike installing
3549a85cb24fSFrançois Tigeot 	 * the nop_submit_request on reset, we can do this from normal
3550a85cb24fSFrançois Tigeot 	 * context and do not require stop_machine().
3551a85cb24fSFrançois Tigeot 	 */
3552a85cb24fSFrançois Tigeot 	intel_engines_reset_default_submission(i915);
35533f2dd94aSFrançois Tigeot 	i915_gem_contexts_lost(i915);
3554a85cb24fSFrançois Tigeot 
3555a85cb24fSFrançois Tigeot 	smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3556a85cb24fSFrançois Tigeot 	clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3557a85cb24fSFrançois Tigeot 
3558a85cb24fSFrançois Tigeot 	return true;
3559e3adcf8fSFrançois Tigeot }
3560e3adcf8fSFrançois Tigeot 
3561e11a51e3SFrançois Tigeot static void
i915_gem_retire_work_handler(struct work_struct * work)3562e11a51e3SFrançois Tigeot i915_gem_retire_work_handler(struct work_struct *work)
3563e11a51e3SFrançois Tigeot {
3564ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv =
3565303bf270SFrançois Tigeot 		container_of(work, typeof(*dev_priv), gt.retire_work.work);
3566303bf270SFrançois Tigeot 	struct drm_device *dev = &dev_priv->drm;
3567e11a51e3SFrançois Tigeot 
3568e11a51e3SFrançois Tigeot 	/* Come back later if the device is busy... */
3569ba55f2f5SFrançois Tigeot 	if (mutex_trylock(&dev->struct_mutex)) {
3570303bf270SFrançois Tigeot 		i915_gem_retire_requests(dev_priv);
3571a2fdbec6SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
3572e11a51e3SFrançois Tigeot 	}
3573303bf270SFrançois Tigeot 
3574303bf270SFrançois Tigeot 	/* Keep the retire handler running until we are finally idle.
3575303bf270SFrançois Tigeot 	 * We do not need to do this test under locking as in the worst-case
3576303bf270SFrançois Tigeot 	 * we queue the retire worker once too often.
3577303bf270SFrançois Tigeot 	 */
357887df8fc6SFrançois Tigeot 	if (READ_ONCE(dev_priv->gt.awake)) {
357987df8fc6SFrançois Tigeot 		i915_queue_hangcheck(dev_priv);
3580303bf270SFrançois Tigeot 		queue_delayed_work(dev_priv->wq,
3581303bf270SFrançois Tigeot 				   &dev_priv->gt.retire_work,
3582ba55f2f5SFrançois Tigeot 				   round_jiffies_up_relative(HZ));
3583ba55f2f5SFrançois Tigeot 	}
358487df8fc6SFrançois Tigeot }
35859edbd4a0SFrançois Tigeot 
35869edbd4a0SFrançois Tigeot static void
i915_gem_idle_work_handler(struct work_struct * work)35879edbd4a0SFrançois Tigeot i915_gem_idle_work_handler(struct work_struct *work)
35889edbd4a0SFrançois Tigeot {
35899edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv =
3590303bf270SFrançois Tigeot 		container_of(work, typeof(*dev_priv), gt.idle_work.work);
3591303bf270SFrançois Tigeot 	struct drm_device *dev = &dev_priv->drm;
3592303bf270SFrançois Tigeot 	bool rearm_hangcheck;
35939edbd4a0SFrançois Tigeot 
3594303bf270SFrançois Tigeot 	if (!READ_ONCE(dev_priv->gt.awake))
359519c468b4SFrançois Tigeot 		return;
359619c468b4SFrançois Tigeot 
35974be47400SFrançois Tigeot 	/*
35984be47400SFrançois Tigeot 	 * Wait for last execlists context complete, but bail out in case a
35994be47400SFrançois Tigeot 	 * new request is submitted.
36004be47400SFrançois Tigeot 	 */
3601a85cb24fSFrançois Tigeot 	wait_for(intel_engines_are_idle(dev_priv), 10);
36024be47400SFrançois Tigeot 	if (READ_ONCE(dev_priv->gt.active_requests))
3603303bf270SFrançois Tigeot 		return;
3604aee94f86SFrançois Tigeot 
3605303bf270SFrançois Tigeot 	rearm_hangcheck =
3606303bf270SFrançois Tigeot 		cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
360719c468b4SFrançois Tigeot 
3608303bf270SFrançois Tigeot 	if (!mutex_trylock(&dev->struct_mutex)) {
3609303bf270SFrançois Tigeot 		/* Currently busy, come back later */
3610303bf270SFrançois Tigeot 		mod_delayed_work(dev_priv->wq,
3611303bf270SFrançois Tigeot 				 &dev_priv->gt.idle_work,
3612303bf270SFrançois Tigeot 				 msecs_to_jiffies(50));
3613303bf270SFrançois Tigeot 		goto out_rearm;
3614303bf270SFrançois Tigeot 	}
3615303bf270SFrançois Tigeot 
36164be47400SFrançois Tigeot 	/*
36174be47400SFrançois Tigeot 	 * New request retired after this work handler started, extend active
36184be47400SFrançois Tigeot 	 * period until next instance of the work.
36194be47400SFrançois Tigeot 	 */
36204be47400SFrançois Tigeot 	if (work_pending(work))
3621303bf270SFrançois Tigeot 		goto out_unlock;
3622303bf270SFrançois Tigeot 
36234be47400SFrançois Tigeot 	if (dev_priv->gt.active_requests)
36244be47400SFrançois Tigeot 		goto out_unlock;
36254be47400SFrançois Tigeot 
3626a85cb24fSFrançois Tigeot 	if (wait_for(intel_engines_are_idle(dev_priv), 10))
36274be47400SFrançois Tigeot 		DRM_ERROR("Timeout waiting for engines to idle\n");
36284be47400SFrançois Tigeot 
36293f2dd94aSFrançois Tigeot 	intel_engines_mark_idle(dev_priv);
36303f2dd94aSFrançois Tigeot 	i915_gem_timelines_mark_idle(dev_priv);
363119c468b4SFrançois Tigeot 
3632303bf270SFrançois Tigeot 	GEM_BUG_ON(!dev_priv->gt.awake);
3633303bf270SFrançois Tigeot 	dev_priv->gt.awake = false;
3634303bf270SFrançois Tigeot 	rearm_hangcheck = false;
3635303bf270SFrançois Tigeot 
3636303bf270SFrançois Tigeot 	if (INTEL_GEN(dev_priv) >= 6)
3637303bf270SFrançois Tigeot 		gen6_rps_idle(dev_priv);
3638303bf270SFrançois Tigeot 	intel_runtime_pm_put(dev_priv);
3639303bf270SFrançois Tigeot out_unlock:
364019c468b4SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
3641303bf270SFrançois Tigeot 
3642303bf270SFrançois Tigeot out_rearm:
3643303bf270SFrançois Tigeot 	if (rearm_hangcheck) {
3644303bf270SFrançois Tigeot 		GEM_BUG_ON(!dev_priv->gt.awake);
3645303bf270SFrançois Tigeot 		i915_queue_hangcheck(dev_priv);
364619c468b4SFrançois Tigeot 	}
36479edbd4a0SFrançois Tigeot }
36489edbd4a0SFrançois Tigeot 
i915_gem_close_object(struct drm_gem_object * gem,struct drm_file * file)364971f41f3eSFrançois Tigeot void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3650f0b54121SFrançois Tigeot {
36513f2dd94aSFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(gem->dev);
365271f41f3eSFrançois Tigeot 	struct drm_i915_gem_object *obj = to_intel_bo(gem);
365371f41f3eSFrançois Tigeot 	struct drm_i915_file_private *fpriv = file->driver_priv;
36543f2dd94aSFrançois Tigeot 	struct i915_lut_handle *lut, *ln;
3655f0b54121SFrançois Tigeot 
36563f2dd94aSFrançois Tigeot 	mutex_lock(&i915->drm.struct_mutex);
36573f2dd94aSFrançois Tigeot 
36583f2dd94aSFrançois Tigeot 	list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
36593f2dd94aSFrançois Tigeot 		struct i915_gem_context *ctx = lut->ctx;
36603f2dd94aSFrançois Tigeot 		struct i915_vma *vma;
36613f2dd94aSFrançois Tigeot 
36623f2dd94aSFrançois Tigeot 		GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
36633f2dd94aSFrançois Tigeot 		if (ctx->file_priv != fpriv)
36643f2dd94aSFrançois Tigeot 			continue;
36653f2dd94aSFrançois Tigeot 
36663f2dd94aSFrançois Tigeot 		vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
36673f2dd94aSFrançois Tigeot 		GEM_BUG_ON(vma->obj != obj);
36683f2dd94aSFrançois Tigeot 
36693f2dd94aSFrançois Tigeot 		/* We allow the process to have multiple handles to the same
36703f2dd94aSFrançois Tigeot 		 * vma, in the same fd namespace, by virtue of flink/open.
36713f2dd94aSFrançois Tigeot 		 */
36723f2dd94aSFrançois Tigeot 		GEM_BUG_ON(!vma->open_count);
36733f2dd94aSFrançois Tigeot 		if (!--vma->open_count && !i915_vma_is_ggtt(vma))
367471f41f3eSFrançois Tigeot 			i915_vma_close(vma);
36754be47400SFrançois Tigeot 
36763f2dd94aSFrançois Tigeot 		list_del(&lut->obj_link);
36773f2dd94aSFrançois Tigeot 		list_del(&lut->ctx_link);
36783f2dd94aSFrançois Tigeot 
36793f2dd94aSFrançois Tigeot 		kmem_cache_free(i915->luts, lut);
36803f2dd94aSFrançois Tigeot 		__i915_gem_object_release_unless_active(obj);
36814be47400SFrançois Tigeot 	}
36823f2dd94aSFrançois Tigeot 
36833f2dd94aSFrançois Tigeot 	mutex_unlock(&i915->drm.struct_mutex);
3684f0b54121SFrançois Tigeot }
3685e11a51e3SFrançois Tigeot 
to_wait_timeout(s64 timeout_ns)36864be47400SFrançois Tigeot static unsigned long to_wait_timeout(s64 timeout_ns)
36874be47400SFrançois Tigeot {
36884be47400SFrançois Tigeot 	if (timeout_ns < 0)
36894be47400SFrançois Tigeot 		return MAX_SCHEDULE_TIMEOUT;
36904be47400SFrançois Tigeot 
36914be47400SFrançois Tigeot 	if (timeout_ns == 0)
36924be47400SFrançois Tigeot 		return 0;
36934be47400SFrançois Tigeot 
36944be47400SFrançois Tigeot 	return nsecs_to_jiffies_timeout(timeout_ns);
36954be47400SFrançois Tigeot }
36964be47400SFrançois Tigeot 
36973d4007e0SFrançois Tigeot /**
3698fabb21f3SFrançois Tigeot  * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
36991487f786SFrançois Tigeot  * @dev: drm device pointer
37001487f786SFrançois Tigeot  * @data: ioctl data blob
37011487f786SFrançois Tigeot  * @file: drm file pointer
3702fabb21f3SFrançois Tigeot  *
3703fabb21f3SFrançois Tigeot  * Returns 0 if successful, else an error is returned with the remaining time in
3704fabb21f3SFrançois Tigeot  * the timeout parameter.
3705fabb21f3SFrançois Tigeot  *  -ETIME: object is still busy after timeout
3706fabb21f3SFrançois Tigeot  *  -ERESTARTSYS: signal interrupted the wait
3707fabb21f3SFrançois Tigeot  *  -ENONENT: object doesn't exist
3708fabb21f3SFrançois Tigeot  * Also possible, but rare:
37093f2dd94aSFrançois Tigeot  *  -EAGAIN: incomplete, restart syscall
3710fabb21f3SFrançois Tigeot  *  -ENOMEM: damn
3711fabb21f3SFrançois Tigeot  *  -ENODEV: Internal IRQ fail
3712fabb21f3SFrançois Tigeot  *  -E?: The add request failed
3713fabb21f3SFrançois Tigeot  *
3714fabb21f3SFrançois Tigeot  * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3715fabb21f3SFrançois Tigeot  * non-zero timeout parameter the wait ioctl will wait for the given number of
3716fabb21f3SFrançois Tigeot  * nanoseconds on an object becoming unbusy. Since the wait itself does so
3717fabb21f3SFrançois Tigeot  * without holding struct_mutex the object may become re-busied before this
3718fabb21f3SFrançois Tigeot  * function completes. A similar but shorter * race condition exists in the busy
3719fabb21f3SFrançois Tigeot  * ioctl
3720fabb21f3SFrançois Tigeot  */
3721fabb21f3SFrançois Tigeot int
i915_gem_wait_ioctl(struct drm_device * dev,void * data,struct drm_file * file)3722fabb21f3SFrançois Tigeot i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3723fabb21f3SFrançois Tigeot {
3724fabb21f3SFrançois Tigeot 	struct drm_i915_gem_wait *args = data;
3725fabb21f3SFrançois Tigeot 	struct drm_i915_gem_object *obj;
37264be47400SFrançois Tigeot 	ktime_t start;
37274be47400SFrançois Tigeot 	long ret;
3728fabb21f3SFrançois Tigeot 
37292c9916cdSFrançois Tigeot 	if (args->flags != 0)
37302c9916cdSFrançois Tigeot 		return -EINVAL;
3731fabb21f3SFrançois Tigeot 
373287df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file, args->bo_handle);
373371f41f3eSFrançois Tigeot 	if (!obj)
3734fabb21f3SFrançois Tigeot 		return -ENOENT;
3735fabb21f3SFrançois Tigeot 
37364be47400SFrançois Tigeot 	start = ktime_get();
3737fabb21f3SFrançois Tigeot 
37384be47400SFrançois Tigeot 	ret = i915_gem_object_wait(obj,
37394be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
37404be47400SFrançois Tigeot 				   to_wait_timeout(args->timeout_ns),
37414be47400SFrançois Tigeot 				   to_rps_client(file));
3742fabb21f3SFrançois Tigeot 
37434be47400SFrançois Tigeot 	if (args->timeout_ns > 0) {
37444be47400SFrançois Tigeot 		args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
37454be47400SFrançois Tigeot 		if (args->timeout_ns < 0)
37464be47400SFrançois Tigeot 			args->timeout_ns = 0;
3747d1c259eeSFrançois Tigeot 
37481b13d190SFrançois Tigeot 		/*
37494be47400SFrançois Tigeot 		 * Apparently ktime isn't accurate enough and occasionally has a
37504be47400SFrançois Tigeot 		 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
37514be47400SFrançois Tigeot 		 * things up to make the test happy. We allow up to 1 jiffy.
37524be47400SFrançois Tigeot 		 *
37534be47400SFrançois Tigeot 		 * This is a regression from the timespec->ktime conversion.
3754d1c259eeSFrançois Tigeot 		 */
37554be47400SFrançois Tigeot 		if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
37564be47400SFrançois Tigeot 			args->timeout_ns = 0;
3757a85cb24fSFrançois Tigeot 
3758a85cb24fSFrançois Tigeot 		/*
3759a85cb24fSFrançois Tigeot 		 * Apparently ktime isn't accurate enough and occasionally has a
3760a85cb24fSFrançois Tigeot 		 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3761a85cb24fSFrançois Tigeot 		 * things up to make the test happy. We allow up to 1 jiffy.
3762a85cb24fSFrançois Tigeot 		 *
3763a85cb24fSFrançois Tigeot 		 * This is a regression from the timespec->ktime conversion.
3764a85cb24fSFrançois Tigeot 		 */
3765a85cb24fSFrançois Tigeot 		if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3766a85cb24fSFrançois Tigeot 			args->timeout_ns = 0;
37673f2dd94aSFrançois Tigeot 
37683f2dd94aSFrançois Tigeot 		/* Asked to wait beyond the jiffie/scheduler precision? */
37693f2dd94aSFrançois Tigeot 		if (ret == -ETIME && args->timeout_ns)
37703f2dd94aSFrançois Tigeot 			ret = -EAGAIN;
3771d1c259eeSFrançois Tigeot 	}
3772d1c259eeSFrançois Tigeot 
37734be47400SFrançois Tigeot 	i915_gem_object_put(obj);
37744be47400SFrançois Tigeot 	return ret;
37754be47400SFrançois Tigeot }
37764be47400SFrançois Tigeot 
wait_for_timeline(struct i915_gem_timeline * tl,unsigned int flags)37774be47400SFrançois Tigeot static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
3778e3adcf8fSFrançois Tigeot {
37794be47400SFrançois Tigeot 	int ret, i;
3780e3adcf8fSFrançois Tigeot 
37814be47400SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
37824be47400SFrançois Tigeot 		ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3783d1c259eeSFrançois Tigeot 		if (ret)
378471f41f3eSFrançois Tigeot 			return ret;
3785aee94f86SFrançois Tigeot 	}
378671f41f3eSFrançois Tigeot 
378771f41f3eSFrançois Tigeot 	return 0;
37884be47400SFrançois Tigeot }
37899edbd4a0SFrançois Tigeot 
wait_for_engines(struct drm_i915_private * i915)3790a85cb24fSFrançois Tigeot static int wait_for_engines(struct drm_i915_private *i915)
3791a85cb24fSFrançois Tigeot {
37923f2dd94aSFrançois Tigeot 	if (wait_for(intel_engines_are_idle(i915), 50)) {
37933f2dd94aSFrançois Tigeot 		DRM_ERROR("Failed to idle engines, declaring wedged!\n");
3794a85cb24fSFrançois Tigeot 		i915_gem_set_wedged(i915);
3795a85cb24fSFrançois Tigeot 		return -EIO;
3796a85cb24fSFrançois Tigeot 	}
3797a85cb24fSFrançois Tigeot 
3798a85cb24fSFrançois Tigeot 	return 0;
3799a85cb24fSFrançois Tigeot }
3800a85cb24fSFrançois Tigeot 
i915_gem_wait_for_idle(struct drm_i915_private * i915,unsigned int flags)38014be47400SFrançois Tigeot int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
38024be47400SFrançois Tigeot {
38034be47400SFrançois Tigeot 	int ret;
38044be47400SFrançois Tigeot 
3805a85cb24fSFrançois Tigeot 	/* If the device is asleep, we have no requests outstanding */
3806a85cb24fSFrançois Tigeot 	if (!READ_ONCE(i915->gt.awake))
3807a85cb24fSFrançois Tigeot 		return 0;
3808a85cb24fSFrançois Tigeot 
38094be47400SFrançois Tigeot 	if (flags & I915_WAIT_LOCKED) {
38104be47400SFrançois Tigeot 		struct i915_gem_timeline *tl;
38114be47400SFrançois Tigeot 
38124be47400SFrançois Tigeot 		lockdep_assert_held(&i915->drm.struct_mutex);
38134be47400SFrançois Tigeot 
38144be47400SFrançois Tigeot 		list_for_each_entry(tl, &i915->gt.timelines, link) {
38154be47400SFrançois Tigeot 			ret = wait_for_timeline(tl, flags);
38164be47400SFrançois Tigeot 			if (ret)
38174be47400SFrançois Tigeot 				return ret;
38184be47400SFrançois Tigeot 		}
3819a85cb24fSFrançois Tigeot 
3820a85cb24fSFrançois Tigeot 		i915_gem_retire_requests(i915);
3821a85cb24fSFrançois Tigeot 		GEM_BUG_ON(i915->gt.active_requests);
3822a85cb24fSFrançois Tigeot 
3823a85cb24fSFrançois Tigeot 		ret = wait_for_engines(i915);
38244be47400SFrançois Tigeot 	} else {
38254be47400SFrançois Tigeot 		ret = wait_for_timeline(&i915->gt.global_timeline, flags);
3826a85cb24fSFrançois Tigeot 	}
3827a85cb24fSFrançois Tigeot 
382871f41f3eSFrançois Tigeot 	return ret;
3829e11a51e3SFrançois Tigeot }
3830e11a51e3SFrançois Tigeot 
__i915_gem_object_flush_for_display(struct drm_i915_gem_object * obj)3831a85cb24fSFrançois Tigeot static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3832a85cb24fSFrançois Tigeot {
38333f2dd94aSFrançois Tigeot 	/*
38343f2dd94aSFrançois Tigeot 	 * We manually flush the CPU domain so that we can override and
38353f2dd94aSFrançois Tigeot 	 * force the flush for the display, and perform it asyncrhonously.
38363f2dd94aSFrançois Tigeot 	 */
38373f2dd94aSFrançois Tigeot 	flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
38383f2dd94aSFrançois Tigeot 	if (obj->cache_dirty)
3839a85cb24fSFrançois Tigeot 		i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
3840a85cb24fSFrançois Tigeot 	obj->base.write_domain = 0;
3841a85cb24fSFrançois Tigeot }
3842a85cb24fSFrançois Tigeot 
i915_gem_object_flush_if_display(struct drm_i915_gem_object * obj)3843a85cb24fSFrançois Tigeot void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3844a85cb24fSFrançois Tigeot {
38453f2dd94aSFrançois Tigeot 	if (!READ_ONCE(obj->pin_global))
3846a85cb24fSFrançois Tigeot 		return;
3847a85cb24fSFrançois Tigeot 
3848a85cb24fSFrançois Tigeot 	mutex_lock(&obj->base.dev->struct_mutex);
3849a85cb24fSFrançois Tigeot 	__i915_gem_object_flush_for_display(obj);
3850a85cb24fSFrançois Tigeot 	mutex_unlock(&obj->base.dev->struct_mutex);
38511e12ee3bSFrançois Tigeot }
38521e12ee3bSFrançois Tigeot 
3853e11a51e3SFrançois Tigeot /**
38543f2dd94aSFrançois Tigeot  * Moves a single object to the WC read, and possibly write domain.
38553f2dd94aSFrançois Tigeot  * @obj: object to act on
38563f2dd94aSFrançois Tigeot  * @write: ask for write access or read only
38573f2dd94aSFrançois Tigeot  *
38583f2dd94aSFrançois Tigeot  * This function returns when the move is complete, including waiting on
38593f2dd94aSFrançois Tigeot  * flushes to occur.
38603f2dd94aSFrançois Tigeot  */
38613f2dd94aSFrançois Tigeot int
i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object * obj,bool write)38623f2dd94aSFrançois Tigeot i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
38633f2dd94aSFrançois Tigeot {
38643f2dd94aSFrançois Tigeot 	int ret;
38653f2dd94aSFrançois Tigeot 
38663f2dd94aSFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
38673f2dd94aSFrançois Tigeot 
38683f2dd94aSFrançois Tigeot 	ret = i915_gem_object_wait(obj,
38693f2dd94aSFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE |
38703f2dd94aSFrançois Tigeot 				   I915_WAIT_LOCKED |
38713f2dd94aSFrançois Tigeot 				   (write ? I915_WAIT_ALL : 0),
38723f2dd94aSFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
38733f2dd94aSFrançois Tigeot 				   NULL);
38743f2dd94aSFrançois Tigeot 	if (ret)
38753f2dd94aSFrançois Tigeot 		return ret;
38763f2dd94aSFrançois Tigeot 
38773f2dd94aSFrançois Tigeot 	if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
38783f2dd94aSFrançois Tigeot 		return 0;
38793f2dd94aSFrançois Tigeot 
38803f2dd94aSFrançois Tigeot 	/* Flush and acquire obj->pages so that we are coherent through
38813f2dd94aSFrançois Tigeot 	 * direct access in memory with previous cached writes through
38823f2dd94aSFrançois Tigeot 	 * shmemfs and that our cache domain tracking remains valid.
38833f2dd94aSFrançois Tigeot 	 * For example, if the obj->filp was moved to swap without us
38843f2dd94aSFrançois Tigeot 	 * being notified and releasing the pages, we would mistakenly
38853f2dd94aSFrançois Tigeot 	 * continue to assume that the obj remained out of the CPU cached
38863f2dd94aSFrançois Tigeot 	 * domain.
38873f2dd94aSFrançois Tigeot 	 */
38883f2dd94aSFrançois Tigeot 	ret = i915_gem_object_pin_pages(obj);
38893f2dd94aSFrançois Tigeot 	if (ret)
38903f2dd94aSFrançois Tigeot 		return ret;
38913f2dd94aSFrançois Tigeot 
38923f2dd94aSFrançois Tigeot 	flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
38933f2dd94aSFrançois Tigeot 
38943f2dd94aSFrançois Tigeot 	/* Serialise direct access to this object with the barriers for
38953f2dd94aSFrançois Tigeot 	 * coherent writes from the GPU, by effectively invalidating the
38963f2dd94aSFrançois Tigeot 	 * WC domain upon first access.
38973f2dd94aSFrançois Tigeot 	 */
38983f2dd94aSFrançois Tigeot 	if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
38993f2dd94aSFrançois Tigeot 		mb();
39003f2dd94aSFrançois Tigeot 
39013f2dd94aSFrançois Tigeot 	/* It should now be out of any other write domains, and we can update
39023f2dd94aSFrançois Tigeot 	 * the domain values for our changes.
39033f2dd94aSFrançois Tigeot 	 */
39043f2dd94aSFrançois Tigeot 	GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
39053f2dd94aSFrançois Tigeot 	obj->base.read_domains |= I915_GEM_DOMAIN_WC;
39063f2dd94aSFrançois Tigeot 	if (write) {
39073f2dd94aSFrançois Tigeot 		obj->base.read_domains = I915_GEM_DOMAIN_WC;
39083f2dd94aSFrançois Tigeot 		obj->base.write_domain = I915_GEM_DOMAIN_WC;
39093f2dd94aSFrançois Tigeot 		obj->mm.dirty = true;
39103f2dd94aSFrançois Tigeot 	}
39113f2dd94aSFrançois Tigeot 
39123f2dd94aSFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
39133f2dd94aSFrançois Tigeot 	return 0;
39143f2dd94aSFrançois Tigeot }
39153f2dd94aSFrançois Tigeot 
39163f2dd94aSFrançois Tigeot /**
3917e11a51e3SFrançois Tigeot  * Moves a single object to the GTT read, and possibly write domain.
39181487f786SFrançois Tigeot  * @obj: object to act on
39191487f786SFrançois Tigeot  * @write: ask for write access or read only
3920e11a51e3SFrançois Tigeot  *
3921e11a51e3SFrançois Tigeot  * This function returns when the move is complete, including waiting on
3922e11a51e3SFrançois Tigeot  * flushes to occur.
3923e11a51e3SFrançois Tigeot  */
3924e11a51e3SFrançois Tigeot int
i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object * obj,bool write)3925e11a51e3SFrançois Tigeot i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
3926e11a51e3SFrançois Tigeot {
3927e11a51e3SFrançois Tigeot 	int ret;
3928e11a51e3SFrançois Tigeot 
39294be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
39304be47400SFrançois Tigeot 
39314be47400SFrançois Tigeot 	ret = i915_gem_object_wait(obj,
39324be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE |
39334be47400SFrançois Tigeot 				   I915_WAIT_LOCKED |
39344be47400SFrançois Tigeot 				   (write ? I915_WAIT_ALL : 0),
39354be47400SFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
39364be47400SFrançois Tigeot 				   NULL);
3937686a02f1SFrançois Tigeot 	if (ret)
3938686a02f1SFrançois Tigeot 		return ret;
3939e11a51e3SFrançois Tigeot 
394087df8fc6SFrançois Tigeot 	if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
394187df8fc6SFrançois Tigeot 		return 0;
394287df8fc6SFrançois Tigeot 
39432c9916cdSFrançois Tigeot 	/* Flush and acquire obj->pages so that we are coherent through
39442c9916cdSFrançois Tigeot 	 * direct access in memory with previous cached writes through
39452c9916cdSFrançois Tigeot 	 * shmemfs and that our cache domain tracking remains valid.
39462c9916cdSFrançois Tigeot 	 * For example, if the obj->filp was moved to swap without us
39472c9916cdSFrançois Tigeot 	 * being notified and releasing the pages, we would mistakenly
39482c9916cdSFrançois Tigeot 	 * continue to assume that the obj remained out of the CPU cached
39492c9916cdSFrançois Tigeot 	 * domain.
39502c9916cdSFrançois Tigeot 	 */
39514be47400SFrançois Tigeot 	ret = i915_gem_object_pin_pages(obj);
39522c9916cdSFrançois Tigeot 	if (ret)
39532c9916cdSFrançois Tigeot 		return ret;
39542c9916cdSFrançois Tigeot 
39553f2dd94aSFrançois Tigeot 	flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
3956e11a51e3SFrançois Tigeot 
3957a2fdbec6SFrançois Tigeot 	/* Serialise direct access to this object with the barriers for
3958a2fdbec6SFrançois Tigeot 	 * coherent writes from the GPU, by effectively invalidating the
3959a2fdbec6SFrançois Tigeot 	 * GTT domain upon first access.
3960a2fdbec6SFrançois Tigeot 	 */
3961a2fdbec6SFrançois Tigeot 	if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
39629edbd4a0SFrançois Tigeot 		mb();
3963a2fdbec6SFrançois Tigeot 
3964686a02f1SFrançois Tigeot 	/* It should now be out of any other write domains, and we can update
3965686a02f1SFrançois Tigeot 	 * the domain values for our changes.
3966686a02f1SFrançois Tigeot 	 */
39674be47400SFrançois Tigeot 	GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3968e11a51e3SFrançois Tigeot 	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3969e11a51e3SFrançois Tigeot 	if (write) {
3970e11a51e3SFrançois Tigeot 		obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3971e11a51e3SFrançois Tigeot 		obj->base.write_domain = I915_GEM_DOMAIN_GTT;
39724be47400SFrançois Tigeot 		obj->mm.dirty = true;
3973e11a51e3SFrançois Tigeot 	}
3974e11a51e3SFrançois Tigeot 
39754be47400SFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
3976686a02f1SFrançois Tigeot 	return 0;
3977e11a51e3SFrançois Tigeot }
3978e11a51e3SFrançois Tigeot 
3979352ff8bdSFrançois Tigeot /**
3980352ff8bdSFrançois Tigeot  * Changes the cache-level of an object across all VMA.
39811487f786SFrançois Tigeot  * @obj: object to act on
39821487f786SFrançois Tigeot  * @cache_level: new cache level to set for the object
3983352ff8bdSFrançois Tigeot  *
3984352ff8bdSFrançois Tigeot  * After this function returns, the object will be in the new cache-level
3985352ff8bdSFrançois Tigeot  * across all GTT and the contents of the backing storage will be coherent,
3986352ff8bdSFrançois Tigeot  * with respect to the new cache-level. In order to keep the backing storage
3987352ff8bdSFrançois Tigeot  * coherent for all users, we only allow a single cache level to be set
3988352ff8bdSFrançois Tigeot  * globally on the object and prevent it from being changed whilst the
3989352ff8bdSFrançois Tigeot  * hardware is reading from the object. That is if the object is currently
3990352ff8bdSFrançois Tigeot  * on the scanout it will be set to uncached (or equivalent display
3991352ff8bdSFrançois Tigeot  * cache coherency) and all non-MOCS GPU access will also be uncached so
3992352ff8bdSFrançois Tigeot  * that all direct access to the scanout remains coherent.
3993352ff8bdSFrançois Tigeot  */
i915_gem_object_set_cache_level(struct drm_i915_gem_object * obj,enum i915_cache_level cache_level)3994e11a51e3SFrançois Tigeot int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3995e11a51e3SFrançois Tigeot 				    enum i915_cache_level cache_level)
3996e11a51e3SFrançois Tigeot {
399771f41f3eSFrançois Tigeot 	struct i915_vma *vma;
39984be47400SFrançois Tigeot 	int ret;
39994be47400SFrançois Tigeot 
40004be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
4001e11a51e3SFrançois Tigeot 
4002e11a51e3SFrançois Tigeot 	if (obj->cache_level == cache_level)
40034be47400SFrançois Tigeot 		return 0;
4004e11a51e3SFrançois Tigeot 
4005352ff8bdSFrançois Tigeot 	/* Inspect the list of currently bound VMA and unbind any that would
4006352ff8bdSFrançois Tigeot 	 * be invalid given the new cache-level. This is principally to
4007352ff8bdSFrançois Tigeot 	 * catch the issue of the CS prefetch crossing page boundaries and
4008352ff8bdSFrançois Tigeot 	 * reading an invalid PTE on older architectures.
4009352ff8bdSFrançois Tigeot 	 */
401071f41f3eSFrançois Tigeot restart:
401171f41f3eSFrançois Tigeot 	list_for_each_entry(vma, &obj->vma_list, obj_link) {
4012352ff8bdSFrançois Tigeot 		if (!drm_mm_node_allocated(&vma->node))
4013352ff8bdSFrançois Tigeot 			continue;
4014352ff8bdSFrançois Tigeot 
401571f41f3eSFrançois Tigeot 		if (i915_vma_is_pinned(vma)) {
4016e11a51e3SFrançois Tigeot 			DRM_DEBUG("can not change the cache level of pinned objects\n");
4017e11a51e3SFrançois Tigeot 			return -EBUSY;
4018e11a51e3SFrançois Tigeot 		}
4019e11a51e3SFrançois Tigeot 
402071f41f3eSFrançois Tigeot 		if (i915_gem_valid_gtt_space(vma, cache_level))
402171f41f3eSFrançois Tigeot 			continue;
402271f41f3eSFrançois Tigeot 
40239edbd4a0SFrançois Tigeot 		ret = i915_vma_unbind(vma);
4024d1c259eeSFrançois Tigeot 		if (ret)
4025d1c259eeSFrançois Tigeot 			return ret;
402671f41f3eSFrançois Tigeot 
402771f41f3eSFrançois Tigeot 		/* As unbinding may affect other elements in the
402871f41f3eSFrançois Tigeot 		 * obj->vma_list (due to side-effects from retiring
402971f41f3eSFrançois Tigeot 		 * an active vma), play safe and restart the iterator.
403071f41f3eSFrançois Tigeot 		 */
403171f41f3eSFrançois Tigeot 		goto restart;
4032d1c259eeSFrançois Tigeot 	}
4033d1c259eeSFrançois Tigeot 
4034352ff8bdSFrançois Tigeot 	/* We can reuse the existing drm_mm nodes but need to change the
4035352ff8bdSFrançois Tigeot 	 * cache-level on the PTE. We could simply unbind them all and
4036352ff8bdSFrançois Tigeot 	 * rebind with the correct cache-level on next use. However since
4037352ff8bdSFrançois Tigeot 	 * we already have a valid slot, dma mapping, pages etc, we may as
4038352ff8bdSFrançois Tigeot 	 * rewrite the PTE in the belief that doing so tramples upon less
4039352ff8bdSFrançois Tigeot 	 * state and so involves less work.
4040352ff8bdSFrançois Tigeot 	 */
404171f41f3eSFrançois Tigeot 	if (obj->bind_count) {
4042352ff8bdSFrançois Tigeot 		/* Before we change the PTE, the GPU must not be accessing it.
4043352ff8bdSFrançois Tigeot 		 * If we wait upon the object, we know that all the bound
4044352ff8bdSFrançois Tigeot 		 * VMA are no longer active.
4045352ff8bdSFrançois Tigeot 		 */
40464be47400SFrançois Tigeot 		ret = i915_gem_object_wait(obj,
40474be47400SFrançois Tigeot 					   I915_WAIT_INTERRUPTIBLE |
40484be47400SFrançois Tigeot 					   I915_WAIT_LOCKED |
40494be47400SFrançois Tigeot 					   I915_WAIT_ALL,
40504be47400SFrançois Tigeot 					   MAX_SCHEDULE_TIMEOUT,
40514be47400SFrançois Tigeot 					   NULL);
4052d2557f23SFrançois Tigeot 		if (ret)
4053d2557f23SFrançois Tigeot 			return ret;
4054e11a51e3SFrançois Tigeot 
40554be47400SFrançois Tigeot 		if (!HAS_LLC(to_i915(obj->base.dev)) &&
40564be47400SFrançois Tigeot 		    cache_level != I915_CACHE_NONE) {
4057352ff8bdSFrançois Tigeot 			/* Access to snoopable pages through the GTT is
4058352ff8bdSFrançois Tigeot 			 * incoherent and on some machines causes a hard
4059352ff8bdSFrançois Tigeot 			 * lockup. Relinquish the CPU mmaping to force
4060352ff8bdSFrançois Tigeot 			 * userspace to refault in the pages and we can
4061352ff8bdSFrançois Tigeot 			 * then double check if the GTT mapping is still
4062352ff8bdSFrançois Tigeot 			 * valid for that pointer access.
4063e11a51e3SFrançois Tigeot 			 */
4064352ff8bdSFrançois Tigeot 			i915_gem_release_mmap(obj);
4065352ff8bdSFrançois Tigeot 
4066352ff8bdSFrançois Tigeot 			/* As we no longer need a fence for GTT access,
4067352ff8bdSFrançois Tigeot 			 * we can relinquish it now (and so prevent having
4068352ff8bdSFrançois Tigeot 			 * to steal a fence from someone else on the next
4069352ff8bdSFrançois Tigeot 			 * fence request). Note GPU activity would have
4070352ff8bdSFrançois Tigeot 			 * dropped the fence as all snoopable access is
4071352ff8bdSFrançois Tigeot 			 * supposed to be linear.
4072352ff8bdSFrançois Tigeot 			 */
40731e12ee3bSFrançois Tigeot 			list_for_each_entry(vma, &obj->vma_list, obj_link) {
40741e12ee3bSFrançois Tigeot 				ret = i915_vma_put_fence(vma);
4075e11a51e3SFrançois Tigeot 				if (ret)
4076e11a51e3SFrançois Tigeot 					return ret;
40771e12ee3bSFrançois Tigeot 			}
4078352ff8bdSFrançois Tigeot 		} else {
4079352ff8bdSFrançois Tigeot 			/* We either have incoherent backing store and
4080352ff8bdSFrançois Tigeot 			 * so no GTT access or the architecture is fully
4081352ff8bdSFrançois Tigeot 			 * coherent. In such cases, existing GTT mmaps
4082352ff8bdSFrançois Tigeot 			 * ignore the cache bit in the PTE and we can
4083352ff8bdSFrançois Tigeot 			 * rewrite it without confusing the GPU or having
4084352ff8bdSFrançois Tigeot 			 * to force userspace to fault back in its mmaps.
4085352ff8bdSFrançois Tigeot 			 */
4086e11a51e3SFrançois Tigeot 		}
4087e11a51e3SFrançois Tigeot 
4088c0e85e96SFrançois Tigeot 		list_for_each_entry(vma, &obj->vma_list, obj_link) {
4089352ff8bdSFrançois Tigeot 			if (!drm_mm_node_allocated(&vma->node))
4090352ff8bdSFrançois Tigeot 				continue;
4091352ff8bdSFrançois Tigeot 
4092352ff8bdSFrançois Tigeot 			ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
40932c9916cdSFrançois Tigeot 			if (ret)
40942c9916cdSFrançois Tigeot 				return ret;
40952c9916cdSFrançois Tigeot 		}
4096e11a51e3SFrançois Tigeot 	}
4097e11a51e3SFrançois Tigeot 
4098c0e85e96SFrançois Tigeot 	list_for_each_entry(vma, &obj->vma_list, obj_link)
40999edbd4a0SFrançois Tigeot 		vma->node.color = cache_level;
41003f2dd94aSFrançois Tigeot 	i915_gem_object_set_cache_coherency(obj, cache_level);
41013f2dd94aSFrançois Tigeot 	obj->cache_dirty = true; /* Always invalidate stale cachelines */
41029edbd4a0SFrançois Tigeot 
4103e11a51e3SFrançois Tigeot 	return 0;
4104e11a51e3SFrançois Tigeot }
4105e11a51e3SFrançois Tigeot 
i915_gem_get_caching_ioctl(struct drm_device * dev,void * data,struct drm_file * file)4106e61afc2bSFrançois Tigeot int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4107e61afc2bSFrançois Tigeot 			       struct drm_file *file)
4108e61afc2bSFrançois Tigeot {
4109e61afc2bSFrançois Tigeot 	struct drm_i915_gem_caching *args = data;
4110e61afc2bSFrançois Tigeot 	struct drm_i915_gem_object *obj;
41114be47400SFrançois Tigeot 	int err = 0;
4112e61afc2bSFrançois Tigeot 
41134be47400SFrançois Tigeot 	rcu_read_lock();
41144be47400SFrançois Tigeot 	obj = i915_gem_object_lookup_rcu(file, args->handle);
41154be47400SFrançois Tigeot 	if (!obj) {
41164be47400SFrançois Tigeot 		err = -ENOENT;
41174be47400SFrançois Tigeot 		goto out;
41184be47400SFrançois Tigeot 	}
4119e61afc2bSFrançois Tigeot 
41209edbd4a0SFrançois Tigeot 	switch (obj->cache_level) {
41219edbd4a0SFrançois Tigeot 	case I915_CACHE_LLC:
41229edbd4a0SFrançois Tigeot 	case I915_CACHE_L3_LLC:
41239edbd4a0SFrançois Tigeot 		args->caching = I915_CACHING_CACHED;
41249edbd4a0SFrançois Tigeot 		break;
41259edbd4a0SFrançois Tigeot 
41269edbd4a0SFrançois Tigeot 	case I915_CACHE_WT:
41279edbd4a0SFrançois Tigeot 		args->caching = I915_CACHING_DISPLAY;
41289edbd4a0SFrançois Tigeot 		break;
41299edbd4a0SFrançois Tigeot 
41309edbd4a0SFrançois Tigeot 	default:
41319edbd4a0SFrançois Tigeot 		args->caching = I915_CACHING_NONE;
41329edbd4a0SFrançois Tigeot 		break;
41339edbd4a0SFrançois Tigeot 	}
41344be47400SFrançois Tigeot out:
41354be47400SFrançois Tigeot 	rcu_read_unlock();
41364be47400SFrançois Tigeot 	return err;
4137e61afc2bSFrançois Tigeot }
4138e61afc2bSFrançois Tigeot 
i915_gem_set_caching_ioctl(struct drm_device * dev,void * data,struct drm_file * file)4139e61afc2bSFrançois Tigeot int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4140e61afc2bSFrançois Tigeot 			       struct drm_file *file)
4141e61afc2bSFrançois Tigeot {
41424be47400SFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(dev);
4143e61afc2bSFrançois Tigeot 	struct drm_i915_gem_caching *args = data;
4144e61afc2bSFrançois Tigeot 	struct drm_i915_gem_object *obj;
4145e61afc2bSFrançois Tigeot 	enum i915_cache_level level;
4146a85cb24fSFrançois Tigeot 	int ret = 0;
4147e61afc2bSFrançois Tigeot 
4148e61afc2bSFrançois Tigeot 	switch (args->caching) {
4149e61afc2bSFrançois Tigeot 	case I915_CACHING_NONE:
4150e61afc2bSFrançois Tigeot 		level = I915_CACHE_NONE;
4151e61afc2bSFrançois Tigeot 		break;
4152e61afc2bSFrançois Tigeot 	case I915_CACHING_CACHED:
4153352ff8bdSFrançois Tigeot 		/*
4154352ff8bdSFrançois Tigeot 		 * Due to a HW issue on BXT A stepping, GPU stores via a
4155352ff8bdSFrançois Tigeot 		 * snooped mapping may leave stale data in a corresponding CPU
4156352ff8bdSFrançois Tigeot 		 * cacheline, whereas normally such cachelines would get
4157352ff8bdSFrançois Tigeot 		 * invalidated.
4158352ff8bdSFrançois Tigeot 		 */
41594be47400SFrançois Tigeot 		if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
4160352ff8bdSFrançois Tigeot 			return -ENODEV;
4161352ff8bdSFrançois Tigeot 
4162e61afc2bSFrançois Tigeot 		level = I915_CACHE_LLC;
4163e61afc2bSFrançois Tigeot 		break;
41649edbd4a0SFrançois Tigeot 	case I915_CACHING_DISPLAY:
41654be47400SFrançois Tigeot 		level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
41669edbd4a0SFrançois Tigeot 		break;
4167e61afc2bSFrançois Tigeot 	default:
4168e61afc2bSFrançois Tigeot 		return -EINVAL;
4169e61afc2bSFrançois Tigeot 	}
4170e61afc2bSFrançois Tigeot 
4171a85cb24fSFrançois Tigeot 	obj = i915_gem_object_lookup(file, args->handle);
4172a85cb24fSFrançois Tigeot 	if (!obj)
4173a85cb24fSFrançois Tigeot 		return -ENOENT;
4174a85cb24fSFrançois Tigeot 
4175a85cb24fSFrançois Tigeot 	if (obj->cache_level == level)
4176a85cb24fSFrançois Tigeot 		goto out;
4177a85cb24fSFrançois Tigeot 
4178a85cb24fSFrançois Tigeot 	ret = i915_gem_object_wait(obj,
4179a85cb24fSFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE,
4180a85cb24fSFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
4181a85cb24fSFrançois Tigeot 				   to_rps_client(file));
4182a85cb24fSFrançois Tigeot 	if (ret)
4183a85cb24fSFrançois Tigeot 		goto out;
4184a85cb24fSFrançois Tigeot 
4185e61afc2bSFrançois Tigeot 	ret = i915_mutex_lock_interruptible(dev);
4186e61afc2bSFrançois Tigeot 	if (ret)
4187a85cb24fSFrançois Tigeot 		goto out;
4188e61afc2bSFrançois Tigeot 
4189e61afc2bSFrançois Tigeot 	ret = i915_gem_object_set_cache_level(obj, level);
4190a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
4191a85cb24fSFrançois Tigeot 
4192a85cb24fSFrançois Tigeot out:
4193a85cb24fSFrançois Tigeot 	i915_gem_object_put(obj);
4194e61afc2bSFrançois Tigeot 	return ret;
4195e61afc2bSFrançois Tigeot }
4196e61afc2bSFrançois Tigeot 
4197e11a51e3SFrançois Tigeot /*
4198e11a51e3SFrançois Tigeot  * Prepare buffer for display plane (scanout, cursors, etc).
4199e11a51e3SFrançois Tigeot  * Can be called from an uninterruptible phase (modesetting) and allows
4200e11a51e3SFrançois Tigeot  * any flushes to be pipelined (for pageflips).
4201e11a51e3SFrançois Tigeot  */
42021e12ee3bSFrançois Tigeot struct i915_vma *
i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object * obj,u32 alignment,const struct i915_ggtt_view * view)4203e11a51e3SFrançois Tigeot i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4204e11a51e3SFrançois Tigeot 				     u32 alignment,
4205477eb7f9SFrançois Tigeot 				     const struct i915_ggtt_view *view)
4206e11a51e3SFrançois Tigeot {
42071e12ee3bSFrançois Tigeot 	struct i915_vma *vma;
4208e11a51e3SFrançois Tigeot 	int ret;
4209e11a51e3SFrançois Tigeot 
42104be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
42114be47400SFrançois Tigeot 
42123f2dd94aSFrançois Tigeot 	/* Mark the global pin early so that we account for the
42139edbd4a0SFrançois Tigeot 	 * display coherency whilst setting up the cache domains.
42149edbd4a0SFrançois Tigeot 	 */
42153f2dd94aSFrançois Tigeot 	obj->pin_global++;
42169edbd4a0SFrançois Tigeot 
4217686a02f1SFrançois Tigeot 	/* The display engine is not coherent with the LLC cache on gen6.  As
4218686a02f1SFrançois Tigeot 	 * a result, we make sure that the pinning that is about to occur is
4219686a02f1SFrançois Tigeot 	 * done with uncached PTEs. This is lowest common denominator for all
4220686a02f1SFrançois Tigeot 	 * chipsets.
4221686a02f1SFrançois Tigeot 	 *
4222686a02f1SFrançois Tigeot 	 * However for gen6+, we could do better by using the GFDT bit instead
4223686a02f1SFrançois Tigeot 	 * of uncaching, which would allow us to flush all the LLC-cached data
4224686a02f1SFrançois Tigeot 	 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4225686a02f1SFrançois Tigeot 	 */
42269edbd4a0SFrançois Tigeot 	ret = i915_gem_object_set_cache_level(obj,
42271e12ee3bSFrançois Tigeot 					      HAS_WT(to_i915(obj->base.dev)) ?
42281e12ee3bSFrançois Tigeot 					      I915_CACHE_WT : I915_CACHE_NONE);
42291e12ee3bSFrançois Tigeot 	if (ret) {
42301e12ee3bSFrançois Tigeot 		vma = ERR_PTR(ret);
42313f2dd94aSFrançois Tigeot 		goto err_unpin_global;
42321e12ee3bSFrançois Tigeot 	}
4233e11a51e3SFrançois Tigeot 
4234686a02f1SFrançois Tigeot 	/* As the user may map the buffer once pinned in the display plane
4235686a02f1SFrançois Tigeot 	 * (e.g. libkms for the bootup splash), we have to ensure that we
42361e12ee3bSFrançois Tigeot 	 * always use map_and_fenceable for all scanout buffers. However,
42371e12ee3bSFrançois Tigeot 	 * it may simply be too big to fit into mappable, in which case
42381e12ee3bSFrançois Tigeot 	 * put it anyway and hope that userspace can cope (but always first
42391e12ee3bSFrançois Tigeot 	 * try to preserve the existing ABI).
4240686a02f1SFrançois Tigeot 	 */
42411e12ee3bSFrançois Tigeot 	vma = ERR_PTR(-ENOSPC);
4242a85cb24fSFrançois Tigeot 	if (!view || view->type == I915_GGTT_VIEW_NORMAL)
42431e12ee3bSFrançois Tigeot 		vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
42441e12ee3bSFrançois Tigeot 					       PIN_MAPPABLE | PIN_NONBLOCK);
42454be47400SFrançois Tigeot 	if (IS_ERR(vma)) {
42464be47400SFrançois Tigeot 		struct drm_i915_private *i915 = to_i915(obj->base.dev);
42474be47400SFrançois Tigeot 		unsigned int flags;
42484be47400SFrançois Tigeot 
42494be47400SFrançois Tigeot 		/* Valleyview is definitely limited to scanning out the first
42504be47400SFrançois Tigeot 		 * 512MiB. Lets presume this behaviour was inherited from the
42514be47400SFrançois Tigeot 		 * g4x display engine and that all earlier gen are similarly
42524be47400SFrançois Tigeot 		 * limited. Testing suggests that it is a little more
42534be47400SFrançois Tigeot 		 * complicated than this. For example, Cherryview appears quite
42544be47400SFrançois Tigeot 		 * happy to scanout from anywhere within its global aperture.
42554be47400SFrançois Tigeot 		 */
42564be47400SFrançois Tigeot 		flags = 0;
42574be47400SFrançois Tigeot 		if (HAS_GMCH_DISPLAY(i915))
42584be47400SFrançois Tigeot 			flags = PIN_MAPPABLE;
42594be47400SFrançois Tigeot 		vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
42604be47400SFrançois Tigeot 	}
42611e12ee3bSFrançois Tigeot 	if (IS_ERR(vma))
42623f2dd94aSFrançois Tigeot 		goto err_unpin_global;
4263e11a51e3SFrançois Tigeot 
42641e12ee3bSFrançois Tigeot 	vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
42651e12ee3bSFrançois Tigeot 
42664be47400SFrançois Tigeot 	/* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
4267a85cb24fSFrançois Tigeot 	__i915_gem_object_flush_for_display(obj);
4268a85cb24fSFrançois Tigeot 	intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
4269e11a51e3SFrançois Tigeot 
4270686a02f1SFrançois Tigeot 	/* It should now be out of any other write domains, and we can update
4271686a02f1SFrançois Tigeot 	 * the domain values for our changes.
4272686a02f1SFrançois Tigeot 	 */
4273e11a51e3SFrançois Tigeot 	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
4274e11a51e3SFrançois Tigeot 
42751e12ee3bSFrançois Tigeot 	return vma;
42769edbd4a0SFrançois Tigeot 
42773f2dd94aSFrançois Tigeot err_unpin_global:
42783f2dd94aSFrançois Tigeot 	obj->pin_global--;
42791e12ee3bSFrançois Tigeot 	return vma;
42809edbd4a0SFrançois Tigeot }
42819edbd4a0SFrançois Tigeot 
42829edbd4a0SFrançois Tigeot void
i915_gem_object_unpin_from_display_plane(struct i915_vma * vma)42831e12ee3bSFrançois Tigeot i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
42849edbd4a0SFrançois Tigeot {
4285a85cb24fSFrançois Tigeot 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
42864be47400SFrançois Tigeot 
42873f2dd94aSFrançois Tigeot 	if (WARN_ON(vma->obj->pin_global == 0))
428819c468b4SFrançois Tigeot 		return;
428919c468b4SFrançois Tigeot 
42903f2dd94aSFrançois Tigeot 	if (--vma->obj->pin_global == 0)
4291a85cb24fSFrançois Tigeot 		vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
4292477eb7f9SFrançois Tigeot 
42931e12ee3bSFrançois Tigeot 	/* Bump the LRU to try and avoid premature eviction whilst flipping  */
4294a85cb24fSFrançois Tigeot 	i915_gem_object_bump_inactive_ggtt(vma->obj);
42951e12ee3bSFrançois Tigeot 
42961e12ee3bSFrançois Tigeot 	i915_vma_unpin(vma);
4297e11a51e3SFrançois Tigeot }
4298e11a51e3SFrançois Tigeot 
4299e11a51e3SFrançois Tigeot /**
4300e11a51e3SFrançois Tigeot  * Moves a single object to the CPU read, and possibly write domain.
43011487f786SFrançois Tigeot  * @obj: object to act on
43021487f786SFrançois Tigeot  * @write: requesting write or read-only access
4303e11a51e3SFrançois Tigeot  *
4304e11a51e3SFrançois Tigeot  * This function returns when the move is complete, including waiting on
4305e11a51e3SFrançois Tigeot  * flushes to occur.
4306e11a51e3SFrançois Tigeot  */
4307e11a51e3SFrançois Tigeot int
i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object * obj,bool write)4308e11a51e3SFrançois Tigeot i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
4309e11a51e3SFrançois Tigeot {
4310e11a51e3SFrançois Tigeot 	int ret;
4311e11a51e3SFrançois Tigeot 
43124be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
43134be47400SFrançois Tigeot 
43144be47400SFrançois Tigeot 	ret = i915_gem_object_wait(obj,
43154be47400SFrançois Tigeot 				   I915_WAIT_INTERRUPTIBLE |
43164be47400SFrançois Tigeot 				   I915_WAIT_LOCKED |
43174be47400SFrançois Tigeot 				   (write ? I915_WAIT_ALL : 0),
43184be47400SFrançois Tigeot 				   MAX_SCHEDULE_TIMEOUT,
43194be47400SFrançois Tigeot 				   NULL);
4320e11a51e3SFrançois Tigeot 	if (ret)
4321e11a51e3SFrançois Tigeot 		return ret;
4322e11a51e3SFrançois Tigeot 
43233f2dd94aSFrançois Tigeot 	flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
4324e11a51e3SFrançois Tigeot 
4325e11a51e3SFrançois Tigeot 	/* Flush the CPU cache if it's still invalid. */
4326e11a51e3SFrançois Tigeot 	if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
4327a85cb24fSFrançois Tigeot 		i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
4328e11a51e3SFrançois Tigeot 		obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
4329e11a51e3SFrançois Tigeot 	}
4330e11a51e3SFrançois Tigeot 
4331e11a51e3SFrançois Tigeot 	/* It should now be out of any other write domains, and we can update
4332e11a51e3SFrançois Tigeot 	 * the domain values for our changes.
4333e11a51e3SFrançois Tigeot 	 */
43343f2dd94aSFrançois Tigeot 	GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
4335e11a51e3SFrançois Tigeot 
4336e11a51e3SFrançois Tigeot 	/* If we're writing through the CPU, then the GPU read domains will
4337e11a51e3SFrançois Tigeot 	 * need to be invalidated at next use.
4338e11a51e3SFrançois Tigeot 	 */
43393f2dd94aSFrançois Tigeot 	if (write)
43403f2dd94aSFrançois Tigeot 		__start_cpu_write(obj);
4341e11a51e3SFrançois Tigeot 
4342e11a51e3SFrançois Tigeot 	return 0;
4343e11a51e3SFrançois Tigeot }
4344e11a51e3SFrançois Tigeot 
4345e11a51e3SFrançois Tigeot /* Throttle our rendering by waiting until the ring has completed our requests
4346e11a51e3SFrançois Tigeot  * emitted over 20 msec ago.
4347e11a51e3SFrançois Tigeot  *
4348e11a51e3SFrançois Tigeot  * Note that if we were to use the current jiffies each time around the loop,
4349e11a51e3SFrançois Tigeot  * we wouldn't escape the function with any frames outstanding if the time to
4350e11a51e3SFrançois Tigeot  * render a frame was over 20ms.
4351e11a51e3SFrançois Tigeot  *
4352e11a51e3SFrançois Tigeot  * This should get us reasonable parallelism between CPU and GPU but also
4353e11a51e3SFrançois Tigeot  * relatively low latency when blocking on a particular request to finish.
4354e11a51e3SFrançois Tigeot  */
4355e11a51e3SFrançois Tigeot static int
i915_gem_ring_throttle(struct drm_device * dev,struct drm_file * file)4356e11a51e3SFrançois Tigeot i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
4357e11a51e3SFrançois Tigeot {
4358303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
4359e11a51e3SFrançois Tigeot 	struct drm_i915_file_private *file_priv = file->driver_priv;
436019c468b4SFrançois Tigeot 	unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
43612c9916cdSFrançois Tigeot 	struct drm_i915_gem_request *request, *target = NULL;
43624be47400SFrançois Tigeot 	long ret;
4363a2fdbec6SFrançois Tigeot 
43648621f407SFrançois Tigeot 	/* ABI: return -EIO if already wedged */
43658621f407SFrançois Tigeot 	if (i915_terminally_wedged(&dev_priv->gpu_error))
43668621f407SFrançois Tigeot 		return -EIO;
4367e11a51e3SFrançois Tigeot 
4368ec5b6af4SFrançois Tigeot 	lockmgr(&file_priv->mm.lock, LK_EXCLUSIVE);
4369a85cb24fSFrançois Tigeot 	list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
4370e11a51e3SFrançois Tigeot 		if (time_after_eq(request->emitted_jiffies, recent_enough))
4371e11a51e3SFrançois Tigeot 			break;
4372e11a51e3SFrançois Tigeot 
4373a85cb24fSFrançois Tigeot 		if (target) {
4374a85cb24fSFrançois Tigeot 			list_del(&target->client_link);
4375a85cb24fSFrançois Tigeot 			target->file_priv = NULL;
4376a85cb24fSFrançois Tigeot 		}
4377a05eeebfSFrançois Tigeot 
43782c9916cdSFrançois Tigeot 		target = request;
4379e11a51e3SFrançois Tigeot 	}
43802c9916cdSFrançois Tigeot 	if (target)
438187df8fc6SFrançois Tigeot 		i915_gem_request_get(target);
4382ec5b6af4SFrançois Tigeot 	lockmgr(&file_priv->mm.lock, LK_RELEASE);
4383e11a51e3SFrançois Tigeot 
43842c9916cdSFrançois Tigeot 	if (target == NULL)
4385e11a51e3SFrançois Tigeot 		return 0;
4386e11a51e3SFrançois Tigeot 
43874be47400SFrançois Tigeot 	ret = i915_wait_request(target,
43884be47400SFrançois Tigeot 				I915_WAIT_INTERRUPTIBLE,
43894be47400SFrançois Tigeot 				MAX_SCHEDULE_TIMEOUT);
439087df8fc6SFrançois Tigeot 	i915_gem_request_put(target);
43912c9916cdSFrançois Tigeot 
43924be47400SFrançois Tigeot 	return ret < 0 ? ret : 0;
439371f41f3eSFrançois Tigeot }
439471f41f3eSFrançois Tigeot 
43951e12ee3bSFrançois Tigeot struct i915_vma *
i915_gem_object_ggtt_pin(struct drm_i915_gem_object * obj,const struct i915_ggtt_view * view,u64 size,u64 alignment,u64 flags)439671f41f3eSFrançois Tigeot i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
439771f41f3eSFrançois Tigeot 			 const struct i915_ggtt_view *view,
439871f41f3eSFrançois Tigeot 			 u64 size,
439971f41f3eSFrançois Tigeot 			 u64 alignment,
440071f41f3eSFrançois Tigeot 			 u64 flags)
440171f41f3eSFrançois Tigeot {
44021e12ee3bSFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
44031e12ee3bSFrançois Tigeot 	struct i915_address_space *vm = &dev_priv->ggtt.base;
440471f41f3eSFrançois Tigeot 	struct i915_vma *vma;
440571f41f3eSFrançois Tigeot 	int ret;
440671f41f3eSFrançois Tigeot 
44074be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
44084be47400SFrançois Tigeot 
44093f2dd94aSFrançois Tigeot 	if (!view && flags & PIN_MAPPABLE) {
44101e12ee3bSFrançois Tigeot 		/* If the required space is larger than the available
44111e12ee3bSFrançois Tigeot 		 * aperture, we will not able to find a slot for the
44121e12ee3bSFrançois Tigeot 		 * object and unbinding the object now will be in
44131e12ee3bSFrançois Tigeot 		 * vain. Worse, doing so may cause us to ping-pong
44141e12ee3bSFrançois Tigeot 		 * the object in and out of the Global GTT and
44151e12ee3bSFrançois Tigeot 		 * waste a lot of cycles under the mutex.
44161e12ee3bSFrançois Tigeot 		 */
44173f2dd94aSFrançois Tigeot 		if (obj->base.size > dev_priv->ggtt.mappable_end)
44181e12ee3bSFrançois Tigeot 			return ERR_PTR(-E2BIG);
44191e12ee3bSFrançois Tigeot 
44201e12ee3bSFrançois Tigeot 		/* If NONBLOCK is set the caller is optimistically
44211e12ee3bSFrançois Tigeot 		 * trying to cache the full object within the mappable
44221e12ee3bSFrançois Tigeot 		 * aperture, and *must* have a fallback in place for
44231e12ee3bSFrançois Tigeot 		 * situations where we cannot bind the object. We
44241e12ee3bSFrançois Tigeot 		 * can be a little more lax here and use the fallback
44251e12ee3bSFrançois Tigeot 		 * more often to avoid costly migrations of ourselves
44261e12ee3bSFrançois Tigeot 		 * and other objects within the aperture.
44271e12ee3bSFrançois Tigeot 		 *
44281e12ee3bSFrançois Tigeot 		 * Half-the-aperture is used as a simple heuristic.
44291e12ee3bSFrançois Tigeot 		 * More interesting would to do search for a free
44301e12ee3bSFrançois Tigeot 		 * block prior to making the commitment to unbind.
44311e12ee3bSFrançois Tigeot 		 * That caters for the self-harm case, and with a
44321e12ee3bSFrançois Tigeot 		 * little more heuristics (e.g. NOFAULT, NOEVICT)
44331e12ee3bSFrançois Tigeot 		 * we could try to minimise harm to others.
44341e12ee3bSFrançois Tigeot 		 */
44351e12ee3bSFrançois Tigeot 		if (flags & PIN_NONBLOCK &&
44363f2dd94aSFrançois Tigeot 		    obj->base.size > dev_priv->ggtt.mappable_end / 2)
44373f2dd94aSFrançois Tigeot 			return ERR_PTR(-ENOSPC);
44383f2dd94aSFrançois Tigeot 	}
44393f2dd94aSFrançois Tigeot 
44403f2dd94aSFrançois Tigeot 	vma = i915_vma_instance(obj, vm, view);
44413f2dd94aSFrançois Tigeot 	if (unlikely(IS_ERR(vma)))
44423f2dd94aSFrançois Tigeot 		return vma;
44433f2dd94aSFrançois Tigeot 
44443f2dd94aSFrançois Tigeot 	if (i915_vma_misplaced(vma, size, alignment, flags)) {
44453f2dd94aSFrançois Tigeot 		if (flags & PIN_NONBLOCK) {
44463f2dd94aSFrançois Tigeot 			if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
44473f2dd94aSFrançois Tigeot 				return ERR_PTR(-ENOSPC);
44483f2dd94aSFrançois Tigeot 
44493f2dd94aSFrançois Tigeot 			if (flags & PIN_MAPPABLE &&
4450a85cb24fSFrançois Tigeot 			    vma->fence_size > dev_priv->ggtt.mappable_end / 2)
44511e12ee3bSFrançois Tigeot 				return ERR_PTR(-ENOSPC);
44521e12ee3bSFrançois Tigeot 		}
445371f41f3eSFrançois Tigeot 
445471f41f3eSFrançois Tigeot 		WARN(i915_vma_is_pinned(vma),
445571f41f3eSFrançois Tigeot 		     "bo is already pinned in ggtt with incorrect alignment:"
44561e12ee3bSFrançois Tigeot 		     " offset=%08x, req.alignment=%llx,"
44571e12ee3bSFrançois Tigeot 		     " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
44581e12ee3bSFrançois Tigeot 		     i915_ggtt_offset(vma), alignment,
4459ba55f2f5SFrançois Tigeot 		     !!(flags & PIN_MAPPABLE),
44601e12ee3bSFrançois Tigeot 		     i915_vma_is_map_and_fenceable(vma));
44619edbd4a0SFrançois Tigeot 		ret = i915_vma_unbind(vma);
4462f192107fSFrançois Tigeot 		if (ret)
44631e12ee3bSFrançois Tigeot 			return ERR_PTR(ret);
4464e11a51e3SFrançois Tigeot 	}
4465e11a51e3SFrançois Tigeot 
44661e12ee3bSFrançois Tigeot 	ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
44671e12ee3bSFrançois Tigeot 	if (ret)
44681e12ee3bSFrançois Tigeot 		return ERR_PTR(ret);
4469477eb7f9SFrançois Tigeot 
44701e12ee3bSFrançois Tigeot 	return vma;
447171f41f3eSFrançois Tigeot }
4472e11a51e3SFrançois Tigeot 
__busy_read_flag(unsigned int id)447371f41f3eSFrançois Tigeot static __always_inline unsigned int __busy_read_flag(unsigned int id)
447471f41f3eSFrançois Tigeot {
447571f41f3eSFrançois Tigeot 	/* Note that we could alias engines in the execbuf API, but
447671f41f3eSFrançois Tigeot 	 * that would be very unwise as it prevents userspace from
447771f41f3eSFrançois Tigeot 	 * fine control over engine selection. Ahem.
447871f41f3eSFrançois Tigeot 	 *
447971f41f3eSFrançois Tigeot 	 * This should be something like EXEC_MAX_ENGINE instead of
448071f41f3eSFrançois Tigeot 	 * I915_NUM_ENGINES.
448171f41f3eSFrançois Tigeot 	 */
448271f41f3eSFrançois Tigeot 	BUILD_BUG_ON(I915_NUM_ENGINES > 16);
448371f41f3eSFrançois Tigeot 	return 0x10000 << id;
448471f41f3eSFrançois Tigeot }
4485ba55f2f5SFrançois Tigeot 
__busy_write_id(unsigned int id)448671f41f3eSFrançois Tigeot static __always_inline unsigned int __busy_write_id(unsigned int id)
448771f41f3eSFrançois Tigeot {
44881e12ee3bSFrançois Tigeot 	/* The uABI guarantees an active writer is also amongst the read
44891e12ee3bSFrançois Tigeot 	 * engines. This would be true if we accessed the activity tracking
44901e12ee3bSFrançois Tigeot 	 * under the lock, but as we perform the lookup of the object and
44911e12ee3bSFrançois Tigeot 	 * its activity locklessly we can not guarantee that the last_write
44921e12ee3bSFrançois Tigeot 	 * being active implies that we have set the same engine flag from
44931e12ee3bSFrançois Tigeot 	 * last_read - hence we always set both read and write busy for
44941e12ee3bSFrançois Tigeot 	 * last_write.
44951e12ee3bSFrançois Tigeot 	 */
44961e12ee3bSFrançois Tigeot 	return id | __busy_read_flag(id);
449771f41f3eSFrançois Tigeot }
449871f41f3eSFrançois Tigeot 
4499a85cb24fSFrançois Tigeot #pragma GCC diagnostic push
4500a85cb24fSFrançois Tigeot #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
4501a85cb24fSFrançois Tigeot 
450271f41f3eSFrançois Tigeot static __always_inline unsigned int
__busy_set_if_active(const struct dma_fence * fence,unsigned int (* flag)(unsigned int id))45034be47400SFrançois Tigeot __busy_set_if_active(const struct dma_fence *fence,
450471f41f3eSFrançois Tigeot 		     unsigned int (*flag)(unsigned int id))
450571f41f3eSFrançois Tigeot {
45064be47400SFrançois Tigeot 	struct drm_i915_gem_request *rq;
450771f41f3eSFrançois Tigeot 
45084be47400SFrançois Tigeot 	/* We have to check the current hw status of the fence as the uABI
45094be47400SFrançois Tigeot 	 * guarantees forward progress. We could rely on the idle worker
45104be47400SFrançois Tigeot 	 * to eventually flush us, but to minimise latency just ask the
45114be47400SFrançois Tigeot 	 * hardware.
45124be47400SFrançois Tigeot 	 *
45134be47400SFrançois Tigeot 	 * Note we only report on the status of native fences.
45144be47400SFrançois Tigeot 	 */
45154be47400SFrançois Tigeot 	if (!dma_fence_is_i915(fence))
451671f41f3eSFrançois Tigeot 		return 0;
451771f41f3eSFrançois Tigeot 
45184be47400SFrançois Tigeot 	/* opencode to_request() in order to avoid const warnings */
45194be47400SFrançois Tigeot 	rq = container_of(fence, struct drm_i915_gem_request, fence);
45204be47400SFrançois Tigeot 	if (i915_gem_request_completed(rq))
45214be47400SFrançois Tigeot 		return 0;
45224be47400SFrançois Tigeot 
45233f2dd94aSFrançois Tigeot 	return flag(rq->engine->uabi_id);
452471f41f3eSFrançois Tigeot }
4525a85cb24fSFrançois Tigeot #pragma GCC diagnostic pop
452671f41f3eSFrançois Tigeot 
452771f41f3eSFrançois Tigeot static __always_inline unsigned int
busy_check_reader(const struct dma_fence * fence)45284be47400SFrançois Tigeot busy_check_reader(const struct dma_fence *fence)
452971f41f3eSFrançois Tigeot {
45304be47400SFrançois Tigeot 	return __busy_set_if_active(fence, __busy_read_flag);
453171f41f3eSFrançois Tigeot }
453271f41f3eSFrançois Tigeot 
453371f41f3eSFrançois Tigeot static __always_inline unsigned int
busy_check_writer(const struct dma_fence * fence)45344be47400SFrançois Tigeot busy_check_writer(const struct dma_fence *fence)
453571f41f3eSFrançois Tigeot {
45364be47400SFrançois Tigeot 	if (!fence)
45374be47400SFrançois Tigeot 		return 0;
45384be47400SFrançois Tigeot 
45394be47400SFrançois Tigeot 	return __busy_set_if_active(fence, __busy_write_id);
4540e11a51e3SFrançois Tigeot }
4541e11a51e3SFrançois Tigeot 
4542e11a51e3SFrançois Tigeot int
i915_gem_busy_ioctl(struct drm_device * dev,void * data,struct drm_file * file)4543e11a51e3SFrançois Tigeot i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4544e11a51e3SFrançois Tigeot 		    struct drm_file *file)
4545e11a51e3SFrançois Tigeot {
4546686a02f1SFrançois Tigeot 	struct drm_i915_gem_busy *args = data;
4547e11a51e3SFrançois Tigeot 	struct drm_i915_gem_object *obj;
45484be47400SFrançois Tigeot 	struct reservation_object_list *list;
45494be47400SFrançois Tigeot 	unsigned int seq;
45504be47400SFrançois Tigeot 	int err;
4551e11a51e3SFrançois Tigeot 
45524be47400SFrançois Tigeot 	err = -ENOENT;
455371f41f3eSFrançois Tigeot 	rcu_read_lock();
45544be47400SFrançois Tigeot 	obj = i915_gem_object_lookup_rcu(file, args->handle);
45554be47400SFrançois Tigeot 	if (!obj)
45564be47400SFrançois Tigeot 		goto out;
4557c0e85e96SFrançois Tigeot 
45584be47400SFrançois Tigeot 	/* A discrepancy here is that we do not report the status of
45594be47400SFrançois Tigeot 	 * non-i915 fences, i.e. even though we may report the object as idle,
45604be47400SFrançois Tigeot 	 * a call to set-domain may still stall waiting for foreign rendering.
45614be47400SFrançois Tigeot 	 * This also means that wait-ioctl may report an object as busy,
45624be47400SFrançois Tigeot 	 * where busy-ioctl considers it idle.
456371f41f3eSFrançois Tigeot 	 *
45644be47400SFrançois Tigeot 	 * We trade the ability to warn of foreign fences to report on which
45654be47400SFrançois Tigeot 	 * i915 engines are active for the object.
45664be47400SFrançois Tigeot 	 *
45674be47400SFrançois Tigeot 	 * Alternatively, we can trade that extra information on read/write
45684be47400SFrançois Tigeot 	 * activity with
45694be47400SFrançois Tigeot 	 *	args->busy =
45704be47400SFrançois Tigeot 	 *		!reservation_object_test_signaled_rcu(obj->resv, true);
45714be47400SFrançois Tigeot 	 * to report the overall busyness. This is what the wait-ioctl does.
45724be47400SFrançois Tigeot 	 *
457371f41f3eSFrançois Tigeot 	 */
45744be47400SFrançois Tigeot retry:
45754be47400SFrançois Tigeot 	seq = raw_read_seqcount(&obj->resv->seq);
457671f41f3eSFrançois Tigeot 
45774be47400SFrançois Tigeot 	/* Translate the exclusive fence to the READ *and* WRITE engine */
45784be47400SFrançois Tigeot 	args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
45794be47400SFrançois Tigeot 
45804be47400SFrançois Tigeot 	/* Translate shared fences to READ set of engines */
45814be47400SFrançois Tigeot 	list = rcu_dereference(obj->resv->fence);
45824be47400SFrançois Tigeot 	if (list) {
45834be47400SFrançois Tigeot 		unsigned int shared_count = list->shared_count, i;
45844be47400SFrançois Tigeot 
45854be47400SFrançois Tigeot 		for (i = 0; i < shared_count; ++i) {
45864be47400SFrançois Tigeot 			struct dma_fence *fence =
45874be47400SFrançois Tigeot 				rcu_dereference(list->shared[i]);
45884be47400SFrançois Tigeot 
45894be47400SFrançois Tigeot 			args->busy |= busy_check_reader(fence);
45904be47400SFrançois Tigeot 		}
4591c0e85e96SFrançois Tigeot 	}
4592e11a51e3SFrançois Tigeot 
45934be47400SFrançois Tigeot 	if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
45944be47400SFrançois Tigeot 		goto retry;
45954be47400SFrançois Tigeot 
45964be47400SFrançois Tigeot 	err = 0;
45974be47400SFrançois Tigeot out:
45984be47400SFrançois Tigeot 	rcu_read_unlock();
45994be47400SFrançois Tigeot 	return err;
4600e11a51e3SFrançois Tigeot }
4601e11a51e3SFrançois Tigeot 
4602e11a51e3SFrançois Tigeot int
i915_gem_throttle_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)4603e11a51e3SFrançois Tigeot i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4604e11a51e3SFrançois Tigeot 			struct drm_file *file_priv)
4605e11a51e3SFrançois Tigeot {
4606d2557f23SFrançois Tigeot 	return i915_gem_ring_throttle(dev, file_priv);
4607e11a51e3SFrançois Tigeot }
4608e11a51e3SFrançois Tigeot 
4609e11a51e3SFrançois Tigeot int
i915_gem_madvise_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)4610e11a51e3SFrançois Tigeot i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4611e11a51e3SFrançois Tigeot 		       struct drm_file *file_priv)
4612e11a51e3SFrançois Tigeot {
4613303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
4614e11a51e3SFrançois Tigeot 	struct drm_i915_gem_madvise *args = data;
4615e11a51e3SFrançois Tigeot 	struct drm_i915_gem_object *obj;
46164be47400SFrançois Tigeot 	int err;
4617e11a51e3SFrançois Tigeot 
4618e11a51e3SFrançois Tigeot 	switch (args->madv) {
4619e11a51e3SFrançois Tigeot 	case I915_MADV_DONTNEED:
4620e11a51e3SFrançois Tigeot 	case I915_MADV_WILLNEED:
4621e11a51e3SFrançois Tigeot 	    break;
4622e11a51e3SFrançois Tigeot 	default:
4623e11a51e3SFrançois Tigeot 	    return -EINVAL;
4624e11a51e3SFrançois Tigeot 	}
4625e11a51e3SFrançois Tigeot 
462687df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file_priv, args->handle);
46274be47400SFrançois Tigeot 	if (!obj)
46284be47400SFrançois Tigeot 		return -ENOENT;
4629e11a51e3SFrançois Tigeot 
46304be47400SFrançois Tigeot 	err = mutex_lock_interruptible(&obj->mm.lock);
46314be47400SFrançois Tigeot 	if (err)
46324be47400SFrançois Tigeot 		goto out;
46334be47400SFrançois Tigeot 
46343f2dd94aSFrançois Tigeot 	if (i915_gem_object_has_pages(obj) &&
463571f41f3eSFrançois Tigeot 	    i915_gem_object_is_tiled(obj) &&
46362c9916cdSFrançois Tigeot 	    dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
46374be47400SFrançois Tigeot 		if (obj->mm.madv == I915_MADV_WILLNEED) {
46384be47400SFrançois Tigeot 			GEM_BUG_ON(!obj->mm.quirked);
46394be47400SFrançois Tigeot 			__i915_gem_object_unpin_pages(obj);
46404be47400SFrançois Tigeot 			obj->mm.quirked = false;
46414be47400SFrançois Tigeot 		}
46424be47400SFrançois Tigeot 		if (args->madv == I915_MADV_WILLNEED) {
46434be47400SFrançois Tigeot 			GEM_BUG_ON(obj->mm.quirked);
46444be47400SFrançois Tigeot 			__i915_gem_object_pin_pages(obj);
46454be47400SFrançois Tigeot 			obj->mm.quirked = true;
46464be47400SFrançois Tigeot 		}
46472c9916cdSFrançois Tigeot 	}
46482c9916cdSFrançois Tigeot 
46494be47400SFrançois Tigeot 	if (obj->mm.madv != __I915_MADV_PURGED)
46504be47400SFrançois Tigeot 		obj->mm.madv = args->madv;
4651e11a51e3SFrançois Tigeot 
4652e11a51e3SFrançois Tigeot 	/* if the object is no longer attached, discard its backing storage */
46533f2dd94aSFrançois Tigeot 	if (obj->mm.madv == I915_MADV_DONTNEED &&
46543f2dd94aSFrançois Tigeot 	    !i915_gem_object_has_pages(obj))
4655e11a51e3SFrançois Tigeot 		i915_gem_object_truncate(obj);
4656e11a51e3SFrançois Tigeot 
46574be47400SFrançois Tigeot 	args->retained = obj->mm.madv != __I915_MADV_PURGED;
46584be47400SFrançois Tigeot 	mutex_unlock(&obj->mm.lock);
4659e11a51e3SFrançois Tigeot 
46604be47400SFrançois Tigeot out:
466187df8fc6SFrançois Tigeot 	i915_gem_object_put(obj);
46624be47400SFrançois Tigeot 	return err;
46634be47400SFrançois Tigeot }
46644be47400SFrançois Tigeot 
46654be47400SFrançois Tigeot static void
frontbuffer_retire(struct i915_gem_active * active,struct drm_i915_gem_request * request)46664be47400SFrançois Tigeot frontbuffer_retire(struct i915_gem_active *active,
46674be47400SFrançois Tigeot 		   struct drm_i915_gem_request *request)
46684be47400SFrançois Tigeot {
46694be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj =
46704be47400SFrançois Tigeot 		container_of(active, typeof(*obj), frontbuffer_write);
46714be47400SFrançois Tigeot 
4672a85cb24fSFrançois Tigeot 	intel_fb_obj_flush(obj, ORIGIN_CS);
4673e11a51e3SFrançois Tigeot }
4674e11a51e3SFrançois Tigeot 
i915_gem_object_init(struct drm_i915_gem_object * obj,const struct drm_i915_gem_object_ops * ops)4675dfa24183SFrançois Tigeot void i915_gem_object_init(struct drm_i915_gem_object *obj,
4676dfa24183SFrançois Tigeot 			  const struct drm_i915_gem_object_ops *ops)
4677dfa24183SFrançois Tigeot {
46784be47400SFrançois Tigeot 	lockinit(&obj->mm.lock, "i9goml", 0, LK_CANRECURSE);
467919c468b4SFrançois Tigeot 
46809edbd4a0SFrançois Tigeot 	INIT_LIST_HEAD(&obj->vma_list);
46813f2dd94aSFrançois Tigeot 	INIT_LIST_HEAD(&obj->lut_list);
468219c468b4SFrançois Tigeot 	INIT_LIST_HEAD(&obj->batch_pool_link);
4683dfa24183SFrançois Tigeot 
4684dfa24183SFrançois Tigeot 	obj->ops = ops;
4685dfa24183SFrançois Tigeot 
46864be47400SFrançois Tigeot 	reservation_object_init(&obj->__builtin_resv);
46874be47400SFrançois Tigeot 	obj->resv = &obj->__builtin_resv;
46884be47400SFrançois Tigeot 
46891e12ee3bSFrançois Tigeot 	obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
46904be47400SFrançois Tigeot 	init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
46914be47400SFrançois Tigeot 
46924be47400SFrançois Tigeot 	obj->mm.madv = I915_MADV_WILLNEED;
46934be47400SFrançois Tigeot 	INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
46944be47400SFrançois Tigeot 	lockinit(&obj->mm.get_page.lock, "i915ogpl", 0, LK_CANRECURSE);
4695dfa24183SFrançois Tigeot 
4696303bf270SFrançois Tigeot 	i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
4697dfa24183SFrançois Tigeot }
4698dfa24183SFrançois Tigeot 
4699dfa24183SFrançois Tigeot static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
47004be47400SFrançois Tigeot 	.flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
47014be47400SFrançois Tigeot 		 I915_GEM_OBJECT_IS_SHRINKABLE,
4702a85cb24fSFrançois Tigeot 
4703dfa24183SFrançois Tigeot 	.get_pages = i915_gem_object_get_pages_gtt,
4704dfa24183SFrançois Tigeot 	.put_pages = i915_gem_object_put_pages_gtt,
4705a85cb24fSFrançois Tigeot 
4706a85cb24fSFrançois Tigeot 	.pwrite = i915_gem_object_pwrite_gtt,
4707dfa24183SFrançois Tigeot };
4708dfa24183SFrançois Tigeot 
i915_gem_object_create_shmem(struct drm_device * dev,struct drm_gem_object * obj,size_t size)47093f2dd94aSFrançois Tigeot static int i915_gem_object_create_shmem(struct drm_device *dev,
47103f2dd94aSFrançois Tigeot 					struct drm_gem_object *obj,
47113f2dd94aSFrançois Tigeot 					size_t size)
47123f2dd94aSFrançois Tigeot {
47133f2dd94aSFrançois Tigeot #ifndef __DragonFly__
47143f2dd94aSFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(dev);
47153f2dd94aSFrançois Tigeot 	unsigned long flags = VM_NORESERVE;
47163f2dd94aSFrançois Tigeot 	struct file *filp;
47173f2dd94aSFrançois Tigeot #endif
47183f2dd94aSFrançois Tigeot 
47193f2dd94aSFrançois Tigeot #ifndef __DragonFly__
47203f2dd94aSFrançois Tigeot 	drm_gem_private_object_init(dev, obj, size);
47213f2dd94aSFrançois Tigeot #else
47223f2dd94aSFrançois Tigeot 	drm_gem_object_init(dev, obj, size);
47233f2dd94aSFrançois Tigeot #endif
47243f2dd94aSFrançois Tigeot 
47253f2dd94aSFrançois Tigeot #ifndef __DragonFly__
47263f2dd94aSFrançois Tigeot 	if (i915->mm.gemfs)
47273f2dd94aSFrançois Tigeot 		filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
47283f2dd94aSFrançois Tigeot 						 flags);
47293f2dd94aSFrançois Tigeot 	else
47303f2dd94aSFrançois Tigeot 		filp = shmem_file_setup("i915", size, flags);
47313f2dd94aSFrançois Tigeot 
47323f2dd94aSFrançois Tigeot 	if (IS_ERR(filp))
47333f2dd94aSFrançois Tigeot 		return PTR_ERR(filp);
47343f2dd94aSFrançois Tigeot 
47353f2dd94aSFrançois Tigeot 	obj->filp = filp;
47363f2dd94aSFrançois Tigeot #endif
47373f2dd94aSFrançois Tigeot 
47383f2dd94aSFrançois Tigeot 	return 0;
47393f2dd94aSFrançois Tigeot }
47403f2dd94aSFrançois Tigeot 
47414be47400SFrançois Tigeot struct drm_i915_gem_object *
i915_gem_object_create(struct drm_i915_private * dev_priv,u64 size)4742a85cb24fSFrançois Tigeot i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
4743e11a51e3SFrançois Tigeot {
4744e11a51e3SFrançois Tigeot 	struct drm_i915_gem_object *obj;
4745dfa24183SFrançois Tigeot #if 0
4746dfa24183SFrançois Tigeot 	struct address_space *mapping;
4747dfa24183SFrançois Tigeot #endif
47483f2dd94aSFrançois Tigeot 	unsigned int cache_level;
47494be47400SFrançois Tigeot 	gfp_t mask;
47501487f786SFrançois Tigeot 	int ret;
4751e11a51e3SFrançois Tigeot 
47524be47400SFrançois Tigeot 	/* There is a prevalence of the assumption that we fit the object's
47534be47400SFrançois Tigeot 	 * page count inside a 32bit _signed_ variable. Let's document this and
47544be47400SFrançois Tigeot 	 * catch if we ever need to fix it. In the meantime, if you do spot
47554be47400SFrançois Tigeot 	 * such a local variable, please consider fixing!
47564be47400SFrançois Tigeot 	 */
47573f2dd94aSFrançois Tigeot 	if (size >> PAGE_SHIFT > INT_MAX)
47584be47400SFrançois Tigeot 		return ERR_PTR(-E2BIG);
47594be47400SFrançois Tigeot 
47604be47400SFrançois Tigeot 	if (overflows_type(size, obj->base.size))
47614be47400SFrançois Tigeot 		return ERR_PTR(-E2BIG);
47624be47400SFrançois Tigeot 
4763a85cb24fSFrançois Tigeot 	obj = i915_gem_object_alloc(dev_priv);
4764dfa24183SFrançois Tigeot 	if (obj == NULL)
47651487f786SFrançois Tigeot 		return ERR_PTR(-ENOMEM);
4766e11a51e3SFrançois Tigeot 
47673f2dd94aSFrançois Tigeot 	ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
47681487f786SFrançois Tigeot 	if (ret)
47691487f786SFrançois Tigeot 		goto fail;
4770e11a51e3SFrançois Tigeot 
4771dfa24183SFrançois Tigeot 	mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4772a85cb24fSFrançois Tigeot 	if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
4773dfa24183SFrançois Tigeot 		/* 965gm cannot relocate objects above 4GiB. */
4774dfa24183SFrançois Tigeot 		mask &= ~__GFP_HIGHMEM;
4775dfa24183SFrançois Tigeot 		mask |= __GFP_DMA32;
4776dfa24183SFrançois Tigeot 	}
4777dfa24183SFrançois Tigeot 
47784be47400SFrançois Tigeot #if 0
47791e12ee3bSFrançois Tigeot 	mapping = obj->base.filp->f_mapping;
4780dfa24183SFrançois Tigeot 	mapping_set_gfp_mask(mapping, mask);
4781dfa24183SFrançois Tigeot #endif
4782dfa24183SFrançois Tigeot 
4783dfa24183SFrançois Tigeot 	i915_gem_object_init(obj, &i915_gem_object_ops);
4784dfa24183SFrançois Tigeot 
4785e11a51e3SFrançois Tigeot 	obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4786e11a51e3SFrançois Tigeot 	obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4787e11a51e3SFrançois Tigeot 
47883f2dd94aSFrançois Tigeot 	if (HAS_LLC(dev_priv))
4789d2557f23SFrançois Tigeot 		/* On some devices, we can have the GPU use the LLC (the CPU
4790d2557f23SFrançois Tigeot 		 * cache) for about a 10% performance improvement
4791d2557f23SFrançois Tigeot 		 * compared to uncached.  Graphics requests other than
4792d2557f23SFrançois Tigeot 		 * display scanout are coherent with the CPU in
4793d2557f23SFrançois Tigeot 		 * accessing this cache.  This means in this mode we
4794d2557f23SFrançois Tigeot 		 * don't need to clflush on the CPU side, and on the
4795d2557f23SFrançois Tigeot 		 * GPU side we only need to flush internal caches to
4796d2557f23SFrançois Tigeot 		 * get data visible to the CPU.
4797d2557f23SFrançois Tigeot 		 *
4798d2557f23SFrançois Tigeot 		 * However, we maintain the display planes as UC, and so
4799d2557f23SFrançois Tigeot 		 * need to rebind when first used as such.
4800d2557f23SFrançois Tigeot 		 */
48013f2dd94aSFrançois Tigeot 		cache_level = I915_CACHE_LLC;
48023f2dd94aSFrançois Tigeot 	else
48033f2dd94aSFrançois Tigeot 		cache_level = I915_CACHE_NONE;
48043f2dd94aSFrançois Tigeot 
48053f2dd94aSFrançois Tigeot 	i915_gem_object_set_cache_coherency(obj, cache_level);
4806e11a51e3SFrançois Tigeot 
48079edbd4a0SFrançois Tigeot 	trace_i915_gem_object_create(obj);
48089edbd4a0SFrançois Tigeot 
4809d2557f23SFrançois Tigeot 	return obj;
48101487f786SFrançois Tigeot 
48111487f786SFrançois Tigeot fail:
48121487f786SFrançois Tigeot 	i915_gem_object_free(obj);
48131487f786SFrançois Tigeot 	return ERR_PTR(ret);
4814e11a51e3SFrançois Tigeot }
4815e11a51e3SFrançois Tigeot 
discard_backing_storage(struct drm_i915_gem_object * obj)4816ba55f2f5SFrançois Tigeot static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4817ba55f2f5SFrançois Tigeot {
4818ba55f2f5SFrançois Tigeot 	/* If we are the last user of the backing storage (be it shmemfs
4819ba55f2f5SFrançois Tigeot 	 * pages or stolen etc), we know that the pages are going to be
4820ba55f2f5SFrançois Tigeot 	 * immediately released. In this case, we can then skip copying
4821ba55f2f5SFrançois Tigeot 	 * back the contents from the GPU.
4822ba55f2f5SFrançois Tigeot 	 */
4823ba55f2f5SFrançois Tigeot 
48244be47400SFrançois Tigeot 	if (obj->mm.madv != I915_MADV_WILLNEED)
4825ba55f2f5SFrançois Tigeot 		return false;
4826ba55f2f5SFrançois Tigeot 
48277ca4ece0SFrançois Tigeot 	if (obj->base.filp == NULL)
4828ba55f2f5SFrançois Tigeot 		return true;
4829ba55f2f5SFrançois Tigeot 
4830ba55f2f5SFrançois Tigeot 	/* At first glance, this looks racy, but then again so would be
4831ba55f2f5SFrançois Tigeot 	 * userspace racing mmap against close. However, the first external
4832ba55f2f5SFrançois Tigeot 	 * reference to the filp can only be obtained through the
4833ba55f2f5SFrançois Tigeot 	 * i915_gem_mmap_ioctl() which safeguards us against the user
4834ba55f2f5SFrançois Tigeot 	 * acquiring such a reference whilst we are in the middle of
4835ba55f2f5SFrançois Tigeot 	 * freeing the object.
4836ba55f2f5SFrançois Tigeot 	 */
4837ba55f2f5SFrançois Tigeot #if 0
4838ba55f2f5SFrançois Tigeot 	return atomic_long_read(&obj->base.filp->f_count) == 1;
4839ba55f2f5SFrançois Tigeot #else
4840ba55f2f5SFrançois Tigeot 	return false;
4841ba55f2f5SFrançois Tigeot #endif
4842ba55f2f5SFrançois Tigeot }
4843ba55f2f5SFrançois Tigeot 
__i915_gem_free_objects(struct drm_i915_private * i915,struct llist_node * freed)48444be47400SFrançois Tigeot static void __i915_gem_free_objects(struct drm_i915_private *i915,
48454be47400SFrançois Tigeot 				    struct llist_node *freed)
4846e3adcf8fSFrançois Tigeot {
48474be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj, *on;
48489edbd4a0SFrançois Tigeot 
48494be47400SFrançois Tigeot 	intel_runtime_pm_get(i915);
48504be47400SFrançois Tigeot 	llist_for_each_entry_safe(obj, on, freed, freed) {
48514be47400SFrançois Tigeot 		struct i915_vma *vma, *vn;
4852e3adcf8fSFrançois Tigeot 
4853b15f8132SFrançois Tigeot 		trace_i915_gem_object_destroy(obj);
4854b15f8132SFrançois Tigeot 
48553f2dd94aSFrançois Tigeot 		mutex_lock(&i915->drm.struct_mutex);
48563f2dd94aSFrançois Tigeot 
48574be47400SFrançois Tigeot 		GEM_BUG_ON(i915_gem_object_is_active(obj));
48584be47400SFrançois Tigeot 		list_for_each_entry_safe(vma, vn,
48594be47400SFrançois Tigeot 					 &obj->vma_list, obj_link) {
48604be47400SFrançois Tigeot 			GEM_BUG_ON(i915_vma_is_active(vma));
48614be47400SFrançois Tigeot 			vma->flags &= ~I915_VMA_PIN_MASK;
48624be47400SFrançois Tigeot 			i915_vma_close(vma);
48634be47400SFrançois Tigeot 		}
48644be47400SFrançois Tigeot 		GEM_BUG_ON(!list_empty(&obj->vma_list));
48654be47400SFrançois Tigeot 		GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
48664be47400SFrançois Tigeot 
48673f2dd94aSFrançois Tigeot 		/* This serializes freeing with the shrinker. Since the free
48683f2dd94aSFrançois Tigeot 		 * is delayed, first by RCU then by the workqueue, we want the
48693f2dd94aSFrançois Tigeot 		 * shrinker to be able to free pages of unreferenced objects,
48703f2dd94aSFrançois Tigeot 		 * or else we may oom whilst there are plenty of deferred
48713f2dd94aSFrançois Tigeot 		 * freed objects.
48723f2dd94aSFrançois Tigeot 		 */
48733f2dd94aSFrançois Tigeot 		if (i915_gem_object_has_pages(obj)) {
48743f2dd94aSFrançois Tigeot 			lockmgr(&i915->mm.obj_lock, LK_EXCLUSIVE);
48753f2dd94aSFrançois Tigeot 			list_del_init(&obj->mm.link);
48763f2dd94aSFrançois Tigeot 			lockmgr(&i915->mm.obj_lock, LK_RELEASE);
48774be47400SFrançois Tigeot 		}
48783f2dd94aSFrançois Tigeot 
48794be47400SFrançois Tigeot 		mutex_unlock(&i915->drm.struct_mutex);
48804be47400SFrançois Tigeot 
48814be47400SFrançois Tigeot 		GEM_BUG_ON(obj->bind_count);
48823f2dd94aSFrançois Tigeot 		GEM_BUG_ON(obj->userfault_count);
48834be47400SFrançois Tigeot 		GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
48843f2dd94aSFrançois Tigeot 		GEM_BUG_ON(!list_empty(&obj->lut_list));
48854be47400SFrançois Tigeot 
48864be47400SFrançois Tigeot 		if (obj->ops->release)
48874be47400SFrançois Tigeot 			obj->ops->release(obj);
48884be47400SFrançois Tigeot 
48894be47400SFrançois Tigeot #if 0
48904be47400SFrançois Tigeot 		if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
48914be47400SFrançois Tigeot #else
48924be47400SFrançois Tigeot 		if (i915_gem_object_has_pinned_pages(obj))
48934be47400SFrançois Tigeot #endif
48944be47400SFrançois Tigeot 			atomic_set(&obj->mm.pages_pin_count, 0);
48954be47400SFrançois Tigeot 		__i915_gem_object_put_pages(obj, I915_MM_NORMAL);
48963f2dd94aSFrançois Tigeot 		GEM_BUG_ON(i915_gem_object_has_pages(obj));
48974be47400SFrançois Tigeot 
48984be47400SFrançois Tigeot 		if (obj->base.import_attach)
48994be47400SFrançois Tigeot 			drm_prime_gem_destroy(&obj->base, NULL);
49004be47400SFrançois Tigeot 
49014be47400SFrançois Tigeot 		reservation_object_fini(&obj->__builtin_resv);
49024be47400SFrançois Tigeot 		drm_gem_object_release(&obj->base);
49034be47400SFrançois Tigeot 		i915_gem_info_remove_obj(i915, obj->base.size);
49044be47400SFrançois Tigeot 
49054be47400SFrançois Tigeot 		kfree(obj->bit_17);
49064be47400SFrançois Tigeot 		i915_gem_object_free(obj);
49073f2dd94aSFrançois Tigeot 
49083f2dd94aSFrançois Tigeot 		if (on)
49093f2dd94aSFrançois Tigeot 			cond_resched();
49104be47400SFrançois Tigeot 	}
49113f2dd94aSFrançois Tigeot 	intel_runtime_pm_put(i915);
49124be47400SFrançois Tigeot }
49134be47400SFrançois Tigeot 
i915_gem_flush_free_objects(struct drm_i915_private * i915)49144be47400SFrançois Tigeot static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
49154be47400SFrançois Tigeot {
49164be47400SFrançois Tigeot 	struct llist_node *freed;
49174be47400SFrançois Tigeot 
49183f2dd94aSFrançois Tigeot 	/* Free the oldest, most stale object to keep the free_list short */
49193f2dd94aSFrançois Tigeot 	freed = NULL;
49203f2dd94aSFrançois Tigeot 	if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
49213f2dd94aSFrançois Tigeot 		/* Only one consumer of llist_del_first() allowed */
49223f2dd94aSFrançois Tigeot 		lockmgr(&i915->mm.free_lock, LK_EXCLUSIVE);
49233f2dd94aSFrançois Tigeot 		freed = llist_del_first(&i915->mm.free_list);
49243f2dd94aSFrançois Tigeot 		lockmgr(&i915->mm.free_lock, LK_RELEASE);
49253f2dd94aSFrançois Tigeot 	}
49263f2dd94aSFrançois Tigeot 	if (unlikely(freed)) {
49273f2dd94aSFrançois Tigeot 		freed->next = NULL;
49284be47400SFrançois Tigeot 		__i915_gem_free_objects(i915, freed);
49294be47400SFrançois Tigeot 	}
49303f2dd94aSFrançois Tigeot }
49314be47400SFrançois Tigeot 
__i915_gem_free_work(struct work_struct * work)49324be47400SFrançois Tigeot static void __i915_gem_free_work(struct work_struct *work)
49334be47400SFrançois Tigeot {
49344be47400SFrançois Tigeot 	struct drm_i915_private *i915 =
49354be47400SFrançois Tigeot 		container_of(work, struct drm_i915_private, mm.free_work);
49364be47400SFrançois Tigeot 	struct llist_node *freed;
49374be47400SFrançois Tigeot 
493871f41f3eSFrançois Tigeot 	/* All file-owned VMA should have been released by this point through
493971f41f3eSFrançois Tigeot 	 * i915_gem_close_object(), or earlier by i915_gem_context_close().
494071f41f3eSFrançois Tigeot 	 * However, the object may also be bound into the global GTT (e.g.
494171f41f3eSFrançois Tigeot 	 * older GPUs without per-process support, or for direct access through
494271f41f3eSFrançois Tigeot 	 * the GTT either for the user or for scanout). Those VMA still need to
494371f41f3eSFrançois Tigeot 	 * unbound now.
494471f41f3eSFrançois Tigeot 	 */
49454be47400SFrançois Tigeot 
49463f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.free_lock, LK_EXCLUSIVE);
49473f2dd94aSFrançois Tigeot 	while ((freed = llist_del_all(&i915->mm.free_list))) {
49483f2dd94aSFrançois Tigeot 		lockmgr(&i915->mm.free_lock, LK_RELEASE);
49493f2dd94aSFrançois Tigeot 
49504be47400SFrançois Tigeot 		__i915_gem_free_objects(i915, freed);
49513f2dd94aSFrançois Tigeot 		if (need_resched())
49523f2dd94aSFrançois Tigeot 			return;
49533f2dd94aSFrançois Tigeot 
49543f2dd94aSFrançois Tigeot 		lockmgr(&i915->mm.free_lock, LK_EXCLUSIVE);
49553f2dd94aSFrançois Tigeot 	}
49563f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.free_lock, LK_RELEASE);
4957e3adcf8fSFrançois Tigeot }
4958e3adcf8fSFrançois Tigeot 
__i915_gem_free_object_rcu(struct rcu_head * head)49594be47400SFrançois Tigeot static void __i915_gem_free_object_rcu(struct rcu_head *head)
49604be47400SFrançois Tigeot {
49614be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj =
49624be47400SFrançois Tigeot 		container_of(head, typeof(*obj), rcu);
49634be47400SFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
49645d0b1887SFrançois Tigeot 
49654be47400SFrançois Tigeot 	/* We can't simply use call_rcu() from i915_gem_free_object()
49664be47400SFrançois Tigeot 	 * as we need to block whilst unbinding, and the call_rcu
49674be47400SFrançois Tigeot 	 * task may be called from softirq context. So we take a
49684be47400SFrançois Tigeot 	 * detour through a worker.
49694be47400SFrançois Tigeot 	 */
49704be47400SFrançois Tigeot 	if (llist_add(&obj->freed, &i915->mm.free_list))
49714be47400SFrançois Tigeot 		schedule_work(&i915->mm.free_work);
49724be47400SFrançois Tigeot }
497324edb884SFrançois Tigeot 
i915_gem_free_object(struct drm_gem_object * gem_obj)49744be47400SFrançois Tigeot void i915_gem_free_object(struct drm_gem_object *gem_obj)
49754be47400SFrançois Tigeot {
49764be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
49772c9916cdSFrançois Tigeot 
49784be47400SFrançois Tigeot 	if (obj->mm.quirked)
49794be47400SFrançois Tigeot 		__i915_gem_object_unpin_pages(obj);
49804be47400SFrançois Tigeot 
4981ba55f2f5SFrançois Tigeot 	if (discard_backing_storage(obj))
49824be47400SFrançois Tigeot 		obj->mm.madv = I915_MADV_DONTNEED;
4983f192107fSFrançois Tigeot 
49844be47400SFrançois Tigeot 	/* Before we free the object, make sure any pure RCU-only
49854be47400SFrançois Tigeot 	 * read-side critical sections are complete, e.g.
49864be47400SFrançois Tigeot 	 * i915_gem_busy_ioctl(). For the corresponding synchronized
49874be47400SFrançois Tigeot 	 * lookup see i915_gem_object_lookup_rcu().
49884be47400SFrançois Tigeot 	 */
49894be47400SFrançois Tigeot 	call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
49904be47400SFrançois Tigeot }
49915d0b1887SFrançois Tigeot 
__i915_gem_object_release_unless_active(struct drm_i915_gem_object * obj)49924be47400SFrançois Tigeot void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
49934be47400SFrançois Tigeot {
49944be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
49959edbd4a0SFrançois Tigeot 
49963f2dd94aSFrançois Tigeot 	if (!i915_gem_object_has_active_reference(obj) &&
49973f2dd94aSFrançois Tigeot 	    i915_gem_object_is_active(obj))
49984be47400SFrançois Tigeot 		i915_gem_object_set_active_reference(obj);
49994be47400SFrançois Tigeot 	else
50004be47400SFrançois Tigeot 		i915_gem_object_put(obj);
50014be47400SFrançois Tigeot }
5002ba55f2f5SFrançois Tigeot 
assert_kernel_context_is_current(struct drm_i915_private * dev_priv)50034be47400SFrançois Tigeot static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
50044be47400SFrançois Tigeot {
50054be47400SFrançois Tigeot 	struct intel_engine_cs *engine;
50064be47400SFrançois Tigeot 	enum intel_engine_id id;
5007e3adcf8fSFrançois Tigeot 
50084be47400SFrançois Tigeot 	for_each_engine(engine, dev_priv, id)
5009a85cb24fSFrançois Tigeot 		GEM_BUG_ON(engine->last_retired_context &&
5010a85cb24fSFrançois Tigeot 			   !i915_gem_context_is_kernel(engine->last_retired_context));
50119edbd4a0SFrançois Tigeot }
50129edbd4a0SFrançois Tigeot 
i915_gem_sanitize(struct drm_i915_private * i915)5013a85cb24fSFrançois Tigeot void i915_gem_sanitize(struct drm_i915_private *i915)
5014ba55f2f5SFrançois Tigeot {
50153f2dd94aSFrançois Tigeot 	if (i915_terminally_wedged(&i915->gpu_error)) {
50163f2dd94aSFrançois Tigeot 		mutex_lock(&i915->drm.struct_mutex);
50173f2dd94aSFrançois Tigeot 		i915_gem_unset_wedged(i915);
50183f2dd94aSFrançois Tigeot 		mutex_unlock(&i915->drm.struct_mutex);
50193f2dd94aSFrançois Tigeot 	}
50203f2dd94aSFrançois Tigeot 
5021a85cb24fSFrançois Tigeot 	/*
5022a85cb24fSFrançois Tigeot 	 * If we inherit context state from the BIOS or earlier occupants
5023a85cb24fSFrançois Tigeot 	 * of the GPU, the GPU may be in an inconsistent state when we
5024a85cb24fSFrançois Tigeot 	 * try to take over. The only way to remove the earlier state
5025a85cb24fSFrançois Tigeot 	 * is by resetting. However, resetting on earlier gen is tricky as
5026a85cb24fSFrançois Tigeot 	 * it may impact the display and we are uncertain about the stability
50273f2dd94aSFrançois Tigeot 	 * of the reset, so this could be applied to even earlier gen.
5028a85cb24fSFrançois Tigeot 	 */
50293f2dd94aSFrançois Tigeot 	if (INTEL_GEN(i915) >= 5) {
5030a85cb24fSFrançois Tigeot 		int reset = intel_gpu_reset(i915, ALL_ENGINES);
5031a85cb24fSFrançois Tigeot 		WARN_ON(reset && reset != -ENODEV);
5032a85cb24fSFrançois Tigeot 	}
5033a85cb24fSFrançois Tigeot }
5034a85cb24fSFrançois Tigeot 
i915_gem_suspend(struct drm_i915_private * dev_priv)5035a85cb24fSFrançois Tigeot int i915_gem_suspend(struct drm_i915_private *dev_priv)
5036a85cb24fSFrançois Tigeot {
5037a85cb24fSFrançois Tigeot 	struct drm_device *dev = &dev_priv->drm;
503871f41f3eSFrançois Tigeot 	int ret;
5039e11a51e3SFrançois Tigeot 
5040a85cb24fSFrançois Tigeot 	intel_runtime_pm_get(dev_priv);
504187df8fc6SFrançois Tigeot 	intel_suspend_gt_powersave(dev_priv);
504287df8fc6SFrançois Tigeot 
5043a2fdbec6SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
504487df8fc6SFrançois Tigeot 
504587df8fc6SFrançois Tigeot 	/* We have to flush all the executing contexts to main memory so
504687df8fc6SFrançois Tigeot 	 * that they can saved in the hibernation image. To ensure the last
504787df8fc6SFrançois Tigeot 	 * context image is coherent, we have to switch away from it. That
504887df8fc6SFrançois Tigeot 	 * leaves the dev_priv->kernel_context still active when
504987df8fc6SFrançois Tigeot 	 * we actually suspend, and its image in memory may not match the GPU
505087df8fc6SFrançois Tigeot 	 * state. Fortunately, the kernel_context is disposable and we do
505187df8fc6SFrançois Tigeot 	 * not rely on its state.
505287df8fc6SFrançois Tigeot 	 */
50533f2dd94aSFrançois Tigeot 	if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
505487df8fc6SFrançois Tigeot 		ret = i915_gem_switch_to_kernel_context(dev_priv);
505587df8fc6SFrançois Tigeot 		if (ret)
5056a85cb24fSFrançois Tigeot 			goto err_unlock;
505787df8fc6SFrançois Tigeot 
50581e12ee3bSFrançois Tigeot 		ret = i915_gem_wait_for_idle(dev_priv,
50591e12ee3bSFrançois Tigeot 					     I915_WAIT_INTERRUPTIBLE |
50601e12ee3bSFrançois Tigeot 					     I915_WAIT_LOCKED);
50613f2dd94aSFrançois Tigeot 		if (ret && ret != -EIO)
5062a85cb24fSFrançois Tigeot 			goto err_unlock;
5063e11a51e3SFrançois Tigeot 
50644be47400SFrançois Tigeot 		assert_kernel_context_is_current(dev_priv);
50653f2dd94aSFrançois Tigeot 	}
50663f2dd94aSFrançois Tigeot 	i915_gem_contexts_lost(dev_priv);
5067a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
5068f192107fSFrançois Tigeot 
5069a85cb24fSFrançois Tigeot 	intel_guc_suspend(dev_priv);
5070a85cb24fSFrançois Tigeot 
50712c9916cdSFrançois Tigeot 	cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
5072303bf270SFrançois Tigeot 	cancel_delayed_work_sync(&dev_priv->gt.retire_work);
5073a85cb24fSFrançois Tigeot 
5074a85cb24fSFrançois Tigeot 	/* As the idle_work is rearming if it detects a race, play safe and
5075a85cb24fSFrançois Tigeot 	 * repeat the flush until it is definitely idle.
5076a85cb24fSFrançois Tigeot 	 */
50773f2dd94aSFrançois Tigeot 	drain_delayed_work(&dev_priv->gt.idle_work);
5078e11a51e3SFrançois Tigeot 
50792c9916cdSFrançois Tigeot 	/* Assert that we sucessfully flushed all the work and
50802c9916cdSFrançois Tigeot 	 * reset the GPU back to its idle, low power state.
50812c9916cdSFrançois Tigeot 	 */
5082303bf270SFrançois Tigeot 	WARN_ON(dev_priv->gt.awake);
50833f2dd94aSFrançois Tigeot 	if (WARN_ON(!intel_engines_are_idle(dev_priv)))
50843f2dd94aSFrançois Tigeot 		i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
50852c9916cdSFrançois Tigeot 
50861e12ee3bSFrançois Tigeot 	/*
50871e12ee3bSFrançois Tigeot 	 * Neither the BIOS, ourselves or any other kernel
50881e12ee3bSFrançois Tigeot 	 * expects the system to be in execlists mode on startup,
50891e12ee3bSFrançois Tigeot 	 * so we need to reset the GPU back to legacy mode. And the only
50901e12ee3bSFrançois Tigeot 	 * known way to disable logical contexts is through a GPU reset.
50911e12ee3bSFrançois Tigeot 	 *
50921e12ee3bSFrançois Tigeot 	 * So in order to leave the system in a known default configuration,
50931e12ee3bSFrançois Tigeot 	 * always reset the GPU upon unload and suspend. Afterwards we then
50941e12ee3bSFrançois Tigeot 	 * clean up the GEM state tracking, flushing off the requests and
50951e12ee3bSFrançois Tigeot 	 * leaving the system in a known idle state.
50961e12ee3bSFrançois Tigeot 	 *
50971e12ee3bSFrançois Tigeot 	 * Note that is of the upmost importance that the GPU is idle and
50981e12ee3bSFrançois Tigeot 	 * all stray writes are flushed *before* we dismantle the backing
50991e12ee3bSFrançois Tigeot 	 * storage for the pinned objects.
51001e12ee3bSFrançois Tigeot 	 *
51011e12ee3bSFrançois Tigeot 	 * However, since we are uncertain that resetting the GPU on older
51021e12ee3bSFrançois Tigeot 	 * machines is a good idea, we don't - just in case it leaves the
51031e12ee3bSFrançois Tigeot 	 * machine in an unusable condition.
51041e12ee3bSFrançois Tigeot 	 */
5105a85cb24fSFrançois Tigeot 	i915_gem_sanitize(dev_priv);
51063f2dd94aSFrançois Tigeot 
51073f2dd94aSFrançois Tigeot 	intel_runtime_pm_put(dev_priv);
51083f2dd94aSFrançois Tigeot 	return 0;
51091e12ee3bSFrançois Tigeot 
5110a85cb24fSFrançois Tigeot err_unlock:
51119edbd4a0SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
5112a85cb24fSFrançois Tigeot 	intel_runtime_pm_put(dev_priv);
51139edbd4a0SFrançois Tigeot 	return ret;
5114e11a51e3SFrançois Tigeot }
5115e11a51e3SFrançois Tigeot 
i915_gem_resume(struct drm_i915_private * dev_priv)5116a85cb24fSFrançois Tigeot void i915_gem_resume(struct drm_i915_private *dev_priv)
511787df8fc6SFrançois Tigeot {
5118a85cb24fSFrançois Tigeot 	struct drm_device *dev = &dev_priv->drm;
511987df8fc6SFrançois Tigeot 
51204be47400SFrançois Tigeot 	WARN_ON(dev_priv->gt.awake);
51214be47400SFrançois Tigeot 
512287df8fc6SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
51234be47400SFrançois Tigeot 	i915_gem_restore_gtt_mappings(dev_priv);
51243f2dd94aSFrançois Tigeot 	i915_gem_restore_fences(dev_priv);
512587df8fc6SFrançois Tigeot 
512687df8fc6SFrançois Tigeot 	/* As we didn't flush the kernel context before suspend, we cannot
512787df8fc6SFrançois Tigeot 	 * guarantee that the context image is complete. So let's just reset
512887df8fc6SFrançois Tigeot 	 * it and start again.
512987df8fc6SFrançois Tigeot 	 */
51301e12ee3bSFrançois Tigeot 	dev_priv->gt.resume(dev_priv);
513187df8fc6SFrançois Tigeot 
513287df8fc6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
513387df8fc6SFrançois Tigeot }
513487df8fc6SFrançois Tigeot 
i915_gem_init_swizzling(struct drm_i915_private * dev_priv)51354be47400SFrançois Tigeot void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
5136e11a51e3SFrançois Tigeot {
51374be47400SFrançois Tigeot 	if (INTEL_GEN(dev_priv) < 5 ||
5138e11a51e3SFrançois Tigeot 	    dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5139e11a51e3SFrançois Tigeot 		return;
5140e11a51e3SFrançois Tigeot 
5141e11a51e3SFrançois Tigeot 	I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5142e11a51e3SFrançois Tigeot 				 DISP_TILE_SURFACE_SWIZZLING);
5143e11a51e3SFrançois Tigeot 
51441e12ee3bSFrançois Tigeot 	if (IS_GEN5(dev_priv))
5145e11a51e3SFrançois Tigeot 		return;
5146e11a51e3SFrançois Tigeot 
5147e11a51e3SFrançois Tigeot 	I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
51481e12ee3bSFrançois Tigeot 	if (IS_GEN6(dev_priv))
5149e11a51e3SFrançois Tigeot 		I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
51501e12ee3bSFrançois Tigeot 	else if (IS_GEN7(dev_priv))
5151e11a51e3SFrançois Tigeot 		I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
51521e12ee3bSFrançois Tigeot 	else if (IS_GEN8(dev_priv))
51539edbd4a0SFrançois Tigeot 		I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
5154a2fdbec6SFrançois Tigeot 	else
5155a2fdbec6SFrançois Tigeot 		BUG();
5156e11a51e3SFrançois Tigeot }
5157e11a51e3SFrançois Tigeot 
init_unused_ring(struct drm_i915_private * dev_priv,u32 base)51581e12ee3bSFrançois Tigeot static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
51591b13d190SFrançois Tigeot {
51601b13d190SFrançois Tigeot 	I915_WRITE(RING_CTL(base), 0);
51611b13d190SFrançois Tigeot 	I915_WRITE(RING_HEAD(base), 0);
51621b13d190SFrançois Tigeot 	I915_WRITE(RING_TAIL(base), 0);
51631b13d190SFrançois Tigeot 	I915_WRITE(RING_START(base), 0);
51641b13d190SFrançois Tigeot }
51651b13d190SFrançois Tigeot 
init_unused_rings(struct drm_i915_private * dev_priv)51661e12ee3bSFrançois Tigeot static void init_unused_rings(struct drm_i915_private *dev_priv)
51671b13d190SFrançois Tigeot {
51681e12ee3bSFrançois Tigeot 	if (IS_I830(dev_priv)) {
51691e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, PRB1_BASE);
51701e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, SRB0_BASE);
51711e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, SRB1_BASE);
51721e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, SRB2_BASE);
51731e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, SRB3_BASE);
51741e12ee3bSFrançois Tigeot 	} else if (IS_GEN2(dev_priv)) {
51751e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, SRB0_BASE);
51761e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, SRB1_BASE);
51771e12ee3bSFrançois Tigeot 	} else if (IS_GEN3(dev_priv)) {
51781e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, PRB1_BASE);
51791e12ee3bSFrançois Tigeot 		init_unused_ring(dev_priv, PRB2_BASE);
51801b13d190SFrançois Tigeot 	}
51811b13d190SFrançois Tigeot }
51821b13d190SFrançois Tigeot 
__i915_gem_restart_engines(void * data)5183a85cb24fSFrançois Tigeot static int __i915_gem_restart_engines(void *data)
5184a2fdbec6SFrançois Tigeot {
5185a85cb24fSFrançois Tigeot 	struct drm_i915_private *i915 = data;
51868621f407SFrançois Tigeot 	struct intel_engine_cs *engine;
51871e12ee3bSFrançois Tigeot 	enum intel_engine_id id;
5188a85cb24fSFrançois Tigeot 	int err;
5189a85cb24fSFrançois Tigeot 
5190a85cb24fSFrançois Tigeot 	for_each_engine(engine, i915, id) {
5191a85cb24fSFrançois Tigeot 		err = engine->init_hw(engine);
5192a85cb24fSFrançois Tigeot 		if (err)
5193a85cb24fSFrançois Tigeot 			return err;
5194a85cb24fSFrançois Tigeot 	}
5195a85cb24fSFrançois Tigeot 
5196a85cb24fSFrançois Tigeot 	return 0;
5197a85cb24fSFrançois Tigeot }
5198a85cb24fSFrançois Tigeot 
i915_gem_init_hw(struct drm_i915_private * dev_priv)5199a85cb24fSFrançois Tigeot int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5200a85cb24fSFrançois Tigeot {
52011487f786SFrançois Tigeot 	int ret;
5202a2fdbec6SFrançois Tigeot 
52034be47400SFrançois Tigeot 	dev_priv->gt.last_init_time = ktime_get();
52044be47400SFrançois Tigeot 
52052c9916cdSFrançois Tigeot 	/* Double layer security blanket, see i915_gem_init() */
52062c9916cdSFrançois Tigeot 	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
52072c9916cdSFrançois Tigeot 
52084be47400SFrançois Tigeot 	if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
52099edbd4a0SFrançois Tigeot 		I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
52109edbd4a0SFrançois Tigeot 
52111e12ee3bSFrançois Tigeot 	if (IS_HASWELL(dev_priv))
52121e12ee3bSFrançois Tigeot 		I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
52139edbd4a0SFrançois Tigeot 			   LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
5214a2fdbec6SFrançois Tigeot 
52151e12ee3bSFrançois Tigeot 	if (HAS_PCH_NOP(dev_priv)) {
52161e12ee3bSFrançois Tigeot 		if (IS_IVYBRIDGE(dev_priv)) {
52178e26cdf6SFrançois Tigeot 			u32 temp = I915_READ(GEN7_MSG_CTL);
52188e26cdf6SFrançois Tigeot 			temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
52198e26cdf6SFrançois Tigeot 			I915_WRITE(GEN7_MSG_CTL, temp);
52204be47400SFrançois Tigeot 		} else if (INTEL_GEN(dev_priv) >= 7) {
5221ba55f2f5SFrançois Tigeot 			u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5222ba55f2f5SFrançois Tigeot 			temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5223ba55f2f5SFrançois Tigeot 			I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5224ba55f2f5SFrançois Tigeot 		}
52258e26cdf6SFrançois Tigeot 	}
52268e26cdf6SFrançois Tigeot 
52274be47400SFrançois Tigeot 	i915_gem_init_swizzling(dev_priv);
5228a2fdbec6SFrançois Tigeot 
52292c9916cdSFrançois Tigeot 	/*
52302c9916cdSFrançois Tigeot 	 * At least 830 can leave some of the unused rings
52312c9916cdSFrançois Tigeot 	 * "active" (ie. head != tail) after resume which
52322c9916cdSFrançois Tigeot 	 * will prevent c3 entry. Makes sure all unused rings
52332c9916cdSFrançois Tigeot 	 * are totally idle.
52342c9916cdSFrançois Tigeot 	 */
52351e12ee3bSFrançois Tigeot 	init_unused_rings(dev_priv);
52362c9916cdSFrançois Tigeot 
5237c0e85e96SFrançois Tigeot 	BUG_ON(!dev_priv->kernel_context);
52383f2dd94aSFrançois Tigeot 	if (i915_terminally_wedged(&dev_priv->gpu_error)) {
52393f2dd94aSFrançois Tigeot 		ret = -EIO;
52403f2dd94aSFrançois Tigeot 		goto out;
52413f2dd94aSFrançois Tigeot 	}
5242a05eeebfSFrançois Tigeot 
52434be47400SFrançois Tigeot 	ret = i915_ppgtt_init_hw(dev_priv);
5244a05eeebfSFrançois Tigeot 	if (ret) {
5245a05eeebfSFrançois Tigeot 		DRM_ERROR("PPGTT enable HW failed %d\n", ret);
5246a05eeebfSFrançois Tigeot 		goto out;
5247a05eeebfSFrançois Tigeot 	}
5248a05eeebfSFrançois Tigeot 
5249a05eeebfSFrançois Tigeot 	/* Need to do basic initialisation of all rings first: */
5250a85cb24fSFrançois Tigeot 	ret = __i915_gem_restart_engines(dev_priv);
5251a2fdbec6SFrançois Tigeot 	if (ret)
52522c9916cdSFrançois Tigeot 		goto out;
525315ac6249SFrançois Tigeot 
5254a85cb24fSFrançois Tigeot 	intel_mocs_init_l3cc_table(dev_priv);
52558621f407SFrançois Tigeot 
5256352ff8bdSFrançois Tigeot 	/* We can't enable contexts until all firmware is loaded */
5257a85cb24fSFrançois Tigeot 	ret = intel_uc_init_hw(dev_priv);
52581487f786SFrançois Tigeot 	if (ret)
5259352ff8bdSFrançois Tigeot 		goto out;
5260352ff8bdSFrançois Tigeot 
52612c9916cdSFrançois Tigeot out:
52622c9916cdSFrançois Tigeot 	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
52639edbd4a0SFrançois Tigeot 	return ret;
52649edbd4a0SFrançois Tigeot }
52659edbd4a0SFrançois Tigeot 
intel_sanitize_semaphores(struct drm_i915_private * dev_priv,int value)526687df8fc6SFrançois Tigeot bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
526787df8fc6SFrançois Tigeot {
526887df8fc6SFrançois Tigeot 	if (INTEL_INFO(dev_priv)->gen < 6)
526987df8fc6SFrançois Tigeot 		return false;
527087df8fc6SFrançois Tigeot 
527187df8fc6SFrançois Tigeot 	/* TODO: make semaphores and Execlists play nicely together */
52723f2dd94aSFrançois Tigeot 	if (i915_modparams.enable_execlists)
527387df8fc6SFrançois Tigeot 		return false;
527487df8fc6SFrançois Tigeot 
527587df8fc6SFrançois Tigeot 	if (value >= 0)
527687df8fc6SFrançois Tigeot 		return value;
527787df8fc6SFrançois Tigeot 
527887df8fc6SFrançois Tigeot 	/* Enable semaphores on SNB when IO remapping is off */
52793f2dd94aSFrançois Tigeot 	if (IS_GEN6(dev_priv) && intel_vtd_active())
528087df8fc6SFrançois Tigeot 		return false;
528187df8fc6SFrançois Tigeot 
528287df8fc6SFrançois Tigeot 	return true;
528387df8fc6SFrançois Tigeot }
528487df8fc6SFrançois Tigeot 
i915_gem_init(struct drm_i915_private * dev_priv)5285a85cb24fSFrançois Tigeot int i915_gem_init(struct drm_i915_private *dev_priv)
5286f192107fSFrançois Tigeot {
5287f192107fSFrançois Tigeot 	int ret;
5288f192107fSFrançois Tigeot 
52893f2dd94aSFrançois Tigeot 	/*
52903f2dd94aSFrançois Tigeot 	 * We need to fallback to 4K pages since gvt gtt handling doesn't
52913f2dd94aSFrançois Tigeot 	 * support huge page entries - we will need to check either hypervisor
52923f2dd94aSFrançois Tigeot 	 * mm can support huge guest page or just do emulation in gvt.
52933f2dd94aSFrançois Tigeot 	 */
52943f2dd94aSFrançois Tigeot 	if (intel_vgpu_active(dev_priv))
52953f2dd94aSFrançois Tigeot 		mkwrite_device_info(dev_priv)->page_sizes =
52963f2dd94aSFrançois Tigeot 			I915_GTT_PAGE_SIZE_4K;
5297a85cb24fSFrançois Tigeot 
52983f2dd94aSFrançois Tigeot 	dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
52998e26cdf6SFrançois Tigeot 
53003f2dd94aSFrançois Tigeot 	if (!i915_modparams.enable_execlists) {
53011e12ee3bSFrançois Tigeot 		dev_priv->gt.resume = intel_legacy_submission_resume;
530271f41f3eSFrançois Tigeot 		dev_priv->gt.cleanup_engine = intel_engine_cleanup;
53031b13d190SFrançois Tigeot 	} else {
53041e12ee3bSFrançois Tigeot 		dev_priv->gt.resume = intel_lr_context_resume;
53058621f407SFrançois Tigeot 		dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
53061b13d190SFrançois Tigeot 	}
53071b13d190SFrançois Tigeot 
53083f2dd94aSFrançois Tigeot 	ret = i915_gem_init_userptr(dev_priv);
53093f2dd94aSFrançois Tigeot 	if (ret)
53103f2dd94aSFrançois Tigeot 		return ret;
53113f2dd94aSFrançois Tigeot 
53122c9916cdSFrançois Tigeot 	/* This is just a security blanket to placate dragons.
53132c9916cdSFrançois Tigeot 	 * On some systems, we very sporadically observe that the first TLBs
53142c9916cdSFrançois Tigeot 	 * used by the CS may be stale, despite us poking the TLB reset. If
53152c9916cdSFrançois Tigeot 	 * we hold the forcewake during initialisation these problems
53162c9916cdSFrançois Tigeot 	 * just magically go away.
53172c9916cdSFrançois Tigeot 	 */
53183f2dd94aSFrançois Tigeot 	mutex_lock(&dev_priv->drm.struct_mutex);
53192c9916cdSFrançois Tigeot 	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
53202c9916cdSFrançois Tigeot 
532171f41f3eSFrançois Tigeot 	ret = i915_gem_init_ggtt(dev_priv);
532271f41f3eSFrançois Tigeot 	if (ret)
532371f41f3eSFrançois Tigeot 		goto out_unlock;
53248e26cdf6SFrançois Tigeot 
53253f2dd94aSFrançois Tigeot 	ret = i915_gem_contexts_init(dev_priv);
53262c9916cdSFrançois Tigeot 	if (ret)
53272c9916cdSFrançois Tigeot 		goto out_unlock;
53282c9916cdSFrançois Tigeot 
5329a85cb24fSFrançois Tigeot 	ret = intel_engines_init(dev_priv);
53302c9916cdSFrançois Tigeot 	if (ret)
53312c9916cdSFrançois Tigeot 		goto out_unlock;
5332f192107fSFrançois Tigeot 
5333a85cb24fSFrançois Tigeot 	ret = i915_gem_init_hw(dev_priv);
5334ba55f2f5SFrançois Tigeot 	if (ret == -EIO) {
533571f41f3eSFrançois Tigeot 		/* Allow engine initialisation to fail by marking the GPU as
5336ba55f2f5SFrançois Tigeot 		 * wedged. But we only want to do this where the GPU is angry,
5337ba55f2f5SFrançois Tigeot 		 * for all other failure, such as an allocation failure, bail.
5338ba55f2f5SFrançois Tigeot 		 */
53393f2dd94aSFrançois Tigeot 		if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5340ba55f2f5SFrançois Tigeot 			DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
53411e12ee3bSFrançois Tigeot 			i915_gem_set_wedged(dev_priv);
53423f2dd94aSFrançois Tigeot 		}
5343ba55f2f5SFrançois Tigeot 		ret = 0;
5344ba55f2f5SFrançois Tigeot 	}
53452c9916cdSFrançois Tigeot 
53462c9916cdSFrançois Tigeot out_unlock:
53472c9916cdSFrançois Tigeot 	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5348a85cb24fSFrançois Tigeot 	mutex_unlock(&dev_priv->drm.struct_mutex);
5349ba55f2f5SFrançois Tigeot 
5350ba55f2f5SFrançois Tigeot 	return ret;
5351f192107fSFrançois Tigeot }
5352f192107fSFrançois Tigeot 
i915_gem_init_mmio(struct drm_i915_private * i915)5353a85cb24fSFrançois Tigeot void i915_gem_init_mmio(struct drm_i915_private *i915)
5354e11a51e3SFrançois Tigeot {
5355a85cb24fSFrançois Tigeot 	i915_gem_sanitize(i915);
5356a85cb24fSFrançois Tigeot }
5357a85cb24fSFrançois Tigeot 
5358a85cb24fSFrançois Tigeot void
i915_gem_cleanup_engines(struct drm_i915_private * dev_priv)5359a85cb24fSFrançois Tigeot i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
5360a85cb24fSFrançois Tigeot {
53618621f407SFrançois Tigeot 	struct intel_engine_cs *engine;
53621e12ee3bSFrançois Tigeot 	enum intel_engine_id id;
5363e11a51e3SFrançois Tigeot 
53641e12ee3bSFrançois Tigeot 	for_each_engine(engine, dev_priv, id)
53658621f407SFrançois Tigeot 		dev_priv->gt.cleanup_engine(engine);
5366e11a51e3SFrançois Tigeot }
5367e11a51e3SFrançois Tigeot 
53688621f407SFrançois Tigeot void
i915_gem_load_init_fences(struct drm_i915_private * dev_priv)53698621f407SFrançois Tigeot i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
53708621f407SFrançois Tigeot {
53711e12ee3bSFrançois Tigeot 	int i;
53728621f407SFrançois Tigeot 
53738621f407SFrançois Tigeot 	if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
53748621f407SFrançois Tigeot 	    !IS_CHERRYVIEW(dev_priv))
53758621f407SFrançois Tigeot 		dev_priv->num_fence_regs = 32;
5376a85cb24fSFrançois Tigeot 	else if (INTEL_INFO(dev_priv)->gen >= 4 ||
5377a85cb24fSFrançois Tigeot 		 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5378a85cb24fSFrançois Tigeot 		 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
53798621f407SFrançois Tigeot 		dev_priv->num_fence_regs = 16;
53808621f407SFrançois Tigeot 	else
53818621f407SFrançois Tigeot 		dev_priv->num_fence_regs = 8;
53828621f407SFrançois Tigeot 
53831487f786SFrançois Tigeot 	if (intel_vgpu_active(dev_priv))
53848621f407SFrançois Tigeot 		dev_priv->num_fence_regs =
53858621f407SFrançois Tigeot 				I915_READ(vgtif_reg(avail_rs.fence_num));
53868621f407SFrançois Tigeot 
53878621f407SFrançois Tigeot 	/* Initialize fence registers to zero */
53881e12ee3bSFrançois Tigeot 	for (i = 0; i < dev_priv->num_fence_regs; i++) {
53891e12ee3bSFrançois Tigeot 		struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
53901e12ee3bSFrançois Tigeot 
53911e12ee3bSFrançois Tigeot 		fence->i915 = dev_priv;
53921e12ee3bSFrançois Tigeot 		fence->id = i;
53931e12ee3bSFrançois Tigeot 		list_add_tail(&fence->link, &dev_priv->mm.fence_list);
53941e12ee3bSFrançois Tigeot 	}
53954be47400SFrançois Tigeot 	i915_gem_restore_fences(dev_priv);
53968621f407SFrançois Tigeot 
53974be47400SFrançois Tigeot 	i915_gem_detect_bit_6_swizzle(dev_priv);
5398e11a51e3SFrançois Tigeot }
5399e11a51e3SFrançois Tigeot 
54004be47400SFrançois Tigeot int
i915_gem_load_init(struct drm_i915_private * dev_priv)5401a85cb24fSFrançois Tigeot i915_gem_load_init(struct drm_i915_private *dev_priv)
5402e11a51e3SFrançois Tigeot {
5403a85cb24fSFrançois Tigeot 	int err = -ENOMEM;
5404e11a51e3SFrançois Tigeot 
5405a85cb24fSFrançois Tigeot 	dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5406a85cb24fSFrançois Tigeot 	if (!dev_priv->objects)
54074be47400SFrançois Tigeot 		goto err_out;
54084be47400SFrançois Tigeot 
5409a85cb24fSFrançois Tigeot 	dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5410a85cb24fSFrançois Tigeot 	if (!dev_priv->vmas)
54114be47400SFrançois Tigeot 		goto err_objects;
54124be47400SFrançois Tigeot 
54133f2dd94aSFrançois Tigeot 	dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
54143f2dd94aSFrançois Tigeot 	if (!dev_priv->luts)
54153f2dd94aSFrançois Tigeot 		goto err_vmas;
54163f2dd94aSFrançois Tigeot 
5417a85cb24fSFrançois Tigeot 	dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
541871f41f3eSFrançois Tigeot 					SLAB_HWCACHE_ALIGN |
541971f41f3eSFrançois Tigeot 					SLAB_RECLAIM_ACCOUNT |
5420a85cb24fSFrançois Tigeot 					SLAB_TYPESAFE_BY_RCU);
5421a85cb24fSFrançois Tigeot 	if (!dev_priv->requests)
54223f2dd94aSFrançois Tigeot 		goto err_luts;
54234be47400SFrançois Tigeot 
54244be47400SFrançois Tigeot 	dev_priv->dependencies = KMEM_CACHE(i915_dependency,
54254be47400SFrançois Tigeot 					    SLAB_HWCACHE_ALIGN |
54264be47400SFrançois Tigeot 					    SLAB_RECLAIM_ACCOUNT);
54274be47400SFrançois Tigeot 	if (!dev_priv->dependencies)
54284be47400SFrançois Tigeot 		goto err_requests;
54294be47400SFrançois Tigeot 
54303f2dd94aSFrançois Tigeot 	dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
54313f2dd94aSFrançois Tigeot 	if (!dev_priv->priorities)
54323f2dd94aSFrançois Tigeot 		goto err_dependencies;
54333f2dd94aSFrançois Tigeot 
54344be47400SFrançois Tigeot 	mutex_lock(&dev_priv->drm.struct_mutex);
54354be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev_priv->gt.timelines);
54364be47400SFrançois Tigeot 	err = i915_gem_timeline_init__global(dev_priv);
54374be47400SFrançois Tigeot 	mutex_unlock(&dev_priv->drm.struct_mutex);
54384be47400SFrançois Tigeot 	if (err)
54393f2dd94aSFrançois Tigeot 		goto err_priorities;
544087df8fc6SFrançois Tigeot 
54414be47400SFrançois Tigeot 	INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
54423f2dd94aSFrançois Tigeot 
54433f2dd94aSFrançois Tigeot 	lockinit(&dev_priv->mm.obj_lock, "i9dpmmo", 0, 0);
54443f2dd94aSFrançois Tigeot 	lockinit(&dev_priv->mm.free_lock, "i9dpmmf", 0, 0);
54454be47400SFrançois Tigeot 	init_llist_head(&dev_priv->mm.free_list);
54460b869d8aSFrançois Tigeot 	INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
54472d63acd6SFrançois Tigeot 	INIT_LIST_HEAD(&dev_priv->mm.bound_list);
54480b869d8aSFrançois Tigeot 	INIT_LIST_HEAD(&dev_priv->mm.fence_list);
54494be47400SFrançois Tigeot 	INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
54503f2dd94aSFrançois Tigeot 
5451303bf270SFrançois Tigeot 	INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
5452e11a51e3SFrançois Tigeot 			  i915_gem_retire_work_handler);
5453303bf270SFrançois Tigeot 	INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
54549edbd4a0SFrançois Tigeot 			  i915_gem_idle_work_handler);
54551487f786SFrançois Tigeot 	init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
5456a2fdbec6SFrançois Tigeot 	init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
5457e11a51e3SFrançois Tigeot 
54581e12ee3bSFrançois Tigeot 	atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
54591e12ee3bSFrançois Tigeot 
546071f41f3eSFrançois Tigeot 	lockinit(&dev_priv->fb_tracking.lock, "drmftl", 0, 0);
54614be47400SFrançois Tigeot 
54624be47400SFrançois Tigeot 	return 0;
54634be47400SFrançois Tigeot 
54643f2dd94aSFrançois Tigeot err_priorities:
54653f2dd94aSFrançois Tigeot 	kmem_cache_destroy(dev_priv->priorities);
54664be47400SFrançois Tigeot err_dependencies:
54674be47400SFrançois Tigeot 	kmem_cache_destroy(dev_priv->dependencies);
54684be47400SFrançois Tigeot err_requests:
54694be47400SFrançois Tigeot 	kmem_cache_destroy(dev_priv->requests);
54703f2dd94aSFrançois Tigeot err_luts:
54713f2dd94aSFrançois Tigeot 	kmem_cache_destroy(dev_priv->luts);
54724be47400SFrançois Tigeot err_vmas:
54734be47400SFrançois Tigeot 	kmem_cache_destroy(dev_priv->vmas);
54744be47400SFrançois Tigeot err_objects:
54754be47400SFrançois Tigeot 	kmem_cache_destroy(dev_priv->objects);
54764be47400SFrançois Tigeot err_out:
54774be47400SFrançois Tigeot 	return err;
5478e11a51e3SFrançois Tigeot }
5479e11a51e3SFrançois Tigeot 
i915_gem_load_cleanup(struct drm_i915_private * dev_priv)5480a85cb24fSFrançois Tigeot void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
5481c0e85e96SFrançois Tigeot {
5482a85cb24fSFrançois Tigeot 	i915_gem_drain_freed_objects(dev_priv);
54834be47400SFrançois Tigeot 	WARN_ON(!llist_empty(&dev_priv->mm.free_list));
5484a85cb24fSFrançois Tigeot 	WARN_ON(dev_priv->mm.object_count);
54854be47400SFrançois Tigeot 
54864be47400SFrançois Tigeot 	mutex_lock(&dev_priv->drm.struct_mutex);
54874be47400SFrançois Tigeot 	i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
54884be47400SFrançois Tigeot 	WARN_ON(!list_empty(&dev_priv->gt.timelines));
54894be47400SFrançois Tigeot 	mutex_unlock(&dev_priv->drm.struct_mutex);
54904be47400SFrançois Tigeot 
54913f2dd94aSFrançois Tigeot 	kmem_cache_destroy(dev_priv->priorities);
54924be47400SFrançois Tigeot 	kmem_cache_destroy(dev_priv->dependencies);
5493c0e85e96SFrançois Tigeot 	kmem_cache_destroy(dev_priv->requests);
54943f2dd94aSFrançois Tigeot 	kmem_cache_destroy(dev_priv->luts);
5495c0e85e96SFrançois Tigeot 	kmem_cache_destroy(dev_priv->vmas);
5496c0e85e96SFrançois Tigeot 	kmem_cache_destroy(dev_priv->objects);
549771f41f3eSFrançois Tigeot 
549871f41f3eSFrançois Tigeot 	/* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
549971f41f3eSFrançois Tigeot 	rcu_barrier();
55003f2dd94aSFrançois Tigeot 
55013f2dd94aSFrançois Tigeot #if 0
55023f2dd94aSFrançois Tigeot 	i915_gemfs_fini(dev_priv);
55033f2dd94aSFrançois Tigeot #endif
5504c0e85e96SFrançois Tigeot }
5505c0e85e96SFrançois Tigeot 
i915_gem_freeze(struct drm_i915_private * dev_priv)55061e12ee3bSFrançois Tigeot int i915_gem_freeze(struct drm_i915_private *dev_priv)
55071e12ee3bSFrançois Tigeot {
55083f2dd94aSFrançois Tigeot 	/* Discard all purgeable objects, let userspace recover those as
55093f2dd94aSFrançois Tigeot 	 * required after resuming.
55103f2dd94aSFrançois Tigeot 	 */
55111e12ee3bSFrançois Tigeot 	i915_gem_shrink_all(dev_priv);
55121e12ee3bSFrançois Tigeot 
55131e12ee3bSFrançois Tigeot 	return 0;
55141e12ee3bSFrançois Tigeot }
55151e12ee3bSFrançois Tigeot 
i915_gem_freeze_late(struct drm_i915_private * dev_priv)55161487f786SFrançois Tigeot int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
55171487f786SFrançois Tigeot {
55181487f786SFrançois Tigeot 	struct drm_i915_gem_object *obj;
55191e12ee3bSFrançois Tigeot 	struct list_head *phases[] = {
55201e12ee3bSFrançois Tigeot 		&dev_priv->mm.unbound_list,
55211e12ee3bSFrançois Tigeot 		&dev_priv->mm.bound_list,
55221e12ee3bSFrançois Tigeot 		NULL
55231e12ee3bSFrançois Tigeot 	}, **p;
55241487f786SFrançois Tigeot 
55251487f786SFrançois Tigeot 	/* Called just before we write the hibernation image.
55261487f786SFrançois Tigeot 	 *
55271487f786SFrançois Tigeot 	 * We need to update the domain tracking to reflect that the CPU
55281487f786SFrançois Tigeot 	 * will be accessing all the pages to create and restore from the
55291487f786SFrançois Tigeot 	 * hibernation, and so upon restoration those pages will be in the
55301487f786SFrançois Tigeot 	 * CPU domain.
55311487f786SFrançois Tigeot 	 *
55321487f786SFrançois Tigeot 	 * To make sure the hibernation image contains the latest state,
55331487f786SFrançois Tigeot 	 * we update that state just before writing out the image.
55341e12ee3bSFrançois Tigeot 	 *
55351e12ee3bSFrançois Tigeot 	 * To try and reduce the hibernation image, we manually shrink
55363f2dd94aSFrançois Tigeot 	 * the objects as well, see i915_gem_freeze()
55371487f786SFrançois Tigeot 	 */
55381487f786SFrançois Tigeot 
55393f2dd94aSFrançois Tigeot 	i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
55403f2dd94aSFrançois Tigeot 	i915_gem_drain_freed_objects(dev_priv);
55411487f786SFrançois Tigeot 
55423f2dd94aSFrançois Tigeot 	lockmgr(&dev_priv->mm.obj_lock, LK_EXCLUSIVE);
55431e12ee3bSFrançois Tigeot 	for (p = phases; *p; p++) {
55443f2dd94aSFrançois Tigeot 		list_for_each_entry(obj, *p, mm.link)
55453f2dd94aSFrançois Tigeot 			__start_cpu_write(obj);
55461487f786SFrançois Tigeot 	}
55473f2dd94aSFrançois Tigeot 	lockmgr(&dev_priv->mm.obj_lock, LK_RELEASE);
55481487f786SFrançois Tigeot 
55491487f786SFrançois Tigeot 	return 0;
55501487f786SFrançois Tigeot }
55511487f786SFrançois Tigeot 
i915_gem_release(struct drm_device * dev,struct drm_file * file)5552d2557f23SFrançois Tigeot void i915_gem_release(struct drm_device *dev, struct drm_file *file)
5553e11a51e3SFrançois Tigeot {
5554d2557f23SFrançois Tigeot 	struct drm_i915_file_private *file_priv = file->driver_priv;
555571f41f3eSFrançois Tigeot 	struct drm_i915_gem_request *request;
5556e11a51e3SFrançois Tigeot 
5557e11a51e3SFrançois Tigeot 	/* Clean up our request list when the client is going away, so that
5558e11a51e3SFrançois Tigeot 	 * later retire_requests won't dereference our soon-to-be-gone
5559e11a51e3SFrançois Tigeot 	 * file_priv.
5560e11a51e3SFrançois Tigeot 	 */
5561ec5b6af4SFrançois Tigeot 	lockmgr(&file_priv->mm.lock, LK_EXCLUSIVE);
5562a85cb24fSFrançois Tigeot 	list_for_each_entry(request, &file_priv->mm.request_list, client_link)
5563e11a51e3SFrançois Tigeot 		request->file_priv = NULL;
5564ec5b6af4SFrançois Tigeot 	lockmgr(&file_priv->mm.lock, LK_RELEASE);
5565e11a51e3SFrançois Tigeot }
5566e11a51e3SFrançois Tigeot 
5567a85cb24fSFrançois Tigeot #ifdef __DragonFly__
5568e9587a4eSFrançois Tigeot int
i915_gem_pager_ctor(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred,u_short * color)5569e11a51e3SFrançois Tigeot i915_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
5570e11a51e3SFrançois Tigeot     vm_ooffset_t foff, struct ucred *cred, u_short *color)
5571e11a51e3SFrançois Tigeot {
5572e11a51e3SFrançois Tigeot 	*color = 0; /* XXXKIB */
5573e11a51e3SFrançois Tigeot 	return (0);
5574e11a51e3SFrançois Tigeot }
5575e11a51e3SFrançois Tigeot 
5576e9587a4eSFrançois Tigeot void
i915_gem_pager_dtor(void * handle)5577e11a51e3SFrançois Tigeot i915_gem_pager_dtor(void *handle)
5578e11a51e3SFrançois Tigeot {
557971f41f3eSFrançois Tigeot 	struct drm_gem_object *obj = handle;
558071f41f3eSFrançois Tigeot 	struct drm_device *dev = obj->dev;
5581e11a51e3SFrançois Tigeot 
5582e11a51e3SFrançois Tigeot 	drm_gem_free_mmap_offset(obj);
5583a85cb24fSFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
5584e11a51e3SFrançois Tigeot 	i915_gem_release_mmap(to_intel_bo(obj));
5585e11a51e3SFrançois Tigeot 	drm_gem_object_unreference(obj);
5586a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
5587e11a51e3SFrançois Tigeot }
5588a85cb24fSFrançois Tigeot #endif
5589e11a51e3SFrançois Tigeot 
i915_gem_open(struct drm_i915_private * i915,struct drm_file * file)55903f2dd94aSFrançois Tigeot int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
55919edbd4a0SFrançois Tigeot {
55929edbd4a0SFrançois Tigeot 	struct drm_i915_file_private *file_priv;
5593ba55f2f5SFrançois Tigeot 	int ret;
55949edbd4a0SFrançois Tigeot 
55954be47400SFrançois Tigeot 	DRM_DEBUG("\n");
55969edbd4a0SFrançois Tigeot 
55979edbd4a0SFrançois Tigeot 	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
55989edbd4a0SFrançois Tigeot 	if (!file_priv)
55999edbd4a0SFrançois Tigeot 		return -ENOMEM;
56009edbd4a0SFrançois Tigeot 
56019edbd4a0SFrançois Tigeot 	file->driver_priv = file_priv;
56023f2dd94aSFrançois Tigeot 	file_priv->dev_priv = i915;
5603ba55f2f5SFrançois Tigeot 	file_priv->file = file;
56049edbd4a0SFrançois Tigeot 
56059a49c39cSFrançois Tigeot 	lockinit(&file_priv->mm.lock, "i915_priv", 0, 0);
56069edbd4a0SFrançois Tigeot 	INIT_LIST_HEAD(&file_priv->mm.request_list);
56079edbd4a0SFrançois Tigeot 
560871f41f3eSFrançois Tigeot 	file_priv->bsd_engine = -1;
5609c0e85e96SFrançois Tigeot 
56103f2dd94aSFrançois Tigeot 	ret = i915_gem_context_open(i915, file);
5611ba55f2f5SFrançois Tigeot 	if (ret)
5612ba55f2f5SFrançois Tigeot 		kfree(file_priv);
56139edbd4a0SFrançois Tigeot 
5614ba55f2f5SFrançois Tigeot 	return ret;
5615e11a51e3SFrançois Tigeot }
56169edbd4a0SFrançois Tigeot 
56172c9916cdSFrançois Tigeot /**
56182c9916cdSFrançois Tigeot  * i915_gem_track_fb - update frontbuffer tracking
5619352ff8bdSFrançois Tigeot  * @old: current GEM buffer for the frontbuffer slots
5620352ff8bdSFrançois Tigeot  * @new: new GEM buffer for the frontbuffer slots
5621352ff8bdSFrançois Tigeot  * @frontbuffer_bits: bitmask of frontbuffer slots
56222c9916cdSFrançois Tigeot  *
56232c9916cdSFrançois Tigeot  * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
56242c9916cdSFrançois Tigeot  * from @old and setting them in @new. Both @old and @new can be NULL.
56252c9916cdSFrançois Tigeot  */
i915_gem_track_fb(struct drm_i915_gem_object * old,struct drm_i915_gem_object * new,unsigned frontbuffer_bits)562624edb884SFrançois Tigeot void i915_gem_track_fb(struct drm_i915_gem_object *old,
562724edb884SFrançois Tigeot 		       struct drm_i915_gem_object *new,
562824edb884SFrançois Tigeot 		       unsigned frontbuffer_bits)
562924edb884SFrançois Tigeot {
563071f41f3eSFrançois Tigeot 	/* Control of individual bits within the mask are guarded by
563171f41f3eSFrançois Tigeot 	 * the owning plane->mutex, i.e. we can never see concurrent
563271f41f3eSFrançois Tigeot 	 * manipulation of individual bits. But since the bitfield as a whole
563371f41f3eSFrançois Tigeot 	 * is updated using RMW, we need to use atomics in order to update
563471f41f3eSFrançois Tigeot 	 * the bits.
563571f41f3eSFrançois Tigeot 	 */
563671f41f3eSFrançois Tigeot 	BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
563771f41f3eSFrançois Tigeot 		     sizeof(atomic_t) * BITS_PER_BYTE);
563871f41f3eSFrançois Tigeot 
563924edb884SFrançois Tigeot 	if (old) {
564071f41f3eSFrançois Tigeot 		WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
564171f41f3eSFrançois Tigeot 		atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
564224edb884SFrançois Tigeot 	}
564324edb884SFrançois Tigeot 
564424edb884SFrançois Tigeot 	if (new) {
564571f41f3eSFrançois Tigeot 		WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
564671f41f3eSFrançois Tigeot 		atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
564724edb884SFrançois Tigeot 	}
564824edb884SFrançois Tigeot }
564924edb884SFrançois Tigeot 
5650a05eeebfSFrançois Tigeot /* Allocate a new GEM object and fill it with the supplied data */
5651a05eeebfSFrançois Tigeot struct drm_i915_gem_object *
i915_gem_object_create_from_data(struct drm_i915_private * dev_priv,const void * data,size_t size)5652a85cb24fSFrançois Tigeot i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
5653a05eeebfSFrançois Tigeot 			         const void *data, size_t size)
5654a05eeebfSFrançois Tigeot {
5655a05eeebfSFrançois Tigeot 	struct drm_i915_gem_object *obj;
5656a85cb24fSFrançois Tigeot 	struct vm_object *file;
5657a85cb24fSFrançois Tigeot 	size_t offset;
5658a85cb24fSFrançois Tigeot 	int err;
5659a05eeebfSFrançois Tigeot 
5660a85cb24fSFrançois Tigeot 	obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
56611487f786SFrançois Tigeot 	if (IS_ERR(obj))
5662a05eeebfSFrançois Tigeot 		return obj;
5663a05eeebfSFrançois Tigeot 
5664a85cb24fSFrançois Tigeot 	GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
5665a85cb24fSFrançois Tigeot 
5666a85cb24fSFrançois Tigeot 	file = obj->base.filp;
5667a85cb24fSFrançois Tigeot 	offset = 0;
5668a85cb24fSFrançois Tigeot 	do {
5669a85cb24fSFrançois Tigeot 		unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5670a85cb24fSFrançois Tigeot 		struct page *page;
5671a85cb24fSFrançois Tigeot 		void *pgdata, *vaddr;
5672a85cb24fSFrançois Tigeot 
5673a85cb24fSFrançois Tigeot 		err = pagecache_write_begin(file, NULL,
5674a85cb24fSFrançois Tigeot 					    offset, len, 0,
5675a85cb24fSFrançois Tigeot 					    &page, &pgdata);
5676a85cb24fSFrançois Tigeot 		if (err < 0)
5677a05eeebfSFrançois Tigeot 			goto fail;
5678a05eeebfSFrançois Tigeot 
5679a85cb24fSFrançois Tigeot 		vaddr = kmap(page);
5680a85cb24fSFrançois Tigeot 		memcpy(vaddr, data, len);
5681a85cb24fSFrançois Tigeot 		kunmap(page);
5682a05eeebfSFrançois Tigeot 
5683a85cb24fSFrançois Tigeot #ifndef __DragonFly__
5684a85cb24fSFrançois Tigeot 		err = pagecache_write_end(file, file->f_mapping,
56853f2dd94aSFrançois Tigeot #else
56863f2dd94aSFrançois Tigeot 		err = pagecache_write_end(file, NULL,
56873f2dd94aSFrançois Tigeot #endif
5688a85cb24fSFrançois Tigeot 					  offset, len, len,
5689a85cb24fSFrançois Tigeot 					  page, pgdata);
5690a85cb24fSFrançois Tigeot 		if (err < 0)
5691a05eeebfSFrançois Tigeot 			goto fail;
5692a85cb24fSFrançois Tigeot 
5693a85cb24fSFrançois Tigeot 		size -= len;
5694a85cb24fSFrançois Tigeot 		data += len;
5695a85cb24fSFrançois Tigeot 		offset += len;
5696a85cb24fSFrançois Tigeot 	} while (size);
5697a05eeebfSFrançois Tigeot 
5698a05eeebfSFrançois Tigeot 	return obj;
5699a05eeebfSFrançois Tigeot 
5700a05eeebfSFrançois Tigeot fail:
570187df8fc6SFrançois Tigeot 	i915_gem_object_put(obj);
5702a85cb24fSFrançois Tigeot 	return ERR_PTR(err);
5703a05eeebfSFrançois Tigeot }
57044be47400SFrançois Tigeot 
57054be47400SFrançois Tigeot struct scatterlist *
i915_gem_object_get_sg(struct drm_i915_gem_object * obj,unsigned int n,unsigned int * offset)57064be47400SFrançois Tigeot i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
57074be47400SFrançois Tigeot 		       unsigned int n,
57084be47400SFrançois Tigeot 		       unsigned int *offset)
57094be47400SFrançois Tigeot {
57104be47400SFrançois Tigeot 	struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
57114be47400SFrançois Tigeot 	struct scatterlist *sg;
57124be47400SFrançois Tigeot 	unsigned int idx, count;
57134be47400SFrançois Tigeot 
57144be47400SFrançois Tigeot 	might_sleep();
57154be47400SFrançois Tigeot 	GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
57164be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
57174be47400SFrançois Tigeot 
57184be47400SFrançois Tigeot 	/* As we iterate forward through the sg, we record each entry in a
57194be47400SFrançois Tigeot 	 * radixtree for quick repeated (backwards) lookups. If we have seen
57204be47400SFrançois Tigeot 	 * this index previously, we will have an entry for it.
57214be47400SFrançois Tigeot 	 *
57224be47400SFrançois Tigeot 	 * Initial lookup is O(N), but this is amortized to O(1) for
57234be47400SFrançois Tigeot 	 * sequential page access (where each new request is consecutive
57244be47400SFrançois Tigeot 	 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
57254be47400SFrançois Tigeot 	 * i.e. O(1) with a large constant!
57264be47400SFrançois Tigeot 	 */
57274be47400SFrançois Tigeot 	if (n < READ_ONCE(iter->sg_idx))
57284be47400SFrançois Tigeot 		goto lookup;
57294be47400SFrançois Tigeot 
57304be47400SFrançois Tigeot 	mutex_lock(&iter->lock);
57314be47400SFrançois Tigeot 
57324be47400SFrançois Tigeot 	/* We prefer to reuse the last sg so that repeated lookup of this
57334be47400SFrançois Tigeot 	 * (or the subsequent) sg are fast - comparing against the last
57344be47400SFrançois Tigeot 	 * sg is faster than going through the radixtree.
57354be47400SFrançois Tigeot 	 */
57364be47400SFrançois Tigeot 
57374be47400SFrançois Tigeot 	sg = iter->sg_pos;
57384be47400SFrançois Tigeot 	idx = iter->sg_idx;
57394be47400SFrançois Tigeot 	count = __sg_page_count(sg);
57404be47400SFrançois Tigeot 
57414be47400SFrançois Tigeot 	while (idx + count <= n) {
57424be47400SFrançois Tigeot 		unsigned long exception, i;
57434be47400SFrançois Tigeot 		int ret;
57444be47400SFrançois Tigeot 
57454be47400SFrançois Tigeot 		/* If we cannot allocate and insert this entry, or the
57464be47400SFrançois Tigeot 		 * individual pages from this range, cancel updating the
57474be47400SFrançois Tigeot 		 * sg_idx so that on this lookup we are forced to linearly
57484be47400SFrançois Tigeot 		 * scan onwards, but on future lookups we will try the
57494be47400SFrançois Tigeot 		 * insertion again (in which case we need to be careful of
57504be47400SFrançois Tigeot 		 * the error return reporting that we have already inserted
57514be47400SFrançois Tigeot 		 * this index).
57524be47400SFrançois Tigeot 		 */
57534be47400SFrançois Tigeot 		ret = radix_tree_insert(&iter->radix, idx, sg);
57544be47400SFrançois Tigeot 		if (ret && ret != -EEXIST)
57554be47400SFrançois Tigeot 			goto scan;
57564be47400SFrançois Tigeot 
57574be47400SFrançois Tigeot 		exception =
57584be47400SFrançois Tigeot 			RADIX_TREE_EXCEPTIONAL_ENTRY |
57594be47400SFrançois Tigeot 			idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
57604be47400SFrançois Tigeot 		for (i = 1; i < count; i++) {
57614be47400SFrançois Tigeot 			ret = radix_tree_insert(&iter->radix, idx + i,
57624be47400SFrançois Tigeot 						(void *)exception);
57634be47400SFrançois Tigeot 			if (ret && ret != -EEXIST)
57644be47400SFrançois Tigeot 				goto scan;
57654be47400SFrançois Tigeot 		}
57664be47400SFrançois Tigeot 
57674be47400SFrançois Tigeot 		idx += count;
57684be47400SFrançois Tigeot 		sg = ____sg_next(sg);
57694be47400SFrançois Tigeot 		count = __sg_page_count(sg);
57704be47400SFrançois Tigeot 	}
57714be47400SFrançois Tigeot 
57724be47400SFrançois Tigeot scan:
57734be47400SFrançois Tigeot 	iter->sg_pos = sg;
57744be47400SFrançois Tigeot 	iter->sg_idx = idx;
57754be47400SFrançois Tigeot 
57764be47400SFrançois Tigeot 	mutex_unlock(&iter->lock);
57774be47400SFrançois Tigeot 
57784be47400SFrançois Tigeot 	if (unlikely(n < idx)) /* insertion completed by another thread */
57794be47400SFrançois Tigeot 		goto lookup;
57804be47400SFrançois Tigeot 
57814be47400SFrançois Tigeot 	/* In case we failed to insert the entry into the radixtree, we need
57824be47400SFrançois Tigeot 	 * to look beyond the current sg.
57834be47400SFrançois Tigeot 	 */
57844be47400SFrançois Tigeot 	while (idx + count <= n) {
57854be47400SFrançois Tigeot 		idx += count;
57864be47400SFrançois Tigeot 		sg = ____sg_next(sg);
57874be47400SFrançois Tigeot 		count = __sg_page_count(sg);
57884be47400SFrançois Tigeot 	}
57894be47400SFrançois Tigeot 
57904be47400SFrançois Tigeot 	*offset = n - idx;
57914be47400SFrançois Tigeot 	return sg;
57924be47400SFrançois Tigeot 
57934be47400SFrançois Tigeot lookup:
57944be47400SFrançois Tigeot 	rcu_read_lock();
57954be47400SFrançois Tigeot 
57964be47400SFrançois Tigeot 	sg = radix_tree_lookup(&iter->radix, n);
57974be47400SFrançois Tigeot 	GEM_BUG_ON(!sg);
57984be47400SFrançois Tigeot 
57994be47400SFrançois Tigeot 	/* If this index is in the middle of multi-page sg entry,
58004be47400SFrançois Tigeot 	 * the radixtree will contain an exceptional entry that points
58014be47400SFrançois Tigeot 	 * to the start of that range. We will return the pointer to
58024be47400SFrançois Tigeot 	 * the base page and the offset of this page within the
58034be47400SFrançois Tigeot 	 * sg entry's range.
58044be47400SFrançois Tigeot 	 */
58054be47400SFrançois Tigeot 	*offset = 0;
58064be47400SFrançois Tigeot 	if (unlikely(radix_tree_exception(sg))) {
58074be47400SFrançois Tigeot 		unsigned long base =
58084be47400SFrançois Tigeot 			(unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
58094be47400SFrançois Tigeot 
58104be47400SFrançois Tigeot 		sg = radix_tree_lookup(&iter->radix, base);
58114be47400SFrançois Tigeot 		GEM_BUG_ON(!sg);
58124be47400SFrançois Tigeot 
58134be47400SFrançois Tigeot 		*offset = n - base;
58144be47400SFrançois Tigeot 	}
58154be47400SFrançois Tigeot 
58164be47400SFrançois Tigeot 	rcu_read_unlock();
58174be47400SFrançois Tigeot 
58184be47400SFrançois Tigeot 	return sg;
58194be47400SFrançois Tigeot }
58204be47400SFrançois Tigeot 
58214be47400SFrançois Tigeot struct page *
i915_gem_object_get_page(struct drm_i915_gem_object * obj,unsigned int n)58224be47400SFrançois Tigeot i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
58234be47400SFrançois Tigeot {
58244be47400SFrançois Tigeot 	struct scatterlist *sg;
58254be47400SFrançois Tigeot 	unsigned int offset;
58264be47400SFrançois Tigeot 
58274be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
58284be47400SFrançois Tigeot 
58294be47400SFrançois Tigeot 	sg = i915_gem_object_get_sg(obj, n, &offset);
58304be47400SFrançois Tigeot 	return nth_page(sg_page(sg), offset);
58314be47400SFrançois Tigeot }
58324be47400SFrançois Tigeot 
58334be47400SFrançois Tigeot /* Like i915_gem_object_get_page(), but mark the returned page dirty */
58344be47400SFrançois Tigeot struct page *
i915_gem_object_get_dirty_page(struct drm_i915_gem_object * obj,unsigned int n)58354be47400SFrançois Tigeot i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
58364be47400SFrançois Tigeot 			       unsigned int n)
58374be47400SFrançois Tigeot {
58384be47400SFrançois Tigeot 	struct page *page;
58394be47400SFrançois Tigeot 
58404be47400SFrançois Tigeot 	page = i915_gem_object_get_page(obj, n);
58414be47400SFrançois Tigeot 	if (!obj->mm.dirty)
58424be47400SFrançois Tigeot 		set_page_dirty(page);
58434be47400SFrançois Tigeot 
58444be47400SFrançois Tigeot 	return page;
58454be47400SFrançois Tigeot }
58464be47400SFrançois Tigeot 
58474be47400SFrançois Tigeot dma_addr_t
i915_gem_object_get_dma_address(struct drm_i915_gem_object * obj,unsigned long n)58484be47400SFrançois Tigeot i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
58494be47400SFrançois Tigeot 				unsigned long n)
58504be47400SFrançois Tigeot {
58514be47400SFrançois Tigeot 	struct scatterlist *sg;
58524be47400SFrançois Tigeot 	unsigned int offset;
58534be47400SFrançois Tigeot 
58544be47400SFrançois Tigeot 	sg = i915_gem_object_get_sg(obj, n, &offset);
58554be47400SFrançois Tigeot 	return sg_dma_address(sg) + (offset << PAGE_SHIFT);
58564be47400SFrançois Tigeot }
5857a85cb24fSFrançois Tigeot 
i915_gem_object_attach_phys(struct drm_i915_gem_object * obj,int align)58583f2dd94aSFrançois Tigeot int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
58593f2dd94aSFrançois Tigeot {
58603f2dd94aSFrançois Tigeot 	struct sg_table *pages;
58613f2dd94aSFrançois Tigeot 	int err;
58623f2dd94aSFrançois Tigeot 
58633f2dd94aSFrançois Tigeot 	if (align > obj->base.size)
58643f2dd94aSFrançois Tigeot 		return -EINVAL;
58653f2dd94aSFrançois Tigeot 
58663f2dd94aSFrançois Tigeot 	if (obj->ops == &i915_gem_phys_ops)
58673f2dd94aSFrançois Tigeot 		return 0;
58683f2dd94aSFrançois Tigeot 
58693f2dd94aSFrançois Tigeot 	if (obj->ops != &i915_gem_object_ops)
58703f2dd94aSFrançois Tigeot 		return -EINVAL;
58713f2dd94aSFrançois Tigeot 
58723f2dd94aSFrançois Tigeot 	err = i915_gem_object_unbind(obj);
58733f2dd94aSFrançois Tigeot 	if (err)
58743f2dd94aSFrançois Tigeot 		return err;
58753f2dd94aSFrançois Tigeot 
58763f2dd94aSFrançois Tigeot 	mutex_lock(&obj->mm.lock);
58773f2dd94aSFrançois Tigeot 
58783f2dd94aSFrançois Tigeot 	if (obj->mm.madv != I915_MADV_WILLNEED) {
58793f2dd94aSFrançois Tigeot 		err = -EFAULT;
58803f2dd94aSFrançois Tigeot 		goto err_unlock;
58813f2dd94aSFrançois Tigeot 	}
58823f2dd94aSFrançois Tigeot 
58833f2dd94aSFrançois Tigeot 	if (obj->mm.quirked) {
58843f2dd94aSFrançois Tigeot 		err = -EFAULT;
58853f2dd94aSFrançois Tigeot 		goto err_unlock;
58863f2dd94aSFrançois Tigeot 	}
58873f2dd94aSFrançois Tigeot 
58883f2dd94aSFrançois Tigeot 	if (obj->mm.mapping) {
58893f2dd94aSFrançois Tigeot 		err = -EBUSY;
58903f2dd94aSFrançois Tigeot 		goto err_unlock;
58913f2dd94aSFrançois Tigeot 	}
58923f2dd94aSFrançois Tigeot 
58933f2dd94aSFrançois Tigeot 	pages = fetch_and_zero(&obj->mm.pages);
58943f2dd94aSFrançois Tigeot 	if (pages) {
58953f2dd94aSFrançois Tigeot 		struct drm_i915_private *i915 = to_i915(obj->base.dev);
58963f2dd94aSFrançois Tigeot 
58973f2dd94aSFrançois Tigeot 		__i915_gem_object_reset_page_iter(obj);
58983f2dd94aSFrançois Tigeot 
58993f2dd94aSFrançois Tigeot 		lockmgr(&i915->mm.obj_lock, LK_EXCLUSIVE);
59003f2dd94aSFrançois Tigeot 		list_del(&obj->mm.link);
59013f2dd94aSFrançois Tigeot 		lockmgr(&i915->mm.obj_lock, LK_RELEASE);
59023f2dd94aSFrançois Tigeot 	}
59033f2dd94aSFrançois Tigeot 
59043f2dd94aSFrançois Tigeot 	obj->ops = &i915_gem_phys_ops;
59053f2dd94aSFrançois Tigeot 
59063f2dd94aSFrançois Tigeot 	err = ____i915_gem_object_get_pages(obj);
59073f2dd94aSFrançois Tigeot 	if (err)
59083f2dd94aSFrançois Tigeot 		goto err_xfer;
59093f2dd94aSFrançois Tigeot 
59103f2dd94aSFrançois Tigeot 	/* Perma-pin (until release) the physical set of pages */
59113f2dd94aSFrançois Tigeot 	__i915_gem_object_pin_pages(obj);
59123f2dd94aSFrançois Tigeot 
59133f2dd94aSFrançois Tigeot 	if (!IS_ERR_OR_NULL(pages))
59143f2dd94aSFrançois Tigeot 		i915_gem_object_ops.put_pages(obj, pages);
59153f2dd94aSFrançois Tigeot 	mutex_unlock(&obj->mm.lock);
59163f2dd94aSFrançois Tigeot 	return 0;
59173f2dd94aSFrançois Tigeot 
59183f2dd94aSFrançois Tigeot err_xfer:
59193f2dd94aSFrançois Tigeot 	obj->ops = &i915_gem_object_ops;
59203f2dd94aSFrançois Tigeot 	obj->mm.pages = pages;
59213f2dd94aSFrançois Tigeot err_unlock:
59223f2dd94aSFrançois Tigeot 	mutex_unlock(&obj->mm.lock);
59233f2dd94aSFrançois Tigeot 	return err;
59243f2dd94aSFrançois Tigeot }
59253f2dd94aSFrançois Tigeot 
5926a85cb24fSFrançois Tigeot #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5927a85cb24fSFrançois Tigeot #include "selftests/scatterlist.c"
5928a85cb24fSFrançois Tigeot #include "selftests/mock_gem_device.c"
5929a85cb24fSFrançois Tigeot #include "selftests/huge_gem_object.c"
59303f2dd94aSFrançois Tigeot #include "selftests/huge_pages.c"
5931a85cb24fSFrançois Tigeot #include "selftests/i915_gem_object.c"
5932a85cb24fSFrançois Tigeot #include "selftests/i915_gem_coherency.c"
5933a85cb24fSFrançois Tigeot #endif
5934