blob: c3d6507063299a114b126d50075280f101fc2334 [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
Chris Wilsonfa730552018-03-07 17:13:03 +0000436 /*
437 * If both shared fences and an exclusive fence exist,
438 * then by construction the shared fences must be later
439 * than the exclusive fence. If we successfully wait for
440 * all the shared fences, we know that the exclusive fence
441 * must all be signaled. If all the shared fences are
442 * signaled, we can prune the array and recover the
443 * floating references on the fences/requests.
444 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000445 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100446 } else {
447 excl = reservation_object_get_excl_rcu(resv);
448 }
449
Chris Wilsonfa730552018-03-07 17:13:03 +0000450 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100451 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
452 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100453
454 dma_fence_put(excl);
455
Chris Wilsonfa730552018-03-07 17:13:03 +0000456 /*
457 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000458 * signaled and that the reservation object has not been changed (i.e.
459 * no new fences have been added).
460 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000461 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000462 if (reservation_object_trylock(resv)) {
463 if (!__read_seqcount_retry(&resv->seq, seq))
464 reservation_object_add_excl_fence(resv, NULL);
465 reservation_object_unlock(resv);
466 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000467 }
468
Chris Wilsone95433c2016-10-28 13:58:27 +0100469 return timeout;
470}
471
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000472static void __fence_set_priority(struct dma_fence *fence, int prio)
473{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000474 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000475 struct intel_engine_cs *engine;
476
Chris Wilsonc218ee02018-01-06 10:56:18 +0000477 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000478 return;
479
480 rq = to_request(fence);
481 engine = rq->engine;
482 if (!engine->schedule)
483 return;
484
485 engine->schedule(rq, prio);
486}
487
488static void fence_set_priority(struct dma_fence *fence, int prio)
489{
490 /* Recurse once into a fence-array */
491 if (dma_fence_is_array(fence)) {
492 struct dma_fence_array *array = to_dma_fence_array(fence);
493 int i;
494
495 for (i = 0; i < array->num_fences; i++)
496 __fence_set_priority(array->fences[i], prio);
497 } else {
498 __fence_set_priority(fence, prio);
499 }
500}
501
502int
503i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
504 unsigned int flags,
505 int prio)
506{
507 struct dma_fence *excl;
508
509 if (flags & I915_WAIT_ALL) {
510 struct dma_fence **shared;
511 unsigned int count, i;
512 int ret;
513
514 ret = reservation_object_get_fences_rcu(obj->resv,
515 &excl, &count, &shared);
516 if (ret)
517 return ret;
518
519 for (i = 0; i < count; i++) {
520 fence_set_priority(shared[i], prio);
521 dma_fence_put(shared[i]);
522 }
523
524 kfree(shared);
525 } else {
526 excl = reservation_object_get_excl_rcu(obj->resv);
527 }
528
529 if (excl) {
530 fence_set_priority(excl, prio);
531 dma_fence_put(excl);
532 }
533 return 0;
534}
535
Chris Wilson00e60f22016-08-04 16:32:40 +0100536/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100537 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100538 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100539 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
540 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000541 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100542 */
543int
Chris Wilsone95433c2016-10-28 13:58:27 +0100544i915_gem_object_wait(struct drm_i915_gem_object *obj,
545 unsigned int flags,
546 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100547 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100548{
Chris Wilsone95433c2016-10-28 13:58:27 +0100549 might_sleep();
550#if IS_ENABLED(CONFIG_LOCKDEP)
551 GEM_BUG_ON(debug_locks &&
552 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
553 !!(flags & I915_WAIT_LOCKED));
554#endif
555 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100556
Chris Wilsond07f0e52016-10-28 13:58:44 +0100557 timeout = i915_gem_object_wait_reservation(obj->resv,
558 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100559 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100560 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100561}
562
563static struct intel_rps_client *to_rps_client(struct drm_file *file)
564{
565 struct drm_i915_file_private *fpriv = file->driver_priv;
566
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100567 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100568}
569
Chris Wilson00731152014-05-21 12:42:56 +0100570static int
571i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
572 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100573 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100574{
Chris Wilson00731152014-05-21 12:42:56 +0100575 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300576 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800577
578 /* We manually control the domain here and pretend that it
579 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
580 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700581 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000582 if (copy_from_user(vaddr, user_data, args->size))
583 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100584
Chris Wilson6a2c4232014-11-04 04:51:40 -0800585 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000586 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200587
Chris Wilsond59b21e2017-02-22 11:40:49 +0000588 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000589 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100590}
591
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000592void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000593{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100594 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000595}
596
597void i915_gem_object_free(struct drm_i915_gem_object *obj)
598{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100599 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100600 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000601}
602
Dave Airlieff72145b2011-02-07 12:16:14 +1000603static int
604i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000605 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000606 uint64_t size,
607 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700608{
Chris Wilson05394f32010-11-08 19:18:58 +0000609 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300610 int ret;
611 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700612
Dave Airlieff72145b2011-02-07 12:16:14 +1000613 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200614 if (size == 0)
615 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700616
617 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000618 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100619 if (IS_ERR(obj))
620 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700621
Chris Wilson05394f32010-11-08 19:18:58 +0000622 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100623 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100624 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200625 if (ret)
626 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100627
Dave Airlieff72145b2011-02-07 12:16:14 +1000628 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700629 return 0;
630}
631
Dave Airlieff72145b2011-02-07 12:16:14 +1000632int
633i915_gem_dumb_create(struct drm_file *file,
634 struct drm_device *dev,
635 struct drm_mode_create_dumb *args)
636{
637 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300638 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000639 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000640 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000641 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000642}
643
Chris Wilsone27ab732017-06-15 13:38:49 +0100644static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
645{
646 return !(obj->cache_level == I915_CACHE_NONE ||
647 obj->cache_level == I915_CACHE_WT);
648}
649
Dave Airlieff72145b2011-02-07 12:16:14 +1000650/**
651 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100652 * @dev: drm device pointer
653 * @data: ioctl data blob
654 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000655 */
656int
657i915_gem_create_ioctl(struct drm_device *dev, void *data,
658 struct drm_file *file)
659{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000660 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000661 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200662
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000663 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100664
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000665 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000666 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000667}
668
Chris Wilsonef749212017-04-12 12:01:10 +0100669static inline enum fb_op_origin
670fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
671{
672 return (domain == I915_GEM_DOMAIN_GTT ?
673 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
674}
675
Chris Wilson71253972017-12-06 12:49:14 +0000676void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100677{
Chris Wilson71253972017-12-06 12:49:14 +0000678 /*
679 * No actual flushing is required for the GTT write domain for reads
680 * from the GTT domain. Writes to it "immediately" go to main memory
681 * as far as we know, so there's no chipset flush. It also doesn't
682 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100683 *
684 * However, we do have to enforce the order so that all writes through
685 * the GTT land before any writes to the device, such as updates to
686 * the GATT itself.
687 *
688 * We also have to wait a bit for the writes to land from the GTT.
689 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
690 * timing. This issue has only been observed when switching quickly
691 * between GTT writes and CPU reads from inside the kernel on recent hw,
692 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson71253972017-12-06 12:49:14 +0000693 * system agents we cannot reproduce this behaviour, until Cannonlake
694 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100695 */
Chris Wilson71253972017-12-06 12:49:14 +0000696
Chris Wilsonef749212017-04-12 12:01:10 +0100697 wmb();
698
Chris Wilson71253972017-12-06 12:49:14 +0000699 intel_runtime_pm_get(dev_priv);
700 spin_lock_irq(&dev_priv->uncore.lock);
701
702 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
703
704 spin_unlock_irq(&dev_priv->uncore.lock);
705 intel_runtime_pm_put(dev_priv);
706}
707
708static void
709flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
710{
711 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
712 struct i915_vma *vma;
713
Christian Königc0a51fd2018-02-16 13:43:38 +0100714 if (!(obj->write_domain & flush_domains))
Chris Wilson71253972017-12-06 12:49:14 +0000715 return;
716
Christian Königc0a51fd2018-02-16 13:43:38 +0100717 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100718 case I915_GEM_DOMAIN_GTT:
Chris Wilson71253972017-12-06 12:49:14 +0000719 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100720
721 intel_fb_obj_flush(obj,
722 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson71253972017-12-06 12:49:14 +0000723
Chris Wilsone2189dd2017-12-07 21:14:07 +0000724 for_each_ggtt_vma(vma, obj) {
Chris Wilson71253972017-12-06 12:49:14 +0000725 if (vma->iomap)
726 continue;
727
728 i915_vma_unset_ggtt_write(vma);
729 }
Chris Wilsonef749212017-04-12 12:01:10 +0100730 break;
731
732 case I915_GEM_DOMAIN_CPU:
733 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
734 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100735
736 case I915_GEM_DOMAIN_RENDER:
737 if (gpu_write_needs_clflush(obj))
738 obj->cache_dirty = true;
739 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100740 }
741
Christian Königc0a51fd2018-02-16 13:43:38 +0100742 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100743}
744
Daniel Vetter8c599672011-12-14 13:57:31 +0100745static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100746__copy_to_user_swizzled(char __user *cpu_vaddr,
747 const char *gpu_vaddr, int gpu_offset,
748 int length)
749{
750 int ret, cpu_offset = 0;
751
752 while (length > 0) {
753 int cacheline_end = ALIGN(gpu_offset + 1, 64);
754 int this_length = min(cacheline_end - gpu_offset, length);
755 int swizzled_gpu_offset = gpu_offset ^ 64;
756
757 ret = __copy_to_user(cpu_vaddr + cpu_offset,
758 gpu_vaddr + swizzled_gpu_offset,
759 this_length);
760 if (ret)
761 return ret + length;
762
763 cpu_offset += this_length;
764 gpu_offset += this_length;
765 length -= this_length;
766 }
767
768 return 0;
769}
770
771static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700772__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
773 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100774 int length)
775{
776 int ret, cpu_offset = 0;
777
778 while (length > 0) {
779 int cacheline_end = ALIGN(gpu_offset + 1, 64);
780 int this_length = min(cacheline_end - gpu_offset, length);
781 int swizzled_gpu_offset = gpu_offset ^ 64;
782
783 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
784 cpu_vaddr + cpu_offset,
785 this_length);
786 if (ret)
787 return ret + length;
788
789 cpu_offset += this_length;
790 gpu_offset += this_length;
791 length -= this_length;
792 }
793
794 return 0;
795}
796
Brad Volkin4c914c02014-02-18 10:15:45 -0800797/*
798 * Pins the specified object's pages and synchronizes the object with
799 * GPU accesses. Sets needs_clflush to non-zero if the caller should
800 * flush the object from the CPU cache.
801 */
802int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100803 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800804{
805 int ret;
806
Chris Wilsone95433c2016-10-28 13:58:27 +0100807 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800808
Chris Wilsone95433c2016-10-28 13:58:27 +0100809 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100810 if (!i915_gem_object_has_struct_page(obj))
811 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800812
Chris Wilsone95433c2016-10-28 13:58:27 +0100813 ret = i915_gem_object_wait(obj,
814 I915_WAIT_INTERRUPTIBLE |
815 I915_WAIT_LOCKED,
816 MAX_SCHEDULE_TIMEOUT,
817 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100818 if (ret)
819 return ret;
820
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100821 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100822 if (ret)
823 return ret;
824
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100825 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
826 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000827 ret = i915_gem_object_set_to_cpu_domain(obj, false);
828 if (ret)
829 goto err_unpin;
830 else
831 goto out;
832 }
833
Chris Wilsonef749212017-04-12 12:01:10 +0100834 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100835
Chris Wilson43394c72016-08-18 17:16:47 +0100836 /* If we're not in the cpu read domain, set ourself into the gtt
837 * read domain and manually flush cachelines (if required). This
838 * optimizes for the case when the gpu will dirty the data
839 * anyway again before the next pread happens.
840 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100841 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100842 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000843 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800844
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000845out:
Chris Wilson97649512016-08-18 17:16:50 +0100846 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100847 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100848
849err_unpin:
850 i915_gem_object_unpin_pages(obj);
851 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100852}
853
854int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
855 unsigned int *needs_clflush)
856{
857 int ret;
858
Chris Wilsone95433c2016-10-28 13:58:27 +0100859 lockdep_assert_held(&obj->base.dev->struct_mutex);
860
Chris Wilson43394c72016-08-18 17:16:47 +0100861 *needs_clflush = 0;
862 if (!i915_gem_object_has_struct_page(obj))
863 return -ENODEV;
864
Chris Wilsone95433c2016-10-28 13:58:27 +0100865 ret = i915_gem_object_wait(obj,
866 I915_WAIT_INTERRUPTIBLE |
867 I915_WAIT_LOCKED |
868 I915_WAIT_ALL,
869 MAX_SCHEDULE_TIMEOUT,
870 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100871 if (ret)
872 return ret;
873
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100874 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100875 if (ret)
876 return ret;
877
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100878 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
879 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000880 ret = i915_gem_object_set_to_cpu_domain(obj, true);
881 if (ret)
882 goto err_unpin;
883 else
884 goto out;
885 }
886
Chris Wilsonef749212017-04-12 12:01:10 +0100887 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100888
Chris Wilson43394c72016-08-18 17:16:47 +0100889 /* If we're not in the cpu write domain, set ourself into the
890 * gtt write domain and manually flush cachelines (as required).
891 * This optimizes for the case when the gpu will use the data
892 * right away and we therefore have to clflush anyway.
893 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100894 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000895 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100896
Chris Wilsone27ab732017-06-15 13:38:49 +0100897 /*
898 * Same trick applies to invalidate partially written
899 * cachelines read before writing.
900 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100901 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100902 *needs_clflush |= CLFLUSH_BEFORE;
903 }
Chris Wilson43394c72016-08-18 17:16:47 +0100904
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000905out:
Chris Wilson43394c72016-08-18 17:16:47 +0100906 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100907 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100908 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100909 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100910
911err_unpin:
912 i915_gem_object_unpin_pages(obj);
913 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800914}
915
Daniel Vetter23c18c72012-03-25 19:47:42 +0200916static void
917shmem_clflush_swizzled_range(char *addr, unsigned long length,
918 bool swizzled)
919{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200920 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200921 unsigned long start = (unsigned long) addr;
922 unsigned long end = (unsigned long) addr + length;
923
924 /* For swizzling simply ensure that we always flush both
925 * channels. Lame, but simple and it works. Swizzled
926 * pwrite/pread is far from a hotpath - current userspace
927 * doesn't use it at all. */
928 start = round_down(start, 128);
929 end = round_up(end, 128);
930
931 drm_clflush_virt_range((void *)start, end - start);
932 } else {
933 drm_clflush_virt_range(addr, length);
934 }
935
936}
937
Daniel Vetterd174bd62012-03-25 19:47:40 +0200938/* Only difference to the fast-path function is that this can handle bit17
939 * and uses non-atomic copy and kmap functions. */
940static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100941shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200942 char __user *user_data,
943 bool page_do_bit17_swizzling, bool needs_clflush)
944{
945 char *vaddr;
946 int ret;
947
948 vaddr = kmap(page);
949 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100950 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200951 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200952
953 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100954 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200955 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100956 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200957 kunmap(page);
958
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100959 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200960}
961
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100962static int
963shmem_pread(struct page *page, int offset, int length, char __user *user_data,
964 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530965{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100966 int ret;
967
968 ret = -ENODEV;
969 if (!page_do_bit17_swizzling) {
970 char *vaddr = kmap_atomic(page);
971
972 if (needs_clflush)
973 drm_clflush_virt_range(vaddr + offset, length);
974 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
975 kunmap_atomic(vaddr);
976 }
977 if (ret == 0)
978 return 0;
979
980 return shmem_pread_slow(page, offset, length, user_data,
981 page_do_bit17_swizzling, needs_clflush);
982}
983
984static int
985i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
986 struct drm_i915_gem_pread *args)
987{
988 char __user *user_data;
989 u64 remain;
990 unsigned int obj_do_bit17_swizzling;
991 unsigned int needs_clflush;
992 unsigned int idx, offset;
993 int ret;
994
995 obj_do_bit17_swizzling = 0;
996 if (i915_gem_object_needs_bit17_swizzle(obj))
997 obj_do_bit17_swizzling = BIT(17);
998
999 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1000 if (ret)
1001 return ret;
1002
1003 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1004 mutex_unlock(&obj->base.dev->struct_mutex);
1005 if (ret)
1006 return ret;
1007
1008 remain = args->size;
1009 user_data = u64_to_user_ptr(args->data_ptr);
1010 offset = offset_in_page(args->offset);
1011 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1012 struct page *page = i915_gem_object_get_page(obj, idx);
1013 int length;
1014
1015 length = remain;
1016 if (offset + length > PAGE_SIZE)
1017 length = PAGE_SIZE - offset;
1018
1019 ret = shmem_pread(page, offset, length, user_data,
1020 page_to_phys(page) & obj_do_bit17_swizzling,
1021 needs_clflush);
1022 if (ret)
1023 break;
1024
1025 remain -= length;
1026 user_data += length;
1027 offset = 0;
1028 }
1029
1030 i915_gem_obj_finish_shmem_access(obj);
1031 return ret;
1032}
1033
1034static inline bool
1035gtt_user_read(struct io_mapping *mapping,
1036 loff_t base, int offset,
1037 char __user *user_data, int length)
1038{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001039 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001040 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301041
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301042 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001043 vaddr = io_mapping_map_atomic_wc(mapping, base);
1044 unwritten = __copy_to_user_inatomic(user_data,
1045 (void __force *)vaddr + offset,
1046 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001047 io_mapping_unmap_atomic(vaddr);
1048 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001049 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1050 unwritten = copy_to_user(user_data,
1051 (void __force *)vaddr + offset,
1052 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001053 io_mapping_unmap(vaddr);
1054 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301055 return unwritten;
1056}
1057
1058static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001059i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1060 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301061{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001062 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1063 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301064 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001065 struct i915_vma *vma;
1066 void __user *user_data;
1067 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301068 int ret;
1069
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001070 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1071 if (ret)
1072 return ret;
1073
1074 intel_runtime_pm_get(i915);
1075 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001076 PIN_MAPPABLE |
1077 PIN_NONFAULT |
1078 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001079 if (!IS_ERR(vma)) {
1080 node.start = i915_ggtt_offset(vma);
1081 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001082 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001083 if (ret) {
1084 i915_vma_unpin(vma);
1085 vma = ERR_PTR(ret);
1086 }
1087 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001088 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001089 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301090 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001091 goto out_unlock;
1092 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301093 }
1094
1095 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1096 if (ret)
1097 goto out_unpin;
1098
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001099 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301100
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001101 user_data = u64_to_user_ptr(args->data_ptr);
1102 remain = args->size;
1103 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301104
1105 while (remain > 0) {
1106 /* Operation in this page
1107 *
1108 * page_base = page offset within aperture
1109 * page_offset = offset within page
1110 * page_length = bytes to copy for this page
1111 */
1112 u32 page_base = node.start;
1113 unsigned page_offset = offset_in_page(offset);
1114 unsigned page_length = PAGE_SIZE - page_offset;
1115 page_length = remain < page_length ? remain : page_length;
1116 if (node.allocated) {
1117 wmb();
1118 ggtt->base.insert_page(&ggtt->base,
1119 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001120 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301121 wmb();
1122 } else {
1123 page_base += offset & PAGE_MASK;
1124 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001125
Matthew Auld73ebd502017-12-11 15:18:20 +00001126 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001127 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301128 ret = -EFAULT;
1129 break;
1130 }
1131
1132 remain -= page_length;
1133 user_data += page_length;
1134 offset += page_length;
1135 }
1136
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001137 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301138out_unpin:
1139 if (node.allocated) {
1140 wmb();
1141 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001142 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301143 remove_mappable_node(&node);
1144 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001145 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301146 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001147out_unlock:
1148 intel_runtime_pm_put(i915);
1149 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001150
Eric Anholteb014592009-03-10 11:44:52 -07001151 return ret;
1152}
1153
Eric Anholt673a3942008-07-30 12:06:12 -07001154/**
1155 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001156 * @dev: drm device pointer
1157 * @data: ioctl data blob
1158 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001159 *
1160 * On error, the contents of *data are undefined.
1161 */
1162int
1163i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001164 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001165{
1166 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001167 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001168 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001169
Chris Wilson51311d02010-11-17 09:10:42 +00001170 if (args->size == 0)
1171 return 0;
1172
1173 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001174 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001175 args->size))
1176 return -EFAULT;
1177
Chris Wilson03ac0642016-07-20 13:31:51 +01001178 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001179 if (!obj)
1180 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001181
Chris Wilson7dcd2492010-09-26 20:21:44 +01001182 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001183 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001184 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001185 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001186 }
1187
Chris Wilsondb53a302011-02-03 11:57:46 +00001188 trace_i915_gem_object_pread(obj, args->offset, args->size);
1189
Chris Wilsone95433c2016-10-28 13:58:27 +01001190 ret = i915_gem_object_wait(obj,
1191 I915_WAIT_INTERRUPTIBLE,
1192 MAX_SCHEDULE_TIMEOUT,
1193 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001194 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001195 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001196
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001198 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001199 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001200
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001201 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001202 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001203 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301204
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001205 i915_gem_object_unpin_pages(obj);
1206out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001207 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001208 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001209}
1210
Keith Packard0839ccb2008-10-30 19:38:48 -07001211/* This is the fast write path which cannot handle
1212 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001213 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001214
Chris Wilsonfe115622016-10-28 13:58:40 +01001215static inline bool
1216ggtt_write(struct io_mapping *mapping,
1217 loff_t base, int offset,
1218 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001219{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001220 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001221 unsigned long unwritten;
1222
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001223 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001224 vaddr = io_mapping_map_atomic_wc(mapping, base);
1225 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001226 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001227 io_mapping_unmap_atomic(vaddr);
1228 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001229 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1230 unwritten = copy_from_user((void __force *)vaddr + offset,
1231 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001232 io_mapping_unmap(vaddr);
1233 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001234
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001235 return unwritten;
1236}
1237
Eric Anholt3de09aa2009-03-09 09:42:23 -07001238/**
1239 * This is the fast pwrite path, where we copy the data directly from the
1240 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001241 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001242 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001243 */
Eric Anholt673a3942008-07-30 12:06:12 -07001244static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001245i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1246 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001247{
Chris Wilsonfe115622016-10-28 13:58:40 +01001248 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301249 struct i915_ggtt *ggtt = &i915->ggtt;
1250 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001251 struct i915_vma *vma;
1252 u64 remain, offset;
1253 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301254 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301255
Chris Wilsonfe115622016-10-28 13:58:40 +01001256 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1257 if (ret)
1258 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001259
Chris Wilson8bd81812017-10-19 07:37:33 +01001260 if (i915_gem_object_has_struct_page(obj)) {
1261 /*
1262 * Avoid waking the device up if we can fallback, as
1263 * waking/resuming is very slow (worst-case 10-100 ms
1264 * depending on PCI sleeps and our own resume time).
1265 * This easily dwarfs any performance advantage from
1266 * using the cache bypass of indirect GGTT access.
1267 */
1268 if (!intel_runtime_pm_get_if_in_use(i915)) {
1269 ret = -EFAULT;
1270 goto out_unlock;
1271 }
1272 } else {
1273 /* No backing pages, no fallback, we must force GGTT access */
1274 intel_runtime_pm_get(i915);
1275 }
1276
Chris Wilson058d88c2016-08-15 10:49:06 +01001277 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001278 PIN_MAPPABLE |
1279 PIN_NONFAULT |
1280 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001281 if (!IS_ERR(vma)) {
1282 node.start = i915_ggtt_offset(vma);
1283 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001284 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001285 if (ret) {
1286 i915_vma_unpin(vma);
1287 vma = ERR_PTR(ret);
1288 }
1289 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001290 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001291 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301292 if (ret)
Chris Wilson8bd81812017-10-19 07:37:33 +01001293 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001294 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301295 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001296
1297 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1298 if (ret)
1299 goto out_unpin;
1300
Chris Wilsonfe115622016-10-28 13:58:40 +01001301 mutex_unlock(&i915->drm.struct_mutex);
1302
Chris Wilsonb19482d2016-08-18 17:16:43 +01001303 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001304
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301305 user_data = u64_to_user_ptr(args->data_ptr);
1306 offset = args->offset;
1307 remain = args->size;
1308 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001309 /* Operation in this page
1310 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001311 * page_base = page offset within aperture
1312 * page_offset = offset within page
1313 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001314 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301315 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001316 unsigned int page_offset = offset_in_page(offset);
1317 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301318 page_length = remain < page_length ? remain : page_length;
1319 if (node.allocated) {
1320 wmb(); /* flush the write before we modify the GGTT */
1321 ggtt->base.insert_page(&ggtt->base,
1322 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1323 node.start, I915_CACHE_NONE, 0);
1324 wmb(); /* flush modifications to the GGTT (insert_page) */
1325 } else {
1326 page_base += offset & PAGE_MASK;
1327 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001328 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001329 * source page isn't available. Return the error and we'll
1330 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301331 * If the object is non-shmem backed, we retry again with the
1332 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001333 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001334 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001335 user_data, page_length)) {
1336 ret = -EFAULT;
1337 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001338 }
Eric Anholt673a3942008-07-30 12:06:12 -07001339
Keith Packard0839ccb2008-10-30 19:38:48 -07001340 remain -= page_length;
1341 user_data += page_length;
1342 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001343 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001344 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001345
1346 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001347out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301348 if (node.allocated) {
1349 wmb();
1350 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001351 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301352 remove_mappable_node(&node);
1353 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001354 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301355 }
Chris Wilson8bd81812017-10-19 07:37:33 +01001356out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001357 intel_runtime_pm_put(i915);
Chris Wilson8bd81812017-10-19 07:37:33 +01001358out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001359 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001360 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001361}
1362
Eric Anholt673a3942008-07-30 12:06:12 -07001363static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001364shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001365 char __user *user_data,
1366 bool page_do_bit17_swizzling,
1367 bool needs_clflush_before,
1368 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001369{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001370 char *vaddr;
1371 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001372
Daniel Vetterd174bd62012-03-25 19:47:40 +02001373 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001374 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
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 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001378 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1379 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001380 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001381 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001382 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001383 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001384 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001385 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001386
Chris Wilson755d2212012-09-04 21:02:55 +01001387 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001388}
1389
Chris Wilsonfe115622016-10-28 13:58:40 +01001390/* Per-page copy function for the shmem pwrite fastpath.
1391 * Flushes invalid cachelines before writing to the target if
1392 * needs_clflush_before is set and flushes out any written cachelines after
1393 * writing if needs_clflush is set.
1394 */
Eric Anholt40123c12009-03-09 13:42:30 -07001395static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001396shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1397 bool page_do_bit17_swizzling,
1398 bool needs_clflush_before,
1399 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001400{
Chris Wilsonfe115622016-10-28 13:58:40 +01001401 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001402
Chris Wilsonfe115622016-10-28 13:58:40 +01001403 ret = -ENODEV;
1404 if (!page_do_bit17_swizzling) {
1405 char *vaddr = kmap_atomic(page);
1406
1407 if (needs_clflush_before)
1408 drm_clflush_virt_range(vaddr + offset, len);
1409 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1410 if (needs_clflush_after)
1411 drm_clflush_virt_range(vaddr + offset, len);
1412
1413 kunmap_atomic(vaddr);
1414 }
1415 if (ret == 0)
1416 return ret;
1417
1418 return shmem_pwrite_slow(page, offset, len, user_data,
1419 page_do_bit17_swizzling,
1420 needs_clflush_before,
1421 needs_clflush_after);
1422}
1423
1424static int
1425i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1426 const struct drm_i915_gem_pwrite *args)
1427{
1428 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1429 void __user *user_data;
1430 u64 remain;
1431 unsigned int obj_do_bit17_swizzling;
1432 unsigned int partial_cacheline_write;
1433 unsigned int needs_clflush;
1434 unsigned int offset, idx;
1435 int ret;
1436
1437 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001438 if (ret)
1439 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001440
Chris Wilsonfe115622016-10-28 13:58:40 +01001441 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1442 mutex_unlock(&i915->drm.struct_mutex);
1443 if (ret)
1444 return ret;
1445
1446 obj_do_bit17_swizzling = 0;
1447 if (i915_gem_object_needs_bit17_swizzle(obj))
1448 obj_do_bit17_swizzling = BIT(17);
1449
1450 /* If we don't overwrite a cacheline completely we need to be
1451 * careful to have up-to-date data by first clflushing. Don't
1452 * overcomplicate things and flush the entire patch.
1453 */
1454 partial_cacheline_write = 0;
1455 if (needs_clflush & CLFLUSH_BEFORE)
1456 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1457
Chris Wilson43394c72016-08-18 17:16:47 +01001458 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001459 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001460 offset = offset_in_page(args->offset);
1461 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1462 struct page *page = i915_gem_object_get_page(obj, idx);
1463 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001464
Chris Wilsonfe115622016-10-28 13:58:40 +01001465 length = remain;
1466 if (offset + length > PAGE_SIZE)
1467 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001468
Chris Wilsonfe115622016-10-28 13:58:40 +01001469 ret = shmem_pwrite(page, offset, length, user_data,
1470 page_to_phys(page) & obj_do_bit17_swizzling,
1471 (offset | length) & partial_cacheline_write,
1472 needs_clflush & CLFLUSH_AFTER);
1473 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001474 break;
1475
Chris Wilsonfe115622016-10-28 13:58:40 +01001476 remain -= length;
1477 user_data += length;
1478 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001479 }
1480
Chris Wilsond59b21e2017-02-22 11:40:49 +00001481 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001482 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001483 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001484}
1485
1486/**
1487 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001488 * @dev: drm device
1489 * @data: ioctl data blob
1490 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001491 *
1492 * On error, the contents of the buffer that were to be modified are undefined.
1493 */
1494int
1495i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001496 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001497{
1498 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001499 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001500 int ret;
1501
1502 if (args->size == 0)
1503 return 0;
1504
1505 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001506 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001507 args->size))
1508 return -EFAULT;
1509
Chris Wilson03ac0642016-07-20 13:31:51 +01001510 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001511 if (!obj)
1512 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001513
Chris Wilson7dcd2492010-09-26 20:21:44 +01001514 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001515 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001516 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001517 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001518 }
1519
Chris Wilsondb53a302011-02-03 11:57:46 +00001520 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1521
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001522 ret = -ENODEV;
1523 if (obj->ops->pwrite)
1524 ret = obj->ops->pwrite(obj, args);
1525 if (ret != -ENODEV)
1526 goto err;
1527
Chris Wilsone95433c2016-10-28 13:58:27 +01001528 ret = i915_gem_object_wait(obj,
1529 I915_WAIT_INTERRUPTIBLE |
1530 I915_WAIT_ALL,
1531 MAX_SCHEDULE_TIMEOUT,
1532 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001533 if (ret)
1534 goto err;
1535
Chris Wilsonfe115622016-10-28 13:58:40 +01001536 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001537 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001538 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001539
Daniel Vetter935aaa62012-03-25 19:47:35 +02001540 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001541 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1542 * it would end up going through the fenced access, and we'll get
1543 * different detiling behavior between reading and writing.
1544 * pread/pwrite currently are reading and writing from the CPU
1545 * perspective, requiring manual detiling by the client.
1546 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001547 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001548 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001549 /* Note that the gtt paths might fail with non-page-backed user
1550 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001551 * textures). Fallback to the shmem path in that case.
1552 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001553 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001554
Chris Wilsond1054ee2016-07-16 18:42:36 +01001555 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001556 if (obj->phys_handle)
1557 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301558 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001559 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001560 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001561
Chris Wilsonfe115622016-10-28 13:58:40 +01001562 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001563err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001564 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001565 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001566}
1567
Chris Wilson40e62d52016-10-28 13:58:41 +01001568static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1569{
1570 struct drm_i915_private *i915;
1571 struct list_head *list;
1572 struct i915_vma *vma;
1573
Chris Wilsonf2123812017-10-16 12:40:37 +01001574 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1575
Chris Wilsone2189dd2017-12-07 21:14:07 +00001576 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001577 if (i915_vma_is_active(vma))
1578 continue;
1579
1580 if (!drm_mm_node_allocated(&vma->node))
1581 continue;
1582
1583 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1584 }
1585
1586 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001587 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001588 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001589 list_move_tail(&obj->mm.link, list);
1590 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001591}
1592
Eric Anholt673a3942008-07-30 12:06:12 -07001593/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001594 * Called when user space prepares to use an object with the CPU, either
1595 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001596 * @dev: drm device
1597 * @data: ioctl data blob
1598 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001599 */
1600int
1601i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001602 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001603{
1604 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001605 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001606 uint32_t read_domains = args->read_domains;
1607 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001608 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001609
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001610 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001611 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001612 return -EINVAL;
1613
1614 /* Having something in the write domain implies it's in the read
1615 * domain, and only that read domain. Enforce that in the request.
1616 */
1617 if (write_domain != 0 && read_domains != write_domain)
1618 return -EINVAL;
1619
Chris Wilson03ac0642016-07-20 13:31:51 +01001620 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001621 if (!obj)
1622 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001623
Chris Wilson3236f572012-08-24 09:35:09 +01001624 /* Try to flush the object off the GPU without holding the lock.
1625 * We will repeat the flush holding the lock in the normal manner
1626 * to catch cases where we are gazumped.
1627 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001628 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001629 I915_WAIT_INTERRUPTIBLE |
1630 (write_domain ? I915_WAIT_ALL : 0),
1631 MAX_SCHEDULE_TIMEOUT,
1632 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001633 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001634 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001635
Tina Zhanga03f3952017-11-14 10:25:13 +00001636 /*
1637 * Proxy objects do not control access to the backing storage, ergo
1638 * they cannot be used as a means to manipulate the cache domain
1639 * tracking for that backing storage. The proxy object is always
1640 * considered to be outside of any cache domain.
1641 */
1642 if (i915_gem_object_is_proxy(obj)) {
1643 err = -ENXIO;
1644 goto out;
1645 }
1646
1647 /*
1648 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001649 * direct access in memory with previous cached writes through
1650 * shmemfs and that our cache domain tracking remains valid.
1651 * For example, if the obj->filp was moved to swap without us
1652 * being notified and releasing the pages, we would mistakenly
1653 * continue to assume that the obj remained out of the CPU cached
1654 * domain.
1655 */
1656 err = i915_gem_object_pin_pages(obj);
1657 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001658 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001659
1660 err = i915_mutex_lock_interruptible(dev);
1661 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001662 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001663
Chris Wilsone22d8e32017-04-12 12:01:11 +01001664 if (read_domains & I915_GEM_DOMAIN_WC)
1665 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1666 else if (read_domains & I915_GEM_DOMAIN_GTT)
1667 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301668 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001669 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001670
1671 /* And bump the LRU for this access */
1672 i915_gem_object_bump_inactive_ggtt(obj);
1673
1674 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001675
Daniel Vetter031b6982015-06-26 19:35:16 +02001676 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001677 intel_fb_obj_invalidate(obj,
1678 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001679
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001680out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001681 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001682out:
1683 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001684 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001685}
1686
1687/**
1688 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001689 * @dev: drm device
1690 * @data: ioctl data blob
1691 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001692 */
1693int
1694i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001695 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001696{
1697 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001698 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001699
Chris Wilson03ac0642016-07-20 13:31:51 +01001700 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001701 if (!obj)
1702 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001703
Tina Zhanga03f3952017-11-14 10:25:13 +00001704 /*
1705 * Proxy objects are barred from CPU access, so there is no
1706 * need to ban sw_finish as it is a nop.
1707 */
1708
Eric Anholt673a3942008-07-30 12:06:12 -07001709 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001710 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001711 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001712
1713 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001714}
1715
1716/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001717 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1718 * it is mapped to.
1719 * @dev: drm device
1720 * @data: ioctl data blob
1721 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001722 *
1723 * While the mapping holds a reference on the contents of the object, it doesn't
1724 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001725 *
1726 * IMPORTANT:
1727 *
1728 * DRM driver writers who look a this function as an example for how to do GEM
1729 * mmap support, please don't implement mmap support like here. The modern way
1730 * to implement DRM mmap support is with an mmap offset ioctl (like
1731 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1732 * That way debug tooling like valgrind will understand what's going on, hiding
1733 * the mmap call in a driver private ioctl will break that. The i915 driver only
1734 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001735 */
1736int
1737i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001738 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001739{
1740 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001741 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001742 unsigned long addr;
1743
Akash Goel1816f922015-01-02 16:29:30 +05301744 if (args->flags & ~(I915_MMAP_WC))
1745 return -EINVAL;
1746
Borislav Petkov568a58e2016-03-29 17:42:01 +02001747 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301748 return -ENODEV;
1749
Chris Wilson03ac0642016-07-20 13:31:51 +01001750 obj = i915_gem_object_lookup(file, args->handle);
1751 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001752 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001753
Daniel Vetter1286ff72012-05-10 15:25:09 +02001754 /* prime objects have no backing filp to GEM mmap
1755 * pages from.
1756 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001757 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001758 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001759 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001760 }
1761
Chris Wilson03ac0642016-07-20 13:31:51 +01001762 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001763 PROT_READ | PROT_WRITE, MAP_SHARED,
1764 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301765 if (args->flags & I915_MMAP_WC) {
1766 struct mm_struct *mm = current->mm;
1767 struct vm_area_struct *vma;
1768
Michal Hocko80a89a52016-05-23 16:26:11 -07001769 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001770 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001771 return -EINTR;
1772 }
Akash Goel1816f922015-01-02 16:29:30 +05301773 vma = find_vma(mm, addr);
1774 if (vma)
1775 vma->vm_page_prot =
1776 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1777 else
1778 addr = -ENOMEM;
1779 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001780
1781 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001782 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301783 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001784 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001785 if (IS_ERR((void *)addr))
1786 return addr;
1787
1788 args->addr_ptr = (uint64_t) addr;
1789
1790 return 0;
1791}
1792
Chris Wilson03af84f2016-08-18 17:17:01 +01001793static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1794{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001795 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001796}
1797
Jesse Barnesde151cf2008-11-12 10:03:55 -08001798/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001799 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1800 *
1801 * A history of the GTT mmap interface:
1802 *
1803 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1804 * aligned and suitable for fencing, and still fit into the available
1805 * mappable space left by the pinned display objects. A classic problem
1806 * we called the page-fault-of-doom where we would ping-pong between
1807 * two objects that could not fit inside the GTT and so the memcpy
1808 * would page one object in at the expense of the other between every
1809 * single byte.
1810 *
1811 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1812 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1813 * object is too large for the available space (or simply too large
1814 * for the mappable aperture!), a view is created instead and faulted
1815 * into userspace. (This view is aligned and sized appropriately for
1816 * fenced access.)
1817 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001818 * 2 - Recognise WC as a separate cache domain so that we can flush the
1819 * delayed writes via GTT before performing direct access via WC.
1820 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001821 * Restrictions:
1822 *
1823 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1824 * hangs on some architectures, corruption on others. An attempt to service
1825 * a GTT page fault from a snoopable object will generate a SIGBUS.
1826 *
1827 * * the object must be able to fit into RAM (physical memory, though no
1828 * limited to the mappable aperture).
1829 *
1830 *
1831 * Caveats:
1832 *
1833 * * a new GTT page fault will synchronize rendering from the GPU and flush
1834 * all data to system memory. Subsequent access will not be synchronized.
1835 *
1836 * * all mappings are revoked on runtime device suspend.
1837 *
1838 * * there are only 8, 16 or 32 fence registers to share between all users
1839 * (older machines require fence register for display and blitter access
1840 * as well). Contention of the fence registers will cause the previous users
1841 * to be unmapped and any new access will generate new page faults.
1842 *
1843 * * running out of memory while servicing a fault may generate a SIGBUS,
1844 * rather than the expected SIGSEGV.
1845 */
1846int i915_gem_mmap_gtt_version(void)
1847{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001848 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001849}
1850
Chris Wilson2d4281b2017-01-10 09:56:32 +00001851static inline struct i915_ggtt_view
1852compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001853 pgoff_t page_offset,
1854 unsigned int chunk)
1855{
1856 struct i915_ggtt_view view;
1857
1858 if (i915_gem_object_is_tiled(obj))
1859 chunk = roundup(chunk, tile_row_pages(obj));
1860
Chris Wilson2d4281b2017-01-10 09:56:32 +00001861 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001862 view.partial.offset = rounddown(page_offset, chunk);
1863 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001864 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001865 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001866
1867 /* If the partial covers the entire object, just create a normal VMA. */
1868 if (chunk >= obj->base.size >> PAGE_SHIFT)
1869 view.type = I915_GGTT_VIEW_NORMAL;
1870
1871 return view;
1872}
1873
Chris Wilson4cc69072016-08-25 19:05:19 +01001874/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001875 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001876 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001877 *
1878 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1879 * from userspace. The fault handler takes care of binding the object to
1880 * the GTT (if needed), allocating and programming a fence register (again,
1881 * only if needed based on whether the old reg is still valid or the object
1882 * is tiled) and inserting a new PTE into the faulting process.
1883 *
1884 * Note that the faulting process may involve evicting existing objects
1885 * from the GTT and/or fence registers to make room. So performance may
1886 * suffer if the GTT working set is large or there are few fence registers
1887 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001888 *
1889 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1890 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001891 */
Dave Jiang11bac802017-02-24 14:56:41 -08001892int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001893{
Chris Wilson03af84f2016-08-18 17:17:01 +01001894#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001895 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001896 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001897 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001898 struct drm_i915_private *dev_priv = to_i915(dev);
1899 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001900 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001901 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001902 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001903 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001904 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001905
Jesse Barnesde151cf2008-11-12 10:03:55 -08001906 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001907 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001908
Chris Wilsondb53a302011-02-03 11:57:46 +00001909 trace_i915_gem_object_fault(obj, page_offset, true, write);
1910
Chris Wilson6e4930f2014-02-07 18:37:06 -02001911 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001912 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001913 * repeat the flush holding the lock in the normal manner to catch cases
1914 * where we are gazumped.
1915 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001916 ret = i915_gem_object_wait(obj,
1917 I915_WAIT_INTERRUPTIBLE,
1918 MAX_SCHEDULE_TIMEOUT,
1919 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001920 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001921 goto err;
1922
Chris Wilson40e62d52016-10-28 13:58:41 +01001923 ret = i915_gem_object_pin_pages(obj);
1924 if (ret)
1925 goto err;
1926
Chris Wilsonb8f90962016-08-05 10:14:07 +01001927 intel_runtime_pm_get(dev_priv);
1928
1929 ret = i915_mutex_lock_interruptible(dev);
1930 if (ret)
1931 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001932
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001933 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001934 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001935 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001936 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001937 }
1938
Chris Wilson82118872016-08-18 17:17:05 +01001939 /* If the object is smaller than a couple of partial vma, it is
1940 * not worth only creating a single partial vma - we may as well
1941 * clear enough space for the full object.
1942 */
1943 flags = PIN_MAPPABLE;
1944 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1945 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1946
Chris Wilsona61007a2016-08-18 17:17:02 +01001947 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001948 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001949 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001950 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001951 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001952 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001953
Chris Wilson50349242016-08-18 17:17:04 +01001954 /* Userspace is now writing through an untracked VMA, abandon
1955 * all hope that the hardware is able to track future writes.
1956 */
1957 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1958
Chris Wilsona61007a2016-08-18 17:17:02 +01001959 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1960 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001961 if (IS_ERR(vma)) {
1962 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001963 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001964 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001965
Chris Wilsonc9839302012-11-20 10:45:17 +00001966 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1967 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001968 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001969
Chris Wilson3bd40732017-10-09 09:43:56 +01001970 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001971 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001972 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001973
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001974 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001975 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001976 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001977 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001978 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001979 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001980 if (ret)
1981 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001982
Chris Wilsona65adaf2017-10-09 09:43:57 +01001983 /* Mark as being mmapped into userspace for later revocation */
1984 assert_rpm_wakelock_held(dev_priv);
1985 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1986 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1987 GEM_BUG_ON(!obj->userfault_count);
1988
Chris Wilson71253972017-12-06 12:49:14 +00001989 i915_vma_set_ggtt_write(vma);
1990
Chris Wilsona65adaf2017-10-09 09:43:57 +01001991err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001992 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001993err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001994 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001995err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001996 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001997err_rpm:
1998 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001999 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002000err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002001 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002002 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002003 /*
2004 * We eat errors when the gpu is terminally wedged to avoid
2005 * userspace unduly crashing (gl has no provisions for mmaps to
2006 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2007 * and so needs to be reported.
2008 */
2009 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002010 ret = VM_FAULT_SIGBUS;
2011 break;
2012 }
Chris Wilson045e7692010-11-07 09:18:22 +00002013 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002014 /*
2015 * EAGAIN means the gpu is hung and we'll wait for the error
2016 * handler to reset everything when re-faulting in
2017 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002018 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002019 case 0:
2020 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002021 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002022 case -EBUSY:
2023 /*
2024 * EBUSY is ok: this just means that another thread
2025 * already did the job.
2026 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002027 ret = VM_FAULT_NOPAGE;
2028 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002029 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002030 ret = VM_FAULT_OOM;
2031 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002032 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002033 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002034 ret = VM_FAULT_SIGBUS;
2035 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002036 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002037 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002038 ret = VM_FAULT_SIGBUS;
2039 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002040 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002041 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002042}
2043
Chris Wilsona65adaf2017-10-09 09:43:57 +01002044static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2045{
2046 struct i915_vma *vma;
2047
2048 GEM_BUG_ON(!obj->userfault_count);
2049
2050 obj->userfault_count = 0;
2051 list_del(&obj->userfault_link);
2052 drm_vma_node_unmap(&obj->base.vma_node,
2053 obj->base.dev->anon_inode->i_mapping);
2054
Chris Wilsone2189dd2017-12-07 21:14:07 +00002055 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002056 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002057}
2058
Jesse Barnesde151cf2008-11-12 10:03:55 -08002059/**
Chris Wilson901782b2009-07-10 08:18:50 +01002060 * i915_gem_release_mmap - remove physical page mappings
2061 * @obj: obj in question
2062 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002063 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002064 * relinquish ownership of the pages back to the system.
2065 *
2066 * It is vital that we remove the page mapping if we have mapped a tiled
2067 * object through the GTT and then lose the fence register due to
2068 * resource pressure. Similarly if the object has been moved out of the
2069 * aperture, than pages mapped into userspace must be revoked. Removing the
2070 * mapping will then trigger a page fault on the next user access, allowing
2071 * fixup by i915_gem_fault().
2072 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002073void
Chris Wilson05394f32010-11-08 19:18:58 +00002074i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002075{
Chris Wilson275f0392016-10-24 13:42:14 +01002076 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002077
Chris Wilson349f2cc2016-04-13 17:35:12 +01002078 /* Serialisation between user GTT access and our code depends upon
2079 * revoking the CPU's PTE whilst the mutex is held. The next user
2080 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002081 *
2082 * Note that RPM complicates somewhat by adding an additional
2083 * requirement that operations to the GGTT be made holding the RPM
2084 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002085 */
Chris Wilson275f0392016-10-24 13:42:14 +01002086 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002087 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002088
Chris Wilsona65adaf2017-10-09 09:43:57 +01002089 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002090 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002091
Chris Wilsona65adaf2017-10-09 09:43:57 +01002092 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002093
2094 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2095 * memory transactions from userspace before we return. The TLB
2096 * flushing implied above by changing the PTE above *should* be
2097 * sufficient, an extra barrier here just provides us with a bit
2098 * of paranoid documentation about our requirement to serialise
2099 * memory writes before touching registers / GSM.
2100 */
2101 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002102
2103out:
2104 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002105}
2106
Chris Wilson7c108fd2016-10-24 13:42:18 +01002107void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002108{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002109 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002110 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002111
Chris Wilson3594a3e2016-10-24 13:42:16 +01002112 /*
2113 * Only called during RPM suspend. All users of the userfault_list
2114 * must be holding an RPM wakeref to ensure that this can not
2115 * run concurrently with themselves (and use the struct_mutex for
2116 * protection between themselves).
2117 */
2118
2119 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002120 &dev_priv->mm.userfault_list, userfault_link)
2121 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002122
2123 /* The fence will be lost when the device powers down. If any were
2124 * in use by hardware (i.e. they are pinned), we should not be powering
2125 * down! All other fences will be reacquired by the user upon waking.
2126 */
2127 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2128 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2129
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002130 /* Ideally we want to assert that the fence register is not
2131 * live at this point (i.e. that no piece of code will be
2132 * trying to write through fence + GTT, as that both violates
2133 * our tracking of activity and associated locking/barriers,
2134 * but also is illegal given that the hw is powered down).
2135 *
2136 * Previously we used reg->pin_count as a "liveness" indicator.
2137 * That is not sufficient, and we need a more fine-grained
2138 * tool if we want to have a sanity check here.
2139 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002140
2141 if (!reg->vma)
2142 continue;
2143
Chris Wilsona65adaf2017-10-09 09:43:57 +01002144 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002145 reg->dirty = true;
2146 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002147}
2148
Chris Wilsond8cb5082012-08-11 15:41:03 +01002149static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2150{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002151 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002152 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002153
Chris Wilsonf3f61842016-08-05 10:14:14 +01002154 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002155 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002156 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002157
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002158 /* Attempt to reap some mmap space from dead objects */
2159 do {
2160 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2161 if (err)
2162 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002163
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002164 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002165 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002166 if (!err)
2167 break;
2168
2169 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002170
Chris Wilsonf3f61842016-08-05 10:14:14 +01002171 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002172}
2173
2174static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2175{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002176 drm_gem_free_mmap_offset(&obj->base);
2177}
2178
Dave Airlieda6b51d2014-12-24 13:11:17 +10002179int
Dave Airlieff72145b2011-02-07 12:16:14 +10002180i915_gem_mmap_gtt(struct drm_file *file,
2181 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002182 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002183 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002184{
Chris Wilson05394f32010-11-08 19:18:58 +00002185 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002186 int ret;
2187
Chris Wilson03ac0642016-07-20 13:31:51 +01002188 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002189 if (!obj)
2190 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002191
Chris Wilsond8cb5082012-08-11 15:41:03 +01002192 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002193 if (ret == 0)
2194 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002195
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002196 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002197 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002198}
2199
Dave Airlieff72145b2011-02-07 12:16:14 +10002200/**
2201 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2202 * @dev: DRM device
2203 * @data: GTT mapping ioctl data
2204 * @file: GEM object info
2205 *
2206 * Simply returns the fake offset to userspace so it can mmap it.
2207 * The mmap call will end up in drm_gem_mmap(), which will set things
2208 * up so we can get faults in the handler above.
2209 *
2210 * The fault handler will take care of binding the object into the GTT
2211 * (since it may have been evicted to make room for something), allocating
2212 * a fence register, and mapping the appropriate aperture address into
2213 * userspace.
2214 */
2215int
2216i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2217 struct drm_file *file)
2218{
2219 struct drm_i915_gem_mmap_gtt *args = data;
2220
Dave Airlieda6b51d2014-12-24 13:11:17 +10002221 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002222}
2223
Daniel Vetter225067e2012-08-20 10:23:20 +02002224/* Immediately discard the backing storage */
2225static void
2226i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002227{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002228 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002229
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002230 if (obj->base.filp == NULL)
2231 return;
2232
Daniel Vetter225067e2012-08-20 10:23:20 +02002233 /* Our goal here is to return as much of the memory as
2234 * is possible back to the system as we are called from OOM.
2235 * To do this we must instruct the shmfs to drop all of its
2236 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002237 */
Chris Wilson55372522014-03-25 13:23:06 +00002238 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002239 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002240 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002241}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002242
Chris Wilson55372522014-03-25 13:23:06 +00002243/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002244void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002245{
Chris Wilson55372522014-03-25 13:23:06 +00002246 struct address_space *mapping;
2247
Chris Wilson1233e2d2016-10-28 13:58:37 +01002248 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002249 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002250
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002251 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002252 case I915_MADV_DONTNEED:
2253 i915_gem_object_truncate(obj);
2254 case __I915_MADV_PURGED:
2255 return;
2256 }
2257
2258 if (obj->base.filp == NULL)
2259 return;
2260
Al Viro93c76a32015-12-04 23:45:44 -05002261 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002262 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002263}
2264
Chris Wilson5cdf5882010-09-27 15:51:07 +01002265static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002266i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2267 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002268{
Dave Gordon85d12252016-05-20 11:54:06 +01002269 struct sgt_iter sgt_iter;
2270 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002271
Chris Wilsone5facdf2016-12-23 14:57:57 +00002272 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002273
Chris Wilson03ac84f2016-10-28 13:58:36 +01002274 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002275
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002276 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002277 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002278
Chris Wilson03ac84f2016-10-28 13:58:36 +01002279 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002280 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002281 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002282
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002283 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002284 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002285
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002286 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002287 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002288 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002289
Chris Wilson03ac84f2016-10-28 13:58:36 +01002290 sg_free_table(pages);
2291 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002292}
2293
Chris Wilson96d77632016-10-28 13:58:33 +01002294static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2295{
2296 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002297 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002298
Chris Wilsonbea6e982017-10-26 14:00:31 +01002299 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002300 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2301 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002302 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002303}
2304
Chris Wilson548625e2016-11-01 12:11:34 +00002305void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2306 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002307{
Chris Wilsonf2123812017-10-16 12:40:37 +01002308 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002309 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002310
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002311 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002312 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002313
Chris Wilson15717de2016-08-04 07:52:26 +01002314 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002315 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002316 return;
2317
2318 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002319 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002320 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2321 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002322
Chris Wilsona2165e32012-12-03 11:49:00 +00002323 /* ->put_pages might need to allocate memory for the bit17 swizzle
2324 * array, hence protect them from being reaped by removing them from gtt
2325 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002326 pages = fetch_and_zero(&obj->mm.pages);
2327 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002328
Chris Wilsonf2123812017-10-16 12:40:37 +01002329 spin_lock(&i915->mm.obj_lock);
2330 list_del(&obj->mm.link);
2331 spin_unlock(&i915->mm.obj_lock);
2332
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002333 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002334 void *ptr;
2335
Chris Wilson0ce81782017-05-17 13:09:59 +01002336 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002337 if (is_vmalloc_addr(ptr))
2338 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002339 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002340 kunmap(kmap_to_page(ptr));
2341
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002342 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002343 }
2344
Chris Wilson96d77632016-10-28 13:58:33 +01002345 __i915_gem_object_reset_page_iter(obj);
2346
Chris Wilson4e5462e2017-03-07 13:20:31 +00002347 if (!IS_ERR(pages))
2348 obj->ops->put_pages(obj, pages);
2349
Matthew Aulda5c081662017-10-06 23:18:18 +01002350 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2351
Chris Wilson1233e2d2016-10-28 13:58:37 +01002352unlock:
2353 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002354}
2355
Chris Wilson935a2f72017-02-13 17:15:13 +00002356static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002357{
2358 struct sg_table new_st;
2359 struct scatterlist *sg, *new_sg;
2360 unsigned int i;
2361
2362 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002363 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002364
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002365 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002366 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002367
2368 new_sg = new_st.sgl;
2369 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2370 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2371 /* called before being DMA mapped, no need to copy sg->dma_* */
2372 new_sg = sg_next(new_sg);
2373 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002374 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002375
2376 sg_free_table(orig_st);
2377
2378 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002379 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002380}
2381
Matthew Auldb91b09e2017-10-06 23:18:17 +01002382static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002383{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002384 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002385 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2386 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002387 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002388 struct sg_table *st;
2389 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002390 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002391 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002392 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002393 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002394 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002395 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002396 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002397
Chris Wilson6c085a72012-08-20 11:40:46 +02002398 /* Assert that the object is not currently in any GPU domain. As it
2399 * wasn't in the GTT, there shouldn't be any way it could have been in
2400 * a GPU cache
2401 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002402 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2403 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002404
Chris Wilson9da3da62012-06-01 15:20:22 +01002405 st = kmalloc(sizeof(*st), GFP_KERNEL);
2406 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002407 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002408
Chris Wilsond766ef52016-12-19 12:43:45 +00002409rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002410 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002411 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002412 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002413 }
2414
2415 /* Get the list of pages out of our struct file. They'll be pinned
2416 * at this point until we release them.
2417 *
2418 * Fail silently without starting the shrinker
2419 */
Al Viro93c76a32015-12-04 23:45:44 -05002420 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002421 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002422 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2423
Imre Deak90797e62013-02-18 19:28:03 +02002424 sg = st->sgl;
2425 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002426 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002427 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002428 const unsigned int shrink[] = {
2429 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2430 0,
2431 }, *s = shrink;
2432 gfp_t gfp = noreclaim;
2433
2434 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002435 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002436 if (likely(!IS_ERR(page)))
2437 break;
2438
2439 if (!*s) {
2440 ret = PTR_ERR(page);
2441 goto err_sg;
2442 }
2443
Chris Wilson912d5722017-09-06 16:19:30 -07002444 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002445 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002446
Chris Wilson6c085a72012-08-20 11:40:46 +02002447 /* We've tried hard to allocate the memory by reaping
2448 * our own buffer, now let the real VM do its job and
2449 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002450 *
2451 * However, since graphics tend to be disposable,
2452 * defer the oom here by reporting the ENOMEM back
2453 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002454 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002455 if (!*s) {
2456 /* reclaim and warn, but no oom */
2457 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002458
2459 /* Our bo are always dirty and so we require
2460 * kswapd to reclaim our pages (direct reclaim
2461 * does not effectively begin pageout of our
2462 * buffers on its own). However, direct reclaim
2463 * only waits for kswapd when under allocation
2464 * congestion. So as a result __GFP_RECLAIM is
2465 * unreliable and fails to actually reclaim our
2466 * dirty pages -- unless you try over and over
2467 * again with !__GFP_NORETRY. However, we still
2468 * want to fail this allocation rather than
2469 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002470 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002471 */
Michal Hockodbb32952017-07-12 14:36:55 -07002472 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002473 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002474 } while (1);
2475
Chris Wilson871dfbd2016-10-11 09:20:21 +01002476 if (!i ||
2477 sg->length >= max_segment ||
2478 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002479 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002480 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002481 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002482 }
Imre Deak90797e62013-02-18 19:28:03 +02002483 st->nents++;
2484 sg_set_page(sg, page, PAGE_SIZE, 0);
2485 } else {
2486 sg->length += PAGE_SIZE;
2487 }
2488 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002489
2490 /* Check that the i965g/gm workaround works. */
2491 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002492 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002493 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002494 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002495 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002496 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002497
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002498 /* Trim unused sg entries to avoid wasting memory. */
2499 i915_sg_trim(st);
2500
Chris Wilson03ac84f2016-10-28 13:58:36 +01002501 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002502 if (ret) {
2503 /* DMA remapping failed? One possible cause is that
2504 * it could not reserve enough large entries, asking
2505 * for PAGE_SIZE chunks instead may be helpful.
2506 */
2507 if (max_segment > PAGE_SIZE) {
2508 for_each_sgt_page(page, sgt_iter, st)
2509 put_page(page);
2510 sg_free_table(st);
2511
2512 max_segment = PAGE_SIZE;
2513 goto rebuild_st;
2514 } else {
2515 dev_warn(&dev_priv->drm.pdev->dev,
2516 "Failed to DMA remap %lu pages\n",
2517 page_count);
2518 goto err_pages;
2519 }
2520 }
Imre Deake2273302015-07-09 12:59:05 +03002521
Eric Anholt673a3942008-07-30 12:06:12 -07002522 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002523 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002524
Matthew Auld84e89782017-10-09 12:00:24 +01002525 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002526
2527 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002528
Chris Wilsonb17993b2016-11-14 11:29:30 +00002529err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002530 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002531err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002532 for_each_sgt_page(page, sgt_iter, st)
2533 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002534 sg_free_table(st);
2535 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002536
2537 /* shmemfs first checks if there is enough memory to allocate the page
2538 * and reports ENOSPC should there be insufficient, along with the usual
2539 * ENOMEM for a genuine allocation failure.
2540 *
2541 * We use ENOSPC in our driver to mean that we have run out of aperture
2542 * space and so want to translate the error from shmemfs back to our
2543 * usual understanding of ENOMEM.
2544 */
Imre Deake2273302015-07-09 12:59:05 +03002545 if (ret == -ENOSPC)
2546 ret = -ENOMEM;
2547
Matthew Auldb91b09e2017-10-06 23:18:17 +01002548 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002549}
2550
2551void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002552 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002553 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002554{
Matthew Aulda5c081662017-10-06 23:18:18 +01002555 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2556 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2557 int i;
2558
Chris Wilson1233e2d2016-10-28 13:58:37 +01002559 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002560
2561 obj->mm.get_page.sg_pos = pages->sgl;
2562 obj->mm.get_page.sg_idx = 0;
2563
2564 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002565
2566 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002567 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002568 GEM_BUG_ON(obj->mm.quirked);
2569 __i915_gem_object_pin_pages(obj);
2570 obj->mm.quirked = true;
2571 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002572
Matthew Auld84e89782017-10-09 12:00:24 +01002573 GEM_BUG_ON(!sg_page_sizes);
2574 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002575
2576 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002577 * Calculate the supported page-sizes which fit into the given
2578 * sg_page_sizes. This will give us the page-sizes which we may be able
2579 * to use opportunistically when later inserting into the GTT. For
2580 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2581 * 64K or 4K pages, although in practice this will depend on a number of
2582 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002583 */
2584 obj->mm.page_sizes.sg = 0;
2585 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2586 if (obj->mm.page_sizes.phys & ~0u << i)
2587 obj->mm.page_sizes.sg |= BIT(i);
2588 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002589 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002590
2591 spin_lock(&i915->mm.obj_lock);
2592 list_add(&obj->mm.link, &i915->mm.unbound_list);
2593 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002594}
2595
2596static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2597{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002598 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002599
2600 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2601 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2602 return -EFAULT;
2603 }
2604
Matthew Auldb91b09e2017-10-06 23:18:17 +01002605 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002606 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002607
Matthew Auldb91b09e2017-10-06 23:18:17 +01002608 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002609}
2610
Chris Wilson37e680a2012-06-07 15:38:42 +01002611/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002612 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002613 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002614 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002615 * either as a result of memory pressure (reaping pages under the shrinker)
2616 * or as the object is itself released.
2617 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002618int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002619{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002620 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002621
Chris Wilson1233e2d2016-10-28 13:58:37 +01002622 err = mutex_lock_interruptible(&obj->mm.lock);
2623 if (err)
2624 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002625
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002626 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002627 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2628
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002629 err = ____i915_gem_object_get_pages(obj);
2630 if (err)
2631 goto unlock;
2632
2633 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002634 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002635 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002636
Chris Wilson1233e2d2016-10-28 13:58:37 +01002637unlock:
2638 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002639 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002640}
2641
Dave Gordondd6034c2016-05-20 11:54:04 +01002642/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002643static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2644 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002645{
2646 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002647 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002648 struct sgt_iter sgt_iter;
2649 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002650 struct page *stack_pages[32];
2651 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002652 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002653 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002654 void *addr;
2655
2656 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002657 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002658 return kmap(sg_page(sgt->sgl));
2659
Dave Gordonb338fa42016-05-20 11:54:05 +01002660 if (n_pages > ARRAY_SIZE(stack_pages)) {
2661 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002662 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002663 if (!pages)
2664 return NULL;
2665 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002666
Dave Gordon85d12252016-05-20 11:54:06 +01002667 for_each_sgt_page(page, sgt_iter, sgt)
2668 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002669
2670 /* Check that we have the expected number of pages */
2671 GEM_BUG_ON(i != n_pages);
2672
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002673 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002674 default:
2675 MISSING_CASE(type);
2676 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002677 case I915_MAP_WB:
2678 pgprot = PAGE_KERNEL;
2679 break;
2680 case I915_MAP_WC:
2681 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2682 break;
2683 }
2684 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002685
Dave Gordonb338fa42016-05-20 11:54:05 +01002686 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002687 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002688
2689 return addr;
2690}
2691
2692/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002693void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2694 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002695{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002696 enum i915_map_type has_type;
2697 bool pinned;
2698 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002699 int ret;
2700
Tina Zhanga03f3952017-11-14 10:25:13 +00002701 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2702 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002703
Chris Wilson1233e2d2016-10-28 13:58:37 +01002704 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002705 if (ret)
2706 return ERR_PTR(ret);
2707
Chris Wilsona575c672017-08-28 11:46:31 +01002708 pinned = !(type & I915_MAP_OVERRIDE);
2709 type &= ~I915_MAP_OVERRIDE;
2710
Chris Wilson1233e2d2016-10-28 13:58:37 +01002711 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002712 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002713 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2714
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002715 ret = ____i915_gem_object_get_pages(obj);
2716 if (ret)
2717 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002718
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002719 smp_mb__before_atomic();
2720 }
2721 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002722 pinned = false;
2723 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002724 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002725
Chris Wilson0ce81782017-05-17 13:09:59 +01002726 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002727 if (ptr && has_type != type) {
2728 if (pinned) {
2729 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002730 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002731 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002732
2733 if (is_vmalloc_addr(ptr))
2734 vunmap(ptr);
2735 else
2736 kunmap(kmap_to_page(ptr));
2737
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002738 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002739 }
2740
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002741 if (!ptr) {
2742 ptr = i915_gem_object_map(obj, type);
2743 if (!ptr) {
2744 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002745 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002746 }
2747
Chris Wilson0ce81782017-05-17 13:09:59 +01002748 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002749 }
2750
Chris Wilson1233e2d2016-10-28 13:58:37 +01002751out_unlock:
2752 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002753 return ptr;
2754
Chris Wilson1233e2d2016-10-28 13:58:37 +01002755err_unpin:
2756 atomic_dec(&obj->mm.pages_pin_count);
2757err_unlock:
2758 ptr = ERR_PTR(ret);
2759 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002760}
2761
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002762static int
2763i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2764 const struct drm_i915_gem_pwrite *arg)
2765{
2766 struct address_space *mapping = obj->base.filp->f_mapping;
2767 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2768 u64 remain, offset;
2769 unsigned int pg;
2770
2771 /* Before we instantiate/pin the backing store for our use, we
2772 * can prepopulate the shmemfs filp efficiently using a write into
2773 * the pagecache. We avoid the penalty of instantiating all the
2774 * pages, important if the user is just writing to a few and never
2775 * uses the object on the GPU, and using a direct write into shmemfs
2776 * allows it to avoid the cost of retrieving a page (either swapin
2777 * or clearing-before-use) before it is overwritten.
2778 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002779 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002780 return -ENODEV;
2781
Chris Wilsona6d65e42017-10-16 21:27:32 +01002782 if (obj->mm.madv != I915_MADV_WILLNEED)
2783 return -EFAULT;
2784
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002785 /* Before the pages are instantiated the object is treated as being
2786 * in the CPU domain. The pages will be clflushed as required before
2787 * use, and we can freely write into the pages directly. If userspace
2788 * races pwrite with any other operation; corruption will ensue -
2789 * that is userspace's prerogative!
2790 */
2791
2792 remain = arg->size;
2793 offset = arg->offset;
2794 pg = offset_in_page(offset);
2795
2796 do {
2797 unsigned int len, unwritten;
2798 struct page *page;
2799 void *data, *vaddr;
2800 int err;
2801
2802 len = PAGE_SIZE - pg;
2803 if (len > remain)
2804 len = remain;
2805
2806 err = pagecache_write_begin(obj->base.filp, mapping,
2807 offset, len, 0,
2808 &page, &data);
2809 if (err < 0)
2810 return err;
2811
2812 vaddr = kmap(page);
2813 unwritten = copy_from_user(vaddr + pg, user_data, len);
2814 kunmap(page);
2815
2816 err = pagecache_write_end(obj->base.filp, mapping,
2817 offset, len, len - unwritten,
2818 page, data);
2819 if (err < 0)
2820 return err;
2821
2822 if (unwritten)
2823 return -EFAULT;
2824
2825 remain -= len;
2826 user_data += len;
2827 offset += len;
2828 pg = 0;
2829 } while (remain);
2830
2831 return 0;
2832}
2833
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002834static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002835{
Chris Wilson77b25a92017-07-21 13:32:30 +01002836 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002837
Chris Wilson77b25a92017-07-21 13:32:30 +01002838 atomic_inc(&ctx->guilty_count);
2839
Chris Wilson24eae082018-02-05 09:22:01 +00002840 banned = false;
2841 if (i915_gem_context_is_bannable(ctx)) {
2842 unsigned int score;
2843
2844 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2845 &ctx->ban_score);
2846 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2847
2848 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2849 ctx->name, score, yesno(banned));
2850 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002851 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002852 return;
2853
Chris Wilson77b25a92017-07-21 13:32:30 +01002854 i915_gem_context_set_banned(ctx);
2855 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2856 atomic_inc(&ctx->file_priv->context_bans);
2857 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2858 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2859 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002860}
2861
2862static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2863{
Chris Wilson77b25a92017-07-21 13:32:30 +01002864 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002865}
2866
Chris Wilsone61e0f52018-02-21 09:56:36 +00002867struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002868i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002869{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002870 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002871 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002872
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002873 /* We are called by the error capture and reset at a random
2874 * point in time. In particular, note that neither is crucially
2875 * ordered with an interrupt. After a hang, the GPU is dead and we
2876 * assume that no more writes can happen (we waited long enough for
2877 * all writes that were in transaction to be flushed) - adding an
2878 * extra delay for a recent interrupt is pointless. Hence, we do
2879 * not need an engine->irq_seqno_barrier() before the seqno reads.
2880 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002881 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002882 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002883 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002884 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002885
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002886 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002887 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2888 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002889
Chris Wilson754c9fd2017-02-23 07:44:14 +00002890 active = request;
2891 break;
2892 }
2893 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2894
2895 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002896}
2897
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002898static bool engine_stalled(struct intel_engine_cs *engine)
2899{
2900 if (!engine->hangcheck.stalled)
2901 return false;
2902
2903 /* Check for possible seqno movement after hang declaration */
2904 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2905 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2906 return false;
2907 }
2908
2909 return true;
2910}
2911
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002912/*
2913 * Ensure irq handler finishes, and not run again.
2914 * Also return the active request so that we only search for it once.
2915 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002916struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002917i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2918{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002919 struct i915_request *request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002920
Chris Wilson1749d902017-10-09 12:02:59 +01002921 /*
2922 * During the reset sequence, we must prevent the engine from
2923 * entering RC6. As the context state is undefined until we restart
2924 * the engine, if it does enter RC6 during the reset, the state
2925 * written to the powercontext is undefined and so we may lose
2926 * GPU state upon resume, i.e. fail to restart after a reset.
2927 */
2928 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2929
2930 /*
2931 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002932 * state (by calling dma_fence_signal) as we are processing
2933 * the reset. The write from the GPU of the seqno is
2934 * asynchronous and the signaler thread may see a different
2935 * value to us and declare the request complete, even though
2936 * the reset routine have picked that request as the active
2937 * (incomplete) request. This conflict is not handled
2938 * gracefully!
2939 */
2940 kthread_park(engine->breadcrumbs.signaler);
2941
Chris Wilson1749d902017-10-09 12:02:59 +01002942 /*
2943 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002944 * completed the reset in i915_gem_reset_finish(). If a request
2945 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302946 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002947 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302948 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002949 * prevents the race.
2950 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302951 tasklet_kill(&engine->execlists.tasklet);
2952 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002953
Michał Winiarskic41937f2017-10-26 15:35:58 +02002954 /*
2955 * We're using worker to queue preemption requests from the tasklet in
2956 * GuC submission mode.
2957 * Even though tasklet was disabled, we may still have a worker queued.
2958 * Let's make sure that all workers scheduled before disabling the
2959 * tasklet are completed before continuing with the reset.
2960 */
2961 if (engine->i915->guc.preempt_wq)
2962 flush_workqueue(engine->i915->guc.preempt_wq);
2963
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002964 if (engine->irq_seqno_barrier)
2965 engine->irq_seqno_barrier(engine);
2966
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002967 request = i915_gem_find_active_request(engine);
2968 if (request && request->fence.error == -EIO)
2969 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002970
2971 return request;
2972}
2973
Chris Wilson0e178ae2017-01-17 17:59:06 +02002974int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002975{
2976 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00002977 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002978 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002979 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002980
Chris Wilson0e178ae2017-01-17 17:59:06 +02002981 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002982 request = i915_gem_reset_prepare_engine(engine);
2983 if (IS_ERR(request)) {
2984 err = PTR_ERR(request);
2985 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002986 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002987
2988 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002989 }
2990
Chris Wilson4c965542017-01-17 17:59:01 +02002991 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002992
2993 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002994}
2995
Chris Wilsone61e0f52018-02-21 09:56:36 +00002996static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002997{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002998 void *vaddr = request->ring->vaddr;
2999 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003000
Chris Wilson821ed7d2016-09-09 14:11:53 +01003001 /* As this request likely depends on state from the lost
3002 * context, clear out all the user operations leaving the
3003 * breadcrumb at the end (so we get the fence notifications).
3004 */
3005 head = request->head;
3006 if (request->postfix < head) {
3007 memset(vaddr + head, 0, request->ring->size - head);
3008 head = 0;
3009 }
3010 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003011
3012 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003013}
3014
Chris Wilsone61e0f52018-02-21 09:56:36 +00003015static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003016{
3017 struct intel_engine_cs *engine = request->engine;
3018 struct i915_gem_context *hung_ctx = request->ctx;
3019 struct intel_timeline *timeline;
3020 unsigned long flags;
3021
3022 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3023
3024 spin_lock_irqsave(&engine->timeline->lock, flags);
3025 spin_lock(&timeline->lock);
3026
3027 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3028 if (request->ctx == hung_ctx)
3029 skip_request(request);
3030
3031 list_for_each_entry(request, &timeline->requests, link)
3032 skip_request(request);
3033
3034 spin_unlock(&timeline->lock);
3035 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3036}
3037
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003038/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003039static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003040i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +00003041 struct i915_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003042{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003043 /* The guilty request will get skipped on a hung engine.
3044 *
3045 * Users of client default contexts do not rely on logical
3046 * state preserved between batches so it is safe to execute
3047 * queued requests following the hang. Non default contexts
3048 * rely on preserved state, so skipping a batch loses the
3049 * evolution of the state and it needs to be considered corrupted.
3050 * Executing more queued batches on top of corrupted state is
3051 * risky. But we take the risk by trying to advance through
3052 * the queued requests in order to make the client behaviour
3053 * more predictable around resets, by not throwing away random
3054 * amount of batches it has prepared for execution. Sophisticated
3055 * clients can use gem_reset_stats_ioctl and dma fence status
3056 * (exported via sync_file info ioctl on explicit fences) to observe
3057 * when it loses the context state and should rebuild accordingly.
3058 *
3059 * The context ban, and ultimately the client ban, mechanism are safety
3060 * valves if client submission ends up resulting in nothing more than
3061 * subsequent hangs.
3062 */
3063
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003064 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003065 i915_gem_context_mark_guilty(request->ctx);
3066 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003067
3068 /* If this context is now banned, skip all pending requests. */
3069 if (i915_gem_context_is_banned(request->ctx))
3070 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003071 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003072 /*
3073 * Since this is not the hung engine, it may have advanced
3074 * since the hang declaration. Double check by refinding
3075 * the active request at the time of the reset.
3076 */
3077 request = i915_gem_find_active_request(engine);
3078 if (request) {
3079 i915_gem_context_mark_innocent(request->ctx);
3080 dma_fence_set_error(&request->fence, -EAGAIN);
3081
3082 /* Rewind the engine to replay the incomplete rq */
3083 spin_lock_irq(&engine->timeline->lock);
3084 request = list_prev_entry(request, link);
3085 if (&request->link == &engine->timeline->requests)
3086 request = NULL;
3087 spin_unlock_irq(&engine->timeline->lock);
3088 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003089 }
3090
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003091 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003092}
3093
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003094void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +00003095 struct i915_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003096{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003097 /*
3098 * Make sure this write is visible before we re-enable the interrupt
3099 * handlers on another CPU, as tasklet_enable() resolves to just
3100 * a compiler barrier which is insufficient for our purpose here.
3101 */
3102 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003103
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003104 if (request)
3105 request = i915_gem_reset_request(engine, request);
3106
3107 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003108 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3109 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003110 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003111
3112 /* Setup the CS to resume from the breadcrumb of the hung request */
3113 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003114}
3115
Chris Wilsond8027092017-02-08 14:30:32 +00003116void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003117{
3118 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303119 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003120
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003121 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3122
Chris Wilsone61e0f52018-02-21 09:56:36 +00003123 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003124
Chris Wilson2ae55732017-02-12 17:20:02 +00003125 for_each_engine(engine, dev_priv, id) {
3126 struct i915_gem_context *ctx;
3127
Michel Thierryc64992e2017-06-20 10:57:44 +01003128 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003129 ctx = fetch_and_zero(&engine->last_retired_context);
3130 if (ctx)
3131 engine->context_unpin(engine, ctx);
Chris Wilson7b6da812017-12-16 00:03:34 +00003132
3133 /*
3134 * Ostensibily, we always want a context loaded for powersaving,
3135 * so if the engine is idle after the reset, send a request
3136 * to load our scratch kernel_context.
3137 *
3138 * More mysteriously, if we leave the engine idle after a reset,
3139 * the next userspace batch may hang, with what appears to be
3140 * an incoherent read by the CS (presumably stale TLB). An
3141 * empty request appears sufficient to paper over the glitch.
3142 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003143 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003144 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003145
Chris Wilsone61e0f52018-02-21 09:56:36 +00003146 rq = i915_request_alloc(engine,
3147 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003148 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003149 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003150 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003151 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003152
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003153 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003154
3155 if (dev_priv->gt.awake) {
3156 intel_sanitize_gt_powersave(dev_priv);
3157 intel_enable_gt_powersave(dev_priv);
3158 if (INTEL_GEN(dev_priv) >= 6)
3159 gen6_rps_busy(dev_priv);
3160 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003161}
3162
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003163void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3164{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303165 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003166 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003167
3168 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003169}
3170
Chris Wilsond8027092017-02-08 14:30:32 +00003171void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3172{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003173 struct intel_engine_cs *engine;
3174 enum intel_engine_id id;
3175
Chris Wilsond8027092017-02-08 14:30:32 +00003176 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003177
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003178 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003179 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003180 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003181 }
Chris Wilsond8027092017-02-08 14:30:32 +00003182}
3183
Chris Wilsone61e0f52018-02-21 09:56:36 +00003184static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003185{
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003186 dma_fence_set_error(&request->fence, -EIO);
3187
Chris Wilsone61e0f52018-02-21 09:56:36 +00003188 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003189}
3190
Chris Wilsone61e0f52018-02-21 09:56:36 +00003191static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003192{
Chris Wilson8d550822017-10-06 12:56:17 +01003193 unsigned long flags;
3194
Chris Wilson3cd94422017-01-10 17:22:45 +00003195 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003196
3197 spin_lock_irqsave(&request->engine->timeline->lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003198 __i915_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003199 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003200 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003201}
3202
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003203void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003204{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003205 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303206 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003207
Chris Wilson559e0402018-02-05 09:21:59 +00003208 if (drm_debug & DRM_UT_DRIVER) {
3209 struct drm_printer p = drm_debug_printer(__func__);
3210
3211 for_each_engine(engine, i915, id)
3212 intel_engine_dump(engine, &p, "%s\n", engine->name);
3213 }
3214
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003215 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3216 smp_mb__after_atomic();
3217
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003218 /*
3219 * First, stop submission to hw, but do not yet complete requests by
3220 * rolling the global seqno forward (since this would complete requests
3221 * for which we haven't set the fence error to EIO yet).
3222 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003223 for_each_engine(engine, i915, id) {
3224 i915_gem_reset_prepare_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003225 engine->submit_request = nop_submit_request;
Chris Wilson963ddd62018-03-02 11:33:24 +00003226 }
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003227
3228 /*
3229 * Make sure no one is running the old callback before we proceed with
3230 * cancelling requests and resetting the completion tracking. Otherwise
3231 * we might submit a request to the hardware which never completes.
3232 */
3233 synchronize_rcu();
3234
3235 for_each_engine(engine, i915, id) {
3236 /* Mark all executing requests as skipped */
3237 engine->cancel_requests(engine);
3238
3239 /*
3240 * Only once we've force-cancelled all in-flight requests can we
3241 * start to complete all requests.
3242 */
3243 engine->submit_request = nop_complete_submit_request;
Chris Wilson3fed1802018-02-07 21:05:43 +00003244 engine->schedule = NULL;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003245 }
3246
Chris Wilson3fed1802018-02-07 21:05:43 +00003247 i915->caps.scheduler = 0;
3248
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003249 /*
3250 * Make sure no request can slip through without getting completed by
3251 * either this call here to intel_engine_init_global_seqno, or the one
3252 * in nop_complete_submit_request.
3253 */
3254 synchronize_rcu();
3255
3256 for_each_engine(engine, i915, id) {
3257 unsigned long flags;
3258
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003259 /*
3260 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003261 * (lockless) lookup doesn't try and wait upon the request as we
3262 * reset it.
3263 */
3264 spin_lock_irqsave(&engine->timeline->lock, flags);
3265 intel_engine_init_global_seqno(engine,
3266 intel_engine_last_submit(engine));
3267 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003268
3269 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003270 }
Chris Wilson20e49332016-11-22 14:41:21 +00003271
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003272 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003273}
3274
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003275bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3276{
3277 struct i915_gem_timeline *tl;
3278 int i;
3279
3280 lockdep_assert_held(&i915->drm.struct_mutex);
3281 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3282 return true;
3283
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003284 /*
3285 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003286 * are flushed and errored out - we may have requests waiting upon
3287 * third party fences. We marked all inflight requests as EIO, and
3288 * every execbuf since returned EIO, for consistency we want all
3289 * the currently pending requests to also be marked as EIO, which
3290 * is done inside our nop_submit_request - and so we must wait.
3291 *
3292 * No more can be submitted until we reset the wedged bit.
3293 */
3294 list_for_each_entry(tl, &i915->gt.timelines, link) {
3295 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003296 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003297
3298 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3299 &i915->drm.struct_mutex);
3300 if (!rq)
3301 continue;
3302
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003303 /*
3304 * We can't use our normal waiter as we want to
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003305 * avoid recursively trying to handle the current
3306 * reset. The basic dma_fence_default_wait() installs
3307 * a callback for dma_fence_signal(), which is
3308 * triggered by our nop handler (indirectly, the
3309 * callback enables the signaler thread which is
3310 * woken by the nop_submit_request() advancing the seqno
3311 * and when the seqno passes the fence, the signaler
3312 * then signals the fence waking us up).
3313 */
3314 if (dma_fence_default_wait(&rq->fence, true,
3315 MAX_SCHEDULE_TIMEOUT) < 0)
3316 return false;
3317 }
3318 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003319 i915_retire_requests(i915);
3320 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003321
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003322 /*
3323 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003324 * being queued (by disallowing execbuf whilst wedged) so having
3325 * waited for all active requests above, we know the system is idle
3326 * and do not have to worry about a thread being inside
3327 * engine->submit_request() as we swap over. So unlike installing
3328 * the nop_submit_request on reset, we can do this from normal
3329 * context and do not require stop_machine().
3330 */
3331 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003332 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003333
3334 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3335 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3336
3337 return true;
3338}
3339
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003340static void
Eric Anholt673a3942008-07-30 12:06:12 -07003341i915_gem_retire_work_handler(struct work_struct *work)
3342{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003343 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003344 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003345 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003346
Chris Wilson891b48c2010-09-29 12:26:37 +01003347 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003348 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003349 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003350 mutex_unlock(&dev->struct_mutex);
3351 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003352
Chris Wilson88923042018-01-29 14:41:04 +00003353 /*
3354 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003355 * We do not need to do this test under locking as in the worst-case
3356 * we queue the retire worker once too often.
3357 */
Chris Wilson88923042018-01-29 14:41:04 +00003358 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003359 queue_delayed_work(dev_priv->wq,
3360 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003361 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003362}
Chris Wilson891b48c2010-09-29 12:26:37 +01003363
Chris Wilson84a10742018-01-24 11:36:08 +00003364static void shrink_caches(struct drm_i915_private *i915)
3365{
3366 /*
3367 * kmem_cache_shrink() discards empty slabs and reorders partially
3368 * filled slabs to prioritise allocating from the mostly full slabs,
3369 * with the aim of reducing fragmentation.
3370 */
3371 kmem_cache_shrink(i915->priorities);
3372 kmem_cache_shrink(i915->dependencies);
3373 kmem_cache_shrink(i915->requests);
3374 kmem_cache_shrink(i915->luts);
3375 kmem_cache_shrink(i915->vmas);
3376 kmem_cache_shrink(i915->objects);
3377}
3378
3379struct sleep_rcu_work {
3380 union {
3381 struct rcu_head rcu;
3382 struct work_struct work;
3383 };
3384 struct drm_i915_private *i915;
3385 unsigned int epoch;
3386};
3387
3388static inline bool
3389same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3390{
3391 /*
3392 * There is a small chance that the epoch wrapped since we started
3393 * sleeping. If we assume that epoch is at least a u32, then it will
3394 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3395 */
3396 return epoch == READ_ONCE(i915->gt.epoch);
3397}
3398
3399static void __sleep_work(struct work_struct *work)
3400{
3401 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3402 struct drm_i915_private *i915 = s->i915;
3403 unsigned int epoch = s->epoch;
3404
3405 kfree(s);
3406 if (same_epoch(i915, epoch))
3407 shrink_caches(i915);
3408}
3409
3410static void __sleep_rcu(struct rcu_head *rcu)
3411{
3412 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3413 struct drm_i915_private *i915 = s->i915;
3414
3415 if (same_epoch(i915, s->epoch)) {
3416 INIT_WORK(&s->work, __sleep_work);
3417 queue_work(i915->wq, &s->work);
3418 } else {
3419 kfree(s);
3420 }
3421}
3422
Chris Wilson5427f202017-10-23 22:32:34 +01003423static inline bool
3424new_requests_since_last_retire(const struct drm_i915_private *i915)
3425{
3426 return (READ_ONCE(i915->gt.active_requests) ||
3427 work_pending(&i915->gt.idle_work.work));
3428}
3429
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003430static void
3431i915_gem_idle_work_handler(struct work_struct *work)
3432{
3433 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003434 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003435 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003436 bool rearm_hangcheck;
3437
3438 if (!READ_ONCE(dev_priv->gt.awake))
3439 return;
3440
Imre Deak0cb56702016-11-07 11:20:04 +02003441 /*
3442 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003443 * new request is submitted. As we don't trust the hardware, we
3444 * continue on if the wait times out. This is necessary to allow
3445 * the machine to suspend even if the hardware dies, and we will
3446 * try to recover in resume (after depriving the hardware of power,
3447 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003448 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003449 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3450 intel_engines_are_idle(dev_priv),
3451 I915_IDLE_ENGINES_TIMEOUT * 1000,
3452 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003453
3454 rearm_hangcheck =
3455 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3456
Chris Wilson5427f202017-10-23 22:32:34 +01003457 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003458 /* Currently busy, come back later */
3459 mod_delayed_work(dev_priv->wq,
3460 &dev_priv->gt.idle_work,
3461 msecs_to_jiffies(50));
3462 goto out_rearm;
3463 }
3464
Imre Deak93c97dc2016-11-07 11:20:03 +02003465 /*
3466 * New request retired after this work handler started, extend active
3467 * period until next instance of the work.
3468 */
Chris Wilson5427f202017-10-23 22:32:34 +01003469 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003470 goto out_unlock;
3471
Chris Wilson5427f202017-10-23 22:32:34 +01003472 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003473 * Be paranoid and flush a concurrent interrupt to make sure
3474 * we don't reactivate any irq tasklets after parking.
3475 *
3476 * FIXME: Note that even though we have waited for execlists to be idle,
3477 * there may still be an in-flight interrupt even though the CSB
3478 * is now empty. synchronize_irq() makes sure that a residual interrupt
3479 * is completed before we continue, but it doesn't prevent the HW from
3480 * raising a spurious interrupt later. To complete the shield we should
3481 * coordinate disabling the CS irq with flushing the interrupts.
3482 */
3483 synchronize_irq(dev_priv->drm.irq);
3484
Chris Wilsonaba5e272017-10-25 15:39:41 +01003485 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003486 i915_gem_timelines_park(dev_priv);
3487
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003488 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003489
Chris Wilson67d97da2016-07-04 08:08:31 +01003490 GEM_BUG_ON(!dev_priv->gt.awake);
3491 dev_priv->gt.awake = false;
Chris Wilson84a10742018-01-24 11:36:08 +00003492 epoch = dev_priv->gt.epoch;
3493 GEM_BUG_ON(epoch == I915_EPOCH_INVALID);
Chris Wilson67d97da2016-07-04 08:08:31 +01003494 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003495
Chris Wilson67d97da2016-07-04 08:08:31 +01003496 if (INTEL_GEN(dev_priv) >= 6)
3497 gen6_rps_idle(dev_priv);
Tvrtko Ursulinb6876372017-12-05 13:28:54 +00003498
3499 intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ);
3500
Chris Wilson67d97da2016-07-04 08:08:31 +01003501 intel_runtime_pm_put(dev_priv);
3502out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003503 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003504
Chris Wilson67d97da2016-07-04 08:08:31 +01003505out_rearm:
3506 if (rearm_hangcheck) {
3507 GEM_BUG_ON(!dev_priv->gt.awake);
3508 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003509 }
Chris Wilson84a10742018-01-24 11:36:08 +00003510
3511 /*
3512 * When we are idle, it is an opportune time to reap our caches.
3513 * However, we have many objects that utilise RCU and the ordered
3514 * i915->wq that this work is executing on. To try and flush any
3515 * pending frees now we are idle, we first wait for an RCU grace
3516 * period, and then queue a task (that will run last on the wq) to
3517 * shrink and re-optimize the caches.
3518 */
3519 if (same_epoch(dev_priv, epoch)) {
3520 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3521 if (s) {
3522 s->i915 = dev_priv;
3523 s->epoch = epoch;
3524 call_rcu(&s->rcu, __sleep_rcu);
3525 }
3526 }
Eric Anholt673a3942008-07-30 12:06:12 -07003527}
3528
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003529void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3530{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003531 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003532 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3533 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003534 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003535
Chris Wilsond1b48c12017-08-16 09:52:08 +01003536 mutex_lock(&i915->drm.struct_mutex);
3537
3538 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3539 struct i915_gem_context *ctx = lut->ctx;
3540 struct i915_vma *vma;
3541
Chris Wilson432295d2017-08-22 12:05:15 +01003542 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003543 if (ctx->file_priv != fpriv)
3544 continue;
3545
3546 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003547 GEM_BUG_ON(vma->obj != obj);
3548
3549 /* We allow the process to have multiple handles to the same
3550 * vma, in the same fd namespace, by virtue of flink/open.
3551 */
3552 GEM_BUG_ON(!vma->open_count);
3553 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003554 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003555
Chris Wilsond1b48c12017-08-16 09:52:08 +01003556 list_del(&lut->obj_link);
3557 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003558
Chris Wilsond1b48c12017-08-16 09:52:08 +01003559 kmem_cache_free(i915->luts, lut);
3560 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003561 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003562
3563 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003564}
3565
Chris Wilsone95433c2016-10-28 13:58:27 +01003566static unsigned long to_wait_timeout(s64 timeout_ns)
3567{
3568 if (timeout_ns < 0)
3569 return MAX_SCHEDULE_TIMEOUT;
3570
3571 if (timeout_ns == 0)
3572 return 0;
3573
3574 return nsecs_to_jiffies_timeout(timeout_ns);
3575}
3576
Ben Widawsky5816d642012-04-11 11:18:19 -07003577/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003578 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003579 * @dev: drm device pointer
3580 * @data: ioctl data blob
3581 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003582 *
3583 * Returns 0 if successful, else an error is returned with the remaining time in
3584 * the timeout parameter.
3585 * -ETIME: object is still busy after timeout
3586 * -ERESTARTSYS: signal interrupted the wait
3587 * -ENONENT: object doesn't exist
3588 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003589 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003590 * -ENOMEM: damn
3591 * -ENODEV: Internal IRQ fail
3592 * -E?: The add request failed
3593 *
3594 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3595 * non-zero timeout parameter the wait ioctl will wait for the given number of
3596 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3597 * without holding struct_mutex the object may become re-busied before this
3598 * function completes. A similar but shorter * race condition exists in the busy
3599 * ioctl
3600 */
3601int
3602i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3603{
3604 struct drm_i915_gem_wait *args = data;
3605 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003606 ktime_t start;
3607 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003608
Daniel Vetter11b5d512014-09-29 15:31:26 +02003609 if (args->flags != 0)
3610 return -EINVAL;
3611
Chris Wilson03ac0642016-07-20 13:31:51 +01003612 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003613 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003614 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003615
Chris Wilsone95433c2016-10-28 13:58:27 +01003616 start = ktime_get();
3617
3618 ret = i915_gem_object_wait(obj,
3619 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3620 to_wait_timeout(args->timeout_ns),
3621 to_rps_client(file));
3622
3623 if (args->timeout_ns > 0) {
3624 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3625 if (args->timeout_ns < 0)
3626 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003627
3628 /*
3629 * Apparently ktime isn't accurate enough and occasionally has a
3630 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3631 * things up to make the test happy. We allow up to 1 jiffy.
3632 *
3633 * This is a regression from the timespec->ktime conversion.
3634 */
3635 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3636 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003637
3638 /* Asked to wait beyond the jiffie/scheduler precision? */
3639 if (ret == -ETIME && args->timeout_ns)
3640 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003641 }
3642
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003643 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003644 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003645}
3646
Chris Wilson73cb9702016-10-28 13:58:46 +01003647static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003648{
Chris Wilson73cb9702016-10-28 13:58:46 +01003649 int ret, i;
3650
3651 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3652 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3653 if (ret)
3654 return ret;
3655 }
3656
3657 return 0;
3658}
3659
Chris Wilson25112b62017-03-30 15:50:39 +01003660static int wait_for_engines(struct drm_i915_private *i915)
3661{
Chris Wilsonee42c002017-12-11 19:41:34 +00003662 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003663 dev_err(i915->drm.dev,
3664 "Failed to idle engines, declaring wedged!\n");
3665 if (drm_debug & DRM_UT_DRIVER) {
3666 struct drm_printer p = drm_debug_printer(__func__);
3667 struct intel_engine_cs *engine;
3668 enum intel_engine_id id;
3669
3670 for_each_engine(engine, i915, id)
3671 intel_engine_dump(engine, &p,
Chris Wilson9e519bc2018-02-05 10:06:18 +00003672 "%s\n", engine->name);
Chris Wilson59e4b192017-12-11 19:41:35 +00003673 }
3674
Chris Wilsoncad99462017-08-26 12:09:33 +01003675 i915_gem_set_wedged(i915);
3676 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003677 }
3678
3679 return 0;
3680}
3681
Chris Wilson73cb9702016-10-28 13:58:46 +01003682int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3683{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003684 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003685
Chris Wilson863e9fd2017-05-30 13:13:32 +01003686 /* If the device is asleep, we have no requests outstanding */
3687 if (!READ_ONCE(i915->gt.awake))
3688 return 0;
3689
Chris Wilson9caa34a2016-11-11 14:58:08 +00003690 if (flags & I915_WAIT_LOCKED) {
3691 struct i915_gem_timeline *tl;
3692
3693 lockdep_assert_held(&i915->drm.struct_mutex);
3694
3695 list_for_each_entry(tl, &i915->gt.timelines, link) {
3696 ret = wait_for_timeline(tl, flags);
3697 if (ret)
3698 return ret;
3699 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003700 i915_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003701
3702 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003703 } else {
3704 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003705 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003706
Chris Wilson25112b62017-03-30 15:50:39 +01003707 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003708}
3709
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003710static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3711{
Chris Wilsone27ab732017-06-15 13:38:49 +01003712 /*
3713 * We manually flush the CPU domain so that we can override and
3714 * force the flush for the display, and perform it asyncrhonously.
3715 */
3716 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3717 if (obj->cache_dirty)
3718 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003719 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003720}
3721
3722void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3723{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003724 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003725 return;
3726
3727 mutex_lock(&obj->base.dev->struct_mutex);
3728 __i915_gem_object_flush_for_display(obj);
3729 mutex_unlock(&obj->base.dev->struct_mutex);
3730}
3731
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003732/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003733 * Moves a single object to the WC read, and possibly write domain.
3734 * @obj: object to act on
3735 * @write: ask for write access or read only
3736 *
3737 * This function returns when the move is complete, including waiting on
3738 * flushes to occur.
3739 */
3740int
3741i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3742{
3743 int ret;
3744
3745 lockdep_assert_held(&obj->base.dev->struct_mutex);
3746
3747 ret = i915_gem_object_wait(obj,
3748 I915_WAIT_INTERRUPTIBLE |
3749 I915_WAIT_LOCKED |
3750 (write ? I915_WAIT_ALL : 0),
3751 MAX_SCHEDULE_TIMEOUT,
3752 NULL);
3753 if (ret)
3754 return ret;
3755
Christian Königc0a51fd2018-02-16 13:43:38 +01003756 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003757 return 0;
3758
3759 /* Flush and acquire obj->pages so that we are coherent through
3760 * direct access in memory with previous cached writes through
3761 * shmemfs and that our cache domain tracking remains valid.
3762 * For example, if the obj->filp was moved to swap without us
3763 * being notified and releasing the pages, we would mistakenly
3764 * continue to assume that the obj remained out of the CPU cached
3765 * domain.
3766 */
3767 ret = i915_gem_object_pin_pages(obj);
3768 if (ret)
3769 return ret;
3770
3771 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3772
3773 /* Serialise direct access to this object with the barriers for
3774 * coherent writes from the GPU, by effectively invalidating the
3775 * WC domain upon first access.
3776 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003777 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003778 mb();
3779
3780 /* It should now be out of any other write domains, and we can update
3781 * the domain values for our changes.
3782 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003783 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3784 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003785 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003786 obj->read_domains = I915_GEM_DOMAIN_WC;
3787 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003788 obj->mm.dirty = true;
3789 }
3790
3791 i915_gem_object_unpin_pages(obj);
3792 return 0;
3793}
3794
3795/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003796 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003797 * @obj: object to act on
3798 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003799 *
3800 * This function returns when the move is complete, including waiting on
3801 * flushes to occur.
3802 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003803int
Chris Wilson20217462010-11-23 15:26:33 +00003804i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003805{
Eric Anholte47c68e2008-11-14 13:35:19 -08003806 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003807
Chris Wilsone95433c2016-10-28 13:58:27 +01003808 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003809
Chris Wilsone95433c2016-10-28 13:58:27 +01003810 ret = i915_gem_object_wait(obj,
3811 I915_WAIT_INTERRUPTIBLE |
3812 I915_WAIT_LOCKED |
3813 (write ? I915_WAIT_ALL : 0),
3814 MAX_SCHEDULE_TIMEOUT,
3815 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003816 if (ret)
3817 return ret;
3818
Christian Königc0a51fd2018-02-16 13:43:38 +01003819 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003820 return 0;
3821
Chris Wilson43566de2015-01-02 16:29:29 +05303822 /* Flush and acquire obj->pages so that we are coherent through
3823 * direct access in memory with previous cached writes through
3824 * shmemfs and that our cache domain tracking remains valid.
3825 * For example, if the obj->filp was moved to swap without us
3826 * being notified and releasing the pages, we would mistakenly
3827 * continue to assume that the obj remained out of the CPU cached
3828 * domain.
3829 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003830 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303831 if (ret)
3832 return ret;
3833
Chris Wilsonef749212017-04-12 12:01:10 +01003834 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003835
Chris Wilsond0a57782012-10-09 19:24:37 +01003836 /* Serialise direct access to this object with the barriers for
3837 * coherent writes from the GPU, by effectively invalidating the
3838 * GTT domain upon first access.
3839 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003840 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003841 mb();
3842
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003843 /* It should now be out of any other write domains, and we can update
3844 * the domain values for our changes.
3845 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003846 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3847 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003848 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003849 obj->read_domains = I915_GEM_DOMAIN_GTT;
3850 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003851 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003852 }
3853
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003854 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003855 return 0;
3856}
3857
Chris Wilsonef55f922015-10-09 14:11:27 +01003858/**
3859 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003860 * @obj: object to act on
3861 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003862 *
3863 * After this function returns, the object will be in the new cache-level
3864 * across all GTT and the contents of the backing storage will be coherent,
3865 * with respect to the new cache-level. In order to keep the backing storage
3866 * coherent for all users, we only allow a single cache level to be set
3867 * globally on the object and prevent it from being changed whilst the
3868 * hardware is reading from the object. That is if the object is currently
3869 * on the scanout it will be set to uncached (or equivalent display
3870 * cache coherency) and all non-MOCS GPU access will also be uncached so
3871 * that all direct access to the scanout remains coherent.
3872 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003873int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3874 enum i915_cache_level cache_level)
3875{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003876 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003877 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003878
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003879 lockdep_assert_held(&obj->base.dev->struct_mutex);
3880
Chris Wilsone4ffd172011-04-04 09:44:39 +01003881 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003882 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003883
Chris Wilsonef55f922015-10-09 14:11:27 +01003884 /* Inspect the list of currently bound VMA and unbind any that would
3885 * be invalid given the new cache-level. This is principally to
3886 * catch the issue of the CS prefetch crossing page boundaries and
3887 * reading an invalid PTE on older architectures.
3888 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003889restart:
3890 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003891 if (!drm_mm_node_allocated(&vma->node))
3892 continue;
3893
Chris Wilson20dfbde2016-08-04 16:32:30 +01003894 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003895 DRM_DEBUG("can not change the cache level of pinned objects\n");
3896 return -EBUSY;
3897 }
3898
Chris Wilson010e3e62017-12-06 12:49:13 +00003899 if (!i915_vma_is_closed(vma) &&
3900 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003901 continue;
3902
3903 ret = i915_vma_unbind(vma);
3904 if (ret)
3905 return ret;
3906
3907 /* As unbinding may affect other elements in the
3908 * obj->vma_list (due to side-effects from retiring
3909 * an active vma), play safe and restart the iterator.
3910 */
3911 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003912 }
3913
Chris Wilsonef55f922015-10-09 14:11:27 +01003914 /* We can reuse the existing drm_mm nodes but need to change the
3915 * cache-level on the PTE. We could simply unbind them all and
3916 * rebind with the correct cache-level on next use. However since
3917 * we already have a valid slot, dma mapping, pages etc, we may as
3918 * rewrite the PTE in the belief that doing so tramples upon less
3919 * state and so involves less work.
3920 */
Chris Wilson15717de2016-08-04 07:52:26 +01003921 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003922 /* Before we change the PTE, the GPU must not be accessing it.
3923 * If we wait upon the object, we know that all the bound
3924 * VMA are no longer active.
3925 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003926 ret = i915_gem_object_wait(obj,
3927 I915_WAIT_INTERRUPTIBLE |
3928 I915_WAIT_LOCKED |
3929 I915_WAIT_ALL,
3930 MAX_SCHEDULE_TIMEOUT,
3931 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003932 if (ret)
3933 return ret;
3934
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003935 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3936 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003937 /* Access to snoopable pages through the GTT is
3938 * incoherent and on some machines causes a hard
3939 * lockup. Relinquish the CPU mmaping to force
3940 * userspace to refault in the pages and we can
3941 * then double check if the GTT mapping is still
3942 * valid for that pointer access.
3943 */
3944 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003945
Chris Wilsonef55f922015-10-09 14:11:27 +01003946 /* As we no longer need a fence for GTT access,
3947 * we can relinquish it now (and so prevent having
3948 * to steal a fence from someone else on the next
3949 * fence request). Note GPU activity would have
3950 * dropped the fence as all snoopable access is
3951 * supposed to be linear.
3952 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003953 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003954 ret = i915_vma_put_fence(vma);
3955 if (ret)
3956 return ret;
3957 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003958 } else {
3959 /* We either have incoherent backing store and
3960 * so no GTT access or the architecture is fully
3961 * coherent. In such cases, existing GTT mmaps
3962 * ignore the cache bit in the PTE and we can
3963 * rewrite it without confusing the GPU or having
3964 * to force userspace to fault back in its mmaps.
3965 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003966 }
3967
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003968 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003969 if (!drm_mm_node_allocated(&vma->node))
3970 continue;
3971
3972 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3973 if (ret)
3974 return ret;
3975 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003976 }
3977
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003978 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003979 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003980 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003981 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003982
Chris Wilsone4ffd172011-04-04 09:44:39 +01003983 return 0;
3984}
3985
Ben Widawsky199adf42012-09-21 17:01:20 -07003986int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3987 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003988{
Ben Widawsky199adf42012-09-21 17:01:20 -07003989 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003990 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003991 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003992
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003993 rcu_read_lock();
3994 obj = i915_gem_object_lookup_rcu(file, args->handle);
3995 if (!obj) {
3996 err = -ENOENT;
3997 goto out;
3998 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003999
Chris Wilson651d7942013-08-08 14:41:10 +01004000 switch (obj->cache_level) {
4001 case I915_CACHE_LLC:
4002 case I915_CACHE_L3_LLC:
4003 args->caching = I915_CACHING_CACHED;
4004 break;
4005
Chris Wilson4257d3b2013-08-08 14:41:11 +01004006 case I915_CACHE_WT:
4007 args->caching = I915_CACHING_DISPLAY;
4008 break;
4009
Chris Wilson651d7942013-08-08 14:41:10 +01004010 default:
4011 args->caching = I915_CACHING_NONE;
4012 break;
4013 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004014out:
4015 rcu_read_unlock();
4016 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004017}
4018
Ben Widawsky199adf42012-09-21 17:01:20 -07004019int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4020 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004021{
Chris Wilson9c870d02016-10-24 13:42:15 +01004022 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004023 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004024 struct drm_i915_gem_object *obj;
4025 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004026 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004027
Ben Widawsky199adf42012-09-21 17:01:20 -07004028 switch (args->caching) {
4029 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004030 level = I915_CACHE_NONE;
4031 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004032 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004033 /*
4034 * Due to a HW issue on BXT A stepping, GPU stores via a
4035 * snooped mapping may leave stale data in a corresponding CPU
4036 * cacheline, whereas normally such cachelines would get
4037 * invalidated.
4038 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004039 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004040 return -ENODEV;
4041
Chris Wilsone6994ae2012-07-10 10:27:08 +01004042 level = I915_CACHE_LLC;
4043 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004044 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004045 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004046 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004047 default:
4048 return -EINVAL;
4049 }
4050
Chris Wilsond65415d2017-01-19 08:22:10 +00004051 obj = i915_gem_object_lookup(file, args->handle);
4052 if (!obj)
4053 return -ENOENT;
4054
Tina Zhanga03f3952017-11-14 10:25:13 +00004055 /*
4056 * The caching mode of proxy object is handled by its generator, and
4057 * not allowed to be changed by userspace.
4058 */
4059 if (i915_gem_object_is_proxy(obj)) {
4060 ret = -ENXIO;
4061 goto out;
4062 }
4063
Chris Wilsond65415d2017-01-19 08:22:10 +00004064 if (obj->cache_level == level)
4065 goto out;
4066
4067 ret = i915_gem_object_wait(obj,
4068 I915_WAIT_INTERRUPTIBLE,
4069 MAX_SCHEDULE_TIMEOUT,
4070 to_rps_client(file));
4071 if (ret)
4072 goto out;
4073
Ben Widawsky3bc29132012-09-26 16:15:20 -07004074 ret = i915_mutex_lock_interruptible(dev);
4075 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004076 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004077
4078 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004079 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004080
4081out:
4082 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004083 return ret;
4084}
4085
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004086/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004087 * Prepare buffer for display plane (scanout, cursors, etc).
4088 * Can be called from an uninterruptible phase (modesetting) and allows
4089 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004090 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004091struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004092i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4093 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004094 const struct i915_ggtt_view *view,
4095 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004096{
Chris Wilson058d88c2016-08-15 10:49:06 +01004097 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004098 int ret;
4099
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004100 lockdep_assert_held(&obj->base.dev->struct_mutex);
4101
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004102 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004103 * display coherency whilst setting up the cache domains.
4104 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004105 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004106
Eric Anholta7ef0642011-03-29 16:59:54 -07004107 /* The display engine is not coherent with the LLC cache on gen6. As
4108 * a result, we make sure that the pinning that is about to occur is
4109 * done with uncached PTEs. This is lowest common denominator for all
4110 * chipsets.
4111 *
4112 * However for gen6+, we could do better by using the GFDT bit instead
4113 * of uncaching, which would allow us to flush all the LLC-cached data
4114 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4115 */
Chris Wilson651d7942013-08-08 14:41:10 +01004116 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004117 HAS_WT(to_i915(obj->base.dev)) ?
4118 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004119 if (ret) {
4120 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004121 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004122 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004123
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004124 /* As the user may map the buffer once pinned in the display plane
4125 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004126 * always use map_and_fenceable for all scanout buffers. However,
4127 * it may simply be too big to fit into mappable, in which case
4128 * put it anyway and hope that userspace can cope (but always first
4129 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004130 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004131 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004132 if ((flags & PIN_MAPPABLE) == 0 &&
4133 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004134 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004135 flags |
4136 PIN_MAPPABLE |
4137 PIN_NONBLOCK);
4138 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004139 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004140 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004141 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004142
Chris Wilsond8923dc2016-08-18 17:17:07 +01004143 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4144
Chris Wilsona6a7cc42016-11-18 21:17:46 +00004145 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004146 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00004147 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004148
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004149 /* It should now be out of any other write domains, and we can update
4150 * the domain values for our changes.
4151 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004152 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004153
Chris Wilson058d88c2016-08-15 10:49:06 +01004154 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004155
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004156err_unpin_global:
4157 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004158 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004159}
4160
4161void
Chris Wilson058d88c2016-08-15 10:49:06 +01004162i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004163{
Chris Wilson49d73912016-11-29 09:50:08 +00004164 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004165
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004166 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004167 return;
4168
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004169 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004170 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004171
Chris Wilson383d5822016-08-18 17:17:08 +01004172 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004173 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004174
Chris Wilson058d88c2016-08-15 10:49:06 +01004175 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004176}
4177
Eric Anholte47c68e2008-11-14 13:35:19 -08004178/**
4179 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004180 * @obj: object to act on
4181 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004182 *
4183 * This function returns when the move is complete, including waiting on
4184 * flushes to occur.
4185 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004186int
Chris Wilson919926a2010-11-12 13:42:53 +00004187i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004188{
Eric Anholte47c68e2008-11-14 13:35:19 -08004189 int ret;
4190
Chris Wilsone95433c2016-10-28 13:58:27 +01004191 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004192
Chris Wilsone95433c2016-10-28 13:58:27 +01004193 ret = i915_gem_object_wait(obj,
4194 I915_WAIT_INTERRUPTIBLE |
4195 I915_WAIT_LOCKED |
4196 (write ? I915_WAIT_ALL : 0),
4197 MAX_SCHEDULE_TIMEOUT,
4198 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004199 if (ret)
4200 return ret;
4201
Chris Wilsonef749212017-04-12 12:01:10 +01004202 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004203
Eric Anholte47c68e2008-11-14 13:35:19 -08004204 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004205 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004206 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004207 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004208 }
4209
4210 /* It should now be out of any other write domains, and we can update
4211 * the domain values for our changes.
4212 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004213 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004214
4215 /* If we're writing through the CPU, then the GPU read domains will
4216 * need to be invalidated at next use.
4217 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004218 if (write)
4219 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004220
4221 return 0;
4222}
4223
Eric Anholt673a3942008-07-30 12:06:12 -07004224/* Throttle our rendering by waiting until the ring has completed our requests
4225 * emitted over 20 msec ago.
4226 *
Eric Anholtb9624422009-06-03 07:27:35 +00004227 * Note that if we were to use the current jiffies each time around the loop,
4228 * we wouldn't escape the function with any frames outstanding if the time to
4229 * render a frame was over 20ms.
4230 *
Eric Anholt673a3942008-07-30 12:06:12 -07004231 * This should get us reasonable parallelism between CPU and GPU but also
4232 * relatively low latency when blocking on a particular request to finish.
4233 */
4234static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004235i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004236{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004237 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004238 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004239 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004240 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004241 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004242
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004243 /* ABI: return -EIO if already wedged */
4244 if (i915_terminally_wedged(&dev_priv->gpu_error))
4245 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004246
Chris Wilson1c255952010-09-26 11:03:27 +01004247 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004248 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004249 if (time_after_eq(request->emitted_jiffies, recent_enough))
4250 break;
4251
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004252 if (target) {
4253 list_del(&target->client_link);
4254 target->file_priv = NULL;
4255 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004256
John Harrison54fb2412014-11-24 18:49:27 +00004257 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004258 }
John Harrisonff865882014-11-24 18:49:28 +00004259 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004260 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004261 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004262
John Harrison54fb2412014-11-24 18:49:27 +00004263 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004264 return 0;
4265
Chris Wilsone61e0f52018-02-21 09:56:36 +00004266 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004267 I915_WAIT_INTERRUPTIBLE,
4268 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004269 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004270
Chris Wilsone95433c2016-10-28 13:58:27 +01004271 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004272}
4273
Chris Wilson058d88c2016-08-15 10:49:06 +01004274struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004275i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4276 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004277 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004278 u64 alignment,
4279 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004280{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004281 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4282 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004283 struct i915_vma *vma;
4284 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004285
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004286 lockdep_assert_held(&obj->base.dev->struct_mutex);
4287
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004288 if (flags & PIN_MAPPABLE &&
4289 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004290 /* If the required space is larger than the available
4291 * aperture, we will not able to find a slot for the
4292 * object and unbinding the object now will be in
4293 * vain. Worse, doing so may cause us to ping-pong
4294 * the object in and out of the Global GTT and
4295 * waste a lot of cycles under the mutex.
4296 */
4297 if (obj->base.size > dev_priv->ggtt.mappable_end)
4298 return ERR_PTR(-E2BIG);
4299
4300 /* If NONBLOCK is set the caller is optimistically
4301 * trying to cache the full object within the mappable
4302 * aperture, and *must* have a fallback in place for
4303 * situations where we cannot bind the object. We
4304 * can be a little more lax here and use the fallback
4305 * more often to avoid costly migrations of ourselves
4306 * and other objects within the aperture.
4307 *
4308 * Half-the-aperture is used as a simple heuristic.
4309 * More interesting would to do search for a free
4310 * block prior to making the commitment to unbind.
4311 * That caters for the self-harm case, and with a
4312 * little more heuristics (e.g. NOFAULT, NOEVICT)
4313 * we could try to minimise harm to others.
4314 */
4315 if (flags & PIN_NONBLOCK &&
4316 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4317 return ERR_PTR(-ENOSPC);
4318 }
4319
Chris Wilson718659a2017-01-16 15:21:28 +00004320 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004321 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004322 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004323
4324 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004325 if (flags & PIN_NONBLOCK) {
4326 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4327 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004328
Chris Wilson43ae70d2017-10-09 09:44:01 +01004329 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004330 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004331 return ERR_PTR(-ENOSPC);
4332 }
4333
Chris Wilson59bfa122016-08-04 16:32:31 +01004334 WARN(i915_vma_is_pinned(vma),
4335 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004336 " offset=%08x, req.alignment=%llx,"
4337 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4338 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004339 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004340 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004341 ret = i915_vma_unbind(vma);
4342 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004343 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004344 }
4345
Chris Wilson058d88c2016-08-15 10:49:06 +01004346 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4347 if (ret)
4348 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004349
Chris Wilson058d88c2016-08-15 10:49:06 +01004350 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004351}
4352
Chris Wilsonedf6b762016-08-09 09:23:33 +01004353static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004354{
4355 /* Note that we could alias engines in the execbuf API, but
4356 * that would be very unwise as it prevents userspace from
4357 * fine control over engine selection. Ahem.
4358 *
4359 * This should be something like EXEC_MAX_ENGINE instead of
4360 * I915_NUM_ENGINES.
4361 */
4362 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4363 return 0x10000 << id;
4364}
4365
4366static __always_inline unsigned int __busy_write_id(unsigned int id)
4367{
Chris Wilson70cb4722016-08-09 18:08:25 +01004368 /* The uABI guarantees an active writer is also amongst the read
4369 * engines. This would be true if we accessed the activity tracking
4370 * under the lock, but as we perform the lookup of the object and
4371 * its activity locklessly we can not guarantee that the last_write
4372 * being active implies that we have set the same engine flag from
4373 * last_read - hence we always set both read and write busy for
4374 * last_write.
4375 */
4376 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004377}
4378
Chris Wilsonedf6b762016-08-09 09:23:33 +01004379static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004380__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004381 unsigned int (*flag)(unsigned int id))
4382{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004383 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004384
Chris Wilsond07f0e52016-10-28 13:58:44 +01004385 /* We have to check the current hw status of the fence as the uABI
4386 * guarantees forward progress. We could rely on the idle worker
4387 * to eventually flush us, but to minimise latency just ask the
4388 * hardware.
4389 *
4390 * Note we only report on the status of native fences.
4391 */
4392 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004393 return 0;
4394
Chris Wilsond07f0e52016-10-28 13:58:44 +01004395 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004396 rq = container_of(fence, struct i915_request, fence);
4397 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004398 return 0;
4399
Chris Wilson1d39f282017-04-11 13:43:06 +01004400 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004401}
4402
Chris Wilsonedf6b762016-08-09 09:23:33 +01004403static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004404busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004405{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004406 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004407}
4408
Chris Wilsonedf6b762016-08-09 09:23:33 +01004409static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004410busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004411{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004412 if (!fence)
4413 return 0;
4414
4415 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004416}
4417
Eric Anholt673a3942008-07-30 12:06:12 -07004418int
Eric Anholt673a3942008-07-30 12:06:12 -07004419i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004420 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004421{
4422 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004423 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004424 struct reservation_object_list *list;
4425 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004426 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004427
Chris Wilsond07f0e52016-10-28 13:58:44 +01004428 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004429 rcu_read_lock();
4430 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004431 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004432 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004433
4434 /* A discrepancy here is that we do not report the status of
4435 * non-i915 fences, i.e. even though we may report the object as idle,
4436 * a call to set-domain may still stall waiting for foreign rendering.
4437 * This also means that wait-ioctl may report an object as busy,
4438 * where busy-ioctl considers it idle.
4439 *
4440 * We trade the ability to warn of foreign fences to report on which
4441 * i915 engines are active for the object.
4442 *
4443 * Alternatively, we can trade that extra information on read/write
4444 * activity with
4445 * args->busy =
4446 * !reservation_object_test_signaled_rcu(obj->resv, true);
4447 * to report the overall busyness. This is what the wait-ioctl does.
4448 *
4449 */
4450retry:
4451 seq = raw_read_seqcount(&obj->resv->seq);
4452
4453 /* Translate the exclusive fence to the READ *and* WRITE engine */
4454 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4455
4456 /* Translate shared fences to READ set of engines */
4457 list = rcu_dereference(obj->resv->fence);
4458 if (list) {
4459 unsigned int shared_count = list->shared_count, i;
4460
4461 for (i = 0; i < shared_count; ++i) {
4462 struct dma_fence *fence =
4463 rcu_dereference(list->shared[i]);
4464
4465 args->busy |= busy_check_reader(fence);
4466 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004467 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004468
Chris Wilsond07f0e52016-10-28 13:58:44 +01004469 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4470 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004471
Chris Wilsond07f0e52016-10-28 13:58:44 +01004472 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004473out:
4474 rcu_read_unlock();
4475 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004476}
4477
4478int
4479i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4480 struct drm_file *file_priv)
4481{
Akshay Joshi0206e352011-08-16 15:34:10 -04004482 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004483}
4484
Chris Wilson3ef94da2009-09-14 16:50:29 +01004485int
4486i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4487 struct drm_file *file_priv)
4488{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004489 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004490 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004491 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004492 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004493
4494 switch (args->madv) {
4495 case I915_MADV_DONTNEED:
4496 case I915_MADV_WILLNEED:
4497 break;
4498 default:
4499 return -EINVAL;
4500 }
4501
Chris Wilson03ac0642016-07-20 13:31:51 +01004502 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004503 if (!obj)
4504 return -ENOENT;
4505
4506 err = mutex_lock_interruptible(&obj->mm.lock);
4507 if (err)
4508 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004509
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004510 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004511 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004512 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004513 if (obj->mm.madv == I915_MADV_WILLNEED) {
4514 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004515 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004516 obj->mm.quirked = false;
4517 }
4518 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004519 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004520 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004521 obj->mm.quirked = true;
4522 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004523 }
4524
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004525 if (obj->mm.madv != __I915_MADV_PURGED)
4526 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004527
Chris Wilson6c085a72012-08-20 11:40:46 +02004528 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004529 if (obj->mm.madv == I915_MADV_DONTNEED &&
4530 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004531 i915_gem_object_truncate(obj);
4532
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004533 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004534 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004535
Chris Wilson1233e2d2016-10-28 13:58:37 +01004536out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004537 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004538 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004539}
4540
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004541static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004542frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004543{
4544 struct drm_i915_gem_object *obj =
4545 container_of(active, typeof(*obj), frontbuffer_write);
4546
Chris Wilsond59b21e2017-02-22 11:40:49 +00004547 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004548}
4549
Chris Wilson37e680a2012-06-07 15:38:42 +01004550void i915_gem_object_init(struct drm_i915_gem_object *obj,
4551 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004552{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004553 mutex_init(&obj->mm.lock);
4554
Ben Widawsky2f633152013-07-17 12:19:03 -07004555 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004556 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004557 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004558
Chris Wilson37e680a2012-06-07 15:38:42 +01004559 obj->ops = ops;
4560
Chris Wilsond07f0e52016-10-28 13:58:44 +01004561 reservation_object_init(&obj->__builtin_resv);
4562 obj->resv = &obj->__builtin_resv;
4563
Chris Wilson50349242016-08-18 17:17:04 +01004564 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004565 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004566
4567 obj->mm.madv = I915_MADV_WILLNEED;
4568 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4569 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004570
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004571 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004572}
4573
Chris Wilson37e680a2012-06-07 15:38:42 +01004574static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004575 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4576 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004577
Chris Wilson37e680a2012-06-07 15:38:42 +01004578 .get_pages = i915_gem_object_get_pages_gtt,
4579 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004580
4581 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004582};
4583
Matthew Auld465c4032017-10-06 23:18:14 +01004584static int i915_gem_object_create_shmem(struct drm_device *dev,
4585 struct drm_gem_object *obj,
4586 size_t size)
4587{
4588 struct drm_i915_private *i915 = to_i915(dev);
4589 unsigned long flags = VM_NORESERVE;
4590 struct file *filp;
4591
4592 drm_gem_private_object_init(dev, obj, size);
4593
4594 if (i915->mm.gemfs)
4595 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4596 flags);
4597 else
4598 filp = shmem_file_setup("i915", size, flags);
4599
4600 if (IS_ERR(filp))
4601 return PTR_ERR(filp);
4602
4603 obj->filp = filp;
4604
4605 return 0;
4606}
4607
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004608struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004609i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004610{
Daniel Vetterc397b902010-04-09 19:05:07 +00004611 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004612 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004613 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004614 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004615 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004616
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004617 /* There is a prevalence of the assumption that we fit the object's
4618 * page count inside a 32bit _signed_ variable. Let's document this and
4619 * catch if we ever need to fix it. In the meantime, if you do spot
4620 * such a local variable, please consider fixing!
4621 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004622 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004623 return ERR_PTR(-E2BIG);
4624
4625 if (overflows_type(size, obj->base.size))
4626 return ERR_PTR(-E2BIG);
4627
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004628 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004629 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004630 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004631
Matthew Auld465c4032017-10-06 23:18:14 +01004632 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004633 if (ret)
4634 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004635
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004636 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004637 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004638 /* 965gm cannot relocate objects above 4GiB. */
4639 mask &= ~__GFP_HIGHMEM;
4640 mask |= __GFP_DMA32;
4641 }
4642
Al Viro93c76a32015-12-04 23:45:44 -05004643 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004644 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004645 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004646
Chris Wilson37e680a2012-06-07 15:38:42 +01004647 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004648
Christian Königc0a51fd2018-02-16 13:43:38 +01004649 obj->write_domain = I915_GEM_DOMAIN_CPU;
4650 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004651
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004652 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004653 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004654 * cache) for about a 10% performance improvement
4655 * compared to uncached. Graphics requests other than
4656 * display scanout are coherent with the CPU in
4657 * accessing this cache. This means in this mode we
4658 * don't need to clflush on the CPU side, and on the
4659 * GPU side we only need to flush internal caches to
4660 * get data visible to the CPU.
4661 *
4662 * However, we maintain the display planes as UC, and so
4663 * need to rebind when first used as such.
4664 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004665 cache_level = I915_CACHE_LLC;
4666 else
4667 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004668
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004669 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004670
Daniel Vetterd861e332013-07-24 23:25:03 +02004671 trace_i915_gem_object_create(obj);
4672
Chris Wilson05394f32010-11-08 19:18:58 +00004673 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004674
4675fail:
4676 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004677 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004678}
4679
Chris Wilson340fbd82014-05-22 09:16:52 +01004680static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4681{
4682 /* If we are the last user of the backing storage (be it shmemfs
4683 * pages or stolen etc), we know that the pages are going to be
4684 * immediately released. In this case, we can then skip copying
4685 * back the contents from the GPU.
4686 */
4687
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004688 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004689 return false;
4690
4691 if (obj->base.filp == NULL)
4692 return true;
4693
4694 /* At first glance, this looks racy, but then again so would be
4695 * userspace racing mmap against close. However, the first external
4696 * reference to the filp can only be obtained through the
4697 * i915_gem_mmap_ioctl() which safeguards us against the user
4698 * acquiring such a reference whilst we are in the middle of
4699 * freeing the object.
4700 */
4701 return atomic_long_read(&obj->base.filp->f_count) == 1;
4702}
4703
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004704static void __i915_gem_free_objects(struct drm_i915_private *i915,
4705 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004706{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004707 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004708
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004709 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004710 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004711 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004712
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004713 trace_i915_gem_object_destroy(obj);
4714
Chris Wilsoncc731f52017-10-13 21:26:21 +01004715 mutex_lock(&i915->drm.struct_mutex);
4716
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004717 GEM_BUG_ON(i915_gem_object_is_active(obj));
4718 list_for_each_entry_safe(vma, vn,
4719 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004720 GEM_BUG_ON(i915_vma_is_active(vma));
4721 vma->flags &= ~I915_VMA_PIN_MASK;
4722 i915_vma_close(vma);
4723 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004724 GEM_BUG_ON(!list_empty(&obj->vma_list));
4725 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004726
Chris Wilsonf2123812017-10-16 12:40:37 +01004727 /* This serializes freeing with the shrinker. Since the free
4728 * is delayed, first by RCU then by the workqueue, we want the
4729 * shrinker to be able to free pages of unreferenced objects,
4730 * or else we may oom whilst there are plenty of deferred
4731 * freed objects.
4732 */
4733 if (i915_gem_object_has_pages(obj)) {
4734 spin_lock(&i915->mm.obj_lock);
4735 list_del_init(&obj->mm.link);
4736 spin_unlock(&i915->mm.obj_lock);
4737 }
4738
Chris Wilsoncc731f52017-10-13 21:26:21 +01004739 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004740
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004741 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004742 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004743 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004744 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004745
4746 if (obj->ops->release)
4747 obj->ops->release(obj);
4748
4749 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4750 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004751 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004752 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004753
4754 if (obj->base.import_attach)
4755 drm_prime_gem_destroy(&obj->base, NULL);
4756
Chris Wilsond07f0e52016-10-28 13:58:44 +01004757 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004758 drm_gem_object_release(&obj->base);
4759 i915_gem_info_remove_obj(i915, obj->base.size);
4760
4761 kfree(obj->bit_17);
4762 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004763
Chris Wilsonc9c70472018-02-19 22:06:31 +00004764 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4765 atomic_dec(&i915->mm.free_count);
4766
Chris Wilsoncc731f52017-10-13 21:26:21 +01004767 if (on)
4768 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004769 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004770 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004771}
4772
4773static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4774{
4775 struct llist_node *freed;
4776
Chris Wilson87701b42017-10-13 21:26:20 +01004777 /* Free the oldest, most stale object to keep the free_list short */
4778 freed = NULL;
4779 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4780 /* Only one consumer of llist_del_first() allowed */
4781 spin_lock(&i915->mm.free_lock);
4782 freed = llist_del_first(&i915->mm.free_list);
4783 spin_unlock(&i915->mm.free_lock);
4784 }
4785 if (unlikely(freed)) {
4786 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004787 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004788 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004789}
4790
4791static void __i915_gem_free_work(struct work_struct *work)
4792{
4793 struct drm_i915_private *i915 =
4794 container_of(work, struct drm_i915_private, mm.free_work);
4795 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004796
Chris Wilson2ef1e722018-01-15 20:57:59 +00004797 /*
4798 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004799 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4800 * However, the object may also be bound into the global GTT (e.g.
4801 * older GPUs without per-process support, or for direct access through
4802 * the GTT either for the user or for scanout). Those VMA still need to
4803 * unbound now.
4804 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004805
Chris Wilsonf991c492017-11-06 11:15:08 +00004806 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004807 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004808 spin_unlock(&i915->mm.free_lock);
4809
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004810 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004811 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004812 return;
4813
4814 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004815 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004816 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004817}
4818
4819static void __i915_gem_free_object_rcu(struct rcu_head *head)
4820{
4821 struct drm_i915_gem_object *obj =
4822 container_of(head, typeof(*obj), rcu);
4823 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4824
Chris Wilson2ef1e722018-01-15 20:57:59 +00004825 /*
4826 * Since we require blocking on struct_mutex to unbind the freed
4827 * object from the GPU before releasing resources back to the
4828 * system, we can not do that directly from the RCU callback (which may
4829 * be a softirq context), but must instead then defer that work onto a
4830 * kthread. We use the RCU callback rather than move the freed object
4831 * directly onto the work queue so that we can mix between using the
4832 * worker and performing frees directly from subsequent allocations for
4833 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004834 */
4835 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004836 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004837}
4838
4839void i915_gem_free_object(struct drm_gem_object *gem_obj)
4840{
4841 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4842
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004843 if (obj->mm.quirked)
4844 __i915_gem_object_unpin_pages(obj);
4845
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004846 if (discard_backing_storage(obj))
4847 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004848
Chris Wilson2ef1e722018-01-15 20:57:59 +00004849 /*
4850 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004851 * read-side critical sections are complete, e.g.
4852 * i915_gem_busy_ioctl(). For the corresponding synchronized
4853 * lookup see i915_gem_object_lookup_rcu().
4854 */
Chris Wilsonc9c70472018-02-19 22:06:31 +00004855 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004856 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004857}
4858
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004859void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4860{
4861 lockdep_assert_held(&obj->base.dev->struct_mutex);
4862
Chris Wilsond1b48c12017-08-16 09:52:08 +01004863 if (!i915_gem_object_has_active_reference(obj) &&
4864 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004865 i915_gem_object_set_active_reference(obj);
4866 else
4867 i915_gem_object_put(obj);
4868}
4869
Chris Wilsonae6c4572017-11-10 14:26:28 +00004870static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004871{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004872 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004873 struct intel_engine_cs *engine;
4874 enum intel_engine_id id;
4875
Chris Wilsonae6c4572017-11-10 14:26:28 +00004876 for_each_engine(engine, i915, id) {
4877 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4878 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4879 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004880}
4881
Chris Wilson24145512017-01-24 11:01:35 +00004882void i915_gem_sanitize(struct drm_i915_private *i915)
4883{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004884 if (i915_terminally_wedged(&i915->gpu_error)) {
4885 mutex_lock(&i915->drm.struct_mutex);
4886 i915_gem_unset_wedged(i915);
4887 mutex_unlock(&i915->drm.struct_mutex);
4888 }
4889
Chris Wilson24145512017-01-24 11:01:35 +00004890 /*
4891 * If we inherit context state from the BIOS or earlier occupants
4892 * of the GPU, the GPU may be in an inconsistent state when we
4893 * try to take over. The only way to remove the earlier state
4894 * is by resetting. However, resetting on earlier gen is tricky as
4895 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004896 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004897 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004898 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4899 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004900}
4901
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004902int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004903{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004904 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004905 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004906
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004907 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004908 intel_suspend_gt_powersave(dev_priv);
4909
Chris Wilson45c5f202013-10-16 11:50:01 +01004910 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004911
4912 /* We have to flush all the executing contexts to main memory so
4913 * that they can saved in the hibernation image. To ensure the last
4914 * context image is coherent, we have to switch away from it. That
4915 * leaves the dev_priv->kernel_context still active when
4916 * we actually suspend, and its image in memory may not match the GPU
4917 * state. Fortunately, the kernel_context is disposable and we do
4918 * not rely on its state.
4919 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004920 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4921 ret = i915_gem_switch_to_kernel_context(dev_priv);
4922 if (ret)
4923 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004924
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004925 ret = i915_gem_wait_for_idle(dev_priv,
4926 I915_WAIT_INTERRUPTIBLE |
4927 I915_WAIT_LOCKED);
4928 if (ret && ret != -EIO)
4929 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004930
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004931 assert_kernel_context_is_current(dev_priv);
4932 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004933 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004934 mutex_unlock(&dev->struct_mutex);
4935
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004936 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304937
Chris Wilson737b1502015-01-26 18:03:03 +02004938 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004939 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004940
4941 /* As the idle_work is rearming if it detects a race, play safe and
4942 * repeat the flush until it is definitely idle.
4943 */
Chris Wilson7c262402017-10-06 11:40:38 +01004944 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004945
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004946 /* Assert that we sucessfully flushed all the work and
4947 * reset the GPU back to its idle, low power state.
4948 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004949 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004950 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4951 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004952
Imre Deak1c777c52016-10-12 17:46:37 +03004953 /*
4954 * Neither the BIOS, ourselves or any other kernel
4955 * expects the system to be in execlists mode on startup,
4956 * so we need to reset the GPU back to legacy mode. And the only
4957 * known way to disable logical contexts is through a GPU reset.
4958 *
4959 * So in order to leave the system in a known default configuration,
4960 * always reset the GPU upon unload and suspend. Afterwards we then
4961 * clean up the GEM state tracking, flushing off the requests and
4962 * leaving the system in a known idle state.
4963 *
4964 * Note that is of the upmost importance that the GPU is idle and
4965 * all stray writes are flushed *before* we dismantle the backing
4966 * storage for the pinned objects.
4967 *
4968 * However, since we are uncertain that resetting the GPU on older
4969 * machines is a good idea, we don't - just in case it leaves the
4970 * machine in an unusable condition.
4971 */
Chris Wilson24145512017-01-24 11:01:35 +00004972 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004973
4974 intel_runtime_pm_put(dev_priv);
4975 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004976
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004977err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004978 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004979 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004980 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004981}
4982
Chris Wilson37cd3302017-11-12 11:27:38 +00004983void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004984{
Chris Wilson37cd3302017-11-12 11:27:38 +00004985 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004986
Chris Wilson37cd3302017-11-12 11:27:38 +00004987 mutex_lock(&i915->drm.struct_mutex);
4988 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004989
Chris Wilson37cd3302017-11-12 11:27:38 +00004990 i915_gem_restore_gtt_mappings(i915);
4991 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004992
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004993 /*
4994 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004995 * guarantee that the context image is complete. So let's just reset
4996 * it and start again.
4997 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004998 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004999
Chris Wilson37cd3302017-11-12 11:27:38 +00005000 if (i915_gem_init_hw(i915))
5001 goto err_wedged;
5002
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005003 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005004
Chris Wilson37cd3302017-11-12 11:27:38 +00005005 /* Always reload a context for powersaving. */
5006 if (i915_gem_switch_to_kernel_context(i915))
5007 goto err_wedged;
5008
5009out_unlock:
5010 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5011 mutex_unlock(&i915->drm.struct_mutex);
5012 return;
5013
5014err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005015 if (!i915_terminally_wedged(&i915->gpu_error)) {
5016 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5017 i915_gem_set_wedged(i915);
5018 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005019 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005020}
5021
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005022void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005023{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005024 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005025 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5026 return;
5027
5028 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5029 DISP_TILE_SURFACE_SWIZZLING);
5030
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005031 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005032 return;
5033
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005034 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005035 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005036 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005037 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005038 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005039 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005040 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005041 else
5042 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005043}
Daniel Vettere21af882012-02-09 20:53:27 +01005044
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005045static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005046{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005047 I915_WRITE(RING_CTL(base), 0);
5048 I915_WRITE(RING_HEAD(base), 0);
5049 I915_WRITE(RING_TAIL(base), 0);
5050 I915_WRITE(RING_START(base), 0);
5051}
5052
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005053static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005054{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005055 if (IS_I830(dev_priv)) {
5056 init_unused_ring(dev_priv, PRB1_BASE);
5057 init_unused_ring(dev_priv, SRB0_BASE);
5058 init_unused_ring(dev_priv, SRB1_BASE);
5059 init_unused_ring(dev_priv, SRB2_BASE);
5060 init_unused_ring(dev_priv, SRB3_BASE);
5061 } else if (IS_GEN2(dev_priv)) {
5062 init_unused_ring(dev_priv, SRB0_BASE);
5063 init_unused_ring(dev_priv, SRB1_BASE);
5064 } else if (IS_GEN3(dev_priv)) {
5065 init_unused_ring(dev_priv, PRB1_BASE);
5066 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005067 }
5068}
5069
Chris Wilson20a8a742017-02-08 14:30:31 +00005070static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005071{
Chris Wilson20a8a742017-02-08 14:30:31 +00005072 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005073 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305074 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005075 int err;
5076
5077 for_each_engine(engine, i915, id) {
5078 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005079 if (err) {
5080 DRM_ERROR("Failed to restart %s (%d)\n",
5081 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005082 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005083 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005084 }
5085
5086 return 0;
5087}
5088
5089int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5090{
Chris Wilsond200cda2016-04-28 09:56:44 +01005091 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005092
Chris Wilsonde867c22016-10-25 13:16:02 +01005093 dev_priv->gt.last_init_time = ktime_get();
5094
Chris Wilson5e4f5182015-02-13 14:35:59 +00005095 /* Double layer security blanket, see i915_gem_init() */
5096 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5097
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005098 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005099 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005100
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005101 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005102 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005103 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005104
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005105 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005106 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005107 u32 temp = I915_READ(GEN7_MSG_CTL);
5108 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5109 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005110 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005111 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5112 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5113 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5114 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005115 }
5116
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005117 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005118
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005119 /*
5120 * At least 830 can leave some of the unused rings
5121 * "active" (ie. head != tail) after resume which
5122 * will prevent c3 entry. Makes sure all unused rings
5123 * are totally idle.
5124 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005125 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005126
Dave Gordoned54c1a2016-01-19 19:02:54 +00005127 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005128 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5129 ret = -EIO;
5130 goto out;
5131 }
John Harrison90638cc2015-05-29 17:43:37 +01005132
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005133 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005134 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005135 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005136 goto out;
5137 }
5138
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005139 /* We can't enable contexts until all firmware is loaded */
5140 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005141 if (ret) {
5142 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005143 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005144 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005145
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005146 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005147
Chris Wilson136109c2017-11-02 13:14:30 +00005148 /* Only when the HW is re-initialised, can we replay the requests */
5149 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005150out:
5151 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005152 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005153}
5154
Chris Wilsond2b4b972017-11-10 14:26:33 +00005155static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5156{
5157 struct i915_gem_context *ctx;
5158 struct intel_engine_cs *engine;
5159 enum intel_engine_id id;
5160 int err;
5161
5162 /*
5163 * As we reset the gpu during very early sanitisation, the current
5164 * register state on the GPU should reflect its defaults values.
5165 * We load a context onto the hw (with restore-inhibit), then switch
5166 * over to a second context to save that default register state. We
5167 * can then prime every new context with that state so they all start
5168 * from the same default HW values.
5169 */
5170
5171 ctx = i915_gem_context_create_kernel(i915, 0);
5172 if (IS_ERR(ctx))
5173 return PTR_ERR(ctx);
5174
5175 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005176 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005177
Chris Wilsone61e0f52018-02-21 09:56:36 +00005178 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005179 if (IS_ERR(rq)) {
5180 err = PTR_ERR(rq);
5181 goto out_ctx;
5182 }
5183
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005184 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005185 if (engine->init_context)
5186 err = engine->init_context(rq);
5187
Chris Wilsone61e0f52018-02-21 09:56:36 +00005188 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005189 if (err)
5190 goto err_active;
5191 }
5192
5193 err = i915_gem_switch_to_kernel_context(i915);
5194 if (err)
5195 goto err_active;
5196
5197 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5198 if (err)
5199 goto err_active;
5200
5201 assert_kernel_context_is_current(i915);
5202
5203 for_each_engine(engine, i915, id) {
5204 struct i915_vma *state;
5205
5206 state = ctx->engine[id].state;
5207 if (!state)
5208 continue;
5209
5210 /*
5211 * As we will hold a reference to the logical state, it will
5212 * not be torn down with the context, and importantly the
5213 * object will hold onto its vma (making it possible for a
5214 * stray GTT write to corrupt our defaults). Unmap the vma
5215 * from the GTT to prevent such accidents and reclaim the
5216 * space.
5217 */
5218 err = i915_vma_unbind(state);
5219 if (err)
5220 goto err_active;
5221
5222 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5223 if (err)
5224 goto err_active;
5225
5226 engine->default_state = i915_gem_object_get(state->obj);
5227 }
5228
5229 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5230 unsigned int found = intel_engines_has_context_isolation(i915);
5231
5232 /*
5233 * Make sure that classes with multiple engine instances all
5234 * share the same basic configuration.
5235 */
5236 for_each_engine(engine, i915, id) {
5237 unsigned int bit = BIT(engine->uabi_class);
5238 unsigned int expected = engine->default_state ? bit : 0;
5239
5240 if ((found & bit) != expected) {
5241 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5242 engine->uabi_class, engine->name);
5243 }
5244 }
5245 }
5246
5247out_ctx:
5248 i915_gem_context_set_closed(ctx);
5249 i915_gem_context_put(ctx);
5250 return err;
5251
5252err_active:
5253 /*
5254 * If we have to abandon now, we expect the engines to be idle
5255 * and ready to be torn-down. First try to flush any remaining
5256 * request, ensure we are pointing at the kernel context and
5257 * then remove it.
5258 */
5259 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5260 goto out_ctx;
5261
5262 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5263 goto out_ctx;
5264
5265 i915_gem_contexts_lost(i915);
5266 goto out_ctx;
5267}
5268
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005269int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005270{
Chris Wilson1070a422012-04-24 15:47:41 +01005271 int ret;
5272
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005273 /*
5274 * We need to fallback to 4K pages since gvt gtt handling doesn't
5275 * support huge page entries - we will need to check either hypervisor
5276 * mm can support huge guest page or just do emulation in gvt.
5277 */
5278 if (intel_vgpu_active(dev_priv))
5279 mkwrite_device_info(dev_priv)->page_sizes =
5280 I915_GTT_PAGE_SIZE_4K;
5281
Chris Wilson94312822017-05-03 10:39:18 +01005282 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005283
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005284 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005285 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005286 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005287 } else {
5288 dev_priv->gt.resume = intel_legacy_submission_resume;
5289 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005290 }
5291
Chris Wilsonee487002017-11-22 17:26:21 +00005292 ret = i915_gem_init_userptr(dev_priv);
5293 if (ret)
5294 return ret;
5295
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305296 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005297 if (ret)
5298 return ret;
5299
Chris Wilson5e4f5182015-02-13 14:35:59 +00005300 /* This is just a security blanket to placate dragons.
5301 * On some systems, we very sporadically observe that the first TLBs
5302 * used by the CS may be stale, despite us poking the TLB reset. If
5303 * we hold the forcewake during initialisation these problems
5304 * just magically go away.
5305 */
Chris Wilsonee487002017-11-22 17:26:21 +00005306 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005307 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5308
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005309 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005310 if (ret) {
5311 GEM_BUG_ON(ret == -EIO);
5312 goto err_unlock;
5313 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005314
Chris Wilson829a0af2017-06-20 12:05:45 +01005315 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005316 if (ret) {
5317 GEM_BUG_ON(ret == -EIO);
5318 goto err_ggtt;
5319 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005320
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005321 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005322 if (ret) {
5323 GEM_BUG_ON(ret == -EIO);
5324 goto err_context;
5325 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005326
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005327 intel_init_gt_powersave(dev_priv);
5328
Michał Winiarski61b5c152017-12-13 23:13:48 +01005329 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005330 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005331 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005332
Michał Winiarski61b5c152017-12-13 23:13:48 +01005333 ret = i915_gem_init_hw(dev_priv);
5334 if (ret)
5335 goto err_uc_init;
5336
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005337 /*
5338 * Despite its name intel_init_clock_gating applies both display
5339 * clock gating workarounds; GT mmio workarounds and the occasional
5340 * GT power context workaround. Worse, sometimes it includes a context
5341 * register workaround which we need to apply before we record the
5342 * default HW state for all contexts.
5343 *
5344 * FIXME: break up the workarounds and apply them at the right time!
5345 */
5346 intel_init_clock_gating(dev_priv);
5347
Chris Wilsond2b4b972017-11-10 14:26:33 +00005348 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005349 if (ret)
5350 goto err_init_hw;
5351
5352 if (i915_inject_load_failure()) {
5353 ret = -ENODEV;
5354 goto err_init_hw;
5355 }
5356
5357 if (i915_inject_load_failure()) {
5358 ret = -EIO;
5359 goto err_init_hw;
5360 }
5361
5362 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5363 mutex_unlock(&dev_priv->drm.struct_mutex);
5364
5365 return 0;
5366
5367 /*
5368 * Unwinding is complicated by that we want to handle -EIO to mean
5369 * disable GPU submission but keep KMS alive. We want to mark the
5370 * HW as irrevisibly wedged, but keep enough state around that the
5371 * driver doesn't explode during runtime.
5372 */
5373err_init_hw:
5374 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5375 i915_gem_contexts_lost(dev_priv);
5376 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005377err_uc_init:
5378 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005379err_pm:
5380 if (ret != -EIO) {
5381 intel_cleanup_gt_powersave(dev_priv);
5382 i915_gem_cleanup_engines(dev_priv);
5383 }
5384err_context:
5385 if (ret != -EIO)
5386 i915_gem_contexts_fini(dev_priv);
5387err_ggtt:
5388err_unlock:
5389 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5390 mutex_unlock(&dev_priv->drm.struct_mutex);
5391
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305392 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305393
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005394 if (ret != -EIO)
5395 i915_gem_cleanup_userptr(dev_priv);
5396
Chris Wilson60990322014-04-09 09:19:42 +01005397 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005398 /*
5399 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005400 * wedged. But we only want to do this where the GPU is angry,
5401 * for all other failure, such as an allocation failure, bail.
5402 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005403 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5404 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5405 i915_gem_set_wedged(dev_priv);
5406 }
Chris Wilson60990322014-04-09 09:19:42 +01005407 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005408 }
5409
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005410 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005411 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005412}
5413
Chris Wilson24145512017-01-24 11:01:35 +00005414void i915_gem_init_mmio(struct drm_i915_private *i915)
5415{
5416 i915_gem_sanitize(i915);
5417}
5418
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005419void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005420i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005421{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005422 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305423 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005424
Akash Goel3b3f1652016-10-13 22:44:48 +05305425 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005426 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005427}
5428
Eric Anholt673a3942008-07-30 12:06:12 -07005429void
Imre Deak40ae4e12016-03-16 14:54:03 +02005430i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5431{
Chris Wilson49ef5292016-08-18 17:17:00 +01005432 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005433
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005434 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005435 !IS_CHERRYVIEW(dev_priv))
5436 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005437 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005438 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5439 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005440 dev_priv->num_fence_regs = 16;
5441 else
5442 dev_priv->num_fence_regs = 8;
5443
Chris Wilsonc0336662016-05-06 15:40:21 +01005444 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005445 dev_priv->num_fence_regs =
5446 I915_READ(vgtif_reg(avail_rs.fence_num));
5447
5448 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005449 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5450 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5451
5452 fence->i915 = dev_priv;
5453 fence->id = i;
5454 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5455 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005456 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005457
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005458 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005459}
5460
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005461static void i915_gem_init__mm(struct drm_i915_private *i915)
5462{
5463 spin_lock_init(&i915->mm.object_stat_lock);
5464 spin_lock_init(&i915->mm.obj_lock);
5465 spin_lock_init(&i915->mm.free_lock);
5466
5467 init_llist_head(&i915->mm.free_list);
5468
5469 INIT_LIST_HEAD(&i915->mm.unbound_list);
5470 INIT_LIST_HEAD(&i915->mm.bound_list);
5471 INIT_LIST_HEAD(&i915->mm.fence_list);
5472 INIT_LIST_HEAD(&i915->mm.userfault_list);
5473
5474 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5475}
5476
Chris Wilson73cb9702016-10-28 13:58:46 +01005477int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005478i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005479{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005480 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005481
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005482 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5483 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005484 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005485
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005486 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5487 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005488 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005489
Chris Wilsond1b48c12017-08-16 09:52:08 +01005490 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5491 if (!dev_priv->luts)
5492 goto err_vmas;
5493
Chris Wilsone61e0f52018-02-21 09:56:36 +00005494 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005495 SLAB_HWCACHE_ALIGN |
5496 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005497 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005498 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005499 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005500
Chris Wilson52e54202016-11-14 20:41:02 +00005501 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5502 SLAB_HWCACHE_ALIGN |
5503 SLAB_RECLAIM_ACCOUNT);
5504 if (!dev_priv->dependencies)
5505 goto err_requests;
5506
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005507 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5508 if (!dev_priv->priorities)
5509 goto err_dependencies;
5510
Chris Wilson73cb9702016-10-28 13:58:46 +01005511 mutex_lock(&dev_priv->drm.struct_mutex);
5512 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005513 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005514 mutex_unlock(&dev_priv->drm.struct_mutex);
5515 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005516 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005517
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005518 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005519
Chris Wilson67d97da2016-07-04 08:08:31 +01005520 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005521 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005522 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005523 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005524 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005525 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005526
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005527 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5528
Chris Wilsonb5add952016-08-04 16:32:36 +01005529 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005530
Matthew Auld465c4032017-10-06 23:18:14 +01005531 err = i915_gemfs_init(dev_priv);
5532 if (err)
5533 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5534
Chris Wilson73cb9702016-10-28 13:58:46 +01005535 return 0;
5536
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005537err_priorities:
5538 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005539err_dependencies:
5540 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005541err_requests:
5542 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005543err_luts:
5544 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005545err_vmas:
5546 kmem_cache_destroy(dev_priv->vmas);
5547err_objects:
5548 kmem_cache_destroy(dev_priv->objects);
5549err_out:
5550 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005551}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005552
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005553void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005554{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005555 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c70472018-02-19 22:06:31 +00005556 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5557 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005558 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005559
Matthew Auldea84aa72016-11-17 21:04:11 +00005560 mutex_lock(&dev_priv->drm.struct_mutex);
5561 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5562 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5563 mutex_unlock(&dev_priv->drm.struct_mutex);
5564
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005565 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005566 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005567 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005568 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005569 kmem_cache_destroy(dev_priv->vmas);
5570 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005571
5572 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5573 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005574
5575 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005576}
5577
Chris Wilson6a800ea2016-09-21 14:51:07 +01005578int i915_gem_freeze(struct drm_i915_private *dev_priv)
5579{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005580 /* Discard all purgeable objects, let userspace recover those as
5581 * required after resuming.
5582 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005583 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005584
Chris Wilson6a800ea2016-09-21 14:51:07 +01005585 return 0;
5586}
5587
Chris Wilson461fb992016-05-14 07:26:33 +01005588int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5589{
5590 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005591 struct list_head *phases[] = {
5592 &dev_priv->mm.unbound_list,
5593 &dev_priv->mm.bound_list,
5594 NULL
5595 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005596
5597 /* Called just before we write the hibernation image.
5598 *
5599 * We need to update the domain tracking to reflect that the CPU
5600 * will be accessing all the pages to create and restore from the
5601 * hibernation, and so upon restoration those pages will be in the
5602 * CPU domain.
5603 *
5604 * To make sure the hibernation image contains the latest state,
5605 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005606 *
5607 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005608 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005609 */
5610
Chris Wilson912d5722017-09-06 16:19:30 -07005611 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005612 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005613
Chris Wilsonf2123812017-10-16 12:40:37 +01005614 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005615 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005616 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005617 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005618 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005619 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005620
5621 return 0;
5622}
5623
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005624void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005625{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005626 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005627 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005628
5629 /* Clean up our request list when the client is going away, so that
5630 * later retire_requests won't dereference our soon-to-be-gone
5631 * file_priv.
5632 */
Chris Wilson1c255952010-09-26 11:03:27 +01005633 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005634 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005635 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005636 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005637}
5638
Chris Wilson829a0af2017-06-20 12:05:45 +01005639int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005640{
5641 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005642 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005643
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005644 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005645
5646 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5647 if (!file_priv)
5648 return -ENOMEM;
5649
5650 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005651 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005652 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005653
5654 spin_lock_init(&file_priv->mm.lock);
5655 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005656
Chris Wilsonc80ff162016-07-27 09:07:27 +01005657 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005658
Chris Wilson829a0af2017-06-20 12:05:45 +01005659 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005660 if (ret)
5661 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005662
Ben Widawskye422b882013-12-06 14:10:58 -08005663 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005664}
5665
Daniel Vetterb680c372014-09-19 18:27:27 +02005666/**
5667 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005668 * @old: current GEM buffer for the frontbuffer slots
5669 * @new: new GEM buffer for the frontbuffer slots
5670 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005671 *
5672 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5673 * from @old and setting them in @new. Both @old and @new can be NULL.
5674 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005675void i915_gem_track_fb(struct drm_i915_gem_object *old,
5676 struct drm_i915_gem_object *new,
5677 unsigned frontbuffer_bits)
5678{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005679 /* Control of individual bits within the mask are guarded by
5680 * the owning plane->mutex, i.e. we can never see concurrent
5681 * manipulation of individual bits. But since the bitfield as a whole
5682 * is updated using RMW, we need to use atomics in order to update
5683 * the bits.
5684 */
5685 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5686 sizeof(atomic_t) * BITS_PER_BYTE);
5687
Daniel Vettera071fa02014-06-18 23:28:09 +02005688 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005689 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5690 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005691 }
5692
5693 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005694 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5695 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005696 }
5697}
5698
Dave Gordonea702992015-07-09 19:29:02 +01005699/* Allocate a new GEM object and fill it with the supplied data */
5700struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005701i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005702 const void *data, size_t size)
5703{
5704 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005705 struct file *file;
5706 size_t offset;
5707 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005708
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005709 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005710 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005711 return obj;
5712
Christian Königc0a51fd2018-02-16 13:43:38 +01005713 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005714
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005715 file = obj->base.filp;
5716 offset = 0;
5717 do {
5718 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5719 struct page *page;
5720 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005721
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005722 err = pagecache_write_begin(file, file->f_mapping,
5723 offset, len, 0,
5724 &page, &pgdata);
5725 if (err < 0)
5726 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005727
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005728 vaddr = kmap(page);
5729 memcpy(vaddr, data, len);
5730 kunmap(page);
5731
5732 err = pagecache_write_end(file, file->f_mapping,
5733 offset, len, len,
5734 page, pgdata);
5735 if (err < 0)
5736 goto fail;
5737
5738 size -= len;
5739 data += len;
5740 offset += len;
5741 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005742
5743 return obj;
5744
5745fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005746 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005747 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005748}
Chris Wilson96d77632016-10-28 13:58:33 +01005749
5750struct scatterlist *
5751i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5752 unsigned int n,
5753 unsigned int *offset)
5754{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005755 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005756 struct scatterlist *sg;
5757 unsigned int idx, count;
5758
5759 might_sleep();
5760 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005761 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005762
5763 /* As we iterate forward through the sg, we record each entry in a
5764 * radixtree for quick repeated (backwards) lookups. If we have seen
5765 * this index previously, we will have an entry for it.
5766 *
5767 * Initial lookup is O(N), but this is amortized to O(1) for
5768 * sequential page access (where each new request is consecutive
5769 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5770 * i.e. O(1) with a large constant!
5771 */
5772 if (n < READ_ONCE(iter->sg_idx))
5773 goto lookup;
5774
5775 mutex_lock(&iter->lock);
5776
5777 /* We prefer to reuse the last sg so that repeated lookup of this
5778 * (or the subsequent) sg are fast - comparing against the last
5779 * sg is faster than going through the radixtree.
5780 */
5781
5782 sg = iter->sg_pos;
5783 idx = iter->sg_idx;
5784 count = __sg_page_count(sg);
5785
5786 while (idx + count <= n) {
5787 unsigned long exception, i;
5788 int ret;
5789
5790 /* If we cannot allocate and insert this entry, or the
5791 * individual pages from this range, cancel updating the
5792 * sg_idx so that on this lookup we are forced to linearly
5793 * scan onwards, but on future lookups we will try the
5794 * insertion again (in which case we need to be careful of
5795 * the error return reporting that we have already inserted
5796 * this index).
5797 */
5798 ret = radix_tree_insert(&iter->radix, idx, sg);
5799 if (ret && ret != -EEXIST)
5800 goto scan;
5801
5802 exception =
5803 RADIX_TREE_EXCEPTIONAL_ENTRY |
5804 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5805 for (i = 1; i < count; i++) {
5806 ret = radix_tree_insert(&iter->radix, idx + i,
5807 (void *)exception);
5808 if (ret && ret != -EEXIST)
5809 goto scan;
5810 }
5811
5812 idx += count;
5813 sg = ____sg_next(sg);
5814 count = __sg_page_count(sg);
5815 }
5816
5817scan:
5818 iter->sg_pos = sg;
5819 iter->sg_idx = idx;
5820
5821 mutex_unlock(&iter->lock);
5822
5823 if (unlikely(n < idx)) /* insertion completed by another thread */
5824 goto lookup;
5825
5826 /* In case we failed to insert the entry into the radixtree, we need
5827 * to look beyond the current sg.
5828 */
5829 while (idx + count <= n) {
5830 idx += count;
5831 sg = ____sg_next(sg);
5832 count = __sg_page_count(sg);
5833 }
5834
5835 *offset = n - idx;
5836 return sg;
5837
5838lookup:
5839 rcu_read_lock();
5840
5841 sg = radix_tree_lookup(&iter->radix, n);
5842 GEM_BUG_ON(!sg);
5843
5844 /* If this index is in the middle of multi-page sg entry,
5845 * the radixtree will contain an exceptional entry that points
5846 * to the start of that range. We will return the pointer to
5847 * the base page and the offset of this page within the
5848 * sg entry's range.
5849 */
5850 *offset = 0;
5851 if (unlikely(radix_tree_exception(sg))) {
5852 unsigned long base =
5853 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5854
5855 sg = radix_tree_lookup(&iter->radix, base);
5856 GEM_BUG_ON(!sg);
5857
5858 *offset = n - base;
5859 }
5860
5861 rcu_read_unlock();
5862
5863 return sg;
5864}
5865
5866struct page *
5867i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5868{
5869 struct scatterlist *sg;
5870 unsigned int offset;
5871
5872 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5873
5874 sg = i915_gem_object_get_sg(obj, n, &offset);
5875 return nth_page(sg_page(sg), offset);
5876}
5877
5878/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5879struct page *
5880i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5881 unsigned int n)
5882{
5883 struct page *page;
5884
5885 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005886 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005887 set_page_dirty(page);
5888
5889 return page;
5890}
5891
5892dma_addr_t
5893i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5894 unsigned long n)
5895{
5896 struct scatterlist *sg;
5897 unsigned int offset;
5898
5899 sg = i915_gem_object_get_sg(obj, n, &offset);
5900 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5901}
Chris Wilson935a2f72017-02-13 17:15:13 +00005902
Chris Wilson8eeb7902017-07-26 19:16:01 +01005903int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5904{
5905 struct sg_table *pages;
5906 int err;
5907
5908 if (align > obj->base.size)
5909 return -EINVAL;
5910
5911 if (obj->ops == &i915_gem_phys_ops)
5912 return 0;
5913
5914 if (obj->ops != &i915_gem_object_ops)
5915 return -EINVAL;
5916
5917 err = i915_gem_object_unbind(obj);
5918 if (err)
5919 return err;
5920
5921 mutex_lock(&obj->mm.lock);
5922
5923 if (obj->mm.madv != I915_MADV_WILLNEED) {
5924 err = -EFAULT;
5925 goto err_unlock;
5926 }
5927
5928 if (obj->mm.quirked) {
5929 err = -EFAULT;
5930 goto err_unlock;
5931 }
5932
5933 if (obj->mm.mapping) {
5934 err = -EBUSY;
5935 goto err_unlock;
5936 }
5937
Chris Wilsonf2123812017-10-16 12:40:37 +01005938 pages = fetch_and_zero(&obj->mm.pages);
5939 if (pages) {
5940 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5941
5942 __i915_gem_object_reset_page_iter(obj);
5943
5944 spin_lock(&i915->mm.obj_lock);
5945 list_del(&obj->mm.link);
5946 spin_unlock(&i915->mm.obj_lock);
5947 }
5948
Chris Wilson8eeb7902017-07-26 19:16:01 +01005949 obj->ops = &i915_gem_phys_ops;
5950
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005951 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005952 if (err)
5953 goto err_xfer;
5954
5955 /* Perma-pin (until release) the physical set of pages */
5956 __i915_gem_object_pin_pages(obj);
5957
5958 if (!IS_ERR_OR_NULL(pages))
5959 i915_gem_object_ops.put_pages(obj, pages);
5960 mutex_unlock(&obj->mm.lock);
5961 return 0;
5962
5963err_xfer:
5964 obj->ops = &i915_gem_object_ops;
5965 obj->mm.pages = pages;
5966err_unlock:
5967 mutex_unlock(&obj->mm.lock);
5968 return err;
5969}
5970
Chris Wilson935a2f72017-02-13 17:15:13 +00005971#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5972#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005973#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005974#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005975#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005976#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005977#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005978#endif