blob: ab88ca53c9a0387985860d0fbcf8d48a0553c2a2 [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
3284 /* Before unwedging, make sure that all pending operations
3285 * are flushed and errored out - we may have requests waiting upon
3286 * third party fences. We marked all inflight requests as EIO, and
3287 * every execbuf since returned EIO, for consistency we want all
3288 * the currently pending requests to also be marked as EIO, which
3289 * is done inside our nop_submit_request - and so we must wait.
3290 *
3291 * No more can be submitted until we reset the wedged bit.
3292 */
3293 list_for_each_entry(tl, &i915->gt.timelines, link) {
3294 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003295 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003296
3297 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3298 &i915->drm.struct_mutex);
3299 if (!rq)
3300 continue;
3301
3302 /* We can't use our normal waiter as we want to
3303 * avoid recursively trying to handle the current
3304 * reset. The basic dma_fence_default_wait() installs
3305 * a callback for dma_fence_signal(), which is
3306 * triggered by our nop handler (indirectly, the
3307 * callback enables the signaler thread which is
3308 * woken by the nop_submit_request() advancing the seqno
3309 * and when the seqno passes the fence, the signaler
3310 * then signals the fence waking us up).
3311 */
3312 if (dma_fence_default_wait(&rq->fence, true,
3313 MAX_SCHEDULE_TIMEOUT) < 0)
3314 return false;
3315 }
3316 }
3317
3318 /* Undo nop_submit_request. We prevent all new i915 requests from
3319 * being queued (by disallowing execbuf whilst wedged) so having
3320 * waited for all active requests above, we know the system is idle
3321 * and do not have to worry about a thread being inside
3322 * engine->submit_request() as we swap over. So unlike installing
3323 * the nop_submit_request on reset, we can do this from normal
3324 * context and do not require stop_machine().
3325 */
3326 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003327 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003328
3329 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3330 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3331
3332 return true;
3333}
3334
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003335static void
Eric Anholt673a3942008-07-30 12:06:12 -07003336i915_gem_retire_work_handler(struct work_struct *work)
3337{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003338 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003339 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003340 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003341
Chris Wilson891b48c2010-09-29 12:26:37 +01003342 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003343 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003344 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003345 mutex_unlock(&dev->struct_mutex);
3346 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003347
Chris Wilson88923042018-01-29 14:41:04 +00003348 /*
3349 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003350 * We do not need to do this test under locking as in the worst-case
3351 * we queue the retire worker once too often.
3352 */
Chris Wilson88923042018-01-29 14:41:04 +00003353 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003354 queue_delayed_work(dev_priv->wq,
3355 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003356 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003357}
Chris Wilson891b48c2010-09-29 12:26:37 +01003358
Chris Wilson84a10742018-01-24 11:36:08 +00003359static void shrink_caches(struct drm_i915_private *i915)
3360{
3361 /*
3362 * kmem_cache_shrink() discards empty slabs and reorders partially
3363 * filled slabs to prioritise allocating from the mostly full slabs,
3364 * with the aim of reducing fragmentation.
3365 */
3366 kmem_cache_shrink(i915->priorities);
3367 kmem_cache_shrink(i915->dependencies);
3368 kmem_cache_shrink(i915->requests);
3369 kmem_cache_shrink(i915->luts);
3370 kmem_cache_shrink(i915->vmas);
3371 kmem_cache_shrink(i915->objects);
3372}
3373
3374struct sleep_rcu_work {
3375 union {
3376 struct rcu_head rcu;
3377 struct work_struct work;
3378 };
3379 struct drm_i915_private *i915;
3380 unsigned int epoch;
3381};
3382
3383static inline bool
3384same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3385{
3386 /*
3387 * There is a small chance that the epoch wrapped since we started
3388 * sleeping. If we assume that epoch is at least a u32, then it will
3389 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3390 */
3391 return epoch == READ_ONCE(i915->gt.epoch);
3392}
3393
3394static void __sleep_work(struct work_struct *work)
3395{
3396 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3397 struct drm_i915_private *i915 = s->i915;
3398 unsigned int epoch = s->epoch;
3399
3400 kfree(s);
3401 if (same_epoch(i915, epoch))
3402 shrink_caches(i915);
3403}
3404
3405static void __sleep_rcu(struct rcu_head *rcu)
3406{
3407 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3408 struct drm_i915_private *i915 = s->i915;
3409
3410 if (same_epoch(i915, s->epoch)) {
3411 INIT_WORK(&s->work, __sleep_work);
3412 queue_work(i915->wq, &s->work);
3413 } else {
3414 kfree(s);
3415 }
3416}
3417
Chris Wilson5427f202017-10-23 22:32:34 +01003418static inline bool
3419new_requests_since_last_retire(const struct drm_i915_private *i915)
3420{
3421 return (READ_ONCE(i915->gt.active_requests) ||
3422 work_pending(&i915->gt.idle_work.work));
3423}
3424
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003425static void
3426i915_gem_idle_work_handler(struct work_struct *work)
3427{
3428 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003429 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003430 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003431 bool rearm_hangcheck;
3432
3433 if (!READ_ONCE(dev_priv->gt.awake))
3434 return;
3435
Imre Deak0cb56702016-11-07 11:20:04 +02003436 /*
3437 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003438 * new request is submitted. As we don't trust the hardware, we
3439 * continue on if the wait times out. This is necessary to allow
3440 * the machine to suspend even if the hardware dies, and we will
3441 * try to recover in resume (after depriving the hardware of power,
3442 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003443 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003444 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3445 intel_engines_are_idle(dev_priv),
3446 I915_IDLE_ENGINES_TIMEOUT * 1000,
3447 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003448
3449 rearm_hangcheck =
3450 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3451
Chris Wilson5427f202017-10-23 22:32:34 +01003452 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003453 /* Currently busy, come back later */
3454 mod_delayed_work(dev_priv->wq,
3455 &dev_priv->gt.idle_work,
3456 msecs_to_jiffies(50));
3457 goto out_rearm;
3458 }
3459
Imre Deak93c97dc2016-11-07 11:20:03 +02003460 /*
3461 * New request retired after this work handler started, extend active
3462 * period until next instance of the work.
3463 */
Chris Wilson5427f202017-10-23 22:32:34 +01003464 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003465 goto out_unlock;
3466
Chris Wilson5427f202017-10-23 22:32:34 +01003467 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003468 * Be paranoid and flush a concurrent interrupt to make sure
3469 * we don't reactivate any irq tasklets after parking.
3470 *
3471 * FIXME: Note that even though we have waited for execlists to be idle,
3472 * there may still be an in-flight interrupt even though the CSB
3473 * is now empty. synchronize_irq() makes sure that a residual interrupt
3474 * is completed before we continue, but it doesn't prevent the HW from
3475 * raising a spurious interrupt later. To complete the shield we should
3476 * coordinate disabling the CS irq with flushing the interrupts.
3477 */
3478 synchronize_irq(dev_priv->drm.irq);
3479
Chris Wilsonaba5e272017-10-25 15:39:41 +01003480 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003481 i915_gem_timelines_park(dev_priv);
3482
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003483 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003484
Chris Wilson67d97da2016-07-04 08:08:31 +01003485 GEM_BUG_ON(!dev_priv->gt.awake);
3486 dev_priv->gt.awake = false;
Chris Wilson84a10742018-01-24 11:36:08 +00003487 epoch = dev_priv->gt.epoch;
3488 GEM_BUG_ON(epoch == I915_EPOCH_INVALID);
Chris Wilson67d97da2016-07-04 08:08:31 +01003489 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003490
Chris Wilson67d97da2016-07-04 08:08:31 +01003491 if (INTEL_GEN(dev_priv) >= 6)
3492 gen6_rps_idle(dev_priv);
Tvrtko Ursulinb6876372017-12-05 13:28:54 +00003493
3494 intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ);
3495
Chris Wilson67d97da2016-07-04 08:08:31 +01003496 intel_runtime_pm_put(dev_priv);
3497out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003498 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003499
Chris Wilson67d97da2016-07-04 08:08:31 +01003500out_rearm:
3501 if (rearm_hangcheck) {
3502 GEM_BUG_ON(!dev_priv->gt.awake);
3503 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003504 }
Chris Wilson84a10742018-01-24 11:36:08 +00003505
3506 /*
3507 * When we are idle, it is an opportune time to reap our caches.
3508 * However, we have many objects that utilise RCU and the ordered
3509 * i915->wq that this work is executing on. To try and flush any
3510 * pending frees now we are idle, we first wait for an RCU grace
3511 * period, and then queue a task (that will run last on the wq) to
3512 * shrink and re-optimize the caches.
3513 */
3514 if (same_epoch(dev_priv, epoch)) {
3515 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3516 if (s) {
3517 s->i915 = dev_priv;
3518 s->epoch = epoch;
3519 call_rcu(&s->rcu, __sleep_rcu);
3520 }
3521 }
Eric Anholt673a3942008-07-30 12:06:12 -07003522}
3523
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003524void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3525{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003526 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003527 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3528 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003529 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003530
Chris Wilsond1b48c12017-08-16 09:52:08 +01003531 mutex_lock(&i915->drm.struct_mutex);
3532
3533 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3534 struct i915_gem_context *ctx = lut->ctx;
3535 struct i915_vma *vma;
3536
Chris Wilson432295d2017-08-22 12:05:15 +01003537 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003538 if (ctx->file_priv != fpriv)
3539 continue;
3540
3541 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003542 GEM_BUG_ON(vma->obj != obj);
3543
3544 /* We allow the process to have multiple handles to the same
3545 * vma, in the same fd namespace, by virtue of flink/open.
3546 */
3547 GEM_BUG_ON(!vma->open_count);
3548 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003549 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003550
Chris Wilsond1b48c12017-08-16 09:52:08 +01003551 list_del(&lut->obj_link);
3552 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003553
Chris Wilsond1b48c12017-08-16 09:52:08 +01003554 kmem_cache_free(i915->luts, lut);
3555 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003556 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003557
3558 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003559}
3560
Chris Wilsone95433c2016-10-28 13:58:27 +01003561static unsigned long to_wait_timeout(s64 timeout_ns)
3562{
3563 if (timeout_ns < 0)
3564 return MAX_SCHEDULE_TIMEOUT;
3565
3566 if (timeout_ns == 0)
3567 return 0;
3568
3569 return nsecs_to_jiffies_timeout(timeout_ns);
3570}
3571
Ben Widawsky5816d642012-04-11 11:18:19 -07003572/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003573 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003574 * @dev: drm device pointer
3575 * @data: ioctl data blob
3576 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003577 *
3578 * Returns 0 if successful, else an error is returned with the remaining time in
3579 * the timeout parameter.
3580 * -ETIME: object is still busy after timeout
3581 * -ERESTARTSYS: signal interrupted the wait
3582 * -ENONENT: object doesn't exist
3583 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003584 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003585 * -ENOMEM: damn
3586 * -ENODEV: Internal IRQ fail
3587 * -E?: The add request failed
3588 *
3589 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3590 * non-zero timeout parameter the wait ioctl will wait for the given number of
3591 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3592 * without holding struct_mutex the object may become re-busied before this
3593 * function completes. A similar but shorter * race condition exists in the busy
3594 * ioctl
3595 */
3596int
3597i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3598{
3599 struct drm_i915_gem_wait *args = data;
3600 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003601 ktime_t start;
3602 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003603
Daniel Vetter11b5d512014-09-29 15:31:26 +02003604 if (args->flags != 0)
3605 return -EINVAL;
3606
Chris Wilson03ac0642016-07-20 13:31:51 +01003607 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003608 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003609 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003610
Chris Wilsone95433c2016-10-28 13:58:27 +01003611 start = ktime_get();
3612
3613 ret = i915_gem_object_wait(obj,
3614 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3615 to_wait_timeout(args->timeout_ns),
3616 to_rps_client(file));
3617
3618 if (args->timeout_ns > 0) {
3619 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3620 if (args->timeout_ns < 0)
3621 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003622
3623 /*
3624 * Apparently ktime isn't accurate enough and occasionally has a
3625 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3626 * things up to make the test happy. We allow up to 1 jiffy.
3627 *
3628 * This is a regression from the timespec->ktime conversion.
3629 */
3630 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3631 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003632
3633 /* Asked to wait beyond the jiffie/scheduler precision? */
3634 if (ret == -ETIME && args->timeout_ns)
3635 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003636 }
3637
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003638 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003639 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003640}
3641
Chris Wilson73cb9702016-10-28 13:58:46 +01003642static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003643{
Chris Wilson73cb9702016-10-28 13:58:46 +01003644 int ret, i;
3645
3646 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3647 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3648 if (ret)
3649 return ret;
3650 }
3651
3652 return 0;
3653}
3654
Chris Wilson25112b62017-03-30 15:50:39 +01003655static int wait_for_engines(struct drm_i915_private *i915)
3656{
Chris Wilsonee42c002017-12-11 19:41:34 +00003657 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003658 dev_err(i915->drm.dev,
3659 "Failed to idle engines, declaring wedged!\n");
3660 if (drm_debug & DRM_UT_DRIVER) {
3661 struct drm_printer p = drm_debug_printer(__func__);
3662 struct intel_engine_cs *engine;
3663 enum intel_engine_id id;
3664
3665 for_each_engine(engine, i915, id)
3666 intel_engine_dump(engine, &p,
Chris Wilson9e519bc2018-02-05 10:06:18 +00003667 "%s\n", engine->name);
Chris Wilson59e4b192017-12-11 19:41:35 +00003668 }
3669
Chris Wilsoncad99462017-08-26 12:09:33 +01003670 i915_gem_set_wedged(i915);
3671 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003672 }
3673
3674 return 0;
3675}
3676
Chris Wilson73cb9702016-10-28 13:58:46 +01003677int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3678{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003679 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003680
Chris Wilson863e9fd2017-05-30 13:13:32 +01003681 /* If the device is asleep, we have no requests outstanding */
3682 if (!READ_ONCE(i915->gt.awake))
3683 return 0;
3684
Chris Wilson9caa34a2016-11-11 14:58:08 +00003685 if (flags & I915_WAIT_LOCKED) {
3686 struct i915_gem_timeline *tl;
3687
3688 lockdep_assert_held(&i915->drm.struct_mutex);
3689
3690 list_for_each_entry(tl, &i915->gt.timelines, link) {
3691 ret = wait_for_timeline(tl, flags);
3692 if (ret)
3693 return ret;
3694 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003695 i915_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003696
3697 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003698 } else {
3699 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003700 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003701
Chris Wilson25112b62017-03-30 15:50:39 +01003702 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003703}
3704
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003705static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3706{
Chris Wilsone27ab732017-06-15 13:38:49 +01003707 /*
3708 * We manually flush the CPU domain so that we can override and
3709 * force the flush for the display, and perform it asyncrhonously.
3710 */
3711 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3712 if (obj->cache_dirty)
3713 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003714 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003715}
3716
3717void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3718{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003719 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003720 return;
3721
3722 mutex_lock(&obj->base.dev->struct_mutex);
3723 __i915_gem_object_flush_for_display(obj);
3724 mutex_unlock(&obj->base.dev->struct_mutex);
3725}
3726
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003727/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003728 * Moves a single object to the WC read, and possibly write domain.
3729 * @obj: object to act on
3730 * @write: ask for write access or read only
3731 *
3732 * This function returns when the move is complete, including waiting on
3733 * flushes to occur.
3734 */
3735int
3736i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3737{
3738 int ret;
3739
3740 lockdep_assert_held(&obj->base.dev->struct_mutex);
3741
3742 ret = i915_gem_object_wait(obj,
3743 I915_WAIT_INTERRUPTIBLE |
3744 I915_WAIT_LOCKED |
3745 (write ? I915_WAIT_ALL : 0),
3746 MAX_SCHEDULE_TIMEOUT,
3747 NULL);
3748 if (ret)
3749 return ret;
3750
Christian Königc0a51fd2018-02-16 13:43:38 +01003751 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003752 return 0;
3753
3754 /* Flush and acquire obj->pages so that we are coherent through
3755 * direct access in memory with previous cached writes through
3756 * shmemfs and that our cache domain tracking remains valid.
3757 * For example, if the obj->filp was moved to swap without us
3758 * being notified and releasing the pages, we would mistakenly
3759 * continue to assume that the obj remained out of the CPU cached
3760 * domain.
3761 */
3762 ret = i915_gem_object_pin_pages(obj);
3763 if (ret)
3764 return ret;
3765
3766 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3767
3768 /* Serialise direct access to this object with the barriers for
3769 * coherent writes from the GPU, by effectively invalidating the
3770 * WC domain upon first access.
3771 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003772 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003773 mb();
3774
3775 /* It should now be out of any other write domains, and we can update
3776 * the domain values for our changes.
3777 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003778 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3779 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003780 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003781 obj->read_domains = I915_GEM_DOMAIN_WC;
3782 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003783 obj->mm.dirty = true;
3784 }
3785
3786 i915_gem_object_unpin_pages(obj);
3787 return 0;
3788}
3789
3790/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003791 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003792 * @obj: object to act on
3793 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003794 *
3795 * This function returns when the move is complete, including waiting on
3796 * flushes to occur.
3797 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003798int
Chris Wilson20217462010-11-23 15:26:33 +00003799i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003800{
Eric Anholte47c68e2008-11-14 13:35:19 -08003801 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003802
Chris Wilsone95433c2016-10-28 13:58:27 +01003803 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003804
Chris Wilsone95433c2016-10-28 13:58:27 +01003805 ret = i915_gem_object_wait(obj,
3806 I915_WAIT_INTERRUPTIBLE |
3807 I915_WAIT_LOCKED |
3808 (write ? I915_WAIT_ALL : 0),
3809 MAX_SCHEDULE_TIMEOUT,
3810 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003811 if (ret)
3812 return ret;
3813
Christian Königc0a51fd2018-02-16 13:43:38 +01003814 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003815 return 0;
3816
Chris Wilson43566de2015-01-02 16:29:29 +05303817 /* Flush and acquire obj->pages so that we are coherent through
3818 * direct access in memory with previous cached writes through
3819 * shmemfs and that our cache domain tracking remains valid.
3820 * For example, if the obj->filp was moved to swap without us
3821 * being notified and releasing the pages, we would mistakenly
3822 * continue to assume that the obj remained out of the CPU cached
3823 * domain.
3824 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003825 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303826 if (ret)
3827 return ret;
3828
Chris Wilsonef749212017-04-12 12:01:10 +01003829 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003830
Chris Wilsond0a57782012-10-09 19:24:37 +01003831 /* Serialise direct access to this object with the barriers for
3832 * coherent writes from the GPU, by effectively invalidating the
3833 * GTT domain upon first access.
3834 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003835 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003836 mb();
3837
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003838 /* It should now be out of any other write domains, and we can update
3839 * the domain values for our changes.
3840 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003841 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3842 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003843 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003844 obj->read_domains = I915_GEM_DOMAIN_GTT;
3845 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003846 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003847 }
3848
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003849 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003850 return 0;
3851}
3852
Chris Wilsonef55f922015-10-09 14:11:27 +01003853/**
3854 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003855 * @obj: object to act on
3856 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003857 *
3858 * After this function returns, the object will be in the new cache-level
3859 * across all GTT and the contents of the backing storage will be coherent,
3860 * with respect to the new cache-level. In order to keep the backing storage
3861 * coherent for all users, we only allow a single cache level to be set
3862 * globally on the object and prevent it from being changed whilst the
3863 * hardware is reading from the object. That is if the object is currently
3864 * on the scanout it will be set to uncached (or equivalent display
3865 * cache coherency) and all non-MOCS GPU access will also be uncached so
3866 * that all direct access to the scanout remains coherent.
3867 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003868int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3869 enum i915_cache_level cache_level)
3870{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003871 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003872 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003873
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003874 lockdep_assert_held(&obj->base.dev->struct_mutex);
3875
Chris Wilsone4ffd172011-04-04 09:44:39 +01003876 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003877 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003878
Chris Wilsonef55f922015-10-09 14:11:27 +01003879 /* Inspect the list of currently bound VMA and unbind any that would
3880 * be invalid given the new cache-level. This is principally to
3881 * catch the issue of the CS prefetch crossing page boundaries and
3882 * reading an invalid PTE on older architectures.
3883 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003884restart:
3885 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003886 if (!drm_mm_node_allocated(&vma->node))
3887 continue;
3888
Chris Wilson20dfbde2016-08-04 16:32:30 +01003889 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003890 DRM_DEBUG("can not change the cache level of pinned objects\n");
3891 return -EBUSY;
3892 }
3893
Chris Wilson010e3e62017-12-06 12:49:13 +00003894 if (!i915_vma_is_closed(vma) &&
3895 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003896 continue;
3897
3898 ret = i915_vma_unbind(vma);
3899 if (ret)
3900 return ret;
3901
3902 /* As unbinding may affect other elements in the
3903 * obj->vma_list (due to side-effects from retiring
3904 * an active vma), play safe and restart the iterator.
3905 */
3906 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003907 }
3908
Chris Wilsonef55f922015-10-09 14:11:27 +01003909 /* We can reuse the existing drm_mm nodes but need to change the
3910 * cache-level on the PTE. We could simply unbind them all and
3911 * rebind with the correct cache-level on next use. However since
3912 * we already have a valid slot, dma mapping, pages etc, we may as
3913 * rewrite the PTE in the belief that doing so tramples upon less
3914 * state and so involves less work.
3915 */
Chris Wilson15717de2016-08-04 07:52:26 +01003916 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003917 /* Before we change the PTE, the GPU must not be accessing it.
3918 * If we wait upon the object, we know that all the bound
3919 * VMA are no longer active.
3920 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003921 ret = i915_gem_object_wait(obj,
3922 I915_WAIT_INTERRUPTIBLE |
3923 I915_WAIT_LOCKED |
3924 I915_WAIT_ALL,
3925 MAX_SCHEDULE_TIMEOUT,
3926 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003927 if (ret)
3928 return ret;
3929
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003930 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3931 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003932 /* Access to snoopable pages through the GTT is
3933 * incoherent and on some machines causes a hard
3934 * lockup. Relinquish the CPU mmaping to force
3935 * userspace to refault in the pages and we can
3936 * then double check if the GTT mapping is still
3937 * valid for that pointer access.
3938 */
3939 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003940
Chris Wilsonef55f922015-10-09 14:11:27 +01003941 /* As we no longer need a fence for GTT access,
3942 * we can relinquish it now (and so prevent having
3943 * to steal a fence from someone else on the next
3944 * fence request). Note GPU activity would have
3945 * dropped the fence as all snoopable access is
3946 * supposed to be linear.
3947 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003948 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003949 ret = i915_vma_put_fence(vma);
3950 if (ret)
3951 return ret;
3952 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003953 } else {
3954 /* We either have incoherent backing store and
3955 * so no GTT access or the architecture is fully
3956 * coherent. In such cases, existing GTT mmaps
3957 * ignore the cache bit in the PTE and we can
3958 * rewrite it without confusing the GPU or having
3959 * to force userspace to fault back in its mmaps.
3960 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003961 }
3962
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003963 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003964 if (!drm_mm_node_allocated(&vma->node))
3965 continue;
3966
3967 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3968 if (ret)
3969 return ret;
3970 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003971 }
3972
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003973 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003974 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003975 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003976 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003977
Chris Wilsone4ffd172011-04-04 09:44:39 +01003978 return 0;
3979}
3980
Ben Widawsky199adf42012-09-21 17:01:20 -07003981int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3982 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003983{
Ben Widawsky199adf42012-09-21 17:01:20 -07003984 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003985 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003986 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003987
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003988 rcu_read_lock();
3989 obj = i915_gem_object_lookup_rcu(file, args->handle);
3990 if (!obj) {
3991 err = -ENOENT;
3992 goto out;
3993 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003994
Chris Wilson651d7942013-08-08 14:41:10 +01003995 switch (obj->cache_level) {
3996 case I915_CACHE_LLC:
3997 case I915_CACHE_L3_LLC:
3998 args->caching = I915_CACHING_CACHED;
3999 break;
4000
Chris Wilson4257d3b2013-08-08 14:41:11 +01004001 case I915_CACHE_WT:
4002 args->caching = I915_CACHING_DISPLAY;
4003 break;
4004
Chris Wilson651d7942013-08-08 14:41:10 +01004005 default:
4006 args->caching = I915_CACHING_NONE;
4007 break;
4008 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004009out:
4010 rcu_read_unlock();
4011 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004012}
4013
Ben Widawsky199adf42012-09-21 17:01:20 -07004014int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4015 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004016{
Chris Wilson9c870d02016-10-24 13:42:15 +01004017 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004018 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004019 struct drm_i915_gem_object *obj;
4020 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004021 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004022
Ben Widawsky199adf42012-09-21 17:01:20 -07004023 switch (args->caching) {
4024 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004025 level = I915_CACHE_NONE;
4026 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004027 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004028 /*
4029 * Due to a HW issue on BXT A stepping, GPU stores via a
4030 * snooped mapping may leave stale data in a corresponding CPU
4031 * cacheline, whereas normally such cachelines would get
4032 * invalidated.
4033 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004034 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004035 return -ENODEV;
4036
Chris Wilsone6994ae2012-07-10 10:27:08 +01004037 level = I915_CACHE_LLC;
4038 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004039 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004040 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004041 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004042 default:
4043 return -EINVAL;
4044 }
4045
Chris Wilsond65415d2017-01-19 08:22:10 +00004046 obj = i915_gem_object_lookup(file, args->handle);
4047 if (!obj)
4048 return -ENOENT;
4049
Tina Zhanga03f3952017-11-14 10:25:13 +00004050 /*
4051 * The caching mode of proxy object is handled by its generator, and
4052 * not allowed to be changed by userspace.
4053 */
4054 if (i915_gem_object_is_proxy(obj)) {
4055 ret = -ENXIO;
4056 goto out;
4057 }
4058
Chris Wilsond65415d2017-01-19 08:22:10 +00004059 if (obj->cache_level == level)
4060 goto out;
4061
4062 ret = i915_gem_object_wait(obj,
4063 I915_WAIT_INTERRUPTIBLE,
4064 MAX_SCHEDULE_TIMEOUT,
4065 to_rps_client(file));
4066 if (ret)
4067 goto out;
4068
Ben Widawsky3bc29132012-09-26 16:15:20 -07004069 ret = i915_mutex_lock_interruptible(dev);
4070 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004071 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004072
4073 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004074 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004075
4076out:
4077 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004078 return ret;
4079}
4080
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004081/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004082 * Prepare buffer for display plane (scanout, cursors, etc).
4083 * Can be called from an uninterruptible phase (modesetting) and allows
4084 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004085 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004086struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004087i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4088 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004089 const struct i915_ggtt_view *view,
4090 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004091{
Chris Wilson058d88c2016-08-15 10:49:06 +01004092 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004093 int ret;
4094
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004095 lockdep_assert_held(&obj->base.dev->struct_mutex);
4096
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004097 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004098 * display coherency whilst setting up the cache domains.
4099 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004100 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004101
Eric Anholta7ef0642011-03-29 16:59:54 -07004102 /* The display engine is not coherent with the LLC cache on gen6. As
4103 * a result, we make sure that the pinning that is about to occur is
4104 * done with uncached PTEs. This is lowest common denominator for all
4105 * chipsets.
4106 *
4107 * However for gen6+, we could do better by using the GFDT bit instead
4108 * of uncaching, which would allow us to flush all the LLC-cached data
4109 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4110 */
Chris Wilson651d7942013-08-08 14:41:10 +01004111 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004112 HAS_WT(to_i915(obj->base.dev)) ?
4113 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004114 if (ret) {
4115 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004116 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004117 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004118
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004119 /* As the user may map the buffer once pinned in the display plane
4120 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004121 * always use map_and_fenceable for all scanout buffers. However,
4122 * it may simply be too big to fit into mappable, in which case
4123 * put it anyway and hope that userspace can cope (but always first
4124 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004125 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004126 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004127 if ((flags & PIN_MAPPABLE) == 0 &&
4128 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004129 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004130 flags |
4131 PIN_MAPPABLE |
4132 PIN_NONBLOCK);
4133 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004134 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004135 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004136 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004137
Chris Wilsond8923dc2016-08-18 17:17:07 +01004138 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4139
Chris Wilsona6a7cc42016-11-18 21:17:46 +00004140 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004141 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00004142 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004143
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004144 /* It should now be out of any other write domains, and we can update
4145 * the domain values for our changes.
4146 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004147 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004148
Chris Wilson058d88c2016-08-15 10:49:06 +01004149 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004150
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004151err_unpin_global:
4152 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004153 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004154}
4155
4156void
Chris Wilson058d88c2016-08-15 10:49:06 +01004157i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004158{
Chris Wilson49d73912016-11-29 09:50:08 +00004159 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004160
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004161 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004162 return;
4163
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004164 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004165 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004166
Chris Wilson383d5822016-08-18 17:17:08 +01004167 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004168 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004169
Chris Wilson058d88c2016-08-15 10:49:06 +01004170 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004171}
4172
Eric Anholte47c68e2008-11-14 13:35:19 -08004173/**
4174 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004175 * @obj: object to act on
4176 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004177 *
4178 * This function returns when the move is complete, including waiting on
4179 * flushes to occur.
4180 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004181int
Chris Wilson919926a2010-11-12 13:42:53 +00004182i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004183{
Eric Anholte47c68e2008-11-14 13:35:19 -08004184 int ret;
4185
Chris Wilsone95433c2016-10-28 13:58:27 +01004186 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004187
Chris Wilsone95433c2016-10-28 13:58:27 +01004188 ret = i915_gem_object_wait(obj,
4189 I915_WAIT_INTERRUPTIBLE |
4190 I915_WAIT_LOCKED |
4191 (write ? I915_WAIT_ALL : 0),
4192 MAX_SCHEDULE_TIMEOUT,
4193 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004194 if (ret)
4195 return ret;
4196
Chris Wilsonef749212017-04-12 12:01:10 +01004197 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004198
Eric Anholte47c68e2008-11-14 13:35:19 -08004199 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004200 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004201 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004202 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004203 }
4204
4205 /* It should now be out of any other write domains, and we can update
4206 * the domain values for our changes.
4207 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004208 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004209
4210 /* If we're writing through the CPU, then the GPU read domains will
4211 * need to be invalidated at next use.
4212 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004213 if (write)
4214 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004215
4216 return 0;
4217}
4218
Eric Anholt673a3942008-07-30 12:06:12 -07004219/* Throttle our rendering by waiting until the ring has completed our requests
4220 * emitted over 20 msec ago.
4221 *
Eric Anholtb9624422009-06-03 07:27:35 +00004222 * Note that if we were to use the current jiffies each time around the loop,
4223 * we wouldn't escape the function with any frames outstanding if the time to
4224 * render a frame was over 20ms.
4225 *
Eric Anholt673a3942008-07-30 12:06:12 -07004226 * This should get us reasonable parallelism between CPU and GPU but also
4227 * relatively low latency when blocking on a particular request to finish.
4228 */
4229static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004230i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004231{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004232 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004233 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004234 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004235 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004236 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004237
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004238 /* ABI: return -EIO if already wedged */
4239 if (i915_terminally_wedged(&dev_priv->gpu_error))
4240 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004241
Chris Wilson1c255952010-09-26 11:03:27 +01004242 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004243 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004244 if (time_after_eq(request->emitted_jiffies, recent_enough))
4245 break;
4246
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004247 if (target) {
4248 list_del(&target->client_link);
4249 target->file_priv = NULL;
4250 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004251
John Harrison54fb2412014-11-24 18:49:27 +00004252 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004253 }
John Harrisonff865882014-11-24 18:49:28 +00004254 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004255 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004256 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004257
John Harrison54fb2412014-11-24 18:49:27 +00004258 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004259 return 0;
4260
Chris Wilsone61e0f52018-02-21 09:56:36 +00004261 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004262 I915_WAIT_INTERRUPTIBLE,
4263 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004264 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004265
Chris Wilsone95433c2016-10-28 13:58:27 +01004266 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004267}
4268
Chris Wilson058d88c2016-08-15 10:49:06 +01004269struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004270i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4271 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004272 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004273 u64 alignment,
4274 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004275{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004276 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4277 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004278 struct i915_vma *vma;
4279 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004280
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004281 lockdep_assert_held(&obj->base.dev->struct_mutex);
4282
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004283 if (flags & PIN_MAPPABLE &&
4284 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004285 /* If the required space is larger than the available
4286 * aperture, we will not able to find a slot for the
4287 * object and unbinding the object now will be in
4288 * vain. Worse, doing so may cause us to ping-pong
4289 * the object in and out of the Global GTT and
4290 * waste a lot of cycles under the mutex.
4291 */
4292 if (obj->base.size > dev_priv->ggtt.mappable_end)
4293 return ERR_PTR(-E2BIG);
4294
4295 /* If NONBLOCK is set the caller is optimistically
4296 * trying to cache the full object within the mappable
4297 * aperture, and *must* have a fallback in place for
4298 * situations where we cannot bind the object. We
4299 * can be a little more lax here and use the fallback
4300 * more often to avoid costly migrations of ourselves
4301 * and other objects within the aperture.
4302 *
4303 * Half-the-aperture is used as a simple heuristic.
4304 * More interesting would to do search for a free
4305 * block prior to making the commitment to unbind.
4306 * That caters for the self-harm case, and with a
4307 * little more heuristics (e.g. NOFAULT, NOEVICT)
4308 * we could try to minimise harm to others.
4309 */
4310 if (flags & PIN_NONBLOCK &&
4311 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4312 return ERR_PTR(-ENOSPC);
4313 }
4314
Chris Wilson718659a2017-01-16 15:21:28 +00004315 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004316 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004317 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004318
4319 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004320 if (flags & PIN_NONBLOCK) {
4321 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4322 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004323
Chris Wilson43ae70d2017-10-09 09:44:01 +01004324 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004325 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004326 return ERR_PTR(-ENOSPC);
4327 }
4328
Chris Wilson59bfa122016-08-04 16:32:31 +01004329 WARN(i915_vma_is_pinned(vma),
4330 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004331 " offset=%08x, req.alignment=%llx,"
4332 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4333 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004334 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004335 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004336 ret = i915_vma_unbind(vma);
4337 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004338 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004339 }
4340
Chris Wilson058d88c2016-08-15 10:49:06 +01004341 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4342 if (ret)
4343 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004344
Chris Wilson058d88c2016-08-15 10:49:06 +01004345 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004346}
4347
Chris Wilsonedf6b762016-08-09 09:23:33 +01004348static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004349{
4350 /* Note that we could alias engines in the execbuf API, but
4351 * that would be very unwise as it prevents userspace from
4352 * fine control over engine selection. Ahem.
4353 *
4354 * This should be something like EXEC_MAX_ENGINE instead of
4355 * I915_NUM_ENGINES.
4356 */
4357 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4358 return 0x10000 << id;
4359}
4360
4361static __always_inline unsigned int __busy_write_id(unsigned int id)
4362{
Chris Wilson70cb4722016-08-09 18:08:25 +01004363 /* The uABI guarantees an active writer is also amongst the read
4364 * engines. This would be true if we accessed the activity tracking
4365 * under the lock, but as we perform the lookup of the object and
4366 * its activity locklessly we can not guarantee that the last_write
4367 * being active implies that we have set the same engine flag from
4368 * last_read - hence we always set both read and write busy for
4369 * last_write.
4370 */
4371 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004372}
4373
Chris Wilsonedf6b762016-08-09 09:23:33 +01004374static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004375__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004376 unsigned int (*flag)(unsigned int id))
4377{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004378 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004379
Chris Wilsond07f0e52016-10-28 13:58:44 +01004380 /* We have to check the current hw status of the fence as the uABI
4381 * guarantees forward progress. We could rely on the idle worker
4382 * to eventually flush us, but to minimise latency just ask the
4383 * hardware.
4384 *
4385 * Note we only report on the status of native fences.
4386 */
4387 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004388 return 0;
4389
Chris Wilsond07f0e52016-10-28 13:58:44 +01004390 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004391 rq = container_of(fence, struct i915_request, fence);
4392 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004393 return 0;
4394
Chris Wilson1d39f282017-04-11 13:43:06 +01004395 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004396}
4397
Chris Wilsonedf6b762016-08-09 09:23:33 +01004398static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004399busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004400{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004401 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004402}
4403
Chris Wilsonedf6b762016-08-09 09:23:33 +01004404static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004405busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004406{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004407 if (!fence)
4408 return 0;
4409
4410 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004411}
4412
Eric Anholt673a3942008-07-30 12:06:12 -07004413int
Eric Anholt673a3942008-07-30 12:06:12 -07004414i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004415 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004416{
4417 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004418 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004419 struct reservation_object_list *list;
4420 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004421 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004422
Chris Wilsond07f0e52016-10-28 13:58:44 +01004423 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004424 rcu_read_lock();
4425 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004426 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004427 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004428
4429 /* A discrepancy here is that we do not report the status of
4430 * non-i915 fences, i.e. even though we may report the object as idle,
4431 * a call to set-domain may still stall waiting for foreign rendering.
4432 * This also means that wait-ioctl may report an object as busy,
4433 * where busy-ioctl considers it idle.
4434 *
4435 * We trade the ability to warn of foreign fences to report on which
4436 * i915 engines are active for the object.
4437 *
4438 * Alternatively, we can trade that extra information on read/write
4439 * activity with
4440 * args->busy =
4441 * !reservation_object_test_signaled_rcu(obj->resv, true);
4442 * to report the overall busyness. This is what the wait-ioctl does.
4443 *
4444 */
4445retry:
4446 seq = raw_read_seqcount(&obj->resv->seq);
4447
4448 /* Translate the exclusive fence to the READ *and* WRITE engine */
4449 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4450
4451 /* Translate shared fences to READ set of engines */
4452 list = rcu_dereference(obj->resv->fence);
4453 if (list) {
4454 unsigned int shared_count = list->shared_count, i;
4455
4456 for (i = 0; i < shared_count; ++i) {
4457 struct dma_fence *fence =
4458 rcu_dereference(list->shared[i]);
4459
4460 args->busy |= busy_check_reader(fence);
4461 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004462 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004463
Chris Wilsond07f0e52016-10-28 13:58:44 +01004464 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4465 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004466
Chris Wilsond07f0e52016-10-28 13:58:44 +01004467 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004468out:
4469 rcu_read_unlock();
4470 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004471}
4472
4473int
4474i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4475 struct drm_file *file_priv)
4476{
Akshay Joshi0206e352011-08-16 15:34:10 -04004477 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004478}
4479
Chris Wilson3ef94da2009-09-14 16:50:29 +01004480int
4481i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4482 struct drm_file *file_priv)
4483{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004484 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004485 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004486 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004487 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004488
4489 switch (args->madv) {
4490 case I915_MADV_DONTNEED:
4491 case I915_MADV_WILLNEED:
4492 break;
4493 default:
4494 return -EINVAL;
4495 }
4496
Chris Wilson03ac0642016-07-20 13:31:51 +01004497 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004498 if (!obj)
4499 return -ENOENT;
4500
4501 err = mutex_lock_interruptible(&obj->mm.lock);
4502 if (err)
4503 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004504
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004505 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004506 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004507 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004508 if (obj->mm.madv == I915_MADV_WILLNEED) {
4509 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004510 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004511 obj->mm.quirked = false;
4512 }
4513 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004514 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004515 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004516 obj->mm.quirked = true;
4517 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004518 }
4519
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004520 if (obj->mm.madv != __I915_MADV_PURGED)
4521 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004522
Chris Wilson6c085a72012-08-20 11:40:46 +02004523 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004524 if (obj->mm.madv == I915_MADV_DONTNEED &&
4525 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004526 i915_gem_object_truncate(obj);
4527
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004528 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004529 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004530
Chris Wilson1233e2d2016-10-28 13:58:37 +01004531out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004532 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004533 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004534}
4535
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004536static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004537frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004538{
4539 struct drm_i915_gem_object *obj =
4540 container_of(active, typeof(*obj), frontbuffer_write);
4541
Chris Wilsond59b21e2017-02-22 11:40:49 +00004542 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004543}
4544
Chris Wilson37e680a2012-06-07 15:38:42 +01004545void i915_gem_object_init(struct drm_i915_gem_object *obj,
4546 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004547{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004548 mutex_init(&obj->mm.lock);
4549
Ben Widawsky2f633152013-07-17 12:19:03 -07004550 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004551 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004552 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004553
Chris Wilson37e680a2012-06-07 15:38:42 +01004554 obj->ops = ops;
4555
Chris Wilsond07f0e52016-10-28 13:58:44 +01004556 reservation_object_init(&obj->__builtin_resv);
4557 obj->resv = &obj->__builtin_resv;
4558
Chris Wilson50349242016-08-18 17:17:04 +01004559 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004560 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004561
4562 obj->mm.madv = I915_MADV_WILLNEED;
4563 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4564 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004565
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004566 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004567}
4568
Chris Wilson37e680a2012-06-07 15:38:42 +01004569static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004570 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4571 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004572
Chris Wilson37e680a2012-06-07 15:38:42 +01004573 .get_pages = i915_gem_object_get_pages_gtt,
4574 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004575
4576 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004577};
4578
Matthew Auld465c4032017-10-06 23:18:14 +01004579static int i915_gem_object_create_shmem(struct drm_device *dev,
4580 struct drm_gem_object *obj,
4581 size_t size)
4582{
4583 struct drm_i915_private *i915 = to_i915(dev);
4584 unsigned long flags = VM_NORESERVE;
4585 struct file *filp;
4586
4587 drm_gem_private_object_init(dev, obj, size);
4588
4589 if (i915->mm.gemfs)
4590 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4591 flags);
4592 else
4593 filp = shmem_file_setup("i915", size, flags);
4594
4595 if (IS_ERR(filp))
4596 return PTR_ERR(filp);
4597
4598 obj->filp = filp;
4599
4600 return 0;
4601}
4602
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004603struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004604i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004605{
Daniel Vetterc397b902010-04-09 19:05:07 +00004606 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004607 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004608 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004609 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004610 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004611
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004612 /* There is a prevalence of the assumption that we fit the object's
4613 * page count inside a 32bit _signed_ variable. Let's document this and
4614 * catch if we ever need to fix it. In the meantime, if you do spot
4615 * such a local variable, please consider fixing!
4616 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004617 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004618 return ERR_PTR(-E2BIG);
4619
4620 if (overflows_type(size, obj->base.size))
4621 return ERR_PTR(-E2BIG);
4622
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004623 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004624 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004625 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004626
Matthew Auld465c4032017-10-06 23:18:14 +01004627 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004628 if (ret)
4629 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004630
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004631 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004632 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004633 /* 965gm cannot relocate objects above 4GiB. */
4634 mask &= ~__GFP_HIGHMEM;
4635 mask |= __GFP_DMA32;
4636 }
4637
Al Viro93c76a32015-12-04 23:45:44 -05004638 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004639 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004640 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004641
Chris Wilson37e680a2012-06-07 15:38:42 +01004642 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004643
Christian Königc0a51fd2018-02-16 13:43:38 +01004644 obj->write_domain = I915_GEM_DOMAIN_CPU;
4645 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004646
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004647 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004648 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004649 * cache) for about a 10% performance improvement
4650 * compared to uncached. Graphics requests other than
4651 * display scanout are coherent with the CPU in
4652 * accessing this cache. This means in this mode we
4653 * don't need to clflush on the CPU side, and on the
4654 * GPU side we only need to flush internal caches to
4655 * get data visible to the CPU.
4656 *
4657 * However, we maintain the display planes as UC, and so
4658 * need to rebind when first used as such.
4659 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004660 cache_level = I915_CACHE_LLC;
4661 else
4662 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004663
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004664 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004665
Daniel Vetterd861e332013-07-24 23:25:03 +02004666 trace_i915_gem_object_create(obj);
4667
Chris Wilson05394f32010-11-08 19:18:58 +00004668 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004669
4670fail:
4671 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004672 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004673}
4674
Chris Wilson340fbd82014-05-22 09:16:52 +01004675static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4676{
4677 /* If we are the last user of the backing storage (be it shmemfs
4678 * pages or stolen etc), we know that the pages are going to be
4679 * immediately released. In this case, we can then skip copying
4680 * back the contents from the GPU.
4681 */
4682
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004683 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004684 return false;
4685
4686 if (obj->base.filp == NULL)
4687 return true;
4688
4689 /* At first glance, this looks racy, but then again so would be
4690 * userspace racing mmap against close. However, the first external
4691 * reference to the filp can only be obtained through the
4692 * i915_gem_mmap_ioctl() which safeguards us against the user
4693 * acquiring such a reference whilst we are in the middle of
4694 * freeing the object.
4695 */
4696 return atomic_long_read(&obj->base.filp->f_count) == 1;
4697}
4698
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004699static void __i915_gem_free_objects(struct drm_i915_private *i915,
4700 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004701{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004702 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004703
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004704 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004705 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004706 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004707
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004708 trace_i915_gem_object_destroy(obj);
4709
Chris Wilsoncc731f52017-10-13 21:26:21 +01004710 mutex_lock(&i915->drm.struct_mutex);
4711
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004712 GEM_BUG_ON(i915_gem_object_is_active(obj));
4713 list_for_each_entry_safe(vma, vn,
4714 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004715 GEM_BUG_ON(i915_vma_is_active(vma));
4716 vma->flags &= ~I915_VMA_PIN_MASK;
4717 i915_vma_close(vma);
4718 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004719 GEM_BUG_ON(!list_empty(&obj->vma_list));
4720 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004721
Chris Wilsonf2123812017-10-16 12:40:37 +01004722 /* This serializes freeing with the shrinker. Since the free
4723 * is delayed, first by RCU then by the workqueue, we want the
4724 * shrinker to be able to free pages of unreferenced objects,
4725 * or else we may oom whilst there are plenty of deferred
4726 * freed objects.
4727 */
4728 if (i915_gem_object_has_pages(obj)) {
4729 spin_lock(&i915->mm.obj_lock);
4730 list_del_init(&obj->mm.link);
4731 spin_unlock(&i915->mm.obj_lock);
4732 }
4733
Chris Wilsoncc731f52017-10-13 21:26:21 +01004734 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004735
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004736 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004737 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004738 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004739 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004740
4741 if (obj->ops->release)
4742 obj->ops->release(obj);
4743
4744 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4745 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004746 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004747 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004748
4749 if (obj->base.import_attach)
4750 drm_prime_gem_destroy(&obj->base, NULL);
4751
Chris Wilsond07f0e52016-10-28 13:58:44 +01004752 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004753 drm_gem_object_release(&obj->base);
4754 i915_gem_info_remove_obj(i915, obj->base.size);
4755
4756 kfree(obj->bit_17);
4757 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004758
Chris Wilsonc9c70472018-02-19 22:06:31 +00004759 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4760 atomic_dec(&i915->mm.free_count);
4761
Chris Wilsoncc731f52017-10-13 21:26:21 +01004762 if (on)
4763 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004764 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004765 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004766}
4767
4768static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4769{
4770 struct llist_node *freed;
4771
Chris Wilson87701b42017-10-13 21:26:20 +01004772 /* Free the oldest, most stale object to keep the free_list short */
4773 freed = NULL;
4774 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4775 /* Only one consumer of llist_del_first() allowed */
4776 spin_lock(&i915->mm.free_lock);
4777 freed = llist_del_first(&i915->mm.free_list);
4778 spin_unlock(&i915->mm.free_lock);
4779 }
4780 if (unlikely(freed)) {
4781 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004782 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004783 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004784}
4785
4786static void __i915_gem_free_work(struct work_struct *work)
4787{
4788 struct drm_i915_private *i915 =
4789 container_of(work, struct drm_i915_private, mm.free_work);
4790 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004791
Chris Wilson2ef1e722018-01-15 20:57:59 +00004792 /*
4793 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004794 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4795 * However, the object may also be bound into the global GTT (e.g.
4796 * older GPUs without per-process support, or for direct access through
4797 * the GTT either for the user or for scanout). Those VMA still need to
4798 * unbound now.
4799 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004800
Chris Wilsonf991c492017-11-06 11:15:08 +00004801 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004802 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004803 spin_unlock(&i915->mm.free_lock);
4804
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004805 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004806 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004807 return;
4808
4809 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004810 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004811 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004812}
4813
4814static void __i915_gem_free_object_rcu(struct rcu_head *head)
4815{
4816 struct drm_i915_gem_object *obj =
4817 container_of(head, typeof(*obj), rcu);
4818 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4819
Chris Wilson2ef1e722018-01-15 20:57:59 +00004820 /*
4821 * Since we require blocking on struct_mutex to unbind the freed
4822 * object from the GPU before releasing resources back to the
4823 * system, we can not do that directly from the RCU callback (which may
4824 * be a softirq context), but must instead then defer that work onto a
4825 * kthread. We use the RCU callback rather than move the freed object
4826 * directly onto the work queue so that we can mix between using the
4827 * worker and performing frees directly from subsequent allocations for
4828 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004829 */
4830 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004831 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004832}
4833
4834void i915_gem_free_object(struct drm_gem_object *gem_obj)
4835{
4836 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4837
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004838 if (obj->mm.quirked)
4839 __i915_gem_object_unpin_pages(obj);
4840
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004841 if (discard_backing_storage(obj))
4842 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004843
Chris Wilson2ef1e722018-01-15 20:57:59 +00004844 /*
4845 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004846 * read-side critical sections are complete, e.g.
4847 * i915_gem_busy_ioctl(). For the corresponding synchronized
4848 * lookup see i915_gem_object_lookup_rcu().
4849 */
Chris Wilsonc9c70472018-02-19 22:06:31 +00004850 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004851 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004852}
4853
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004854void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4855{
4856 lockdep_assert_held(&obj->base.dev->struct_mutex);
4857
Chris Wilsond1b48c12017-08-16 09:52:08 +01004858 if (!i915_gem_object_has_active_reference(obj) &&
4859 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004860 i915_gem_object_set_active_reference(obj);
4861 else
4862 i915_gem_object_put(obj);
4863}
4864
Chris Wilsonae6c4572017-11-10 14:26:28 +00004865static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004866{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004867 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004868 struct intel_engine_cs *engine;
4869 enum intel_engine_id id;
4870
Chris Wilsonae6c4572017-11-10 14:26:28 +00004871 for_each_engine(engine, i915, id) {
4872 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4873 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4874 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004875}
4876
Chris Wilson24145512017-01-24 11:01:35 +00004877void i915_gem_sanitize(struct drm_i915_private *i915)
4878{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004879 if (i915_terminally_wedged(&i915->gpu_error)) {
4880 mutex_lock(&i915->drm.struct_mutex);
4881 i915_gem_unset_wedged(i915);
4882 mutex_unlock(&i915->drm.struct_mutex);
4883 }
4884
Chris Wilson24145512017-01-24 11:01:35 +00004885 /*
4886 * If we inherit context state from the BIOS or earlier occupants
4887 * of the GPU, the GPU may be in an inconsistent state when we
4888 * try to take over. The only way to remove the earlier state
4889 * is by resetting. However, resetting on earlier gen is tricky as
4890 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004891 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004892 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004893 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4894 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004895}
4896
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004897int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004898{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004899 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004900 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004901
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004902 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004903 intel_suspend_gt_powersave(dev_priv);
4904
Chris Wilson45c5f202013-10-16 11:50:01 +01004905 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004906
4907 /* We have to flush all the executing contexts to main memory so
4908 * that they can saved in the hibernation image. To ensure the last
4909 * context image is coherent, we have to switch away from it. That
4910 * leaves the dev_priv->kernel_context still active when
4911 * we actually suspend, and its image in memory may not match the GPU
4912 * state. Fortunately, the kernel_context is disposable and we do
4913 * not rely on its state.
4914 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004915 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4916 ret = i915_gem_switch_to_kernel_context(dev_priv);
4917 if (ret)
4918 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004919
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004920 ret = i915_gem_wait_for_idle(dev_priv,
4921 I915_WAIT_INTERRUPTIBLE |
4922 I915_WAIT_LOCKED);
4923 if (ret && ret != -EIO)
4924 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004925
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004926 assert_kernel_context_is_current(dev_priv);
4927 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004928 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004929 mutex_unlock(&dev->struct_mutex);
4930
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004931 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304932
Chris Wilson737b1502015-01-26 18:03:03 +02004933 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004934 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004935
4936 /* As the idle_work is rearming if it detects a race, play safe and
4937 * repeat the flush until it is definitely idle.
4938 */
Chris Wilson7c262402017-10-06 11:40:38 +01004939 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004940
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004941 /* Assert that we sucessfully flushed all the work and
4942 * reset the GPU back to its idle, low power state.
4943 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004944 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004945 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4946 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004947
Imre Deak1c777c52016-10-12 17:46:37 +03004948 /*
4949 * Neither the BIOS, ourselves or any other kernel
4950 * expects the system to be in execlists mode on startup,
4951 * so we need to reset the GPU back to legacy mode. And the only
4952 * known way to disable logical contexts is through a GPU reset.
4953 *
4954 * So in order to leave the system in a known default configuration,
4955 * always reset the GPU upon unload and suspend. Afterwards we then
4956 * clean up the GEM state tracking, flushing off the requests and
4957 * leaving the system in a known idle state.
4958 *
4959 * Note that is of the upmost importance that the GPU is idle and
4960 * all stray writes are flushed *before* we dismantle the backing
4961 * storage for the pinned objects.
4962 *
4963 * However, since we are uncertain that resetting the GPU on older
4964 * machines is a good idea, we don't - just in case it leaves the
4965 * machine in an unusable condition.
4966 */
Chris Wilson24145512017-01-24 11:01:35 +00004967 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004968
4969 intel_runtime_pm_put(dev_priv);
4970 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004971
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004972err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004973 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004974 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004975 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004976}
4977
Chris Wilson37cd3302017-11-12 11:27:38 +00004978void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004979{
Chris Wilson37cd3302017-11-12 11:27:38 +00004980 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004981
Chris Wilson37cd3302017-11-12 11:27:38 +00004982 mutex_lock(&i915->drm.struct_mutex);
4983 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004984
Chris Wilson37cd3302017-11-12 11:27:38 +00004985 i915_gem_restore_gtt_mappings(i915);
4986 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004987
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004988 /*
4989 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004990 * guarantee that the context image is complete. So let's just reset
4991 * it and start again.
4992 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004993 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004994
Chris Wilson37cd3302017-11-12 11:27:38 +00004995 if (i915_gem_init_hw(i915))
4996 goto err_wedged;
4997
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004998 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00004999
Chris Wilson37cd3302017-11-12 11:27:38 +00005000 /* Always reload a context for powersaving. */
5001 if (i915_gem_switch_to_kernel_context(i915))
5002 goto err_wedged;
5003
5004out_unlock:
5005 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5006 mutex_unlock(&i915->drm.struct_mutex);
5007 return;
5008
5009err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005010 if (!i915_terminally_wedged(&i915->gpu_error)) {
5011 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5012 i915_gem_set_wedged(i915);
5013 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005014 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005015}
5016
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005017void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005018{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005019 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005020 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5021 return;
5022
5023 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5024 DISP_TILE_SURFACE_SWIZZLING);
5025
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005026 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005027 return;
5028
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005029 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005030 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005031 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005032 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005033 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005034 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005035 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005036 else
5037 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005038}
Daniel Vettere21af882012-02-09 20:53:27 +01005039
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005040static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005041{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005042 I915_WRITE(RING_CTL(base), 0);
5043 I915_WRITE(RING_HEAD(base), 0);
5044 I915_WRITE(RING_TAIL(base), 0);
5045 I915_WRITE(RING_START(base), 0);
5046}
5047
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005048static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005049{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005050 if (IS_I830(dev_priv)) {
5051 init_unused_ring(dev_priv, PRB1_BASE);
5052 init_unused_ring(dev_priv, SRB0_BASE);
5053 init_unused_ring(dev_priv, SRB1_BASE);
5054 init_unused_ring(dev_priv, SRB2_BASE);
5055 init_unused_ring(dev_priv, SRB3_BASE);
5056 } else if (IS_GEN2(dev_priv)) {
5057 init_unused_ring(dev_priv, SRB0_BASE);
5058 init_unused_ring(dev_priv, SRB1_BASE);
5059 } else if (IS_GEN3(dev_priv)) {
5060 init_unused_ring(dev_priv, PRB1_BASE);
5061 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005062 }
5063}
5064
Chris Wilson20a8a742017-02-08 14:30:31 +00005065static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005066{
Chris Wilson20a8a742017-02-08 14:30:31 +00005067 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005068 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305069 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005070 int err;
5071
5072 for_each_engine(engine, i915, id) {
5073 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005074 if (err) {
5075 DRM_ERROR("Failed to restart %s (%d)\n",
5076 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005077 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005078 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005079 }
5080
5081 return 0;
5082}
5083
5084int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5085{
Chris Wilsond200cda2016-04-28 09:56:44 +01005086 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005087
Chris Wilsonde867c22016-10-25 13:16:02 +01005088 dev_priv->gt.last_init_time = ktime_get();
5089
Chris Wilson5e4f5182015-02-13 14:35:59 +00005090 /* Double layer security blanket, see i915_gem_init() */
5091 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5092
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005093 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005094 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005095
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005096 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005097 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005098 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005099
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005100 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005101 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005102 u32 temp = I915_READ(GEN7_MSG_CTL);
5103 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5104 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005105 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005106 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5107 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5108 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5109 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005110 }
5111
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005112 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005113
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005114 /*
5115 * At least 830 can leave some of the unused rings
5116 * "active" (ie. head != tail) after resume which
5117 * will prevent c3 entry. Makes sure all unused rings
5118 * are totally idle.
5119 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005120 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005121
Dave Gordoned54c1a2016-01-19 19:02:54 +00005122 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005123 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5124 ret = -EIO;
5125 goto out;
5126 }
John Harrison90638cc2015-05-29 17:43:37 +01005127
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005128 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005129 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005130 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005131 goto out;
5132 }
5133
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005134 /* We can't enable contexts until all firmware is loaded */
5135 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005136 if (ret) {
5137 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005138 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005139 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005140
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005141 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005142
Chris Wilson136109c2017-11-02 13:14:30 +00005143 /* Only when the HW is re-initialised, can we replay the requests */
5144 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005145out:
5146 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005147 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005148}
5149
Chris Wilsond2b4b972017-11-10 14:26:33 +00005150static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5151{
5152 struct i915_gem_context *ctx;
5153 struct intel_engine_cs *engine;
5154 enum intel_engine_id id;
5155 int err;
5156
5157 /*
5158 * As we reset the gpu during very early sanitisation, the current
5159 * register state on the GPU should reflect its defaults values.
5160 * We load a context onto the hw (with restore-inhibit), then switch
5161 * over to a second context to save that default register state. We
5162 * can then prime every new context with that state so they all start
5163 * from the same default HW values.
5164 */
5165
5166 ctx = i915_gem_context_create_kernel(i915, 0);
5167 if (IS_ERR(ctx))
5168 return PTR_ERR(ctx);
5169
5170 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005171 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005172
Chris Wilsone61e0f52018-02-21 09:56:36 +00005173 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005174 if (IS_ERR(rq)) {
5175 err = PTR_ERR(rq);
5176 goto out_ctx;
5177 }
5178
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005179 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005180 if (engine->init_context)
5181 err = engine->init_context(rq);
5182
Chris Wilsone61e0f52018-02-21 09:56:36 +00005183 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005184 if (err)
5185 goto err_active;
5186 }
5187
5188 err = i915_gem_switch_to_kernel_context(i915);
5189 if (err)
5190 goto err_active;
5191
5192 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5193 if (err)
5194 goto err_active;
5195
5196 assert_kernel_context_is_current(i915);
5197
5198 for_each_engine(engine, i915, id) {
5199 struct i915_vma *state;
5200
5201 state = ctx->engine[id].state;
5202 if (!state)
5203 continue;
5204
5205 /*
5206 * As we will hold a reference to the logical state, it will
5207 * not be torn down with the context, and importantly the
5208 * object will hold onto its vma (making it possible for a
5209 * stray GTT write to corrupt our defaults). Unmap the vma
5210 * from the GTT to prevent such accidents and reclaim the
5211 * space.
5212 */
5213 err = i915_vma_unbind(state);
5214 if (err)
5215 goto err_active;
5216
5217 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5218 if (err)
5219 goto err_active;
5220
5221 engine->default_state = i915_gem_object_get(state->obj);
5222 }
5223
5224 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5225 unsigned int found = intel_engines_has_context_isolation(i915);
5226
5227 /*
5228 * Make sure that classes with multiple engine instances all
5229 * share the same basic configuration.
5230 */
5231 for_each_engine(engine, i915, id) {
5232 unsigned int bit = BIT(engine->uabi_class);
5233 unsigned int expected = engine->default_state ? bit : 0;
5234
5235 if ((found & bit) != expected) {
5236 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5237 engine->uabi_class, engine->name);
5238 }
5239 }
5240 }
5241
5242out_ctx:
5243 i915_gem_context_set_closed(ctx);
5244 i915_gem_context_put(ctx);
5245 return err;
5246
5247err_active:
5248 /*
5249 * If we have to abandon now, we expect the engines to be idle
5250 * and ready to be torn-down. First try to flush any remaining
5251 * request, ensure we are pointing at the kernel context and
5252 * then remove it.
5253 */
5254 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5255 goto out_ctx;
5256
5257 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5258 goto out_ctx;
5259
5260 i915_gem_contexts_lost(i915);
5261 goto out_ctx;
5262}
5263
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005264int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005265{
Chris Wilson1070a422012-04-24 15:47:41 +01005266 int ret;
5267
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005268 /*
5269 * We need to fallback to 4K pages since gvt gtt handling doesn't
5270 * support huge page entries - we will need to check either hypervisor
5271 * mm can support huge guest page or just do emulation in gvt.
5272 */
5273 if (intel_vgpu_active(dev_priv))
5274 mkwrite_device_info(dev_priv)->page_sizes =
5275 I915_GTT_PAGE_SIZE_4K;
5276
Chris Wilson94312822017-05-03 10:39:18 +01005277 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005278
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005279 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005280 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005281 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005282 } else {
5283 dev_priv->gt.resume = intel_legacy_submission_resume;
5284 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005285 }
5286
Chris Wilsonee487002017-11-22 17:26:21 +00005287 ret = i915_gem_init_userptr(dev_priv);
5288 if (ret)
5289 return ret;
5290
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305291 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005292 if (ret)
5293 return ret;
5294
Chris Wilson5e4f5182015-02-13 14:35:59 +00005295 /* This is just a security blanket to placate dragons.
5296 * On some systems, we very sporadically observe that the first TLBs
5297 * used by the CS may be stale, despite us poking the TLB reset. If
5298 * we hold the forcewake during initialisation these problems
5299 * just magically go away.
5300 */
Chris Wilsonee487002017-11-22 17:26:21 +00005301 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005302 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5303
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005304 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005305 if (ret) {
5306 GEM_BUG_ON(ret == -EIO);
5307 goto err_unlock;
5308 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005309
Chris Wilson829a0af2017-06-20 12:05:45 +01005310 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005311 if (ret) {
5312 GEM_BUG_ON(ret == -EIO);
5313 goto err_ggtt;
5314 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005315
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005316 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005317 if (ret) {
5318 GEM_BUG_ON(ret == -EIO);
5319 goto err_context;
5320 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005321
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005322 intel_init_gt_powersave(dev_priv);
5323
Michał Winiarski61b5c152017-12-13 23:13:48 +01005324 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005325 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005326 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005327
Michał Winiarski61b5c152017-12-13 23:13:48 +01005328 ret = i915_gem_init_hw(dev_priv);
5329 if (ret)
5330 goto err_uc_init;
5331
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005332 /*
5333 * Despite its name intel_init_clock_gating applies both display
5334 * clock gating workarounds; GT mmio workarounds and the occasional
5335 * GT power context workaround. Worse, sometimes it includes a context
5336 * register workaround which we need to apply before we record the
5337 * default HW state for all contexts.
5338 *
5339 * FIXME: break up the workarounds and apply them at the right time!
5340 */
5341 intel_init_clock_gating(dev_priv);
5342
Chris Wilsond2b4b972017-11-10 14:26:33 +00005343 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005344 if (ret)
5345 goto err_init_hw;
5346
5347 if (i915_inject_load_failure()) {
5348 ret = -ENODEV;
5349 goto err_init_hw;
5350 }
5351
5352 if (i915_inject_load_failure()) {
5353 ret = -EIO;
5354 goto err_init_hw;
5355 }
5356
5357 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5358 mutex_unlock(&dev_priv->drm.struct_mutex);
5359
5360 return 0;
5361
5362 /*
5363 * Unwinding is complicated by that we want to handle -EIO to mean
5364 * disable GPU submission but keep KMS alive. We want to mark the
5365 * HW as irrevisibly wedged, but keep enough state around that the
5366 * driver doesn't explode during runtime.
5367 */
5368err_init_hw:
5369 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5370 i915_gem_contexts_lost(dev_priv);
5371 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005372err_uc_init:
5373 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005374err_pm:
5375 if (ret != -EIO) {
5376 intel_cleanup_gt_powersave(dev_priv);
5377 i915_gem_cleanup_engines(dev_priv);
5378 }
5379err_context:
5380 if (ret != -EIO)
5381 i915_gem_contexts_fini(dev_priv);
5382err_ggtt:
5383err_unlock:
5384 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5385 mutex_unlock(&dev_priv->drm.struct_mutex);
5386
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305387 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305388
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005389 if (ret != -EIO)
5390 i915_gem_cleanup_userptr(dev_priv);
5391
Chris Wilson60990322014-04-09 09:19:42 +01005392 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005393 /*
5394 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005395 * wedged. But we only want to do this where the GPU is angry,
5396 * for all other failure, such as an allocation failure, bail.
5397 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005398 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5399 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5400 i915_gem_set_wedged(dev_priv);
5401 }
Chris Wilson60990322014-04-09 09:19:42 +01005402 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005403 }
5404
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005405 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005406 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005407}
5408
Chris Wilson24145512017-01-24 11:01:35 +00005409void i915_gem_init_mmio(struct drm_i915_private *i915)
5410{
5411 i915_gem_sanitize(i915);
5412}
5413
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005414void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005415i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005416{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005417 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305418 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005419
Akash Goel3b3f1652016-10-13 22:44:48 +05305420 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005421 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005422}
5423
Eric Anholt673a3942008-07-30 12:06:12 -07005424void
Imre Deak40ae4e12016-03-16 14:54:03 +02005425i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5426{
Chris Wilson49ef5292016-08-18 17:17:00 +01005427 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005428
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005429 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005430 !IS_CHERRYVIEW(dev_priv))
5431 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005432 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005433 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5434 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005435 dev_priv->num_fence_regs = 16;
5436 else
5437 dev_priv->num_fence_regs = 8;
5438
Chris Wilsonc0336662016-05-06 15:40:21 +01005439 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005440 dev_priv->num_fence_regs =
5441 I915_READ(vgtif_reg(avail_rs.fence_num));
5442
5443 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005444 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5445 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5446
5447 fence->i915 = dev_priv;
5448 fence->id = i;
5449 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5450 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005451 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005452
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005453 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005454}
5455
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005456static void i915_gem_init__mm(struct drm_i915_private *i915)
5457{
5458 spin_lock_init(&i915->mm.object_stat_lock);
5459 spin_lock_init(&i915->mm.obj_lock);
5460 spin_lock_init(&i915->mm.free_lock);
5461
5462 init_llist_head(&i915->mm.free_list);
5463
5464 INIT_LIST_HEAD(&i915->mm.unbound_list);
5465 INIT_LIST_HEAD(&i915->mm.bound_list);
5466 INIT_LIST_HEAD(&i915->mm.fence_list);
5467 INIT_LIST_HEAD(&i915->mm.userfault_list);
5468
5469 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5470}
5471
Chris Wilson73cb9702016-10-28 13:58:46 +01005472int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005473i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005474{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005475 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005476
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005477 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5478 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005479 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005480
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005481 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5482 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005483 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005484
Chris Wilsond1b48c12017-08-16 09:52:08 +01005485 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5486 if (!dev_priv->luts)
5487 goto err_vmas;
5488
Chris Wilsone61e0f52018-02-21 09:56:36 +00005489 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005490 SLAB_HWCACHE_ALIGN |
5491 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005492 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005493 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005494 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005495
Chris Wilson52e54202016-11-14 20:41:02 +00005496 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5497 SLAB_HWCACHE_ALIGN |
5498 SLAB_RECLAIM_ACCOUNT);
5499 if (!dev_priv->dependencies)
5500 goto err_requests;
5501
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005502 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5503 if (!dev_priv->priorities)
5504 goto err_dependencies;
5505
Chris Wilson73cb9702016-10-28 13:58:46 +01005506 mutex_lock(&dev_priv->drm.struct_mutex);
5507 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005508 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005509 mutex_unlock(&dev_priv->drm.struct_mutex);
5510 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005511 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005512
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005513 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005514
Chris Wilson67d97da2016-07-04 08:08:31 +01005515 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005516 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005517 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005518 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005519 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005520 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005521
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005522 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5523
Chris Wilsonb5add952016-08-04 16:32:36 +01005524 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005525
Matthew Auld465c4032017-10-06 23:18:14 +01005526 err = i915_gemfs_init(dev_priv);
5527 if (err)
5528 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5529
Chris Wilson73cb9702016-10-28 13:58:46 +01005530 return 0;
5531
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005532err_priorities:
5533 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005534err_dependencies:
5535 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005536err_requests:
5537 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005538err_luts:
5539 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005540err_vmas:
5541 kmem_cache_destroy(dev_priv->vmas);
5542err_objects:
5543 kmem_cache_destroy(dev_priv->objects);
5544err_out:
5545 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005546}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005547
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005548void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005549{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005550 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c70472018-02-19 22:06:31 +00005551 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5552 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005553 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005554
Matthew Auldea84aa72016-11-17 21:04:11 +00005555 mutex_lock(&dev_priv->drm.struct_mutex);
5556 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5557 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5558 mutex_unlock(&dev_priv->drm.struct_mutex);
5559
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005560 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005561 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005562 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005563 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005564 kmem_cache_destroy(dev_priv->vmas);
5565 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005566
5567 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5568 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005569
5570 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005571}
5572
Chris Wilson6a800ea2016-09-21 14:51:07 +01005573int i915_gem_freeze(struct drm_i915_private *dev_priv)
5574{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005575 /* Discard all purgeable objects, let userspace recover those as
5576 * required after resuming.
5577 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005578 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005579
Chris Wilson6a800ea2016-09-21 14:51:07 +01005580 return 0;
5581}
5582
Chris Wilson461fb992016-05-14 07:26:33 +01005583int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5584{
5585 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005586 struct list_head *phases[] = {
5587 &dev_priv->mm.unbound_list,
5588 &dev_priv->mm.bound_list,
5589 NULL
5590 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005591
5592 /* Called just before we write the hibernation image.
5593 *
5594 * We need to update the domain tracking to reflect that the CPU
5595 * will be accessing all the pages to create and restore from the
5596 * hibernation, and so upon restoration those pages will be in the
5597 * CPU domain.
5598 *
5599 * To make sure the hibernation image contains the latest state,
5600 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005601 *
5602 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005603 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005604 */
5605
Chris Wilson912d5722017-09-06 16:19:30 -07005606 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005607 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005608
Chris Wilsonf2123812017-10-16 12:40:37 +01005609 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005610 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005611 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005612 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005613 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005614 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005615
5616 return 0;
5617}
5618
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005619void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005620{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005621 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005622 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005623
5624 /* Clean up our request list when the client is going away, so that
5625 * later retire_requests won't dereference our soon-to-be-gone
5626 * file_priv.
5627 */
Chris Wilson1c255952010-09-26 11:03:27 +01005628 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005629 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005630 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005631 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005632}
5633
Chris Wilson829a0af2017-06-20 12:05:45 +01005634int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005635{
5636 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005637 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005638
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005639 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005640
5641 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5642 if (!file_priv)
5643 return -ENOMEM;
5644
5645 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005646 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005647 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005648
5649 spin_lock_init(&file_priv->mm.lock);
5650 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005651
Chris Wilsonc80ff162016-07-27 09:07:27 +01005652 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005653
Chris Wilson829a0af2017-06-20 12:05:45 +01005654 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005655 if (ret)
5656 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005657
Ben Widawskye422b882013-12-06 14:10:58 -08005658 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005659}
5660
Daniel Vetterb680c372014-09-19 18:27:27 +02005661/**
5662 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005663 * @old: current GEM buffer for the frontbuffer slots
5664 * @new: new GEM buffer for the frontbuffer slots
5665 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005666 *
5667 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5668 * from @old and setting them in @new. Both @old and @new can be NULL.
5669 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005670void i915_gem_track_fb(struct drm_i915_gem_object *old,
5671 struct drm_i915_gem_object *new,
5672 unsigned frontbuffer_bits)
5673{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005674 /* Control of individual bits within the mask are guarded by
5675 * the owning plane->mutex, i.e. we can never see concurrent
5676 * manipulation of individual bits. But since the bitfield as a whole
5677 * is updated using RMW, we need to use atomics in order to update
5678 * the bits.
5679 */
5680 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5681 sizeof(atomic_t) * BITS_PER_BYTE);
5682
Daniel Vettera071fa02014-06-18 23:28:09 +02005683 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005684 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5685 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005686 }
5687
5688 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005689 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5690 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005691 }
5692}
5693
Dave Gordonea702992015-07-09 19:29:02 +01005694/* Allocate a new GEM object and fill it with the supplied data */
5695struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005696i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005697 const void *data, size_t size)
5698{
5699 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005700 struct file *file;
5701 size_t offset;
5702 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005703
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005704 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005705 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005706 return obj;
5707
Christian Königc0a51fd2018-02-16 13:43:38 +01005708 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005709
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005710 file = obj->base.filp;
5711 offset = 0;
5712 do {
5713 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5714 struct page *page;
5715 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005716
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005717 err = pagecache_write_begin(file, file->f_mapping,
5718 offset, len, 0,
5719 &page, &pgdata);
5720 if (err < 0)
5721 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005722
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005723 vaddr = kmap(page);
5724 memcpy(vaddr, data, len);
5725 kunmap(page);
5726
5727 err = pagecache_write_end(file, file->f_mapping,
5728 offset, len, len,
5729 page, pgdata);
5730 if (err < 0)
5731 goto fail;
5732
5733 size -= len;
5734 data += len;
5735 offset += len;
5736 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005737
5738 return obj;
5739
5740fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005741 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005742 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005743}
Chris Wilson96d77632016-10-28 13:58:33 +01005744
5745struct scatterlist *
5746i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5747 unsigned int n,
5748 unsigned int *offset)
5749{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005750 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005751 struct scatterlist *sg;
5752 unsigned int idx, count;
5753
5754 might_sleep();
5755 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005756 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005757
5758 /* As we iterate forward through the sg, we record each entry in a
5759 * radixtree for quick repeated (backwards) lookups. If we have seen
5760 * this index previously, we will have an entry for it.
5761 *
5762 * Initial lookup is O(N), but this is amortized to O(1) for
5763 * sequential page access (where each new request is consecutive
5764 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5765 * i.e. O(1) with a large constant!
5766 */
5767 if (n < READ_ONCE(iter->sg_idx))
5768 goto lookup;
5769
5770 mutex_lock(&iter->lock);
5771
5772 /* We prefer to reuse the last sg so that repeated lookup of this
5773 * (or the subsequent) sg are fast - comparing against the last
5774 * sg is faster than going through the radixtree.
5775 */
5776
5777 sg = iter->sg_pos;
5778 idx = iter->sg_idx;
5779 count = __sg_page_count(sg);
5780
5781 while (idx + count <= n) {
5782 unsigned long exception, i;
5783 int ret;
5784
5785 /* If we cannot allocate and insert this entry, or the
5786 * individual pages from this range, cancel updating the
5787 * sg_idx so that on this lookup we are forced to linearly
5788 * scan onwards, but on future lookups we will try the
5789 * insertion again (in which case we need to be careful of
5790 * the error return reporting that we have already inserted
5791 * this index).
5792 */
5793 ret = radix_tree_insert(&iter->radix, idx, sg);
5794 if (ret && ret != -EEXIST)
5795 goto scan;
5796
5797 exception =
5798 RADIX_TREE_EXCEPTIONAL_ENTRY |
5799 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5800 for (i = 1; i < count; i++) {
5801 ret = radix_tree_insert(&iter->radix, idx + i,
5802 (void *)exception);
5803 if (ret && ret != -EEXIST)
5804 goto scan;
5805 }
5806
5807 idx += count;
5808 sg = ____sg_next(sg);
5809 count = __sg_page_count(sg);
5810 }
5811
5812scan:
5813 iter->sg_pos = sg;
5814 iter->sg_idx = idx;
5815
5816 mutex_unlock(&iter->lock);
5817
5818 if (unlikely(n < idx)) /* insertion completed by another thread */
5819 goto lookup;
5820
5821 /* In case we failed to insert the entry into the radixtree, we need
5822 * to look beyond the current sg.
5823 */
5824 while (idx + count <= n) {
5825 idx += count;
5826 sg = ____sg_next(sg);
5827 count = __sg_page_count(sg);
5828 }
5829
5830 *offset = n - idx;
5831 return sg;
5832
5833lookup:
5834 rcu_read_lock();
5835
5836 sg = radix_tree_lookup(&iter->radix, n);
5837 GEM_BUG_ON(!sg);
5838
5839 /* If this index is in the middle of multi-page sg entry,
5840 * the radixtree will contain an exceptional entry that points
5841 * to the start of that range. We will return the pointer to
5842 * the base page and the offset of this page within the
5843 * sg entry's range.
5844 */
5845 *offset = 0;
5846 if (unlikely(radix_tree_exception(sg))) {
5847 unsigned long base =
5848 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5849
5850 sg = radix_tree_lookup(&iter->radix, base);
5851 GEM_BUG_ON(!sg);
5852
5853 *offset = n - base;
5854 }
5855
5856 rcu_read_unlock();
5857
5858 return sg;
5859}
5860
5861struct page *
5862i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5863{
5864 struct scatterlist *sg;
5865 unsigned int offset;
5866
5867 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5868
5869 sg = i915_gem_object_get_sg(obj, n, &offset);
5870 return nth_page(sg_page(sg), offset);
5871}
5872
5873/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5874struct page *
5875i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5876 unsigned int n)
5877{
5878 struct page *page;
5879
5880 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005881 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005882 set_page_dirty(page);
5883
5884 return page;
5885}
5886
5887dma_addr_t
5888i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5889 unsigned long n)
5890{
5891 struct scatterlist *sg;
5892 unsigned int offset;
5893
5894 sg = i915_gem_object_get_sg(obj, n, &offset);
5895 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5896}
Chris Wilson935a2f72017-02-13 17:15:13 +00005897
Chris Wilson8eeb7902017-07-26 19:16:01 +01005898int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5899{
5900 struct sg_table *pages;
5901 int err;
5902
5903 if (align > obj->base.size)
5904 return -EINVAL;
5905
5906 if (obj->ops == &i915_gem_phys_ops)
5907 return 0;
5908
5909 if (obj->ops != &i915_gem_object_ops)
5910 return -EINVAL;
5911
5912 err = i915_gem_object_unbind(obj);
5913 if (err)
5914 return err;
5915
5916 mutex_lock(&obj->mm.lock);
5917
5918 if (obj->mm.madv != I915_MADV_WILLNEED) {
5919 err = -EFAULT;
5920 goto err_unlock;
5921 }
5922
5923 if (obj->mm.quirked) {
5924 err = -EFAULT;
5925 goto err_unlock;
5926 }
5927
5928 if (obj->mm.mapping) {
5929 err = -EBUSY;
5930 goto err_unlock;
5931 }
5932
Chris Wilsonf2123812017-10-16 12:40:37 +01005933 pages = fetch_and_zero(&obj->mm.pages);
5934 if (pages) {
5935 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5936
5937 __i915_gem_object_reset_page_iter(obj);
5938
5939 spin_lock(&i915->mm.obj_lock);
5940 list_del(&obj->mm.link);
5941 spin_unlock(&i915->mm.obj_lock);
5942 }
5943
Chris Wilson8eeb7902017-07-26 19:16:01 +01005944 obj->ops = &i915_gem_phys_ops;
5945
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005946 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005947 if (err)
5948 goto err_xfer;
5949
5950 /* Perma-pin (until release) the physical set of pages */
5951 __i915_gem_object_pin_pages(obj);
5952
5953 if (!IS_ERR_OR_NULL(pages))
5954 i915_gem_object_ops.put_pages(obj, pages);
5955 mutex_unlock(&obj->mm.lock);
5956 return 0;
5957
5958err_xfer:
5959 obj->ops = &i915_gem_object_ops;
5960 obj->mm.pages = pages;
5961err_unlock:
5962 mutex_unlock(&obj->mm.lock);
5963 return err;
5964}
5965
Chris Wilson935a2f72017-02-13 17:15:13 +00005966#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5967#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005968#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005969#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005970#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005971#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005972#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005973#endif