blob: dcdcc09240b9072a673f55616d5588f414d22117 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Matthew Auld465c4032017-10-06 23:18:14 +010038#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000039#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000040#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010041#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070042#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000044#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070045#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080046#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020047#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070048
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010049static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010050
Chris Wilson2c225692013-08-09 12:26:45 +010051static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
52{
Chris Wilsone27ab732017-06-15 13:38:49 +010053 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053054 return false;
55
Chris Wilsonb8f55be2017-08-11 12:11:16 +010056 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010057 return true;
58
Chris Wilsonbd3d2252017-10-13 21:26:14 +010059 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010060}
61
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053062static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010063insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053064 struct drm_mm_node *node, u32 size)
65{
66 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000067 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
68 size, 0, I915_COLOR_UNEVICTABLE,
69 0, ggtt->mappable_end,
70 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053071}
72
73static void
74remove_mappable_node(struct drm_mm_node *node)
75{
76 drm_mm_remove_node(node);
77}
78
Chris Wilson73aa8082010-09-30 11:46:12 +010079/* some bookkeeping */
80static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010081 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010082{
Daniel Vetterc20e8352013-07-24 22:40:23 +020083 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010084 dev_priv->mm.object_count++;
85 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020086 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010087}
88
89static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010090 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010091{
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093 dev_priv->mm.object_count--;
94 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096}
97
Chris Wilson21dd3732011-01-26 15:55:56 +000098static int
Daniel Vetter33196de2012-11-14 17:14:05 +010099i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101 int ret;
102
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100103 might_sleep();
104
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200105 /*
106 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
107 * userspace. If it takes that long something really bad is going on and
108 * we should simply try to bail out and fail as gracefully as possible.
109 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100110 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000111 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100112 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 if (ret == 0) {
114 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
115 return -EIO;
116 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100118 } else {
119 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200120 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121}
122
Chris Wilson54cf91d2010-11-25 18:00:26 +0000123int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100124{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100125 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100126 int ret;
127
Daniel Vetter33196de2012-11-14 17:14:05 +0100128 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 if (ret)
130 return ret;
131
132 ret = mutex_lock_interruptible(&dev->struct_mutex);
133 if (ret)
134 return ret;
135
Chris Wilson76c1dec2010-09-25 11:22:51 +0100136 return 0;
137}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100138
Eric Anholt673a3942008-07-30 12:06:12 -0700139int
Eric Anholt5a125c32008-10-22 21:40:13 -0700140i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000141 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700142{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300143 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200144 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300145 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100146 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800147 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700148
Weinan Liff8f7972017-05-31 10:35:52 +0800149 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100150 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000151 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100152 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100153 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000154 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100155 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100156 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100157 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300159 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000161
Eric Anholt5a125c32008-10-22 21:40:13 -0700162 return 0;
163}
164
Matthew Auldb91b09e2017-10-06 23:18:17 +0100165static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100166{
Al Viro93c76a32015-12-04 23:45:44 -0500167 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000168 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800169 struct sg_table *st;
170 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000171 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800172 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100173 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100174
Chris Wilson6a2c4232014-11-04 04:51:40 -0800175 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100176 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100177
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 /* Always aligning to the object size, allows a single allocation
179 * to handle all possible callers, and given typical object sizes,
180 * the alignment of the buddy allocation will naturally match.
181 */
182 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300183 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000184 roundup_pow_of_two(obj->base.size));
185 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100186 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000187
188 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800189 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
190 struct page *page;
191 char *src;
192
193 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000194 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100195 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000196 goto err_phys;
197 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800198
199 src = kmap_atomic(page);
200 memcpy(vaddr, src, PAGE_SIZE);
201 drm_clflush_virt_range(vaddr, PAGE_SIZE);
202 kunmap_atomic(src);
203
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300204 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800205 vaddr += PAGE_SIZE;
206 }
207
Chris Wilsonc0336662016-05-06 15:40:21 +0100208 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800209
210 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000211 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100212 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000213 goto err_phys;
214 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800215
216 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
217 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100218 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000219 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800220 }
221
222 sg = st->sgl;
223 sg->offset = 0;
224 sg->length = obj->base.size;
225
Chris Wilsondbb43512016-12-07 13:34:11 +0000226 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800227 sg_dma_len(sg) = obj->base.size;
228
Chris Wilsondbb43512016-12-07 13:34:11 +0000229 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100230
Matthew Aulda5c081662017-10-06 23:18:18 +0100231 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100232
233 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000234
235err_phys:
236 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100237
238 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239}
240
Chris Wilsone27ab732017-06-15 13:38:49 +0100241static void __start_cpu_write(struct drm_i915_gem_object *obj)
242{
Christian Königc0a51fd2018-02-16 13:43:38 +0100243 obj->read_domains = I915_GEM_DOMAIN_CPU;
244 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100245 if (cpu_write_needs_clflush(obj))
246 obj->cache_dirty = true;
247}
248
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000250__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000251 struct sg_table *pages,
252 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800253{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100254 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800255
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100256 if (obj->mm.madv == I915_MADV_DONTNEED)
257 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800258
Chris Wilsone5facdf2016-12-23 14:57:57 +0000259 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100260 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100261 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000262 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100263
Chris Wilsone27ab732017-06-15 13:38:49 +0100264 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100265}
266
267static void
268i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
269 struct sg_table *pages)
270{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000271 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100272
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100273 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500274 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100276 int i;
277
278 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800279 struct page *page;
280 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100281
Chris Wilson6a2c4232014-11-04 04:51:40 -0800282 page = shmem_read_mapping_page(mapping, i);
283 if (IS_ERR(page))
284 continue;
285
286 dst = kmap_atomic(page);
287 drm_clflush_virt_range(vaddr, PAGE_SIZE);
288 memcpy(dst, vaddr, PAGE_SIZE);
289 kunmap_atomic(dst);
290
291 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100292 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100293 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100295 vaddr += PAGE_SIZE;
296 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100297 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100298 }
299
Chris Wilson03ac84f2016-10-28 13:58:36 +0100300 sg_free_table(pages);
301 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000302
303 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800304}
305
306static void
307i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
308{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100309 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800310}
311
312static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
313 .get_pages = i915_gem_object_get_pages_phys,
314 .put_pages = i915_gem_object_put_pages_phys,
315 .release = i915_gem_object_release_phys,
316};
317
Chris Wilson581ab1f2017-02-15 16:39:00 +0000318static const struct drm_i915_gem_object_ops i915_gem_object_ops;
319
Chris Wilson35a96112016-08-14 18:44:40 +0100320int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100321{
322 struct i915_vma *vma;
323 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100324 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100325
Chris Wilson02bef8f2016-08-14 18:44:41 +0100326 lockdep_assert_held(&obj->base.dev->struct_mutex);
327
328 /* Closed vma are removed from the obj->vma_list - but they may
329 * still have an active binding on the object. To remove those we
330 * must wait for all rendering to complete to the object (as unbinding
331 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100332 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000333 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100334 if (ret)
335 return ret;
336
Chris Wilsonaa653a62016-08-04 07:52:27 +0100337 while ((vma = list_first_entry_or_null(&obj->vma_list,
338 struct i915_vma,
339 obj_link))) {
340 list_move_tail(&vma->obj_link, &still_in_list);
341 ret = i915_vma_unbind(vma);
342 if (ret)
343 break;
344 }
345 list_splice(&still_in_list, &obj->vma_list);
346
347 return ret;
348}
349
Chris Wilsone95433c2016-10-28 13:58:27 +0100350static long
351i915_gem_object_wait_fence(struct dma_fence *fence,
352 unsigned int flags,
353 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100354 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100355{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000356 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100357
358 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
359
360 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
361 return timeout;
362
363 if (!dma_fence_is_i915(fence))
364 return dma_fence_wait_timeout(fence,
365 flags & I915_WAIT_INTERRUPTIBLE,
366 timeout);
367
368 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000369 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100370 goto out;
371
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000372 /*
373 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100374 * this is undesirable and limits the throughput of the system, as
375 * many clients cannot continue processing user input/output whilst
376 * blocked. RPS autotuning may take tens of milliseconds to respond
377 * to the GPU load and thus incurs additional latency for the client.
378 * We can circumvent that by promoting the GPU frequency to maximum
379 * before we wait. This makes the GPU throttle up much more quickly
380 * (good for benchmarks and user experience, e.g. window animations),
381 * but at a cost of spending more power processing the workload
382 * (bad for battery). Not all clients even want their results
383 * immediately and for them we should just let the GPU select its own
384 * frequency to maximise efficiency. To prevent a single client from
385 * forcing the clocks too high for the whole system, we only allow
386 * each client to waitboost once in a busy period.
387 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000388 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100389 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100390 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100391 }
392
Chris Wilsone61e0f52018-02-21 09:56:36 +0000393 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100394
395out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000396 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
397 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100398
Chris Wilsone95433c2016-10-28 13:58:27 +0100399 return timeout;
400}
401
402static long
403i915_gem_object_wait_reservation(struct reservation_object *resv,
404 unsigned int flags,
405 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100406 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100407{
Chris Wilsone54ca972017-02-17 15:13:04 +0000408 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100409 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000410 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100411
412 if (flags & I915_WAIT_ALL) {
413 struct dma_fence **shared;
414 unsigned int count, i;
415 int ret;
416
417 ret = reservation_object_get_fences_rcu(resv,
418 &excl, &count, &shared);
419 if (ret)
420 return ret;
421
422 for (i = 0; i < count; i++) {
423 timeout = i915_gem_object_wait_fence(shared[i],
424 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100425 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000426 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100427 break;
428
429 dma_fence_put(shared[i]);
430 }
431
432 for (; i < count; i++)
433 dma_fence_put(shared[i]);
434 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000435
436 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100437 } else {
438 excl = reservation_object_get_excl_rcu(resv);
439 }
440
Chris Wilsone54ca972017-02-17 15:13:04 +0000441 if (excl && timeout >= 0) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100442 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
443 rps_client);
Chris Wilsone54ca972017-02-17 15:13:04 +0000444 prune_fences = timeout >= 0;
445 }
Chris Wilsone95433c2016-10-28 13:58:27 +0100446
447 dma_fence_put(excl);
448
Chris Wilson03d1cac2017-03-08 13:26:28 +0000449 /* Oportunistically prune the fences iff we know they have *all* been
450 * signaled and that the reservation object has not been changed (i.e.
451 * no new fences have been added).
452 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000453 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000454 if (reservation_object_trylock(resv)) {
455 if (!__read_seqcount_retry(&resv->seq, seq))
456 reservation_object_add_excl_fence(resv, NULL);
457 reservation_object_unlock(resv);
458 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000459 }
460
Chris Wilsone95433c2016-10-28 13:58:27 +0100461 return timeout;
462}
463
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000464static void __fence_set_priority(struct dma_fence *fence, int prio)
465{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000466 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000467 struct intel_engine_cs *engine;
468
Chris Wilsonc218ee02018-01-06 10:56:18 +0000469 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000470 return;
471
472 rq = to_request(fence);
473 engine = rq->engine;
474 if (!engine->schedule)
475 return;
476
477 engine->schedule(rq, prio);
478}
479
480static void fence_set_priority(struct dma_fence *fence, int prio)
481{
482 /* Recurse once into a fence-array */
483 if (dma_fence_is_array(fence)) {
484 struct dma_fence_array *array = to_dma_fence_array(fence);
485 int i;
486
487 for (i = 0; i < array->num_fences; i++)
488 __fence_set_priority(array->fences[i], prio);
489 } else {
490 __fence_set_priority(fence, prio);
491 }
492}
493
494int
495i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
496 unsigned int flags,
497 int prio)
498{
499 struct dma_fence *excl;
500
501 if (flags & I915_WAIT_ALL) {
502 struct dma_fence **shared;
503 unsigned int count, i;
504 int ret;
505
506 ret = reservation_object_get_fences_rcu(obj->resv,
507 &excl, &count, &shared);
508 if (ret)
509 return ret;
510
511 for (i = 0; i < count; i++) {
512 fence_set_priority(shared[i], prio);
513 dma_fence_put(shared[i]);
514 }
515
516 kfree(shared);
517 } else {
518 excl = reservation_object_get_excl_rcu(obj->resv);
519 }
520
521 if (excl) {
522 fence_set_priority(excl, prio);
523 dma_fence_put(excl);
524 }
525 return 0;
526}
527
Chris Wilson00e60f22016-08-04 16:32:40 +0100528/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100529 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100530 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100531 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
532 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000533 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100534 */
535int
Chris Wilsone95433c2016-10-28 13:58:27 +0100536i915_gem_object_wait(struct drm_i915_gem_object *obj,
537 unsigned int flags,
538 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100539 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100540{
Chris Wilsone95433c2016-10-28 13:58:27 +0100541 might_sleep();
542#if IS_ENABLED(CONFIG_LOCKDEP)
543 GEM_BUG_ON(debug_locks &&
544 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
545 !!(flags & I915_WAIT_LOCKED));
546#endif
547 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100548
Chris Wilsond07f0e52016-10-28 13:58:44 +0100549 timeout = i915_gem_object_wait_reservation(obj->resv,
550 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100551 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100552 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100553}
554
555static struct intel_rps_client *to_rps_client(struct drm_file *file)
556{
557 struct drm_i915_file_private *fpriv = file->driver_priv;
558
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100559 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100560}
561
Chris Wilson00731152014-05-21 12:42:56 +0100562static int
563i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
564 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100565 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100566{
Chris Wilson00731152014-05-21 12:42:56 +0100567 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300568 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800569
570 /* We manually control the domain here and pretend that it
571 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
572 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700573 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000574 if (copy_from_user(vaddr, user_data, args->size))
575 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100576
Chris Wilson6a2c4232014-11-04 04:51:40 -0800577 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000578 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200579
Chris Wilsond59b21e2017-02-22 11:40:49 +0000580 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000581 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100582}
583
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000584void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000585{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100586 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000587}
588
589void i915_gem_object_free(struct drm_i915_gem_object *obj)
590{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100591 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100592 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000593}
594
Dave Airlieff72145b2011-02-07 12:16:14 +1000595static int
596i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000597 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000598 uint64_t size,
599 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700600{
Chris Wilson05394f32010-11-08 19:18:58 +0000601 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300602 int ret;
603 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700604
Dave Airlieff72145b2011-02-07 12:16:14 +1000605 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200606 if (size == 0)
607 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700608
609 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000610 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100611 if (IS_ERR(obj))
612 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700613
Chris Wilson05394f32010-11-08 19:18:58 +0000614 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100615 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100616 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200617 if (ret)
618 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100619
Dave Airlieff72145b2011-02-07 12:16:14 +1000620 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700621 return 0;
622}
623
Dave Airlieff72145b2011-02-07 12:16:14 +1000624int
625i915_gem_dumb_create(struct drm_file *file,
626 struct drm_device *dev,
627 struct drm_mode_create_dumb *args)
628{
629 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300630 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000631 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000632 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000633 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000634}
635
Chris Wilsone27ab732017-06-15 13:38:49 +0100636static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
637{
638 return !(obj->cache_level == I915_CACHE_NONE ||
639 obj->cache_level == I915_CACHE_WT);
640}
641
Dave Airlieff72145b2011-02-07 12:16:14 +1000642/**
643 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100644 * @dev: drm device pointer
645 * @data: ioctl data blob
646 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000647 */
648int
649i915_gem_create_ioctl(struct drm_device *dev, void *data,
650 struct drm_file *file)
651{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000652 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000653 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200654
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000655 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100656
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000657 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000658 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000659}
660
Chris Wilsonef749212017-04-12 12:01:10 +0100661static inline enum fb_op_origin
662fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
663{
664 return (domain == I915_GEM_DOMAIN_GTT ?
665 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
666}
667
Chris Wilson71253972017-12-06 12:49:14 +0000668void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100669{
Chris Wilson71253972017-12-06 12:49:14 +0000670 /*
671 * No actual flushing is required for the GTT write domain for reads
672 * from the GTT domain. Writes to it "immediately" go to main memory
673 * as far as we know, so there's no chipset flush. It also doesn't
674 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100675 *
676 * However, we do have to enforce the order so that all writes through
677 * the GTT land before any writes to the device, such as updates to
678 * the GATT itself.
679 *
680 * We also have to wait a bit for the writes to land from the GTT.
681 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
682 * timing. This issue has only been observed when switching quickly
683 * between GTT writes and CPU reads from inside the kernel on recent hw,
684 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson71253972017-12-06 12:49:14 +0000685 * system agents we cannot reproduce this behaviour, until Cannonlake
686 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100687 */
Chris Wilson71253972017-12-06 12:49:14 +0000688
Chris Wilsonef749212017-04-12 12:01:10 +0100689 wmb();
690
Chris Wilson71253972017-12-06 12:49:14 +0000691 intel_runtime_pm_get(dev_priv);
692 spin_lock_irq(&dev_priv->uncore.lock);
693
694 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
695
696 spin_unlock_irq(&dev_priv->uncore.lock);
697 intel_runtime_pm_put(dev_priv);
698}
699
700static void
701flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
702{
703 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
704 struct i915_vma *vma;
705
Christian Königc0a51fd2018-02-16 13:43:38 +0100706 if (!(obj->write_domain & flush_domains))
Chris Wilson71253972017-12-06 12:49:14 +0000707 return;
708
Christian Königc0a51fd2018-02-16 13:43:38 +0100709 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100710 case I915_GEM_DOMAIN_GTT:
Chris Wilson71253972017-12-06 12:49:14 +0000711 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100712
713 intel_fb_obj_flush(obj,
714 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson71253972017-12-06 12:49:14 +0000715
Chris Wilsone2189dd2017-12-07 21:14:07 +0000716 for_each_ggtt_vma(vma, obj) {
Chris Wilson71253972017-12-06 12:49:14 +0000717 if (vma->iomap)
718 continue;
719
720 i915_vma_unset_ggtt_write(vma);
721 }
Chris Wilsonef749212017-04-12 12:01:10 +0100722 break;
723
724 case I915_GEM_DOMAIN_CPU:
725 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
726 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100727
728 case I915_GEM_DOMAIN_RENDER:
729 if (gpu_write_needs_clflush(obj))
730 obj->cache_dirty = true;
731 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100732 }
733
Christian Königc0a51fd2018-02-16 13:43:38 +0100734 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100735}
736
Daniel Vetter8c599672011-12-14 13:57:31 +0100737static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100738__copy_to_user_swizzled(char __user *cpu_vaddr,
739 const char *gpu_vaddr, int gpu_offset,
740 int length)
741{
742 int ret, cpu_offset = 0;
743
744 while (length > 0) {
745 int cacheline_end = ALIGN(gpu_offset + 1, 64);
746 int this_length = min(cacheline_end - gpu_offset, length);
747 int swizzled_gpu_offset = gpu_offset ^ 64;
748
749 ret = __copy_to_user(cpu_vaddr + cpu_offset,
750 gpu_vaddr + swizzled_gpu_offset,
751 this_length);
752 if (ret)
753 return ret + length;
754
755 cpu_offset += this_length;
756 gpu_offset += this_length;
757 length -= this_length;
758 }
759
760 return 0;
761}
762
763static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700764__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
765 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100766 int length)
767{
768 int ret, cpu_offset = 0;
769
770 while (length > 0) {
771 int cacheline_end = ALIGN(gpu_offset + 1, 64);
772 int this_length = min(cacheline_end - gpu_offset, length);
773 int swizzled_gpu_offset = gpu_offset ^ 64;
774
775 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
776 cpu_vaddr + cpu_offset,
777 this_length);
778 if (ret)
779 return ret + length;
780
781 cpu_offset += this_length;
782 gpu_offset += this_length;
783 length -= this_length;
784 }
785
786 return 0;
787}
788
Brad Volkin4c914c02014-02-18 10:15:45 -0800789/*
790 * Pins the specified object's pages and synchronizes the object with
791 * GPU accesses. Sets needs_clflush to non-zero if the caller should
792 * flush the object from the CPU cache.
793 */
794int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100795 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800796{
797 int ret;
798
Chris Wilsone95433c2016-10-28 13:58:27 +0100799 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800800
Chris Wilsone95433c2016-10-28 13:58:27 +0100801 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100802 if (!i915_gem_object_has_struct_page(obj))
803 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800804
Chris Wilsone95433c2016-10-28 13:58:27 +0100805 ret = i915_gem_object_wait(obj,
806 I915_WAIT_INTERRUPTIBLE |
807 I915_WAIT_LOCKED,
808 MAX_SCHEDULE_TIMEOUT,
809 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100810 if (ret)
811 return ret;
812
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100813 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100814 if (ret)
815 return ret;
816
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100817 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
818 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000819 ret = i915_gem_object_set_to_cpu_domain(obj, false);
820 if (ret)
821 goto err_unpin;
822 else
823 goto out;
824 }
825
Chris Wilsonef749212017-04-12 12:01:10 +0100826 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100827
Chris Wilson43394c72016-08-18 17:16:47 +0100828 /* If we're not in the cpu read domain, set ourself into the gtt
829 * read domain and manually flush cachelines (if required). This
830 * optimizes for the case when the gpu will dirty the data
831 * anyway again before the next pread happens.
832 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100833 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100834 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000835 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800836
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000837out:
Chris Wilson97649512016-08-18 17:16:50 +0100838 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100839 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100840
841err_unpin:
842 i915_gem_object_unpin_pages(obj);
843 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100844}
845
846int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
847 unsigned int *needs_clflush)
848{
849 int ret;
850
Chris Wilsone95433c2016-10-28 13:58:27 +0100851 lockdep_assert_held(&obj->base.dev->struct_mutex);
852
Chris Wilson43394c72016-08-18 17:16:47 +0100853 *needs_clflush = 0;
854 if (!i915_gem_object_has_struct_page(obj))
855 return -ENODEV;
856
Chris Wilsone95433c2016-10-28 13:58:27 +0100857 ret = i915_gem_object_wait(obj,
858 I915_WAIT_INTERRUPTIBLE |
859 I915_WAIT_LOCKED |
860 I915_WAIT_ALL,
861 MAX_SCHEDULE_TIMEOUT,
862 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100863 if (ret)
864 return ret;
865
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100866 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100867 if (ret)
868 return ret;
869
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100870 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
871 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000872 ret = i915_gem_object_set_to_cpu_domain(obj, true);
873 if (ret)
874 goto err_unpin;
875 else
876 goto out;
877 }
878
Chris Wilsonef749212017-04-12 12:01:10 +0100879 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100880
Chris Wilson43394c72016-08-18 17:16:47 +0100881 /* If we're not in the cpu write domain, set ourself into the
882 * gtt write domain and manually flush cachelines (as required).
883 * This optimizes for the case when the gpu will use the data
884 * right away and we therefore have to clflush anyway.
885 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100886 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000887 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100888
Chris Wilsone27ab732017-06-15 13:38:49 +0100889 /*
890 * Same trick applies to invalidate partially written
891 * cachelines read before writing.
892 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100893 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100894 *needs_clflush |= CLFLUSH_BEFORE;
895 }
Chris Wilson43394c72016-08-18 17:16:47 +0100896
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000897out:
Chris Wilson43394c72016-08-18 17:16:47 +0100898 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100899 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100900 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100901 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100902
903err_unpin:
904 i915_gem_object_unpin_pages(obj);
905 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800906}
907
Daniel Vetter23c18c72012-03-25 19:47:42 +0200908static void
909shmem_clflush_swizzled_range(char *addr, unsigned long length,
910 bool swizzled)
911{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200912 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200913 unsigned long start = (unsigned long) addr;
914 unsigned long end = (unsigned long) addr + length;
915
916 /* For swizzling simply ensure that we always flush both
917 * channels. Lame, but simple and it works. Swizzled
918 * pwrite/pread is far from a hotpath - current userspace
919 * doesn't use it at all. */
920 start = round_down(start, 128);
921 end = round_up(end, 128);
922
923 drm_clflush_virt_range((void *)start, end - start);
924 } else {
925 drm_clflush_virt_range(addr, length);
926 }
927
928}
929
Daniel Vetterd174bd62012-03-25 19:47:40 +0200930/* Only difference to the fast-path function is that this can handle bit17
931 * and uses non-atomic copy and kmap functions. */
932static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100933shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200934 char __user *user_data,
935 bool page_do_bit17_swizzling, bool needs_clflush)
936{
937 char *vaddr;
938 int ret;
939
940 vaddr = kmap(page);
941 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100942 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200943 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200944
945 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100946 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200947 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100948 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200949 kunmap(page);
950
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100951 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200952}
953
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100954static int
955shmem_pread(struct page *page, int offset, int length, char __user *user_data,
956 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530957{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100958 int ret;
959
960 ret = -ENODEV;
961 if (!page_do_bit17_swizzling) {
962 char *vaddr = kmap_atomic(page);
963
964 if (needs_clflush)
965 drm_clflush_virt_range(vaddr + offset, length);
966 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
967 kunmap_atomic(vaddr);
968 }
969 if (ret == 0)
970 return 0;
971
972 return shmem_pread_slow(page, offset, length, user_data,
973 page_do_bit17_swizzling, needs_clflush);
974}
975
976static int
977i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
978 struct drm_i915_gem_pread *args)
979{
980 char __user *user_data;
981 u64 remain;
982 unsigned int obj_do_bit17_swizzling;
983 unsigned int needs_clflush;
984 unsigned int idx, offset;
985 int ret;
986
987 obj_do_bit17_swizzling = 0;
988 if (i915_gem_object_needs_bit17_swizzle(obj))
989 obj_do_bit17_swizzling = BIT(17);
990
991 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
992 if (ret)
993 return ret;
994
995 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
996 mutex_unlock(&obj->base.dev->struct_mutex);
997 if (ret)
998 return ret;
999
1000 remain = args->size;
1001 user_data = u64_to_user_ptr(args->data_ptr);
1002 offset = offset_in_page(args->offset);
1003 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1004 struct page *page = i915_gem_object_get_page(obj, idx);
1005 int length;
1006
1007 length = remain;
1008 if (offset + length > PAGE_SIZE)
1009 length = PAGE_SIZE - offset;
1010
1011 ret = shmem_pread(page, offset, length, user_data,
1012 page_to_phys(page) & obj_do_bit17_swizzling,
1013 needs_clflush);
1014 if (ret)
1015 break;
1016
1017 remain -= length;
1018 user_data += length;
1019 offset = 0;
1020 }
1021
1022 i915_gem_obj_finish_shmem_access(obj);
1023 return ret;
1024}
1025
1026static inline bool
1027gtt_user_read(struct io_mapping *mapping,
1028 loff_t base, int offset,
1029 char __user *user_data, int length)
1030{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001031 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001032 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301033
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301034 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001035 vaddr = io_mapping_map_atomic_wc(mapping, base);
1036 unwritten = __copy_to_user_inatomic(user_data,
1037 (void __force *)vaddr + offset,
1038 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001039 io_mapping_unmap_atomic(vaddr);
1040 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001041 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1042 unwritten = copy_to_user(user_data,
1043 (void __force *)vaddr + offset,
1044 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001045 io_mapping_unmap(vaddr);
1046 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301047 return unwritten;
1048}
1049
1050static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001051i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1052 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301053{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001054 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1055 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301056 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001057 struct i915_vma *vma;
1058 void __user *user_data;
1059 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301060 int ret;
1061
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001062 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1063 if (ret)
1064 return ret;
1065
1066 intel_runtime_pm_get(i915);
1067 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001068 PIN_MAPPABLE |
1069 PIN_NONFAULT |
1070 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001071 if (!IS_ERR(vma)) {
1072 node.start = i915_ggtt_offset(vma);
1073 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001074 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001075 if (ret) {
1076 i915_vma_unpin(vma);
1077 vma = ERR_PTR(ret);
1078 }
1079 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001080 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001081 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301082 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001083 goto out_unlock;
1084 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301085 }
1086
1087 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1088 if (ret)
1089 goto out_unpin;
1090
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001091 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301092
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001093 user_data = u64_to_user_ptr(args->data_ptr);
1094 remain = args->size;
1095 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301096
1097 while (remain > 0) {
1098 /* Operation in this page
1099 *
1100 * page_base = page offset within aperture
1101 * page_offset = offset within page
1102 * page_length = bytes to copy for this page
1103 */
1104 u32 page_base = node.start;
1105 unsigned page_offset = offset_in_page(offset);
1106 unsigned page_length = PAGE_SIZE - page_offset;
1107 page_length = remain < page_length ? remain : page_length;
1108 if (node.allocated) {
1109 wmb();
1110 ggtt->base.insert_page(&ggtt->base,
1111 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001112 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301113 wmb();
1114 } else {
1115 page_base += offset & PAGE_MASK;
1116 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001117
Matthew Auld73ebd502017-12-11 15:18:20 +00001118 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001119 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301120 ret = -EFAULT;
1121 break;
1122 }
1123
1124 remain -= page_length;
1125 user_data += page_length;
1126 offset += page_length;
1127 }
1128
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001129 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301130out_unpin:
1131 if (node.allocated) {
1132 wmb();
1133 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001134 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301135 remove_mappable_node(&node);
1136 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001137 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301138 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001139out_unlock:
1140 intel_runtime_pm_put(i915);
1141 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001142
Eric Anholteb014592009-03-10 11:44:52 -07001143 return ret;
1144}
1145
Eric Anholt673a3942008-07-30 12:06:12 -07001146/**
1147 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001148 * @dev: drm device pointer
1149 * @data: ioctl data blob
1150 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001151 *
1152 * On error, the contents of *data are undefined.
1153 */
1154int
1155i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001156 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001157{
1158 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001159 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001160 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001161
Chris Wilson51311d02010-11-17 09:10:42 +00001162 if (args->size == 0)
1163 return 0;
1164
1165 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001166 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001167 args->size))
1168 return -EFAULT;
1169
Chris Wilson03ac0642016-07-20 13:31:51 +01001170 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001171 if (!obj)
1172 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001173
Chris Wilson7dcd2492010-09-26 20:21:44 +01001174 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001175 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001176 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001177 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001178 }
1179
Chris Wilsondb53a302011-02-03 11:57:46 +00001180 trace_i915_gem_object_pread(obj, args->offset, args->size);
1181
Chris Wilsone95433c2016-10-28 13:58:27 +01001182 ret = i915_gem_object_wait(obj,
1183 I915_WAIT_INTERRUPTIBLE,
1184 MAX_SCHEDULE_TIMEOUT,
1185 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001186 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001187 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001188
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001189 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001190 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001191 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001192
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001193 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001194 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001195 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301196
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 i915_gem_object_unpin_pages(obj);
1198out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001199 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001200 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001201}
1202
Keith Packard0839ccb2008-10-30 19:38:48 -07001203/* This is the fast write path which cannot handle
1204 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001205 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001206
Chris Wilsonfe115622016-10-28 13:58:40 +01001207static inline bool
1208ggtt_write(struct io_mapping *mapping,
1209 loff_t base, int offset,
1210 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001211{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001212 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001213 unsigned long unwritten;
1214
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001215 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001216 vaddr = io_mapping_map_atomic_wc(mapping, base);
1217 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001218 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001219 io_mapping_unmap_atomic(vaddr);
1220 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001221 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1222 unwritten = copy_from_user((void __force *)vaddr + offset,
1223 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001224 io_mapping_unmap(vaddr);
1225 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001226
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001227 return unwritten;
1228}
1229
Eric Anholt3de09aa2009-03-09 09:42:23 -07001230/**
1231 * This is the fast pwrite path, where we copy the data directly from the
1232 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001233 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001234 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001235 */
Eric Anholt673a3942008-07-30 12:06:12 -07001236static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001237i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1238 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001239{
Chris Wilsonfe115622016-10-28 13:58:40 +01001240 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301241 struct i915_ggtt *ggtt = &i915->ggtt;
1242 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001243 struct i915_vma *vma;
1244 u64 remain, offset;
1245 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301246 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301247
Chris Wilsonfe115622016-10-28 13:58:40 +01001248 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1249 if (ret)
1250 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001251
Chris Wilson8bd81812017-10-19 07:37:33 +01001252 if (i915_gem_object_has_struct_page(obj)) {
1253 /*
1254 * Avoid waking the device up if we can fallback, as
1255 * waking/resuming is very slow (worst-case 10-100 ms
1256 * depending on PCI sleeps and our own resume time).
1257 * This easily dwarfs any performance advantage from
1258 * using the cache bypass of indirect GGTT access.
1259 */
1260 if (!intel_runtime_pm_get_if_in_use(i915)) {
1261 ret = -EFAULT;
1262 goto out_unlock;
1263 }
1264 } else {
1265 /* No backing pages, no fallback, we must force GGTT access */
1266 intel_runtime_pm_get(i915);
1267 }
1268
Chris Wilson058d88c2016-08-15 10:49:06 +01001269 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001270 PIN_MAPPABLE |
1271 PIN_NONFAULT |
1272 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001273 if (!IS_ERR(vma)) {
1274 node.start = i915_ggtt_offset(vma);
1275 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001276 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001277 if (ret) {
1278 i915_vma_unpin(vma);
1279 vma = ERR_PTR(ret);
1280 }
1281 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001282 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001283 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301284 if (ret)
Chris Wilson8bd81812017-10-19 07:37:33 +01001285 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001286 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301287 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001288
1289 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1290 if (ret)
1291 goto out_unpin;
1292
Chris Wilsonfe115622016-10-28 13:58:40 +01001293 mutex_unlock(&i915->drm.struct_mutex);
1294
Chris Wilsonb19482d2016-08-18 17:16:43 +01001295 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001296
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301297 user_data = u64_to_user_ptr(args->data_ptr);
1298 offset = args->offset;
1299 remain = args->size;
1300 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001301 /* Operation in this page
1302 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001303 * page_base = page offset within aperture
1304 * page_offset = offset within page
1305 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001306 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301307 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001308 unsigned int page_offset = offset_in_page(offset);
1309 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301310 page_length = remain < page_length ? remain : page_length;
1311 if (node.allocated) {
1312 wmb(); /* flush the write before we modify the GGTT */
1313 ggtt->base.insert_page(&ggtt->base,
1314 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1315 node.start, I915_CACHE_NONE, 0);
1316 wmb(); /* flush modifications to the GGTT (insert_page) */
1317 } else {
1318 page_base += offset & PAGE_MASK;
1319 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001320 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001321 * source page isn't available. Return the error and we'll
1322 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301323 * If the object is non-shmem backed, we retry again with the
1324 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001325 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001326 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001327 user_data, page_length)) {
1328 ret = -EFAULT;
1329 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001330 }
Eric Anholt673a3942008-07-30 12:06:12 -07001331
Keith Packard0839ccb2008-10-30 19:38:48 -07001332 remain -= page_length;
1333 user_data += page_length;
1334 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001335 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001336 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001337
1338 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001339out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301340 if (node.allocated) {
1341 wmb();
1342 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001343 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301344 remove_mappable_node(&node);
1345 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001346 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301347 }
Chris Wilson8bd81812017-10-19 07:37:33 +01001348out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001349 intel_runtime_pm_put(i915);
Chris Wilson8bd81812017-10-19 07:37:33 +01001350out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001351 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001352 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001353}
1354
Eric Anholt673a3942008-07-30 12:06:12 -07001355static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001356shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001357 char __user *user_data,
1358 bool page_do_bit17_swizzling,
1359 bool needs_clflush_before,
1360 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001361{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001362 char *vaddr;
1363 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001364
Daniel Vetterd174bd62012-03-25 19:47:40 +02001365 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001366 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001367 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001368 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001369 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001370 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1371 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001372 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001373 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001374 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001375 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001376 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001377 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001378
Chris Wilson755d2212012-09-04 21:02:55 +01001379 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001380}
1381
Chris Wilsonfe115622016-10-28 13:58:40 +01001382/* Per-page copy function for the shmem pwrite fastpath.
1383 * Flushes invalid cachelines before writing to the target if
1384 * needs_clflush_before is set and flushes out any written cachelines after
1385 * writing if needs_clflush is set.
1386 */
Eric Anholt40123c12009-03-09 13:42:30 -07001387static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001388shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1389 bool page_do_bit17_swizzling,
1390 bool needs_clflush_before,
1391 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001392{
Chris Wilsonfe115622016-10-28 13:58:40 +01001393 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001394
Chris Wilsonfe115622016-10-28 13:58:40 +01001395 ret = -ENODEV;
1396 if (!page_do_bit17_swizzling) {
1397 char *vaddr = kmap_atomic(page);
1398
1399 if (needs_clflush_before)
1400 drm_clflush_virt_range(vaddr + offset, len);
1401 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1402 if (needs_clflush_after)
1403 drm_clflush_virt_range(vaddr + offset, len);
1404
1405 kunmap_atomic(vaddr);
1406 }
1407 if (ret == 0)
1408 return ret;
1409
1410 return shmem_pwrite_slow(page, offset, len, user_data,
1411 page_do_bit17_swizzling,
1412 needs_clflush_before,
1413 needs_clflush_after);
1414}
1415
1416static int
1417i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1418 const struct drm_i915_gem_pwrite *args)
1419{
1420 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1421 void __user *user_data;
1422 u64 remain;
1423 unsigned int obj_do_bit17_swizzling;
1424 unsigned int partial_cacheline_write;
1425 unsigned int needs_clflush;
1426 unsigned int offset, idx;
1427 int ret;
1428
1429 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001430 if (ret)
1431 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001432
Chris Wilsonfe115622016-10-28 13:58:40 +01001433 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1434 mutex_unlock(&i915->drm.struct_mutex);
1435 if (ret)
1436 return ret;
1437
1438 obj_do_bit17_swizzling = 0;
1439 if (i915_gem_object_needs_bit17_swizzle(obj))
1440 obj_do_bit17_swizzling = BIT(17);
1441
1442 /* If we don't overwrite a cacheline completely we need to be
1443 * careful to have up-to-date data by first clflushing. Don't
1444 * overcomplicate things and flush the entire patch.
1445 */
1446 partial_cacheline_write = 0;
1447 if (needs_clflush & CLFLUSH_BEFORE)
1448 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1449
Chris Wilson43394c72016-08-18 17:16:47 +01001450 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001451 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001452 offset = offset_in_page(args->offset);
1453 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1454 struct page *page = i915_gem_object_get_page(obj, idx);
1455 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001456
Chris Wilsonfe115622016-10-28 13:58:40 +01001457 length = remain;
1458 if (offset + length > PAGE_SIZE)
1459 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001460
Chris Wilsonfe115622016-10-28 13:58:40 +01001461 ret = shmem_pwrite(page, offset, length, user_data,
1462 page_to_phys(page) & obj_do_bit17_swizzling,
1463 (offset | length) & partial_cacheline_write,
1464 needs_clflush & CLFLUSH_AFTER);
1465 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001466 break;
1467
Chris Wilsonfe115622016-10-28 13:58:40 +01001468 remain -= length;
1469 user_data += length;
1470 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001471 }
1472
Chris Wilsond59b21e2017-02-22 11:40:49 +00001473 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001474 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001475 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001476}
1477
1478/**
1479 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001480 * @dev: drm device
1481 * @data: ioctl data blob
1482 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001483 *
1484 * On error, the contents of the buffer that were to be modified are undefined.
1485 */
1486int
1487i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001488 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001489{
1490 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001491 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001492 int ret;
1493
1494 if (args->size == 0)
1495 return 0;
1496
1497 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001498 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001499 args->size))
1500 return -EFAULT;
1501
Chris Wilson03ac0642016-07-20 13:31:51 +01001502 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001503 if (!obj)
1504 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001505
Chris Wilson7dcd2492010-09-26 20:21:44 +01001506 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001507 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001508 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001509 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001510 }
1511
Chris Wilsondb53a302011-02-03 11:57:46 +00001512 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1513
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001514 ret = -ENODEV;
1515 if (obj->ops->pwrite)
1516 ret = obj->ops->pwrite(obj, args);
1517 if (ret != -ENODEV)
1518 goto err;
1519
Chris Wilsone95433c2016-10-28 13:58:27 +01001520 ret = i915_gem_object_wait(obj,
1521 I915_WAIT_INTERRUPTIBLE |
1522 I915_WAIT_ALL,
1523 MAX_SCHEDULE_TIMEOUT,
1524 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001525 if (ret)
1526 goto err;
1527
Chris Wilsonfe115622016-10-28 13:58:40 +01001528 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001529 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001530 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001531
Daniel Vetter935aaa62012-03-25 19:47:35 +02001532 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001533 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1534 * it would end up going through the fenced access, and we'll get
1535 * different detiling behavior between reading and writing.
1536 * pread/pwrite currently are reading and writing from the CPU
1537 * perspective, requiring manual detiling by the client.
1538 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001539 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001540 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001541 /* Note that the gtt paths might fail with non-page-backed user
1542 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001543 * textures). Fallback to the shmem path in that case.
1544 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001545 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001546
Chris Wilsond1054ee2016-07-16 18:42:36 +01001547 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001548 if (obj->phys_handle)
1549 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301550 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001551 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001552 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001553
Chris Wilsonfe115622016-10-28 13:58:40 +01001554 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001555err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001556 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001557 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001558}
1559
Chris Wilson40e62d52016-10-28 13:58:41 +01001560static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1561{
1562 struct drm_i915_private *i915;
1563 struct list_head *list;
1564 struct i915_vma *vma;
1565
Chris Wilsonf2123812017-10-16 12:40:37 +01001566 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1567
Chris Wilsone2189dd2017-12-07 21:14:07 +00001568 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001569 if (i915_vma_is_active(vma))
1570 continue;
1571
1572 if (!drm_mm_node_allocated(&vma->node))
1573 continue;
1574
1575 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1576 }
1577
1578 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001579 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001580 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001581 list_move_tail(&obj->mm.link, list);
1582 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001583}
1584
Eric Anholt673a3942008-07-30 12:06:12 -07001585/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001586 * Called when user space prepares to use an object with the CPU, either
1587 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001588 * @dev: drm device
1589 * @data: ioctl data blob
1590 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001591 */
1592int
1593i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001594 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001595{
1596 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001597 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001598 uint32_t read_domains = args->read_domains;
1599 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001600 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001601
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001602 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001603 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001604 return -EINVAL;
1605
1606 /* Having something in the write domain implies it's in the read
1607 * domain, and only that read domain. Enforce that in the request.
1608 */
1609 if (write_domain != 0 && read_domains != write_domain)
1610 return -EINVAL;
1611
Chris Wilson03ac0642016-07-20 13:31:51 +01001612 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001613 if (!obj)
1614 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001615
Chris Wilson3236f572012-08-24 09:35:09 +01001616 /* Try to flush the object off the GPU without holding the lock.
1617 * We will repeat the flush holding the lock in the normal manner
1618 * to catch cases where we are gazumped.
1619 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001620 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001621 I915_WAIT_INTERRUPTIBLE |
1622 (write_domain ? I915_WAIT_ALL : 0),
1623 MAX_SCHEDULE_TIMEOUT,
1624 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001625 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001626 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001627
Tina Zhanga03f3952017-11-14 10:25:13 +00001628 /*
1629 * Proxy objects do not control access to the backing storage, ergo
1630 * they cannot be used as a means to manipulate the cache domain
1631 * tracking for that backing storage. The proxy object is always
1632 * considered to be outside of any cache domain.
1633 */
1634 if (i915_gem_object_is_proxy(obj)) {
1635 err = -ENXIO;
1636 goto out;
1637 }
1638
1639 /*
1640 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001641 * direct access in memory with previous cached writes through
1642 * shmemfs and that our cache domain tracking remains valid.
1643 * For example, if the obj->filp was moved to swap without us
1644 * being notified and releasing the pages, we would mistakenly
1645 * continue to assume that the obj remained out of the CPU cached
1646 * domain.
1647 */
1648 err = i915_gem_object_pin_pages(obj);
1649 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001650 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001651
1652 err = i915_mutex_lock_interruptible(dev);
1653 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001654 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001655
Chris Wilsone22d8e32017-04-12 12:01:11 +01001656 if (read_domains & I915_GEM_DOMAIN_WC)
1657 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1658 else if (read_domains & I915_GEM_DOMAIN_GTT)
1659 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301660 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001661 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001662
1663 /* And bump the LRU for this access */
1664 i915_gem_object_bump_inactive_ggtt(obj);
1665
1666 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001667
Daniel Vetter031b6982015-06-26 19:35:16 +02001668 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001669 intel_fb_obj_invalidate(obj,
1670 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001671
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001672out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001673 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001674out:
1675 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001676 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001677}
1678
1679/**
1680 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001681 * @dev: drm device
1682 * @data: ioctl data blob
1683 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001684 */
1685int
1686i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001687 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001688{
1689 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001690 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001691
Chris Wilson03ac0642016-07-20 13:31:51 +01001692 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001693 if (!obj)
1694 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001695
Tina Zhanga03f3952017-11-14 10:25:13 +00001696 /*
1697 * Proxy objects are barred from CPU access, so there is no
1698 * need to ban sw_finish as it is a nop.
1699 */
1700
Eric Anholt673a3942008-07-30 12:06:12 -07001701 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001702 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001703 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001704
1705 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001706}
1707
1708/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001709 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1710 * it is mapped to.
1711 * @dev: drm device
1712 * @data: ioctl data blob
1713 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001714 *
1715 * While the mapping holds a reference on the contents of the object, it doesn't
1716 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001717 *
1718 * IMPORTANT:
1719 *
1720 * DRM driver writers who look a this function as an example for how to do GEM
1721 * mmap support, please don't implement mmap support like here. The modern way
1722 * to implement DRM mmap support is with an mmap offset ioctl (like
1723 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1724 * That way debug tooling like valgrind will understand what's going on, hiding
1725 * the mmap call in a driver private ioctl will break that. The i915 driver only
1726 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001727 */
1728int
1729i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001730 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001731{
1732 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001733 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001734 unsigned long addr;
1735
Akash Goel1816f922015-01-02 16:29:30 +05301736 if (args->flags & ~(I915_MMAP_WC))
1737 return -EINVAL;
1738
Borislav Petkov568a58e2016-03-29 17:42:01 +02001739 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301740 return -ENODEV;
1741
Chris Wilson03ac0642016-07-20 13:31:51 +01001742 obj = i915_gem_object_lookup(file, args->handle);
1743 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001744 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001745
Daniel Vetter1286ff72012-05-10 15:25:09 +02001746 /* prime objects have no backing filp to GEM mmap
1747 * pages from.
1748 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001749 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001750 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001751 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001752 }
1753
Chris Wilson03ac0642016-07-20 13:31:51 +01001754 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001755 PROT_READ | PROT_WRITE, MAP_SHARED,
1756 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301757 if (args->flags & I915_MMAP_WC) {
1758 struct mm_struct *mm = current->mm;
1759 struct vm_area_struct *vma;
1760
Michal Hocko80a89a52016-05-23 16:26:11 -07001761 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001762 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001763 return -EINTR;
1764 }
Akash Goel1816f922015-01-02 16:29:30 +05301765 vma = find_vma(mm, addr);
1766 if (vma)
1767 vma->vm_page_prot =
1768 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1769 else
1770 addr = -ENOMEM;
1771 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001772
1773 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001774 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301775 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001776 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001777 if (IS_ERR((void *)addr))
1778 return addr;
1779
1780 args->addr_ptr = (uint64_t) addr;
1781
1782 return 0;
1783}
1784
Chris Wilson03af84f2016-08-18 17:17:01 +01001785static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1786{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001787 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001788}
1789
Jesse Barnesde151cf2008-11-12 10:03:55 -08001790/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001791 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1792 *
1793 * A history of the GTT mmap interface:
1794 *
1795 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1796 * aligned and suitable for fencing, and still fit into the available
1797 * mappable space left by the pinned display objects. A classic problem
1798 * we called the page-fault-of-doom where we would ping-pong between
1799 * two objects that could not fit inside the GTT and so the memcpy
1800 * would page one object in at the expense of the other between every
1801 * single byte.
1802 *
1803 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1804 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1805 * object is too large for the available space (or simply too large
1806 * for the mappable aperture!), a view is created instead and faulted
1807 * into userspace. (This view is aligned and sized appropriately for
1808 * fenced access.)
1809 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001810 * 2 - Recognise WC as a separate cache domain so that we can flush the
1811 * delayed writes via GTT before performing direct access via WC.
1812 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001813 * Restrictions:
1814 *
1815 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1816 * hangs on some architectures, corruption on others. An attempt to service
1817 * a GTT page fault from a snoopable object will generate a SIGBUS.
1818 *
1819 * * the object must be able to fit into RAM (physical memory, though no
1820 * limited to the mappable aperture).
1821 *
1822 *
1823 * Caveats:
1824 *
1825 * * a new GTT page fault will synchronize rendering from the GPU and flush
1826 * all data to system memory. Subsequent access will not be synchronized.
1827 *
1828 * * all mappings are revoked on runtime device suspend.
1829 *
1830 * * there are only 8, 16 or 32 fence registers to share between all users
1831 * (older machines require fence register for display and blitter access
1832 * as well). Contention of the fence registers will cause the previous users
1833 * to be unmapped and any new access will generate new page faults.
1834 *
1835 * * running out of memory while servicing a fault may generate a SIGBUS,
1836 * rather than the expected SIGSEGV.
1837 */
1838int i915_gem_mmap_gtt_version(void)
1839{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001840 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001841}
1842
Chris Wilson2d4281b2017-01-10 09:56:32 +00001843static inline struct i915_ggtt_view
1844compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001845 pgoff_t page_offset,
1846 unsigned int chunk)
1847{
1848 struct i915_ggtt_view view;
1849
1850 if (i915_gem_object_is_tiled(obj))
1851 chunk = roundup(chunk, tile_row_pages(obj));
1852
Chris Wilson2d4281b2017-01-10 09:56:32 +00001853 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001854 view.partial.offset = rounddown(page_offset, chunk);
1855 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001856 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001857 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001858
1859 /* If the partial covers the entire object, just create a normal VMA. */
1860 if (chunk >= obj->base.size >> PAGE_SHIFT)
1861 view.type = I915_GGTT_VIEW_NORMAL;
1862
1863 return view;
1864}
1865
Chris Wilson4cc69072016-08-25 19:05:19 +01001866/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001867 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001868 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001869 *
1870 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1871 * from userspace. The fault handler takes care of binding the object to
1872 * the GTT (if needed), allocating and programming a fence register (again,
1873 * only if needed based on whether the old reg is still valid or the object
1874 * is tiled) and inserting a new PTE into the faulting process.
1875 *
1876 * Note that the faulting process may involve evicting existing objects
1877 * from the GTT and/or fence registers to make room. So performance may
1878 * suffer if the GTT working set is large or there are few fence registers
1879 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001880 *
1881 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1882 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001883 */
Dave Jiang11bac802017-02-24 14:56:41 -08001884int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001885{
Chris Wilson03af84f2016-08-18 17:17:01 +01001886#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001887 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001888 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001889 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001890 struct drm_i915_private *dev_priv = to_i915(dev);
1891 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001892 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001893 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001894 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001895 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001896 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001897
Jesse Barnesde151cf2008-11-12 10:03:55 -08001898 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001899 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001900
Chris Wilsondb53a302011-02-03 11:57:46 +00001901 trace_i915_gem_object_fault(obj, page_offset, true, write);
1902
Chris Wilson6e4930f2014-02-07 18:37:06 -02001903 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001904 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001905 * repeat the flush holding the lock in the normal manner to catch cases
1906 * where we are gazumped.
1907 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001908 ret = i915_gem_object_wait(obj,
1909 I915_WAIT_INTERRUPTIBLE,
1910 MAX_SCHEDULE_TIMEOUT,
1911 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001912 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001913 goto err;
1914
Chris Wilson40e62d52016-10-28 13:58:41 +01001915 ret = i915_gem_object_pin_pages(obj);
1916 if (ret)
1917 goto err;
1918
Chris Wilsonb8f90962016-08-05 10:14:07 +01001919 intel_runtime_pm_get(dev_priv);
1920
1921 ret = i915_mutex_lock_interruptible(dev);
1922 if (ret)
1923 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001924
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001925 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001926 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001927 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001928 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001929 }
1930
Chris Wilson82118872016-08-18 17:17:05 +01001931 /* If the object is smaller than a couple of partial vma, it is
1932 * not worth only creating a single partial vma - we may as well
1933 * clear enough space for the full object.
1934 */
1935 flags = PIN_MAPPABLE;
1936 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1937 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1938
Chris Wilsona61007a2016-08-18 17:17:02 +01001939 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001940 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001941 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001942 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001943 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001944 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001945
Chris Wilson50349242016-08-18 17:17:04 +01001946 /* Userspace is now writing through an untracked VMA, abandon
1947 * all hope that the hardware is able to track future writes.
1948 */
1949 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1950
Chris Wilsona61007a2016-08-18 17:17:02 +01001951 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1952 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001953 if (IS_ERR(vma)) {
1954 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001955 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001956 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001957
Chris Wilsonc9839302012-11-20 10:45:17 +00001958 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1959 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001960 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001961
Chris Wilson3bd40732017-10-09 09:43:56 +01001962 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001963 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001964 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001965
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001966 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001967 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001968 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001969 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001970 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001971 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001972 if (ret)
1973 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001974
Chris Wilsona65adaf2017-10-09 09:43:57 +01001975 /* Mark as being mmapped into userspace for later revocation */
1976 assert_rpm_wakelock_held(dev_priv);
1977 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1978 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1979 GEM_BUG_ON(!obj->userfault_count);
1980
Chris Wilson71253972017-12-06 12:49:14 +00001981 i915_vma_set_ggtt_write(vma);
1982
Chris Wilsona65adaf2017-10-09 09:43:57 +01001983err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001984 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001985err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001986 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001987err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001988 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001989err_rpm:
1990 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001991 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001992err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001993 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001994 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001995 /*
1996 * We eat errors when the gpu is terminally wedged to avoid
1997 * userspace unduly crashing (gl has no provisions for mmaps to
1998 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1999 * and so needs to be reported.
2000 */
2001 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002002 ret = VM_FAULT_SIGBUS;
2003 break;
2004 }
Chris Wilson045e7692010-11-07 09:18:22 +00002005 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002006 /*
2007 * EAGAIN means the gpu is hung and we'll wait for the error
2008 * handler to reset everything when re-faulting in
2009 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002010 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002011 case 0:
2012 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002013 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002014 case -EBUSY:
2015 /*
2016 * EBUSY is ok: this just means that another thread
2017 * already did the job.
2018 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002019 ret = VM_FAULT_NOPAGE;
2020 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002021 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002022 ret = VM_FAULT_OOM;
2023 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002024 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002025 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002026 ret = VM_FAULT_SIGBUS;
2027 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002028 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002029 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002030 ret = VM_FAULT_SIGBUS;
2031 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002032 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002033 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002034}
2035
Chris Wilsona65adaf2017-10-09 09:43:57 +01002036static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2037{
2038 struct i915_vma *vma;
2039
2040 GEM_BUG_ON(!obj->userfault_count);
2041
2042 obj->userfault_count = 0;
2043 list_del(&obj->userfault_link);
2044 drm_vma_node_unmap(&obj->base.vma_node,
2045 obj->base.dev->anon_inode->i_mapping);
2046
Chris Wilsone2189dd2017-12-07 21:14:07 +00002047 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002048 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002049}
2050
Jesse Barnesde151cf2008-11-12 10:03:55 -08002051/**
Chris Wilson901782b2009-07-10 08:18:50 +01002052 * i915_gem_release_mmap - remove physical page mappings
2053 * @obj: obj in question
2054 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002055 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002056 * relinquish ownership of the pages back to the system.
2057 *
2058 * It is vital that we remove the page mapping if we have mapped a tiled
2059 * object through the GTT and then lose the fence register due to
2060 * resource pressure. Similarly if the object has been moved out of the
2061 * aperture, than pages mapped into userspace must be revoked. Removing the
2062 * mapping will then trigger a page fault on the next user access, allowing
2063 * fixup by i915_gem_fault().
2064 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002065void
Chris Wilson05394f32010-11-08 19:18:58 +00002066i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002067{
Chris Wilson275f0392016-10-24 13:42:14 +01002068 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002069
Chris Wilson349f2cc2016-04-13 17:35:12 +01002070 /* Serialisation between user GTT access and our code depends upon
2071 * revoking the CPU's PTE whilst the mutex is held. The next user
2072 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002073 *
2074 * Note that RPM complicates somewhat by adding an additional
2075 * requirement that operations to the GGTT be made holding the RPM
2076 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002077 */
Chris Wilson275f0392016-10-24 13:42:14 +01002078 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002079 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002080
Chris Wilsona65adaf2017-10-09 09:43:57 +01002081 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002082 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002083
Chris Wilsona65adaf2017-10-09 09:43:57 +01002084 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002085
2086 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2087 * memory transactions from userspace before we return. The TLB
2088 * flushing implied above by changing the PTE above *should* be
2089 * sufficient, an extra barrier here just provides us with a bit
2090 * of paranoid documentation about our requirement to serialise
2091 * memory writes before touching registers / GSM.
2092 */
2093 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002094
2095out:
2096 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002097}
2098
Chris Wilson7c108fd2016-10-24 13:42:18 +01002099void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002100{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002101 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002102 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002103
Chris Wilson3594a3e2016-10-24 13:42:16 +01002104 /*
2105 * Only called during RPM suspend. All users of the userfault_list
2106 * must be holding an RPM wakeref to ensure that this can not
2107 * run concurrently with themselves (and use the struct_mutex for
2108 * protection between themselves).
2109 */
2110
2111 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002112 &dev_priv->mm.userfault_list, userfault_link)
2113 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002114
2115 /* The fence will be lost when the device powers down. If any were
2116 * in use by hardware (i.e. they are pinned), we should not be powering
2117 * down! All other fences will be reacquired by the user upon waking.
2118 */
2119 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2120 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2121
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002122 /* Ideally we want to assert that the fence register is not
2123 * live at this point (i.e. that no piece of code will be
2124 * trying to write through fence + GTT, as that both violates
2125 * our tracking of activity and associated locking/barriers,
2126 * but also is illegal given that the hw is powered down).
2127 *
2128 * Previously we used reg->pin_count as a "liveness" indicator.
2129 * That is not sufficient, and we need a more fine-grained
2130 * tool if we want to have a sanity check here.
2131 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002132
2133 if (!reg->vma)
2134 continue;
2135
Chris Wilsona65adaf2017-10-09 09:43:57 +01002136 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002137 reg->dirty = true;
2138 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002139}
2140
Chris Wilsond8cb5082012-08-11 15:41:03 +01002141static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2142{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002143 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002144 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002145
Chris Wilsonf3f61842016-08-05 10:14:14 +01002146 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002147 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002148 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002149
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002150 /* Attempt to reap some mmap space from dead objects */
2151 do {
2152 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2153 if (err)
2154 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002155
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002156 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002157 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002158 if (!err)
2159 break;
2160
2161 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002162
Chris Wilsonf3f61842016-08-05 10:14:14 +01002163 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002164}
2165
2166static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2167{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002168 drm_gem_free_mmap_offset(&obj->base);
2169}
2170
Dave Airlieda6b51d2014-12-24 13:11:17 +10002171int
Dave Airlieff72145b2011-02-07 12:16:14 +10002172i915_gem_mmap_gtt(struct drm_file *file,
2173 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002174 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002175 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002176{
Chris Wilson05394f32010-11-08 19:18:58 +00002177 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002178 int ret;
2179
Chris Wilson03ac0642016-07-20 13:31:51 +01002180 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002181 if (!obj)
2182 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002183
Chris Wilsond8cb5082012-08-11 15:41:03 +01002184 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002185 if (ret == 0)
2186 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002187
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002188 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002189 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002190}
2191
Dave Airlieff72145b2011-02-07 12:16:14 +10002192/**
2193 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2194 * @dev: DRM device
2195 * @data: GTT mapping ioctl data
2196 * @file: GEM object info
2197 *
2198 * Simply returns the fake offset to userspace so it can mmap it.
2199 * The mmap call will end up in drm_gem_mmap(), which will set things
2200 * up so we can get faults in the handler above.
2201 *
2202 * The fault handler will take care of binding the object into the GTT
2203 * (since it may have been evicted to make room for something), allocating
2204 * a fence register, and mapping the appropriate aperture address into
2205 * userspace.
2206 */
2207int
2208i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2209 struct drm_file *file)
2210{
2211 struct drm_i915_gem_mmap_gtt *args = data;
2212
Dave Airlieda6b51d2014-12-24 13:11:17 +10002213 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002214}
2215
Daniel Vetter225067e2012-08-20 10:23:20 +02002216/* Immediately discard the backing storage */
2217static void
2218i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002219{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002220 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002221
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002222 if (obj->base.filp == NULL)
2223 return;
2224
Daniel Vetter225067e2012-08-20 10:23:20 +02002225 /* Our goal here is to return as much of the memory as
2226 * is possible back to the system as we are called from OOM.
2227 * To do this we must instruct the shmfs to drop all of its
2228 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002229 */
Chris Wilson55372522014-03-25 13:23:06 +00002230 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002231 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002232 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002233}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002234
Chris Wilson55372522014-03-25 13:23:06 +00002235/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002236void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002237{
Chris Wilson55372522014-03-25 13:23:06 +00002238 struct address_space *mapping;
2239
Chris Wilson1233e2d2016-10-28 13:58:37 +01002240 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002241 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002242
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002243 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002244 case I915_MADV_DONTNEED:
2245 i915_gem_object_truncate(obj);
2246 case __I915_MADV_PURGED:
2247 return;
2248 }
2249
2250 if (obj->base.filp == NULL)
2251 return;
2252
Al Viro93c76a32015-12-04 23:45:44 -05002253 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002254 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002255}
2256
Chris Wilson5cdf5882010-09-27 15:51:07 +01002257static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002258i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2259 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002260{
Dave Gordon85d12252016-05-20 11:54:06 +01002261 struct sgt_iter sgt_iter;
2262 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002263
Chris Wilsone5facdf2016-12-23 14:57:57 +00002264 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002265
Chris Wilson03ac84f2016-10-28 13:58:36 +01002266 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002267
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002268 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002269 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002270
Chris Wilson03ac84f2016-10-28 13:58:36 +01002271 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002272 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002273 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002274
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002275 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002276 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002277
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002278 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002279 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002280 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002281
Chris Wilson03ac84f2016-10-28 13:58:36 +01002282 sg_free_table(pages);
2283 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002284}
2285
Chris Wilson96d77632016-10-28 13:58:33 +01002286static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2287{
2288 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002289 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002290
Chris Wilsonbea6e982017-10-26 14:00:31 +01002291 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002292 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2293 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002294 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002295}
2296
Chris Wilson548625e2016-11-01 12:11:34 +00002297void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2298 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002299{
Chris Wilsonf2123812017-10-16 12:40:37 +01002300 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002301 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002302
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002303 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002304 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002305
Chris Wilson15717de2016-08-04 07:52:26 +01002306 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002307 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002308 return;
2309
2310 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002311 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002312 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2313 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002314
Chris Wilsona2165e32012-12-03 11:49:00 +00002315 /* ->put_pages might need to allocate memory for the bit17 swizzle
2316 * array, hence protect them from being reaped by removing them from gtt
2317 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002318 pages = fetch_and_zero(&obj->mm.pages);
2319 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002320
Chris Wilsonf2123812017-10-16 12:40:37 +01002321 spin_lock(&i915->mm.obj_lock);
2322 list_del(&obj->mm.link);
2323 spin_unlock(&i915->mm.obj_lock);
2324
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002325 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002326 void *ptr;
2327
Chris Wilson0ce81782017-05-17 13:09:59 +01002328 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002329 if (is_vmalloc_addr(ptr))
2330 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002331 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002332 kunmap(kmap_to_page(ptr));
2333
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002334 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002335 }
2336
Chris Wilson96d77632016-10-28 13:58:33 +01002337 __i915_gem_object_reset_page_iter(obj);
2338
Chris Wilson4e5462e2017-03-07 13:20:31 +00002339 if (!IS_ERR(pages))
2340 obj->ops->put_pages(obj, pages);
2341
Matthew Aulda5c081662017-10-06 23:18:18 +01002342 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2343
Chris Wilson1233e2d2016-10-28 13:58:37 +01002344unlock:
2345 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002346}
2347
Chris Wilson935a2f72017-02-13 17:15:13 +00002348static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002349{
2350 struct sg_table new_st;
2351 struct scatterlist *sg, *new_sg;
2352 unsigned int i;
2353
2354 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002355 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002356
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002357 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002358 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002359
2360 new_sg = new_st.sgl;
2361 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2362 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2363 /* called before being DMA mapped, no need to copy sg->dma_* */
2364 new_sg = sg_next(new_sg);
2365 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002366 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002367
2368 sg_free_table(orig_st);
2369
2370 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002371 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002372}
2373
Matthew Auldb91b09e2017-10-06 23:18:17 +01002374static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002375{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002376 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002377 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2378 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002379 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002380 struct sg_table *st;
2381 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002382 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002383 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002384 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002385 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002386 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002387 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002388 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002389
Chris Wilson6c085a72012-08-20 11:40:46 +02002390 /* Assert that the object is not currently in any GPU domain. As it
2391 * wasn't in the GTT, there shouldn't be any way it could have been in
2392 * a GPU cache
2393 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002394 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2395 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002396
Chris Wilson9da3da62012-06-01 15:20:22 +01002397 st = kmalloc(sizeof(*st), GFP_KERNEL);
2398 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002399 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002400
Chris Wilsond766ef52016-12-19 12:43:45 +00002401rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002402 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002403 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002404 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002405 }
2406
2407 /* Get the list of pages out of our struct file. They'll be pinned
2408 * at this point until we release them.
2409 *
2410 * Fail silently without starting the shrinker
2411 */
Al Viro93c76a32015-12-04 23:45:44 -05002412 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002413 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002414 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2415
Imre Deak90797e62013-02-18 19:28:03 +02002416 sg = st->sgl;
2417 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002418 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002419 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002420 const unsigned int shrink[] = {
2421 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2422 0,
2423 }, *s = shrink;
2424 gfp_t gfp = noreclaim;
2425
2426 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002427 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002428 if (likely(!IS_ERR(page)))
2429 break;
2430
2431 if (!*s) {
2432 ret = PTR_ERR(page);
2433 goto err_sg;
2434 }
2435
Chris Wilson912d5722017-09-06 16:19:30 -07002436 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002437 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002438
Chris Wilson6c085a72012-08-20 11:40:46 +02002439 /* We've tried hard to allocate the memory by reaping
2440 * our own buffer, now let the real VM do its job and
2441 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002442 *
2443 * However, since graphics tend to be disposable,
2444 * defer the oom here by reporting the ENOMEM back
2445 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002446 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002447 if (!*s) {
2448 /* reclaim and warn, but no oom */
2449 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002450
2451 /* Our bo are always dirty and so we require
2452 * kswapd to reclaim our pages (direct reclaim
2453 * does not effectively begin pageout of our
2454 * buffers on its own). However, direct reclaim
2455 * only waits for kswapd when under allocation
2456 * congestion. So as a result __GFP_RECLAIM is
2457 * unreliable and fails to actually reclaim our
2458 * dirty pages -- unless you try over and over
2459 * again with !__GFP_NORETRY. However, we still
2460 * want to fail this allocation rather than
2461 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002462 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002463 */
Michal Hockodbb32952017-07-12 14:36:55 -07002464 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002465 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002466 } while (1);
2467
Chris Wilson871dfbd2016-10-11 09:20:21 +01002468 if (!i ||
2469 sg->length >= max_segment ||
2470 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002471 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002472 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002473 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002474 }
Imre Deak90797e62013-02-18 19:28:03 +02002475 st->nents++;
2476 sg_set_page(sg, page, PAGE_SIZE, 0);
2477 } else {
2478 sg->length += PAGE_SIZE;
2479 }
2480 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002481
2482 /* Check that the i965g/gm workaround works. */
2483 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002484 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002485 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002486 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002487 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002488 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002489
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002490 /* Trim unused sg entries to avoid wasting memory. */
2491 i915_sg_trim(st);
2492
Chris Wilson03ac84f2016-10-28 13:58:36 +01002493 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002494 if (ret) {
2495 /* DMA remapping failed? One possible cause is that
2496 * it could not reserve enough large entries, asking
2497 * for PAGE_SIZE chunks instead may be helpful.
2498 */
2499 if (max_segment > PAGE_SIZE) {
2500 for_each_sgt_page(page, sgt_iter, st)
2501 put_page(page);
2502 sg_free_table(st);
2503
2504 max_segment = PAGE_SIZE;
2505 goto rebuild_st;
2506 } else {
2507 dev_warn(&dev_priv->drm.pdev->dev,
2508 "Failed to DMA remap %lu pages\n",
2509 page_count);
2510 goto err_pages;
2511 }
2512 }
Imre Deake2273302015-07-09 12:59:05 +03002513
Eric Anholt673a3942008-07-30 12:06:12 -07002514 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002515 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002516
Matthew Auld84e89782017-10-09 12:00:24 +01002517 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002518
2519 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002520
Chris Wilsonb17993b2016-11-14 11:29:30 +00002521err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002522 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002523err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002524 for_each_sgt_page(page, sgt_iter, st)
2525 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002526 sg_free_table(st);
2527 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002528
2529 /* shmemfs first checks if there is enough memory to allocate the page
2530 * and reports ENOSPC should there be insufficient, along with the usual
2531 * ENOMEM for a genuine allocation failure.
2532 *
2533 * We use ENOSPC in our driver to mean that we have run out of aperture
2534 * space and so want to translate the error from shmemfs back to our
2535 * usual understanding of ENOMEM.
2536 */
Imre Deake2273302015-07-09 12:59:05 +03002537 if (ret == -ENOSPC)
2538 ret = -ENOMEM;
2539
Matthew Auldb91b09e2017-10-06 23:18:17 +01002540 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002541}
2542
2543void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002544 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002545 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002546{
Matthew Aulda5c081662017-10-06 23:18:18 +01002547 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2548 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2549 int i;
2550
Chris Wilson1233e2d2016-10-28 13:58:37 +01002551 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002552
2553 obj->mm.get_page.sg_pos = pages->sgl;
2554 obj->mm.get_page.sg_idx = 0;
2555
2556 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002557
2558 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002559 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002560 GEM_BUG_ON(obj->mm.quirked);
2561 __i915_gem_object_pin_pages(obj);
2562 obj->mm.quirked = true;
2563 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002564
Matthew Auld84e89782017-10-09 12:00:24 +01002565 GEM_BUG_ON(!sg_page_sizes);
2566 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002567
2568 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002569 * Calculate the supported page-sizes which fit into the given
2570 * sg_page_sizes. This will give us the page-sizes which we may be able
2571 * to use opportunistically when later inserting into the GTT. For
2572 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2573 * 64K or 4K pages, although in practice this will depend on a number of
2574 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002575 */
2576 obj->mm.page_sizes.sg = 0;
2577 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2578 if (obj->mm.page_sizes.phys & ~0u << i)
2579 obj->mm.page_sizes.sg |= BIT(i);
2580 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002581 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002582
2583 spin_lock(&i915->mm.obj_lock);
2584 list_add(&obj->mm.link, &i915->mm.unbound_list);
2585 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002586}
2587
2588static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2589{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002590 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002591
2592 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2593 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2594 return -EFAULT;
2595 }
2596
Matthew Auldb91b09e2017-10-06 23:18:17 +01002597 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002598 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002599
Matthew Auldb91b09e2017-10-06 23:18:17 +01002600 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002601}
2602
Chris Wilson37e680a2012-06-07 15:38:42 +01002603/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002604 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002605 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002606 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002607 * either as a result of memory pressure (reaping pages under the shrinker)
2608 * or as the object is itself released.
2609 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002610int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002611{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002612 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002613
Chris Wilson1233e2d2016-10-28 13:58:37 +01002614 err = mutex_lock_interruptible(&obj->mm.lock);
2615 if (err)
2616 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002617
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002618 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002619 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2620
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002621 err = ____i915_gem_object_get_pages(obj);
2622 if (err)
2623 goto unlock;
2624
2625 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002626 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002627 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002628
Chris Wilson1233e2d2016-10-28 13:58:37 +01002629unlock:
2630 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002631 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002632}
2633
Dave Gordondd6034c2016-05-20 11:54:04 +01002634/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002635static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2636 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002637{
2638 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002639 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002640 struct sgt_iter sgt_iter;
2641 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002642 struct page *stack_pages[32];
2643 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002644 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002645 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002646 void *addr;
2647
2648 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002649 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002650 return kmap(sg_page(sgt->sgl));
2651
Dave Gordonb338fa42016-05-20 11:54:05 +01002652 if (n_pages > ARRAY_SIZE(stack_pages)) {
2653 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002654 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002655 if (!pages)
2656 return NULL;
2657 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002658
Dave Gordon85d12252016-05-20 11:54:06 +01002659 for_each_sgt_page(page, sgt_iter, sgt)
2660 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002661
2662 /* Check that we have the expected number of pages */
2663 GEM_BUG_ON(i != n_pages);
2664
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002665 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002666 default:
2667 MISSING_CASE(type);
2668 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002669 case I915_MAP_WB:
2670 pgprot = PAGE_KERNEL;
2671 break;
2672 case I915_MAP_WC:
2673 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2674 break;
2675 }
2676 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002677
Dave Gordonb338fa42016-05-20 11:54:05 +01002678 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002679 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002680
2681 return addr;
2682}
2683
2684/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002685void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2686 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002687{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002688 enum i915_map_type has_type;
2689 bool pinned;
2690 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002691 int ret;
2692
Tina Zhanga03f3952017-11-14 10:25:13 +00002693 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2694 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002695
Chris Wilson1233e2d2016-10-28 13:58:37 +01002696 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002697 if (ret)
2698 return ERR_PTR(ret);
2699
Chris Wilsona575c672017-08-28 11:46:31 +01002700 pinned = !(type & I915_MAP_OVERRIDE);
2701 type &= ~I915_MAP_OVERRIDE;
2702
Chris Wilson1233e2d2016-10-28 13:58:37 +01002703 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002704 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002705 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2706
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002707 ret = ____i915_gem_object_get_pages(obj);
2708 if (ret)
2709 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002710
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002711 smp_mb__before_atomic();
2712 }
2713 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002714 pinned = false;
2715 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002716 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002717
Chris Wilson0ce81782017-05-17 13:09:59 +01002718 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002719 if (ptr && has_type != type) {
2720 if (pinned) {
2721 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002722 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002723 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002724
2725 if (is_vmalloc_addr(ptr))
2726 vunmap(ptr);
2727 else
2728 kunmap(kmap_to_page(ptr));
2729
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002730 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002731 }
2732
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002733 if (!ptr) {
2734 ptr = i915_gem_object_map(obj, type);
2735 if (!ptr) {
2736 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002737 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002738 }
2739
Chris Wilson0ce81782017-05-17 13:09:59 +01002740 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002741 }
2742
Chris Wilson1233e2d2016-10-28 13:58:37 +01002743out_unlock:
2744 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002745 return ptr;
2746
Chris Wilson1233e2d2016-10-28 13:58:37 +01002747err_unpin:
2748 atomic_dec(&obj->mm.pages_pin_count);
2749err_unlock:
2750 ptr = ERR_PTR(ret);
2751 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002752}
2753
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002754static int
2755i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2756 const struct drm_i915_gem_pwrite *arg)
2757{
2758 struct address_space *mapping = obj->base.filp->f_mapping;
2759 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2760 u64 remain, offset;
2761 unsigned int pg;
2762
2763 /* Before we instantiate/pin the backing store for our use, we
2764 * can prepopulate the shmemfs filp efficiently using a write into
2765 * the pagecache. We avoid the penalty of instantiating all the
2766 * pages, important if the user is just writing to a few and never
2767 * uses the object on the GPU, and using a direct write into shmemfs
2768 * allows it to avoid the cost of retrieving a page (either swapin
2769 * or clearing-before-use) before it is overwritten.
2770 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002771 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002772 return -ENODEV;
2773
Chris Wilsona6d65e42017-10-16 21:27:32 +01002774 if (obj->mm.madv != I915_MADV_WILLNEED)
2775 return -EFAULT;
2776
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002777 /* Before the pages are instantiated the object is treated as being
2778 * in the CPU domain. The pages will be clflushed as required before
2779 * use, and we can freely write into the pages directly. If userspace
2780 * races pwrite with any other operation; corruption will ensue -
2781 * that is userspace's prerogative!
2782 */
2783
2784 remain = arg->size;
2785 offset = arg->offset;
2786 pg = offset_in_page(offset);
2787
2788 do {
2789 unsigned int len, unwritten;
2790 struct page *page;
2791 void *data, *vaddr;
2792 int err;
2793
2794 len = PAGE_SIZE - pg;
2795 if (len > remain)
2796 len = remain;
2797
2798 err = pagecache_write_begin(obj->base.filp, mapping,
2799 offset, len, 0,
2800 &page, &data);
2801 if (err < 0)
2802 return err;
2803
2804 vaddr = kmap(page);
2805 unwritten = copy_from_user(vaddr + pg, user_data, len);
2806 kunmap(page);
2807
2808 err = pagecache_write_end(obj->base.filp, mapping,
2809 offset, len, len - unwritten,
2810 page, data);
2811 if (err < 0)
2812 return err;
2813
2814 if (unwritten)
2815 return -EFAULT;
2816
2817 remain -= len;
2818 user_data += len;
2819 offset += len;
2820 pg = 0;
2821 } while (remain);
2822
2823 return 0;
2824}
2825
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002826static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002827{
Chris Wilson77b25a92017-07-21 13:32:30 +01002828 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002829
Chris Wilson77b25a92017-07-21 13:32:30 +01002830 atomic_inc(&ctx->guilty_count);
2831
Chris Wilson24eae082018-02-05 09:22:01 +00002832 banned = false;
2833 if (i915_gem_context_is_bannable(ctx)) {
2834 unsigned int score;
2835
2836 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2837 &ctx->ban_score);
2838 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2839
2840 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2841 ctx->name, score, yesno(banned));
2842 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002843 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002844 return;
2845
Chris Wilson77b25a92017-07-21 13:32:30 +01002846 i915_gem_context_set_banned(ctx);
2847 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2848 atomic_inc(&ctx->file_priv->context_bans);
2849 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2850 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2851 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002852}
2853
2854static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2855{
Chris Wilson77b25a92017-07-21 13:32:30 +01002856 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002857}
2858
Chris Wilsone61e0f52018-02-21 09:56:36 +00002859struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002860i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002861{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002862 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002863 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002864
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002865 /* We are called by the error capture and reset at a random
2866 * point in time. In particular, note that neither is crucially
2867 * ordered with an interrupt. After a hang, the GPU is dead and we
2868 * assume that no more writes can happen (we waited long enough for
2869 * all writes that were in transaction to be flushed) - adding an
2870 * extra delay for a recent interrupt is pointless. Hence, we do
2871 * not need an engine->irq_seqno_barrier() before the seqno reads.
2872 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002873 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002874 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002875 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002876 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002877
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002878 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002879 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2880 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002881
Chris Wilson754c9fd2017-02-23 07:44:14 +00002882 active = request;
2883 break;
2884 }
2885 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2886
2887 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002888}
2889
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002890static bool engine_stalled(struct intel_engine_cs *engine)
2891{
2892 if (!engine->hangcheck.stalled)
2893 return false;
2894
2895 /* Check for possible seqno movement after hang declaration */
2896 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2897 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2898 return false;
2899 }
2900
2901 return true;
2902}
2903
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002904/*
2905 * Ensure irq handler finishes, and not run again.
2906 * Also return the active request so that we only search for it once.
2907 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002908struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002909i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2910{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002911 struct i915_request *request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002912
Chris Wilson1749d902017-10-09 12:02:59 +01002913 /*
2914 * During the reset sequence, we must prevent the engine from
2915 * entering RC6. As the context state is undefined until we restart
2916 * the engine, if it does enter RC6 during the reset, the state
2917 * written to the powercontext is undefined and so we may lose
2918 * GPU state upon resume, i.e. fail to restart after a reset.
2919 */
2920 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2921
2922 /*
2923 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002924 * state (by calling dma_fence_signal) as we are processing
2925 * the reset. The write from the GPU of the seqno is
2926 * asynchronous and the signaler thread may see a different
2927 * value to us and declare the request complete, even though
2928 * the reset routine have picked that request as the active
2929 * (incomplete) request. This conflict is not handled
2930 * gracefully!
2931 */
2932 kthread_park(engine->breadcrumbs.signaler);
2933
Chris Wilson1749d902017-10-09 12:02:59 +01002934 /*
2935 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002936 * completed the reset in i915_gem_reset_finish(). If a request
2937 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302938 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002939 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302940 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002941 * prevents the race.
2942 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302943 tasklet_kill(&engine->execlists.tasklet);
2944 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002945
Michał Winiarskic41937f2017-10-26 15:35:58 +02002946 /*
2947 * We're using worker to queue preemption requests from the tasklet in
2948 * GuC submission mode.
2949 * Even though tasklet was disabled, we may still have a worker queued.
2950 * Let's make sure that all workers scheduled before disabling the
2951 * tasklet are completed before continuing with the reset.
2952 */
2953 if (engine->i915->guc.preempt_wq)
2954 flush_workqueue(engine->i915->guc.preempt_wq);
2955
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002956 if (engine->irq_seqno_barrier)
2957 engine->irq_seqno_barrier(engine);
2958
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002959 request = i915_gem_find_active_request(engine);
2960 if (request && request->fence.error == -EIO)
2961 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002962
2963 return request;
2964}
2965
Chris Wilson0e178ae2017-01-17 17:59:06 +02002966int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002967{
2968 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00002969 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002970 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002971 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002972
Chris Wilson0e178ae2017-01-17 17:59:06 +02002973 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002974 request = i915_gem_reset_prepare_engine(engine);
2975 if (IS_ERR(request)) {
2976 err = PTR_ERR(request);
2977 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002978 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002979
2980 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002981 }
2982
Chris Wilson4c965542017-01-17 17:59:01 +02002983 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002984
2985 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002986}
2987
Chris Wilsone61e0f52018-02-21 09:56:36 +00002988static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002989{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002990 void *vaddr = request->ring->vaddr;
2991 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002992
Chris Wilson821ed7d2016-09-09 14:11:53 +01002993 /* As this request likely depends on state from the lost
2994 * context, clear out all the user operations leaving the
2995 * breadcrumb at the end (so we get the fence notifications).
2996 */
2997 head = request->head;
2998 if (request->postfix < head) {
2999 memset(vaddr + head, 0, request->ring->size - head);
3000 head = 0;
3001 }
3002 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003003
3004 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003005}
3006
Chris Wilsone61e0f52018-02-21 09:56:36 +00003007static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003008{
3009 struct intel_engine_cs *engine = request->engine;
3010 struct i915_gem_context *hung_ctx = request->ctx;
3011 struct intel_timeline *timeline;
3012 unsigned long flags;
3013
3014 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3015
3016 spin_lock_irqsave(&engine->timeline->lock, flags);
3017 spin_lock(&timeline->lock);
3018
3019 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3020 if (request->ctx == hung_ctx)
3021 skip_request(request);
3022
3023 list_for_each_entry(request, &timeline->requests, link)
3024 skip_request(request);
3025
3026 spin_unlock(&timeline->lock);
3027 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3028}
3029
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003030/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003031static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003032i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +00003033 struct i915_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003034{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003035 /* The guilty request will get skipped on a hung engine.
3036 *
3037 * Users of client default contexts do not rely on logical
3038 * state preserved between batches so it is safe to execute
3039 * queued requests following the hang. Non default contexts
3040 * rely on preserved state, so skipping a batch loses the
3041 * evolution of the state and it needs to be considered corrupted.
3042 * Executing more queued batches on top of corrupted state is
3043 * risky. But we take the risk by trying to advance through
3044 * the queued requests in order to make the client behaviour
3045 * more predictable around resets, by not throwing away random
3046 * amount of batches it has prepared for execution. Sophisticated
3047 * clients can use gem_reset_stats_ioctl and dma fence status
3048 * (exported via sync_file info ioctl on explicit fences) to observe
3049 * when it loses the context state and should rebuild accordingly.
3050 *
3051 * The context ban, and ultimately the client ban, mechanism are safety
3052 * valves if client submission ends up resulting in nothing more than
3053 * subsequent hangs.
3054 */
3055
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003056 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003057 i915_gem_context_mark_guilty(request->ctx);
3058 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003059
3060 /* If this context is now banned, skip all pending requests. */
3061 if (i915_gem_context_is_banned(request->ctx))
3062 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003063 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003064 /*
3065 * Since this is not the hung engine, it may have advanced
3066 * since the hang declaration. Double check by refinding
3067 * the active request at the time of the reset.
3068 */
3069 request = i915_gem_find_active_request(engine);
3070 if (request) {
3071 i915_gem_context_mark_innocent(request->ctx);
3072 dma_fence_set_error(&request->fence, -EAGAIN);
3073
3074 /* Rewind the engine to replay the incomplete rq */
3075 spin_lock_irq(&engine->timeline->lock);
3076 request = list_prev_entry(request, link);
3077 if (&request->link == &engine->timeline->requests)
3078 request = NULL;
3079 spin_unlock_irq(&engine->timeline->lock);
3080 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003081 }
3082
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003083 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003084}
3085
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003086void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +00003087 struct i915_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003088{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003089 /*
3090 * Make sure this write is visible before we re-enable the interrupt
3091 * handlers on another CPU, as tasklet_enable() resolves to just
3092 * a compiler barrier which is insufficient for our purpose here.
3093 */
3094 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003095
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003096 if (request)
3097 request = i915_gem_reset_request(engine, request);
3098
3099 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003100 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3101 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003102 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003103
3104 /* Setup the CS to resume from the breadcrumb of the hung request */
3105 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003106}
3107
Chris Wilsond8027092017-02-08 14:30:32 +00003108void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003109{
3110 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303111 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003112
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003113 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3114
Chris Wilsone61e0f52018-02-21 09:56:36 +00003115 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003116
Chris Wilson2ae55732017-02-12 17:20:02 +00003117 for_each_engine(engine, dev_priv, id) {
3118 struct i915_gem_context *ctx;
3119
Michel Thierryc64992e2017-06-20 10:57:44 +01003120 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003121 ctx = fetch_and_zero(&engine->last_retired_context);
3122 if (ctx)
3123 engine->context_unpin(engine, ctx);
Chris Wilson7b6da812017-12-16 00:03:34 +00003124
3125 /*
3126 * Ostensibily, we always want a context loaded for powersaving,
3127 * so if the engine is idle after the reset, send a request
3128 * to load our scratch kernel_context.
3129 *
3130 * More mysteriously, if we leave the engine idle after a reset,
3131 * the next userspace batch may hang, with what appears to be
3132 * an incoherent read by the CS (presumably stale TLB). An
3133 * empty request appears sufficient to paper over the glitch.
3134 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003135 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003136 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003137
Chris Wilsone61e0f52018-02-21 09:56:36 +00003138 rq = i915_request_alloc(engine,
3139 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003140 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003141 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003142 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003143 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003144
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003145 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003146
3147 if (dev_priv->gt.awake) {
3148 intel_sanitize_gt_powersave(dev_priv);
3149 intel_enable_gt_powersave(dev_priv);
3150 if (INTEL_GEN(dev_priv) >= 6)
3151 gen6_rps_busy(dev_priv);
3152 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003153}
3154
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003155void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3156{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303157 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003158 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003159
3160 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003161}
3162
Chris Wilsond8027092017-02-08 14:30:32 +00003163void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3164{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003165 struct intel_engine_cs *engine;
3166 enum intel_engine_id id;
3167
Chris Wilsond8027092017-02-08 14:30:32 +00003168 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003169
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003170 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003171 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003172 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003173 }
Chris Wilsond8027092017-02-08 14:30:32 +00003174}
3175
Chris Wilsone61e0f52018-02-21 09:56:36 +00003176static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003177{
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003178 dma_fence_set_error(&request->fence, -EIO);
3179
Chris Wilsone61e0f52018-02-21 09:56:36 +00003180 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003181}
3182
Chris Wilsone61e0f52018-02-21 09:56:36 +00003183static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003184{
Chris Wilson8d550822017-10-06 12:56:17 +01003185 unsigned long flags;
3186
Chris Wilson3cd94422017-01-10 17:22:45 +00003187 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003188
3189 spin_lock_irqsave(&request->engine->timeline->lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003190 __i915_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003191 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003192 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003193}
3194
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003195void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003196{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003197 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303198 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003199
Chris Wilson559e0402018-02-05 09:21:59 +00003200 if (drm_debug & DRM_UT_DRIVER) {
3201 struct drm_printer p = drm_debug_printer(__func__);
3202
3203 for_each_engine(engine, i915, id)
3204 intel_engine_dump(engine, &p, "%s\n", engine->name);
3205 }
3206
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003207 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3208 smp_mb__after_atomic();
3209
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003210 /*
3211 * First, stop submission to hw, but do not yet complete requests by
3212 * rolling the global seqno forward (since this would complete requests
3213 * for which we haven't set the fence error to EIO yet).
3214 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003215 for_each_engine(engine, i915, id) {
3216 i915_gem_reset_prepare_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003217 engine->submit_request = nop_submit_request;
Chris Wilson963ddd62018-03-02 11:33:24 +00003218 }
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003219
3220 /*
3221 * Make sure no one is running the old callback before we proceed with
3222 * cancelling requests and resetting the completion tracking. Otherwise
3223 * we might submit a request to the hardware which never completes.
3224 */
3225 synchronize_rcu();
3226
3227 for_each_engine(engine, i915, id) {
3228 /* Mark all executing requests as skipped */
3229 engine->cancel_requests(engine);
3230
3231 /*
3232 * Only once we've force-cancelled all in-flight requests can we
3233 * start to complete all requests.
3234 */
3235 engine->submit_request = nop_complete_submit_request;
Chris Wilson3fed1802018-02-07 21:05:43 +00003236 engine->schedule = NULL;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003237 }
3238
Chris Wilson3fed1802018-02-07 21:05:43 +00003239 i915->caps.scheduler = 0;
3240
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003241 /*
3242 * Make sure no request can slip through without getting completed by
3243 * either this call here to intel_engine_init_global_seqno, or the one
3244 * in nop_complete_submit_request.
3245 */
3246 synchronize_rcu();
3247
3248 for_each_engine(engine, i915, id) {
3249 unsigned long flags;
3250
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003251 /*
3252 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003253 * (lockless) lookup doesn't try and wait upon the request as we
3254 * reset it.
3255 */
3256 spin_lock_irqsave(&engine->timeline->lock, flags);
3257 intel_engine_init_global_seqno(engine,
3258 intel_engine_last_submit(engine));
3259 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003260
3261 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003262 }
Chris Wilson20e49332016-11-22 14:41:21 +00003263
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003264 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003265}
3266
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003267bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3268{
3269 struct i915_gem_timeline *tl;
3270 int i;
3271
3272 lockdep_assert_held(&i915->drm.struct_mutex);
3273 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3274 return true;
3275
3276 /* Before unwedging, make sure that all pending operations
3277 * are flushed and errored out - we may have requests waiting upon
3278 * third party fences. We marked all inflight requests as EIO, and
3279 * every execbuf since returned EIO, for consistency we want all
3280 * the currently pending requests to also be marked as EIO, which
3281 * is done inside our nop_submit_request - and so we must wait.
3282 *
3283 * No more can be submitted until we reset the wedged bit.
3284 */
3285 list_for_each_entry(tl, &i915->gt.timelines, link) {
3286 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003287 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003288
3289 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3290 &i915->drm.struct_mutex);
3291 if (!rq)
3292 continue;
3293
3294 /* We can't use our normal waiter as we want to
3295 * avoid recursively trying to handle the current
3296 * reset. The basic dma_fence_default_wait() installs
3297 * a callback for dma_fence_signal(), which is
3298 * triggered by our nop handler (indirectly, the
3299 * callback enables the signaler thread which is
3300 * woken by the nop_submit_request() advancing the seqno
3301 * and when the seqno passes the fence, the signaler
3302 * then signals the fence waking us up).
3303 */
3304 if (dma_fence_default_wait(&rq->fence, true,
3305 MAX_SCHEDULE_TIMEOUT) < 0)
3306 return false;
3307 }
3308 }
3309
3310 /* Undo nop_submit_request. We prevent all new i915 requests from
3311 * being queued (by disallowing execbuf whilst wedged) so having
3312 * waited for all active requests above, we know the system is idle
3313 * and do not have to worry about a thread being inside
3314 * engine->submit_request() as we swap over. So unlike installing
3315 * the nop_submit_request on reset, we can do this from normal
3316 * context and do not require stop_machine().
3317 */
3318 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003319 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003320
3321 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3322 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3323
3324 return true;
3325}
3326
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003327static void
Eric Anholt673a3942008-07-30 12:06:12 -07003328i915_gem_retire_work_handler(struct work_struct *work)
3329{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003330 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003331 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003332 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003333
Chris Wilson891b48c2010-09-29 12:26:37 +01003334 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003335 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003336 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003337 mutex_unlock(&dev->struct_mutex);
3338 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003339
Chris Wilson88923042018-01-29 14:41:04 +00003340 /*
3341 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003342 * We do not need to do this test under locking as in the worst-case
3343 * we queue the retire worker once too often.
3344 */
Chris Wilson88923042018-01-29 14:41:04 +00003345 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003346 queue_delayed_work(dev_priv->wq,
3347 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003348 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003349}
Chris Wilson891b48c2010-09-29 12:26:37 +01003350
Chris Wilson84a10742018-01-24 11:36:08 +00003351static void shrink_caches(struct drm_i915_private *i915)
3352{
3353 /*
3354 * kmem_cache_shrink() discards empty slabs and reorders partially
3355 * filled slabs to prioritise allocating from the mostly full slabs,
3356 * with the aim of reducing fragmentation.
3357 */
3358 kmem_cache_shrink(i915->priorities);
3359 kmem_cache_shrink(i915->dependencies);
3360 kmem_cache_shrink(i915->requests);
3361 kmem_cache_shrink(i915->luts);
3362 kmem_cache_shrink(i915->vmas);
3363 kmem_cache_shrink(i915->objects);
3364}
3365
3366struct sleep_rcu_work {
3367 union {
3368 struct rcu_head rcu;
3369 struct work_struct work;
3370 };
3371 struct drm_i915_private *i915;
3372 unsigned int epoch;
3373};
3374
3375static inline bool
3376same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3377{
3378 /*
3379 * There is a small chance that the epoch wrapped since we started
3380 * sleeping. If we assume that epoch is at least a u32, then it will
3381 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3382 */
3383 return epoch == READ_ONCE(i915->gt.epoch);
3384}
3385
3386static void __sleep_work(struct work_struct *work)
3387{
3388 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3389 struct drm_i915_private *i915 = s->i915;
3390 unsigned int epoch = s->epoch;
3391
3392 kfree(s);
3393 if (same_epoch(i915, epoch))
3394 shrink_caches(i915);
3395}
3396
3397static void __sleep_rcu(struct rcu_head *rcu)
3398{
3399 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3400 struct drm_i915_private *i915 = s->i915;
3401
3402 if (same_epoch(i915, s->epoch)) {
3403 INIT_WORK(&s->work, __sleep_work);
3404 queue_work(i915->wq, &s->work);
3405 } else {
3406 kfree(s);
3407 }
3408}
3409
Chris Wilson5427f202017-10-23 22:32:34 +01003410static inline bool
3411new_requests_since_last_retire(const struct drm_i915_private *i915)
3412{
3413 return (READ_ONCE(i915->gt.active_requests) ||
3414 work_pending(&i915->gt.idle_work.work));
3415}
3416
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003417static void
3418i915_gem_idle_work_handler(struct work_struct *work)
3419{
3420 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003421 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003422 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003423 bool rearm_hangcheck;
3424
3425 if (!READ_ONCE(dev_priv->gt.awake))
3426 return;
3427
Imre Deak0cb56702016-11-07 11:20:04 +02003428 /*
3429 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003430 * new request is submitted. As we don't trust the hardware, we
3431 * continue on if the wait times out. This is necessary to allow
3432 * the machine to suspend even if the hardware dies, and we will
3433 * try to recover in resume (after depriving the hardware of power,
3434 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003435 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003436 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3437 intel_engines_are_idle(dev_priv),
3438 I915_IDLE_ENGINES_TIMEOUT * 1000,
3439 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003440
3441 rearm_hangcheck =
3442 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3443
Chris Wilson5427f202017-10-23 22:32:34 +01003444 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003445 /* Currently busy, come back later */
3446 mod_delayed_work(dev_priv->wq,
3447 &dev_priv->gt.idle_work,
3448 msecs_to_jiffies(50));
3449 goto out_rearm;
3450 }
3451
Imre Deak93c97dc2016-11-07 11:20:03 +02003452 /*
3453 * New request retired after this work handler started, extend active
3454 * period until next instance of the work.
3455 */
Chris Wilson5427f202017-10-23 22:32:34 +01003456 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003457 goto out_unlock;
3458
Chris Wilson5427f202017-10-23 22:32:34 +01003459 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003460 * Be paranoid and flush a concurrent interrupt to make sure
3461 * we don't reactivate any irq tasklets after parking.
3462 *
3463 * FIXME: Note that even though we have waited for execlists to be idle,
3464 * there may still be an in-flight interrupt even though the CSB
3465 * is now empty. synchronize_irq() makes sure that a residual interrupt
3466 * is completed before we continue, but it doesn't prevent the HW from
3467 * raising a spurious interrupt later. To complete the shield we should
3468 * coordinate disabling the CS irq with flushing the interrupts.
3469 */
3470 synchronize_irq(dev_priv->drm.irq);
3471
Chris Wilsonaba5e272017-10-25 15:39:41 +01003472 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003473 i915_gem_timelines_park(dev_priv);
3474
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003475 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003476
Chris Wilson67d97da2016-07-04 08:08:31 +01003477 GEM_BUG_ON(!dev_priv->gt.awake);
3478 dev_priv->gt.awake = false;
Chris Wilson84a10742018-01-24 11:36:08 +00003479 epoch = dev_priv->gt.epoch;
3480 GEM_BUG_ON(epoch == I915_EPOCH_INVALID);
Chris Wilson67d97da2016-07-04 08:08:31 +01003481 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003482
Chris Wilson67d97da2016-07-04 08:08:31 +01003483 if (INTEL_GEN(dev_priv) >= 6)
3484 gen6_rps_idle(dev_priv);
Tvrtko Ursulinb6876372017-12-05 13:28:54 +00003485
3486 intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ);
3487
Chris Wilson67d97da2016-07-04 08:08:31 +01003488 intel_runtime_pm_put(dev_priv);
3489out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003490 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003491
Chris Wilson67d97da2016-07-04 08:08:31 +01003492out_rearm:
3493 if (rearm_hangcheck) {
3494 GEM_BUG_ON(!dev_priv->gt.awake);
3495 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003496 }
Chris Wilson84a10742018-01-24 11:36:08 +00003497
3498 /*
3499 * When we are idle, it is an opportune time to reap our caches.
3500 * However, we have many objects that utilise RCU and the ordered
3501 * i915->wq that this work is executing on. To try and flush any
3502 * pending frees now we are idle, we first wait for an RCU grace
3503 * period, and then queue a task (that will run last on the wq) to
3504 * shrink and re-optimize the caches.
3505 */
3506 if (same_epoch(dev_priv, epoch)) {
3507 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3508 if (s) {
3509 s->i915 = dev_priv;
3510 s->epoch = epoch;
3511 call_rcu(&s->rcu, __sleep_rcu);
3512 }
3513 }
Eric Anholt673a3942008-07-30 12:06:12 -07003514}
3515
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003516void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3517{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003518 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003519 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3520 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003521 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003522
Chris Wilsond1b48c12017-08-16 09:52:08 +01003523 mutex_lock(&i915->drm.struct_mutex);
3524
3525 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3526 struct i915_gem_context *ctx = lut->ctx;
3527 struct i915_vma *vma;
3528
Chris Wilson432295d2017-08-22 12:05:15 +01003529 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003530 if (ctx->file_priv != fpriv)
3531 continue;
3532
3533 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003534 GEM_BUG_ON(vma->obj != obj);
3535
3536 /* We allow the process to have multiple handles to the same
3537 * vma, in the same fd namespace, by virtue of flink/open.
3538 */
3539 GEM_BUG_ON(!vma->open_count);
3540 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003541 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003542
Chris Wilsond1b48c12017-08-16 09:52:08 +01003543 list_del(&lut->obj_link);
3544 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003545
Chris Wilsond1b48c12017-08-16 09:52:08 +01003546 kmem_cache_free(i915->luts, lut);
3547 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003548 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003549
3550 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003551}
3552
Chris Wilsone95433c2016-10-28 13:58:27 +01003553static unsigned long to_wait_timeout(s64 timeout_ns)
3554{
3555 if (timeout_ns < 0)
3556 return MAX_SCHEDULE_TIMEOUT;
3557
3558 if (timeout_ns == 0)
3559 return 0;
3560
3561 return nsecs_to_jiffies_timeout(timeout_ns);
3562}
3563
Ben Widawsky5816d642012-04-11 11:18:19 -07003564/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003565 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003566 * @dev: drm device pointer
3567 * @data: ioctl data blob
3568 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003569 *
3570 * Returns 0 if successful, else an error is returned with the remaining time in
3571 * the timeout parameter.
3572 * -ETIME: object is still busy after timeout
3573 * -ERESTARTSYS: signal interrupted the wait
3574 * -ENONENT: object doesn't exist
3575 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003576 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003577 * -ENOMEM: damn
3578 * -ENODEV: Internal IRQ fail
3579 * -E?: The add request failed
3580 *
3581 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3582 * non-zero timeout parameter the wait ioctl will wait for the given number of
3583 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3584 * without holding struct_mutex the object may become re-busied before this
3585 * function completes. A similar but shorter * race condition exists in the busy
3586 * ioctl
3587 */
3588int
3589i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3590{
3591 struct drm_i915_gem_wait *args = data;
3592 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003593 ktime_t start;
3594 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003595
Daniel Vetter11b5d512014-09-29 15:31:26 +02003596 if (args->flags != 0)
3597 return -EINVAL;
3598
Chris Wilson03ac0642016-07-20 13:31:51 +01003599 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003600 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003601 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003602
Chris Wilsone95433c2016-10-28 13:58:27 +01003603 start = ktime_get();
3604
3605 ret = i915_gem_object_wait(obj,
3606 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3607 to_wait_timeout(args->timeout_ns),
3608 to_rps_client(file));
3609
3610 if (args->timeout_ns > 0) {
3611 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3612 if (args->timeout_ns < 0)
3613 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003614
3615 /*
3616 * Apparently ktime isn't accurate enough and occasionally has a
3617 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3618 * things up to make the test happy. We allow up to 1 jiffy.
3619 *
3620 * This is a regression from the timespec->ktime conversion.
3621 */
3622 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3623 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003624
3625 /* Asked to wait beyond the jiffie/scheduler precision? */
3626 if (ret == -ETIME && args->timeout_ns)
3627 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003628 }
3629
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003630 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003631 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003632}
3633
Chris Wilson73cb9702016-10-28 13:58:46 +01003634static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003635{
Chris Wilson73cb9702016-10-28 13:58:46 +01003636 int ret, i;
3637
3638 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3639 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3640 if (ret)
3641 return ret;
3642 }
3643
3644 return 0;
3645}
3646
Chris Wilson25112b62017-03-30 15:50:39 +01003647static int wait_for_engines(struct drm_i915_private *i915)
3648{
Chris Wilsonee42c002017-12-11 19:41:34 +00003649 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003650 dev_err(i915->drm.dev,
3651 "Failed to idle engines, declaring wedged!\n");
3652 if (drm_debug & DRM_UT_DRIVER) {
3653 struct drm_printer p = drm_debug_printer(__func__);
3654 struct intel_engine_cs *engine;
3655 enum intel_engine_id id;
3656
3657 for_each_engine(engine, i915, id)
3658 intel_engine_dump(engine, &p,
Chris Wilson9e519bc2018-02-05 10:06:18 +00003659 "%s\n", engine->name);
Chris Wilson59e4b192017-12-11 19:41:35 +00003660 }
3661
Chris Wilsoncad99462017-08-26 12:09:33 +01003662 i915_gem_set_wedged(i915);
3663 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003664 }
3665
3666 return 0;
3667}
3668
Chris Wilson73cb9702016-10-28 13:58:46 +01003669int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3670{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003671 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003672
Chris Wilson863e9fd2017-05-30 13:13:32 +01003673 /* If the device is asleep, we have no requests outstanding */
3674 if (!READ_ONCE(i915->gt.awake))
3675 return 0;
3676
Chris Wilson9caa34a2016-11-11 14:58:08 +00003677 if (flags & I915_WAIT_LOCKED) {
3678 struct i915_gem_timeline *tl;
3679
3680 lockdep_assert_held(&i915->drm.struct_mutex);
3681
3682 list_for_each_entry(tl, &i915->gt.timelines, link) {
3683 ret = wait_for_timeline(tl, flags);
3684 if (ret)
3685 return ret;
3686 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003687 i915_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003688
3689 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003690 } else {
3691 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003692 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003693
Chris Wilson25112b62017-03-30 15:50:39 +01003694 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003695}
3696
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003697static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3698{
Chris Wilsone27ab732017-06-15 13:38:49 +01003699 /*
3700 * We manually flush the CPU domain so that we can override and
3701 * force the flush for the display, and perform it asyncrhonously.
3702 */
3703 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3704 if (obj->cache_dirty)
3705 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003706 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003707}
3708
3709void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3710{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003711 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003712 return;
3713
3714 mutex_lock(&obj->base.dev->struct_mutex);
3715 __i915_gem_object_flush_for_display(obj);
3716 mutex_unlock(&obj->base.dev->struct_mutex);
3717}
3718
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003719/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003720 * Moves a single object to the WC read, and possibly write domain.
3721 * @obj: object to act on
3722 * @write: ask for write access or read only
3723 *
3724 * This function returns when the move is complete, including waiting on
3725 * flushes to occur.
3726 */
3727int
3728i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3729{
3730 int ret;
3731
3732 lockdep_assert_held(&obj->base.dev->struct_mutex);
3733
3734 ret = i915_gem_object_wait(obj,
3735 I915_WAIT_INTERRUPTIBLE |
3736 I915_WAIT_LOCKED |
3737 (write ? I915_WAIT_ALL : 0),
3738 MAX_SCHEDULE_TIMEOUT,
3739 NULL);
3740 if (ret)
3741 return ret;
3742
Christian Königc0a51fd2018-02-16 13:43:38 +01003743 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003744 return 0;
3745
3746 /* Flush and acquire obj->pages so that we are coherent through
3747 * direct access in memory with previous cached writes through
3748 * shmemfs and that our cache domain tracking remains valid.
3749 * For example, if the obj->filp was moved to swap without us
3750 * being notified and releasing the pages, we would mistakenly
3751 * continue to assume that the obj remained out of the CPU cached
3752 * domain.
3753 */
3754 ret = i915_gem_object_pin_pages(obj);
3755 if (ret)
3756 return ret;
3757
3758 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3759
3760 /* Serialise direct access to this object with the barriers for
3761 * coherent writes from the GPU, by effectively invalidating the
3762 * WC domain upon first access.
3763 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003764 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003765 mb();
3766
3767 /* It should now be out of any other write domains, and we can update
3768 * the domain values for our changes.
3769 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003770 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3771 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003772 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003773 obj->read_domains = I915_GEM_DOMAIN_WC;
3774 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003775 obj->mm.dirty = true;
3776 }
3777
3778 i915_gem_object_unpin_pages(obj);
3779 return 0;
3780}
3781
3782/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003783 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003784 * @obj: object to act on
3785 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003786 *
3787 * This function returns when the move is complete, including waiting on
3788 * flushes to occur.
3789 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003790int
Chris Wilson20217462010-11-23 15:26:33 +00003791i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003792{
Eric Anholte47c68e2008-11-14 13:35:19 -08003793 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003794
Chris Wilsone95433c2016-10-28 13:58:27 +01003795 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003796
Chris Wilsone95433c2016-10-28 13:58:27 +01003797 ret = i915_gem_object_wait(obj,
3798 I915_WAIT_INTERRUPTIBLE |
3799 I915_WAIT_LOCKED |
3800 (write ? I915_WAIT_ALL : 0),
3801 MAX_SCHEDULE_TIMEOUT,
3802 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003803 if (ret)
3804 return ret;
3805
Christian Königc0a51fd2018-02-16 13:43:38 +01003806 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003807 return 0;
3808
Chris Wilson43566de2015-01-02 16:29:29 +05303809 /* Flush and acquire obj->pages so that we are coherent through
3810 * direct access in memory with previous cached writes through
3811 * shmemfs and that our cache domain tracking remains valid.
3812 * For example, if the obj->filp was moved to swap without us
3813 * being notified and releasing the pages, we would mistakenly
3814 * continue to assume that the obj remained out of the CPU cached
3815 * domain.
3816 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003817 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303818 if (ret)
3819 return ret;
3820
Chris Wilsonef749212017-04-12 12:01:10 +01003821 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003822
Chris Wilsond0a57782012-10-09 19:24:37 +01003823 /* Serialise direct access to this object with the barriers for
3824 * coherent writes from the GPU, by effectively invalidating the
3825 * GTT domain upon first access.
3826 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003827 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003828 mb();
3829
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003830 /* It should now be out of any other write domains, and we can update
3831 * the domain values for our changes.
3832 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003833 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3834 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003835 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003836 obj->read_domains = I915_GEM_DOMAIN_GTT;
3837 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003838 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003839 }
3840
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003841 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003842 return 0;
3843}
3844
Chris Wilsonef55f922015-10-09 14:11:27 +01003845/**
3846 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003847 * @obj: object to act on
3848 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003849 *
3850 * After this function returns, the object will be in the new cache-level
3851 * across all GTT and the contents of the backing storage will be coherent,
3852 * with respect to the new cache-level. In order to keep the backing storage
3853 * coherent for all users, we only allow a single cache level to be set
3854 * globally on the object and prevent it from being changed whilst the
3855 * hardware is reading from the object. That is if the object is currently
3856 * on the scanout it will be set to uncached (or equivalent display
3857 * cache coherency) and all non-MOCS GPU access will also be uncached so
3858 * that all direct access to the scanout remains coherent.
3859 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003860int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3861 enum i915_cache_level cache_level)
3862{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003863 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003864 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003865
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003866 lockdep_assert_held(&obj->base.dev->struct_mutex);
3867
Chris Wilsone4ffd172011-04-04 09:44:39 +01003868 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003869 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003870
Chris Wilsonef55f922015-10-09 14:11:27 +01003871 /* Inspect the list of currently bound VMA and unbind any that would
3872 * be invalid given the new cache-level. This is principally to
3873 * catch the issue of the CS prefetch crossing page boundaries and
3874 * reading an invalid PTE on older architectures.
3875 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003876restart:
3877 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003878 if (!drm_mm_node_allocated(&vma->node))
3879 continue;
3880
Chris Wilson20dfbde2016-08-04 16:32:30 +01003881 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003882 DRM_DEBUG("can not change the cache level of pinned objects\n");
3883 return -EBUSY;
3884 }
3885
Chris Wilson010e3e62017-12-06 12:49:13 +00003886 if (!i915_vma_is_closed(vma) &&
3887 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003888 continue;
3889
3890 ret = i915_vma_unbind(vma);
3891 if (ret)
3892 return ret;
3893
3894 /* As unbinding may affect other elements in the
3895 * obj->vma_list (due to side-effects from retiring
3896 * an active vma), play safe and restart the iterator.
3897 */
3898 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003899 }
3900
Chris Wilsonef55f922015-10-09 14:11:27 +01003901 /* We can reuse the existing drm_mm nodes but need to change the
3902 * cache-level on the PTE. We could simply unbind them all and
3903 * rebind with the correct cache-level on next use. However since
3904 * we already have a valid slot, dma mapping, pages etc, we may as
3905 * rewrite the PTE in the belief that doing so tramples upon less
3906 * state and so involves less work.
3907 */
Chris Wilson15717de2016-08-04 07:52:26 +01003908 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003909 /* Before we change the PTE, the GPU must not be accessing it.
3910 * If we wait upon the object, we know that all the bound
3911 * VMA are no longer active.
3912 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003913 ret = i915_gem_object_wait(obj,
3914 I915_WAIT_INTERRUPTIBLE |
3915 I915_WAIT_LOCKED |
3916 I915_WAIT_ALL,
3917 MAX_SCHEDULE_TIMEOUT,
3918 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003919 if (ret)
3920 return ret;
3921
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003922 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3923 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003924 /* Access to snoopable pages through the GTT is
3925 * incoherent and on some machines causes a hard
3926 * lockup. Relinquish the CPU mmaping to force
3927 * userspace to refault in the pages and we can
3928 * then double check if the GTT mapping is still
3929 * valid for that pointer access.
3930 */
3931 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003932
Chris Wilsonef55f922015-10-09 14:11:27 +01003933 /* As we no longer need a fence for GTT access,
3934 * we can relinquish it now (and so prevent having
3935 * to steal a fence from someone else on the next
3936 * fence request). Note GPU activity would have
3937 * dropped the fence as all snoopable access is
3938 * supposed to be linear.
3939 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003940 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003941 ret = i915_vma_put_fence(vma);
3942 if (ret)
3943 return ret;
3944 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003945 } else {
3946 /* We either have incoherent backing store and
3947 * so no GTT access or the architecture is fully
3948 * coherent. In such cases, existing GTT mmaps
3949 * ignore the cache bit in the PTE and we can
3950 * rewrite it without confusing the GPU or having
3951 * to force userspace to fault back in its mmaps.
3952 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003953 }
3954
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003955 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003956 if (!drm_mm_node_allocated(&vma->node))
3957 continue;
3958
3959 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3960 if (ret)
3961 return ret;
3962 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003963 }
3964
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003965 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003966 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003967 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003968 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003969
Chris Wilsone4ffd172011-04-04 09:44:39 +01003970 return 0;
3971}
3972
Ben Widawsky199adf42012-09-21 17:01:20 -07003973int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3974 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003975{
Ben Widawsky199adf42012-09-21 17:01:20 -07003976 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003977 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003978 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003979
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003980 rcu_read_lock();
3981 obj = i915_gem_object_lookup_rcu(file, args->handle);
3982 if (!obj) {
3983 err = -ENOENT;
3984 goto out;
3985 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003986
Chris Wilson651d7942013-08-08 14:41:10 +01003987 switch (obj->cache_level) {
3988 case I915_CACHE_LLC:
3989 case I915_CACHE_L3_LLC:
3990 args->caching = I915_CACHING_CACHED;
3991 break;
3992
Chris Wilson4257d3b2013-08-08 14:41:11 +01003993 case I915_CACHE_WT:
3994 args->caching = I915_CACHING_DISPLAY;
3995 break;
3996
Chris Wilson651d7942013-08-08 14:41:10 +01003997 default:
3998 args->caching = I915_CACHING_NONE;
3999 break;
4000 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004001out:
4002 rcu_read_unlock();
4003 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004004}
4005
Ben Widawsky199adf42012-09-21 17:01:20 -07004006int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4007 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004008{
Chris Wilson9c870d02016-10-24 13:42:15 +01004009 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004010 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004011 struct drm_i915_gem_object *obj;
4012 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004013 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004014
Ben Widawsky199adf42012-09-21 17:01:20 -07004015 switch (args->caching) {
4016 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004017 level = I915_CACHE_NONE;
4018 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004019 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004020 /*
4021 * Due to a HW issue on BXT A stepping, GPU stores via a
4022 * snooped mapping may leave stale data in a corresponding CPU
4023 * cacheline, whereas normally such cachelines would get
4024 * invalidated.
4025 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004026 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004027 return -ENODEV;
4028
Chris Wilsone6994ae2012-07-10 10:27:08 +01004029 level = I915_CACHE_LLC;
4030 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004031 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004032 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004033 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004034 default:
4035 return -EINVAL;
4036 }
4037
Chris Wilsond65415d2017-01-19 08:22:10 +00004038 obj = i915_gem_object_lookup(file, args->handle);
4039 if (!obj)
4040 return -ENOENT;
4041
Tina Zhanga03f3952017-11-14 10:25:13 +00004042 /*
4043 * The caching mode of proxy object is handled by its generator, and
4044 * not allowed to be changed by userspace.
4045 */
4046 if (i915_gem_object_is_proxy(obj)) {
4047 ret = -ENXIO;
4048 goto out;
4049 }
4050
Chris Wilsond65415d2017-01-19 08:22:10 +00004051 if (obj->cache_level == level)
4052 goto out;
4053
4054 ret = i915_gem_object_wait(obj,
4055 I915_WAIT_INTERRUPTIBLE,
4056 MAX_SCHEDULE_TIMEOUT,
4057 to_rps_client(file));
4058 if (ret)
4059 goto out;
4060
Ben Widawsky3bc29132012-09-26 16:15:20 -07004061 ret = i915_mutex_lock_interruptible(dev);
4062 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004063 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004064
4065 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004066 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004067
4068out:
4069 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004070 return ret;
4071}
4072
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004073/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004074 * Prepare buffer for display plane (scanout, cursors, etc).
4075 * Can be called from an uninterruptible phase (modesetting) and allows
4076 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004077 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004078struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004079i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4080 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004081 const struct i915_ggtt_view *view,
4082 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004083{
Chris Wilson058d88c2016-08-15 10:49:06 +01004084 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004085 int ret;
4086
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004087 lockdep_assert_held(&obj->base.dev->struct_mutex);
4088
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004089 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004090 * display coherency whilst setting up the cache domains.
4091 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004092 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004093
Eric Anholta7ef0642011-03-29 16:59:54 -07004094 /* The display engine is not coherent with the LLC cache on gen6. As
4095 * a result, we make sure that the pinning that is about to occur is
4096 * done with uncached PTEs. This is lowest common denominator for all
4097 * chipsets.
4098 *
4099 * However for gen6+, we could do better by using the GFDT bit instead
4100 * of uncaching, which would allow us to flush all the LLC-cached data
4101 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4102 */
Chris Wilson651d7942013-08-08 14:41:10 +01004103 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004104 HAS_WT(to_i915(obj->base.dev)) ?
4105 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004106 if (ret) {
4107 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004108 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004109 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004110
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004111 /* As the user may map the buffer once pinned in the display plane
4112 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004113 * always use map_and_fenceable for all scanout buffers. However,
4114 * it may simply be too big to fit into mappable, in which case
4115 * put it anyway and hope that userspace can cope (but always first
4116 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004117 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004118 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004119 if ((flags & PIN_MAPPABLE) == 0 &&
4120 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004121 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004122 flags |
4123 PIN_MAPPABLE |
4124 PIN_NONBLOCK);
4125 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004126 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004127 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004128 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004129
Chris Wilsond8923dc2016-08-18 17:17:07 +01004130 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4131
Chris Wilsona6a7cc42016-11-18 21:17:46 +00004132 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004133 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00004134 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004135
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004136 /* It should now be out of any other write domains, and we can update
4137 * the domain values for our changes.
4138 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004139 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004140
Chris Wilson058d88c2016-08-15 10:49:06 +01004141 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004142
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004143err_unpin_global:
4144 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004145 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004146}
4147
4148void
Chris Wilson058d88c2016-08-15 10:49:06 +01004149i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004150{
Chris Wilson49d73912016-11-29 09:50:08 +00004151 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004152
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004153 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004154 return;
4155
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004156 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004157 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004158
Chris Wilson383d5822016-08-18 17:17:08 +01004159 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004160 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004161
Chris Wilson058d88c2016-08-15 10:49:06 +01004162 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004163}
4164
Eric Anholte47c68e2008-11-14 13:35:19 -08004165/**
4166 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004167 * @obj: object to act on
4168 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004169 *
4170 * This function returns when the move is complete, including waiting on
4171 * flushes to occur.
4172 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004173int
Chris Wilson919926a2010-11-12 13:42:53 +00004174i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004175{
Eric Anholte47c68e2008-11-14 13:35:19 -08004176 int ret;
4177
Chris Wilsone95433c2016-10-28 13:58:27 +01004178 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004179
Chris Wilsone95433c2016-10-28 13:58:27 +01004180 ret = i915_gem_object_wait(obj,
4181 I915_WAIT_INTERRUPTIBLE |
4182 I915_WAIT_LOCKED |
4183 (write ? I915_WAIT_ALL : 0),
4184 MAX_SCHEDULE_TIMEOUT,
4185 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004186 if (ret)
4187 return ret;
4188
Chris Wilsonef749212017-04-12 12:01:10 +01004189 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004190
Eric Anholte47c68e2008-11-14 13:35:19 -08004191 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004192 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004193 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004194 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004195 }
4196
4197 /* It should now be out of any other write domains, and we can update
4198 * the domain values for our changes.
4199 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004200 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004201
4202 /* If we're writing through the CPU, then the GPU read domains will
4203 * need to be invalidated at next use.
4204 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004205 if (write)
4206 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004207
4208 return 0;
4209}
4210
Eric Anholt673a3942008-07-30 12:06:12 -07004211/* Throttle our rendering by waiting until the ring has completed our requests
4212 * emitted over 20 msec ago.
4213 *
Eric Anholtb9624422009-06-03 07:27:35 +00004214 * Note that if we were to use the current jiffies each time around the loop,
4215 * we wouldn't escape the function with any frames outstanding if the time to
4216 * render a frame was over 20ms.
4217 *
Eric Anholt673a3942008-07-30 12:06:12 -07004218 * This should get us reasonable parallelism between CPU and GPU but also
4219 * relatively low latency when blocking on a particular request to finish.
4220 */
4221static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004222i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004223{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004224 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004225 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004226 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004227 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004228 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004229
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004230 /* ABI: return -EIO if already wedged */
4231 if (i915_terminally_wedged(&dev_priv->gpu_error))
4232 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004233
Chris Wilson1c255952010-09-26 11:03:27 +01004234 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004235 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004236 if (time_after_eq(request->emitted_jiffies, recent_enough))
4237 break;
4238
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004239 if (target) {
4240 list_del(&target->client_link);
4241 target->file_priv = NULL;
4242 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004243
John Harrison54fb2412014-11-24 18:49:27 +00004244 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004245 }
John Harrisonff865882014-11-24 18:49:28 +00004246 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004247 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004248 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004249
John Harrison54fb2412014-11-24 18:49:27 +00004250 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004251 return 0;
4252
Chris Wilsone61e0f52018-02-21 09:56:36 +00004253 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004254 I915_WAIT_INTERRUPTIBLE,
4255 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004256 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004257
Chris Wilsone95433c2016-10-28 13:58:27 +01004258 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004259}
4260
Chris Wilson058d88c2016-08-15 10:49:06 +01004261struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004262i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4263 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004264 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004265 u64 alignment,
4266 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004267{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004268 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4269 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004270 struct i915_vma *vma;
4271 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004272
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004273 lockdep_assert_held(&obj->base.dev->struct_mutex);
4274
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004275 if (flags & PIN_MAPPABLE &&
4276 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004277 /* If the required space is larger than the available
4278 * aperture, we will not able to find a slot for the
4279 * object and unbinding the object now will be in
4280 * vain. Worse, doing so may cause us to ping-pong
4281 * the object in and out of the Global GTT and
4282 * waste a lot of cycles under the mutex.
4283 */
4284 if (obj->base.size > dev_priv->ggtt.mappable_end)
4285 return ERR_PTR(-E2BIG);
4286
4287 /* If NONBLOCK is set the caller is optimistically
4288 * trying to cache the full object within the mappable
4289 * aperture, and *must* have a fallback in place for
4290 * situations where we cannot bind the object. We
4291 * can be a little more lax here and use the fallback
4292 * more often to avoid costly migrations of ourselves
4293 * and other objects within the aperture.
4294 *
4295 * Half-the-aperture is used as a simple heuristic.
4296 * More interesting would to do search for a free
4297 * block prior to making the commitment to unbind.
4298 * That caters for the self-harm case, and with a
4299 * little more heuristics (e.g. NOFAULT, NOEVICT)
4300 * we could try to minimise harm to others.
4301 */
4302 if (flags & PIN_NONBLOCK &&
4303 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4304 return ERR_PTR(-ENOSPC);
4305 }
4306
Chris Wilson718659a2017-01-16 15:21:28 +00004307 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004308 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004309 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004310
4311 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004312 if (flags & PIN_NONBLOCK) {
4313 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4314 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004315
Chris Wilson43ae70d2017-10-09 09:44:01 +01004316 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004317 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004318 return ERR_PTR(-ENOSPC);
4319 }
4320
Chris Wilson59bfa122016-08-04 16:32:31 +01004321 WARN(i915_vma_is_pinned(vma),
4322 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004323 " offset=%08x, req.alignment=%llx,"
4324 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4325 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004326 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004327 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004328 ret = i915_vma_unbind(vma);
4329 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004330 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004331 }
4332
Chris Wilson058d88c2016-08-15 10:49:06 +01004333 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4334 if (ret)
4335 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004336
Chris Wilson058d88c2016-08-15 10:49:06 +01004337 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004338}
4339
Chris Wilsonedf6b762016-08-09 09:23:33 +01004340static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004341{
4342 /* Note that we could alias engines in the execbuf API, but
4343 * that would be very unwise as it prevents userspace from
4344 * fine control over engine selection. Ahem.
4345 *
4346 * This should be something like EXEC_MAX_ENGINE instead of
4347 * I915_NUM_ENGINES.
4348 */
4349 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4350 return 0x10000 << id;
4351}
4352
4353static __always_inline unsigned int __busy_write_id(unsigned int id)
4354{
Chris Wilson70cb4722016-08-09 18:08:25 +01004355 /* The uABI guarantees an active writer is also amongst the read
4356 * engines. This would be true if we accessed the activity tracking
4357 * under the lock, but as we perform the lookup of the object and
4358 * its activity locklessly we can not guarantee that the last_write
4359 * being active implies that we have set the same engine flag from
4360 * last_read - hence we always set both read and write busy for
4361 * last_write.
4362 */
4363 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004364}
4365
Chris Wilsonedf6b762016-08-09 09:23:33 +01004366static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004367__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004368 unsigned int (*flag)(unsigned int id))
4369{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004370 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004371
Chris Wilsond07f0e52016-10-28 13:58:44 +01004372 /* We have to check the current hw status of the fence as the uABI
4373 * guarantees forward progress. We could rely on the idle worker
4374 * to eventually flush us, but to minimise latency just ask the
4375 * hardware.
4376 *
4377 * Note we only report on the status of native fences.
4378 */
4379 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004380 return 0;
4381
Chris Wilsond07f0e52016-10-28 13:58:44 +01004382 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004383 rq = container_of(fence, struct i915_request, fence);
4384 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004385 return 0;
4386
Chris Wilson1d39f282017-04-11 13:43:06 +01004387 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004388}
4389
Chris Wilsonedf6b762016-08-09 09:23:33 +01004390static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004391busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004392{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004393 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004394}
4395
Chris Wilsonedf6b762016-08-09 09:23:33 +01004396static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004397busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004398{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004399 if (!fence)
4400 return 0;
4401
4402 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004403}
4404
Eric Anholt673a3942008-07-30 12:06:12 -07004405int
Eric Anholt673a3942008-07-30 12:06:12 -07004406i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004407 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004408{
4409 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004410 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004411 struct reservation_object_list *list;
4412 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004413 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004414
Chris Wilsond07f0e52016-10-28 13:58:44 +01004415 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004416 rcu_read_lock();
4417 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004418 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004419 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004420
4421 /* A discrepancy here is that we do not report the status of
4422 * non-i915 fences, i.e. even though we may report the object as idle,
4423 * a call to set-domain may still stall waiting for foreign rendering.
4424 * This also means that wait-ioctl may report an object as busy,
4425 * where busy-ioctl considers it idle.
4426 *
4427 * We trade the ability to warn of foreign fences to report on which
4428 * i915 engines are active for the object.
4429 *
4430 * Alternatively, we can trade that extra information on read/write
4431 * activity with
4432 * args->busy =
4433 * !reservation_object_test_signaled_rcu(obj->resv, true);
4434 * to report the overall busyness. This is what the wait-ioctl does.
4435 *
4436 */
4437retry:
4438 seq = raw_read_seqcount(&obj->resv->seq);
4439
4440 /* Translate the exclusive fence to the READ *and* WRITE engine */
4441 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4442
4443 /* Translate shared fences to READ set of engines */
4444 list = rcu_dereference(obj->resv->fence);
4445 if (list) {
4446 unsigned int shared_count = list->shared_count, i;
4447
4448 for (i = 0; i < shared_count; ++i) {
4449 struct dma_fence *fence =
4450 rcu_dereference(list->shared[i]);
4451
4452 args->busy |= busy_check_reader(fence);
4453 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004454 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004455
Chris Wilsond07f0e52016-10-28 13:58:44 +01004456 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4457 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004458
Chris Wilsond07f0e52016-10-28 13:58:44 +01004459 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004460out:
4461 rcu_read_unlock();
4462 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004463}
4464
4465int
4466i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4467 struct drm_file *file_priv)
4468{
Akshay Joshi0206e352011-08-16 15:34:10 -04004469 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004470}
4471
Chris Wilson3ef94da2009-09-14 16:50:29 +01004472int
4473i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4474 struct drm_file *file_priv)
4475{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004476 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004477 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004478 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004479 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004480
4481 switch (args->madv) {
4482 case I915_MADV_DONTNEED:
4483 case I915_MADV_WILLNEED:
4484 break;
4485 default:
4486 return -EINVAL;
4487 }
4488
Chris Wilson03ac0642016-07-20 13:31:51 +01004489 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004490 if (!obj)
4491 return -ENOENT;
4492
4493 err = mutex_lock_interruptible(&obj->mm.lock);
4494 if (err)
4495 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004496
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004497 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004498 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004499 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004500 if (obj->mm.madv == I915_MADV_WILLNEED) {
4501 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004502 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004503 obj->mm.quirked = false;
4504 }
4505 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004506 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004507 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004508 obj->mm.quirked = true;
4509 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004510 }
4511
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004512 if (obj->mm.madv != __I915_MADV_PURGED)
4513 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004514
Chris Wilson6c085a72012-08-20 11:40:46 +02004515 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004516 if (obj->mm.madv == I915_MADV_DONTNEED &&
4517 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004518 i915_gem_object_truncate(obj);
4519
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004520 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004521 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004522
Chris Wilson1233e2d2016-10-28 13:58:37 +01004523out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004524 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004525 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004526}
4527
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004528static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004529frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004530{
4531 struct drm_i915_gem_object *obj =
4532 container_of(active, typeof(*obj), frontbuffer_write);
4533
Chris Wilsond59b21e2017-02-22 11:40:49 +00004534 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004535}
4536
Chris Wilson37e680a2012-06-07 15:38:42 +01004537void i915_gem_object_init(struct drm_i915_gem_object *obj,
4538 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004539{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004540 mutex_init(&obj->mm.lock);
4541
Ben Widawsky2f633152013-07-17 12:19:03 -07004542 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004543 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004544 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004545
Chris Wilson37e680a2012-06-07 15:38:42 +01004546 obj->ops = ops;
4547
Chris Wilsond07f0e52016-10-28 13:58:44 +01004548 reservation_object_init(&obj->__builtin_resv);
4549 obj->resv = &obj->__builtin_resv;
4550
Chris Wilson50349242016-08-18 17:17:04 +01004551 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004552 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004553
4554 obj->mm.madv = I915_MADV_WILLNEED;
4555 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4556 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004557
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004558 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004559}
4560
Chris Wilson37e680a2012-06-07 15:38:42 +01004561static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004562 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4563 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004564
Chris Wilson37e680a2012-06-07 15:38:42 +01004565 .get_pages = i915_gem_object_get_pages_gtt,
4566 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004567
4568 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004569};
4570
Matthew Auld465c4032017-10-06 23:18:14 +01004571static int i915_gem_object_create_shmem(struct drm_device *dev,
4572 struct drm_gem_object *obj,
4573 size_t size)
4574{
4575 struct drm_i915_private *i915 = to_i915(dev);
4576 unsigned long flags = VM_NORESERVE;
4577 struct file *filp;
4578
4579 drm_gem_private_object_init(dev, obj, size);
4580
4581 if (i915->mm.gemfs)
4582 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4583 flags);
4584 else
4585 filp = shmem_file_setup("i915", size, flags);
4586
4587 if (IS_ERR(filp))
4588 return PTR_ERR(filp);
4589
4590 obj->filp = filp;
4591
4592 return 0;
4593}
4594
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004595struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004596i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004597{
Daniel Vetterc397b902010-04-09 19:05:07 +00004598 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004599 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004600 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004601 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004602 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004603
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004604 /* There is a prevalence of the assumption that we fit the object's
4605 * page count inside a 32bit _signed_ variable. Let's document this and
4606 * catch if we ever need to fix it. In the meantime, if you do spot
4607 * such a local variable, please consider fixing!
4608 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004609 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004610 return ERR_PTR(-E2BIG);
4611
4612 if (overflows_type(size, obj->base.size))
4613 return ERR_PTR(-E2BIG);
4614
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004615 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004616 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004617 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004618
Matthew Auld465c4032017-10-06 23:18:14 +01004619 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004620 if (ret)
4621 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004622
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004623 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004624 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004625 /* 965gm cannot relocate objects above 4GiB. */
4626 mask &= ~__GFP_HIGHMEM;
4627 mask |= __GFP_DMA32;
4628 }
4629
Al Viro93c76a32015-12-04 23:45:44 -05004630 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004631 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004632 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004633
Chris Wilson37e680a2012-06-07 15:38:42 +01004634 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004635
Christian Königc0a51fd2018-02-16 13:43:38 +01004636 obj->write_domain = I915_GEM_DOMAIN_CPU;
4637 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004638
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004639 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004640 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004641 * cache) for about a 10% performance improvement
4642 * compared to uncached. Graphics requests other than
4643 * display scanout are coherent with the CPU in
4644 * accessing this cache. This means in this mode we
4645 * don't need to clflush on the CPU side, and on the
4646 * GPU side we only need to flush internal caches to
4647 * get data visible to the CPU.
4648 *
4649 * However, we maintain the display planes as UC, and so
4650 * need to rebind when first used as such.
4651 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004652 cache_level = I915_CACHE_LLC;
4653 else
4654 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004655
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004656 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004657
Daniel Vetterd861e332013-07-24 23:25:03 +02004658 trace_i915_gem_object_create(obj);
4659
Chris Wilson05394f32010-11-08 19:18:58 +00004660 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004661
4662fail:
4663 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004664 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004665}
4666
Chris Wilson340fbd82014-05-22 09:16:52 +01004667static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4668{
4669 /* If we are the last user of the backing storage (be it shmemfs
4670 * pages or stolen etc), we know that the pages are going to be
4671 * immediately released. In this case, we can then skip copying
4672 * back the contents from the GPU.
4673 */
4674
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004675 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004676 return false;
4677
4678 if (obj->base.filp == NULL)
4679 return true;
4680
4681 /* At first glance, this looks racy, but then again so would be
4682 * userspace racing mmap against close. However, the first external
4683 * reference to the filp can only be obtained through the
4684 * i915_gem_mmap_ioctl() which safeguards us against the user
4685 * acquiring such a reference whilst we are in the middle of
4686 * freeing the object.
4687 */
4688 return atomic_long_read(&obj->base.filp->f_count) == 1;
4689}
4690
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004691static void __i915_gem_free_objects(struct drm_i915_private *i915,
4692 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004693{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004694 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004695
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004696 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004697 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004698 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004699
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004700 trace_i915_gem_object_destroy(obj);
4701
Chris Wilsoncc731f52017-10-13 21:26:21 +01004702 mutex_lock(&i915->drm.struct_mutex);
4703
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004704 GEM_BUG_ON(i915_gem_object_is_active(obj));
4705 list_for_each_entry_safe(vma, vn,
4706 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004707 GEM_BUG_ON(i915_vma_is_active(vma));
4708 vma->flags &= ~I915_VMA_PIN_MASK;
4709 i915_vma_close(vma);
4710 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004711 GEM_BUG_ON(!list_empty(&obj->vma_list));
4712 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004713
Chris Wilsonf2123812017-10-16 12:40:37 +01004714 /* This serializes freeing with the shrinker. Since the free
4715 * is delayed, first by RCU then by the workqueue, we want the
4716 * shrinker to be able to free pages of unreferenced objects,
4717 * or else we may oom whilst there are plenty of deferred
4718 * freed objects.
4719 */
4720 if (i915_gem_object_has_pages(obj)) {
4721 spin_lock(&i915->mm.obj_lock);
4722 list_del_init(&obj->mm.link);
4723 spin_unlock(&i915->mm.obj_lock);
4724 }
4725
Chris Wilsoncc731f52017-10-13 21:26:21 +01004726 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004727
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004728 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004729 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004730 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004731 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004732
4733 if (obj->ops->release)
4734 obj->ops->release(obj);
4735
4736 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4737 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004738 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004739 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004740
4741 if (obj->base.import_attach)
4742 drm_prime_gem_destroy(&obj->base, NULL);
4743
Chris Wilsond07f0e52016-10-28 13:58:44 +01004744 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004745 drm_gem_object_release(&obj->base);
4746 i915_gem_info_remove_obj(i915, obj->base.size);
4747
4748 kfree(obj->bit_17);
4749 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004750
Chris Wilsonc9c70472018-02-19 22:06:31 +00004751 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4752 atomic_dec(&i915->mm.free_count);
4753
Chris Wilsoncc731f52017-10-13 21:26:21 +01004754 if (on)
4755 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004756 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004757 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004758}
4759
4760static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4761{
4762 struct llist_node *freed;
4763
Chris Wilson87701b42017-10-13 21:26:20 +01004764 /* Free the oldest, most stale object to keep the free_list short */
4765 freed = NULL;
4766 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4767 /* Only one consumer of llist_del_first() allowed */
4768 spin_lock(&i915->mm.free_lock);
4769 freed = llist_del_first(&i915->mm.free_list);
4770 spin_unlock(&i915->mm.free_lock);
4771 }
4772 if (unlikely(freed)) {
4773 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004774 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004775 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004776}
4777
4778static void __i915_gem_free_work(struct work_struct *work)
4779{
4780 struct drm_i915_private *i915 =
4781 container_of(work, struct drm_i915_private, mm.free_work);
4782 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004783
Chris Wilson2ef1e722018-01-15 20:57:59 +00004784 /*
4785 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004786 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4787 * However, the object may also be bound into the global GTT (e.g.
4788 * older GPUs without per-process support, or for direct access through
4789 * the GTT either for the user or for scanout). Those VMA still need to
4790 * unbound now.
4791 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004792
Chris Wilsonf991c492017-11-06 11:15:08 +00004793 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004794 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004795 spin_unlock(&i915->mm.free_lock);
4796
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004797 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004798 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004799 return;
4800
4801 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004802 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004803 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004804}
4805
4806static void __i915_gem_free_object_rcu(struct rcu_head *head)
4807{
4808 struct drm_i915_gem_object *obj =
4809 container_of(head, typeof(*obj), rcu);
4810 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4811
Chris Wilson2ef1e722018-01-15 20:57:59 +00004812 /*
4813 * Since we require blocking on struct_mutex to unbind the freed
4814 * object from the GPU before releasing resources back to the
4815 * system, we can not do that directly from the RCU callback (which may
4816 * be a softirq context), but must instead then defer that work onto a
4817 * kthread. We use the RCU callback rather than move the freed object
4818 * directly onto the work queue so that we can mix between using the
4819 * worker and performing frees directly from subsequent allocations for
4820 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004821 */
4822 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004823 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004824}
4825
4826void i915_gem_free_object(struct drm_gem_object *gem_obj)
4827{
4828 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4829
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004830 if (obj->mm.quirked)
4831 __i915_gem_object_unpin_pages(obj);
4832
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004833 if (discard_backing_storage(obj))
4834 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004835
Chris Wilson2ef1e722018-01-15 20:57:59 +00004836 /*
4837 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004838 * read-side critical sections are complete, e.g.
4839 * i915_gem_busy_ioctl(). For the corresponding synchronized
4840 * lookup see i915_gem_object_lookup_rcu().
4841 */
Chris Wilsonc9c70472018-02-19 22:06:31 +00004842 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004843 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004844}
4845
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004846void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4847{
4848 lockdep_assert_held(&obj->base.dev->struct_mutex);
4849
Chris Wilsond1b48c12017-08-16 09:52:08 +01004850 if (!i915_gem_object_has_active_reference(obj) &&
4851 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004852 i915_gem_object_set_active_reference(obj);
4853 else
4854 i915_gem_object_put(obj);
4855}
4856
Chris Wilsonae6c4572017-11-10 14:26:28 +00004857static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004858{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004859 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004860 struct intel_engine_cs *engine;
4861 enum intel_engine_id id;
4862
Chris Wilsonae6c4572017-11-10 14:26:28 +00004863 for_each_engine(engine, i915, id) {
4864 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4865 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4866 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004867}
4868
Chris Wilson24145512017-01-24 11:01:35 +00004869void i915_gem_sanitize(struct drm_i915_private *i915)
4870{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004871 if (i915_terminally_wedged(&i915->gpu_error)) {
4872 mutex_lock(&i915->drm.struct_mutex);
4873 i915_gem_unset_wedged(i915);
4874 mutex_unlock(&i915->drm.struct_mutex);
4875 }
4876
Chris Wilson24145512017-01-24 11:01:35 +00004877 /*
4878 * If we inherit context state from the BIOS or earlier occupants
4879 * of the GPU, the GPU may be in an inconsistent state when we
4880 * try to take over. The only way to remove the earlier state
4881 * is by resetting. However, resetting on earlier gen is tricky as
4882 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004883 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004884 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004885 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4886 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004887}
4888
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004889int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004890{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004891 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004892 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004893
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004894 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004895 intel_suspend_gt_powersave(dev_priv);
4896
Chris Wilson45c5f202013-10-16 11:50:01 +01004897 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004898
4899 /* We have to flush all the executing contexts to main memory so
4900 * that they can saved in the hibernation image. To ensure the last
4901 * context image is coherent, we have to switch away from it. That
4902 * leaves the dev_priv->kernel_context still active when
4903 * we actually suspend, and its image in memory may not match the GPU
4904 * state. Fortunately, the kernel_context is disposable and we do
4905 * not rely on its state.
4906 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004907 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4908 ret = i915_gem_switch_to_kernel_context(dev_priv);
4909 if (ret)
4910 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004911
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004912 ret = i915_gem_wait_for_idle(dev_priv,
4913 I915_WAIT_INTERRUPTIBLE |
4914 I915_WAIT_LOCKED);
4915 if (ret && ret != -EIO)
4916 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004917
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004918 assert_kernel_context_is_current(dev_priv);
4919 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004920 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004921 mutex_unlock(&dev->struct_mutex);
4922
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304923 intel_guc_suspend(dev_priv);
4924
Chris Wilson737b1502015-01-26 18:03:03 +02004925 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004926 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004927
4928 /* As the idle_work is rearming if it detects a race, play safe and
4929 * repeat the flush until it is definitely idle.
4930 */
Chris Wilson7c262402017-10-06 11:40:38 +01004931 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004932
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004933 /* Assert that we sucessfully flushed all the work and
4934 * reset the GPU back to its idle, low power state.
4935 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004936 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004937 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4938 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004939
Imre Deak1c777c52016-10-12 17:46:37 +03004940 /*
4941 * Neither the BIOS, ourselves or any other kernel
4942 * expects the system to be in execlists mode on startup,
4943 * so we need to reset the GPU back to legacy mode. And the only
4944 * known way to disable logical contexts is through a GPU reset.
4945 *
4946 * So in order to leave the system in a known default configuration,
4947 * always reset the GPU upon unload and suspend. Afterwards we then
4948 * clean up the GEM state tracking, flushing off the requests and
4949 * leaving the system in a known idle state.
4950 *
4951 * Note that is of the upmost importance that the GPU is idle and
4952 * all stray writes are flushed *before* we dismantle the backing
4953 * storage for the pinned objects.
4954 *
4955 * However, since we are uncertain that resetting the GPU on older
4956 * machines is a good idea, we don't - just in case it leaves the
4957 * machine in an unusable condition.
4958 */
Chris Wilson24145512017-01-24 11:01:35 +00004959 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004960
4961 intel_runtime_pm_put(dev_priv);
4962 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004963
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004964err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004965 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004966 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004967 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004968}
4969
Chris Wilson37cd3302017-11-12 11:27:38 +00004970void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004971{
Chris Wilson37cd3302017-11-12 11:27:38 +00004972 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004973
Chris Wilson37cd3302017-11-12 11:27:38 +00004974 mutex_lock(&i915->drm.struct_mutex);
4975 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004976
Chris Wilson37cd3302017-11-12 11:27:38 +00004977 i915_gem_restore_gtt_mappings(i915);
4978 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004979
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004980 /*
4981 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004982 * guarantee that the context image is complete. So let's just reset
4983 * it and start again.
4984 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004985 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004986
Chris Wilson37cd3302017-11-12 11:27:38 +00004987 if (i915_gem_init_hw(i915))
4988 goto err_wedged;
4989
Chris Wilson7469c622017-11-14 13:03:00 +00004990 intel_guc_resume(i915);
4991
Chris Wilson37cd3302017-11-12 11:27:38 +00004992 /* Always reload a context for powersaving. */
4993 if (i915_gem_switch_to_kernel_context(i915))
4994 goto err_wedged;
4995
4996out_unlock:
4997 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4998 mutex_unlock(&i915->drm.struct_mutex);
4999 return;
5000
5001err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005002 if (!i915_terminally_wedged(&i915->gpu_error)) {
5003 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5004 i915_gem_set_wedged(i915);
5005 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005006 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005007}
5008
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005009void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005010{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005011 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005012 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5013 return;
5014
5015 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5016 DISP_TILE_SURFACE_SWIZZLING);
5017
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005018 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005019 return;
5020
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005021 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005022 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005023 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005024 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005025 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005026 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005027 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005028 else
5029 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005030}
Daniel Vettere21af882012-02-09 20:53:27 +01005031
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005032static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005033{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005034 I915_WRITE(RING_CTL(base), 0);
5035 I915_WRITE(RING_HEAD(base), 0);
5036 I915_WRITE(RING_TAIL(base), 0);
5037 I915_WRITE(RING_START(base), 0);
5038}
5039
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005040static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005041{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005042 if (IS_I830(dev_priv)) {
5043 init_unused_ring(dev_priv, PRB1_BASE);
5044 init_unused_ring(dev_priv, SRB0_BASE);
5045 init_unused_ring(dev_priv, SRB1_BASE);
5046 init_unused_ring(dev_priv, SRB2_BASE);
5047 init_unused_ring(dev_priv, SRB3_BASE);
5048 } else if (IS_GEN2(dev_priv)) {
5049 init_unused_ring(dev_priv, SRB0_BASE);
5050 init_unused_ring(dev_priv, SRB1_BASE);
5051 } else if (IS_GEN3(dev_priv)) {
5052 init_unused_ring(dev_priv, PRB1_BASE);
5053 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005054 }
5055}
5056
Chris Wilson20a8a742017-02-08 14:30:31 +00005057static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005058{
Chris Wilson20a8a742017-02-08 14:30:31 +00005059 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005060 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305061 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005062 int err;
5063
5064 for_each_engine(engine, i915, id) {
5065 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005066 if (err) {
5067 DRM_ERROR("Failed to restart %s (%d)\n",
5068 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005069 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005070 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005071 }
5072
5073 return 0;
5074}
5075
5076int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5077{
Chris Wilsond200cda2016-04-28 09:56:44 +01005078 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005079
Chris Wilsonde867c22016-10-25 13:16:02 +01005080 dev_priv->gt.last_init_time = ktime_get();
5081
Chris Wilson5e4f5182015-02-13 14:35:59 +00005082 /* Double layer security blanket, see i915_gem_init() */
5083 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5084
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005085 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005086 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005087
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005088 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005089 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005090 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005091
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005092 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005093 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005094 u32 temp = I915_READ(GEN7_MSG_CTL);
5095 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5096 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005097 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005098 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5099 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5100 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5101 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005102 }
5103
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005104 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005105
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005106 /*
5107 * At least 830 can leave some of the unused rings
5108 * "active" (ie. head != tail) after resume which
5109 * will prevent c3 entry. Makes sure all unused rings
5110 * are totally idle.
5111 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005112 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005113
Dave Gordoned54c1a2016-01-19 19:02:54 +00005114 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005115 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5116 ret = -EIO;
5117 goto out;
5118 }
John Harrison90638cc2015-05-29 17:43:37 +01005119
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005120 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005121 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005122 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005123 goto out;
5124 }
5125
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005126 /* We can't enable contexts until all firmware is loaded */
5127 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005128 if (ret) {
5129 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005130 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005131 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005132
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005133 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005134
Chris Wilson136109c2017-11-02 13:14:30 +00005135 /* Only when the HW is re-initialised, can we replay the requests */
5136 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005137out:
5138 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005139 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005140}
5141
Chris Wilsond2b4b972017-11-10 14:26:33 +00005142static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5143{
5144 struct i915_gem_context *ctx;
5145 struct intel_engine_cs *engine;
5146 enum intel_engine_id id;
5147 int err;
5148
5149 /*
5150 * As we reset the gpu during very early sanitisation, the current
5151 * register state on the GPU should reflect its defaults values.
5152 * We load a context onto the hw (with restore-inhibit), then switch
5153 * over to a second context to save that default register state. We
5154 * can then prime every new context with that state so they all start
5155 * from the same default HW values.
5156 */
5157
5158 ctx = i915_gem_context_create_kernel(i915, 0);
5159 if (IS_ERR(ctx))
5160 return PTR_ERR(ctx);
5161
5162 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005163 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005164
Chris Wilsone61e0f52018-02-21 09:56:36 +00005165 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005166 if (IS_ERR(rq)) {
5167 err = PTR_ERR(rq);
5168 goto out_ctx;
5169 }
5170
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005171 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005172 if (engine->init_context)
5173 err = engine->init_context(rq);
5174
Chris Wilsone61e0f52018-02-21 09:56:36 +00005175 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005176 if (err)
5177 goto err_active;
5178 }
5179
5180 err = i915_gem_switch_to_kernel_context(i915);
5181 if (err)
5182 goto err_active;
5183
5184 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5185 if (err)
5186 goto err_active;
5187
5188 assert_kernel_context_is_current(i915);
5189
5190 for_each_engine(engine, i915, id) {
5191 struct i915_vma *state;
5192
5193 state = ctx->engine[id].state;
5194 if (!state)
5195 continue;
5196
5197 /*
5198 * As we will hold a reference to the logical state, it will
5199 * not be torn down with the context, and importantly the
5200 * object will hold onto its vma (making it possible for a
5201 * stray GTT write to corrupt our defaults). Unmap the vma
5202 * from the GTT to prevent such accidents and reclaim the
5203 * space.
5204 */
5205 err = i915_vma_unbind(state);
5206 if (err)
5207 goto err_active;
5208
5209 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5210 if (err)
5211 goto err_active;
5212
5213 engine->default_state = i915_gem_object_get(state->obj);
5214 }
5215
5216 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5217 unsigned int found = intel_engines_has_context_isolation(i915);
5218
5219 /*
5220 * Make sure that classes with multiple engine instances all
5221 * share the same basic configuration.
5222 */
5223 for_each_engine(engine, i915, id) {
5224 unsigned int bit = BIT(engine->uabi_class);
5225 unsigned int expected = engine->default_state ? bit : 0;
5226
5227 if ((found & bit) != expected) {
5228 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5229 engine->uabi_class, engine->name);
5230 }
5231 }
5232 }
5233
5234out_ctx:
5235 i915_gem_context_set_closed(ctx);
5236 i915_gem_context_put(ctx);
5237 return err;
5238
5239err_active:
5240 /*
5241 * If we have to abandon now, we expect the engines to be idle
5242 * and ready to be torn-down. First try to flush any remaining
5243 * request, ensure we are pointing at the kernel context and
5244 * then remove it.
5245 */
5246 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5247 goto out_ctx;
5248
5249 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5250 goto out_ctx;
5251
5252 i915_gem_contexts_lost(i915);
5253 goto out_ctx;
5254}
5255
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005256int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005257{
Chris Wilson1070a422012-04-24 15:47:41 +01005258 int ret;
5259
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005260 /*
5261 * We need to fallback to 4K pages since gvt gtt handling doesn't
5262 * support huge page entries - we will need to check either hypervisor
5263 * mm can support huge guest page or just do emulation in gvt.
5264 */
5265 if (intel_vgpu_active(dev_priv))
5266 mkwrite_device_info(dev_priv)->page_sizes =
5267 I915_GTT_PAGE_SIZE_4K;
5268
Chris Wilson94312822017-05-03 10:39:18 +01005269 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005270
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005271 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005272 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005273 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005274 } else {
5275 dev_priv->gt.resume = intel_legacy_submission_resume;
5276 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005277 }
5278
Chris Wilsonee487002017-11-22 17:26:21 +00005279 ret = i915_gem_init_userptr(dev_priv);
5280 if (ret)
5281 return ret;
5282
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305283 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005284 if (ret)
5285 return ret;
5286
Chris Wilson5e4f5182015-02-13 14:35:59 +00005287 /* This is just a security blanket to placate dragons.
5288 * On some systems, we very sporadically observe that the first TLBs
5289 * used by the CS may be stale, despite us poking the TLB reset. If
5290 * we hold the forcewake during initialisation these problems
5291 * just magically go away.
5292 */
Chris Wilsonee487002017-11-22 17:26:21 +00005293 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005294 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5295
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005296 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005297 if (ret) {
5298 GEM_BUG_ON(ret == -EIO);
5299 goto err_unlock;
5300 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005301
Chris Wilson829a0af2017-06-20 12:05:45 +01005302 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005303 if (ret) {
5304 GEM_BUG_ON(ret == -EIO);
5305 goto err_ggtt;
5306 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005307
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005308 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005309 if (ret) {
5310 GEM_BUG_ON(ret == -EIO);
5311 goto err_context;
5312 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005313
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005314 intel_init_gt_powersave(dev_priv);
5315
Michał Winiarski61b5c152017-12-13 23:13:48 +01005316 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005317 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005318 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005319
Michał Winiarski61b5c152017-12-13 23:13:48 +01005320 ret = i915_gem_init_hw(dev_priv);
5321 if (ret)
5322 goto err_uc_init;
5323
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005324 /*
5325 * Despite its name intel_init_clock_gating applies both display
5326 * clock gating workarounds; GT mmio workarounds and the occasional
5327 * GT power context workaround. Worse, sometimes it includes a context
5328 * register workaround which we need to apply before we record the
5329 * default HW state for all contexts.
5330 *
5331 * FIXME: break up the workarounds and apply them at the right time!
5332 */
5333 intel_init_clock_gating(dev_priv);
5334
Chris Wilsond2b4b972017-11-10 14:26:33 +00005335 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005336 if (ret)
5337 goto err_init_hw;
5338
5339 if (i915_inject_load_failure()) {
5340 ret = -ENODEV;
5341 goto err_init_hw;
5342 }
5343
5344 if (i915_inject_load_failure()) {
5345 ret = -EIO;
5346 goto err_init_hw;
5347 }
5348
5349 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5350 mutex_unlock(&dev_priv->drm.struct_mutex);
5351
5352 return 0;
5353
5354 /*
5355 * Unwinding is complicated by that we want to handle -EIO to mean
5356 * disable GPU submission but keep KMS alive. We want to mark the
5357 * HW as irrevisibly wedged, but keep enough state around that the
5358 * driver doesn't explode during runtime.
5359 */
5360err_init_hw:
5361 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5362 i915_gem_contexts_lost(dev_priv);
5363 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005364err_uc_init:
5365 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005366err_pm:
5367 if (ret != -EIO) {
5368 intel_cleanup_gt_powersave(dev_priv);
5369 i915_gem_cleanup_engines(dev_priv);
5370 }
5371err_context:
5372 if (ret != -EIO)
5373 i915_gem_contexts_fini(dev_priv);
5374err_ggtt:
5375err_unlock:
5376 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5377 mutex_unlock(&dev_priv->drm.struct_mutex);
5378
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305379 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305380
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005381 if (ret != -EIO)
5382 i915_gem_cleanup_userptr(dev_priv);
5383
Chris Wilson60990322014-04-09 09:19:42 +01005384 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005385 /*
5386 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005387 * wedged. But we only want to do this where the GPU is angry,
5388 * for all other failure, such as an allocation failure, bail.
5389 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005390 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5391 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5392 i915_gem_set_wedged(dev_priv);
5393 }
Chris Wilson60990322014-04-09 09:19:42 +01005394 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005395 }
5396
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005397 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005398 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005399}
5400
Chris Wilson24145512017-01-24 11:01:35 +00005401void i915_gem_init_mmio(struct drm_i915_private *i915)
5402{
5403 i915_gem_sanitize(i915);
5404}
5405
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005406void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005407i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005408{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005409 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305410 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005411
Akash Goel3b3f1652016-10-13 22:44:48 +05305412 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005413 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005414}
5415
Eric Anholt673a3942008-07-30 12:06:12 -07005416void
Imre Deak40ae4e12016-03-16 14:54:03 +02005417i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5418{
Chris Wilson49ef5292016-08-18 17:17:00 +01005419 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005420
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005421 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005422 !IS_CHERRYVIEW(dev_priv))
5423 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005424 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005425 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5426 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005427 dev_priv->num_fence_regs = 16;
5428 else
5429 dev_priv->num_fence_regs = 8;
5430
Chris Wilsonc0336662016-05-06 15:40:21 +01005431 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005432 dev_priv->num_fence_regs =
5433 I915_READ(vgtif_reg(avail_rs.fence_num));
5434
5435 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005436 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5437 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5438
5439 fence->i915 = dev_priv;
5440 fence->id = i;
5441 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5442 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005443 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005444
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005445 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005446}
5447
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005448static void i915_gem_init__mm(struct drm_i915_private *i915)
5449{
5450 spin_lock_init(&i915->mm.object_stat_lock);
5451 spin_lock_init(&i915->mm.obj_lock);
5452 spin_lock_init(&i915->mm.free_lock);
5453
5454 init_llist_head(&i915->mm.free_list);
5455
5456 INIT_LIST_HEAD(&i915->mm.unbound_list);
5457 INIT_LIST_HEAD(&i915->mm.bound_list);
5458 INIT_LIST_HEAD(&i915->mm.fence_list);
5459 INIT_LIST_HEAD(&i915->mm.userfault_list);
5460
5461 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5462}
5463
Chris Wilson73cb9702016-10-28 13:58:46 +01005464int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005465i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005466{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005467 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005468
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005469 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5470 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005471 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005472
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005473 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5474 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005475 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005476
Chris Wilsond1b48c12017-08-16 09:52:08 +01005477 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5478 if (!dev_priv->luts)
5479 goto err_vmas;
5480
Chris Wilsone61e0f52018-02-21 09:56:36 +00005481 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005482 SLAB_HWCACHE_ALIGN |
5483 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005484 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005485 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005486 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005487
Chris Wilson52e54202016-11-14 20:41:02 +00005488 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5489 SLAB_HWCACHE_ALIGN |
5490 SLAB_RECLAIM_ACCOUNT);
5491 if (!dev_priv->dependencies)
5492 goto err_requests;
5493
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005494 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5495 if (!dev_priv->priorities)
5496 goto err_dependencies;
5497
Chris Wilson73cb9702016-10-28 13:58:46 +01005498 mutex_lock(&dev_priv->drm.struct_mutex);
5499 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005500 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005501 mutex_unlock(&dev_priv->drm.struct_mutex);
5502 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005503 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005504
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005505 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005506
Chris Wilson67d97da2016-07-04 08:08:31 +01005507 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005508 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005509 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005510 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005511 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005512 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005513
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005514 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5515
Chris Wilsonb5add952016-08-04 16:32:36 +01005516 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005517
Matthew Auld465c4032017-10-06 23:18:14 +01005518 err = i915_gemfs_init(dev_priv);
5519 if (err)
5520 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5521
Chris Wilson73cb9702016-10-28 13:58:46 +01005522 return 0;
5523
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005524err_priorities:
5525 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005526err_dependencies:
5527 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005528err_requests:
5529 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005530err_luts:
5531 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005532err_vmas:
5533 kmem_cache_destroy(dev_priv->vmas);
5534err_objects:
5535 kmem_cache_destroy(dev_priv->objects);
5536err_out:
5537 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005538}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005539
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005540void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005541{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005542 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c70472018-02-19 22:06:31 +00005543 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5544 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005545 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005546
Matthew Auldea84aa72016-11-17 21:04:11 +00005547 mutex_lock(&dev_priv->drm.struct_mutex);
5548 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5549 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5550 mutex_unlock(&dev_priv->drm.struct_mutex);
5551
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005552 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005553 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005554 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005555 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005556 kmem_cache_destroy(dev_priv->vmas);
5557 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005558
5559 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5560 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005561
5562 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005563}
5564
Chris Wilson6a800ea2016-09-21 14:51:07 +01005565int i915_gem_freeze(struct drm_i915_private *dev_priv)
5566{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005567 /* Discard all purgeable objects, let userspace recover those as
5568 * required after resuming.
5569 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005570 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005571
Chris Wilson6a800ea2016-09-21 14:51:07 +01005572 return 0;
5573}
5574
Chris Wilson461fb992016-05-14 07:26:33 +01005575int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5576{
5577 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005578 struct list_head *phases[] = {
5579 &dev_priv->mm.unbound_list,
5580 &dev_priv->mm.bound_list,
5581 NULL
5582 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005583
5584 /* Called just before we write the hibernation image.
5585 *
5586 * We need to update the domain tracking to reflect that the CPU
5587 * will be accessing all the pages to create and restore from the
5588 * hibernation, and so upon restoration those pages will be in the
5589 * CPU domain.
5590 *
5591 * To make sure the hibernation image contains the latest state,
5592 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005593 *
5594 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005595 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005596 */
5597
Chris Wilson912d5722017-09-06 16:19:30 -07005598 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005599 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005600
Chris Wilsonf2123812017-10-16 12:40:37 +01005601 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005602 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005603 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005604 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005605 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005606 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005607
5608 return 0;
5609}
5610
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005611void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005612{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005613 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005614 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005615
5616 /* Clean up our request list when the client is going away, so that
5617 * later retire_requests won't dereference our soon-to-be-gone
5618 * file_priv.
5619 */
Chris Wilson1c255952010-09-26 11:03:27 +01005620 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005621 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005622 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005623 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005624}
5625
Chris Wilson829a0af2017-06-20 12:05:45 +01005626int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005627{
5628 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005629 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005630
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005631 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005632
5633 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5634 if (!file_priv)
5635 return -ENOMEM;
5636
5637 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005638 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005639 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005640
5641 spin_lock_init(&file_priv->mm.lock);
5642 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005643
Chris Wilsonc80ff162016-07-27 09:07:27 +01005644 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005645
Chris Wilson829a0af2017-06-20 12:05:45 +01005646 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005647 if (ret)
5648 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005649
Ben Widawskye422b882013-12-06 14:10:58 -08005650 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005651}
5652
Daniel Vetterb680c372014-09-19 18:27:27 +02005653/**
5654 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005655 * @old: current GEM buffer for the frontbuffer slots
5656 * @new: new GEM buffer for the frontbuffer slots
5657 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005658 *
5659 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5660 * from @old and setting them in @new. Both @old and @new can be NULL.
5661 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005662void i915_gem_track_fb(struct drm_i915_gem_object *old,
5663 struct drm_i915_gem_object *new,
5664 unsigned frontbuffer_bits)
5665{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005666 /* Control of individual bits within the mask are guarded by
5667 * the owning plane->mutex, i.e. we can never see concurrent
5668 * manipulation of individual bits. But since the bitfield as a whole
5669 * is updated using RMW, we need to use atomics in order to update
5670 * the bits.
5671 */
5672 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5673 sizeof(atomic_t) * BITS_PER_BYTE);
5674
Daniel Vettera071fa02014-06-18 23:28:09 +02005675 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005676 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5677 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005678 }
5679
5680 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005681 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5682 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005683 }
5684}
5685
Dave Gordonea702992015-07-09 19:29:02 +01005686/* Allocate a new GEM object and fill it with the supplied data */
5687struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005688i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005689 const void *data, size_t size)
5690{
5691 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005692 struct file *file;
5693 size_t offset;
5694 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005695
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005696 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005697 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005698 return obj;
5699
Christian Königc0a51fd2018-02-16 13:43:38 +01005700 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005701
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005702 file = obj->base.filp;
5703 offset = 0;
5704 do {
5705 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5706 struct page *page;
5707 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005708
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005709 err = pagecache_write_begin(file, file->f_mapping,
5710 offset, len, 0,
5711 &page, &pgdata);
5712 if (err < 0)
5713 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005714
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005715 vaddr = kmap(page);
5716 memcpy(vaddr, data, len);
5717 kunmap(page);
5718
5719 err = pagecache_write_end(file, file->f_mapping,
5720 offset, len, len,
5721 page, pgdata);
5722 if (err < 0)
5723 goto fail;
5724
5725 size -= len;
5726 data += len;
5727 offset += len;
5728 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005729
5730 return obj;
5731
5732fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005733 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005734 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005735}
Chris Wilson96d77632016-10-28 13:58:33 +01005736
5737struct scatterlist *
5738i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5739 unsigned int n,
5740 unsigned int *offset)
5741{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005742 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005743 struct scatterlist *sg;
5744 unsigned int idx, count;
5745
5746 might_sleep();
5747 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005748 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005749
5750 /* As we iterate forward through the sg, we record each entry in a
5751 * radixtree for quick repeated (backwards) lookups. If we have seen
5752 * this index previously, we will have an entry for it.
5753 *
5754 * Initial lookup is O(N), but this is amortized to O(1) for
5755 * sequential page access (where each new request is consecutive
5756 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5757 * i.e. O(1) with a large constant!
5758 */
5759 if (n < READ_ONCE(iter->sg_idx))
5760 goto lookup;
5761
5762 mutex_lock(&iter->lock);
5763
5764 /* We prefer to reuse the last sg so that repeated lookup of this
5765 * (or the subsequent) sg are fast - comparing against the last
5766 * sg is faster than going through the radixtree.
5767 */
5768
5769 sg = iter->sg_pos;
5770 idx = iter->sg_idx;
5771 count = __sg_page_count(sg);
5772
5773 while (idx + count <= n) {
5774 unsigned long exception, i;
5775 int ret;
5776
5777 /* If we cannot allocate and insert this entry, or the
5778 * individual pages from this range, cancel updating the
5779 * sg_idx so that on this lookup we are forced to linearly
5780 * scan onwards, but on future lookups we will try the
5781 * insertion again (in which case we need to be careful of
5782 * the error return reporting that we have already inserted
5783 * this index).
5784 */
5785 ret = radix_tree_insert(&iter->radix, idx, sg);
5786 if (ret && ret != -EEXIST)
5787 goto scan;
5788
5789 exception =
5790 RADIX_TREE_EXCEPTIONAL_ENTRY |
5791 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5792 for (i = 1; i < count; i++) {
5793 ret = radix_tree_insert(&iter->radix, idx + i,
5794 (void *)exception);
5795 if (ret && ret != -EEXIST)
5796 goto scan;
5797 }
5798
5799 idx += count;
5800 sg = ____sg_next(sg);
5801 count = __sg_page_count(sg);
5802 }
5803
5804scan:
5805 iter->sg_pos = sg;
5806 iter->sg_idx = idx;
5807
5808 mutex_unlock(&iter->lock);
5809
5810 if (unlikely(n < idx)) /* insertion completed by another thread */
5811 goto lookup;
5812
5813 /* In case we failed to insert the entry into the radixtree, we need
5814 * to look beyond the current sg.
5815 */
5816 while (idx + count <= n) {
5817 idx += count;
5818 sg = ____sg_next(sg);
5819 count = __sg_page_count(sg);
5820 }
5821
5822 *offset = n - idx;
5823 return sg;
5824
5825lookup:
5826 rcu_read_lock();
5827
5828 sg = radix_tree_lookup(&iter->radix, n);
5829 GEM_BUG_ON(!sg);
5830
5831 /* If this index is in the middle of multi-page sg entry,
5832 * the radixtree will contain an exceptional entry that points
5833 * to the start of that range. We will return the pointer to
5834 * the base page and the offset of this page within the
5835 * sg entry's range.
5836 */
5837 *offset = 0;
5838 if (unlikely(radix_tree_exception(sg))) {
5839 unsigned long base =
5840 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5841
5842 sg = radix_tree_lookup(&iter->radix, base);
5843 GEM_BUG_ON(!sg);
5844
5845 *offset = n - base;
5846 }
5847
5848 rcu_read_unlock();
5849
5850 return sg;
5851}
5852
5853struct page *
5854i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5855{
5856 struct scatterlist *sg;
5857 unsigned int offset;
5858
5859 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5860
5861 sg = i915_gem_object_get_sg(obj, n, &offset);
5862 return nth_page(sg_page(sg), offset);
5863}
5864
5865/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5866struct page *
5867i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5868 unsigned int n)
5869{
5870 struct page *page;
5871
5872 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005873 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005874 set_page_dirty(page);
5875
5876 return page;
5877}
5878
5879dma_addr_t
5880i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5881 unsigned long n)
5882{
5883 struct scatterlist *sg;
5884 unsigned int offset;
5885
5886 sg = i915_gem_object_get_sg(obj, n, &offset);
5887 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5888}
Chris Wilson935a2f72017-02-13 17:15:13 +00005889
Chris Wilson8eeb7902017-07-26 19:16:01 +01005890int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5891{
5892 struct sg_table *pages;
5893 int err;
5894
5895 if (align > obj->base.size)
5896 return -EINVAL;
5897
5898 if (obj->ops == &i915_gem_phys_ops)
5899 return 0;
5900
5901 if (obj->ops != &i915_gem_object_ops)
5902 return -EINVAL;
5903
5904 err = i915_gem_object_unbind(obj);
5905 if (err)
5906 return err;
5907
5908 mutex_lock(&obj->mm.lock);
5909
5910 if (obj->mm.madv != I915_MADV_WILLNEED) {
5911 err = -EFAULT;
5912 goto err_unlock;
5913 }
5914
5915 if (obj->mm.quirked) {
5916 err = -EFAULT;
5917 goto err_unlock;
5918 }
5919
5920 if (obj->mm.mapping) {
5921 err = -EBUSY;
5922 goto err_unlock;
5923 }
5924
Chris Wilsonf2123812017-10-16 12:40:37 +01005925 pages = fetch_and_zero(&obj->mm.pages);
5926 if (pages) {
5927 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5928
5929 __i915_gem_object_reset_page_iter(obj);
5930
5931 spin_lock(&i915->mm.obj_lock);
5932 list_del(&obj->mm.link);
5933 spin_unlock(&i915->mm.obj_lock);
5934 }
5935
Chris Wilson8eeb7902017-07-26 19:16:01 +01005936 obj->ops = &i915_gem_phys_ops;
5937
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005938 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005939 if (err)
5940 goto err_xfer;
5941
5942 /* Perma-pin (until release) the physical set of pages */
5943 __i915_gem_object_pin_pages(obj);
5944
5945 if (!IS_ERR_OR_NULL(pages))
5946 i915_gem_object_ops.put_pages(obj, pages);
5947 mutex_unlock(&obj->mm.lock);
5948 return 0;
5949
5950err_xfer:
5951 obj->ops = &i915_gem_object_ops;
5952 obj->mm.pages = pages;
5953err_unlock:
5954 mutex_unlock(&obj->mm.lock);
5955 return err;
5956}
5957
Chris Wilson935a2f72017-02-13 17:15:13 +00005958#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5959#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005960#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005961#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005962#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005963#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005964#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005965#endif