blob: ba43482d0a0ce123fd9889dce8c0c27c0c70242e [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{
243 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
244 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
245 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 &&
260 (obj->base.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{
356 struct drm_i915_gem_request *rq;
357
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);
369 if (i915_gem_request_completed(rq))
370 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 Wilsone9af4ea2018-01-18 13:16:09 +0000388 if (rps_client && !i915_gem_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
393 timeout = i915_wait_request(rq, flags, timeout);
394
395out:
396 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
397 i915_gem_request_retire_upto(rq);
398
Chris Wilsone95433c2016-10-28 13:58:27 +0100399 return timeout;
400}
401
402static long
403i915_gem_object_wait_reservation(struct reservation_object *resv,
404 unsigned int flags,
405 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100406 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100407{
Chris Wilsone54ca972017-02-17 15:13:04 +0000408 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100409 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000410 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100411
412 if (flags & I915_WAIT_ALL) {
413 struct dma_fence **shared;
414 unsigned int count, i;
415 int ret;
416
417 ret = reservation_object_get_fences_rcu(resv,
418 &excl, &count, &shared);
419 if (ret)
420 return ret;
421
422 for (i = 0; i < count; i++) {
423 timeout = i915_gem_object_wait_fence(shared[i],
424 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100425 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000426 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100427 break;
428
429 dma_fence_put(shared[i]);
430 }
431
432 for (; i < count; i++)
433 dma_fence_put(shared[i]);
434 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000435
436 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100437 } else {
438 excl = reservation_object_get_excl_rcu(resv);
439 }
440
Chris Wilsone54ca972017-02-17 15:13:04 +0000441 if (excl && timeout >= 0) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100442 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
443 rps_client);
Chris Wilsone54ca972017-02-17 15:13:04 +0000444 prune_fences = timeout >= 0;
445 }
Chris Wilsone95433c2016-10-28 13:58:27 +0100446
447 dma_fence_put(excl);
448
Chris Wilson03d1cac2017-03-08 13:26:28 +0000449 /* Oportunistically prune the fences iff we know they have *all* been
450 * signaled and that the reservation object has not been changed (i.e.
451 * no new fences have been added).
452 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000453 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000454 if (reservation_object_trylock(resv)) {
455 if (!__read_seqcount_retry(&resv->seq, seq))
456 reservation_object_add_excl_fence(resv, NULL);
457 reservation_object_unlock(resv);
458 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000459 }
460
Chris Wilsone95433c2016-10-28 13:58:27 +0100461 return timeout;
462}
463
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000464static void __fence_set_priority(struct dma_fence *fence, int prio)
465{
466 struct drm_i915_gem_request *rq;
467 struct intel_engine_cs *engine;
468
Chris Wilsonc218ee02018-01-06 10:56:18 +0000469 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000470 return;
471
472 rq = to_request(fence);
473 engine = rq->engine;
474 if (!engine->schedule)
475 return;
476
477 engine->schedule(rq, prio);
478}
479
480static void fence_set_priority(struct dma_fence *fence, int prio)
481{
482 /* Recurse once into a fence-array */
483 if (dma_fence_is_array(fence)) {
484 struct dma_fence_array *array = to_dma_fence_array(fence);
485 int i;
486
487 for (i = 0; i < array->num_fences; i++)
488 __fence_set_priority(array->fences[i], prio);
489 } else {
490 __fence_set_priority(fence, prio);
491 }
492}
493
494int
495i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
496 unsigned int flags,
497 int prio)
498{
499 struct dma_fence *excl;
500
501 if (flags & I915_WAIT_ALL) {
502 struct dma_fence **shared;
503 unsigned int count, i;
504 int ret;
505
506 ret = reservation_object_get_fences_rcu(obj->resv,
507 &excl, &count, &shared);
508 if (ret)
509 return ret;
510
511 for (i = 0; i < count; i++) {
512 fence_set_priority(shared[i], prio);
513 dma_fence_put(shared[i]);
514 }
515
516 kfree(shared);
517 } else {
518 excl = reservation_object_get_excl_rcu(obj->resv);
519 }
520
521 if (excl) {
522 fence_set_priority(excl, prio);
523 dma_fence_put(excl);
524 }
525 return 0;
526}
527
Chris Wilson00e60f22016-08-04 16:32:40 +0100528/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100529 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100530 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100531 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
532 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000533 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100534 */
535int
Chris Wilsone95433c2016-10-28 13:58:27 +0100536i915_gem_object_wait(struct drm_i915_gem_object *obj,
537 unsigned int flags,
538 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100539 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100540{
Chris Wilsone95433c2016-10-28 13:58:27 +0100541 might_sleep();
542#if IS_ENABLED(CONFIG_LOCKDEP)
543 GEM_BUG_ON(debug_locks &&
544 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
545 !!(flags & I915_WAIT_LOCKED));
546#endif
547 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100548
Chris Wilsond07f0e52016-10-28 13:58:44 +0100549 timeout = i915_gem_object_wait_reservation(obj->resv,
550 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100551 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100552 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100553}
554
555static struct intel_rps_client *to_rps_client(struct drm_file *file)
556{
557 struct drm_i915_file_private *fpriv = file->driver_priv;
558
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100559 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100560}
561
Chris Wilson00731152014-05-21 12:42:56 +0100562static int
563i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
564 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100565 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100566{
Chris Wilson00731152014-05-21 12:42:56 +0100567 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300568 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800569
570 /* We manually control the domain here and pretend that it
571 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
572 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700573 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000574 if (copy_from_user(vaddr, user_data, args->size))
575 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100576
Chris Wilson6a2c4232014-11-04 04:51:40 -0800577 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000578 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200579
Chris Wilsond59b21e2017-02-22 11:40:49 +0000580 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000581 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100582}
583
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000584void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000585{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100586 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000587}
588
589void i915_gem_object_free(struct drm_i915_gem_object *obj)
590{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100591 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100592 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000593}
594
Dave Airlieff72145b2011-02-07 12:16:14 +1000595static int
596i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000597 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000598 uint64_t size,
599 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700600{
Chris Wilson05394f32010-11-08 19:18:58 +0000601 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300602 int ret;
603 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700604
Dave Airlieff72145b2011-02-07 12:16:14 +1000605 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200606 if (size == 0)
607 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700608
609 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000610 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100611 if (IS_ERR(obj))
612 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700613
Chris Wilson05394f32010-11-08 19:18:58 +0000614 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100615 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100616 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200617 if (ret)
618 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100619
Dave Airlieff72145b2011-02-07 12:16:14 +1000620 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700621 return 0;
622}
623
Dave Airlieff72145b2011-02-07 12:16:14 +1000624int
625i915_gem_dumb_create(struct drm_file *file,
626 struct drm_device *dev,
627 struct drm_mode_create_dumb *args)
628{
629 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300630 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000631 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000632 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000633 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000634}
635
Chris Wilsone27ab732017-06-15 13:38:49 +0100636static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
637{
638 return !(obj->cache_level == I915_CACHE_NONE ||
639 obj->cache_level == I915_CACHE_WT);
640}
641
Dave Airlieff72145b2011-02-07 12:16:14 +1000642/**
643 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100644 * @dev: drm device pointer
645 * @data: ioctl data blob
646 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000647 */
648int
649i915_gem_create_ioctl(struct drm_device *dev, void *data,
650 struct drm_file *file)
651{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000652 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000653 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200654
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000655 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100656
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000657 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000658 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000659}
660
Chris Wilsonef749212017-04-12 12:01:10 +0100661static inline enum fb_op_origin
662fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
663{
664 return (domain == I915_GEM_DOMAIN_GTT ?
665 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
666}
667
Chris Wilson71253972017-12-06 12:49:14 +0000668void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100669{
Chris Wilson71253972017-12-06 12:49:14 +0000670 /*
671 * No actual flushing is required for the GTT write domain for reads
672 * from the GTT domain. Writes to it "immediately" go to main memory
673 * as far as we know, so there's no chipset flush. It also doesn't
674 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100675 *
676 * However, we do have to enforce the order so that all writes through
677 * the GTT land before any writes to the device, such as updates to
678 * the GATT itself.
679 *
680 * We also have to wait a bit for the writes to land from the GTT.
681 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
682 * timing. This issue has only been observed when switching quickly
683 * between GTT writes and CPU reads from inside the kernel on recent hw,
684 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson71253972017-12-06 12:49:14 +0000685 * system agents we cannot reproduce this behaviour, until Cannonlake
686 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100687 */
Chris Wilson71253972017-12-06 12:49:14 +0000688
Chris Wilsonef749212017-04-12 12:01:10 +0100689 wmb();
690
Chris Wilson71253972017-12-06 12:49:14 +0000691 intel_runtime_pm_get(dev_priv);
692 spin_lock_irq(&dev_priv->uncore.lock);
693
694 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
695
696 spin_unlock_irq(&dev_priv->uncore.lock);
697 intel_runtime_pm_put(dev_priv);
698}
699
700static void
701flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
702{
703 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
704 struct i915_vma *vma;
705
706 if (!(obj->base.write_domain & flush_domains))
707 return;
708
Chris Wilsonef749212017-04-12 12:01:10 +0100709 switch (obj->base.write_domain) {
710 case I915_GEM_DOMAIN_GTT:
Chris Wilson71253972017-12-06 12:49:14 +0000711 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100712
713 intel_fb_obj_flush(obj,
714 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson71253972017-12-06 12:49:14 +0000715
Chris Wilsone2189dd2017-12-07 21:14:07 +0000716 for_each_ggtt_vma(vma, obj) {
Chris Wilson71253972017-12-06 12:49:14 +0000717 if (vma->iomap)
718 continue;
719
720 i915_vma_unset_ggtt_write(vma);
721 }
Chris Wilsonef749212017-04-12 12:01:10 +0100722 break;
723
724 case I915_GEM_DOMAIN_CPU:
725 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
726 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100727
728 case I915_GEM_DOMAIN_RENDER:
729 if (gpu_write_needs_clflush(obj))
730 obj->cache_dirty = true;
731 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100732 }
733
734 obj->base.write_domain = 0;
735}
736
Daniel Vetter8c599672011-12-14 13:57:31 +0100737static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100738__copy_to_user_swizzled(char __user *cpu_vaddr,
739 const char *gpu_vaddr, int gpu_offset,
740 int length)
741{
742 int ret, cpu_offset = 0;
743
744 while (length > 0) {
745 int cacheline_end = ALIGN(gpu_offset + 1, 64);
746 int this_length = min(cacheline_end - gpu_offset, length);
747 int swizzled_gpu_offset = gpu_offset ^ 64;
748
749 ret = __copy_to_user(cpu_vaddr + cpu_offset,
750 gpu_vaddr + swizzled_gpu_offset,
751 this_length);
752 if (ret)
753 return ret + length;
754
755 cpu_offset += this_length;
756 gpu_offset += this_length;
757 length -= this_length;
758 }
759
760 return 0;
761}
762
763static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700764__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
765 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100766 int length)
767{
768 int ret, cpu_offset = 0;
769
770 while (length > 0) {
771 int cacheline_end = ALIGN(gpu_offset + 1, 64);
772 int this_length = min(cacheline_end - gpu_offset, length);
773 int swizzled_gpu_offset = gpu_offset ^ 64;
774
775 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
776 cpu_vaddr + cpu_offset,
777 this_length);
778 if (ret)
779 return ret + length;
780
781 cpu_offset += this_length;
782 gpu_offset += this_length;
783 length -= this_length;
784 }
785
786 return 0;
787}
788
Brad Volkin4c914c02014-02-18 10:15:45 -0800789/*
790 * Pins the specified object's pages and synchronizes the object with
791 * GPU accesses. Sets needs_clflush to non-zero if the caller should
792 * flush the object from the CPU cache.
793 */
794int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100795 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800796{
797 int ret;
798
Chris Wilsone95433c2016-10-28 13:58:27 +0100799 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800800
Chris Wilsone95433c2016-10-28 13:58:27 +0100801 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100802 if (!i915_gem_object_has_struct_page(obj))
803 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800804
Chris Wilsone95433c2016-10-28 13:58:27 +0100805 ret = i915_gem_object_wait(obj,
806 I915_WAIT_INTERRUPTIBLE |
807 I915_WAIT_LOCKED,
808 MAX_SCHEDULE_TIMEOUT,
809 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100810 if (ret)
811 return ret;
812
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100813 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100814 if (ret)
815 return ret;
816
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100817 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
818 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000819 ret = i915_gem_object_set_to_cpu_domain(obj, false);
820 if (ret)
821 goto err_unpin;
822 else
823 goto out;
824 }
825
Chris Wilsonef749212017-04-12 12:01:10 +0100826 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100827
Chris Wilson43394c72016-08-18 17:16:47 +0100828 /* If we're not in the cpu read domain, set ourself into the gtt
829 * read domain and manually flush cachelines (if required). This
830 * optimizes for the case when the gpu will dirty the data
831 * anyway again before the next pread happens.
832 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100833 if (!obj->cache_dirty &&
834 !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000835 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800836
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000837out:
Chris Wilson97649512016-08-18 17:16:50 +0100838 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100839 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100840
841err_unpin:
842 i915_gem_object_unpin_pages(obj);
843 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100844}
845
846int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
847 unsigned int *needs_clflush)
848{
849 int ret;
850
Chris Wilsone95433c2016-10-28 13:58:27 +0100851 lockdep_assert_held(&obj->base.dev->struct_mutex);
852
Chris Wilson43394c72016-08-18 17:16:47 +0100853 *needs_clflush = 0;
854 if (!i915_gem_object_has_struct_page(obj))
855 return -ENODEV;
856
Chris Wilsone95433c2016-10-28 13:58:27 +0100857 ret = i915_gem_object_wait(obj,
858 I915_WAIT_INTERRUPTIBLE |
859 I915_WAIT_LOCKED |
860 I915_WAIT_ALL,
861 MAX_SCHEDULE_TIMEOUT,
862 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100863 if (ret)
864 return ret;
865
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100866 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100867 if (ret)
868 return ret;
869
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100870 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
871 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000872 ret = i915_gem_object_set_to_cpu_domain(obj, true);
873 if (ret)
874 goto err_unpin;
875 else
876 goto out;
877 }
878
Chris Wilsonef749212017-04-12 12:01:10 +0100879 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100880
Chris Wilson43394c72016-08-18 17:16:47 +0100881 /* If we're not in the cpu write domain, set ourself into the
882 * gtt write domain and manually flush cachelines (as required).
883 * This optimizes for the case when the gpu will use the data
884 * right away and we therefore have to clflush anyway.
885 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100886 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000887 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100888
Chris Wilsone27ab732017-06-15 13:38:49 +0100889 /*
890 * Same trick applies to invalidate partially written
891 * cachelines read before writing.
892 */
893 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
894 *needs_clflush |= CLFLUSH_BEFORE;
895 }
Chris Wilson43394c72016-08-18 17:16:47 +0100896
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000897out:
Chris Wilson43394c72016-08-18 17:16:47 +0100898 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100899 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100900 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100901 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100902
903err_unpin:
904 i915_gem_object_unpin_pages(obj);
905 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800906}
907
Daniel Vetter23c18c72012-03-25 19:47:42 +0200908static void
909shmem_clflush_swizzled_range(char *addr, unsigned long length,
910 bool swizzled)
911{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200912 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200913 unsigned long start = (unsigned long) addr;
914 unsigned long end = (unsigned long) addr + length;
915
916 /* For swizzling simply ensure that we always flush both
917 * channels. Lame, but simple and it works. Swizzled
918 * pwrite/pread is far from a hotpath - current userspace
919 * doesn't use it at all. */
920 start = round_down(start, 128);
921 end = round_up(end, 128);
922
923 drm_clflush_virt_range((void *)start, end - start);
924 } else {
925 drm_clflush_virt_range(addr, length);
926 }
927
928}
929
Daniel Vetterd174bd62012-03-25 19:47:40 +0200930/* Only difference to the fast-path function is that this can handle bit17
931 * and uses non-atomic copy and kmap functions. */
932static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100933shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200934 char __user *user_data,
935 bool page_do_bit17_swizzling, bool needs_clflush)
936{
937 char *vaddr;
938 int ret;
939
940 vaddr = kmap(page);
941 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100942 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200943 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200944
945 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100946 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200947 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100948 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200949 kunmap(page);
950
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100951 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200952}
953
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100954static int
955shmem_pread(struct page *page, int offset, int length, char __user *user_data,
956 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530957{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100958 int ret;
959
960 ret = -ENODEV;
961 if (!page_do_bit17_swizzling) {
962 char *vaddr = kmap_atomic(page);
963
964 if (needs_clflush)
965 drm_clflush_virt_range(vaddr + offset, length);
966 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
967 kunmap_atomic(vaddr);
968 }
969 if (ret == 0)
970 return 0;
971
972 return shmem_pread_slow(page, offset, length, user_data,
973 page_do_bit17_swizzling, needs_clflush);
974}
975
976static int
977i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
978 struct drm_i915_gem_pread *args)
979{
980 char __user *user_data;
981 u64 remain;
982 unsigned int obj_do_bit17_swizzling;
983 unsigned int needs_clflush;
984 unsigned int idx, offset;
985 int ret;
986
987 obj_do_bit17_swizzling = 0;
988 if (i915_gem_object_needs_bit17_swizzle(obj))
989 obj_do_bit17_swizzling = BIT(17);
990
991 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
992 if (ret)
993 return ret;
994
995 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
996 mutex_unlock(&obj->base.dev->struct_mutex);
997 if (ret)
998 return ret;
999
1000 remain = args->size;
1001 user_data = u64_to_user_ptr(args->data_ptr);
1002 offset = offset_in_page(args->offset);
1003 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1004 struct page *page = i915_gem_object_get_page(obj, idx);
1005 int length;
1006
1007 length = remain;
1008 if (offset + length > PAGE_SIZE)
1009 length = PAGE_SIZE - offset;
1010
1011 ret = shmem_pread(page, offset, length, user_data,
1012 page_to_phys(page) & obj_do_bit17_swizzling,
1013 needs_clflush);
1014 if (ret)
1015 break;
1016
1017 remain -= length;
1018 user_data += length;
1019 offset = 0;
1020 }
1021
1022 i915_gem_obj_finish_shmem_access(obj);
1023 return ret;
1024}
1025
1026static inline bool
1027gtt_user_read(struct io_mapping *mapping,
1028 loff_t base, int offset,
1029 char __user *user_data, int length)
1030{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001031 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001032 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301033
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301034 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001035 vaddr = io_mapping_map_atomic_wc(mapping, base);
1036 unwritten = __copy_to_user_inatomic(user_data,
1037 (void __force *)vaddr + offset,
1038 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001039 io_mapping_unmap_atomic(vaddr);
1040 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001041 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1042 unwritten = copy_to_user(user_data,
1043 (void __force *)vaddr + offset,
1044 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001045 io_mapping_unmap(vaddr);
1046 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301047 return unwritten;
1048}
1049
1050static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001051i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1052 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301053{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001054 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1055 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301056 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001057 struct i915_vma *vma;
1058 void __user *user_data;
1059 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301060 int ret;
1061
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001062 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1063 if (ret)
1064 return ret;
1065
1066 intel_runtime_pm_get(i915);
1067 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001068 PIN_MAPPABLE |
1069 PIN_NONFAULT |
1070 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001071 if (!IS_ERR(vma)) {
1072 node.start = i915_ggtt_offset(vma);
1073 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001074 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001075 if (ret) {
1076 i915_vma_unpin(vma);
1077 vma = ERR_PTR(ret);
1078 }
1079 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001080 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001081 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301082 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001083 goto out_unlock;
1084 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301085 }
1086
1087 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1088 if (ret)
1089 goto out_unpin;
1090
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001091 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301092
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001093 user_data = u64_to_user_ptr(args->data_ptr);
1094 remain = args->size;
1095 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301096
1097 while (remain > 0) {
1098 /* Operation in this page
1099 *
1100 * page_base = page offset within aperture
1101 * page_offset = offset within page
1102 * page_length = bytes to copy for this page
1103 */
1104 u32 page_base = node.start;
1105 unsigned page_offset = offset_in_page(offset);
1106 unsigned page_length = PAGE_SIZE - page_offset;
1107 page_length = remain < page_length ? remain : page_length;
1108 if (node.allocated) {
1109 wmb();
1110 ggtt->base.insert_page(&ggtt->base,
1111 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001112 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301113 wmb();
1114 } else {
1115 page_base += offset & PAGE_MASK;
1116 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001117
Matthew Auld73ebd502017-12-11 15:18:20 +00001118 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001119 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301120 ret = -EFAULT;
1121 break;
1122 }
1123
1124 remain -= page_length;
1125 user_data += page_length;
1126 offset += page_length;
1127 }
1128
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001129 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301130out_unpin:
1131 if (node.allocated) {
1132 wmb();
1133 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001134 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301135 remove_mappable_node(&node);
1136 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001137 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301138 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001139out_unlock:
1140 intel_runtime_pm_put(i915);
1141 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001142
Eric Anholteb014592009-03-10 11:44:52 -07001143 return ret;
1144}
1145
Eric Anholt673a3942008-07-30 12:06:12 -07001146/**
1147 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001148 * @dev: drm device pointer
1149 * @data: ioctl data blob
1150 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001151 *
1152 * On error, the contents of *data are undefined.
1153 */
1154int
1155i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001156 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001157{
1158 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001159 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001160 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001161
Chris Wilson51311d02010-11-17 09:10:42 +00001162 if (args->size == 0)
1163 return 0;
1164
1165 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001166 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001167 args->size))
1168 return -EFAULT;
1169
Chris Wilson03ac0642016-07-20 13:31:51 +01001170 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001171 if (!obj)
1172 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001173
Chris Wilson7dcd2492010-09-26 20:21:44 +01001174 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001175 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001176 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001177 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001178 }
1179
Chris Wilsondb53a302011-02-03 11:57:46 +00001180 trace_i915_gem_object_pread(obj, args->offset, args->size);
1181
Chris Wilsone95433c2016-10-28 13:58:27 +01001182 ret = i915_gem_object_wait(obj,
1183 I915_WAIT_INTERRUPTIBLE,
1184 MAX_SCHEDULE_TIMEOUT,
1185 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001186 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001187 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001188
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001189 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001190 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001191 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001192
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001193 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001194 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001195 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301196
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 i915_gem_object_unpin_pages(obj);
1198out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001199 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001200 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001201}
1202
Keith Packard0839ccb2008-10-30 19:38:48 -07001203/* This is the fast write path which cannot handle
1204 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001205 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001206
Chris Wilsonfe115622016-10-28 13:58:40 +01001207static inline bool
1208ggtt_write(struct io_mapping *mapping,
1209 loff_t base, int offset,
1210 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001211{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001212 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001213 unsigned long unwritten;
1214
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001215 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001216 vaddr = io_mapping_map_atomic_wc(mapping, base);
1217 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001218 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001219 io_mapping_unmap_atomic(vaddr);
1220 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001221 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1222 unwritten = copy_from_user((void __force *)vaddr + offset,
1223 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001224 io_mapping_unmap(vaddr);
1225 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001226
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001227 return unwritten;
1228}
1229
Eric Anholt3de09aa2009-03-09 09:42:23 -07001230/**
1231 * This is the fast pwrite path, where we copy the data directly from the
1232 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001233 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001234 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001235 */
Eric Anholt673a3942008-07-30 12:06:12 -07001236static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001237i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1238 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001239{
Chris Wilsonfe115622016-10-28 13:58:40 +01001240 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301241 struct i915_ggtt *ggtt = &i915->ggtt;
1242 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001243 struct i915_vma *vma;
1244 u64 remain, offset;
1245 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301246 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301247
Chris Wilsonfe115622016-10-28 13:58:40 +01001248 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1249 if (ret)
1250 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001251
Chris Wilson8bd81812017-10-19 07:37:33 +01001252 if (i915_gem_object_has_struct_page(obj)) {
1253 /*
1254 * Avoid waking the device up if we can fallback, as
1255 * waking/resuming is very slow (worst-case 10-100 ms
1256 * depending on PCI sleeps and our own resume time).
1257 * This easily dwarfs any performance advantage from
1258 * using the cache bypass of indirect GGTT access.
1259 */
1260 if (!intel_runtime_pm_get_if_in_use(i915)) {
1261 ret = -EFAULT;
1262 goto out_unlock;
1263 }
1264 } else {
1265 /* No backing pages, no fallback, we must force GGTT access */
1266 intel_runtime_pm_get(i915);
1267 }
1268
Chris Wilson058d88c2016-08-15 10:49:06 +01001269 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001270 PIN_MAPPABLE |
1271 PIN_NONFAULT |
1272 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001273 if (!IS_ERR(vma)) {
1274 node.start = i915_ggtt_offset(vma);
1275 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001276 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001277 if (ret) {
1278 i915_vma_unpin(vma);
1279 vma = ERR_PTR(ret);
1280 }
1281 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001282 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001283 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301284 if (ret)
Chris Wilson8bd81812017-10-19 07:37:33 +01001285 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001286 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301287 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001288
1289 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1290 if (ret)
1291 goto out_unpin;
1292
Chris Wilsonfe115622016-10-28 13:58:40 +01001293 mutex_unlock(&i915->drm.struct_mutex);
1294
Chris Wilsonb19482d2016-08-18 17:16:43 +01001295 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001296
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301297 user_data = u64_to_user_ptr(args->data_ptr);
1298 offset = args->offset;
1299 remain = args->size;
1300 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001301 /* Operation in this page
1302 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001303 * page_base = page offset within aperture
1304 * page_offset = offset within page
1305 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001306 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301307 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001308 unsigned int page_offset = offset_in_page(offset);
1309 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301310 page_length = remain < page_length ? remain : page_length;
1311 if (node.allocated) {
1312 wmb(); /* flush the write before we modify the GGTT */
1313 ggtt->base.insert_page(&ggtt->base,
1314 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1315 node.start, I915_CACHE_NONE, 0);
1316 wmb(); /* flush modifications to the GGTT (insert_page) */
1317 } else {
1318 page_base += offset & PAGE_MASK;
1319 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001320 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001321 * source page isn't available. Return the error and we'll
1322 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301323 * If the object is non-shmem backed, we retry again with the
1324 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001325 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001326 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001327 user_data, page_length)) {
1328 ret = -EFAULT;
1329 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001330 }
Eric Anholt673a3942008-07-30 12:06:12 -07001331
Keith Packard0839ccb2008-10-30 19:38:48 -07001332 remain -= page_length;
1333 user_data += page_length;
1334 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001335 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001336 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001337
1338 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001339out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301340 if (node.allocated) {
1341 wmb();
1342 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001343 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301344 remove_mappable_node(&node);
1345 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001346 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301347 }
Chris Wilson8bd81812017-10-19 07:37:33 +01001348out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001349 intel_runtime_pm_put(i915);
Chris Wilson8bd81812017-10-19 07:37:33 +01001350out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001351 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001352 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001353}
1354
Eric Anholt673a3942008-07-30 12:06:12 -07001355static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001356shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001357 char __user *user_data,
1358 bool page_do_bit17_swizzling,
1359 bool needs_clflush_before,
1360 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001361{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001362 char *vaddr;
1363 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001364
Daniel Vetterd174bd62012-03-25 19:47:40 +02001365 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001366 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001367 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001368 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001369 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001370 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1371 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001372 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001373 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001374 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001375 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001376 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001377 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001378
Chris Wilson755d2212012-09-04 21:02:55 +01001379 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001380}
1381
Chris Wilsonfe115622016-10-28 13:58:40 +01001382/* Per-page copy function for the shmem pwrite fastpath.
1383 * Flushes invalid cachelines before writing to the target if
1384 * needs_clflush_before is set and flushes out any written cachelines after
1385 * writing if needs_clflush is set.
1386 */
Eric Anholt40123c12009-03-09 13:42:30 -07001387static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001388shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1389 bool page_do_bit17_swizzling,
1390 bool needs_clflush_before,
1391 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001392{
Chris Wilsonfe115622016-10-28 13:58:40 +01001393 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001394
Chris Wilsonfe115622016-10-28 13:58:40 +01001395 ret = -ENODEV;
1396 if (!page_do_bit17_swizzling) {
1397 char *vaddr = kmap_atomic(page);
1398
1399 if (needs_clflush_before)
1400 drm_clflush_virt_range(vaddr + offset, len);
1401 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1402 if (needs_clflush_after)
1403 drm_clflush_virt_range(vaddr + offset, len);
1404
1405 kunmap_atomic(vaddr);
1406 }
1407 if (ret == 0)
1408 return ret;
1409
1410 return shmem_pwrite_slow(page, offset, len, user_data,
1411 page_do_bit17_swizzling,
1412 needs_clflush_before,
1413 needs_clflush_after);
1414}
1415
1416static int
1417i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1418 const struct drm_i915_gem_pwrite *args)
1419{
1420 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1421 void __user *user_data;
1422 u64 remain;
1423 unsigned int obj_do_bit17_swizzling;
1424 unsigned int partial_cacheline_write;
1425 unsigned int needs_clflush;
1426 unsigned int offset, idx;
1427 int ret;
1428
1429 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001430 if (ret)
1431 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001432
Chris Wilsonfe115622016-10-28 13:58:40 +01001433 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1434 mutex_unlock(&i915->drm.struct_mutex);
1435 if (ret)
1436 return ret;
1437
1438 obj_do_bit17_swizzling = 0;
1439 if (i915_gem_object_needs_bit17_swizzle(obj))
1440 obj_do_bit17_swizzling = BIT(17);
1441
1442 /* If we don't overwrite a cacheline completely we need to be
1443 * careful to have up-to-date data by first clflushing. Don't
1444 * overcomplicate things and flush the entire patch.
1445 */
1446 partial_cacheline_write = 0;
1447 if (needs_clflush & CLFLUSH_BEFORE)
1448 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1449
Chris Wilson43394c72016-08-18 17:16:47 +01001450 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001451 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001452 offset = offset_in_page(args->offset);
1453 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1454 struct page *page = i915_gem_object_get_page(obj, idx);
1455 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001456
Chris Wilsonfe115622016-10-28 13:58:40 +01001457 length = remain;
1458 if (offset + length > PAGE_SIZE)
1459 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001460
Chris Wilsonfe115622016-10-28 13:58:40 +01001461 ret = shmem_pwrite(page, offset, length, user_data,
1462 page_to_phys(page) & obj_do_bit17_swizzling,
1463 (offset | length) & partial_cacheline_write,
1464 needs_clflush & CLFLUSH_AFTER);
1465 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001466 break;
1467
Chris Wilsonfe115622016-10-28 13:58:40 +01001468 remain -= length;
1469 user_data += length;
1470 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001471 }
1472
Chris Wilsond59b21e2017-02-22 11:40:49 +00001473 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001474 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001475 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001476}
1477
1478/**
1479 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001480 * @dev: drm device
1481 * @data: ioctl data blob
1482 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001483 *
1484 * On error, the contents of the buffer that were to be modified are undefined.
1485 */
1486int
1487i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001488 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001489{
1490 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001491 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001492 int ret;
1493
1494 if (args->size == 0)
1495 return 0;
1496
1497 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001498 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001499 args->size))
1500 return -EFAULT;
1501
Chris Wilson03ac0642016-07-20 13:31:51 +01001502 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001503 if (!obj)
1504 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001505
Chris Wilson7dcd2492010-09-26 20:21:44 +01001506 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001507 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001508 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001509 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001510 }
1511
Chris Wilsondb53a302011-02-03 11:57:46 +00001512 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1513
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001514 ret = -ENODEV;
1515 if (obj->ops->pwrite)
1516 ret = obj->ops->pwrite(obj, args);
1517 if (ret != -ENODEV)
1518 goto err;
1519
Chris Wilsone95433c2016-10-28 13:58:27 +01001520 ret = i915_gem_object_wait(obj,
1521 I915_WAIT_INTERRUPTIBLE |
1522 I915_WAIT_ALL,
1523 MAX_SCHEDULE_TIMEOUT,
1524 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001525 if (ret)
1526 goto err;
1527
Chris Wilsonfe115622016-10-28 13:58:40 +01001528 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001529 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001530 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001531
Daniel Vetter935aaa62012-03-25 19:47:35 +02001532 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001533 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1534 * it would end up going through the fenced access, and we'll get
1535 * different detiling behavior between reading and writing.
1536 * pread/pwrite currently are reading and writing from the CPU
1537 * perspective, requiring manual detiling by the client.
1538 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001539 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001540 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001541 /* Note that the gtt paths might fail with non-page-backed user
1542 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001543 * textures). Fallback to the shmem path in that case.
1544 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001545 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001546
Chris Wilsond1054ee2016-07-16 18:42:36 +01001547 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001548 if (obj->phys_handle)
1549 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301550 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001551 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001552 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001553
Chris Wilsonfe115622016-10-28 13:58:40 +01001554 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001555err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001556 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001557 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001558}
1559
Chris Wilson40e62d52016-10-28 13:58:41 +01001560static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1561{
1562 struct drm_i915_private *i915;
1563 struct list_head *list;
1564 struct i915_vma *vma;
1565
Chris Wilsonf2123812017-10-16 12:40:37 +01001566 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1567
Chris Wilsone2189dd2017-12-07 21:14:07 +00001568 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001569 if (i915_vma_is_active(vma))
1570 continue;
1571
1572 if (!drm_mm_node_allocated(&vma->node))
1573 continue;
1574
1575 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1576 }
1577
1578 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001579 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001580 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001581 list_move_tail(&obj->mm.link, list);
1582 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001583}
1584
Eric Anholt673a3942008-07-30 12:06:12 -07001585/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001586 * Called when user space prepares to use an object with the CPU, either
1587 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001588 * @dev: drm device
1589 * @data: ioctl data blob
1590 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001591 */
1592int
1593i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001594 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001595{
1596 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001597 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001598 uint32_t read_domains = args->read_domains;
1599 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001600 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001601
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001602 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001603 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001604 return -EINVAL;
1605
1606 /* Having something in the write domain implies it's in the read
1607 * domain, and only that read domain. Enforce that in the request.
1608 */
1609 if (write_domain != 0 && read_domains != write_domain)
1610 return -EINVAL;
1611
Chris Wilson03ac0642016-07-20 13:31:51 +01001612 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001613 if (!obj)
1614 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001615
Chris Wilson3236f572012-08-24 09:35:09 +01001616 /* Try to flush the object off the GPU without holding the lock.
1617 * We will repeat the flush holding the lock in the normal manner
1618 * to catch cases where we are gazumped.
1619 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001620 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001621 I915_WAIT_INTERRUPTIBLE |
1622 (write_domain ? I915_WAIT_ALL : 0),
1623 MAX_SCHEDULE_TIMEOUT,
1624 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001625 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001626 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001627
Tina Zhanga03f3952017-11-14 10:25:13 +00001628 /*
1629 * Proxy objects do not control access to the backing storage, ergo
1630 * they cannot be used as a means to manipulate the cache domain
1631 * tracking for that backing storage. The proxy object is always
1632 * considered to be outside of any cache domain.
1633 */
1634 if (i915_gem_object_is_proxy(obj)) {
1635 err = -ENXIO;
1636 goto out;
1637 }
1638
1639 /*
1640 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001641 * direct access in memory with previous cached writes through
1642 * shmemfs and that our cache domain tracking remains valid.
1643 * For example, if the obj->filp was moved to swap without us
1644 * being notified and releasing the pages, we would mistakenly
1645 * continue to assume that the obj remained out of the CPU cached
1646 * domain.
1647 */
1648 err = i915_gem_object_pin_pages(obj);
1649 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001650 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001651
1652 err = i915_mutex_lock_interruptible(dev);
1653 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001654 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001655
Chris Wilsone22d8e32017-04-12 12:01:11 +01001656 if (read_domains & I915_GEM_DOMAIN_WC)
1657 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1658 else if (read_domains & I915_GEM_DOMAIN_GTT)
1659 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301660 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001661 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001662
1663 /* And bump the LRU for this access */
1664 i915_gem_object_bump_inactive_ggtt(obj);
1665
1666 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001667
Daniel Vetter031b6982015-06-26 19:35:16 +02001668 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001669 intel_fb_obj_invalidate(obj,
1670 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001671
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001672out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001673 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001674out:
1675 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001676 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001677}
1678
1679/**
1680 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001681 * @dev: drm device
1682 * @data: ioctl data blob
1683 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001684 */
1685int
1686i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001687 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001688{
1689 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001690 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001691
Chris Wilson03ac0642016-07-20 13:31:51 +01001692 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001693 if (!obj)
1694 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001695
Tina Zhanga03f3952017-11-14 10:25:13 +00001696 /*
1697 * Proxy objects are barred from CPU access, so there is no
1698 * need to ban sw_finish as it is a nop.
1699 */
1700
Eric Anholt673a3942008-07-30 12:06:12 -07001701 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001702 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001703 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001704
1705 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001706}
1707
1708/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001709 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1710 * it is mapped to.
1711 * @dev: drm device
1712 * @data: ioctl data blob
1713 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001714 *
1715 * While the mapping holds a reference on the contents of the object, it doesn't
1716 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001717 *
1718 * IMPORTANT:
1719 *
1720 * DRM driver writers who look a this function as an example for how to do GEM
1721 * mmap support, please don't implement mmap support like here. The modern way
1722 * to implement DRM mmap support is with an mmap offset ioctl (like
1723 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1724 * That way debug tooling like valgrind will understand what's going on, hiding
1725 * the mmap call in a driver private ioctl will break that. The i915 driver only
1726 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001727 */
1728int
1729i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001730 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001731{
1732 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001733 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001734 unsigned long addr;
1735
Akash Goel1816f922015-01-02 16:29:30 +05301736 if (args->flags & ~(I915_MMAP_WC))
1737 return -EINVAL;
1738
Borislav Petkov568a58e2016-03-29 17:42:01 +02001739 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301740 return -ENODEV;
1741
Chris Wilson03ac0642016-07-20 13:31:51 +01001742 obj = i915_gem_object_lookup(file, args->handle);
1743 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001744 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001745
Daniel Vetter1286ff72012-05-10 15:25:09 +02001746 /* prime objects have no backing filp to GEM mmap
1747 * pages from.
1748 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001749 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001750 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001751 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001752 }
1753
Chris Wilson03ac0642016-07-20 13:31:51 +01001754 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001755 PROT_READ | PROT_WRITE, MAP_SHARED,
1756 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301757 if (args->flags & I915_MMAP_WC) {
1758 struct mm_struct *mm = current->mm;
1759 struct vm_area_struct *vma;
1760
Michal Hocko80a89a52016-05-23 16:26:11 -07001761 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001762 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001763 return -EINTR;
1764 }
Akash Goel1816f922015-01-02 16:29:30 +05301765 vma = find_vma(mm, addr);
1766 if (vma)
1767 vma->vm_page_prot =
1768 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1769 else
1770 addr = -ENOMEM;
1771 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001772
1773 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001774 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301775 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001776 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001777 if (IS_ERR((void *)addr))
1778 return addr;
1779
1780 args->addr_ptr = (uint64_t) addr;
1781
1782 return 0;
1783}
1784
Chris Wilson03af84f2016-08-18 17:17:01 +01001785static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1786{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001787 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001788}
1789
Jesse Barnesde151cf2008-11-12 10:03:55 -08001790/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001791 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1792 *
1793 * A history of the GTT mmap interface:
1794 *
1795 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1796 * aligned and suitable for fencing, and still fit into the available
1797 * mappable space left by the pinned display objects. A classic problem
1798 * we called the page-fault-of-doom where we would ping-pong between
1799 * two objects that could not fit inside the GTT and so the memcpy
1800 * would page one object in at the expense of the other between every
1801 * single byte.
1802 *
1803 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1804 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1805 * object is too large for the available space (or simply too large
1806 * for the mappable aperture!), a view is created instead and faulted
1807 * into userspace. (This view is aligned and sized appropriately for
1808 * fenced access.)
1809 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001810 * 2 - Recognise WC as a separate cache domain so that we can flush the
1811 * delayed writes via GTT before performing direct access via WC.
1812 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001813 * Restrictions:
1814 *
1815 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1816 * hangs on some architectures, corruption on others. An attempt to service
1817 * a GTT page fault from a snoopable object will generate a SIGBUS.
1818 *
1819 * * the object must be able to fit into RAM (physical memory, though no
1820 * limited to the mappable aperture).
1821 *
1822 *
1823 * Caveats:
1824 *
1825 * * a new GTT page fault will synchronize rendering from the GPU and flush
1826 * all data to system memory. Subsequent access will not be synchronized.
1827 *
1828 * * all mappings are revoked on runtime device suspend.
1829 *
1830 * * there are only 8, 16 or 32 fence registers to share between all users
1831 * (older machines require fence register for display and blitter access
1832 * as well). Contention of the fence registers will cause the previous users
1833 * to be unmapped and any new access will generate new page faults.
1834 *
1835 * * running out of memory while servicing a fault may generate a SIGBUS,
1836 * rather than the expected SIGSEGV.
1837 */
1838int i915_gem_mmap_gtt_version(void)
1839{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001840 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001841}
1842
Chris Wilson2d4281b2017-01-10 09:56:32 +00001843static inline struct i915_ggtt_view
1844compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001845 pgoff_t page_offset,
1846 unsigned int chunk)
1847{
1848 struct i915_ggtt_view view;
1849
1850 if (i915_gem_object_is_tiled(obj))
1851 chunk = roundup(chunk, tile_row_pages(obj));
1852
Chris Wilson2d4281b2017-01-10 09:56:32 +00001853 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001854 view.partial.offset = rounddown(page_offset, chunk);
1855 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001856 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001857 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001858
1859 /* If the partial covers the entire object, just create a normal VMA. */
1860 if (chunk >= obj->base.size >> PAGE_SHIFT)
1861 view.type = I915_GGTT_VIEW_NORMAL;
1862
1863 return view;
1864}
1865
Chris Wilson4cc69072016-08-25 19:05:19 +01001866/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001867 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001868 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001869 *
1870 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1871 * from userspace. The fault handler takes care of binding the object to
1872 * the GTT (if needed), allocating and programming a fence register (again,
1873 * only if needed based on whether the old reg is still valid or the object
1874 * is tiled) and inserting a new PTE into the faulting process.
1875 *
1876 * Note that the faulting process may involve evicting existing objects
1877 * from the GTT and/or fence registers to make room. So performance may
1878 * suffer if the GTT working set is large or there are few fence registers
1879 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001880 *
1881 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1882 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001883 */
Dave Jiang11bac802017-02-24 14:56:41 -08001884int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001885{
Chris Wilson03af84f2016-08-18 17:17:01 +01001886#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001887 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001888 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001889 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001890 struct drm_i915_private *dev_priv = to_i915(dev);
1891 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001892 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001893 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001894 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001895 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001896 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001897
Jesse Barnesde151cf2008-11-12 10:03:55 -08001898 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001899 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001900
Chris Wilsondb53a302011-02-03 11:57:46 +00001901 trace_i915_gem_object_fault(obj, page_offset, true, write);
1902
Chris Wilson6e4930f2014-02-07 18:37:06 -02001903 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001904 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001905 * repeat the flush holding the lock in the normal manner to catch cases
1906 * where we are gazumped.
1907 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001908 ret = i915_gem_object_wait(obj,
1909 I915_WAIT_INTERRUPTIBLE,
1910 MAX_SCHEDULE_TIMEOUT,
1911 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001912 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001913 goto err;
1914
Chris Wilson40e62d52016-10-28 13:58:41 +01001915 ret = i915_gem_object_pin_pages(obj);
1916 if (ret)
1917 goto err;
1918
Chris Wilsonb8f90962016-08-05 10:14:07 +01001919 intel_runtime_pm_get(dev_priv);
1920
1921 ret = i915_mutex_lock_interruptible(dev);
1922 if (ret)
1923 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001924
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001925 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001926 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001927 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001928 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001929 }
1930
Chris Wilson82118872016-08-18 17:17:05 +01001931 /* If the object is smaller than a couple of partial vma, it is
1932 * not worth only creating a single partial vma - we may as well
1933 * clear enough space for the full object.
1934 */
1935 flags = PIN_MAPPABLE;
1936 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1937 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1938
Chris Wilsona61007a2016-08-18 17:17:02 +01001939 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001940 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001941 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001942 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001943 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001944 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001945
Chris Wilson50349242016-08-18 17:17:04 +01001946 /* Userspace is now writing through an untracked VMA, abandon
1947 * all hope that the hardware is able to track future writes.
1948 */
1949 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1950
Chris Wilsona61007a2016-08-18 17:17:02 +01001951 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1952 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001953 if (IS_ERR(vma)) {
1954 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001955 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001956 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001957
Chris Wilsonc9839302012-11-20 10:45:17 +00001958 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1959 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001960 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001961
Chris Wilson3bd40732017-10-09 09:43:56 +01001962 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001963 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001964 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001965
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001966 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001967 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001968 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001969 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001970 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001971 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001972 if (ret)
1973 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001974
Chris Wilsona65adaf2017-10-09 09:43:57 +01001975 /* Mark as being mmapped into userspace for later revocation */
1976 assert_rpm_wakelock_held(dev_priv);
1977 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1978 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1979 GEM_BUG_ON(!obj->userfault_count);
1980
Chris Wilson71253972017-12-06 12:49:14 +00001981 i915_vma_set_ggtt_write(vma);
1982
Chris Wilsona65adaf2017-10-09 09:43:57 +01001983err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001984 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001985err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001986 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001987err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001988 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001989err_rpm:
1990 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001991 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001992err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001993 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001994 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001995 /*
1996 * We eat errors when the gpu is terminally wedged to avoid
1997 * userspace unduly crashing (gl has no provisions for mmaps to
1998 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1999 * and so needs to be reported.
2000 */
2001 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002002 ret = VM_FAULT_SIGBUS;
2003 break;
2004 }
Chris Wilson045e7692010-11-07 09:18:22 +00002005 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002006 /*
2007 * EAGAIN means the gpu is hung and we'll wait for the error
2008 * handler to reset everything when re-faulting in
2009 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002010 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002011 case 0:
2012 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002013 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002014 case -EBUSY:
2015 /*
2016 * EBUSY is ok: this just means that another thread
2017 * already did the job.
2018 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002019 ret = VM_FAULT_NOPAGE;
2020 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002021 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002022 ret = VM_FAULT_OOM;
2023 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002024 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002025 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002026 ret = VM_FAULT_SIGBUS;
2027 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002028 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002029 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002030 ret = VM_FAULT_SIGBUS;
2031 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002032 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002033 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002034}
2035
Chris Wilsona65adaf2017-10-09 09:43:57 +01002036static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2037{
2038 struct i915_vma *vma;
2039
2040 GEM_BUG_ON(!obj->userfault_count);
2041
2042 obj->userfault_count = 0;
2043 list_del(&obj->userfault_link);
2044 drm_vma_node_unmap(&obj->base.vma_node,
2045 obj->base.dev->anon_inode->i_mapping);
2046
Chris Wilsone2189dd2017-12-07 21:14:07 +00002047 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002048 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002049}
2050
Jesse Barnesde151cf2008-11-12 10:03:55 -08002051/**
Chris Wilson901782b2009-07-10 08:18:50 +01002052 * i915_gem_release_mmap - remove physical page mappings
2053 * @obj: obj in question
2054 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002055 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002056 * relinquish ownership of the pages back to the system.
2057 *
2058 * It is vital that we remove the page mapping if we have mapped a tiled
2059 * object through the GTT and then lose the fence register due to
2060 * resource pressure. Similarly if the object has been moved out of the
2061 * aperture, than pages mapped into userspace must be revoked. Removing the
2062 * mapping will then trigger a page fault on the next user access, allowing
2063 * fixup by i915_gem_fault().
2064 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002065void
Chris Wilson05394f32010-11-08 19:18:58 +00002066i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002067{
Chris Wilson275f0392016-10-24 13:42:14 +01002068 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002069
Chris Wilson349f2cc2016-04-13 17:35:12 +01002070 /* Serialisation between user GTT access and our code depends upon
2071 * revoking the CPU's PTE whilst the mutex is held. The next user
2072 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002073 *
2074 * Note that RPM complicates somewhat by adding an additional
2075 * requirement that operations to the GGTT be made holding the RPM
2076 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002077 */
Chris Wilson275f0392016-10-24 13:42:14 +01002078 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002079 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002080
Chris Wilsona65adaf2017-10-09 09:43:57 +01002081 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002082 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002083
Chris Wilsona65adaf2017-10-09 09:43:57 +01002084 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002085
2086 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2087 * memory transactions from userspace before we return. The TLB
2088 * flushing implied above by changing the PTE above *should* be
2089 * sufficient, an extra barrier here just provides us with a bit
2090 * of paranoid documentation about our requirement to serialise
2091 * memory writes before touching registers / GSM.
2092 */
2093 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002094
2095out:
2096 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002097}
2098
Chris Wilson7c108fd2016-10-24 13:42:18 +01002099void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002100{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002101 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002102 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002103
Chris Wilson3594a3e2016-10-24 13:42:16 +01002104 /*
2105 * Only called during RPM suspend. All users of the userfault_list
2106 * must be holding an RPM wakeref to ensure that this can not
2107 * run concurrently with themselves (and use the struct_mutex for
2108 * protection between themselves).
2109 */
2110
2111 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002112 &dev_priv->mm.userfault_list, userfault_link)
2113 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002114
2115 /* The fence will be lost when the device powers down. If any were
2116 * in use by hardware (i.e. they are pinned), we should not be powering
2117 * down! All other fences will be reacquired by the user upon waking.
2118 */
2119 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2120 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2121
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002122 /* Ideally we want to assert that the fence register is not
2123 * live at this point (i.e. that no piece of code will be
2124 * trying to write through fence + GTT, as that both violates
2125 * our tracking of activity and associated locking/barriers,
2126 * but also is illegal given that the hw is powered down).
2127 *
2128 * Previously we used reg->pin_count as a "liveness" indicator.
2129 * That is not sufficient, and we need a more fine-grained
2130 * tool if we want to have a sanity check here.
2131 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002132
2133 if (!reg->vma)
2134 continue;
2135
Chris Wilsona65adaf2017-10-09 09:43:57 +01002136 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002137 reg->dirty = true;
2138 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002139}
2140
Chris Wilsond8cb5082012-08-11 15:41:03 +01002141static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2142{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002143 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002144 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002145
Chris Wilsonf3f61842016-08-05 10:14:14 +01002146 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002147 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002148 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002149
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002150 /* Attempt to reap some mmap space from dead objects */
2151 do {
2152 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2153 if (err)
2154 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002155
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002156 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002157 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002158 if (!err)
2159 break;
2160
2161 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002162
Chris Wilsonf3f61842016-08-05 10:14:14 +01002163 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002164}
2165
2166static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2167{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002168 drm_gem_free_mmap_offset(&obj->base);
2169}
2170
Dave Airlieda6b51d2014-12-24 13:11:17 +10002171int
Dave Airlieff72145b2011-02-07 12:16:14 +10002172i915_gem_mmap_gtt(struct drm_file *file,
2173 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002174 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002175 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002176{
Chris Wilson05394f32010-11-08 19:18:58 +00002177 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002178 int ret;
2179
Chris Wilson03ac0642016-07-20 13:31:51 +01002180 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002181 if (!obj)
2182 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002183
Chris Wilsond8cb5082012-08-11 15:41:03 +01002184 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002185 if (ret == 0)
2186 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002187
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002188 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002189 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002190}
2191
Dave Airlieff72145b2011-02-07 12:16:14 +10002192/**
2193 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2194 * @dev: DRM device
2195 * @data: GTT mapping ioctl data
2196 * @file: GEM object info
2197 *
2198 * Simply returns the fake offset to userspace so it can mmap it.
2199 * The mmap call will end up in drm_gem_mmap(), which will set things
2200 * up so we can get faults in the handler above.
2201 *
2202 * The fault handler will take care of binding the object into the GTT
2203 * (since it may have been evicted to make room for something), allocating
2204 * a fence register, and mapping the appropriate aperture address into
2205 * userspace.
2206 */
2207int
2208i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2209 struct drm_file *file)
2210{
2211 struct drm_i915_gem_mmap_gtt *args = data;
2212
Dave Airlieda6b51d2014-12-24 13:11:17 +10002213 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002214}
2215
Daniel Vetter225067e2012-08-20 10:23:20 +02002216/* Immediately discard the backing storage */
2217static void
2218i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002219{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002220 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002221
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002222 if (obj->base.filp == NULL)
2223 return;
2224
Daniel Vetter225067e2012-08-20 10:23:20 +02002225 /* Our goal here is to return as much of the memory as
2226 * is possible back to the system as we are called from OOM.
2227 * To do this we must instruct the shmfs to drop all of its
2228 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002229 */
Chris Wilson55372522014-03-25 13:23:06 +00002230 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002231 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002232 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002233}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002234
Chris Wilson55372522014-03-25 13:23:06 +00002235/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002236void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002237{
Chris Wilson55372522014-03-25 13:23:06 +00002238 struct address_space *mapping;
2239
Chris Wilson1233e2d2016-10-28 13:58:37 +01002240 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002241 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002242
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002243 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002244 case I915_MADV_DONTNEED:
2245 i915_gem_object_truncate(obj);
2246 case __I915_MADV_PURGED:
2247 return;
2248 }
2249
2250 if (obj->base.filp == NULL)
2251 return;
2252
Al Viro93c76a32015-12-04 23:45:44 -05002253 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002254 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002255}
2256
Chris Wilson5cdf5882010-09-27 15:51:07 +01002257static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002258i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2259 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002260{
Dave Gordon85d12252016-05-20 11:54:06 +01002261 struct sgt_iter sgt_iter;
2262 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002263
Chris Wilsone5facdf2016-12-23 14:57:57 +00002264 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002265
Chris Wilson03ac84f2016-10-28 13:58:36 +01002266 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002267
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002268 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002269 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002270
Chris Wilson03ac84f2016-10-28 13:58:36 +01002271 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002272 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002273 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002274
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002275 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002276 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002277
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002278 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002279 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002280 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002281
Chris Wilson03ac84f2016-10-28 13:58:36 +01002282 sg_free_table(pages);
2283 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002284}
2285
Chris Wilson96d77632016-10-28 13:58:33 +01002286static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2287{
2288 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002289 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002290
Chris Wilsonbea6e982017-10-26 14:00:31 +01002291 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002292 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2293 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002294 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002295}
2296
Chris Wilson548625e2016-11-01 12:11:34 +00002297void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2298 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002299{
Chris Wilsonf2123812017-10-16 12:40:37 +01002300 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002301 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002302
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002303 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002304 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002305
Chris Wilson15717de2016-08-04 07:52:26 +01002306 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002307 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002308 return;
2309
2310 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002311 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002312 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2313 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002314
Chris Wilsona2165e32012-12-03 11:49:00 +00002315 /* ->put_pages might need to allocate memory for the bit17 swizzle
2316 * array, hence protect them from being reaped by removing them from gtt
2317 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002318 pages = fetch_and_zero(&obj->mm.pages);
2319 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002320
Chris Wilsonf2123812017-10-16 12:40:37 +01002321 spin_lock(&i915->mm.obj_lock);
2322 list_del(&obj->mm.link);
2323 spin_unlock(&i915->mm.obj_lock);
2324
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002325 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002326 void *ptr;
2327
Chris Wilson0ce81782017-05-17 13:09:59 +01002328 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002329 if (is_vmalloc_addr(ptr))
2330 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002331 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002332 kunmap(kmap_to_page(ptr));
2333
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002334 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002335 }
2336
Chris Wilson96d77632016-10-28 13:58:33 +01002337 __i915_gem_object_reset_page_iter(obj);
2338
Chris Wilson4e5462e2017-03-07 13:20:31 +00002339 if (!IS_ERR(pages))
2340 obj->ops->put_pages(obj, pages);
2341
Matthew Aulda5c081662017-10-06 23:18:18 +01002342 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2343
Chris Wilson1233e2d2016-10-28 13:58:37 +01002344unlock:
2345 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002346}
2347
Chris Wilson935a2f72017-02-13 17:15:13 +00002348static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002349{
2350 struct sg_table new_st;
2351 struct scatterlist *sg, *new_sg;
2352 unsigned int i;
2353
2354 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002355 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002356
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002357 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002358 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002359
2360 new_sg = new_st.sgl;
2361 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2362 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2363 /* called before being DMA mapped, no need to copy sg->dma_* */
2364 new_sg = sg_next(new_sg);
2365 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002366 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002367
2368 sg_free_table(orig_st);
2369
2370 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002371 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002372}
2373
Matthew Auldb91b09e2017-10-06 23:18:17 +01002374static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002375{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002376 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002377 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2378 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002379 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002380 struct sg_table *st;
2381 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002382 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002383 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002384 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002385 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002386 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002387 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002388 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002389
Chris Wilson6c085a72012-08-20 11:40:46 +02002390 /* Assert that the object is not currently in any GPU domain. As it
2391 * wasn't in the GTT, there shouldn't be any way it could have been in
2392 * a GPU cache
2393 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002394 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2395 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002396
Chris Wilson9da3da62012-06-01 15:20:22 +01002397 st = kmalloc(sizeof(*st), GFP_KERNEL);
2398 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002399 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002400
Chris Wilsond766ef52016-12-19 12:43:45 +00002401rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002402 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002403 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002404 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002405 }
2406
2407 /* Get the list of pages out of our struct file. They'll be pinned
2408 * at this point until we release them.
2409 *
2410 * Fail silently without starting the shrinker
2411 */
Al Viro93c76a32015-12-04 23:45:44 -05002412 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002413 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002414 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2415
Imre Deak90797e62013-02-18 19:28:03 +02002416 sg = st->sgl;
2417 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002418 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002419 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002420 const unsigned int shrink[] = {
2421 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2422 0,
2423 }, *s = shrink;
2424 gfp_t gfp = noreclaim;
2425
2426 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002427 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002428 if (likely(!IS_ERR(page)))
2429 break;
2430
2431 if (!*s) {
2432 ret = PTR_ERR(page);
2433 goto err_sg;
2434 }
2435
Chris Wilson912d5722017-09-06 16:19:30 -07002436 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002437 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002438
Chris Wilson6c085a72012-08-20 11:40:46 +02002439 /* We've tried hard to allocate the memory by reaping
2440 * our own buffer, now let the real VM do its job and
2441 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002442 *
2443 * However, since graphics tend to be disposable,
2444 * defer the oom here by reporting the ENOMEM back
2445 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002446 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002447 if (!*s) {
2448 /* reclaim and warn, but no oom */
2449 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002450
2451 /* Our bo are always dirty and so we require
2452 * kswapd to reclaim our pages (direct reclaim
2453 * does not effectively begin pageout of our
2454 * buffers on its own). However, direct reclaim
2455 * only waits for kswapd when under allocation
2456 * congestion. So as a result __GFP_RECLAIM is
2457 * unreliable and fails to actually reclaim our
2458 * dirty pages -- unless you try over and over
2459 * again with !__GFP_NORETRY. However, we still
2460 * want to fail this allocation rather than
2461 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002462 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002463 */
Michal Hockodbb32952017-07-12 14:36:55 -07002464 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002465 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002466 } while (1);
2467
Chris Wilson871dfbd2016-10-11 09:20:21 +01002468 if (!i ||
2469 sg->length >= max_segment ||
2470 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002471 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002472 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002473 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002474 }
Imre Deak90797e62013-02-18 19:28:03 +02002475 st->nents++;
2476 sg_set_page(sg, page, PAGE_SIZE, 0);
2477 } else {
2478 sg->length += PAGE_SIZE;
2479 }
2480 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002481
2482 /* Check that the i965g/gm workaround works. */
2483 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002484 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002485 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002486 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002487 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002488 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002489
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002490 /* Trim unused sg entries to avoid wasting memory. */
2491 i915_sg_trim(st);
2492
Chris Wilson03ac84f2016-10-28 13:58:36 +01002493 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002494 if (ret) {
2495 /* DMA remapping failed? One possible cause is that
2496 * it could not reserve enough large entries, asking
2497 * for PAGE_SIZE chunks instead may be helpful.
2498 */
2499 if (max_segment > PAGE_SIZE) {
2500 for_each_sgt_page(page, sgt_iter, st)
2501 put_page(page);
2502 sg_free_table(st);
2503
2504 max_segment = PAGE_SIZE;
2505 goto rebuild_st;
2506 } else {
2507 dev_warn(&dev_priv->drm.pdev->dev,
2508 "Failed to DMA remap %lu pages\n",
2509 page_count);
2510 goto err_pages;
2511 }
2512 }
Imre Deake2273302015-07-09 12:59:05 +03002513
Eric Anholt673a3942008-07-30 12:06:12 -07002514 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002515 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002516
Matthew Auld84e89782017-10-09 12:00:24 +01002517 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002518
2519 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002520
Chris Wilsonb17993b2016-11-14 11:29:30 +00002521err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002522 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002523err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002524 for_each_sgt_page(page, sgt_iter, st)
2525 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002526 sg_free_table(st);
2527 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002528
2529 /* shmemfs first checks if there is enough memory to allocate the page
2530 * and reports ENOSPC should there be insufficient, along with the usual
2531 * ENOMEM for a genuine allocation failure.
2532 *
2533 * We use ENOSPC in our driver to mean that we have run out of aperture
2534 * space and so want to translate the error from shmemfs back to our
2535 * usual understanding of ENOMEM.
2536 */
Imre Deake2273302015-07-09 12:59:05 +03002537 if (ret == -ENOSPC)
2538 ret = -ENOMEM;
2539
Matthew Auldb91b09e2017-10-06 23:18:17 +01002540 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002541}
2542
2543void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002544 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002545 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002546{
Matthew Aulda5c081662017-10-06 23:18:18 +01002547 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2548 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2549 int i;
2550
Chris Wilson1233e2d2016-10-28 13:58:37 +01002551 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002552
2553 obj->mm.get_page.sg_pos = pages->sgl;
2554 obj->mm.get_page.sg_idx = 0;
2555
2556 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002557
2558 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002559 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002560 GEM_BUG_ON(obj->mm.quirked);
2561 __i915_gem_object_pin_pages(obj);
2562 obj->mm.quirked = true;
2563 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002564
Matthew Auld84e89782017-10-09 12:00:24 +01002565 GEM_BUG_ON(!sg_page_sizes);
2566 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002567
2568 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002569 * Calculate the supported page-sizes which fit into the given
2570 * sg_page_sizes. This will give us the page-sizes which we may be able
2571 * to use opportunistically when later inserting into the GTT. For
2572 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2573 * 64K or 4K pages, although in practice this will depend on a number of
2574 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002575 */
2576 obj->mm.page_sizes.sg = 0;
2577 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2578 if (obj->mm.page_sizes.phys & ~0u << i)
2579 obj->mm.page_sizes.sg |= BIT(i);
2580 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002581 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002582
2583 spin_lock(&i915->mm.obj_lock);
2584 list_add(&obj->mm.link, &i915->mm.unbound_list);
2585 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002586}
2587
2588static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2589{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002590 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002591
2592 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2593 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2594 return -EFAULT;
2595 }
2596
Matthew Auldb91b09e2017-10-06 23:18:17 +01002597 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002598 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002599
Matthew Auldb91b09e2017-10-06 23:18:17 +01002600 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002601}
2602
Chris Wilson37e680a2012-06-07 15:38:42 +01002603/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002604 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002605 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002606 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002607 * either as a result of memory pressure (reaping pages under the shrinker)
2608 * or as the object is itself released.
2609 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002610int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002611{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002612 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002613
Chris Wilson1233e2d2016-10-28 13:58:37 +01002614 err = mutex_lock_interruptible(&obj->mm.lock);
2615 if (err)
2616 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002617
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002618 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002619 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2620
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002621 err = ____i915_gem_object_get_pages(obj);
2622 if (err)
2623 goto unlock;
2624
2625 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002626 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002627 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002628
Chris Wilson1233e2d2016-10-28 13:58:37 +01002629unlock:
2630 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002631 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002632}
2633
Dave Gordondd6034c2016-05-20 11:54:04 +01002634/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002635static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2636 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002637{
2638 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002639 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002640 struct sgt_iter sgt_iter;
2641 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002642 struct page *stack_pages[32];
2643 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002644 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002645 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002646 void *addr;
2647
2648 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002649 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002650 return kmap(sg_page(sgt->sgl));
2651
Dave Gordonb338fa42016-05-20 11:54:05 +01002652 if (n_pages > ARRAY_SIZE(stack_pages)) {
2653 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002654 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002655 if (!pages)
2656 return NULL;
2657 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002658
Dave Gordon85d12252016-05-20 11:54:06 +01002659 for_each_sgt_page(page, sgt_iter, sgt)
2660 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002661
2662 /* Check that we have the expected number of pages */
2663 GEM_BUG_ON(i != n_pages);
2664
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002665 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002666 default:
2667 MISSING_CASE(type);
2668 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002669 case I915_MAP_WB:
2670 pgprot = PAGE_KERNEL;
2671 break;
2672 case I915_MAP_WC:
2673 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2674 break;
2675 }
2676 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002677
Dave Gordonb338fa42016-05-20 11:54:05 +01002678 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002679 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002680
2681 return addr;
2682}
2683
2684/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002685void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2686 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002687{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002688 enum i915_map_type has_type;
2689 bool pinned;
2690 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002691 int ret;
2692
Tina Zhanga03f3952017-11-14 10:25:13 +00002693 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2694 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002695
Chris Wilson1233e2d2016-10-28 13:58:37 +01002696 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002697 if (ret)
2698 return ERR_PTR(ret);
2699
Chris Wilsona575c672017-08-28 11:46:31 +01002700 pinned = !(type & I915_MAP_OVERRIDE);
2701 type &= ~I915_MAP_OVERRIDE;
2702
Chris Wilson1233e2d2016-10-28 13:58:37 +01002703 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002704 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002705 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2706
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002707 ret = ____i915_gem_object_get_pages(obj);
2708 if (ret)
2709 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002710
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002711 smp_mb__before_atomic();
2712 }
2713 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002714 pinned = false;
2715 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002716 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002717
Chris Wilson0ce81782017-05-17 13:09:59 +01002718 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002719 if (ptr && has_type != type) {
2720 if (pinned) {
2721 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002722 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002723 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002724
2725 if (is_vmalloc_addr(ptr))
2726 vunmap(ptr);
2727 else
2728 kunmap(kmap_to_page(ptr));
2729
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002730 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002731 }
2732
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002733 if (!ptr) {
2734 ptr = i915_gem_object_map(obj, type);
2735 if (!ptr) {
2736 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002737 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002738 }
2739
Chris Wilson0ce81782017-05-17 13:09:59 +01002740 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002741 }
2742
Chris Wilson1233e2d2016-10-28 13:58:37 +01002743out_unlock:
2744 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002745 return ptr;
2746
Chris Wilson1233e2d2016-10-28 13:58:37 +01002747err_unpin:
2748 atomic_dec(&obj->mm.pages_pin_count);
2749err_unlock:
2750 ptr = ERR_PTR(ret);
2751 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002752}
2753
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002754static int
2755i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2756 const struct drm_i915_gem_pwrite *arg)
2757{
2758 struct address_space *mapping = obj->base.filp->f_mapping;
2759 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2760 u64 remain, offset;
2761 unsigned int pg;
2762
2763 /* Before we instantiate/pin the backing store for our use, we
2764 * can prepopulate the shmemfs filp efficiently using a write into
2765 * the pagecache. We avoid the penalty of instantiating all the
2766 * pages, important if the user is just writing to a few and never
2767 * uses the object on the GPU, and using a direct write into shmemfs
2768 * allows it to avoid the cost of retrieving a page (either swapin
2769 * or clearing-before-use) before it is overwritten.
2770 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002771 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002772 return -ENODEV;
2773
Chris Wilsona6d65e42017-10-16 21:27:32 +01002774 if (obj->mm.madv != I915_MADV_WILLNEED)
2775 return -EFAULT;
2776
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002777 /* Before the pages are instantiated the object is treated as being
2778 * in the CPU domain. The pages will be clflushed as required before
2779 * use, and we can freely write into the pages directly. If userspace
2780 * races pwrite with any other operation; corruption will ensue -
2781 * that is userspace's prerogative!
2782 */
2783
2784 remain = arg->size;
2785 offset = arg->offset;
2786 pg = offset_in_page(offset);
2787
2788 do {
2789 unsigned int len, unwritten;
2790 struct page *page;
2791 void *data, *vaddr;
2792 int err;
2793
2794 len = PAGE_SIZE - pg;
2795 if (len > remain)
2796 len = remain;
2797
2798 err = pagecache_write_begin(obj->base.filp, mapping,
2799 offset, len, 0,
2800 &page, &data);
2801 if (err < 0)
2802 return err;
2803
2804 vaddr = kmap(page);
2805 unwritten = copy_from_user(vaddr + pg, user_data, len);
2806 kunmap(page);
2807
2808 err = pagecache_write_end(obj->base.filp, mapping,
2809 offset, len, len - unwritten,
2810 page, data);
2811 if (err < 0)
2812 return err;
2813
2814 if (unwritten)
2815 return -EFAULT;
2816
2817 remain -= len;
2818 user_data += len;
2819 offset += len;
2820 pg = 0;
2821 } while (remain);
2822
2823 return 0;
2824}
2825
Chris Wilson77b25a92017-07-21 13:32:30 +01002826static bool ban_context(const struct i915_gem_context *ctx,
2827 unsigned int score)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002828{
Chris Wilson60958682016-12-31 11:20:11 +00002829 return (i915_gem_context_is_bannable(ctx) &&
Chris Wilson77b25a92017-07-21 13:32:30 +01002830 score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002831}
2832
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002833static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002834{
Chris Wilson77b25a92017-07-21 13:32:30 +01002835 unsigned int score;
2836 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002837
Chris Wilson77b25a92017-07-21 13:32:30 +01002838 atomic_inc(&ctx->guilty_count);
2839
2840 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
2841 banned = ban_context(ctx, score);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002842 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Chris Wilson77b25a92017-07-21 13:32:30 +01002843 ctx->name, score, yesno(banned));
2844 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002845 return;
2846
Chris Wilson77b25a92017-07-21 13:32:30 +01002847 i915_gem_context_set_banned(ctx);
2848 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2849 atomic_inc(&ctx->file_priv->context_bans);
2850 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2851 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2852 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002853}
2854
2855static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2856{
Chris Wilson77b25a92017-07-21 13:32:30 +01002857 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002858}
2859
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002860struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002861i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002862{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002863 struct drm_i915_gem_request *request, *active = NULL;
2864 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002865
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002866 /* We are called by the error capture and reset at a random
2867 * point in time. In particular, note that neither is crucially
2868 * ordered with an interrupt. After a hang, the GPU is dead and we
2869 * assume that no more writes can happen (we waited long enough for
2870 * all writes that were in transaction to be flushed) - adding an
2871 * extra delay for a recent interrupt is pointless. Hence, we do
2872 * not need an engine->irq_seqno_barrier() before the seqno reads.
2873 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002874 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002875 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002876 if (__i915_gem_request_completed(request,
2877 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002878 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002879
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002880 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002881 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2882 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002883
Chris Wilson754c9fd2017-02-23 07:44:14 +00002884 active = request;
2885 break;
2886 }
2887 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2888
2889 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002890}
2891
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002892static bool engine_stalled(struct intel_engine_cs *engine)
2893{
2894 if (!engine->hangcheck.stalled)
2895 return false;
2896
2897 /* Check for possible seqno movement after hang declaration */
2898 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2899 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2900 return false;
2901 }
2902
2903 return true;
2904}
2905
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002906/*
2907 * Ensure irq handler finishes, and not run again.
2908 * Also return the active request so that we only search for it once.
2909 */
2910struct drm_i915_gem_request *
2911i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2912{
2913 struct drm_i915_gem_request *request = NULL;
2914
Chris Wilson1749d902017-10-09 12:02:59 +01002915 /*
2916 * During the reset sequence, we must prevent the engine from
2917 * entering RC6. As the context state is undefined until we restart
2918 * the engine, if it does enter RC6 during the reset, the state
2919 * written to the powercontext is undefined and so we may lose
2920 * GPU state upon resume, i.e. fail to restart after a reset.
2921 */
2922 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2923
2924 /*
2925 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002926 * state (by calling dma_fence_signal) as we are processing
2927 * the reset. The write from the GPU of the seqno is
2928 * asynchronous and the signaler thread may see a different
2929 * value to us and declare the request complete, even though
2930 * the reset routine have picked that request as the active
2931 * (incomplete) request. This conflict is not handled
2932 * gracefully!
2933 */
2934 kthread_park(engine->breadcrumbs.signaler);
2935
Chris Wilson1749d902017-10-09 12:02:59 +01002936 /*
2937 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002938 * completed the reset in i915_gem_reset_finish(). If a request
2939 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302940 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002941 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302942 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002943 * prevents the race.
2944 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302945 tasklet_kill(&engine->execlists.tasklet);
2946 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002947
Michał Winiarskic41937f2017-10-26 15:35:58 +02002948 /*
2949 * We're using worker to queue preemption requests from the tasklet in
2950 * GuC submission mode.
2951 * Even though tasklet was disabled, we may still have a worker queued.
2952 * Let's make sure that all workers scheduled before disabling the
2953 * tasklet are completed before continuing with the reset.
2954 */
2955 if (engine->i915->guc.preempt_wq)
2956 flush_workqueue(engine->i915->guc.preempt_wq);
2957
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002958 if (engine->irq_seqno_barrier)
2959 engine->irq_seqno_barrier(engine);
2960
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002961 request = i915_gem_find_active_request(engine);
2962 if (request && request->fence.error == -EIO)
2963 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002964
2965 return request;
2966}
2967
Chris Wilson0e178ae2017-01-17 17:59:06 +02002968int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002969{
2970 struct intel_engine_cs *engine;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002971 struct drm_i915_gem_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002972 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002973 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002974
Chris Wilson0e178ae2017-01-17 17:59:06 +02002975 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002976 request = i915_gem_reset_prepare_engine(engine);
2977 if (IS_ERR(request)) {
2978 err = PTR_ERR(request);
2979 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002980 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002981
2982 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002983 }
2984
Chris Wilson4c965542017-01-17 17:59:01 +02002985 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002986
2987 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002988}
2989
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002990static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002991{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002992 void *vaddr = request->ring->vaddr;
2993 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002994
Chris Wilson821ed7d2016-09-09 14:11:53 +01002995 /* As this request likely depends on state from the lost
2996 * context, clear out all the user operations leaving the
2997 * breadcrumb at the end (so we get the fence notifications).
2998 */
2999 head = request->head;
3000 if (request->postfix < head) {
3001 memset(vaddr + head, 0, request->ring->size - head);
3002 head = 0;
3003 }
3004 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003005
3006 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003007}
3008
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003009static void engine_skip_context(struct drm_i915_gem_request *request)
3010{
3011 struct intel_engine_cs *engine = request->engine;
3012 struct i915_gem_context *hung_ctx = request->ctx;
3013 struct intel_timeline *timeline;
3014 unsigned long flags;
3015
3016 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3017
3018 spin_lock_irqsave(&engine->timeline->lock, flags);
3019 spin_lock(&timeline->lock);
3020
3021 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3022 if (request->ctx == hung_ctx)
3023 skip_request(request);
3024
3025 list_for_each_entry(request, &timeline->requests, link)
3026 skip_request(request);
3027
3028 spin_unlock(&timeline->lock);
3029 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3030}
3031
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003032/* Returns the request if it was guilty of the hang */
3033static struct drm_i915_gem_request *
3034i915_gem_reset_request(struct intel_engine_cs *engine,
3035 struct drm_i915_gem_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003036{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003037 /* The guilty request will get skipped on a hung engine.
3038 *
3039 * Users of client default contexts do not rely on logical
3040 * state preserved between batches so it is safe to execute
3041 * queued requests following the hang. Non default contexts
3042 * rely on preserved state, so skipping a batch loses the
3043 * evolution of the state and it needs to be considered corrupted.
3044 * Executing more queued batches on top of corrupted state is
3045 * risky. But we take the risk by trying to advance through
3046 * the queued requests in order to make the client behaviour
3047 * more predictable around resets, by not throwing away random
3048 * amount of batches it has prepared for execution. Sophisticated
3049 * clients can use gem_reset_stats_ioctl and dma fence status
3050 * (exported via sync_file info ioctl on explicit fences) to observe
3051 * when it loses the context state and should rebuild accordingly.
3052 *
3053 * The context ban, and ultimately the client ban, mechanism are safety
3054 * valves if client submission ends up resulting in nothing more than
3055 * subsequent hangs.
3056 */
3057
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003058 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003059 i915_gem_context_mark_guilty(request->ctx);
3060 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003061
3062 /* If this context is now banned, skip all pending requests. */
3063 if (i915_gem_context_is_banned(request->ctx))
3064 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003065 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003066 /*
3067 * Since this is not the hung engine, it may have advanced
3068 * since the hang declaration. Double check by refinding
3069 * the active request at the time of the reset.
3070 */
3071 request = i915_gem_find_active_request(engine);
3072 if (request) {
3073 i915_gem_context_mark_innocent(request->ctx);
3074 dma_fence_set_error(&request->fence, -EAGAIN);
3075
3076 /* Rewind the engine to replay the incomplete rq */
3077 spin_lock_irq(&engine->timeline->lock);
3078 request = list_prev_entry(request, link);
3079 if (&request->link == &engine->timeline->requests)
3080 request = NULL;
3081 spin_unlock_irq(&engine->timeline->lock);
3082 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003083 }
3084
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003085 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003086}
3087
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003088void i915_gem_reset_engine(struct intel_engine_cs *engine,
3089 struct drm_i915_gem_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003090{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003091 /*
3092 * Make sure this write is visible before we re-enable the interrupt
3093 * handlers on another CPU, as tasklet_enable() resolves to just
3094 * a compiler barrier which is insufficient for our purpose here.
3095 */
3096 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003097
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003098 if (request)
3099 request = i915_gem_reset_request(engine, request);
3100
3101 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003102 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3103 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003104 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003105
3106 /* Setup the CS to resume from the breadcrumb of the hung request */
3107 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003108}
3109
Chris Wilsond8027092017-02-08 14:30:32 +00003110void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003111{
3112 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303113 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003114
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003115 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3116
Chris Wilson821ed7d2016-09-09 14:11:53 +01003117 i915_gem_retire_requests(dev_priv);
3118
Chris Wilson2ae55732017-02-12 17:20:02 +00003119 for_each_engine(engine, dev_priv, id) {
3120 struct i915_gem_context *ctx;
3121
Michel Thierryc64992e2017-06-20 10:57:44 +01003122 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003123 ctx = fetch_and_zero(&engine->last_retired_context);
3124 if (ctx)
3125 engine->context_unpin(engine, ctx);
Chris Wilson7b6da812017-12-16 00:03:34 +00003126
3127 /*
3128 * Ostensibily, we always want a context loaded for powersaving,
3129 * so if the engine is idle after the reset, send a request
3130 * to load our scratch kernel_context.
3131 *
3132 * More mysteriously, if we leave the engine idle after a reset,
3133 * the next userspace batch may hang, with what appears to be
3134 * an incoherent read by the CS (presumably stale TLB). An
3135 * empty request appears sufficient to paper over the glitch.
3136 */
3137 if (list_empty(&engine->timeline->requests)) {
3138 struct drm_i915_gem_request *rq;
3139
3140 rq = i915_gem_request_alloc(engine,
3141 dev_priv->kernel_context);
3142 if (!IS_ERR(rq))
3143 __i915_add_request(rq, false);
3144 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003145 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003146
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003147 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003148
3149 if (dev_priv->gt.awake) {
3150 intel_sanitize_gt_powersave(dev_priv);
3151 intel_enable_gt_powersave(dev_priv);
3152 if (INTEL_GEN(dev_priv) >= 6)
3153 gen6_rps_busy(dev_priv);
3154 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003155}
3156
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003157void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3158{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303159 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003160 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003161
3162 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003163}
3164
Chris Wilsond8027092017-02-08 14:30:32 +00003165void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3166{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003167 struct intel_engine_cs *engine;
3168 enum intel_engine_id id;
3169
Chris Wilsond8027092017-02-08 14:30:32 +00003170 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003171
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003172 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003173 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003174 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003175 }
Chris Wilsond8027092017-02-08 14:30:32 +00003176}
3177
Chris Wilson821ed7d2016-09-09 14:11:53 +01003178static void nop_submit_request(struct drm_i915_gem_request *request)
3179{
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003180 dma_fence_set_error(&request->fence, -EIO);
3181
3182 i915_gem_request_submit(request);
3183}
3184
3185static void nop_complete_submit_request(struct drm_i915_gem_request *request)
3186{
Chris Wilson8d550822017-10-06 12:56:17 +01003187 unsigned long flags;
3188
Chris Wilson3cd94422017-01-10 17:22:45 +00003189 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003190
3191 spin_lock_irqsave(&request->engine->timeline->lock, flags);
3192 __i915_gem_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003193 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003194 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003195}
3196
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003197void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003198{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003199 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303200 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003201
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003202 /*
3203 * First, stop submission to hw, but do not yet complete requests by
3204 * rolling the global seqno forward (since this would complete requests
3205 * for which we haven't set the fence error to EIO yet).
3206 */
Chris Wilson20e49332016-11-22 14:41:21 +00003207 for_each_engine(engine, i915, id)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003208 engine->submit_request = nop_submit_request;
3209
3210 /*
3211 * Make sure no one is running the old callback before we proceed with
3212 * cancelling requests and resetting the completion tracking. Otherwise
3213 * we might submit a request to the hardware which never completes.
3214 */
3215 synchronize_rcu();
3216
3217 for_each_engine(engine, i915, id) {
3218 /* Mark all executing requests as skipped */
3219 engine->cancel_requests(engine);
3220
3221 /*
3222 * Only once we've force-cancelled all in-flight requests can we
3223 * start to complete all requests.
3224 */
3225 engine->submit_request = nop_complete_submit_request;
3226 }
3227
3228 /*
3229 * Make sure no request can slip through without getting completed by
3230 * either this call here to intel_engine_init_global_seqno, or the one
3231 * in nop_complete_submit_request.
3232 */
3233 synchronize_rcu();
3234
3235 for_each_engine(engine, i915, id) {
3236 unsigned long flags;
3237
3238 /* Mark all pending requests as complete so that any concurrent
3239 * (lockless) lookup doesn't try and wait upon the request as we
3240 * reset it.
3241 */
3242 spin_lock_irqsave(&engine->timeline->lock, flags);
3243 intel_engine_init_global_seqno(engine,
3244 intel_engine_last_submit(engine));
3245 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3246 }
Chris Wilson20e49332016-11-22 14:41:21 +00003247
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003248 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3249 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003250}
3251
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003252bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3253{
3254 struct i915_gem_timeline *tl;
3255 int i;
3256
3257 lockdep_assert_held(&i915->drm.struct_mutex);
3258 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3259 return true;
3260
3261 /* Before unwedging, make sure that all pending operations
3262 * are flushed and errored out - we may have requests waiting upon
3263 * third party fences. We marked all inflight requests as EIO, and
3264 * every execbuf since returned EIO, for consistency we want all
3265 * the currently pending requests to also be marked as EIO, which
3266 * is done inside our nop_submit_request - and so we must wait.
3267 *
3268 * No more can be submitted until we reset the wedged bit.
3269 */
3270 list_for_each_entry(tl, &i915->gt.timelines, link) {
3271 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3272 struct drm_i915_gem_request *rq;
3273
3274 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3275 &i915->drm.struct_mutex);
3276 if (!rq)
3277 continue;
3278
3279 /* We can't use our normal waiter as we want to
3280 * avoid recursively trying to handle the current
3281 * reset. The basic dma_fence_default_wait() installs
3282 * a callback for dma_fence_signal(), which is
3283 * triggered by our nop handler (indirectly, the
3284 * callback enables the signaler thread which is
3285 * woken by the nop_submit_request() advancing the seqno
3286 * and when the seqno passes the fence, the signaler
3287 * then signals the fence waking us up).
3288 */
3289 if (dma_fence_default_wait(&rq->fence, true,
3290 MAX_SCHEDULE_TIMEOUT) < 0)
3291 return false;
3292 }
3293 }
3294
3295 /* Undo nop_submit_request. We prevent all new i915 requests from
3296 * being queued (by disallowing execbuf whilst wedged) so having
3297 * waited for all active requests above, we know the system is idle
3298 * and do not have to worry about a thread being inside
3299 * engine->submit_request() as we swap over. So unlike installing
3300 * the nop_submit_request on reset, we can do this from normal
3301 * context and do not require stop_machine().
3302 */
3303 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003304 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003305
3306 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3307 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3308
3309 return true;
3310}
3311
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003312static void
Eric Anholt673a3942008-07-30 12:06:12 -07003313i915_gem_retire_work_handler(struct work_struct *work)
3314{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003315 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003316 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003317 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003318
Chris Wilson891b48c2010-09-29 12:26:37 +01003319 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003320 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003321 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003322 mutex_unlock(&dev->struct_mutex);
3323 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003324
3325 /* Keep the retire handler running until we are finally idle.
3326 * We do not need to do this test under locking as in the worst-case
3327 * we queue the retire worker once too often.
3328 */
Chris Wilsonc9615612016-07-09 10:12:06 +01003329 if (READ_ONCE(dev_priv->gt.awake)) {
3330 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01003331 queue_delayed_work(dev_priv->wq,
3332 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003333 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01003334 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003335}
Chris Wilson891b48c2010-09-29 12:26:37 +01003336
Chris Wilson84a10742018-01-24 11:36:08 +00003337static void shrink_caches(struct drm_i915_private *i915)
3338{
3339 /*
3340 * kmem_cache_shrink() discards empty slabs and reorders partially
3341 * filled slabs to prioritise allocating from the mostly full slabs,
3342 * with the aim of reducing fragmentation.
3343 */
3344 kmem_cache_shrink(i915->priorities);
3345 kmem_cache_shrink(i915->dependencies);
3346 kmem_cache_shrink(i915->requests);
3347 kmem_cache_shrink(i915->luts);
3348 kmem_cache_shrink(i915->vmas);
3349 kmem_cache_shrink(i915->objects);
3350}
3351
3352struct sleep_rcu_work {
3353 union {
3354 struct rcu_head rcu;
3355 struct work_struct work;
3356 };
3357 struct drm_i915_private *i915;
3358 unsigned int epoch;
3359};
3360
3361static inline bool
3362same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3363{
3364 /*
3365 * There is a small chance that the epoch wrapped since we started
3366 * sleeping. If we assume that epoch is at least a u32, then it will
3367 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3368 */
3369 return epoch == READ_ONCE(i915->gt.epoch);
3370}
3371
3372static void __sleep_work(struct work_struct *work)
3373{
3374 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3375 struct drm_i915_private *i915 = s->i915;
3376 unsigned int epoch = s->epoch;
3377
3378 kfree(s);
3379 if (same_epoch(i915, epoch))
3380 shrink_caches(i915);
3381}
3382
3383static void __sleep_rcu(struct rcu_head *rcu)
3384{
3385 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3386 struct drm_i915_private *i915 = s->i915;
3387
3388 if (same_epoch(i915, s->epoch)) {
3389 INIT_WORK(&s->work, __sleep_work);
3390 queue_work(i915->wq, &s->work);
3391 } else {
3392 kfree(s);
3393 }
3394}
3395
Chris Wilson5427f202017-10-23 22:32:34 +01003396static inline bool
3397new_requests_since_last_retire(const struct drm_i915_private *i915)
3398{
3399 return (READ_ONCE(i915->gt.active_requests) ||
3400 work_pending(&i915->gt.idle_work.work));
3401}
3402
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003403static void
3404i915_gem_idle_work_handler(struct work_struct *work)
3405{
3406 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003407 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003408 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003409 bool rearm_hangcheck;
Chris Wilson5427f202017-10-23 22:32:34 +01003410 ktime_t end;
Chris Wilson67d97da2016-07-04 08:08:31 +01003411
3412 if (!READ_ONCE(dev_priv->gt.awake))
3413 return;
3414
Imre Deak0cb56702016-11-07 11:20:04 +02003415 /*
3416 * Wait for last execlists context complete, but bail out in case a
3417 * new request is submitted.
3418 */
Chris Wilsonee42c002017-12-11 19:41:34 +00003419 end = ktime_add_ms(ktime_get(), I915_IDLE_ENGINES_TIMEOUT);
Chris Wilson5427f202017-10-23 22:32:34 +01003420 do {
3421 if (new_requests_since_last_retire(dev_priv))
3422 return;
3423
3424 if (intel_engines_are_idle(dev_priv))
3425 break;
3426
3427 usleep_range(100, 500);
3428 } while (ktime_before(ktime_get(), end));
Chris Wilson67d97da2016-07-04 08:08:31 +01003429
3430 rearm_hangcheck =
3431 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3432
Chris Wilson5427f202017-10-23 22:32:34 +01003433 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003434 /* Currently busy, come back later */
3435 mod_delayed_work(dev_priv->wq,
3436 &dev_priv->gt.idle_work,
3437 msecs_to_jiffies(50));
3438 goto out_rearm;
3439 }
3440
Imre Deak93c97dc2016-11-07 11:20:03 +02003441 /*
3442 * New request retired after this work handler started, extend active
3443 * period until next instance of the work.
3444 */
Chris Wilson5427f202017-10-23 22:32:34 +01003445 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003446 goto out_unlock;
3447
Chris Wilson5427f202017-10-23 22:32:34 +01003448 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003449 * Be paranoid and flush a concurrent interrupt to make sure
3450 * we don't reactivate any irq tasklets after parking.
3451 *
3452 * FIXME: Note that even though we have waited for execlists to be idle,
3453 * there may still be an in-flight interrupt even though the CSB
3454 * is now empty. synchronize_irq() makes sure that a residual interrupt
3455 * is completed before we continue, but it doesn't prevent the HW from
3456 * raising a spurious interrupt later. To complete the shield we should
3457 * coordinate disabling the CS irq with flushing the interrupts.
3458 */
3459 synchronize_irq(dev_priv->drm.irq);
3460
Chris Wilsonaba5e272017-10-25 15:39:41 +01003461 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003462 i915_gem_timelines_park(dev_priv);
3463
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003464 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003465
Chris Wilson67d97da2016-07-04 08:08:31 +01003466 GEM_BUG_ON(!dev_priv->gt.awake);
3467 dev_priv->gt.awake = false;
Chris Wilson84a10742018-01-24 11:36:08 +00003468 epoch = dev_priv->gt.epoch;
3469 GEM_BUG_ON(epoch == I915_EPOCH_INVALID);
Chris Wilson67d97da2016-07-04 08:08:31 +01003470 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003471
Chris Wilson67d97da2016-07-04 08:08:31 +01003472 if (INTEL_GEN(dev_priv) >= 6)
3473 gen6_rps_idle(dev_priv);
Tvrtko Ursulinb6876372017-12-05 13:28:54 +00003474
3475 intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ);
3476
Chris Wilson67d97da2016-07-04 08:08:31 +01003477 intel_runtime_pm_put(dev_priv);
3478out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003479 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003480
Chris Wilson67d97da2016-07-04 08:08:31 +01003481out_rearm:
3482 if (rearm_hangcheck) {
3483 GEM_BUG_ON(!dev_priv->gt.awake);
3484 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003485 }
Chris Wilson84a10742018-01-24 11:36:08 +00003486
3487 /*
3488 * When we are idle, it is an opportune time to reap our caches.
3489 * However, we have many objects that utilise RCU and the ordered
3490 * i915->wq that this work is executing on. To try and flush any
3491 * pending frees now we are idle, we first wait for an RCU grace
3492 * period, and then queue a task (that will run last on the wq) to
3493 * shrink and re-optimize the caches.
3494 */
3495 if (same_epoch(dev_priv, epoch)) {
3496 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3497 if (s) {
3498 s->i915 = dev_priv;
3499 s->epoch = epoch;
3500 call_rcu(&s->rcu, __sleep_rcu);
3501 }
3502 }
Eric Anholt673a3942008-07-30 12:06:12 -07003503}
3504
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003505void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3506{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003507 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003508 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3509 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003510 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003511
Chris Wilsond1b48c12017-08-16 09:52:08 +01003512 mutex_lock(&i915->drm.struct_mutex);
3513
3514 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3515 struct i915_gem_context *ctx = lut->ctx;
3516 struct i915_vma *vma;
3517
Chris Wilson432295d2017-08-22 12:05:15 +01003518 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003519 if (ctx->file_priv != fpriv)
3520 continue;
3521
3522 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003523 GEM_BUG_ON(vma->obj != obj);
3524
3525 /* We allow the process to have multiple handles to the same
3526 * vma, in the same fd namespace, by virtue of flink/open.
3527 */
3528 GEM_BUG_ON(!vma->open_count);
3529 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003530 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003531
Chris Wilsond1b48c12017-08-16 09:52:08 +01003532 list_del(&lut->obj_link);
3533 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003534
Chris Wilsond1b48c12017-08-16 09:52:08 +01003535 kmem_cache_free(i915->luts, lut);
3536 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003537 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003538
3539 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003540}
3541
Chris Wilsone95433c2016-10-28 13:58:27 +01003542static unsigned long to_wait_timeout(s64 timeout_ns)
3543{
3544 if (timeout_ns < 0)
3545 return MAX_SCHEDULE_TIMEOUT;
3546
3547 if (timeout_ns == 0)
3548 return 0;
3549
3550 return nsecs_to_jiffies_timeout(timeout_ns);
3551}
3552
Ben Widawsky5816d642012-04-11 11:18:19 -07003553/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003554 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003555 * @dev: drm device pointer
3556 * @data: ioctl data blob
3557 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003558 *
3559 * Returns 0 if successful, else an error is returned with the remaining time in
3560 * the timeout parameter.
3561 * -ETIME: object is still busy after timeout
3562 * -ERESTARTSYS: signal interrupted the wait
3563 * -ENONENT: object doesn't exist
3564 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003565 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003566 * -ENOMEM: damn
3567 * -ENODEV: Internal IRQ fail
3568 * -E?: The add request failed
3569 *
3570 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3571 * non-zero timeout parameter the wait ioctl will wait for the given number of
3572 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3573 * without holding struct_mutex the object may become re-busied before this
3574 * function completes. A similar but shorter * race condition exists in the busy
3575 * ioctl
3576 */
3577int
3578i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3579{
3580 struct drm_i915_gem_wait *args = data;
3581 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003582 ktime_t start;
3583 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003584
Daniel Vetter11b5d512014-09-29 15:31:26 +02003585 if (args->flags != 0)
3586 return -EINVAL;
3587
Chris Wilson03ac0642016-07-20 13:31:51 +01003588 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003589 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003590 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003591
Chris Wilsone95433c2016-10-28 13:58:27 +01003592 start = ktime_get();
3593
3594 ret = i915_gem_object_wait(obj,
3595 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3596 to_wait_timeout(args->timeout_ns),
3597 to_rps_client(file));
3598
3599 if (args->timeout_ns > 0) {
3600 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3601 if (args->timeout_ns < 0)
3602 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003603
3604 /*
3605 * Apparently ktime isn't accurate enough and occasionally has a
3606 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3607 * things up to make the test happy. We allow up to 1 jiffy.
3608 *
3609 * This is a regression from the timespec->ktime conversion.
3610 */
3611 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3612 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003613
3614 /* Asked to wait beyond the jiffie/scheduler precision? */
3615 if (ret == -ETIME && args->timeout_ns)
3616 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003617 }
3618
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003619 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003620 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003621}
3622
Chris Wilson73cb9702016-10-28 13:58:46 +01003623static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003624{
Chris Wilson73cb9702016-10-28 13:58:46 +01003625 int ret, i;
3626
3627 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3628 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3629 if (ret)
3630 return ret;
3631 }
3632
3633 return 0;
3634}
3635
Chris Wilson25112b62017-03-30 15:50:39 +01003636static int wait_for_engines(struct drm_i915_private *i915)
3637{
Chris Wilsonee42c002017-12-11 19:41:34 +00003638 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003639 dev_err(i915->drm.dev,
3640 "Failed to idle engines, declaring wedged!\n");
3641 if (drm_debug & DRM_UT_DRIVER) {
3642 struct drm_printer p = drm_debug_printer(__func__);
3643 struct intel_engine_cs *engine;
3644 enum intel_engine_id id;
3645
3646 for_each_engine(engine, i915, id)
3647 intel_engine_dump(engine, &p,
3648 "%s", engine->name);
3649 }
3650
Chris Wilsoncad99462017-08-26 12:09:33 +01003651 i915_gem_set_wedged(i915);
3652 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003653 }
3654
3655 return 0;
3656}
3657
Chris Wilson73cb9702016-10-28 13:58:46 +01003658int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3659{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003660 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003661
Chris Wilson863e9fd2017-05-30 13:13:32 +01003662 /* If the device is asleep, we have no requests outstanding */
3663 if (!READ_ONCE(i915->gt.awake))
3664 return 0;
3665
Chris Wilson9caa34a2016-11-11 14:58:08 +00003666 if (flags & I915_WAIT_LOCKED) {
3667 struct i915_gem_timeline *tl;
3668
3669 lockdep_assert_held(&i915->drm.struct_mutex);
3670
3671 list_for_each_entry(tl, &i915->gt.timelines, link) {
3672 ret = wait_for_timeline(tl, flags);
3673 if (ret)
3674 return ret;
3675 }
Chris Wilson72022a72017-03-30 15:50:38 +01003676 i915_gem_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003677
3678 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003679 } else {
3680 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003681 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003682
Chris Wilson25112b62017-03-30 15:50:39 +01003683 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003684}
3685
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003686static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3687{
Chris Wilsone27ab732017-06-15 13:38:49 +01003688 /*
3689 * We manually flush the CPU domain so that we can override and
3690 * force the flush for the display, and perform it asyncrhonously.
3691 */
3692 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3693 if (obj->cache_dirty)
3694 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003695 obj->base.write_domain = 0;
3696}
3697
3698void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3699{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003700 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003701 return;
3702
3703 mutex_lock(&obj->base.dev->struct_mutex);
3704 __i915_gem_object_flush_for_display(obj);
3705 mutex_unlock(&obj->base.dev->struct_mutex);
3706}
3707
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003708/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003709 * Moves a single object to the WC read, and possibly write domain.
3710 * @obj: object to act on
3711 * @write: ask for write access or read only
3712 *
3713 * This function returns when the move is complete, including waiting on
3714 * flushes to occur.
3715 */
3716int
3717i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3718{
3719 int ret;
3720
3721 lockdep_assert_held(&obj->base.dev->struct_mutex);
3722
3723 ret = i915_gem_object_wait(obj,
3724 I915_WAIT_INTERRUPTIBLE |
3725 I915_WAIT_LOCKED |
3726 (write ? I915_WAIT_ALL : 0),
3727 MAX_SCHEDULE_TIMEOUT,
3728 NULL);
3729 if (ret)
3730 return ret;
3731
3732 if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
3733 return 0;
3734
3735 /* Flush and acquire obj->pages so that we are coherent through
3736 * direct access in memory with previous cached writes through
3737 * shmemfs and that our cache domain tracking remains valid.
3738 * For example, if the obj->filp was moved to swap without us
3739 * being notified and releasing the pages, we would mistakenly
3740 * continue to assume that the obj remained out of the CPU cached
3741 * domain.
3742 */
3743 ret = i915_gem_object_pin_pages(obj);
3744 if (ret)
3745 return ret;
3746
3747 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3748
3749 /* Serialise direct access to this object with the barriers for
3750 * coherent writes from the GPU, by effectively invalidating the
3751 * WC domain upon first access.
3752 */
3753 if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
3754 mb();
3755
3756 /* It should now be out of any other write domains, and we can update
3757 * the domain values for our changes.
3758 */
3759 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3760 obj->base.read_domains |= I915_GEM_DOMAIN_WC;
3761 if (write) {
3762 obj->base.read_domains = I915_GEM_DOMAIN_WC;
3763 obj->base.write_domain = I915_GEM_DOMAIN_WC;
3764 obj->mm.dirty = true;
3765 }
3766
3767 i915_gem_object_unpin_pages(obj);
3768 return 0;
3769}
3770
3771/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003772 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003773 * @obj: object to act on
3774 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003775 *
3776 * This function returns when the move is complete, including waiting on
3777 * flushes to occur.
3778 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003779int
Chris Wilson20217462010-11-23 15:26:33 +00003780i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003781{
Eric Anholte47c68e2008-11-14 13:35:19 -08003782 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003783
Chris Wilsone95433c2016-10-28 13:58:27 +01003784 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003785
Chris Wilsone95433c2016-10-28 13:58:27 +01003786 ret = i915_gem_object_wait(obj,
3787 I915_WAIT_INTERRUPTIBLE |
3788 I915_WAIT_LOCKED |
3789 (write ? I915_WAIT_ALL : 0),
3790 MAX_SCHEDULE_TIMEOUT,
3791 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003792 if (ret)
3793 return ret;
3794
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003795 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3796 return 0;
3797
Chris Wilson43566de2015-01-02 16:29:29 +05303798 /* Flush and acquire obj->pages so that we are coherent through
3799 * direct access in memory with previous cached writes through
3800 * shmemfs and that our cache domain tracking remains valid.
3801 * For example, if the obj->filp was moved to swap without us
3802 * being notified and releasing the pages, we would mistakenly
3803 * continue to assume that the obj remained out of the CPU cached
3804 * domain.
3805 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003806 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303807 if (ret)
3808 return ret;
3809
Chris Wilsonef749212017-04-12 12:01:10 +01003810 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003811
Chris Wilsond0a57782012-10-09 19:24:37 +01003812 /* Serialise direct access to this object with the barriers for
3813 * coherent writes from the GPU, by effectively invalidating the
3814 * GTT domain upon first access.
3815 */
3816 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3817 mb();
3818
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003819 /* It should now be out of any other write domains, and we can update
3820 * the domain values for our changes.
3821 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003822 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003823 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003824 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003825 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3826 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003827 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003828 }
3829
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003830 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003831 return 0;
3832}
3833
Chris Wilsonef55f922015-10-09 14:11:27 +01003834/**
3835 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003836 * @obj: object to act on
3837 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003838 *
3839 * After this function returns, the object will be in the new cache-level
3840 * across all GTT and the contents of the backing storage will be coherent,
3841 * with respect to the new cache-level. In order to keep the backing storage
3842 * coherent for all users, we only allow a single cache level to be set
3843 * globally on the object and prevent it from being changed whilst the
3844 * hardware is reading from the object. That is if the object is currently
3845 * on the scanout it will be set to uncached (or equivalent display
3846 * cache coherency) and all non-MOCS GPU access will also be uncached so
3847 * that all direct access to the scanout remains coherent.
3848 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003849int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3850 enum i915_cache_level cache_level)
3851{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003852 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003853 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003854
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003855 lockdep_assert_held(&obj->base.dev->struct_mutex);
3856
Chris Wilsone4ffd172011-04-04 09:44:39 +01003857 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003858 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003859
Chris Wilsonef55f922015-10-09 14:11:27 +01003860 /* Inspect the list of currently bound VMA and unbind any that would
3861 * be invalid given the new cache-level. This is principally to
3862 * catch the issue of the CS prefetch crossing page boundaries and
3863 * reading an invalid PTE on older architectures.
3864 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003865restart:
3866 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003867 if (!drm_mm_node_allocated(&vma->node))
3868 continue;
3869
Chris Wilson20dfbde2016-08-04 16:32:30 +01003870 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003871 DRM_DEBUG("can not change the cache level of pinned objects\n");
3872 return -EBUSY;
3873 }
3874
Chris Wilson010e3e62017-12-06 12:49:13 +00003875 if (!i915_vma_is_closed(vma) &&
3876 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003877 continue;
3878
3879 ret = i915_vma_unbind(vma);
3880 if (ret)
3881 return ret;
3882
3883 /* As unbinding may affect other elements in the
3884 * obj->vma_list (due to side-effects from retiring
3885 * an active vma), play safe and restart the iterator.
3886 */
3887 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003888 }
3889
Chris Wilsonef55f922015-10-09 14:11:27 +01003890 /* We can reuse the existing drm_mm nodes but need to change the
3891 * cache-level on the PTE. We could simply unbind them all and
3892 * rebind with the correct cache-level on next use. However since
3893 * we already have a valid slot, dma mapping, pages etc, we may as
3894 * rewrite the PTE in the belief that doing so tramples upon less
3895 * state and so involves less work.
3896 */
Chris Wilson15717de2016-08-04 07:52:26 +01003897 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003898 /* Before we change the PTE, the GPU must not be accessing it.
3899 * If we wait upon the object, we know that all the bound
3900 * VMA are no longer active.
3901 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003902 ret = i915_gem_object_wait(obj,
3903 I915_WAIT_INTERRUPTIBLE |
3904 I915_WAIT_LOCKED |
3905 I915_WAIT_ALL,
3906 MAX_SCHEDULE_TIMEOUT,
3907 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003908 if (ret)
3909 return ret;
3910
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003911 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3912 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003913 /* Access to snoopable pages through the GTT is
3914 * incoherent and on some machines causes a hard
3915 * lockup. Relinquish the CPU mmaping to force
3916 * userspace to refault in the pages and we can
3917 * then double check if the GTT mapping is still
3918 * valid for that pointer access.
3919 */
3920 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003921
Chris Wilsonef55f922015-10-09 14:11:27 +01003922 /* As we no longer need a fence for GTT access,
3923 * we can relinquish it now (and so prevent having
3924 * to steal a fence from someone else on the next
3925 * fence request). Note GPU activity would have
3926 * dropped the fence as all snoopable access is
3927 * supposed to be linear.
3928 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003929 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003930 ret = i915_vma_put_fence(vma);
3931 if (ret)
3932 return ret;
3933 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003934 } else {
3935 /* We either have incoherent backing store and
3936 * so no GTT access or the architecture is fully
3937 * coherent. In such cases, existing GTT mmaps
3938 * ignore the cache bit in the PTE and we can
3939 * rewrite it without confusing the GPU or having
3940 * to force userspace to fault back in its mmaps.
3941 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003942 }
3943
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003944 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003945 if (!drm_mm_node_allocated(&vma->node))
3946 continue;
3947
3948 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3949 if (ret)
3950 return ret;
3951 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003952 }
3953
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003954 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003955 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003956 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003957 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003958
Chris Wilsone4ffd172011-04-04 09:44:39 +01003959 return 0;
3960}
3961
Ben Widawsky199adf42012-09-21 17:01:20 -07003962int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3963 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003964{
Ben Widawsky199adf42012-09-21 17:01:20 -07003965 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003966 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003967 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003968
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003969 rcu_read_lock();
3970 obj = i915_gem_object_lookup_rcu(file, args->handle);
3971 if (!obj) {
3972 err = -ENOENT;
3973 goto out;
3974 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003975
Chris Wilson651d7942013-08-08 14:41:10 +01003976 switch (obj->cache_level) {
3977 case I915_CACHE_LLC:
3978 case I915_CACHE_L3_LLC:
3979 args->caching = I915_CACHING_CACHED;
3980 break;
3981
Chris Wilson4257d3b2013-08-08 14:41:11 +01003982 case I915_CACHE_WT:
3983 args->caching = I915_CACHING_DISPLAY;
3984 break;
3985
Chris Wilson651d7942013-08-08 14:41:10 +01003986 default:
3987 args->caching = I915_CACHING_NONE;
3988 break;
3989 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003990out:
3991 rcu_read_unlock();
3992 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003993}
3994
Ben Widawsky199adf42012-09-21 17:01:20 -07003995int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3996 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003997{
Chris Wilson9c870d02016-10-24 13:42:15 +01003998 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003999 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004000 struct drm_i915_gem_object *obj;
4001 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004002 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004003
Ben Widawsky199adf42012-09-21 17:01:20 -07004004 switch (args->caching) {
4005 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004006 level = I915_CACHE_NONE;
4007 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004008 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004009 /*
4010 * Due to a HW issue on BXT A stepping, GPU stores via a
4011 * snooped mapping may leave stale data in a corresponding CPU
4012 * cacheline, whereas normally such cachelines would get
4013 * invalidated.
4014 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004015 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004016 return -ENODEV;
4017
Chris Wilsone6994ae2012-07-10 10:27:08 +01004018 level = I915_CACHE_LLC;
4019 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004020 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004021 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004022 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004023 default:
4024 return -EINVAL;
4025 }
4026
Chris Wilsond65415d2017-01-19 08:22:10 +00004027 obj = i915_gem_object_lookup(file, args->handle);
4028 if (!obj)
4029 return -ENOENT;
4030
Tina Zhanga03f3952017-11-14 10:25:13 +00004031 /*
4032 * The caching mode of proxy object is handled by its generator, and
4033 * not allowed to be changed by userspace.
4034 */
4035 if (i915_gem_object_is_proxy(obj)) {
4036 ret = -ENXIO;
4037 goto out;
4038 }
4039
Chris Wilsond65415d2017-01-19 08:22:10 +00004040 if (obj->cache_level == level)
4041 goto out;
4042
4043 ret = i915_gem_object_wait(obj,
4044 I915_WAIT_INTERRUPTIBLE,
4045 MAX_SCHEDULE_TIMEOUT,
4046 to_rps_client(file));
4047 if (ret)
4048 goto out;
4049
Ben Widawsky3bc29132012-09-26 16:15:20 -07004050 ret = i915_mutex_lock_interruptible(dev);
4051 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004052 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004053
4054 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004055 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004056
4057out:
4058 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004059 return ret;
4060}
4061
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004062/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004063 * Prepare buffer for display plane (scanout, cursors, etc).
4064 * Can be called from an uninterruptible phase (modesetting) and allows
4065 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004066 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004067struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004068i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4069 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004070 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004071{
Chris Wilson058d88c2016-08-15 10:49:06 +01004072 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004073 int ret;
4074
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004075 lockdep_assert_held(&obj->base.dev->struct_mutex);
4076
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004077 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004078 * display coherency whilst setting up the cache domains.
4079 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004080 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004081
Eric Anholta7ef0642011-03-29 16:59:54 -07004082 /* The display engine is not coherent with the LLC cache on gen6. As
4083 * a result, we make sure that the pinning that is about to occur is
4084 * done with uncached PTEs. This is lowest common denominator for all
4085 * chipsets.
4086 *
4087 * However for gen6+, we could do better by using the GFDT bit instead
4088 * of uncaching, which would allow us to flush all the LLC-cached data
4089 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4090 */
Chris Wilson651d7942013-08-08 14:41:10 +01004091 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004092 HAS_WT(to_i915(obj->base.dev)) ?
4093 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004094 if (ret) {
4095 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004096 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004097 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004098
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004099 /* As the user may map the buffer once pinned in the display plane
4100 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004101 * always use map_and_fenceable for all scanout buffers. However,
4102 * it may simply be too big to fit into mappable, in which case
4103 * put it anyway and hope that userspace can cope (but always first
4104 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004105 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004106 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00004107 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01004108 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
4109 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00004110 if (IS_ERR(vma)) {
4111 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4112 unsigned int flags;
4113
4114 /* Valleyview is definitely limited to scanning out the first
4115 * 512MiB. Lets presume this behaviour was inherited from the
4116 * g4x display engine and that all earlier gen are similarly
4117 * limited. Testing suggests that it is a little more
4118 * complicated than this. For example, Cherryview appears quite
4119 * happy to scanout from anywhere within its global aperture.
4120 */
4121 flags = 0;
4122 if (HAS_GMCH_DISPLAY(i915))
4123 flags = PIN_MAPPABLE;
4124 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
4125 }
Chris Wilson058d88c2016-08-15 10:49:06 +01004126 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004127 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004128
Chris Wilsond8923dc2016-08-18 17:17:07 +01004129 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4130
Chris Wilsona6a7cc42016-11-18 21:17:46 +00004131 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004132 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00004133 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004134
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004135 /* It should now be out of any other write domains, and we can update
4136 * the domain values for our changes.
4137 */
Chris Wilson05394f32010-11-08 19:18:58 +00004138 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004139
Chris Wilson058d88c2016-08-15 10:49:06 +01004140 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004141
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004142err_unpin_global:
4143 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004144 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004145}
4146
4147void
Chris Wilson058d88c2016-08-15 10:49:06 +01004148i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004149{
Chris Wilson49d73912016-11-29 09:50:08 +00004150 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004151
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004152 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004153 return;
4154
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004155 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004156 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004157
Chris Wilson383d5822016-08-18 17:17:08 +01004158 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004159 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004160
Chris Wilson058d88c2016-08-15 10:49:06 +01004161 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004162}
4163
Eric Anholte47c68e2008-11-14 13:35:19 -08004164/**
4165 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004166 * @obj: object to act on
4167 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004168 *
4169 * This function returns when the move is complete, including waiting on
4170 * flushes to occur.
4171 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004172int
Chris Wilson919926a2010-11-12 13:42:53 +00004173i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004174{
Eric Anholte47c68e2008-11-14 13:35:19 -08004175 int ret;
4176
Chris Wilsone95433c2016-10-28 13:58:27 +01004177 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004178
Chris Wilsone95433c2016-10-28 13:58:27 +01004179 ret = i915_gem_object_wait(obj,
4180 I915_WAIT_INTERRUPTIBLE |
4181 I915_WAIT_LOCKED |
4182 (write ? I915_WAIT_ALL : 0),
4183 MAX_SCHEDULE_TIMEOUT,
4184 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004185 if (ret)
4186 return ret;
4187
Chris Wilsonef749212017-04-12 12:01:10 +01004188 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004189
Eric Anholte47c68e2008-11-14 13:35:19 -08004190 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00004191 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004192 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00004193 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004194 }
4195
4196 /* It should now be out of any other write domains, and we can update
4197 * the domain values for our changes.
4198 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004199 GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004200
4201 /* If we're writing through the CPU, then the GPU read domains will
4202 * need to be invalidated at next use.
4203 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004204 if (write)
4205 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004206
4207 return 0;
4208}
4209
Eric Anholt673a3942008-07-30 12:06:12 -07004210/* Throttle our rendering by waiting until the ring has completed our requests
4211 * emitted over 20 msec ago.
4212 *
Eric Anholtb9624422009-06-03 07:27:35 +00004213 * Note that if we were to use the current jiffies each time around the loop,
4214 * we wouldn't escape the function with any frames outstanding if the time to
4215 * render a frame was over 20ms.
4216 *
Eric Anholt673a3942008-07-30 12:06:12 -07004217 * This should get us reasonable parallelism between CPU and GPU but also
4218 * relatively low latency when blocking on a particular request to finish.
4219 */
4220static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004221i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004222{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004223 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004224 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004225 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00004226 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004227 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004228
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004229 /* ABI: return -EIO if already wedged */
4230 if (i915_terminally_wedged(&dev_priv->gpu_error))
4231 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004232
Chris Wilson1c255952010-09-26 11:03:27 +01004233 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004234 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004235 if (time_after_eq(request->emitted_jiffies, recent_enough))
4236 break;
4237
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004238 if (target) {
4239 list_del(&target->client_link);
4240 target->file_priv = NULL;
4241 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004242
John Harrison54fb2412014-11-24 18:49:27 +00004243 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004244 }
John Harrisonff865882014-11-24 18:49:28 +00004245 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01004246 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004247 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004248
John Harrison54fb2412014-11-24 18:49:27 +00004249 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004250 return 0;
4251
Chris Wilsone95433c2016-10-28 13:58:27 +01004252 ret = i915_wait_request(target,
4253 I915_WAIT_INTERRUPTIBLE,
4254 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01004255 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004256
Chris Wilsone95433c2016-10-28 13:58:27 +01004257 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004258}
4259
Chris Wilson058d88c2016-08-15 10:49:06 +01004260struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004261i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4262 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004263 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004264 u64 alignment,
4265 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004266{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004267 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4268 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004269 struct i915_vma *vma;
4270 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004271
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004272 lockdep_assert_held(&obj->base.dev->struct_mutex);
4273
Chris Wilson43ae70d2017-10-09 09:44:01 +01004274 if (!view && flags & PIN_MAPPABLE) {
4275 /* If the required space is larger than the available
4276 * aperture, we will not able to find a slot for the
4277 * object and unbinding the object now will be in
4278 * vain. Worse, doing so may cause us to ping-pong
4279 * the object in and out of the Global GTT and
4280 * waste a lot of cycles under the mutex.
4281 */
4282 if (obj->base.size > dev_priv->ggtt.mappable_end)
4283 return ERR_PTR(-E2BIG);
4284
4285 /* If NONBLOCK is set the caller is optimistically
4286 * trying to cache the full object within the mappable
4287 * aperture, and *must* have a fallback in place for
4288 * situations where we cannot bind the object. We
4289 * can be a little more lax here and use the fallback
4290 * more often to avoid costly migrations of ourselves
4291 * and other objects within the aperture.
4292 *
4293 * Half-the-aperture is used as a simple heuristic.
4294 * More interesting would to do search for a free
4295 * block prior to making the commitment to unbind.
4296 * That caters for the self-harm case, and with a
4297 * little more heuristics (e.g. NOFAULT, NOEVICT)
4298 * we could try to minimise harm to others.
4299 */
4300 if (flags & PIN_NONBLOCK &&
4301 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4302 return ERR_PTR(-ENOSPC);
4303 }
4304
Chris Wilson718659a2017-01-16 15:21:28 +00004305 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004306 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004307 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004308
4309 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004310 if (flags & PIN_NONBLOCK) {
4311 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4312 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004313
Chris Wilson43ae70d2017-10-09 09:44:01 +01004314 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004315 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004316 return ERR_PTR(-ENOSPC);
4317 }
4318
Chris Wilson59bfa122016-08-04 16:32:31 +01004319 WARN(i915_vma_is_pinned(vma),
4320 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004321 " offset=%08x, req.alignment=%llx,"
4322 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4323 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004324 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004325 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004326 ret = i915_vma_unbind(vma);
4327 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004328 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004329 }
4330
Chris Wilson058d88c2016-08-15 10:49:06 +01004331 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4332 if (ret)
4333 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004334
Chris Wilson058d88c2016-08-15 10:49:06 +01004335 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004336}
4337
Chris Wilsonedf6b762016-08-09 09:23:33 +01004338static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004339{
4340 /* Note that we could alias engines in the execbuf API, but
4341 * that would be very unwise as it prevents userspace from
4342 * fine control over engine selection. Ahem.
4343 *
4344 * This should be something like EXEC_MAX_ENGINE instead of
4345 * I915_NUM_ENGINES.
4346 */
4347 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4348 return 0x10000 << id;
4349}
4350
4351static __always_inline unsigned int __busy_write_id(unsigned int id)
4352{
Chris Wilson70cb4722016-08-09 18:08:25 +01004353 /* The uABI guarantees an active writer is also amongst the read
4354 * engines. This would be true if we accessed the activity tracking
4355 * under the lock, but as we perform the lookup of the object and
4356 * its activity locklessly we can not guarantee that the last_write
4357 * being active implies that we have set the same engine flag from
4358 * last_read - hence we always set both read and write busy for
4359 * last_write.
4360 */
4361 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004362}
4363
Chris Wilsonedf6b762016-08-09 09:23:33 +01004364static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004365__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004366 unsigned int (*flag)(unsigned int id))
4367{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004368 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004369
Chris Wilsond07f0e52016-10-28 13:58:44 +01004370 /* We have to check the current hw status of the fence as the uABI
4371 * guarantees forward progress. We could rely on the idle worker
4372 * to eventually flush us, but to minimise latency just ask the
4373 * hardware.
4374 *
4375 * Note we only report on the status of native fences.
4376 */
4377 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004378 return 0;
4379
Chris Wilsond07f0e52016-10-28 13:58:44 +01004380 /* opencode to_request() in order to avoid const warnings */
4381 rq = container_of(fence, struct drm_i915_gem_request, fence);
4382 if (i915_gem_request_completed(rq))
4383 return 0;
4384
Chris Wilson1d39f282017-04-11 13:43:06 +01004385 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004386}
4387
Chris Wilsonedf6b762016-08-09 09:23:33 +01004388static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004389busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004390{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004391 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004392}
4393
Chris Wilsonedf6b762016-08-09 09:23:33 +01004394static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004395busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004396{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004397 if (!fence)
4398 return 0;
4399
4400 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004401}
4402
Eric Anholt673a3942008-07-30 12:06:12 -07004403int
Eric Anholt673a3942008-07-30 12:06:12 -07004404i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004405 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004406{
4407 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004408 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004409 struct reservation_object_list *list;
4410 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004411 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004412
Chris Wilsond07f0e52016-10-28 13:58:44 +01004413 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004414 rcu_read_lock();
4415 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004416 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004417 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004418
4419 /* A discrepancy here is that we do not report the status of
4420 * non-i915 fences, i.e. even though we may report the object as idle,
4421 * a call to set-domain may still stall waiting for foreign rendering.
4422 * This also means that wait-ioctl may report an object as busy,
4423 * where busy-ioctl considers it idle.
4424 *
4425 * We trade the ability to warn of foreign fences to report on which
4426 * i915 engines are active for the object.
4427 *
4428 * Alternatively, we can trade that extra information on read/write
4429 * activity with
4430 * args->busy =
4431 * !reservation_object_test_signaled_rcu(obj->resv, true);
4432 * to report the overall busyness. This is what the wait-ioctl does.
4433 *
4434 */
4435retry:
4436 seq = raw_read_seqcount(&obj->resv->seq);
4437
4438 /* Translate the exclusive fence to the READ *and* WRITE engine */
4439 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4440
4441 /* Translate shared fences to READ set of engines */
4442 list = rcu_dereference(obj->resv->fence);
4443 if (list) {
4444 unsigned int shared_count = list->shared_count, i;
4445
4446 for (i = 0; i < shared_count; ++i) {
4447 struct dma_fence *fence =
4448 rcu_dereference(list->shared[i]);
4449
4450 args->busy |= busy_check_reader(fence);
4451 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004452 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004453
Chris Wilsond07f0e52016-10-28 13:58:44 +01004454 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4455 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004456
Chris Wilsond07f0e52016-10-28 13:58:44 +01004457 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004458out:
4459 rcu_read_unlock();
4460 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004461}
4462
4463int
4464i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4465 struct drm_file *file_priv)
4466{
Akshay Joshi0206e352011-08-16 15:34:10 -04004467 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004468}
4469
Chris Wilson3ef94da2009-09-14 16:50:29 +01004470int
4471i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4472 struct drm_file *file_priv)
4473{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004474 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004475 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004476 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004477 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004478
4479 switch (args->madv) {
4480 case I915_MADV_DONTNEED:
4481 case I915_MADV_WILLNEED:
4482 break;
4483 default:
4484 return -EINVAL;
4485 }
4486
Chris Wilson03ac0642016-07-20 13:31:51 +01004487 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004488 if (!obj)
4489 return -ENOENT;
4490
4491 err = mutex_lock_interruptible(&obj->mm.lock);
4492 if (err)
4493 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004494
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004495 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004496 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004497 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004498 if (obj->mm.madv == I915_MADV_WILLNEED) {
4499 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004500 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004501 obj->mm.quirked = false;
4502 }
4503 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004504 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004505 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004506 obj->mm.quirked = true;
4507 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004508 }
4509
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004510 if (obj->mm.madv != __I915_MADV_PURGED)
4511 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004512
Chris Wilson6c085a72012-08-20 11:40:46 +02004513 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004514 if (obj->mm.madv == I915_MADV_DONTNEED &&
4515 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004516 i915_gem_object_truncate(obj);
4517
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004518 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004519 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004520
Chris Wilson1233e2d2016-10-28 13:58:37 +01004521out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004522 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004523 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004524}
4525
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004526static void
4527frontbuffer_retire(struct i915_gem_active *active,
4528 struct drm_i915_gem_request *request)
4529{
4530 struct drm_i915_gem_object *obj =
4531 container_of(active, typeof(*obj), frontbuffer_write);
4532
Chris Wilsond59b21e2017-02-22 11:40:49 +00004533 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004534}
4535
Chris Wilson37e680a2012-06-07 15:38:42 +01004536void i915_gem_object_init(struct drm_i915_gem_object *obj,
4537 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004538{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004539 mutex_init(&obj->mm.lock);
4540
Ben Widawsky2f633152013-07-17 12:19:03 -07004541 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004542 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004543 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004544
Chris Wilson37e680a2012-06-07 15:38:42 +01004545 obj->ops = ops;
4546
Chris Wilsond07f0e52016-10-28 13:58:44 +01004547 reservation_object_init(&obj->__builtin_resv);
4548 obj->resv = &obj->__builtin_resv;
4549
Chris Wilson50349242016-08-18 17:17:04 +01004550 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004551 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004552
4553 obj->mm.madv = I915_MADV_WILLNEED;
4554 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4555 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004556
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004557 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004558}
4559
Chris Wilson37e680a2012-06-07 15:38:42 +01004560static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004561 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4562 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004563
Chris Wilson37e680a2012-06-07 15:38:42 +01004564 .get_pages = i915_gem_object_get_pages_gtt,
4565 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004566
4567 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004568};
4569
Matthew Auld465c4032017-10-06 23:18:14 +01004570static int i915_gem_object_create_shmem(struct drm_device *dev,
4571 struct drm_gem_object *obj,
4572 size_t size)
4573{
4574 struct drm_i915_private *i915 = to_i915(dev);
4575 unsigned long flags = VM_NORESERVE;
4576 struct file *filp;
4577
4578 drm_gem_private_object_init(dev, obj, size);
4579
4580 if (i915->mm.gemfs)
4581 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4582 flags);
4583 else
4584 filp = shmem_file_setup("i915", size, flags);
4585
4586 if (IS_ERR(filp))
4587 return PTR_ERR(filp);
4588
4589 obj->filp = filp;
4590
4591 return 0;
4592}
4593
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004594struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004595i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004596{
Daniel Vetterc397b902010-04-09 19:05:07 +00004597 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004598 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004599 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004600 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004601 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004602
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004603 /* There is a prevalence of the assumption that we fit the object's
4604 * page count inside a 32bit _signed_ variable. Let's document this and
4605 * catch if we ever need to fix it. In the meantime, if you do spot
4606 * such a local variable, please consider fixing!
4607 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004608 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004609 return ERR_PTR(-E2BIG);
4610
4611 if (overflows_type(size, obj->base.size))
4612 return ERR_PTR(-E2BIG);
4613
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004614 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004615 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004616 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004617
Matthew Auld465c4032017-10-06 23:18:14 +01004618 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004619 if (ret)
4620 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004621
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004622 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004623 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004624 /* 965gm cannot relocate objects above 4GiB. */
4625 mask &= ~__GFP_HIGHMEM;
4626 mask |= __GFP_DMA32;
4627 }
4628
Al Viro93c76a32015-12-04 23:45:44 -05004629 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004630 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004631 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004632
Chris Wilson37e680a2012-06-07 15:38:42 +01004633 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004634
Daniel Vetterc397b902010-04-09 19:05:07 +00004635 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4636 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4637
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004638 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004639 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004640 * cache) for about a 10% performance improvement
4641 * compared to uncached. Graphics requests other than
4642 * display scanout are coherent with the CPU in
4643 * accessing this cache. This means in this mode we
4644 * don't need to clflush on the CPU side, and on the
4645 * GPU side we only need to flush internal caches to
4646 * get data visible to the CPU.
4647 *
4648 * However, we maintain the display planes as UC, and so
4649 * need to rebind when first used as such.
4650 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004651 cache_level = I915_CACHE_LLC;
4652 else
4653 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004654
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004655 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004656
Daniel Vetterd861e332013-07-24 23:25:03 +02004657 trace_i915_gem_object_create(obj);
4658
Chris Wilson05394f32010-11-08 19:18:58 +00004659 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004660
4661fail:
4662 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004663 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004664}
4665
Chris Wilson340fbd82014-05-22 09:16:52 +01004666static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4667{
4668 /* If we are the last user of the backing storage (be it shmemfs
4669 * pages or stolen etc), we know that the pages are going to be
4670 * immediately released. In this case, we can then skip copying
4671 * back the contents from the GPU.
4672 */
4673
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004674 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004675 return false;
4676
4677 if (obj->base.filp == NULL)
4678 return true;
4679
4680 /* At first glance, this looks racy, but then again so would be
4681 * userspace racing mmap against close. However, the first external
4682 * reference to the filp can only be obtained through the
4683 * i915_gem_mmap_ioctl() which safeguards us against the user
4684 * acquiring such a reference whilst we are in the middle of
4685 * freeing the object.
4686 */
4687 return atomic_long_read(&obj->base.filp->f_count) == 1;
4688}
4689
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004690static void __i915_gem_free_objects(struct drm_i915_private *i915,
4691 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004692{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004693 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004694
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004695 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004696 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004697 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004698
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004699 trace_i915_gem_object_destroy(obj);
4700
Chris Wilsoncc731f52017-10-13 21:26:21 +01004701 mutex_lock(&i915->drm.struct_mutex);
4702
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004703 GEM_BUG_ON(i915_gem_object_is_active(obj));
4704 list_for_each_entry_safe(vma, vn,
4705 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004706 GEM_BUG_ON(i915_vma_is_active(vma));
4707 vma->flags &= ~I915_VMA_PIN_MASK;
4708 i915_vma_close(vma);
4709 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004710 GEM_BUG_ON(!list_empty(&obj->vma_list));
4711 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004712
Chris Wilsonf2123812017-10-16 12:40:37 +01004713 /* This serializes freeing with the shrinker. Since the free
4714 * is delayed, first by RCU then by the workqueue, we want the
4715 * shrinker to be able to free pages of unreferenced objects,
4716 * or else we may oom whilst there are plenty of deferred
4717 * freed objects.
4718 */
4719 if (i915_gem_object_has_pages(obj)) {
4720 spin_lock(&i915->mm.obj_lock);
4721 list_del_init(&obj->mm.link);
4722 spin_unlock(&i915->mm.obj_lock);
4723 }
4724
Chris Wilsoncc731f52017-10-13 21:26:21 +01004725 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004726
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004727 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004728 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004729 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004730 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004731
4732 if (obj->ops->release)
4733 obj->ops->release(obj);
4734
4735 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4736 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004737 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004738 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004739
4740 if (obj->base.import_attach)
4741 drm_prime_gem_destroy(&obj->base, NULL);
4742
Chris Wilsond07f0e52016-10-28 13:58:44 +01004743 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004744 drm_gem_object_release(&obj->base);
4745 i915_gem_info_remove_obj(i915, obj->base.size);
4746
4747 kfree(obj->bit_17);
4748 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004749
4750 if (on)
4751 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004752 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004753 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004754}
4755
4756static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4757{
4758 struct llist_node *freed;
4759
Chris Wilson87701b42017-10-13 21:26:20 +01004760 /* Free the oldest, most stale object to keep the free_list short */
4761 freed = NULL;
4762 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4763 /* Only one consumer of llist_del_first() allowed */
4764 spin_lock(&i915->mm.free_lock);
4765 freed = llist_del_first(&i915->mm.free_list);
4766 spin_unlock(&i915->mm.free_lock);
4767 }
4768 if (unlikely(freed)) {
4769 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004770 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004771 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004772}
4773
4774static void __i915_gem_free_work(struct work_struct *work)
4775{
4776 struct drm_i915_private *i915 =
4777 container_of(work, struct drm_i915_private, mm.free_work);
4778 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004779
Chris Wilson2ef1e722018-01-15 20:57:59 +00004780 /*
4781 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004782 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4783 * However, the object may also be bound into the global GTT (e.g.
4784 * older GPUs without per-process support, or for direct access through
4785 * the GTT either for the user or for scanout). Those VMA still need to
4786 * unbound now.
4787 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004788
Chris Wilsonf991c492017-11-06 11:15:08 +00004789 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004790 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004791 spin_unlock(&i915->mm.free_lock);
4792
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004793 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004794 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004795 return;
4796
4797 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004798 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004799 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004800}
4801
4802static void __i915_gem_free_object_rcu(struct rcu_head *head)
4803{
4804 struct drm_i915_gem_object *obj =
4805 container_of(head, typeof(*obj), rcu);
4806 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4807
Chris Wilson2ef1e722018-01-15 20:57:59 +00004808 /*
4809 * Since we require blocking on struct_mutex to unbind the freed
4810 * object from the GPU before releasing resources back to the
4811 * system, we can not do that directly from the RCU callback (which may
4812 * be a softirq context), but must instead then defer that work onto a
4813 * kthread. We use the RCU callback rather than move the freed object
4814 * directly onto the work queue so that we can mix between using the
4815 * worker and performing frees directly from subsequent allocations for
4816 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004817 */
4818 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004819 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004820}
4821
4822void i915_gem_free_object(struct drm_gem_object *gem_obj)
4823{
4824 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4825
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004826 if (obj->mm.quirked)
4827 __i915_gem_object_unpin_pages(obj);
4828
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004829 if (discard_backing_storage(obj))
4830 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004831
Chris Wilson2ef1e722018-01-15 20:57:59 +00004832 /*
4833 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004834 * read-side critical sections are complete, e.g.
4835 * i915_gem_busy_ioctl(). For the corresponding synchronized
4836 * lookup see i915_gem_object_lookup_rcu().
4837 */
4838 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004839}
4840
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004841void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4842{
4843 lockdep_assert_held(&obj->base.dev->struct_mutex);
4844
Chris Wilsond1b48c12017-08-16 09:52:08 +01004845 if (!i915_gem_object_has_active_reference(obj) &&
4846 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004847 i915_gem_object_set_active_reference(obj);
4848 else
4849 i915_gem_object_put(obj);
4850}
4851
Chris Wilsonae6c4572017-11-10 14:26:28 +00004852static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004853{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004854 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004855 struct intel_engine_cs *engine;
4856 enum intel_engine_id id;
4857
Chris Wilsonae6c4572017-11-10 14:26:28 +00004858 for_each_engine(engine, i915, id) {
4859 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4860 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4861 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004862}
4863
Chris Wilson24145512017-01-24 11:01:35 +00004864void i915_gem_sanitize(struct drm_i915_private *i915)
4865{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004866 if (i915_terminally_wedged(&i915->gpu_error)) {
4867 mutex_lock(&i915->drm.struct_mutex);
4868 i915_gem_unset_wedged(i915);
4869 mutex_unlock(&i915->drm.struct_mutex);
4870 }
4871
Chris Wilson24145512017-01-24 11:01:35 +00004872 /*
4873 * If we inherit context state from the BIOS or earlier occupants
4874 * of the GPU, the GPU may be in an inconsistent state when we
4875 * try to take over. The only way to remove the earlier state
4876 * is by resetting. However, resetting on earlier gen is tricky as
4877 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004878 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004879 */
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004880 if (INTEL_GEN(i915) >= 5) {
Chris Wilson24145512017-01-24 11:01:35 +00004881 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4882 WARN_ON(reset && reset != -ENODEV);
4883 }
4884}
4885
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004886int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004887{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004888 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004889 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004890
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004891 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004892 intel_suspend_gt_powersave(dev_priv);
4893
Chris Wilson45c5f202013-10-16 11:50:01 +01004894 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004895
4896 /* We have to flush all the executing contexts to main memory so
4897 * that they can saved in the hibernation image. To ensure the last
4898 * context image is coherent, we have to switch away from it. That
4899 * leaves the dev_priv->kernel_context still active when
4900 * we actually suspend, and its image in memory may not match the GPU
4901 * state. Fortunately, the kernel_context is disposable and we do
4902 * not rely on its state.
4903 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004904 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4905 ret = i915_gem_switch_to_kernel_context(dev_priv);
4906 if (ret)
4907 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004908
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004909 ret = i915_gem_wait_for_idle(dev_priv,
4910 I915_WAIT_INTERRUPTIBLE |
4911 I915_WAIT_LOCKED);
4912 if (ret && ret != -EIO)
4913 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004914
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004915 assert_kernel_context_is_current(dev_priv);
4916 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004917 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004918 mutex_unlock(&dev->struct_mutex);
4919
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304920 intel_guc_suspend(dev_priv);
4921
Chris Wilson737b1502015-01-26 18:03:03 +02004922 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004923 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004924
4925 /* As the idle_work is rearming if it detects a race, play safe and
4926 * repeat the flush until it is definitely idle.
4927 */
Chris Wilson7c262402017-10-06 11:40:38 +01004928 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004929
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004930 /* Assert that we sucessfully flushed all the work and
4931 * reset the GPU back to its idle, low power state.
4932 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004933 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004934 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4935 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004936
Imre Deak1c777c52016-10-12 17:46:37 +03004937 /*
4938 * Neither the BIOS, ourselves or any other kernel
4939 * expects the system to be in execlists mode on startup,
4940 * so we need to reset the GPU back to legacy mode. And the only
4941 * known way to disable logical contexts is through a GPU reset.
4942 *
4943 * So in order to leave the system in a known default configuration,
4944 * always reset the GPU upon unload and suspend. Afterwards we then
4945 * clean up the GEM state tracking, flushing off the requests and
4946 * leaving the system in a known idle state.
4947 *
4948 * Note that is of the upmost importance that the GPU is idle and
4949 * all stray writes are flushed *before* we dismantle the backing
4950 * storage for the pinned objects.
4951 *
4952 * However, since we are uncertain that resetting the GPU on older
4953 * machines is a good idea, we don't - just in case it leaves the
4954 * machine in an unusable condition.
4955 */
Chris Wilson24145512017-01-24 11:01:35 +00004956 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004957
4958 intel_runtime_pm_put(dev_priv);
4959 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004960
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004961err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004962 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004963 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004964 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004965}
4966
Chris Wilson37cd3302017-11-12 11:27:38 +00004967void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004968{
Chris Wilson37cd3302017-11-12 11:27:38 +00004969 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004970
Chris Wilson37cd3302017-11-12 11:27:38 +00004971 mutex_lock(&i915->drm.struct_mutex);
4972 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004973
Chris Wilson37cd3302017-11-12 11:27:38 +00004974 i915_gem_restore_gtt_mappings(i915);
4975 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004976
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004977 /*
4978 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004979 * guarantee that the context image is complete. So let's just reset
4980 * it and start again.
4981 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004982 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004983
Chris Wilson37cd3302017-11-12 11:27:38 +00004984 if (i915_gem_init_hw(i915))
4985 goto err_wedged;
4986
Chris Wilson7469c622017-11-14 13:03:00 +00004987 intel_guc_resume(i915);
4988
Chris Wilson37cd3302017-11-12 11:27:38 +00004989 /* Always reload a context for powersaving. */
4990 if (i915_gem_switch_to_kernel_context(i915))
4991 goto err_wedged;
4992
4993out_unlock:
4994 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4995 mutex_unlock(&i915->drm.struct_mutex);
4996 return;
4997
4998err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004999 if (!i915_terminally_wedged(&i915->gpu_error)) {
5000 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5001 i915_gem_set_wedged(i915);
5002 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005003 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005004}
5005
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005006void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005007{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005008 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005009 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5010 return;
5011
5012 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5013 DISP_TILE_SURFACE_SWIZZLING);
5014
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005015 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005016 return;
5017
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005018 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005019 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005020 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005021 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005022 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005023 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005024 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005025 else
5026 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005027}
Daniel Vettere21af882012-02-09 20:53:27 +01005028
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005029static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005030{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005031 I915_WRITE(RING_CTL(base), 0);
5032 I915_WRITE(RING_HEAD(base), 0);
5033 I915_WRITE(RING_TAIL(base), 0);
5034 I915_WRITE(RING_START(base), 0);
5035}
5036
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005037static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005038{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005039 if (IS_I830(dev_priv)) {
5040 init_unused_ring(dev_priv, PRB1_BASE);
5041 init_unused_ring(dev_priv, SRB0_BASE);
5042 init_unused_ring(dev_priv, SRB1_BASE);
5043 init_unused_ring(dev_priv, SRB2_BASE);
5044 init_unused_ring(dev_priv, SRB3_BASE);
5045 } else if (IS_GEN2(dev_priv)) {
5046 init_unused_ring(dev_priv, SRB0_BASE);
5047 init_unused_ring(dev_priv, SRB1_BASE);
5048 } else if (IS_GEN3(dev_priv)) {
5049 init_unused_ring(dev_priv, PRB1_BASE);
5050 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005051 }
5052}
5053
Chris Wilson20a8a742017-02-08 14:30:31 +00005054static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005055{
Chris Wilson20a8a742017-02-08 14:30:31 +00005056 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005057 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305058 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005059 int err;
5060
5061 for_each_engine(engine, i915, id) {
5062 err = engine->init_hw(engine);
5063 if (err)
5064 return err;
5065 }
5066
5067 return 0;
5068}
5069
5070int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5071{
Chris Wilsond200cda2016-04-28 09:56:44 +01005072 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005073
Chris Wilsonde867c22016-10-25 13:16:02 +01005074 dev_priv->gt.last_init_time = ktime_get();
5075
Chris Wilson5e4f5182015-02-13 14:35:59 +00005076 /* Double layer security blanket, see i915_gem_init() */
5077 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5078
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005079 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005080 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005081
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005082 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005083 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005084 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005085
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005086 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005087 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005088 u32 temp = I915_READ(GEN7_MSG_CTL);
5089 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5090 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005091 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005092 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5093 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5094 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5095 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005096 }
5097
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005098 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005099
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005100 /*
5101 * At least 830 can leave some of the unused rings
5102 * "active" (ie. head != tail) after resume which
5103 * will prevent c3 entry. Makes sure all unused rings
5104 * are totally idle.
5105 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005106 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005107
Dave Gordoned54c1a2016-01-19 19:02:54 +00005108 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005109 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5110 ret = -EIO;
5111 goto out;
5112 }
John Harrison90638cc2015-05-29 17:43:37 +01005113
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005114 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005115 if (ret) {
5116 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
5117 goto out;
5118 }
5119
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005120 /* We can't enable contexts until all firmware is loaded */
5121 ret = intel_uc_init_hw(dev_priv);
5122 if (ret)
5123 goto out;
5124
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005125 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005126
Chris Wilson136109c2017-11-02 13:14:30 +00005127 /* Only when the HW is re-initialised, can we replay the requests */
5128 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005129out:
5130 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005131 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005132}
5133
Chris Wilsond2b4b972017-11-10 14:26:33 +00005134static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5135{
5136 struct i915_gem_context *ctx;
5137 struct intel_engine_cs *engine;
5138 enum intel_engine_id id;
5139 int err;
5140
5141 /*
5142 * As we reset the gpu during very early sanitisation, the current
5143 * register state on the GPU should reflect its defaults values.
5144 * We load a context onto the hw (with restore-inhibit), then switch
5145 * over to a second context to save that default register state. We
5146 * can then prime every new context with that state so they all start
5147 * from the same default HW values.
5148 */
5149
5150 ctx = i915_gem_context_create_kernel(i915, 0);
5151 if (IS_ERR(ctx))
5152 return PTR_ERR(ctx);
5153
5154 for_each_engine(engine, i915, id) {
5155 struct drm_i915_gem_request *rq;
5156
5157 rq = i915_gem_request_alloc(engine, ctx);
5158 if (IS_ERR(rq)) {
5159 err = PTR_ERR(rq);
5160 goto out_ctx;
5161 }
5162
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005163 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005164 if (engine->init_context)
5165 err = engine->init_context(rq);
5166
5167 __i915_add_request(rq, true);
5168 if (err)
5169 goto err_active;
5170 }
5171
5172 err = i915_gem_switch_to_kernel_context(i915);
5173 if (err)
5174 goto err_active;
5175
5176 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5177 if (err)
5178 goto err_active;
5179
5180 assert_kernel_context_is_current(i915);
5181
5182 for_each_engine(engine, i915, id) {
5183 struct i915_vma *state;
5184
5185 state = ctx->engine[id].state;
5186 if (!state)
5187 continue;
5188
5189 /*
5190 * As we will hold a reference to the logical state, it will
5191 * not be torn down with the context, and importantly the
5192 * object will hold onto its vma (making it possible for a
5193 * stray GTT write to corrupt our defaults). Unmap the vma
5194 * from the GTT to prevent such accidents and reclaim the
5195 * space.
5196 */
5197 err = i915_vma_unbind(state);
5198 if (err)
5199 goto err_active;
5200
5201 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5202 if (err)
5203 goto err_active;
5204
5205 engine->default_state = i915_gem_object_get(state->obj);
5206 }
5207
5208 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5209 unsigned int found = intel_engines_has_context_isolation(i915);
5210
5211 /*
5212 * Make sure that classes with multiple engine instances all
5213 * share the same basic configuration.
5214 */
5215 for_each_engine(engine, i915, id) {
5216 unsigned int bit = BIT(engine->uabi_class);
5217 unsigned int expected = engine->default_state ? bit : 0;
5218
5219 if ((found & bit) != expected) {
5220 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5221 engine->uabi_class, engine->name);
5222 }
5223 }
5224 }
5225
5226out_ctx:
5227 i915_gem_context_set_closed(ctx);
5228 i915_gem_context_put(ctx);
5229 return err;
5230
5231err_active:
5232 /*
5233 * If we have to abandon now, we expect the engines to be idle
5234 * and ready to be torn-down. First try to flush any remaining
5235 * request, ensure we are pointing at the kernel context and
5236 * then remove it.
5237 */
5238 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5239 goto out_ctx;
5240
5241 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5242 goto out_ctx;
5243
5244 i915_gem_contexts_lost(i915);
5245 goto out_ctx;
5246}
5247
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005248int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005249{
Chris Wilson1070a422012-04-24 15:47:41 +01005250 int ret;
5251
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005252 /*
5253 * We need to fallback to 4K pages since gvt gtt handling doesn't
5254 * support huge page entries - we will need to check either hypervisor
5255 * mm can support huge guest page or just do emulation in gvt.
5256 */
5257 if (intel_vgpu_active(dev_priv))
5258 mkwrite_device_info(dev_priv)->page_sizes =
5259 I915_GTT_PAGE_SIZE_4K;
5260
Chris Wilson94312822017-05-03 10:39:18 +01005261 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005262
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005263 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005264 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005265 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005266 } else {
5267 dev_priv->gt.resume = intel_legacy_submission_resume;
5268 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005269 }
5270
Chris Wilsonee487002017-11-22 17:26:21 +00005271 ret = i915_gem_init_userptr(dev_priv);
5272 if (ret)
5273 return ret;
5274
Michał Winiarski3176ff42017-12-13 23:13:47 +01005275 ret = intel_uc_init_wq(dev_priv);
5276 if (ret)
5277 return ret;
5278
Chris Wilson5e4f5182015-02-13 14:35:59 +00005279 /* This is just a security blanket to placate dragons.
5280 * On some systems, we very sporadically observe that the first TLBs
5281 * used by the CS may be stale, despite us poking the TLB reset. If
5282 * we hold the forcewake during initialisation these problems
5283 * just magically go away.
5284 */
Chris Wilsonee487002017-11-22 17:26:21 +00005285 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005286 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5287
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005288 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005289 if (ret) {
5290 GEM_BUG_ON(ret == -EIO);
5291 goto err_unlock;
5292 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005293
Chris Wilson829a0af2017-06-20 12:05:45 +01005294 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005295 if (ret) {
5296 GEM_BUG_ON(ret == -EIO);
5297 goto err_ggtt;
5298 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005299
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005300 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005301 if (ret) {
5302 GEM_BUG_ON(ret == -EIO);
5303 goto err_context;
5304 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005305
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005306 intel_init_gt_powersave(dev_priv);
5307
Michał Winiarski61b5c152017-12-13 23:13:48 +01005308 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005309 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005310 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005311
Michał Winiarski61b5c152017-12-13 23:13:48 +01005312 ret = i915_gem_init_hw(dev_priv);
5313 if (ret)
5314 goto err_uc_init;
5315
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005316 /*
5317 * Despite its name intel_init_clock_gating applies both display
5318 * clock gating workarounds; GT mmio workarounds and the occasional
5319 * GT power context workaround. Worse, sometimes it includes a context
5320 * register workaround which we need to apply before we record the
5321 * default HW state for all contexts.
5322 *
5323 * FIXME: break up the workarounds and apply them at the right time!
5324 */
5325 intel_init_clock_gating(dev_priv);
5326
Chris Wilsond2b4b972017-11-10 14:26:33 +00005327 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005328 if (ret)
5329 goto err_init_hw;
5330
5331 if (i915_inject_load_failure()) {
5332 ret = -ENODEV;
5333 goto err_init_hw;
5334 }
5335
5336 if (i915_inject_load_failure()) {
5337 ret = -EIO;
5338 goto err_init_hw;
5339 }
5340
5341 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5342 mutex_unlock(&dev_priv->drm.struct_mutex);
5343
5344 return 0;
5345
5346 /*
5347 * Unwinding is complicated by that we want to handle -EIO to mean
5348 * disable GPU submission but keep KMS alive. We want to mark the
5349 * HW as irrevisibly wedged, but keep enough state around that the
5350 * driver doesn't explode during runtime.
5351 */
5352err_init_hw:
5353 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5354 i915_gem_contexts_lost(dev_priv);
5355 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005356err_uc_init:
5357 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005358err_pm:
5359 if (ret != -EIO) {
5360 intel_cleanup_gt_powersave(dev_priv);
5361 i915_gem_cleanup_engines(dev_priv);
5362 }
5363err_context:
5364 if (ret != -EIO)
5365 i915_gem_contexts_fini(dev_priv);
5366err_ggtt:
5367err_unlock:
5368 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5369 mutex_unlock(&dev_priv->drm.struct_mutex);
5370
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305371 intel_uc_fini_wq(dev_priv);
5372
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005373 if (ret != -EIO)
5374 i915_gem_cleanup_userptr(dev_priv);
5375
Chris Wilson60990322014-04-09 09:19:42 +01005376 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005377 /*
5378 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005379 * wedged. But we only want to do this where the GPU is angry,
5380 * for all other failure, such as an allocation failure, bail.
5381 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005382 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5383 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5384 i915_gem_set_wedged(dev_priv);
5385 }
Chris Wilson60990322014-04-09 09:19:42 +01005386 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005387 }
5388
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005389 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005390 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005391}
5392
Chris Wilson24145512017-01-24 11:01:35 +00005393void i915_gem_init_mmio(struct drm_i915_private *i915)
5394{
5395 i915_gem_sanitize(i915);
5396}
5397
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005398void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005399i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005400{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005401 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305402 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005403
Akash Goel3b3f1652016-10-13 22:44:48 +05305404 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005405 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005406}
5407
Eric Anholt673a3942008-07-30 12:06:12 -07005408void
Imre Deak40ae4e12016-03-16 14:54:03 +02005409i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5410{
Chris Wilson49ef5292016-08-18 17:17:00 +01005411 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005412
5413 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
5414 !IS_CHERRYVIEW(dev_priv))
5415 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02005416 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
5417 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5418 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005419 dev_priv->num_fence_regs = 16;
5420 else
5421 dev_priv->num_fence_regs = 8;
5422
Chris Wilsonc0336662016-05-06 15:40:21 +01005423 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005424 dev_priv->num_fence_regs =
5425 I915_READ(vgtif_reg(avail_rs.fence_num));
5426
5427 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005428 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5429 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5430
5431 fence->i915 = dev_priv;
5432 fence->id = i;
5433 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5434 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005435 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005436
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005437 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005438}
5439
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005440static void i915_gem_init__mm(struct drm_i915_private *i915)
5441{
5442 spin_lock_init(&i915->mm.object_stat_lock);
5443 spin_lock_init(&i915->mm.obj_lock);
5444 spin_lock_init(&i915->mm.free_lock);
5445
5446 init_llist_head(&i915->mm.free_list);
5447
5448 INIT_LIST_HEAD(&i915->mm.unbound_list);
5449 INIT_LIST_HEAD(&i915->mm.bound_list);
5450 INIT_LIST_HEAD(&i915->mm.fence_list);
5451 INIT_LIST_HEAD(&i915->mm.userfault_list);
5452
5453 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5454}
5455
Chris Wilson73cb9702016-10-28 13:58:46 +01005456int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005457i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005458{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005459 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005460
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005461 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5462 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005463 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005464
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005465 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5466 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005467 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005468
Chris Wilsond1b48c12017-08-16 09:52:08 +01005469 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5470 if (!dev_priv->luts)
5471 goto err_vmas;
5472
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005473 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
5474 SLAB_HWCACHE_ALIGN |
5475 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005476 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005477 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005478 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005479
Chris Wilson52e54202016-11-14 20:41:02 +00005480 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5481 SLAB_HWCACHE_ALIGN |
5482 SLAB_RECLAIM_ACCOUNT);
5483 if (!dev_priv->dependencies)
5484 goto err_requests;
5485
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005486 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5487 if (!dev_priv->priorities)
5488 goto err_dependencies;
5489
Chris Wilson73cb9702016-10-28 13:58:46 +01005490 mutex_lock(&dev_priv->drm.struct_mutex);
5491 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005492 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005493 mutex_unlock(&dev_priv->drm.struct_mutex);
5494 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005495 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005496
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005497 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005498
Chris Wilson67d97da2016-07-04 08:08:31 +01005499 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005500 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005501 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005502 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005503 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005504 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005505
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005506 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5507
Chris Wilsonb5add952016-08-04 16:32:36 +01005508 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005509
Matthew Auld465c4032017-10-06 23:18:14 +01005510 err = i915_gemfs_init(dev_priv);
5511 if (err)
5512 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5513
Chris Wilson73cb9702016-10-28 13:58:46 +01005514 return 0;
5515
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005516err_priorities:
5517 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005518err_dependencies:
5519 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005520err_requests:
5521 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005522err_luts:
5523 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005524err_vmas:
5525 kmem_cache_destroy(dev_priv->vmas);
5526err_objects:
5527 kmem_cache_destroy(dev_priv->objects);
5528err_out:
5529 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005530}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005531
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005532void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005533{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005534 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005535 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005536 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005537
Matthew Auldea84aa72016-11-17 21:04:11 +00005538 mutex_lock(&dev_priv->drm.struct_mutex);
5539 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5540 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5541 mutex_unlock(&dev_priv->drm.struct_mutex);
5542
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005543 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005544 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005545 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005546 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005547 kmem_cache_destroy(dev_priv->vmas);
5548 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005549
5550 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5551 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005552
5553 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005554}
5555
Chris Wilson6a800ea2016-09-21 14:51:07 +01005556int i915_gem_freeze(struct drm_i915_private *dev_priv)
5557{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005558 /* Discard all purgeable objects, let userspace recover those as
5559 * required after resuming.
5560 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005561 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005562
Chris Wilson6a800ea2016-09-21 14:51:07 +01005563 return 0;
5564}
5565
Chris Wilson461fb992016-05-14 07:26:33 +01005566int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5567{
5568 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005569 struct list_head *phases[] = {
5570 &dev_priv->mm.unbound_list,
5571 &dev_priv->mm.bound_list,
5572 NULL
5573 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005574
5575 /* Called just before we write the hibernation image.
5576 *
5577 * We need to update the domain tracking to reflect that the CPU
5578 * will be accessing all the pages to create and restore from the
5579 * hibernation, and so upon restoration those pages will be in the
5580 * CPU domain.
5581 *
5582 * To make sure the hibernation image contains the latest state,
5583 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005584 *
5585 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005586 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005587 */
5588
Chris Wilson912d5722017-09-06 16:19:30 -07005589 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005590 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005591
Chris Wilsonf2123812017-10-16 12:40:37 +01005592 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005593 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005594 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005595 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005596 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005597 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005598
5599 return 0;
5600}
5601
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005602void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005603{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005604 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01005605 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005606
5607 /* Clean up our request list when the client is going away, so that
5608 * later retire_requests won't dereference our soon-to-be-gone
5609 * file_priv.
5610 */
Chris Wilson1c255952010-09-26 11:03:27 +01005611 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005612 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005613 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005614 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005615}
5616
Chris Wilson829a0af2017-06-20 12:05:45 +01005617int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005618{
5619 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005620 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005621
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005622 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005623
5624 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5625 if (!file_priv)
5626 return -ENOMEM;
5627
5628 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005629 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005630 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005631
5632 spin_lock_init(&file_priv->mm.lock);
5633 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005634
Chris Wilsonc80ff162016-07-27 09:07:27 +01005635 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005636
Chris Wilson829a0af2017-06-20 12:05:45 +01005637 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005638 if (ret)
5639 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005640
Ben Widawskye422b882013-12-06 14:10:58 -08005641 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005642}
5643
Daniel Vetterb680c372014-09-19 18:27:27 +02005644/**
5645 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005646 * @old: current GEM buffer for the frontbuffer slots
5647 * @new: new GEM buffer for the frontbuffer slots
5648 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005649 *
5650 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5651 * from @old and setting them in @new. Both @old and @new can be NULL.
5652 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005653void i915_gem_track_fb(struct drm_i915_gem_object *old,
5654 struct drm_i915_gem_object *new,
5655 unsigned frontbuffer_bits)
5656{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005657 /* Control of individual bits within the mask are guarded by
5658 * the owning plane->mutex, i.e. we can never see concurrent
5659 * manipulation of individual bits. But since the bitfield as a whole
5660 * is updated using RMW, we need to use atomics in order to update
5661 * the bits.
5662 */
5663 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5664 sizeof(atomic_t) * BITS_PER_BYTE);
5665
Daniel Vettera071fa02014-06-18 23:28:09 +02005666 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005667 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5668 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005669 }
5670
5671 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005672 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5673 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005674 }
5675}
5676
Dave Gordonea702992015-07-09 19:29:02 +01005677/* Allocate a new GEM object and fill it with the supplied data */
5678struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005679i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005680 const void *data, size_t size)
5681{
5682 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005683 struct file *file;
5684 size_t offset;
5685 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005686
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005687 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005688 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005689 return obj;
5690
Chris Wilsonce8ff092017-03-17 19:46:47 +00005691 GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005692
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005693 file = obj->base.filp;
5694 offset = 0;
5695 do {
5696 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5697 struct page *page;
5698 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005699
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005700 err = pagecache_write_begin(file, file->f_mapping,
5701 offset, len, 0,
5702 &page, &pgdata);
5703 if (err < 0)
5704 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005705
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005706 vaddr = kmap(page);
5707 memcpy(vaddr, data, len);
5708 kunmap(page);
5709
5710 err = pagecache_write_end(file, file->f_mapping,
5711 offset, len, len,
5712 page, pgdata);
5713 if (err < 0)
5714 goto fail;
5715
5716 size -= len;
5717 data += len;
5718 offset += len;
5719 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005720
5721 return obj;
5722
5723fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005724 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005725 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005726}
Chris Wilson96d77632016-10-28 13:58:33 +01005727
5728struct scatterlist *
5729i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5730 unsigned int n,
5731 unsigned int *offset)
5732{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005733 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005734 struct scatterlist *sg;
5735 unsigned int idx, count;
5736
5737 might_sleep();
5738 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005739 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005740
5741 /* As we iterate forward through the sg, we record each entry in a
5742 * radixtree for quick repeated (backwards) lookups. If we have seen
5743 * this index previously, we will have an entry for it.
5744 *
5745 * Initial lookup is O(N), but this is amortized to O(1) for
5746 * sequential page access (where each new request is consecutive
5747 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5748 * i.e. O(1) with a large constant!
5749 */
5750 if (n < READ_ONCE(iter->sg_idx))
5751 goto lookup;
5752
5753 mutex_lock(&iter->lock);
5754
5755 /* We prefer to reuse the last sg so that repeated lookup of this
5756 * (or the subsequent) sg are fast - comparing against the last
5757 * sg is faster than going through the radixtree.
5758 */
5759
5760 sg = iter->sg_pos;
5761 idx = iter->sg_idx;
5762 count = __sg_page_count(sg);
5763
5764 while (idx + count <= n) {
5765 unsigned long exception, i;
5766 int ret;
5767
5768 /* If we cannot allocate and insert this entry, or the
5769 * individual pages from this range, cancel updating the
5770 * sg_idx so that on this lookup we are forced to linearly
5771 * scan onwards, but on future lookups we will try the
5772 * insertion again (in which case we need to be careful of
5773 * the error return reporting that we have already inserted
5774 * this index).
5775 */
5776 ret = radix_tree_insert(&iter->radix, idx, sg);
5777 if (ret && ret != -EEXIST)
5778 goto scan;
5779
5780 exception =
5781 RADIX_TREE_EXCEPTIONAL_ENTRY |
5782 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5783 for (i = 1; i < count; i++) {
5784 ret = radix_tree_insert(&iter->radix, idx + i,
5785 (void *)exception);
5786 if (ret && ret != -EEXIST)
5787 goto scan;
5788 }
5789
5790 idx += count;
5791 sg = ____sg_next(sg);
5792 count = __sg_page_count(sg);
5793 }
5794
5795scan:
5796 iter->sg_pos = sg;
5797 iter->sg_idx = idx;
5798
5799 mutex_unlock(&iter->lock);
5800
5801 if (unlikely(n < idx)) /* insertion completed by another thread */
5802 goto lookup;
5803
5804 /* In case we failed to insert the entry into the radixtree, we need
5805 * to look beyond the current sg.
5806 */
5807 while (idx + count <= n) {
5808 idx += count;
5809 sg = ____sg_next(sg);
5810 count = __sg_page_count(sg);
5811 }
5812
5813 *offset = n - idx;
5814 return sg;
5815
5816lookup:
5817 rcu_read_lock();
5818
5819 sg = radix_tree_lookup(&iter->radix, n);
5820 GEM_BUG_ON(!sg);
5821
5822 /* If this index is in the middle of multi-page sg entry,
5823 * the radixtree will contain an exceptional entry that points
5824 * to the start of that range. We will return the pointer to
5825 * the base page and the offset of this page within the
5826 * sg entry's range.
5827 */
5828 *offset = 0;
5829 if (unlikely(radix_tree_exception(sg))) {
5830 unsigned long base =
5831 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5832
5833 sg = radix_tree_lookup(&iter->radix, base);
5834 GEM_BUG_ON(!sg);
5835
5836 *offset = n - base;
5837 }
5838
5839 rcu_read_unlock();
5840
5841 return sg;
5842}
5843
5844struct page *
5845i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5846{
5847 struct scatterlist *sg;
5848 unsigned int offset;
5849
5850 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5851
5852 sg = i915_gem_object_get_sg(obj, n, &offset);
5853 return nth_page(sg_page(sg), offset);
5854}
5855
5856/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5857struct page *
5858i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5859 unsigned int n)
5860{
5861 struct page *page;
5862
5863 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005864 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005865 set_page_dirty(page);
5866
5867 return page;
5868}
5869
5870dma_addr_t
5871i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5872 unsigned long n)
5873{
5874 struct scatterlist *sg;
5875 unsigned int offset;
5876
5877 sg = i915_gem_object_get_sg(obj, n, &offset);
5878 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5879}
Chris Wilson935a2f72017-02-13 17:15:13 +00005880
Chris Wilson8eeb7902017-07-26 19:16:01 +01005881int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5882{
5883 struct sg_table *pages;
5884 int err;
5885
5886 if (align > obj->base.size)
5887 return -EINVAL;
5888
5889 if (obj->ops == &i915_gem_phys_ops)
5890 return 0;
5891
5892 if (obj->ops != &i915_gem_object_ops)
5893 return -EINVAL;
5894
5895 err = i915_gem_object_unbind(obj);
5896 if (err)
5897 return err;
5898
5899 mutex_lock(&obj->mm.lock);
5900
5901 if (obj->mm.madv != I915_MADV_WILLNEED) {
5902 err = -EFAULT;
5903 goto err_unlock;
5904 }
5905
5906 if (obj->mm.quirked) {
5907 err = -EFAULT;
5908 goto err_unlock;
5909 }
5910
5911 if (obj->mm.mapping) {
5912 err = -EBUSY;
5913 goto err_unlock;
5914 }
5915
Chris Wilsonf2123812017-10-16 12:40:37 +01005916 pages = fetch_and_zero(&obj->mm.pages);
5917 if (pages) {
5918 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5919
5920 __i915_gem_object_reset_page_iter(obj);
5921
5922 spin_lock(&i915->mm.obj_lock);
5923 list_del(&obj->mm.link);
5924 spin_unlock(&i915->mm.obj_lock);
5925 }
5926
Chris Wilson8eeb7902017-07-26 19:16:01 +01005927 obj->ops = &i915_gem_phys_ops;
5928
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005929 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005930 if (err)
5931 goto err_xfer;
5932
5933 /* Perma-pin (until release) the physical set of pages */
5934 __i915_gem_object_pin_pages(obj);
5935
5936 if (!IS_ERR_OR_NULL(pages))
5937 i915_gem_object_ops.put_pages(obj, pages);
5938 mutex_unlock(&obj->mm.lock);
5939 return 0;
5940
5941err_xfer:
5942 obj->ops = &i915_gem_object_ops;
5943 obj->mm.pages = pages;
5944err_unlock:
5945 mutex_unlock(&obj->mm.lock);
5946 return err;
5947}
5948
Chris Wilson935a2f72017-02-13 17:15:13 +00005949#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5950#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005951#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005952#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005953#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005954#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005955#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005956#endif