blob: 631a2db2bb6e2af1f12b139bf7c1730286dd9311 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Matthew Auld465c4032017-10-06 23:18:14 +010038#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000039#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000040#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010041#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070042#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000044#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070045#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080046#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020047#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070048
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010049static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010050
Chris Wilson2c225692013-08-09 12:26:45 +010051static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
52{
Chris Wilsone27ab732017-06-15 13:38:49 +010053 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053054 return false;
55
Chris Wilsonb8f55be2017-08-11 12:11:16 +010056 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010057 return true;
58
Chris Wilsonbd3d2252017-10-13 21:26:14 +010059 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010060}
61
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053062static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010063insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053064 struct drm_mm_node *node, u32 size)
65{
66 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000067 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
68 size, 0, I915_COLOR_UNEVICTABLE,
69 0, ggtt->mappable_end,
70 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053071}
72
73static void
74remove_mappable_node(struct drm_mm_node *node)
75{
76 drm_mm_remove_node(node);
77}
78
Chris Wilson73aa8082010-09-30 11:46:12 +010079/* some bookkeeping */
80static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010081 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010082{
Daniel Vetterc20e8352013-07-24 22:40:23 +020083 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010084 dev_priv->mm.object_count++;
85 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020086 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010087}
88
89static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010090 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010091{
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093 dev_priv->mm.object_count--;
94 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096}
97
Chris Wilson21dd3732011-01-26 15:55:56 +000098static int
Daniel Vetter33196de2012-11-14 17:14:05 +010099i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101 int ret;
102
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100103 might_sleep();
104
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200105 /*
106 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
107 * userspace. If it takes that long something really bad is going on and
108 * we should simply try to bail out and fail as gracefully as possible.
109 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100110 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000111 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100112 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 if (ret == 0) {
114 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
115 return -EIO;
116 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100118 } else {
119 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200120 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121}
122
Chris Wilson54cf91d2010-11-25 18:00:26 +0000123int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100124{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100125 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100126 int ret;
127
Daniel Vetter33196de2012-11-14 17:14:05 +0100128 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 if (ret)
130 return ret;
131
132 ret = mutex_lock_interruptible(&dev->struct_mutex);
133 if (ret)
134 return ret;
135
Chris Wilson76c1dec2010-09-25 11:22:51 +0100136 return 0;
137}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100138
Eric Anholt673a3942008-07-30 12:06:12 -0700139int
Eric Anholt5a125c32008-10-22 21:40:13 -0700140i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000141 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700142{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300143 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200144 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300145 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100146 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800147 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700148
Weinan Liff8f7972017-05-31 10:35:52 +0800149 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100150 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000151 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100152 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100153 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000154 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100155 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100156 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100157 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300159 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000161
Eric Anholt5a125c32008-10-22 21:40:13 -0700162 return 0;
163}
164
Matthew Auldb91b09e2017-10-06 23:18:17 +0100165static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100166{
Al Viro93c76a32015-12-04 23:45:44 -0500167 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000168 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800169 struct sg_table *st;
170 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000171 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800172 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100173 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100174
Chris Wilson6a2c4232014-11-04 04:51:40 -0800175 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100176 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100177
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 /* Always aligning to the object size, allows a single allocation
179 * to handle all possible callers, and given typical object sizes,
180 * the alignment of the buddy allocation will naturally match.
181 */
182 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300183 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000184 roundup_pow_of_two(obj->base.size));
185 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100186 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000187
188 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800189 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
190 struct page *page;
191 char *src;
192
193 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000194 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100195 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000196 goto err_phys;
197 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800198
199 src = kmap_atomic(page);
200 memcpy(vaddr, src, PAGE_SIZE);
201 drm_clflush_virt_range(vaddr, PAGE_SIZE);
202 kunmap_atomic(src);
203
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300204 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800205 vaddr += PAGE_SIZE;
206 }
207
Chris Wilsonc0336662016-05-06 15:40:21 +0100208 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800209
210 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000211 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100212 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000213 goto err_phys;
214 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800215
216 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
217 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100218 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000219 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800220 }
221
222 sg = st->sgl;
223 sg->offset = 0;
224 sg->length = obj->base.size;
225
Chris Wilsondbb43512016-12-07 13:34:11 +0000226 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800227 sg_dma_len(sg) = obj->base.size;
228
Chris Wilsondbb43512016-12-07 13:34:11 +0000229 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100230
Matthew Aulda5c081662017-10-06 23:18:18 +0100231 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100232
233 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000234
235err_phys:
236 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100237
238 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239}
240
Chris Wilsone27ab732017-06-15 13:38:49 +0100241static void __start_cpu_write(struct drm_i915_gem_object *obj)
242{
Christian Königc0a51fd2018-02-16 13:43:38 +0100243 obj->read_domains = I915_GEM_DOMAIN_CPU;
244 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100245 if (cpu_write_needs_clflush(obj))
246 obj->cache_dirty = true;
247}
248
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000250__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000251 struct sg_table *pages,
252 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800253{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100254 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800255
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100256 if (obj->mm.madv == I915_MADV_DONTNEED)
257 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800258
Chris Wilsone5facdf2016-12-23 14:57:57 +0000259 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100260 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100261 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000262 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100263
Chris Wilsone27ab732017-06-15 13:38:49 +0100264 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100265}
266
267static void
268i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
269 struct sg_table *pages)
270{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000271 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100272
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100273 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500274 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100276 int i;
277
278 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800279 struct page *page;
280 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100281
Chris Wilson6a2c4232014-11-04 04:51:40 -0800282 page = shmem_read_mapping_page(mapping, i);
283 if (IS_ERR(page))
284 continue;
285
286 dst = kmap_atomic(page);
287 drm_clflush_virt_range(vaddr, PAGE_SIZE);
288 memcpy(dst, vaddr, PAGE_SIZE);
289 kunmap_atomic(dst);
290
291 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100292 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100293 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100295 vaddr += PAGE_SIZE;
296 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100297 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100298 }
299
Chris Wilson03ac84f2016-10-28 13:58:36 +0100300 sg_free_table(pages);
301 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000302
303 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800304}
305
306static void
307i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
308{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100309 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800310}
311
312static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
313 .get_pages = i915_gem_object_get_pages_phys,
314 .put_pages = i915_gem_object_put_pages_phys,
315 .release = i915_gem_object_release_phys,
316};
317
Chris Wilson581ab1f2017-02-15 16:39:00 +0000318static const struct drm_i915_gem_object_ops i915_gem_object_ops;
319
Chris Wilson35a96112016-08-14 18:44:40 +0100320int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100321{
322 struct i915_vma *vma;
323 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100324 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100325
Chris Wilson02bef8f2016-08-14 18:44:41 +0100326 lockdep_assert_held(&obj->base.dev->struct_mutex);
327
328 /* Closed vma are removed from the obj->vma_list - but they may
329 * still have an active binding on the object. To remove those we
330 * must wait for all rendering to complete to the object (as unbinding
331 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100332 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000333 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100334 if (ret)
335 return ret;
336
Chris Wilsonaa653a62016-08-04 07:52:27 +0100337 while ((vma = list_first_entry_or_null(&obj->vma_list,
338 struct i915_vma,
339 obj_link))) {
340 list_move_tail(&vma->obj_link, &still_in_list);
341 ret = i915_vma_unbind(vma);
342 if (ret)
343 break;
344 }
345 list_splice(&still_in_list, &obj->vma_list);
346
347 return ret;
348}
349
Chris Wilsone95433c2016-10-28 13:58:27 +0100350static long
351i915_gem_object_wait_fence(struct dma_fence *fence,
352 unsigned int flags,
353 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100354 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100355{
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 Wilson7125397b2017-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 Wilson7125397b2017-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 Wilson7125397b2017-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 Wilson7125397b2017-12-06 12:49:14 +0000688
Chris Wilsonef749212017-04-12 12:01:10 +0100689 wmb();
690
Chris Wilson7125397b2017-12-06 12:49:14 +0000691 intel_runtime_pm_get(dev_priv);
692 spin_lock_irq(&dev_priv->uncore.lock);
693
694 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
695
696 spin_unlock_irq(&dev_priv->uncore.lock);
697 intel_runtime_pm_put(dev_priv);
698}
699
700static void
701flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
702{
703 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
704 struct i915_vma *vma;
705
Christian Königc0a51fd2018-02-16 13:43:38 +0100706 if (!(obj->write_domain & flush_domains))
Chris Wilson7125397b2017-12-06 12:49:14 +0000707 return;
708
Christian Königc0a51fd2018-02-16 13:43:38 +0100709 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100710 case I915_GEM_DOMAIN_GTT:
Chris Wilson7125397b2017-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 Wilson7125397b2017-12-06 12:49:14 +0000715
Chris Wilsone2189dd2017-12-07 21:14:07 +0000716 for_each_ggtt_vma(vma, obj) {
Chris Wilson7125397b2017-12-06 12:49:14 +0000717 if (vma->iomap)
718 continue;
719
720 i915_vma_unset_ggtt_write(vma);
721 }
Chris Wilsonef749212017-04-12 12:01:10 +0100722 break;
723
724 case I915_GEM_DOMAIN_CPU:
725 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
726 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100727
728 case I915_GEM_DOMAIN_RENDER:
729 if (gpu_write_needs_clflush(obj))
730 obj->cache_dirty = true;
731 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100732 }
733
Christian Königc0a51fd2018-02-16 13:43:38 +0100734 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100735}
736
Daniel Vetter8c599672011-12-14 13:57:31 +0100737static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100738__copy_to_user_swizzled(char __user *cpu_vaddr,
739 const char *gpu_vaddr, int gpu_offset,
740 int length)
741{
742 int ret, cpu_offset = 0;
743
744 while (length > 0) {
745 int cacheline_end = ALIGN(gpu_offset + 1, 64);
746 int this_length = min(cacheline_end - gpu_offset, length);
747 int swizzled_gpu_offset = gpu_offset ^ 64;
748
749 ret = __copy_to_user(cpu_vaddr + cpu_offset,
750 gpu_vaddr + swizzled_gpu_offset,
751 this_length);
752 if (ret)
753 return ret + length;
754
755 cpu_offset += this_length;
756 gpu_offset += this_length;
757 length -= this_length;
758 }
759
760 return 0;
761}
762
763static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700764__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
765 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100766 int length)
767{
768 int ret, cpu_offset = 0;
769
770 while (length > 0) {
771 int cacheline_end = ALIGN(gpu_offset + 1, 64);
772 int this_length = min(cacheline_end - gpu_offset, length);
773 int swizzled_gpu_offset = gpu_offset ^ 64;
774
775 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
776 cpu_vaddr + cpu_offset,
777 this_length);
778 if (ret)
779 return ret + length;
780
781 cpu_offset += this_length;
782 gpu_offset += this_length;
783 length -= this_length;
784 }
785
786 return 0;
787}
788
Brad Volkin4c914c02014-02-18 10:15:45 -0800789/*
790 * Pins the specified object's pages and synchronizes the object with
791 * GPU accesses. Sets needs_clflush to non-zero if the caller should
792 * flush the object from the CPU cache.
793 */
794int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100795 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800796{
797 int ret;
798
Chris Wilsone95433c2016-10-28 13:58:27 +0100799 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800800
Chris Wilsone95433c2016-10-28 13:58:27 +0100801 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100802 if (!i915_gem_object_has_struct_page(obj))
803 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800804
Chris Wilsone95433c2016-10-28 13:58:27 +0100805 ret = i915_gem_object_wait(obj,
806 I915_WAIT_INTERRUPTIBLE |
807 I915_WAIT_LOCKED,
808 MAX_SCHEDULE_TIMEOUT,
809 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100810 if (ret)
811 return ret;
812
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100813 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100814 if (ret)
815 return ret;
816
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100817 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
818 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000819 ret = i915_gem_object_set_to_cpu_domain(obj, false);
820 if (ret)
821 goto err_unpin;
822 else
823 goto out;
824 }
825
Chris Wilsonef749212017-04-12 12:01:10 +0100826 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100827
Chris Wilson43394c72016-08-18 17:16:47 +0100828 /* If we're not in the cpu read domain, set ourself into the gtt
829 * read domain and manually flush cachelines (if required). This
830 * optimizes for the case when the gpu will dirty the data
831 * anyway again before the next pread happens.
832 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100833 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100834 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000835 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800836
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000837out:
Chris Wilson97649512016-08-18 17:16:50 +0100838 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100839 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100840
841err_unpin:
842 i915_gem_object_unpin_pages(obj);
843 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100844}
845
846int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
847 unsigned int *needs_clflush)
848{
849 int ret;
850
Chris Wilsone95433c2016-10-28 13:58:27 +0100851 lockdep_assert_held(&obj->base.dev->struct_mutex);
852
Chris Wilson43394c72016-08-18 17:16:47 +0100853 *needs_clflush = 0;
854 if (!i915_gem_object_has_struct_page(obj))
855 return -ENODEV;
856
Chris Wilsone95433c2016-10-28 13:58:27 +0100857 ret = i915_gem_object_wait(obj,
858 I915_WAIT_INTERRUPTIBLE |
859 I915_WAIT_LOCKED |
860 I915_WAIT_ALL,
861 MAX_SCHEDULE_TIMEOUT,
862 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100863 if (ret)
864 return ret;
865
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100866 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100867 if (ret)
868 return ret;
869
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100870 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
871 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000872 ret = i915_gem_object_set_to_cpu_domain(obj, true);
873 if (ret)
874 goto err_unpin;
875 else
876 goto out;
877 }
878
Chris Wilsonef749212017-04-12 12:01:10 +0100879 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100880
Chris Wilson43394c72016-08-18 17:16:47 +0100881 /* If we're not in the cpu write domain, set ourself into the
882 * gtt write domain and manually flush cachelines (as required).
883 * This optimizes for the case when the gpu will use the data
884 * right away and we therefore have to clflush anyway.
885 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100886 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000887 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100888
Chris Wilsone27ab732017-06-15 13:38:49 +0100889 /*
890 * Same trick applies to invalidate partially written
891 * cachelines read before writing.
892 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100893 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100894 *needs_clflush |= CLFLUSH_BEFORE;
895 }
Chris Wilson43394c72016-08-18 17:16:47 +0100896
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000897out:
Chris Wilson43394c72016-08-18 17:16:47 +0100898 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100899 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100900 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100901 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100902
903err_unpin:
904 i915_gem_object_unpin_pages(obj);
905 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800906}
907
Daniel Vetter23c18c72012-03-25 19:47:42 +0200908static void
909shmem_clflush_swizzled_range(char *addr, unsigned long length,
910 bool swizzled)
911{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200912 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200913 unsigned long start = (unsigned long) addr;
914 unsigned long end = (unsigned long) addr + length;
915
916 /* For swizzling simply ensure that we always flush both
917 * channels. Lame, but simple and it works. Swizzled
918 * pwrite/pread is far from a hotpath - current userspace
919 * doesn't use it at all. */
920 start = round_down(start, 128);
921 end = round_up(end, 128);
922
923 drm_clflush_virt_range((void *)start, end - start);
924 } else {
925 drm_clflush_virt_range(addr, length);
926 }
927
928}
929
Daniel Vetterd174bd62012-03-25 19:47:40 +0200930/* Only difference to the fast-path function is that this can handle bit17
931 * and uses non-atomic copy and kmap functions. */
932static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100933shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200934 char __user *user_data,
935 bool page_do_bit17_swizzling, bool needs_clflush)
936{
937 char *vaddr;
938 int ret;
939
940 vaddr = kmap(page);
941 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100942 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200943 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200944
945 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100946 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200947 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100948 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200949 kunmap(page);
950
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100951 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200952}
953
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100954static int
955shmem_pread(struct page *page, int offset, int length, char __user *user_data,
956 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530957{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100958 int ret;
959
960 ret = -ENODEV;
961 if (!page_do_bit17_swizzling) {
962 char *vaddr = kmap_atomic(page);
963
964 if (needs_clflush)
965 drm_clflush_virt_range(vaddr + offset, length);
966 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
967 kunmap_atomic(vaddr);
968 }
969 if (ret == 0)
970 return 0;
971
972 return shmem_pread_slow(page, offset, length, user_data,
973 page_do_bit17_swizzling, needs_clflush);
974}
975
976static int
977i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
978 struct drm_i915_gem_pread *args)
979{
980 char __user *user_data;
981 u64 remain;
982 unsigned int obj_do_bit17_swizzling;
983 unsigned int needs_clflush;
984 unsigned int idx, offset;
985 int ret;
986
987 obj_do_bit17_swizzling = 0;
988 if (i915_gem_object_needs_bit17_swizzle(obj))
989 obj_do_bit17_swizzling = BIT(17);
990
991 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
992 if (ret)
993 return ret;
994
995 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
996 mutex_unlock(&obj->base.dev->struct_mutex);
997 if (ret)
998 return ret;
999
1000 remain = args->size;
1001 user_data = u64_to_user_ptr(args->data_ptr);
1002 offset = offset_in_page(args->offset);
1003 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1004 struct page *page = i915_gem_object_get_page(obj, idx);
1005 int length;
1006
1007 length = remain;
1008 if (offset + length > PAGE_SIZE)
1009 length = PAGE_SIZE - offset;
1010
1011 ret = shmem_pread(page, offset, length, user_data,
1012 page_to_phys(page) & obj_do_bit17_swizzling,
1013 needs_clflush);
1014 if (ret)
1015 break;
1016
1017 remain -= length;
1018 user_data += length;
1019 offset = 0;
1020 }
1021
1022 i915_gem_obj_finish_shmem_access(obj);
1023 return ret;
1024}
1025
1026static inline bool
1027gtt_user_read(struct io_mapping *mapping,
1028 loff_t base, int offset,
1029 char __user *user_data, int length)
1030{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001031 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001032 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301033
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301034 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001035 vaddr = io_mapping_map_atomic_wc(mapping, base);
1036 unwritten = __copy_to_user_inatomic(user_data,
1037 (void __force *)vaddr + offset,
1038 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001039 io_mapping_unmap_atomic(vaddr);
1040 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001041 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1042 unwritten = copy_to_user(user_data,
1043 (void __force *)vaddr + offset,
1044 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001045 io_mapping_unmap(vaddr);
1046 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301047 return unwritten;
1048}
1049
1050static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001051i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1052 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301053{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001054 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1055 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301056 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001057 struct i915_vma *vma;
1058 void __user *user_data;
1059 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301060 int ret;
1061
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001062 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1063 if (ret)
1064 return ret;
1065
1066 intel_runtime_pm_get(i915);
1067 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001068 PIN_MAPPABLE |
1069 PIN_NONFAULT |
1070 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001071 if (!IS_ERR(vma)) {
1072 node.start = i915_ggtt_offset(vma);
1073 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001074 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001075 if (ret) {
1076 i915_vma_unpin(vma);
1077 vma = ERR_PTR(ret);
1078 }
1079 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001080 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001081 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301082 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001083 goto out_unlock;
1084 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301085 }
1086
1087 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1088 if (ret)
1089 goto out_unpin;
1090
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001091 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301092
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001093 user_data = u64_to_user_ptr(args->data_ptr);
1094 remain = args->size;
1095 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301096
1097 while (remain > 0) {
1098 /* Operation in this page
1099 *
1100 * page_base = page offset within aperture
1101 * page_offset = offset within page
1102 * page_length = bytes to copy for this page
1103 */
1104 u32 page_base = node.start;
1105 unsigned page_offset = offset_in_page(offset);
1106 unsigned page_length = PAGE_SIZE - page_offset;
1107 page_length = remain < page_length ? remain : page_length;
1108 if (node.allocated) {
1109 wmb();
1110 ggtt->base.insert_page(&ggtt->base,
1111 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001112 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301113 wmb();
1114 } else {
1115 page_base += offset & PAGE_MASK;
1116 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001117
Matthew Auld73ebd502017-12-11 15:18:20 +00001118 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001119 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301120 ret = -EFAULT;
1121 break;
1122 }
1123
1124 remain -= page_length;
1125 user_data += page_length;
1126 offset += page_length;
1127 }
1128
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001129 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301130out_unpin:
1131 if (node.allocated) {
1132 wmb();
1133 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001134 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301135 remove_mappable_node(&node);
1136 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001137 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301138 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001139out_unlock:
1140 intel_runtime_pm_put(i915);
1141 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001142
Eric Anholteb014592009-03-10 11:44:52 -07001143 return ret;
1144}
1145
Eric Anholt673a3942008-07-30 12:06:12 -07001146/**
1147 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001148 * @dev: drm device pointer
1149 * @data: ioctl data blob
1150 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001151 *
1152 * On error, the contents of *data are undefined.
1153 */
1154int
1155i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001156 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001157{
1158 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001159 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001160 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001161
Chris Wilson51311d02010-11-17 09:10:42 +00001162 if (args->size == 0)
1163 return 0;
1164
1165 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001166 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001167 args->size))
1168 return -EFAULT;
1169
Chris Wilson03ac0642016-07-20 13:31:51 +01001170 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001171 if (!obj)
1172 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001173
Chris Wilson7dcd2492010-09-26 20:21:44 +01001174 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001175 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001176 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001177 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001178 }
1179
Chris Wilsondb53a302011-02-03 11:57:46 +00001180 trace_i915_gem_object_pread(obj, args->offset, args->size);
1181
Chris Wilsone95433c2016-10-28 13:58:27 +01001182 ret = i915_gem_object_wait(obj,
1183 I915_WAIT_INTERRUPTIBLE,
1184 MAX_SCHEDULE_TIMEOUT,
1185 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001186 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001187 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001188
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001189 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001190 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001191 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001192
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001193 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001194 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001195 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301196
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 i915_gem_object_unpin_pages(obj);
1198out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001199 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001200 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001201}
1202
Keith Packard0839ccb2008-10-30 19:38:48 -07001203/* This is the fast write path which cannot handle
1204 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001205 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001206
Chris Wilsonfe115622016-10-28 13:58:40 +01001207static inline bool
1208ggtt_write(struct io_mapping *mapping,
1209 loff_t base, int offset,
1210 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001211{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001212 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001213 unsigned long unwritten;
1214
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001215 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001216 vaddr = io_mapping_map_atomic_wc(mapping, base);
1217 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001218 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001219 io_mapping_unmap_atomic(vaddr);
1220 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001221 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1222 unwritten = copy_from_user((void __force *)vaddr + offset,
1223 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001224 io_mapping_unmap(vaddr);
1225 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001226
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001227 return unwritten;
1228}
1229
Eric Anholt3de09aa2009-03-09 09:42:23 -07001230/**
1231 * This is the fast pwrite path, where we copy the data directly from the
1232 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001233 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001234 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001235 */
Eric Anholt673a3942008-07-30 12:06:12 -07001236static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001237i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1238 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001239{
Chris Wilsonfe115622016-10-28 13:58:40 +01001240 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301241 struct i915_ggtt *ggtt = &i915->ggtt;
1242 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001243 struct i915_vma *vma;
1244 u64 remain, offset;
1245 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301246 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301247
Chris Wilsonfe115622016-10-28 13:58:40 +01001248 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1249 if (ret)
1250 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001251
Chris Wilson8bd818152017-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 Wilson8bd818152017-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 Wilson8bd818152017-10-19 07:37:33 +01001348out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001349 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-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 Wilson7125397b2017-12-06 12:49:14 +00001981 i915_vma_set_ggtt_write(vma);
1982
Chris Wilsona65adaf2017-10-09 09:43:57 +01001983err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001984 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001985err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001986 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001987err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001988 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001989err_rpm:
1990 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001991 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001992err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001993 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001994 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001995 /*
1996 * We eat errors when the gpu is terminally wedged to avoid
1997 * userspace unduly crashing (gl has no provisions for mmaps to
1998 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1999 * and so needs to be reported.
2000 */
2001 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002002 ret = VM_FAULT_SIGBUS;
2003 break;
2004 }
Chris Wilson045e7692010-11-07 09:18:22 +00002005 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002006 /*
2007 * EAGAIN means the gpu is hung and we'll wait for the error
2008 * handler to reset everything when re-faulting in
2009 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002010 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002011 case 0:
2012 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002013 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002014 case -EBUSY:
2015 /*
2016 * EBUSY is ok: this just means that another thread
2017 * already did the job.
2018 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002019 ret = VM_FAULT_NOPAGE;
2020 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002021 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002022 ret = VM_FAULT_OOM;
2023 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002024 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002025 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002026 ret = VM_FAULT_SIGBUS;
2027 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002028 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002029 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002030 ret = VM_FAULT_SIGBUS;
2031 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002032 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002033 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002034}
2035
Chris Wilsona65adaf2017-10-09 09:43:57 +01002036static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2037{
2038 struct i915_vma *vma;
2039
2040 GEM_BUG_ON(!obj->userfault_count);
2041
2042 obj->userfault_count = 0;
2043 list_del(&obj->userfault_link);
2044 drm_vma_node_unmap(&obj->base.vma_node,
2045 obj->base.dev->anon_inode->i_mapping);
2046
Chris Wilsone2189dd2017-12-07 21:14:07 +00002047 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002048 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002049}
2050
Jesse Barnesde151cf2008-11-12 10:03:55 -08002051/**
Chris Wilson901782b2009-07-10 08:18:50 +01002052 * i915_gem_release_mmap - remove physical page mappings
2053 * @obj: obj in question
2054 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002055 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002056 * relinquish ownership of the pages back to the system.
2057 *
2058 * It is vital that we remove the page mapping if we have mapped a tiled
2059 * object through the GTT and then lose the fence register due to
2060 * resource pressure. Similarly if the object has been moved out of the
2061 * aperture, than pages mapped into userspace must be revoked. Removing the
2062 * mapping will then trigger a page fault on the next user access, allowing
2063 * fixup by i915_gem_fault().
2064 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002065void
Chris Wilson05394f32010-11-08 19:18:58 +00002066i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002067{
Chris Wilson275f0392016-10-24 13:42:14 +01002068 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002069
Chris Wilson349f2cc2016-04-13 17:35:12 +01002070 /* Serialisation between user GTT access and our code depends upon
2071 * revoking the CPU's PTE whilst the mutex is held. The next user
2072 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002073 *
2074 * Note that RPM complicates somewhat by adding an additional
2075 * requirement that operations to the GGTT be made holding the RPM
2076 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002077 */
Chris Wilson275f0392016-10-24 13:42:14 +01002078 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002079 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002080
Chris Wilsona65adaf2017-10-09 09:43:57 +01002081 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002082 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002083
Chris Wilsona65adaf2017-10-09 09:43:57 +01002084 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002085
2086 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2087 * memory transactions from userspace before we return. The TLB
2088 * flushing implied above by changing the PTE above *should* be
2089 * sufficient, an extra barrier here just provides us with a bit
2090 * of paranoid documentation about our requirement to serialise
2091 * memory writes before touching registers / GSM.
2092 */
2093 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002094
2095out:
2096 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002097}
2098
Chris Wilson7c108fd2016-10-24 13:42:18 +01002099void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002100{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002101 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002102 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002103
Chris Wilson3594a3e2016-10-24 13:42:16 +01002104 /*
2105 * Only called during RPM suspend. All users of the userfault_list
2106 * must be holding an RPM wakeref to ensure that this can not
2107 * run concurrently with themselves (and use the struct_mutex for
2108 * protection between themselves).
2109 */
2110
2111 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002112 &dev_priv->mm.userfault_list, userfault_link)
2113 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002114
2115 /* The fence will be lost when the device powers down. If any were
2116 * in use by hardware (i.e. they are pinned), we should not be powering
2117 * down! All other fences will be reacquired by the user upon waking.
2118 */
2119 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2120 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2121
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002122 /* Ideally we want to assert that the fence register is not
2123 * live at this point (i.e. that no piece of code will be
2124 * trying to write through fence + GTT, as that both violates
2125 * our tracking of activity and associated locking/barriers,
2126 * but also is illegal given that the hw is powered down).
2127 *
2128 * Previously we used reg->pin_count as a "liveness" indicator.
2129 * That is not sufficient, and we need a more fine-grained
2130 * tool if we want to have a sanity check here.
2131 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002132
2133 if (!reg->vma)
2134 continue;
2135
Chris Wilsona65adaf2017-10-09 09:43:57 +01002136 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002137 reg->dirty = true;
2138 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002139}
2140
Chris Wilsond8cb5082012-08-11 15:41:03 +01002141static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2142{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002143 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002144 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002145
Chris Wilsonf3f61842016-08-05 10:14:14 +01002146 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002147 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002148 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002149
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002150 /* Attempt to reap some mmap space from dead objects */
2151 do {
2152 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2153 if (err)
2154 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002155
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002156 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002157 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002158 if (!err)
2159 break;
2160
2161 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002162
Chris Wilsonf3f61842016-08-05 10:14:14 +01002163 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002164}
2165
2166static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2167{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002168 drm_gem_free_mmap_offset(&obj->base);
2169}
2170
Dave Airlieda6b51d2014-12-24 13:11:17 +10002171int
Dave Airlieff72145b2011-02-07 12:16:14 +10002172i915_gem_mmap_gtt(struct drm_file *file,
2173 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002174 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002175 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002176{
Chris Wilson05394f32010-11-08 19:18:58 +00002177 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002178 int ret;
2179
Chris Wilson03ac0642016-07-20 13:31:51 +01002180 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002181 if (!obj)
2182 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002183
Chris Wilsond8cb5082012-08-11 15:41:03 +01002184 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002185 if (ret == 0)
2186 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002187
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002188 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002189 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002190}
2191
Dave Airlieff72145b2011-02-07 12:16:14 +10002192/**
2193 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2194 * @dev: DRM device
2195 * @data: GTT mapping ioctl data
2196 * @file: GEM object info
2197 *
2198 * Simply returns the fake offset to userspace so it can mmap it.
2199 * The mmap call will end up in drm_gem_mmap(), which will set things
2200 * up so we can get faults in the handler above.
2201 *
2202 * The fault handler will take care of binding the object into the GTT
2203 * (since it may have been evicted to make room for something), allocating
2204 * a fence register, and mapping the appropriate aperture address into
2205 * userspace.
2206 */
2207int
2208i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2209 struct drm_file *file)
2210{
2211 struct drm_i915_gem_mmap_gtt *args = data;
2212
Dave Airlieda6b51d2014-12-24 13:11:17 +10002213 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002214}
2215
Daniel Vetter225067e2012-08-20 10:23:20 +02002216/* Immediately discard the backing storage */
2217static void
2218i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002219{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002220 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002221
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002222 if (obj->base.filp == NULL)
2223 return;
2224
Daniel Vetter225067e2012-08-20 10:23:20 +02002225 /* Our goal here is to return as much of the memory as
2226 * is possible back to the system as we are called from OOM.
2227 * To do this we must instruct the shmfs to drop all of its
2228 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002229 */
Chris Wilson55372522014-03-25 13:23:06 +00002230 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002231 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002232 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002233}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002234
Chris Wilson55372522014-03-25 13:23:06 +00002235/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002236void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002237{
Chris Wilson55372522014-03-25 13:23:06 +00002238 struct address_space *mapping;
2239
Chris Wilson1233e2d2016-10-28 13:58:37 +01002240 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002241 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002242
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002243 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002244 case I915_MADV_DONTNEED:
2245 i915_gem_object_truncate(obj);
2246 case __I915_MADV_PURGED:
2247 return;
2248 }
2249
2250 if (obj->base.filp == NULL)
2251 return;
2252
Al Viro93c76a32015-12-04 23:45:44 -05002253 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002254 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002255}
2256
Chris Wilson5cdf5882010-09-27 15:51:07 +01002257static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002258i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2259 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002260{
Dave Gordon85d12252016-05-20 11:54:06 +01002261 struct sgt_iter sgt_iter;
2262 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002263
Chris Wilsone5facdf2016-12-23 14:57:57 +00002264 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002265
Chris Wilson03ac84f2016-10-28 13:58:36 +01002266 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002267
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002268 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002269 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002270
Chris Wilson03ac84f2016-10-28 13:58:36 +01002271 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002272 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002273 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002274
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002275 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002276 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002277
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002278 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002279 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002280 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002281
Chris Wilson03ac84f2016-10-28 13:58:36 +01002282 sg_free_table(pages);
2283 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002284}
2285
Chris Wilson96d77632016-10-28 13:58:33 +01002286static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2287{
2288 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002289 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002290
Chris Wilsonbea6e982017-10-26 14:00:31 +01002291 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002292 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2293 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002294 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002295}
2296
Chris Wilson548625e2016-11-01 12:11:34 +00002297void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2298 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002299{
Chris Wilsonf2123812017-10-16 12:40:37 +01002300 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002301 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002302
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002303 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002304 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002305
Chris Wilson15717de2016-08-04 07:52:26 +01002306 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002307 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002308 return;
2309
2310 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002311 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002312 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2313 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002314
Chris Wilsona2165e32012-12-03 11:49:00 +00002315 /* ->put_pages might need to allocate memory for the bit17 swizzle
2316 * array, hence protect them from being reaped by removing them from gtt
2317 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002318 pages = fetch_and_zero(&obj->mm.pages);
2319 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002320
Chris Wilsonf2123812017-10-16 12:40:37 +01002321 spin_lock(&i915->mm.obj_lock);
2322 list_del(&obj->mm.link);
2323 spin_unlock(&i915->mm.obj_lock);
2324
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002325 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002326 void *ptr;
2327
Chris Wilson0ce81782017-05-17 13:09:59 +01002328 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002329 if (is_vmalloc_addr(ptr))
2330 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002331 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002332 kunmap(kmap_to_page(ptr));
2333
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002334 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002335 }
2336
Chris Wilson96d77632016-10-28 13:58:33 +01002337 __i915_gem_object_reset_page_iter(obj);
2338
Chris Wilson4e5462e2017-03-07 13:20:31 +00002339 if (!IS_ERR(pages))
2340 obj->ops->put_pages(obj, pages);
2341
Matthew Aulda5c081662017-10-06 23:18:18 +01002342 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2343
Chris Wilson1233e2d2016-10-28 13:58:37 +01002344unlock:
2345 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002346}
2347
Chris Wilson935a2f72017-02-13 17:15:13 +00002348static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002349{
2350 struct sg_table new_st;
2351 struct scatterlist *sg, *new_sg;
2352 unsigned int i;
2353
2354 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002355 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002356
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002357 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002358 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002359
2360 new_sg = new_st.sgl;
2361 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2362 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2363 /* called before being DMA mapped, no need to copy sg->dma_* */
2364 new_sg = sg_next(new_sg);
2365 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002366 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002367
2368 sg_free_table(orig_st);
2369
2370 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002371 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002372}
2373
Matthew Auldb91b09e2017-10-06 23:18:17 +01002374static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002375{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002376 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002377 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2378 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002379 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002380 struct sg_table *st;
2381 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002382 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002383 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002384 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002385 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002386 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002387 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002388 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002389
Chris Wilson6c085a72012-08-20 11:40:46 +02002390 /* Assert that the object is not currently in any GPU domain. As it
2391 * wasn't in the GTT, there shouldn't be any way it could have been in
2392 * a GPU cache
2393 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002394 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2395 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002396
Chris Wilson9da3da62012-06-01 15:20:22 +01002397 st = kmalloc(sizeof(*st), GFP_KERNEL);
2398 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002399 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002400
Chris Wilsond766ef52016-12-19 12:43:45 +00002401rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002402 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002403 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002404 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002405 }
2406
2407 /* Get the list of pages out of our struct file. They'll be pinned
2408 * at this point until we release them.
2409 *
2410 * Fail silently without starting the shrinker
2411 */
Al Viro93c76a32015-12-04 23:45:44 -05002412 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002413 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002414 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2415
Imre Deak90797e62013-02-18 19:28:03 +02002416 sg = st->sgl;
2417 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002418 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002419 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002420 const unsigned int shrink[] = {
2421 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2422 0,
2423 }, *s = shrink;
2424 gfp_t gfp = noreclaim;
2425
2426 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002427 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002428 if (likely(!IS_ERR(page)))
2429 break;
2430
2431 if (!*s) {
2432 ret = PTR_ERR(page);
2433 goto err_sg;
2434 }
2435
Chris Wilson912d5722017-09-06 16:19:30 -07002436 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002437 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002438
Chris Wilson6c085a72012-08-20 11:40:46 +02002439 /* We've tried hard to allocate the memory by reaping
2440 * our own buffer, now let the real VM do its job and
2441 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002442 *
2443 * However, since graphics tend to be disposable,
2444 * defer the oom here by reporting the ENOMEM back
2445 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002446 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002447 if (!*s) {
2448 /* reclaim and warn, but no oom */
2449 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002450
2451 /* Our bo are always dirty and so we require
2452 * kswapd to reclaim our pages (direct reclaim
2453 * does not effectively begin pageout of our
2454 * buffers on its own). However, direct reclaim
2455 * only waits for kswapd when under allocation
2456 * congestion. So as a result __GFP_RECLAIM is
2457 * unreliable and fails to actually reclaim our
2458 * dirty pages -- unless you try over and over
2459 * again with !__GFP_NORETRY. However, we still
2460 * want to fail this allocation rather than
2461 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002462 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002463 */
Michal Hockodbb32952017-07-12 14:36:55 -07002464 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002465 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002466 } while (1);
2467
Chris Wilson871dfbd2016-10-11 09:20:21 +01002468 if (!i ||
2469 sg->length >= max_segment ||
2470 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002471 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002472 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002473 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002474 }
Imre Deak90797e62013-02-18 19:28:03 +02002475 st->nents++;
2476 sg_set_page(sg, page, PAGE_SIZE, 0);
2477 } else {
2478 sg->length += PAGE_SIZE;
2479 }
2480 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002481
2482 /* Check that the i965g/gm workaround works. */
2483 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002484 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002485 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002486 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002487 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002488 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002489
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002490 /* Trim unused sg entries to avoid wasting memory. */
2491 i915_sg_trim(st);
2492
Chris Wilson03ac84f2016-10-28 13:58:36 +01002493 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002494 if (ret) {
2495 /* DMA remapping failed? One possible cause is that
2496 * it could not reserve enough large entries, asking
2497 * for PAGE_SIZE chunks instead may be helpful.
2498 */
2499 if (max_segment > PAGE_SIZE) {
2500 for_each_sgt_page(page, sgt_iter, st)
2501 put_page(page);
2502 sg_free_table(st);
2503
2504 max_segment = PAGE_SIZE;
2505 goto rebuild_st;
2506 } else {
2507 dev_warn(&dev_priv->drm.pdev->dev,
2508 "Failed to DMA remap %lu pages\n",
2509 page_count);
2510 goto err_pages;
2511 }
2512 }
Imre Deake2273302015-07-09 12:59:05 +03002513
Eric Anholt673a3942008-07-30 12:06:12 -07002514 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002515 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002516
Matthew Auld84e89782017-10-09 12:00:24 +01002517 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002518
2519 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002520
Chris Wilsonb17993b2016-11-14 11:29:30 +00002521err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002522 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002523err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002524 for_each_sgt_page(page, sgt_iter, st)
2525 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002526 sg_free_table(st);
2527 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002528
2529 /* shmemfs first checks if there is enough memory to allocate the page
2530 * and reports ENOSPC should there be insufficient, along with the usual
2531 * ENOMEM for a genuine allocation failure.
2532 *
2533 * We use ENOSPC in our driver to mean that we have run out of aperture
2534 * space and so want to translate the error from shmemfs back to our
2535 * usual understanding of ENOMEM.
2536 */
Imre Deake2273302015-07-09 12:59:05 +03002537 if (ret == -ENOSPC)
2538 ret = -ENOMEM;
2539
Matthew Auldb91b09e2017-10-06 23:18:17 +01002540 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002541}
2542
2543void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002544 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002545 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002546{
Matthew Aulda5c081662017-10-06 23:18:18 +01002547 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2548 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2549 int i;
2550
Chris Wilson1233e2d2016-10-28 13:58:37 +01002551 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002552
2553 obj->mm.get_page.sg_pos = pages->sgl;
2554 obj->mm.get_page.sg_idx = 0;
2555
2556 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002557
2558 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002559 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002560 GEM_BUG_ON(obj->mm.quirked);
2561 __i915_gem_object_pin_pages(obj);
2562 obj->mm.quirked = true;
2563 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002564
Matthew Auld84e89782017-10-09 12:00:24 +01002565 GEM_BUG_ON(!sg_page_sizes);
2566 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002567
2568 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002569 * Calculate the supported page-sizes which fit into the given
2570 * sg_page_sizes. This will give us the page-sizes which we may be able
2571 * to use opportunistically when later inserting into the GTT. For
2572 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2573 * 64K or 4K pages, although in practice this will depend on a number of
2574 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002575 */
2576 obj->mm.page_sizes.sg = 0;
2577 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2578 if (obj->mm.page_sizes.phys & ~0u << i)
2579 obj->mm.page_sizes.sg |= BIT(i);
2580 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002581 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002582
2583 spin_lock(&i915->mm.obj_lock);
2584 list_add(&obj->mm.link, &i915->mm.unbound_list);
2585 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002586}
2587
2588static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2589{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002590 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002591
2592 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2593 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2594 return -EFAULT;
2595 }
2596
Matthew Auldb91b09e2017-10-06 23:18:17 +01002597 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002598 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002599
Matthew Auldb91b09e2017-10-06 23:18:17 +01002600 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002601}
2602
Chris Wilson37e680a2012-06-07 15:38:42 +01002603/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002604 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002605 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002606 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002607 * either as a result of memory pressure (reaping pages under the shrinker)
2608 * or as the object is itself released.
2609 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002610int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002611{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002612 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002613
Chris Wilson1233e2d2016-10-28 13:58:37 +01002614 err = mutex_lock_interruptible(&obj->mm.lock);
2615 if (err)
2616 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002617
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002618 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002619 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2620
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002621 err = ____i915_gem_object_get_pages(obj);
2622 if (err)
2623 goto unlock;
2624
2625 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002626 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002627 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002628
Chris Wilson1233e2d2016-10-28 13:58:37 +01002629unlock:
2630 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002631 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002632}
2633
Dave Gordondd6034c2016-05-20 11:54:04 +01002634/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002635static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2636 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002637{
2638 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002639 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002640 struct sgt_iter sgt_iter;
2641 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002642 struct page *stack_pages[32];
2643 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002644 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002645 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002646 void *addr;
2647
2648 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002649 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002650 return kmap(sg_page(sgt->sgl));
2651
Dave Gordonb338fa42016-05-20 11:54:05 +01002652 if (n_pages > ARRAY_SIZE(stack_pages)) {
2653 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002654 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002655 if (!pages)
2656 return NULL;
2657 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002658
Dave Gordon85d12252016-05-20 11:54:06 +01002659 for_each_sgt_page(page, sgt_iter, sgt)
2660 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002661
2662 /* Check that we have the expected number of pages */
2663 GEM_BUG_ON(i != n_pages);
2664
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002665 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002666 default:
2667 MISSING_CASE(type);
2668 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002669 case I915_MAP_WB:
2670 pgprot = PAGE_KERNEL;
2671 break;
2672 case I915_MAP_WC:
2673 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2674 break;
2675 }
2676 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002677
Dave Gordonb338fa42016-05-20 11:54:05 +01002678 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002679 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002680
2681 return addr;
2682}
2683
2684/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002685void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2686 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002687{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002688 enum i915_map_type has_type;
2689 bool pinned;
2690 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002691 int ret;
2692
Tina Zhanga03f3952017-11-14 10:25:13 +00002693 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2694 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002695
Chris Wilson1233e2d2016-10-28 13:58:37 +01002696 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002697 if (ret)
2698 return ERR_PTR(ret);
2699
Chris Wilsona575c672017-08-28 11:46:31 +01002700 pinned = !(type & I915_MAP_OVERRIDE);
2701 type &= ~I915_MAP_OVERRIDE;
2702
Chris Wilson1233e2d2016-10-28 13:58:37 +01002703 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002704 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002705 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2706
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002707 ret = ____i915_gem_object_get_pages(obj);
2708 if (ret)
2709 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002710
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002711 smp_mb__before_atomic();
2712 }
2713 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002714 pinned = false;
2715 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002716 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002717
Chris Wilson0ce81782017-05-17 13:09:59 +01002718 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002719 if (ptr && has_type != type) {
2720 if (pinned) {
2721 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002722 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002723 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002724
2725 if (is_vmalloc_addr(ptr))
2726 vunmap(ptr);
2727 else
2728 kunmap(kmap_to_page(ptr));
2729
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002730 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002731 }
2732
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002733 if (!ptr) {
2734 ptr = i915_gem_object_map(obj, type);
2735 if (!ptr) {
2736 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002737 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002738 }
2739
Chris Wilson0ce81782017-05-17 13:09:59 +01002740 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002741 }
2742
Chris Wilson1233e2d2016-10-28 13:58:37 +01002743out_unlock:
2744 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002745 return ptr;
2746
Chris Wilson1233e2d2016-10-28 13:58:37 +01002747err_unpin:
2748 atomic_dec(&obj->mm.pages_pin_count);
2749err_unlock:
2750 ptr = ERR_PTR(ret);
2751 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002752}
2753
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002754static int
2755i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2756 const struct drm_i915_gem_pwrite *arg)
2757{
2758 struct address_space *mapping = obj->base.filp->f_mapping;
2759 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2760 u64 remain, offset;
2761 unsigned int pg;
2762
2763 /* Before we instantiate/pin the backing store for our use, we
2764 * can prepopulate the shmemfs filp efficiently using a write into
2765 * the pagecache. We avoid the penalty of instantiating all the
2766 * pages, important if the user is just writing to a few and never
2767 * uses the object on the GPU, and using a direct write into shmemfs
2768 * allows it to avoid the cost of retrieving a page (either swapin
2769 * or clearing-before-use) before it is overwritten.
2770 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002771 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002772 return -ENODEV;
2773
Chris Wilsona6d65e42017-10-16 21:27:32 +01002774 if (obj->mm.madv != I915_MADV_WILLNEED)
2775 return -EFAULT;
2776
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002777 /* Before the pages are instantiated the object is treated as being
2778 * in the CPU domain. The pages will be clflushed as required before
2779 * use, and we can freely write into the pages directly. If userspace
2780 * races pwrite with any other operation; corruption will ensue -
2781 * that is userspace's prerogative!
2782 */
2783
2784 remain = arg->size;
2785 offset = arg->offset;
2786 pg = offset_in_page(offset);
2787
2788 do {
2789 unsigned int len, unwritten;
2790 struct page *page;
2791 void *data, *vaddr;
2792 int err;
2793
2794 len = PAGE_SIZE - pg;
2795 if (len > remain)
2796 len = remain;
2797
2798 err = pagecache_write_begin(obj->base.filp, mapping,
2799 offset, len, 0,
2800 &page, &data);
2801 if (err < 0)
2802 return err;
2803
2804 vaddr = kmap(page);
2805 unwritten = copy_from_user(vaddr + pg, user_data, len);
2806 kunmap(page);
2807
2808 err = pagecache_write_end(obj->base.filp, mapping,
2809 offset, len, len - unwritten,
2810 page, data);
2811 if (err < 0)
2812 return err;
2813
2814 if (unwritten)
2815 return -EFAULT;
2816
2817 remain -= len;
2818 user_data += len;
2819 offset += len;
2820 pg = 0;
2821 } while (remain);
2822
2823 return 0;
2824}
2825
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002826static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002827{
Chris Wilson77b25a92017-07-21 13:32:30 +01002828 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002829
Chris Wilson77b25a92017-07-21 13:32:30 +01002830 atomic_inc(&ctx->guilty_count);
2831
Chris Wilson24eae082018-02-05 09:22:01 +00002832 banned = false;
2833 if (i915_gem_context_is_bannable(ctx)) {
2834 unsigned int score;
2835
2836 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2837 &ctx->ban_score);
2838 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2839
2840 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2841 ctx->name, score, yesno(banned));
2842 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002843 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002844 return;
2845
Chris Wilson77b25a92017-07-21 13:32:30 +01002846 i915_gem_context_set_banned(ctx);
2847 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2848 atomic_inc(&ctx->file_priv->context_bans);
2849 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2850 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2851 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002852}
2853
2854static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2855{
Chris Wilson77b25a92017-07-21 13:32:30 +01002856 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002857}
2858
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002859struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002860i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002861{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002862 struct drm_i915_gem_request *request, *active = NULL;
2863 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002864
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002865 /* We are called by the error capture and reset at a random
2866 * point in time. In particular, note that neither is crucially
2867 * ordered with an interrupt. After a hang, the GPU is dead and we
2868 * assume that no more writes can happen (we waited long enough for
2869 * all writes that were in transaction to be flushed) - adding an
2870 * extra delay for a recent interrupt is pointless. Hence, we do
2871 * not need an engine->irq_seqno_barrier() before the seqno reads.
2872 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002873 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002874 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002875 if (__i915_gem_request_completed(request,
2876 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002877 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002878
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002879 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002880 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2881 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002882
Chris Wilson754c9fd2017-02-23 07:44:14 +00002883 active = request;
2884 break;
2885 }
2886 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2887
2888 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002889}
2890
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002891static bool engine_stalled(struct intel_engine_cs *engine)
2892{
2893 if (!engine->hangcheck.stalled)
2894 return false;
2895
2896 /* Check for possible seqno movement after hang declaration */
2897 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2898 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2899 return false;
2900 }
2901
2902 return true;
2903}
2904
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002905/*
2906 * Ensure irq handler finishes, and not run again.
2907 * Also return the active request so that we only search for it once.
2908 */
2909struct drm_i915_gem_request *
2910i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2911{
2912 struct drm_i915_gem_request *request = NULL;
2913
Chris Wilson1749d902017-10-09 12:02:59 +01002914 /*
2915 * During the reset sequence, we must prevent the engine from
2916 * entering RC6. As the context state is undefined until we restart
2917 * the engine, if it does enter RC6 during the reset, the state
2918 * written to the powercontext is undefined and so we may lose
2919 * GPU state upon resume, i.e. fail to restart after a reset.
2920 */
2921 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2922
2923 /*
2924 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002925 * state (by calling dma_fence_signal) as we are processing
2926 * the reset. The write from the GPU of the seqno is
2927 * asynchronous and the signaler thread may see a different
2928 * value to us and declare the request complete, even though
2929 * the reset routine have picked that request as the active
2930 * (incomplete) request. This conflict is not handled
2931 * gracefully!
2932 */
2933 kthread_park(engine->breadcrumbs.signaler);
2934
Chris Wilson1749d902017-10-09 12:02:59 +01002935 /*
2936 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002937 * completed the reset in i915_gem_reset_finish(). If a request
2938 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302939 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002940 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302941 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002942 * prevents the race.
2943 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302944 tasklet_kill(&engine->execlists.tasklet);
2945 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002946
Michał Winiarskic41937f2017-10-26 15:35:58 +02002947 /*
2948 * We're using worker to queue preemption requests from the tasklet in
2949 * GuC submission mode.
2950 * Even though tasklet was disabled, we may still have a worker queued.
2951 * Let's make sure that all workers scheduled before disabling the
2952 * tasklet are completed before continuing with the reset.
2953 */
2954 if (engine->i915->guc.preempt_wq)
2955 flush_workqueue(engine->i915->guc.preempt_wq);
2956
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002957 if (engine->irq_seqno_barrier)
2958 engine->irq_seqno_barrier(engine);
2959
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002960 request = i915_gem_find_active_request(engine);
2961 if (request && request->fence.error == -EIO)
2962 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002963
2964 return request;
2965}
2966
Chris Wilson0e178ae2017-01-17 17:59:06 +02002967int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002968{
2969 struct intel_engine_cs *engine;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002970 struct drm_i915_gem_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002971 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002972 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002973
Chris Wilson0e178ae2017-01-17 17:59:06 +02002974 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002975 request = i915_gem_reset_prepare_engine(engine);
2976 if (IS_ERR(request)) {
2977 err = PTR_ERR(request);
2978 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002979 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002980
2981 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002982 }
2983
Chris Wilson4c965542017-01-17 17:59:01 +02002984 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002985
2986 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002987}
2988
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002989static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002990{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002991 void *vaddr = request->ring->vaddr;
2992 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002993
Chris Wilson821ed7d2016-09-09 14:11:53 +01002994 /* As this request likely depends on state from the lost
2995 * context, clear out all the user operations leaving the
2996 * breadcrumb at the end (so we get the fence notifications).
2997 */
2998 head = request->head;
2999 if (request->postfix < head) {
3000 memset(vaddr + head, 0, request->ring->size - head);
3001 head = 0;
3002 }
3003 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003004
3005 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003006}
3007
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003008static void engine_skip_context(struct drm_i915_gem_request *request)
3009{
3010 struct intel_engine_cs *engine = request->engine;
3011 struct i915_gem_context *hung_ctx = request->ctx;
3012 struct intel_timeline *timeline;
3013 unsigned long flags;
3014
3015 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3016
3017 spin_lock_irqsave(&engine->timeline->lock, flags);
3018 spin_lock(&timeline->lock);
3019
3020 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3021 if (request->ctx == hung_ctx)
3022 skip_request(request);
3023
3024 list_for_each_entry(request, &timeline->requests, link)
3025 skip_request(request);
3026
3027 spin_unlock(&timeline->lock);
3028 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3029}
3030
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003031/* Returns the request if it was guilty of the hang */
3032static struct drm_i915_gem_request *
3033i915_gem_reset_request(struct intel_engine_cs *engine,
3034 struct drm_i915_gem_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003035{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003036 /* The guilty request will get skipped on a hung engine.
3037 *
3038 * Users of client default contexts do not rely on logical
3039 * state preserved between batches so it is safe to execute
3040 * queued requests following the hang. Non default contexts
3041 * rely on preserved state, so skipping a batch loses the
3042 * evolution of the state and it needs to be considered corrupted.
3043 * Executing more queued batches on top of corrupted state is
3044 * risky. But we take the risk by trying to advance through
3045 * the queued requests in order to make the client behaviour
3046 * more predictable around resets, by not throwing away random
3047 * amount of batches it has prepared for execution. Sophisticated
3048 * clients can use gem_reset_stats_ioctl and dma fence status
3049 * (exported via sync_file info ioctl on explicit fences) to observe
3050 * when it loses the context state and should rebuild accordingly.
3051 *
3052 * The context ban, and ultimately the client ban, mechanism are safety
3053 * valves if client submission ends up resulting in nothing more than
3054 * subsequent hangs.
3055 */
3056
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003057 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003058 i915_gem_context_mark_guilty(request->ctx);
3059 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003060
3061 /* If this context is now banned, skip all pending requests. */
3062 if (i915_gem_context_is_banned(request->ctx))
3063 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003064 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003065 /*
3066 * Since this is not the hung engine, it may have advanced
3067 * since the hang declaration. Double check by refinding
3068 * the active request at the time of the reset.
3069 */
3070 request = i915_gem_find_active_request(engine);
3071 if (request) {
3072 i915_gem_context_mark_innocent(request->ctx);
3073 dma_fence_set_error(&request->fence, -EAGAIN);
3074
3075 /* Rewind the engine to replay the incomplete rq */
3076 spin_lock_irq(&engine->timeline->lock);
3077 request = list_prev_entry(request, link);
3078 if (&request->link == &engine->timeline->requests)
3079 request = NULL;
3080 spin_unlock_irq(&engine->timeline->lock);
3081 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003082 }
3083
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003084 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003085}
3086
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003087void i915_gem_reset_engine(struct intel_engine_cs *engine,
3088 struct drm_i915_gem_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003089{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003090 /*
3091 * Make sure this write is visible before we re-enable the interrupt
3092 * handlers on another CPU, as tasklet_enable() resolves to just
3093 * a compiler barrier which is insufficient for our purpose here.
3094 */
3095 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003096
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003097 if (request)
3098 request = i915_gem_reset_request(engine, request);
3099
3100 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003101 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3102 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003103 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003104
3105 /* Setup the CS to resume from the breadcrumb of the hung request */
3106 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003107}
3108
Chris Wilsond8027092017-02-08 14:30:32 +00003109void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003110{
3111 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303112 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003113
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003114 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3115
Chris Wilson821ed7d2016-09-09 14:11:53 +01003116 i915_gem_retire_requests(dev_priv);
3117
Chris Wilson2ae55732017-02-12 17:20:02 +00003118 for_each_engine(engine, dev_priv, id) {
3119 struct i915_gem_context *ctx;
3120
Michel Thierryc64992e2017-06-20 10:57:44 +01003121 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003122 ctx = fetch_and_zero(&engine->last_retired_context);
3123 if (ctx)
3124 engine->context_unpin(engine, ctx);
Chris Wilson7b6da812017-12-16 00:03:34 +00003125
3126 /*
3127 * Ostensibily, we always want a context loaded for powersaving,
3128 * so if the engine is idle after the reset, send a request
3129 * to load our scratch kernel_context.
3130 *
3131 * More mysteriously, if we leave the engine idle after a reset,
3132 * the next userspace batch may hang, with what appears to be
3133 * an incoherent read by the CS (presumably stale TLB). An
3134 * empty request appears sufficient to paper over the glitch.
3135 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003136 if (intel_engine_is_idle(engine)) {
Chris Wilson7b6da812017-12-16 00:03:34 +00003137 struct drm_i915_gem_request *rq;
3138
3139 rq = i915_gem_request_alloc(engine,
3140 dev_priv->kernel_context);
3141 if (!IS_ERR(rq))
3142 __i915_add_request(rq, false);
3143 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003144 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003145
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003146 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003147
3148 if (dev_priv->gt.awake) {
3149 intel_sanitize_gt_powersave(dev_priv);
3150 intel_enable_gt_powersave(dev_priv);
3151 if (INTEL_GEN(dev_priv) >= 6)
3152 gen6_rps_busy(dev_priv);
3153 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003154}
3155
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003156void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3157{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303158 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003159 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003160
3161 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003162}
3163
Chris Wilsond8027092017-02-08 14:30:32 +00003164void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3165{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003166 struct intel_engine_cs *engine;
3167 enum intel_engine_id id;
3168
Chris Wilsond8027092017-02-08 14:30:32 +00003169 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003170
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003171 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003172 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003173 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003174 }
Chris Wilsond8027092017-02-08 14:30:32 +00003175}
3176
Chris Wilson821ed7d2016-09-09 14:11:53 +01003177static void nop_submit_request(struct drm_i915_gem_request *request)
3178{
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003179 dma_fence_set_error(&request->fence, -EIO);
3180
3181 i915_gem_request_submit(request);
3182}
3183
3184static void nop_complete_submit_request(struct drm_i915_gem_request *request)
3185{
Chris Wilson8d550822017-10-06 12:56:17 +01003186 unsigned long flags;
3187
Chris Wilson3cd94422017-01-10 17:22:45 +00003188 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003189
3190 spin_lock_irqsave(&request->engine->timeline->lock, flags);
3191 __i915_gem_request_submit(request);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00003192 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003193 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003194}
3195
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003196void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003197{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003198 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303199 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003200
Chris Wilson559e0402018-02-05 09:21:59 +00003201 if (drm_debug & DRM_UT_DRIVER) {
3202 struct drm_printer p = drm_debug_printer(__func__);
3203
3204 for_each_engine(engine, i915, id)
3205 intel_engine_dump(engine, &p, "%s\n", engine->name);
3206 }
3207
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003208 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3209 smp_mb__after_atomic();
3210
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003211 /*
3212 * First, stop submission to hw, but do not yet complete requests by
3213 * rolling the global seqno forward (since this would complete requests
3214 * for which we haven't set the fence error to EIO yet).
3215 */
Chris Wilson20e49332016-11-22 14:41:21 +00003216 for_each_engine(engine, i915, id)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003217 engine->submit_request = nop_submit_request;
3218
3219 /*
3220 * Make sure no one is running the old callback before we proceed with
3221 * cancelling requests and resetting the completion tracking. Otherwise
3222 * we might submit a request to the hardware which never completes.
3223 */
3224 synchronize_rcu();
3225
3226 for_each_engine(engine, i915, id) {
3227 /* Mark all executing requests as skipped */
3228 engine->cancel_requests(engine);
3229
3230 /*
3231 * Only once we've force-cancelled all in-flight requests can we
3232 * start to complete all requests.
3233 */
3234 engine->submit_request = nop_complete_submit_request;
Chris Wilson3fed1802018-02-07 21:05:43 +00003235 engine->schedule = NULL;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003236 }
3237
Chris Wilson3fed1802018-02-07 21:05:43 +00003238 i915->caps.scheduler = 0;
3239
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003240 /*
3241 * Make sure no request can slip through without getting completed by
3242 * either this call here to intel_engine_init_global_seqno, or the one
3243 * in nop_complete_submit_request.
3244 */
3245 synchronize_rcu();
3246
3247 for_each_engine(engine, i915, id) {
3248 unsigned long flags;
3249
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003250 /*
3251 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003252 * (lockless) lookup doesn't try and wait upon the request as we
3253 * reset it.
3254 */
3255 spin_lock_irqsave(&engine->timeline->lock, flags);
3256 intel_engine_init_global_seqno(engine,
3257 intel_engine_last_submit(engine));
3258 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3259 }
Chris Wilson20e49332016-11-22 14:41:21 +00003260
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003261 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003262}
3263
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003264bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3265{
3266 struct i915_gem_timeline *tl;
3267 int i;
3268
3269 lockdep_assert_held(&i915->drm.struct_mutex);
3270 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3271 return true;
3272
3273 /* Before unwedging, make sure that all pending operations
3274 * are flushed and errored out - we may have requests waiting upon
3275 * third party fences. We marked all inflight requests as EIO, and
3276 * every execbuf since returned EIO, for consistency we want all
3277 * the currently pending requests to also be marked as EIO, which
3278 * is done inside our nop_submit_request - and so we must wait.
3279 *
3280 * No more can be submitted until we reset the wedged bit.
3281 */
3282 list_for_each_entry(tl, &i915->gt.timelines, link) {
3283 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3284 struct drm_i915_gem_request *rq;
3285
3286 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3287 &i915->drm.struct_mutex);
3288 if (!rq)
3289 continue;
3290
3291 /* We can't use our normal waiter as we want to
3292 * avoid recursively trying to handle the current
3293 * reset. The basic dma_fence_default_wait() installs
3294 * a callback for dma_fence_signal(), which is
3295 * triggered by our nop handler (indirectly, the
3296 * callback enables the signaler thread which is
3297 * woken by the nop_submit_request() advancing the seqno
3298 * and when the seqno passes the fence, the signaler
3299 * then signals the fence waking us up).
3300 */
3301 if (dma_fence_default_wait(&rq->fence, true,
3302 MAX_SCHEDULE_TIMEOUT) < 0)
3303 return false;
3304 }
3305 }
3306
3307 /* Undo nop_submit_request. We prevent all new i915 requests from
3308 * being queued (by disallowing execbuf whilst wedged) so having
3309 * waited for all active requests above, we know the system is idle
3310 * and do not have to worry about a thread being inside
3311 * engine->submit_request() as we swap over. So unlike installing
3312 * the nop_submit_request on reset, we can do this from normal
3313 * context and do not require stop_machine().
3314 */
3315 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003316 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003317
3318 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3319 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3320
3321 return true;
3322}
3323
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003324static void
Eric Anholt673a3942008-07-30 12:06:12 -07003325i915_gem_retire_work_handler(struct work_struct *work)
3326{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003327 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003328 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003329 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003330
Chris Wilson891b48c2010-09-29 12:26:37 +01003331 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003332 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003333 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003334 mutex_unlock(&dev->struct_mutex);
3335 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003336
Chris Wilson88923042018-01-29 14:41:04 +00003337 /*
3338 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003339 * We do not need to do this test under locking as in the worst-case
3340 * we queue the retire worker once too often.
3341 */
Chris Wilson88923042018-01-29 14:41:04 +00003342 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003343 queue_delayed_work(dev_priv->wq,
3344 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003345 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003346}
Chris Wilson891b48c2010-09-29 12:26:37 +01003347
Chris Wilson84a10742018-01-24 11:36:08 +00003348static void shrink_caches(struct drm_i915_private *i915)
3349{
3350 /*
3351 * kmem_cache_shrink() discards empty slabs and reorders partially
3352 * filled slabs to prioritise allocating from the mostly full slabs,
3353 * with the aim of reducing fragmentation.
3354 */
3355 kmem_cache_shrink(i915->priorities);
3356 kmem_cache_shrink(i915->dependencies);
3357 kmem_cache_shrink(i915->requests);
3358 kmem_cache_shrink(i915->luts);
3359 kmem_cache_shrink(i915->vmas);
3360 kmem_cache_shrink(i915->objects);
3361}
3362
3363struct sleep_rcu_work {
3364 union {
3365 struct rcu_head rcu;
3366 struct work_struct work;
3367 };
3368 struct drm_i915_private *i915;
3369 unsigned int epoch;
3370};
3371
3372static inline bool
3373same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3374{
3375 /*
3376 * There is a small chance that the epoch wrapped since we started
3377 * sleeping. If we assume that epoch is at least a u32, then it will
3378 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3379 */
3380 return epoch == READ_ONCE(i915->gt.epoch);
3381}
3382
3383static void __sleep_work(struct work_struct *work)
3384{
3385 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3386 struct drm_i915_private *i915 = s->i915;
3387 unsigned int epoch = s->epoch;
3388
3389 kfree(s);
3390 if (same_epoch(i915, epoch))
3391 shrink_caches(i915);
3392}
3393
3394static void __sleep_rcu(struct rcu_head *rcu)
3395{
3396 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3397 struct drm_i915_private *i915 = s->i915;
3398
3399 if (same_epoch(i915, s->epoch)) {
3400 INIT_WORK(&s->work, __sleep_work);
3401 queue_work(i915->wq, &s->work);
3402 } else {
3403 kfree(s);
3404 }
3405}
3406
Chris Wilson5427f202017-10-23 22:32:34 +01003407static inline bool
3408new_requests_since_last_retire(const struct drm_i915_private *i915)
3409{
3410 return (READ_ONCE(i915->gt.active_requests) ||
3411 work_pending(&i915->gt.idle_work.work));
3412}
3413
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003414static void
3415i915_gem_idle_work_handler(struct work_struct *work)
3416{
3417 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003418 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003419 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003420 bool rearm_hangcheck;
Chris Wilson5427f202017-10-23 22:32:34 +01003421 ktime_t end;
Chris Wilson67d97da2016-07-04 08:08:31 +01003422
3423 if (!READ_ONCE(dev_priv->gt.awake))
3424 return;
3425
Imre Deak0cb56702016-11-07 11:20:04 +02003426 /*
3427 * Wait for last execlists context complete, but bail out in case a
3428 * new request is submitted.
3429 */
Chris Wilsonee42c002017-12-11 19:41:34 +00003430 end = ktime_add_ms(ktime_get(), I915_IDLE_ENGINES_TIMEOUT);
Chris Wilson5427f202017-10-23 22:32:34 +01003431 do {
3432 if (new_requests_since_last_retire(dev_priv))
3433 return;
3434
3435 if (intel_engines_are_idle(dev_priv))
3436 break;
3437
3438 usleep_range(100, 500);
3439 } while (ktime_before(ktime_get(), end));
Chris Wilson67d97da2016-07-04 08:08:31 +01003440
3441 rearm_hangcheck =
3442 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3443
Chris Wilson5427f202017-10-23 22:32:34 +01003444 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003445 /* Currently busy, come back later */
3446 mod_delayed_work(dev_priv->wq,
3447 &dev_priv->gt.idle_work,
3448 msecs_to_jiffies(50));
3449 goto out_rearm;
3450 }
3451
Imre Deak93c97dc2016-11-07 11:20:03 +02003452 /*
3453 * New request retired after this work handler started, extend active
3454 * period until next instance of the work.
3455 */
Chris Wilson5427f202017-10-23 22:32:34 +01003456 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003457 goto out_unlock;
3458
Chris Wilson5427f202017-10-23 22:32:34 +01003459 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003460 * Be paranoid and flush a concurrent interrupt to make sure
3461 * we don't reactivate any irq tasklets after parking.
3462 *
3463 * FIXME: Note that even though we have waited for execlists to be idle,
3464 * there may still be an in-flight interrupt even though the CSB
3465 * is now empty. synchronize_irq() makes sure that a residual interrupt
3466 * is completed before we continue, but it doesn't prevent the HW from
3467 * raising a spurious interrupt later. To complete the shield we should
3468 * coordinate disabling the CS irq with flushing the interrupts.
3469 */
3470 synchronize_irq(dev_priv->drm.irq);
3471
Chris Wilsonaba5e272017-10-25 15:39:41 +01003472 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003473 i915_gem_timelines_park(dev_priv);
3474
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003475 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003476
Chris Wilson67d97da2016-07-04 08:08:31 +01003477 GEM_BUG_ON(!dev_priv->gt.awake);
3478 dev_priv->gt.awake = false;
Chris Wilson84a10742018-01-24 11:36:08 +00003479 epoch = dev_priv->gt.epoch;
3480 GEM_BUG_ON(epoch == I915_EPOCH_INVALID);
Chris Wilson67d97da2016-07-04 08:08:31 +01003481 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003482
Chris Wilson67d97da2016-07-04 08:08:31 +01003483 if (INTEL_GEN(dev_priv) >= 6)
3484 gen6_rps_idle(dev_priv);
Tvrtko Ursulinb6876372017-12-05 13:28:54 +00003485
3486 intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ);
3487
Chris Wilson67d97da2016-07-04 08:08:31 +01003488 intel_runtime_pm_put(dev_priv);
3489out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003490 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003491
Chris Wilson67d97da2016-07-04 08:08:31 +01003492out_rearm:
3493 if (rearm_hangcheck) {
3494 GEM_BUG_ON(!dev_priv->gt.awake);
3495 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003496 }
Chris Wilson84a10742018-01-24 11:36:08 +00003497
3498 /*
3499 * When we are idle, it is an opportune time to reap our caches.
3500 * However, we have many objects that utilise RCU and the ordered
3501 * i915->wq that this work is executing on. To try and flush any
3502 * pending frees now we are idle, we first wait for an RCU grace
3503 * period, and then queue a task (that will run last on the wq) to
3504 * shrink and re-optimize the caches.
3505 */
3506 if (same_epoch(dev_priv, epoch)) {
3507 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3508 if (s) {
3509 s->i915 = dev_priv;
3510 s->epoch = epoch;
3511 call_rcu(&s->rcu, __sleep_rcu);
3512 }
3513 }
Eric Anholt673a3942008-07-30 12:06:12 -07003514}
3515
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003516void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3517{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003518 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003519 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3520 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003521 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003522
Chris Wilsond1b48c12017-08-16 09:52:08 +01003523 mutex_lock(&i915->drm.struct_mutex);
3524
3525 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3526 struct i915_gem_context *ctx = lut->ctx;
3527 struct i915_vma *vma;
3528
Chris Wilson432295d2017-08-22 12:05:15 +01003529 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003530 if (ctx->file_priv != fpriv)
3531 continue;
3532
3533 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003534 GEM_BUG_ON(vma->obj != obj);
3535
3536 /* We allow the process to have multiple handles to the same
3537 * vma, in the same fd namespace, by virtue of flink/open.
3538 */
3539 GEM_BUG_ON(!vma->open_count);
3540 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003541 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003542
Chris Wilsond1b48c12017-08-16 09:52:08 +01003543 list_del(&lut->obj_link);
3544 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003545
Chris Wilsond1b48c12017-08-16 09:52:08 +01003546 kmem_cache_free(i915->luts, lut);
3547 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003548 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003549
3550 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003551}
3552
Chris Wilsone95433c2016-10-28 13:58:27 +01003553static unsigned long to_wait_timeout(s64 timeout_ns)
3554{
3555 if (timeout_ns < 0)
3556 return MAX_SCHEDULE_TIMEOUT;
3557
3558 if (timeout_ns == 0)
3559 return 0;
3560
3561 return nsecs_to_jiffies_timeout(timeout_ns);
3562}
3563
Ben Widawsky5816d642012-04-11 11:18:19 -07003564/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003565 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003566 * @dev: drm device pointer
3567 * @data: ioctl data blob
3568 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003569 *
3570 * Returns 0 if successful, else an error is returned with the remaining time in
3571 * the timeout parameter.
3572 * -ETIME: object is still busy after timeout
3573 * -ERESTARTSYS: signal interrupted the wait
3574 * -ENONENT: object doesn't exist
3575 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003576 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003577 * -ENOMEM: damn
3578 * -ENODEV: Internal IRQ fail
3579 * -E?: The add request failed
3580 *
3581 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3582 * non-zero timeout parameter the wait ioctl will wait for the given number of
3583 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3584 * without holding struct_mutex the object may become re-busied before this
3585 * function completes. A similar but shorter * race condition exists in the busy
3586 * ioctl
3587 */
3588int
3589i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3590{
3591 struct drm_i915_gem_wait *args = data;
3592 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003593 ktime_t start;
3594 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003595
Daniel Vetter11b5d512014-09-29 15:31:26 +02003596 if (args->flags != 0)
3597 return -EINVAL;
3598
Chris Wilson03ac0642016-07-20 13:31:51 +01003599 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003600 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003601 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003602
Chris Wilsone95433c2016-10-28 13:58:27 +01003603 start = ktime_get();
3604
3605 ret = i915_gem_object_wait(obj,
3606 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3607 to_wait_timeout(args->timeout_ns),
3608 to_rps_client(file));
3609
3610 if (args->timeout_ns > 0) {
3611 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3612 if (args->timeout_ns < 0)
3613 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003614
3615 /*
3616 * Apparently ktime isn't accurate enough and occasionally has a
3617 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3618 * things up to make the test happy. We allow up to 1 jiffy.
3619 *
3620 * This is a regression from the timespec->ktime conversion.
3621 */
3622 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3623 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003624
3625 /* Asked to wait beyond the jiffie/scheduler precision? */
3626 if (ret == -ETIME && args->timeout_ns)
3627 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003628 }
3629
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003630 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003631 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003632}
3633
Chris Wilson73cb9702016-10-28 13:58:46 +01003634static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003635{
Chris Wilson73cb9702016-10-28 13:58:46 +01003636 int ret, i;
3637
3638 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3639 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3640 if (ret)
3641 return ret;
3642 }
3643
3644 return 0;
3645}
3646
Chris Wilson25112b62017-03-30 15:50:39 +01003647static int wait_for_engines(struct drm_i915_private *i915)
3648{
Chris Wilsonee42c002017-12-11 19:41:34 +00003649 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003650 dev_err(i915->drm.dev,
3651 "Failed to idle engines, declaring wedged!\n");
3652 if (drm_debug & DRM_UT_DRIVER) {
3653 struct drm_printer p = drm_debug_printer(__func__);
3654 struct intel_engine_cs *engine;
3655 enum intel_engine_id id;
3656
3657 for_each_engine(engine, i915, id)
3658 intel_engine_dump(engine, &p,
Chris Wilson9e519bc2018-02-05 10:06:18 +00003659 "%s\n", engine->name);
Chris Wilson59e4b192017-12-11 19:41:35 +00003660 }
3661
Chris Wilsoncad99462017-08-26 12:09:33 +01003662 i915_gem_set_wedged(i915);
3663 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003664 }
3665
3666 return 0;
3667}
3668
Chris Wilson73cb9702016-10-28 13:58:46 +01003669int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3670{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003671 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003672
Chris Wilson863e9fd2017-05-30 13:13:32 +01003673 /* If the device is asleep, we have no requests outstanding */
3674 if (!READ_ONCE(i915->gt.awake))
3675 return 0;
3676
Chris Wilson9caa34a2016-11-11 14:58:08 +00003677 if (flags & I915_WAIT_LOCKED) {
3678 struct i915_gem_timeline *tl;
3679
3680 lockdep_assert_held(&i915->drm.struct_mutex);
3681
3682 list_for_each_entry(tl, &i915->gt.timelines, link) {
3683 ret = wait_for_timeline(tl, flags);
3684 if (ret)
3685 return ret;
3686 }
Chris Wilson72022a72017-03-30 15:50:38 +01003687 i915_gem_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003688
3689 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003690 } else {
3691 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003692 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003693
Chris Wilson25112b62017-03-30 15:50:39 +01003694 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003695}
3696
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003697static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3698{
Chris Wilsone27ab732017-06-15 13:38:49 +01003699 /*
3700 * We manually flush the CPU domain so that we can override and
3701 * force the flush for the display, and perform it asyncrhonously.
3702 */
3703 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3704 if (obj->cache_dirty)
3705 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003706 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003707}
3708
3709void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3710{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003711 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003712 return;
3713
3714 mutex_lock(&obj->base.dev->struct_mutex);
3715 __i915_gem_object_flush_for_display(obj);
3716 mutex_unlock(&obj->base.dev->struct_mutex);
3717}
3718
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003719/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003720 * Moves a single object to the WC read, and possibly write domain.
3721 * @obj: object to act on
3722 * @write: ask for write access or read only
3723 *
3724 * This function returns when the move is complete, including waiting on
3725 * flushes to occur.
3726 */
3727int
3728i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3729{
3730 int ret;
3731
3732 lockdep_assert_held(&obj->base.dev->struct_mutex);
3733
3734 ret = i915_gem_object_wait(obj,
3735 I915_WAIT_INTERRUPTIBLE |
3736 I915_WAIT_LOCKED |
3737 (write ? I915_WAIT_ALL : 0),
3738 MAX_SCHEDULE_TIMEOUT,
3739 NULL);
3740 if (ret)
3741 return ret;
3742
Christian Königc0a51fd2018-02-16 13:43:38 +01003743 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003744 return 0;
3745
3746 /* Flush and acquire obj->pages so that we are coherent through
3747 * direct access in memory with previous cached writes through
3748 * shmemfs and that our cache domain tracking remains valid.
3749 * For example, if the obj->filp was moved to swap without us
3750 * being notified and releasing the pages, we would mistakenly
3751 * continue to assume that the obj remained out of the CPU cached
3752 * domain.
3753 */
3754 ret = i915_gem_object_pin_pages(obj);
3755 if (ret)
3756 return ret;
3757
3758 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3759
3760 /* Serialise direct access to this object with the barriers for
3761 * coherent writes from the GPU, by effectively invalidating the
3762 * WC domain upon first access.
3763 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003764 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003765 mb();
3766
3767 /* It should now be out of any other write domains, and we can update
3768 * the domain values for our changes.
3769 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003770 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3771 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003772 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003773 obj->read_domains = I915_GEM_DOMAIN_WC;
3774 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003775 obj->mm.dirty = true;
3776 }
3777
3778 i915_gem_object_unpin_pages(obj);
3779 return 0;
3780}
3781
3782/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003783 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003784 * @obj: object to act on
3785 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003786 *
3787 * This function returns when the move is complete, including waiting on
3788 * flushes to occur.
3789 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003790int
Chris Wilson20217462010-11-23 15:26:33 +00003791i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003792{
Eric Anholte47c68e2008-11-14 13:35:19 -08003793 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003794
Chris Wilsone95433c2016-10-28 13:58:27 +01003795 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003796
Chris Wilsone95433c2016-10-28 13:58:27 +01003797 ret = i915_gem_object_wait(obj,
3798 I915_WAIT_INTERRUPTIBLE |
3799 I915_WAIT_LOCKED |
3800 (write ? I915_WAIT_ALL : 0),
3801 MAX_SCHEDULE_TIMEOUT,
3802 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003803 if (ret)
3804 return ret;
3805
Christian Königc0a51fd2018-02-16 13:43:38 +01003806 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003807 return 0;
3808
Chris Wilson43566de2015-01-02 16:29:29 +05303809 /* Flush and acquire obj->pages so that we are coherent through
3810 * direct access in memory with previous cached writes through
3811 * shmemfs and that our cache domain tracking remains valid.
3812 * For example, if the obj->filp was moved to swap without us
3813 * being notified and releasing the pages, we would mistakenly
3814 * continue to assume that the obj remained out of the CPU cached
3815 * domain.
3816 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003817 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303818 if (ret)
3819 return ret;
3820
Chris Wilsonef749212017-04-12 12:01:10 +01003821 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003822
Chris Wilsond0a57782012-10-09 19:24:37 +01003823 /* Serialise direct access to this object with the barriers for
3824 * coherent writes from the GPU, by effectively invalidating the
3825 * GTT domain upon first access.
3826 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003827 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003828 mb();
3829
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003830 /* It should now be out of any other write domains, and we can update
3831 * the domain values for our changes.
3832 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003833 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3834 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003835 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003836 obj->read_domains = I915_GEM_DOMAIN_GTT;
3837 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003838 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003839 }
3840
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003841 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003842 return 0;
3843}
3844
Chris Wilsonef55f922015-10-09 14:11:27 +01003845/**
3846 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003847 * @obj: object to act on
3848 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003849 *
3850 * After this function returns, the object will be in the new cache-level
3851 * across all GTT and the contents of the backing storage will be coherent,
3852 * with respect to the new cache-level. In order to keep the backing storage
3853 * coherent for all users, we only allow a single cache level to be set
3854 * globally on the object and prevent it from being changed whilst the
3855 * hardware is reading from the object. That is if the object is currently
3856 * on the scanout it will be set to uncached (or equivalent display
3857 * cache coherency) and all non-MOCS GPU access will also be uncached so
3858 * that all direct access to the scanout remains coherent.
3859 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003860int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3861 enum i915_cache_level cache_level)
3862{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003863 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003864 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003865
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003866 lockdep_assert_held(&obj->base.dev->struct_mutex);
3867
Chris Wilsone4ffd172011-04-04 09:44:39 +01003868 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003869 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003870
Chris Wilsonef55f922015-10-09 14:11:27 +01003871 /* Inspect the list of currently bound VMA and unbind any that would
3872 * be invalid given the new cache-level. This is principally to
3873 * catch the issue of the CS prefetch crossing page boundaries and
3874 * reading an invalid PTE on older architectures.
3875 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003876restart:
3877 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003878 if (!drm_mm_node_allocated(&vma->node))
3879 continue;
3880
Chris Wilson20dfbde2016-08-04 16:32:30 +01003881 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003882 DRM_DEBUG("can not change the cache level of pinned objects\n");
3883 return -EBUSY;
3884 }
3885
Chris Wilson010e3e62017-12-06 12:49:13 +00003886 if (!i915_vma_is_closed(vma) &&
3887 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003888 continue;
3889
3890 ret = i915_vma_unbind(vma);
3891 if (ret)
3892 return ret;
3893
3894 /* As unbinding may affect other elements in the
3895 * obj->vma_list (due to side-effects from retiring
3896 * an active vma), play safe and restart the iterator.
3897 */
3898 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003899 }
3900
Chris Wilsonef55f922015-10-09 14:11:27 +01003901 /* We can reuse the existing drm_mm nodes but need to change the
3902 * cache-level on the PTE. We could simply unbind them all and
3903 * rebind with the correct cache-level on next use. However since
3904 * we already have a valid slot, dma mapping, pages etc, we may as
3905 * rewrite the PTE in the belief that doing so tramples upon less
3906 * state and so involves less work.
3907 */
Chris Wilson15717de2016-08-04 07:52:26 +01003908 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003909 /* Before we change the PTE, the GPU must not be accessing it.
3910 * If we wait upon the object, we know that all the bound
3911 * VMA are no longer active.
3912 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003913 ret = i915_gem_object_wait(obj,
3914 I915_WAIT_INTERRUPTIBLE |
3915 I915_WAIT_LOCKED |
3916 I915_WAIT_ALL,
3917 MAX_SCHEDULE_TIMEOUT,
3918 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003919 if (ret)
3920 return ret;
3921
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003922 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3923 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003924 /* Access to snoopable pages through the GTT is
3925 * incoherent and on some machines causes a hard
3926 * lockup. Relinquish the CPU mmaping to force
3927 * userspace to refault in the pages and we can
3928 * then double check if the GTT mapping is still
3929 * valid for that pointer access.
3930 */
3931 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003932
Chris Wilsonef55f922015-10-09 14:11:27 +01003933 /* As we no longer need a fence for GTT access,
3934 * we can relinquish it now (and so prevent having
3935 * to steal a fence from someone else on the next
3936 * fence request). Note GPU activity would have
3937 * dropped the fence as all snoopable access is
3938 * supposed to be linear.
3939 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003940 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003941 ret = i915_vma_put_fence(vma);
3942 if (ret)
3943 return ret;
3944 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003945 } else {
3946 /* We either have incoherent backing store and
3947 * so no GTT access or the architecture is fully
3948 * coherent. In such cases, existing GTT mmaps
3949 * ignore the cache bit in the PTE and we can
3950 * rewrite it without confusing the GPU or having
3951 * to force userspace to fault back in its mmaps.
3952 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003953 }
3954
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003955 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003956 if (!drm_mm_node_allocated(&vma->node))
3957 continue;
3958
3959 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3960 if (ret)
3961 return ret;
3962 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003963 }
3964
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003965 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003966 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003967 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003968 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003969
Chris Wilsone4ffd172011-04-04 09:44:39 +01003970 return 0;
3971}
3972
Ben Widawsky199adf42012-09-21 17:01:20 -07003973int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3974 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003975{
Ben Widawsky199adf42012-09-21 17:01:20 -07003976 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003977 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003978 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003979
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003980 rcu_read_lock();
3981 obj = i915_gem_object_lookup_rcu(file, args->handle);
3982 if (!obj) {
3983 err = -ENOENT;
3984 goto out;
3985 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003986
Chris Wilson651d7942013-08-08 14:41:10 +01003987 switch (obj->cache_level) {
3988 case I915_CACHE_LLC:
3989 case I915_CACHE_L3_LLC:
3990 args->caching = I915_CACHING_CACHED;
3991 break;
3992
Chris Wilson4257d3b2013-08-08 14:41:11 +01003993 case I915_CACHE_WT:
3994 args->caching = I915_CACHING_DISPLAY;
3995 break;
3996
Chris Wilson651d7942013-08-08 14:41:10 +01003997 default:
3998 args->caching = I915_CACHING_NONE;
3999 break;
4000 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004001out:
4002 rcu_read_unlock();
4003 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004004}
4005
Ben Widawsky199adf42012-09-21 17:01:20 -07004006int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4007 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004008{
Chris Wilson9c870d02016-10-24 13:42:15 +01004009 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004010 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004011 struct drm_i915_gem_object *obj;
4012 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004013 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004014
Ben Widawsky199adf42012-09-21 17:01:20 -07004015 switch (args->caching) {
4016 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004017 level = I915_CACHE_NONE;
4018 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004019 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004020 /*
4021 * Due to a HW issue on BXT A stepping, GPU stores via a
4022 * snooped mapping may leave stale data in a corresponding CPU
4023 * cacheline, whereas normally such cachelines would get
4024 * invalidated.
4025 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004026 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004027 return -ENODEV;
4028
Chris Wilsone6994ae2012-07-10 10:27:08 +01004029 level = I915_CACHE_LLC;
4030 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004031 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004032 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004033 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004034 default:
4035 return -EINVAL;
4036 }
4037
Chris Wilsond65415d2017-01-19 08:22:10 +00004038 obj = i915_gem_object_lookup(file, args->handle);
4039 if (!obj)
4040 return -ENOENT;
4041
Tina Zhanga03f3952017-11-14 10:25:13 +00004042 /*
4043 * The caching mode of proxy object is handled by its generator, and
4044 * not allowed to be changed by userspace.
4045 */
4046 if (i915_gem_object_is_proxy(obj)) {
4047 ret = -ENXIO;
4048 goto out;
4049 }
4050
Chris Wilsond65415d2017-01-19 08:22:10 +00004051 if (obj->cache_level == level)
4052 goto out;
4053
4054 ret = i915_gem_object_wait(obj,
4055 I915_WAIT_INTERRUPTIBLE,
4056 MAX_SCHEDULE_TIMEOUT,
4057 to_rps_client(file));
4058 if (ret)
4059 goto out;
4060
Ben Widawsky3bc29132012-09-26 16:15:20 -07004061 ret = i915_mutex_lock_interruptible(dev);
4062 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004063 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004064
4065 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004066 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004067
4068out:
4069 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004070 return ret;
4071}
4072
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004073/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004074 * Prepare buffer for display plane (scanout, cursors, etc).
4075 * Can be called from an uninterruptible phase (modesetting) and allows
4076 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004077 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004078struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004079i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4080 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004081 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004082{
Chris Wilson058d88c2016-08-15 10:49:06 +01004083 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004084 int ret;
4085
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004086 lockdep_assert_held(&obj->base.dev->struct_mutex);
4087
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004088 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004089 * display coherency whilst setting up the cache domains.
4090 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004091 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004092
Eric Anholta7ef0642011-03-29 16:59:54 -07004093 /* The display engine is not coherent with the LLC cache on gen6. As
4094 * a result, we make sure that the pinning that is about to occur is
4095 * done with uncached PTEs. This is lowest common denominator for all
4096 * chipsets.
4097 *
4098 * However for gen6+, we could do better by using the GFDT bit instead
4099 * of uncaching, which would allow us to flush all the LLC-cached data
4100 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4101 */
Chris Wilson651d7942013-08-08 14:41:10 +01004102 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004103 HAS_WT(to_i915(obj->base.dev)) ?
4104 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004105 if (ret) {
4106 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004107 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004108 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004109
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004110 /* As the user may map the buffer once pinned in the display plane
4111 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004112 * always use map_and_fenceable for all scanout buffers. However,
4113 * it may simply be too big to fit into mappable, in which case
4114 * put it anyway and hope that userspace can cope (but always first
4115 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004116 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004117 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00004118 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01004119 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
4120 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00004121 if (IS_ERR(vma)) {
4122 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4123 unsigned int flags;
4124
4125 /* Valleyview is definitely limited to scanning out the first
4126 * 512MiB. Lets presume this behaviour was inherited from the
4127 * g4x display engine and that all earlier gen are similarly
4128 * limited. Testing suggests that it is a little more
4129 * complicated than this. For example, Cherryview appears quite
4130 * happy to scanout from anywhere within its global aperture.
4131 */
4132 flags = 0;
4133 if (HAS_GMCH_DISPLAY(i915))
4134 flags = PIN_MAPPABLE;
4135 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
4136 }
Chris Wilson058d88c2016-08-15 10:49:06 +01004137 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004138 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004139
Chris Wilsond8923dc2016-08-18 17:17:07 +01004140 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4141
Chris Wilsona6a7cc42016-11-18 21:17:46 +00004142 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004143 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00004144 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004145
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004146 /* It should now be out of any other write domains, and we can update
4147 * the domain values for our changes.
4148 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004149 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004150
Chris Wilson058d88c2016-08-15 10:49:06 +01004151 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004152
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004153err_unpin_global:
4154 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004155 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004156}
4157
4158void
Chris Wilson058d88c2016-08-15 10:49:06 +01004159i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004160{
Chris Wilson49d73912016-11-29 09:50:08 +00004161 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004162
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004163 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004164 return;
4165
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004166 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004167 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004168
Chris Wilson383d5822016-08-18 17:17:08 +01004169 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004170 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004171
Chris Wilson058d88c2016-08-15 10:49:06 +01004172 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004173}
4174
Eric Anholte47c68e2008-11-14 13:35:19 -08004175/**
4176 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004177 * @obj: object to act on
4178 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004179 *
4180 * This function returns when the move is complete, including waiting on
4181 * flushes to occur.
4182 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004183int
Chris Wilson919926a2010-11-12 13:42:53 +00004184i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004185{
Eric Anholte47c68e2008-11-14 13:35:19 -08004186 int ret;
4187
Chris Wilsone95433c2016-10-28 13:58:27 +01004188 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004189
Chris Wilsone95433c2016-10-28 13:58:27 +01004190 ret = i915_gem_object_wait(obj,
4191 I915_WAIT_INTERRUPTIBLE |
4192 I915_WAIT_LOCKED |
4193 (write ? I915_WAIT_ALL : 0),
4194 MAX_SCHEDULE_TIMEOUT,
4195 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004196 if (ret)
4197 return ret;
4198
Chris Wilsonef749212017-04-12 12:01:10 +01004199 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004200
Eric Anholte47c68e2008-11-14 13:35:19 -08004201 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004202 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004203 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004204 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004205 }
4206
4207 /* It should now be out of any other write domains, and we can update
4208 * the domain values for our changes.
4209 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004210 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004211
4212 /* If we're writing through the CPU, then the GPU read domains will
4213 * need to be invalidated at next use.
4214 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004215 if (write)
4216 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004217
4218 return 0;
4219}
4220
Eric Anholt673a3942008-07-30 12:06:12 -07004221/* Throttle our rendering by waiting until the ring has completed our requests
4222 * emitted over 20 msec ago.
4223 *
Eric Anholtb9624422009-06-03 07:27:35 +00004224 * Note that if we were to use the current jiffies each time around the loop,
4225 * we wouldn't escape the function with any frames outstanding if the time to
4226 * render a frame was over 20ms.
4227 *
Eric Anholt673a3942008-07-30 12:06:12 -07004228 * This should get us reasonable parallelism between CPU and GPU but also
4229 * relatively low latency when blocking on a particular request to finish.
4230 */
4231static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004232i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004233{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004234 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004235 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004236 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00004237 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004238 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004239
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004240 /* ABI: return -EIO if already wedged */
4241 if (i915_terminally_wedged(&dev_priv->gpu_error))
4242 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004243
Chris Wilson1c255952010-09-26 11:03:27 +01004244 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004245 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004246 if (time_after_eq(request->emitted_jiffies, recent_enough))
4247 break;
4248
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004249 if (target) {
4250 list_del(&target->client_link);
4251 target->file_priv = NULL;
4252 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004253
John Harrison54fb2412014-11-24 18:49:27 +00004254 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004255 }
John Harrisonff865882014-11-24 18:49:28 +00004256 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01004257 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004258 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004259
John Harrison54fb2412014-11-24 18:49:27 +00004260 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004261 return 0;
4262
Chris Wilsone95433c2016-10-28 13:58:27 +01004263 ret = i915_wait_request(target,
4264 I915_WAIT_INTERRUPTIBLE,
4265 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01004266 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004267
Chris Wilsone95433c2016-10-28 13:58:27 +01004268 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004269}
4270
Chris Wilson058d88c2016-08-15 10:49:06 +01004271struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004272i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4273 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004274 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004275 u64 alignment,
4276 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004277{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004278 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4279 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004280 struct i915_vma *vma;
4281 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004282
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004283 lockdep_assert_held(&obj->base.dev->struct_mutex);
4284
Chris Wilson43ae70d92017-10-09 09:44:01 +01004285 if (!view && flags & PIN_MAPPABLE) {
4286 /* If the required space is larger than the available
4287 * aperture, we will not able to find a slot for the
4288 * object and unbinding the object now will be in
4289 * vain. Worse, doing so may cause us to ping-pong
4290 * the object in and out of the Global GTT and
4291 * waste a lot of cycles under the mutex.
4292 */
4293 if (obj->base.size > dev_priv->ggtt.mappable_end)
4294 return ERR_PTR(-E2BIG);
4295
4296 /* If NONBLOCK is set the caller is optimistically
4297 * trying to cache the full object within the mappable
4298 * aperture, and *must* have a fallback in place for
4299 * situations where we cannot bind the object. We
4300 * can be a little more lax here and use the fallback
4301 * more often to avoid costly migrations of ourselves
4302 * and other objects within the aperture.
4303 *
4304 * Half-the-aperture is used as a simple heuristic.
4305 * More interesting would to do search for a free
4306 * block prior to making the commitment to unbind.
4307 * That caters for the self-harm case, and with a
4308 * little more heuristics (e.g. NOFAULT, NOEVICT)
4309 * we could try to minimise harm to others.
4310 */
4311 if (flags & PIN_NONBLOCK &&
4312 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4313 return ERR_PTR(-ENOSPC);
4314 }
4315
Chris Wilson718659a2017-01-16 15:21:28 +00004316 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004317 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004318 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004319
4320 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004321 if (flags & PIN_NONBLOCK) {
4322 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4323 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004324
Chris Wilson43ae70d92017-10-09 09:44:01 +01004325 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004326 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004327 return ERR_PTR(-ENOSPC);
4328 }
4329
Chris Wilson59bfa122016-08-04 16:32:31 +01004330 WARN(i915_vma_is_pinned(vma),
4331 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004332 " offset=%08x, req.alignment=%llx,"
4333 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4334 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004335 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004336 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004337 ret = i915_vma_unbind(vma);
4338 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004339 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004340 }
4341
Chris Wilson058d88c2016-08-15 10:49:06 +01004342 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4343 if (ret)
4344 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004345
Chris Wilson058d88c2016-08-15 10:49:06 +01004346 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004347}
4348
Chris Wilsonedf6b762016-08-09 09:23:33 +01004349static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004350{
4351 /* Note that we could alias engines in the execbuf API, but
4352 * that would be very unwise as it prevents userspace from
4353 * fine control over engine selection. Ahem.
4354 *
4355 * This should be something like EXEC_MAX_ENGINE instead of
4356 * I915_NUM_ENGINES.
4357 */
4358 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4359 return 0x10000 << id;
4360}
4361
4362static __always_inline unsigned int __busy_write_id(unsigned int id)
4363{
Chris Wilson70cb4722016-08-09 18:08:25 +01004364 /* The uABI guarantees an active writer is also amongst the read
4365 * engines. This would be true if we accessed the activity tracking
4366 * under the lock, but as we perform the lookup of the object and
4367 * its activity locklessly we can not guarantee that the last_write
4368 * being active implies that we have set the same engine flag from
4369 * last_read - hence we always set both read and write busy for
4370 * last_write.
4371 */
4372 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004373}
4374
Chris Wilsonedf6b762016-08-09 09:23:33 +01004375static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004376__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004377 unsigned int (*flag)(unsigned int id))
4378{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004379 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004380
Chris Wilsond07f0e52016-10-28 13:58:44 +01004381 /* We have to check the current hw status of the fence as the uABI
4382 * guarantees forward progress. We could rely on the idle worker
4383 * to eventually flush us, but to minimise latency just ask the
4384 * hardware.
4385 *
4386 * Note we only report on the status of native fences.
4387 */
4388 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004389 return 0;
4390
Chris Wilsond07f0e52016-10-28 13:58:44 +01004391 /* opencode to_request() in order to avoid const warnings */
4392 rq = container_of(fence, struct drm_i915_gem_request, fence);
4393 if (i915_gem_request_completed(rq))
4394 return 0;
4395
Chris Wilson1d39f282017-04-11 13:43:06 +01004396 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004397}
4398
Chris Wilsonedf6b762016-08-09 09:23:33 +01004399static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004400busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004401{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004402 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004403}
4404
Chris Wilsonedf6b762016-08-09 09:23:33 +01004405static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004406busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004407{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004408 if (!fence)
4409 return 0;
4410
4411 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004412}
4413
Eric Anholt673a3942008-07-30 12:06:12 -07004414int
Eric Anholt673a3942008-07-30 12:06:12 -07004415i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004416 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004417{
4418 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004419 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004420 struct reservation_object_list *list;
4421 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004422 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004423
Chris Wilsond07f0e52016-10-28 13:58:44 +01004424 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004425 rcu_read_lock();
4426 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004427 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004428 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004429
4430 /* A discrepancy here is that we do not report the status of
4431 * non-i915 fences, i.e. even though we may report the object as idle,
4432 * a call to set-domain may still stall waiting for foreign rendering.
4433 * This also means that wait-ioctl may report an object as busy,
4434 * where busy-ioctl considers it idle.
4435 *
4436 * We trade the ability to warn of foreign fences to report on which
4437 * i915 engines are active for the object.
4438 *
4439 * Alternatively, we can trade that extra information on read/write
4440 * activity with
4441 * args->busy =
4442 * !reservation_object_test_signaled_rcu(obj->resv, true);
4443 * to report the overall busyness. This is what the wait-ioctl does.
4444 *
4445 */
4446retry:
4447 seq = raw_read_seqcount(&obj->resv->seq);
4448
4449 /* Translate the exclusive fence to the READ *and* WRITE engine */
4450 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4451
4452 /* Translate shared fences to READ set of engines */
4453 list = rcu_dereference(obj->resv->fence);
4454 if (list) {
4455 unsigned int shared_count = list->shared_count, i;
4456
4457 for (i = 0; i < shared_count; ++i) {
4458 struct dma_fence *fence =
4459 rcu_dereference(list->shared[i]);
4460
4461 args->busy |= busy_check_reader(fence);
4462 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004463 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004464
Chris Wilsond07f0e52016-10-28 13:58:44 +01004465 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4466 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004467
Chris Wilsond07f0e52016-10-28 13:58:44 +01004468 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004469out:
4470 rcu_read_unlock();
4471 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004472}
4473
4474int
4475i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4476 struct drm_file *file_priv)
4477{
Akshay Joshi0206e352011-08-16 15:34:10 -04004478 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004479}
4480
Chris Wilson3ef94da2009-09-14 16:50:29 +01004481int
4482i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4483 struct drm_file *file_priv)
4484{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004485 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004486 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004487 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004488 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004489
4490 switch (args->madv) {
4491 case I915_MADV_DONTNEED:
4492 case I915_MADV_WILLNEED:
4493 break;
4494 default:
4495 return -EINVAL;
4496 }
4497
Chris Wilson03ac0642016-07-20 13:31:51 +01004498 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004499 if (!obj)
4500 return -ENOENT;
4501
4502 err = mutex_lock_interruptible(&obj->mm.lock);
4503 if (err)
4504 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004505
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004506 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004507 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004508 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004509 if (obj->mm.madv == I915_MADV_WILLNEED) {
4510 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004511 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004512 obj->mm.quirked = false;
4513 }
4514 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004515 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004516 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004517 obj->mm.quirked = true;
4518 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004519 }
4520
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004521 if (obj->mm.madv != __I915_MADV_PURGED)
4522 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004523
Chris Wilson6c085a72012-08-20 11:40:46 +02004524 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004525 if (obj->mm.madv == I915_MADV_DONTNEED &&
4526 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004527 i915_gem_object_truncate(obj);
4528
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004529 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004530 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004531
Chris Wilson1233e2d2016-10-28 13:58:37 +01004532out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004533 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004534 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004535}
4536
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004537static void
4538frontbuffer_retire(struct i915_gem_active *active,
4539 struct drm_i915_gem_request *request)
4540{
4541 struct drm_i915_gem_object *obj =
4542 container_of(active, typeof(*obj), frontbuffer_write);
4543
Chris Wilsond59b21e2017-02-22 11:40:49 +00004544 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004545}
4546
Chris Wilson37e680a2012-06-07 15:38:42 +01004547void i915_gem_object_init(struct drm_i915_gem_object *obj,
4548 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004549{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004550 mutex_init(&obj->mm.lock);
4551
Ben Widawsky2f633152013-07-17 12:19:03 -07004552 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004553 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004554 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004555
Chris Wilson37e680a2012-06-07 15:38:42 +01004556 obj->ops = ops;
4557
Chris Wilsond07f0e52016-10-28 13:58:44 +01004558 reservation_object_init(&obj->__builtin_resv);
4559 obj->resv = &obj->__builtin_resv;
4560
Chris Wilson50349242016-08-18 17:17:04 +01004561 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004562 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004563
4564 obj->mm.madv = I915_MADV_WILLNEED;
4565 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4566 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004567
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004568 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004569}
4570
Chris Wilson37e680a2012-06-07 15:38:42 +01004571static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004572 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4573 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004574
Chris Wilson37e680a2012-06-07 15:38:42 +01004575 .get_pages = i915_gem_object_get_pages_gtt,
4576 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004577
4578 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004579};
4580
Matthew Auld465c4032017-10-06 23:18:14 +01004581static int i915_gem_object_create_shmem(struct drm_device *dev,
4582 struct drm_gem_object *obj,
4583 size_t size)
4584{
4585 struct drm_i915_private *i915 = to_i915(dev);
4586 unsigned long flags = VM_NORESERVE;
4587 struct file *filp;
4588
4589 drm_gem_private_object_init(dev, obj, size);
4590
4591 if (i915->mm.gemfs)
4592 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4593 flags);
4594 else
4595 filp = shmem_file_setup("i915", size, flags);
4596
4597 if (IS_ERR(filp))
4598 return PTR_ERR(filp);
4599
4600 obj->filp = filp;
4601
4602 return 0;
4603}
4604
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004605struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004606i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004607{
Daniel Vetterc397b902010-04-09 19:05:07 +00004608 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004609 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004610 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004611 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004612 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004613
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004614 /* There is a prevalence of the assumption that we fit the object's
4615 * page count inside a 32bit _signed_ variable. Let's document this and
4616 * catch if we ever need to fix it. In the meantime, if you do spot
4617 * such a local variable, please consider fixing!
4618 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004619 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004620 return ERR_PTR(-E2BIG);
4621
4622 if (overflows_type(size, obj->base.size))
4623 return ERR_PTR(-E2BIG);
4624
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004625 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004626 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004627 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004628
Matthew Auld465c4032017-10-06 23:18:14 +01004629 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004630 if (ret)
4631 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004632
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004633 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004634 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004635 /* 965gm cannot relocate objects above 4GiB. */
4636 mask &= ~__GFP_HIGHMEM;
4637 mask |= __GFP_DMA32;
4638 }
4639
Al Viro93c76a32015-12-04 23:45:44 -05004640 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004641 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004642 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004643
Chris Wilson37e680a2012-06-07 15:38:42 +01004644 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004645
Christian Königc0a51fd2018-02-16 13:43:38 +01004646 obj->write_domain = I915_GEM_DOMAIN_CPU;
4647 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004648
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004649 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004650 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004651 * cache) for about a 10% performance improvement
4652 * compared to uncached. Graphics requests other than
4653 * display scanout are coherent with the CPU in
4654 * accessing this cache. This means in this mode we
4655 * don't need to clflush on the CPU side, and on the
4656 * GPU side we only need to flush internal caches to
4657 * get data visible to the CPU.
4658 *
4659 * However, we maintain the display planes as UC, and so
4660 * need to rebind when first used as such.
4661 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004662 cache_level = I915_CACHE_LLC;
4663 else
4664 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004665
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004666 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004667
Daniel Vetterd861e332013-07-24 23:25:03 +02004668 trace_i915_gem_object_create(obj);
4669
Chris Wilson05394f32010-11-08 19:18:58 +00004670 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004671
4672fail:
4673 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004674 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004675}
4676
Chris Wilson340fbd82014-05-22 09:16:52 +01004677static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4678{
4679 /* If we are the last user of the backing storage (be it shmemfs
4680 * pages or stolen etc), we know that the pages are going to be
4681 * immediately released. In this case, we can then skip copying
4682 * back the contents from the GPU.
4683 */
4684
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004685 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004686 return false;
4687
4688 if (obj->base.filp == NULL)
4689 return true;
4690
4691 /* At first glance, this looks racy, but then again so would be
4692 * userspace racing mmap against close. However, the first external
4693 * reference to the filp can only be obtained through the
4694 * i915_gem_mmap_ioctl() which safeguards us against the user
4695 * acquiring such a reference whilst we are in the middle of
4696 * freeing the object.
4697 */
4698 return atomic_long_read(&obj->base.filp->f_count) == 1;
4699}
4700
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004701static void __i915_gem_free_objects(struct drm_i915_private *i915,
4702 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004703{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004704 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004705
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004706 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004707 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004708 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004709
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004710 trace_i915_gem_object_destroy(obj);
4711
Chris Wilsoncc731f52017-10-13 21:26:21 +01004712 mutex_lock(&i915->drm.struct_mutex);
4713
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004714 GEM_BUG_ON(i915_gem_object_is_active(obj));
4715 list_for_each_entry_safe(vma, vn,
4716 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004717 GEM_BUG_ON(i915_vma_is_active(vma));
4718 vma->flags &= ~I915_VMA_PIN_MASK;
4719 i915_vma_close(vma);
4720 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004721 GEM_BUG_ON(!list_empty(&obj->vma_list));
4722 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004723
Chris Wilsonf2123812017-10-16 12:40:37 +01004724 /* This serializes freeing with the shrinker. Since the free
4725 * is delayed, first by RCU then by the workqueue, we want the
4726 * shrinker to be able to free pages of unreferenced objects,
4727 * or else we may oom whilst there are plenty of deferred
4728 * freed objects.
4729 */
4730 if (i915_gem_object_has_pages(obj)) {
4731 spin_lock(&i915->mm.obj_lock);
4732 list_del_init(&obj->mm.link);
4733 spin_unlock(&i915->mm.obj_lock);
4734 }
4735
Chris Wilsoncc731f52017-10-13 21:26:21 +01004736 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004737
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004738 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004739 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004740 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004741 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004742
4743 if (obj->ops->release)
4744 obj->ops->release(obj);
4745
4746 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4747 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004748 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004749 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004750
4751 if (obj->base.import_attach)
4752 drm_prime_gem_destroy(&obj->base, NULL);
4753
Chris Wilsond07f0e52016-10-28 13:58:44 +01004754 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004755 drm_gem_object_release(&obj->base);
4756 i915_gem_info_remove_obj(i915, obj->base.size);
4757
4758 kfree(obj->bit_17);
4759 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004760
Chris Wilsonc9c704712018-02-19 22:06:31 +00004761 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4762 atomic_dec(&i915->mm.free_count);
4763
Chris Wilsoncc731f52017-10-13 21:26:21 +01004764 if (on)
4765 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004766 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004767 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004768}
4769
4770static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4771{
4772 struct llist_node *freed;
4773
Chris Wilson87701b42017-10-13 21:26:20 +01004774 /* Free the oldest, most stale object to keep the free_list short */
4775 freed = NULL;
4776 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4777 /* Only one consumer of llist_del_first() allowed */
4778 spin_lock(&i915->mm.free_lock);
4779 freed = llist_del_first(&i915->mm.free_list);
4780 spin_unlock(&i915->mm.free_lock);
4781 }
4782 if (unlikely(freed)) {
4783 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004784 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004785 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004786}
4787
4788static void __i915_gem_free_work(struct work_struct *work)
4789{
4790 struct drm_i915_private *i915 =
4791 container_of(work, struct drm_i915_private, mm.free_work);
4792 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004793
Chris Wilson2ef1e722018-01-15 20:57:59 +00004794 /*
4795 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004796 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4797 * However, the object may also be bound into the global GTT (e.g.
4798 * older GPUs without per-process support, or for direct access through
4799 * the GTT either for the user or for scanout). Those VMA still need to
4800 * unbound now.
4801 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004802
Chris Wilsonf991c492017-11-06 11:15:08 +00004803 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004804 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004805 spin_unlock(&i915->mm.free_lock);
4806
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004807 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004808 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004809 return;
4810
4811 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004812 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004813 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004814}
4815
4816static void __i915_gem_free_object_rcu(struct rcu_head *head)
4817{
4818 struct drm_i915_gem_object *obj =
4819 container_of(head, typeof(*obj), rcu);
4820 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4821
Chris Wilson2ef1e722018-01-15 20:57:59 +00004822 /*
4823 * Since we require blocking on struct_mutex to unbind the freed
4824 * object from the GPU before releasing resources back to the
4825 * system, we can not do that directly from the RCU callback (which may
4826 * be a softirq context), but must instead then defer that work onto a
4827 * kthread. We use the RCU callback rather than move the freed object
4828 * directly onto the work queue so that we can mix between using the
4829 * worker and performing frees directly from subsequent allocations for
4830 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004831 */
4832 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004833 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004834}
4835
4836void i915_gem_free_object(struct drm_gem_object *gem_obj)
4837{
4838 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4839
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004840 if (obj->mm.quirked)
4841 __i915_gem_object_unpin_pages(obj);
4842
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004843 if (discard_backing_storage(obj))
4844 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004845
Chris Wilson2ef1e722018-01-15 20:57:59 +00004846 /*
4847 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004848 * read-side critical sections are complete, e.g.
4849 * i915_gem_busy_ioctl(). For the corresponding synchronized
4850 * lookup see i915_gem_object_lookup_rcu().
4851 */
Chris Wilsonc9c704712018-02-19 22:06:31 +00004852 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004853 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004854}
4855
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004856void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4857{
4858 lockdep_assert_held(&obj->base.dev->struct_mutex);
4859
Chris Wilsond1b48c12017-08-16 09:52:08 +01004860 if (!i915_gem_object_has_active_reference(obj) &&
4861 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004862 i915_gem_object_set_active_reference(obj);
4863 else
4864 i915_gem_object_put(obj);
4865}
4866
Chris Wilsonae6c4572017-11-10 14:26:28 +00004867static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004868{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004869 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004870 struct intel_engine_cs *engine;
4871 enum intel_engine_id id;
4872
Chris Wilsonae6c4572017-11-10 14:26:28 +00004873 for_each_engine(engine, i915, id) {
4874 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4875 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4876 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004877}
4878
Chris Wilson24145512017-01-24 11:01:35 +00004879void i915_gem_sanitize(struct drm_i915_private *i915)
4880{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004881 if (i915_terminally_wedged(&i915->gpu_error)) {
4882 mutex_lock(&i915->drm.struct_mutex);
4883 i915_gem_unset_wedged(i915);
4884 mutex_unlock(&i915->drm.struct_mutex);
4885 }
4886
Chris Wilson24145512017-01-24 11:01:35 +00004887 /*
4888 * If we inherit context state from the BIOS or earlier occupants
4889 * of the GPU, the GPU may be in an inconsistent state when we
4890 * try to take over. The only way to remove the earlier state
4891 * is by resetting. However, resetting on earlier gen is tricky as
4892 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004893 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004894 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004895 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4896 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004897}
4898
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004899int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004900{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004901 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004902 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004903
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004904 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004905 intel_suspend_gt_powersave(dev_priv);
4906
Chris Wilson45c5f202013-10-16 11:50:01 +01004907 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004908
4909 /* We have to flush all the executing contexts to main memory so
4910 * that they can saved in the hibernation image. To ensure the last
4911 * context image is coherent, we have to switch away from it. That
4912 * leaves the dev_priv->kernel_context still active when
4913 * we actually suspend, and its image in memory may not match the GPU
4914 * state. Fortunately, the kernel_context is disposable and we do
4915 * not rely on its state.
4916 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004917 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4918 ret = i915_gem_switch_to_kernel_context(dev_priv);
4919 if (ret)
4920 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004921
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004922 ret = i915_gem_wait_for_idle(dev_priv,
4923 I915_WAIT_INTERRUPTIBLE |
4924 I915_WAIT_LOCKED);
4925 if (ret && ret != -EIO)
4926 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004927
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004928 assert_kernel_context_is_current(dev_priv);
4929 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004930 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004931 mutex_unlock(&dev->struct_mutex);
4932
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304933 intel_guc_suspend(dev_priv);
4934
Chris Wilson737b1502015-01-26 18:03:03 +02004935 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004936 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004937
4938 /* As the idle_work is rearming if it detects a race, play safe and
4939 * repeat the flush until it is definitely idle.
4940 */
Chris Wilson7c262402017-10-06 11:40:38 +01004941 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004942
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004943 /* Assert that we sucessfully flushed all the work and
4944 * reset the GPU back to its idle, low power state.
4945 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004946 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004947 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4948 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004949
Imre Deak1c777c52016-10-12 17:46:37 +03004950 /*
4951 * Neither the BIOS, ourselves or any other kernel
4952 * expects the system to be in execlists mode on startup,
4953 * so we need to reset the GPU back to legacy mode. And the only
4954 * known way to disable logical contexts is through a GPU reset.
4955 *
4956 * So in order to leave the system in a known default configuration,
4957 * always reset the GPU upon unload and suspend. Afterwards we then
4958 * clean up the GEM state tracking, flushing off the requests and
4959 * leaving the system in a known idle state.
4960 *
4961 * Note that is of the upmost importance that the GPU is idle and
4962 * all stray writes are flushed *before* we dismantle the backing
4963 * storage for the pinned objects.
4964 *
4965 * However, since we are uncertain that resetting the GPU on older
4966 * machines is a good idea, we don't - just in case it leaves the
4967 * machine in an unusable condition.
4968 */
Chris Wilson24145512017-01-24 11:01:35 +00004969 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004970
4971 intel_runtime_pm_put(dev_priv);
4972 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004973
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004974err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004975 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004976 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004977 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004978}
4979
Chris Wilson37cd3302017-11-12 11:27:38 +00004980void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004981{
Chris Wilson37cd3302017-11-12 11:27:38 +00004982 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004983
Chris Wilson37cd3302017-11-12 11:27:38 +00004984 mutex_lock(&i915->drm.struct_mutex);
4985 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004986
Chris Wilson37cd3302017-11-12 11:27:38 +00004987 i915_gem_restore_gtt_mappings(i915);
4988 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004989
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00004990 /*
4991 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01004992 * guarantee that the context image is complete. So let's just reset
4993 * it and start again.
4994 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004995 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004996
Chris Wilson37cd3302017-11-12 11:27:38 +00004997 if (i915_gem_init_hw(i915))
4998 goto err_wedged;
4999
Chris Wilson7469c622017-11-14 13:03:00 +00005000 intel_guc_resume(i915);
5001
Chris Wilson37cd3302017-11-12 11:27:38 +00005002 /* Always reload a context for powersaving. */
5003 if (i915_gem_switch_to_kernel_context(i915))
5004 goto err_wedged;
5005
5006out_unlock:
5007 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5008 mutex_unlock(&i915->drm.struct_mutex);
5009 return;
5010
5011err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005012 if (!i915_terminally_wedged(&i915->gpu_error)) {
5013 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5014 i915_gem_set_wedged(i915);
5015 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005016 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005017}
5018
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005019void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005020{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005021 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005022 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5023 return;
5024
5025 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5026 DISP_TILE_SURFACE_SWIZZLING);
5027
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005028 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005029 return;
5030
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005031 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005032 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005033 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005034 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005035 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005036 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005037 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005038 else
5039 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005040}
Daniel Vettere21af882012-02-09 20:53:27 +01005041
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005042static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005043{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005044 I915_WRITE(RING_CTL(base), 0);
5045 I915_WRITE(RING_HEAD(base), 0);
5046 I915_WRITE(RING_TAIL(base), 0);
5047 I915_WRITE(RING_START(base), 0);
5048}
5049
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005050static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005051{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005052 if (IS_I830(dev_priv)) {
5053 init_unused_ring(dev_priv, PRB1_BASE);
5054 init_unused_ring(dev_priv, SRB0_BASE);
5055 init_unused_ring(dev_priv, SRB1_BASE);
5056 init_unused_ring(dev_priv, SRB2_BASE);
5057 init_unused_ring(dev_priv, SRB3_BASE);
5058 } else if (IS_GEN2(dev_priv)) {
5059 init_unused_ring(dev_priv, SRB0_BASE);
5060 init_unused_ring(dev_priv, SRB1_BASE);
5061 } else if (IS_GEN3(dev_priv)) {
5062 init_unused_ring(dev_priv, PRB1_BASE);
5063 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005064 }
5065}
5066
Chris Wilson20a8a742017-02-08 14:30:31 +00005067static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005068{
Chris Wilson20a8a742017-02-08 14:30:31 +00005069 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005070 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305071 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005072 int err;
5073
5074 for_each_engine(engine, i915, id) {
5075 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005076 if (err) {
5077 DRM_ERROR("Failed to restart %s (%d)\n",
5078 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005079 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005080 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005081 }
5082
5083 return 0;
5084}
5085
5086int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5087{
Chris Wilsond200cda2016-04-28 09:56:44 +01005088 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005089
Chris Wilsonde867c22016-10-25 13:16:02 +01005090 dev_priv->gt.last_init_time = ktime_get();
5091
Chris Wilson5e4f5182015-02-13 14:35:59 +00005092 /* Double layer security blanket, see i915_gem_init() */
5093 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5094
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005095 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005096 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005097
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005098 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005099 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005100 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005101
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005102 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005103 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005104 u32 temp = I915_READ(GEN7_MSG_CTL);
5105 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5106 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005107 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005108 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5109 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5110 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5111 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005112 }
5113
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005114 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005115
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005116 /*
5117 * At least 830 can leave some of the unused rings
5118 * "active" (ie. head != tail) after resume which
5119 * will prevent c3 entry. Makes sure all unused rings
5120 * are totally idle.
5121 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005122 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005123
Dave Gordoned54c1a2016-01-19 19:02:54 +00005124 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005125 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5126 ret = -EIO;
5127 goto out;
5128 }
John Harrison90638cc2015-05-29 17:43:37 +01005129
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005130 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005131 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005132 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005133 goto out;
5134 }
5135
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005136 /* We can't enable contexts until all firmware is loaded */
5137 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005138 if (ret) {
5139 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005140 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005141 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005142
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005143 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005144
Chris Wilson136109c2017-11-02 13:14:30 +00005145 /* Only when the HW is re-initialised, can we replay the requests */
5146 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005147out:
5148 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005149 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005150}
5151
Chris Wilsond2b4b972017-11-10 14:26:33 +00005152static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5153{
5154 struct i915_gem_context *ctx;
5155 struct intel_engine_cs *engine;
5156 enum intel_engine_id id;
5157 int err;
5158
5159 /*
5160 * As we reset the gpu during very early sanitisation, the current
5161 * register state on the GPU should reflect its defaults values.
5162 * We load a context onto the hw (with restore-inhibit), then switch
5163 * over to a second context to save that default register state. We
5164 * can then prime every new context with that state so they all start
5165 * from the same default HW values.
5166 */
5167
5168 ctx = i915_gem_context_create_kernel(i915, 0);
5169 if (IS_ERR(ctx))
5170 return PTR_ERR(ctx);
5171
5172 for_each_engine(engine, i915, id) {
5173 struct drm_i915_gem_request *rq;
5174
5175 rq = i915_gem_request_alloc(engine, ctx);
5176 if (IS_ERR(rq)) {
5177 err = PTR_ERR(rq);
5178 goto out_ctx;
5179 }
5180
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005181 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005182 if (engine->init_context)
5183 err = engine->init_context(rq);
5184
5185 __i915_add_request(rq, true);
5186 if (err)
5187 goto err_active;
5188 }
5189
5190 err = i915_gem_switch_to_kernel_context(i915);
5191 if (err)
5192 goto err_active;
5193
5194 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5195 if (err)
5196 goto err_active;
5197
5198 assert_kernel_context_is_current(i915);
5199
5200 for_each_engine(engine, i915, id) {
5201 struct i915_vma *state;
5202
5203 state = ctx->engine[id].state;
5204 if (!state)
5205 continue;
5206
5207 /*
5208 * As we will hold a reference to the logical state, it will
5209 * not be torn down with the context, and importantly the
5210 * object will hold onto its vma (making it possible for a
5211 * stray GTT write to corrupt our defaults). Unmap the vma
5212 * from the GTT to prevent such accidents and reclaim the
5213 * space.
5214 */
5215 err = i915_vma_unbind(state);
5216 if (err)
5217 goto err_active;
5218
5219 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5220 if (err)
5221 goto err_active;
5222
5223 engine->default_state = i915_gem_object_get(state->obj);
5224 }
5225
5226 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5227 unsigned int found = intel_engines_has_context_isolation(i915);
5228
5229 /*
5230 * Make sure that classes with multiple engine instances all
5231 * share the same basic configuration.
5232 */
5233 for_each_engine(engine, i915, id) {
5234 unsigned int bit = BIT(engine->uabi_class);
5235 unsigned int expected = engine->default_state ? bit : 0;
5236
5237 if ((found & bit) != expected) {
5238 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5239 engine->uabi_class, engine->name);
5240 }
5241 }
5242 }
5243
5244out_ctx:
5245 i915_gem_context_set_closed(ctx);
5246 i915_gem_context_put(ctx);
5247 return err;
5248
5249err_active:
5250 /*
5251 * If we have to abandon now, we expect the engines to be idle
5252 * and ready to be torn-down. First try to flush any remaining
5253 * request, ensure we are pointing at the kernel context and
5254 * then remove it.
5255 */
5256 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5257 goto out_ctx;
5258
5259 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5260 goto out_ctx;
5261
5262 i915_gem_contexts_lost(i915);
5263 goto out_ctx;
5264}
5265
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005266int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005267{
Chris Wilson1070a422012-04-24 15:47:41 +01005268 int ret;
5269
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005270 /*
5271 * We need to fallback to 4K pages since gvt gtt handling doesn't
5272 * support huge page entries - we will need to check either hypervisor
5273 * mm can support huge guest page or just do emulation in gvt.
5274 */
5275 if (intel_vgpu_active(dev_priv))
5276 mkwrite_device_info(dev_priv)->page_sizes =
5277 I915_GTT_PAGE_SIZE_4K;
5278
Chris Wilson94312822017-05-03 10:39:18 +01005279 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005280
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005281 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005282 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005283 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005284 } else {
5285 dev_priv->gt.resume = intel_legacy_submission_resume;
5286 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005287 }
5288
Chris Wilsonee487002017-11-22 17:26:21 +00005289 ret = i915_gem_init_userptr(dev_priv);
5290 if (ret)
5291 return ret;
5292
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305293 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005294 if (ret)
5295 return ret;
5296
Chris Wilson5e4f5182015-02-13 14:35:59 +00005297 /* This is just a security blanket to placate dragons.
5298 * On some systems, we very sporadically observe that the first TLBs
5299 * used by the CS may be stale, despite us poking the TLB reset. If
5300 * we hold the forcewake during initialisation these problems
5301 * just magically go away.
5302 */
Chris Wilsonee487002017-11-22 17:26:21 +00005303 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005304 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5305
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005306 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005307 if (ret) {
5308 GEM_BUG_ON(ret == -EIO);
5309 goto err_unlock;
5310 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005311
Chris Wilson829a0af2017-06-20 12:05:45 +01005312 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005313 if (ret) {
5314 GEM_BUG_ON(ret == -EIO);
5315 goto err_ggtt;
5316 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005317
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005318 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005319 if (ret) {
5320 GEM_BUG_ON(ret == -EIO);
5321 goto err_context;
5322 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005323
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005324 intel_init_gt_powersave(dev_priv);
5325
Michał Winiarski61b5c152017-12-13 23:13:48 +01005326 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005327 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005328 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005329
Michał Winiarski61b5c152017-12-13 23:13:48 +01005330 ret = i915_gem_init_hw(dev_priv);
5331 if (ret)
5332 goto err_uc_init;
5333
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005334 /*
5335 * Despite its name intel_init_clock_gating applies both display
5336 * clock gating workarounds; GT mmio workarounds and the occasional
5337 * GT power context workaround. Worse, sometimes it includes a context
5338 * register workaround which we need to apply before we record the
5339 * default HW state for all contexts.
5340 *
5341 * FIXME: break up the workarounds and apply them at the right time!
5342 */
5343 intel_init_clock_gating(dev_priv);
5344
Chris Wilsond2b4b972017-11-10 14:26:33 +00005345 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005346 if (ret)
5347 goto err_init_hw;
5348
5349 if (i915_inject_load_failure()) {
5350 ret = -ENODEV;
5351 goto err_init_hw;
5352 }
5353
5354 if (i915_inject_load_failure()) {
5355 ret = -EIO;
5356 goto err_init_hw;
5357 }
5358
5359 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5360 mutex_unlock(&dev_priv->drm.struct_mutex);
5361
5362 return 0;
5363
5364 /*
5365 * Unwinding is complicated by that we want to handle -EIO to mean
5366 * disable GPU submission but keep KMS alive. We want to mark the
5367 * HW as irrevisibly wedged, but keep enough state around that the
5368 * driver doesn't explode during runtime.
5369 */
5370err_init_hw:
5371 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5372 i915_gem_contexts_lost(dev_priv);
5373 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005374err_uc_init:
5375 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005376err_pm:
5377 if (ret != -EIO) {
5378 intel_cleanup_gt_powersave(dev_priv);
5379 i915_gem_cleanup_engines(dev_priv);
5380 }
5381err_context:
5382 if (ret != -EIO)
5383 i915_gem_contexts_fini(dev_priv);
5384err_ggtt:
5385err_unlock:
5386 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5387 mutex_unlock(&dev_priv->drm.struct_mutex);
5388
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305389 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305390
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005391 if (ret != -EIO)
5392 i915_gem_cleanup_userptr(dev_priv);
5393
Chris Wilson60990322014-04-09 09:19:42 +01005394 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005395 /*
5396 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005397 * wedged. But we only want to do this where the GPU is angry,
5398 * for all other failure, such as an allocation failure, bail.
5399 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005400 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5401 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5402 i915_gem_set_wedged(dev_priv);
5403 }
Chris Wilson60990322014-04-09 09:19:42 +01005404 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005405 }
5406
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005407 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005408 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005409}
5410
Chris Wilson24145512017-01-24 11:01:35 +00005411void i915_gem_init_mmio(struct drm_i915_private *i915)
5412{
5413 i915_gem_sanitize(i915);
5414}
5415
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005416void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005417i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005418{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005419 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305420 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005421
Akash Goel3b3f1652016-10-13 22:44:48 +05305422 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005423 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005424}
5425
Eric Anholt673a3942008-07-30 12:06:12 -07005426void
Imre Deak40ae4e12016-03-16 14:54:03 +02005427i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5428{
Chris Wilson49ef5292016-08-18 17:17:00 +01005429 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005430
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005431 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005432 !IS_CHERRYVIEW(dev_priv))
5433 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005434 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005435 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5436 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005437 dev_priv->num_fence_regs = 16;
5438 else
5439 dev_priv->num_fence_regs = 8;
5440
Chris Wilsonc0336662016-05-06 15:40:21 +01005441 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005442 dev_priv->num_fence_regs =
5443 I915_READ(vgtif_reg(avail_rs.fence_num));
5444
5445 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005446 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5447 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5448
5449 fence->i915 = dev_priv;
5450 fence->id = i;
5451 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5452 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005453 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005454
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005455 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005456}
5457
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005458static void i915_gem_init__mm(struct drm_i915_private *i915)
5459{
5460 spin_lock_init(&i915->mm.object_stat_lock);
5461 spin_lock_init(&i915->mm.obj_lock);
5462 spin_lock_init(&i915->mm.free_lock);
5463
5464 init_llist_head(&i915->mm.free_list);
5465
5466 INIT_LIST_HEAD(&i915->mm.unbound_list);
5467 INIT_LIST_HEAD(&i915->mm.bound_list);
5468 INIT_LIST_HEAD(&i915->mm.fence_list);
5469 INIT_LIST_HEAD(&i915->mm.userfault_list);
5470
5471 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5472}
5473
Chris Wilson73cb9702016-10-28 13:58:46 +01005474int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005475i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005476{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005477 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005478
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005479 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5480 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005481 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005482
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005483 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5484 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005485 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005486
Chris Wilsond1b48c12017-08-16 09:52:08 +01005487 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5488 if (!dev_priv->luts)
5489 goto err_vmas;
5490
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005491 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
5492 SLAB_HWCACHE_ALIGN |
5493 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005494 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005495 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005496 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005497
Chris Wilson52e54202016-11-14 20:41:02 +00005498 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5499 SLAB_HWCACHE_ALIGN |
5500 SLAB_RECLAIM_ACCOUNT);
5501 if (!dev_priv->dependencies)
5502 goto err_requests;
5503
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005504 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5505 if (!dev_priv->priorities)
5506 goto err_dependencies;
5507
Chris Wilson73cb9702016-10-28 13:58:46 +01005508 mutex_lock(&dev_priv->drm.struct_mutex);
5509 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005510 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005511 mutex_unlock(&dev_priv->drm.struct_mutex);
5512 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005513 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005514
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005515 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005516
Chris Wilson67d97da2016-07-04 08:08:31 +01005517 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005518 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005519 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005520 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005521 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005522 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005523
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005524 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5525
Chris Wilsonb5add952016-08-04 16:32:36 +01005526 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005527
Matthew Auld465c4032017-10-06 23:18:14 +01005528 err = i915_gemfs_init(dev_priv);
5529 if (err)
5530 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5531
Chris Wilson73cb9702016-10-28 13:58:46 +01005532 return 0;
5533
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005534err_priorities:
5535 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005536err_dependencies:
5537 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005538err_requests:
5539 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005540err_luts:
5541 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005542err_vmas:
5543 kmem_cache_destroy(dev_priv->vmas);
5544err_objects:
5545 kmem_cache_destroy(dev_priv->objects);
5546err_out:
5547 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005548}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005549
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005550void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005551{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005552 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c704712018-02-19 22:06:31 +00005553 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5554 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005555 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005556
Matthew Auldea84aa72016-11-17 21:04:11 +00005557 mutex_lock(&dev_priv->drm.struct_mutex);
5558 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5559 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5560 mutex_unlock(&dev_priv->drm.struct_mutex);
5561
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005562 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005563 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005564 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005565 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005566 kmem_cache_destroy(dev_priv->vmas);
5567 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005568
5569 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5570 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005571
5572 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005573}
5574
Chris Wilson6a800ea2016-09-21 14:51:07 +01005575int i915_gem_freeze(struct drm_i915_private *dev_priv)
5576{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005577 /* Discard all purgeable objects, let userspace recover those as
5578 * required after resuming.
5579 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005580 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005581
Chris Wilson6a800ea2016-09-21 14:51:07 +01005582 return 0;
5583}
5584
Chris Wilson461fb992016-05-14 07:26:33 +01005585int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5586{
5587 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005588 struct list_head *phases[] = {
5589 &dev_priv->mm.unbound_list,
5590 &dev_priv->mm.bound_list,
5591 NULL
5592 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005593
5594 /* Called just before we write the hibernation image.
5595 *
5596 * We need to update the domain tracking to reflect that the CPU
5597 * will be accessing all the pages to create and restore from the
5598 * hibernation, and so upon restoration those pages will be in the
5599 * CPU domain.
5600 *
5601 * To make sure the hibernation image contains the latest state,
5602 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005603 *
5604 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005605 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005606 */
5607
Chris Wilson912d5722017-09-06 16:19:30 -07005608 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005609 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005610
Chris Wilsonf2123812017-10-16 12:40:37 +01005611 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005612 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005613 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005614 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005615 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005616 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005617
5618 return 0;
5619}
5620
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005621void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005622{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005623 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01005624 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005625
5626 /* Clean up our request list when the client is going away, so that
5627 * later retire_requests won't dereference our soon-to-be-gone
5628 * file_priv.
5629 */
Chris Wilson1c255952010-09-26 11:03:27 +01005630 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005631 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005632 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005633 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005634}
5635
Chris Wilson829a0af2017-06-20 12:05:45 +01005636int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005637{
5638 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005639 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005640
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005641 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005642
5643 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5644 if (!file_priv)
5645 return -ENOMEM;
5646
5647 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005648 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005649 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005650
5651 spin_lock_init(&file_priv->mm.lock);
5652 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005653
Chris Wilsonc80ff162016-07-27 09:07:27 +01005654 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005655
Chris Wilson829a0af2017-06-20 12:05:45 +01005656 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005657 if (ret)
5658 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005659
Ben Widawskye422b882013-12-06 14:10:58 -08005660 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005661}
5662
Daniel Vetterb680c372014-09-19 18:27:27 +02005663/**
5664 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005665 * @old: current GEM buffer for the frontbuffer slots
5666 * @new: new GEM buffer for the frontbuffer slots
5667 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005668 *
5669 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5670 * from @old and setting them in @new. Both @old and @new can be NULL.
5671 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005672void i915_gem_track_fb(struct drm_i915_gem_object *old,
5673 struct drm_i915_gem_object *new,
5674 unsigned frontbuffer_bits)
5675{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005676 /* Control of individual bits within the mask are guarded by
5677 * the owning plane->mutex, i.e. we can never see concurrent
5678 * manipulation of individual bits. But since the bitfield as a whole
5679 * is updated using RMW, we need to use atomics in order to update
5680 * the bits.
5681 */
5682 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5683 sizeof(atomic_t) * BITS_PER_BYTE);
5684
Daniel Vettera071fa02014-06-18 23:28:09 +02005685 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005686 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5687 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005688 }
5689
5690 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005691 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5692 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005693 }
5694}
5695
Dave Gordonea702992015-07-09 19:29:02 +01005696/* Allocate a new GEM object and fill it with the supplied data */
5697struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005698i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005699 const void *data, size_t size)
5700{
5701 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005702 struct file *file;
5703 size_t offset;
5704 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005705
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005706 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005707 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005708 return obj;
5709
Christian Königc0a51fd2018-02-16 13:43:38 +01005710 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005711
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005712 file = obj->base.filp;
5713 offset = 0;
5714 do {
5715 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5716 struct page *page;
5717 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005718
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005719 err = pagecache_write_begin(file, file->f_mapping,
5720 offset, len, 0,
5721 &page, &pgdata);
5722 if (err < 0)
5723 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005724
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005725 vaddr = kmap(page);
5726 memcpy(vaddr, data, len);
5727 kunmap(page);
5728
5729 err = pagecache_write_end(file, file->f_mapping,
5730 offset, len, len,
5731 page, pgdata);
5732 if (err < 0)
5733 goto fail;
5734
5735 size -= len;
5736 data += len;
5737 offset += len;
5738 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005739
5740 return obj;
5741
5742fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005743 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005744 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005745}
Chris Wilson96d77632016-10-28 13:58:33 +01005746
5747struct scatterlist *
5748i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5749 unsigned int n,
5750 unsigned int *offset)
5751{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005752 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005753 struct scatterlist *sg;
5754 unsigned int idx, count;
5755
5756 might_sleep();
5757 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005758 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005759
5760 /* As we iterate forward through the sg, we record each entry in a
5761 * radixtree for quick repeated (backwards) lookups. If we have seen
5762 * this index previously, we will have an entry for it.
5763 *
5764 * Initial lookup is O(N), but this is amortized to O(1) for
5765 * sequential page access (where each new request is consecutive
5766 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5767 * i.e. O(1) with a large constant!
5768 */
5769 if (n < READ_ONCE(iter->sg_idx))
5770 goto lookup;
5771
5772 mutex_lock(&iter->lock);
5773
5774 /* We prefer to reuse the last sg so that repeated lookup of this
5775 * (or the subsequent) sg are fast - comparing against the last
5776 * sg is faster than going through the radixtree.
5777 */
5778
5779 sg = iter->sg_pos;
5780 idx = iter->sg_idx;
5781 count = __sg_page_count(sg);
5782
5783 while (idx + count <= n) {
5784 unsigned long exception, i;
5785 int ret;
5786
5787 /* If we cannot allocate and insert this entry, or the
5788 * individual pages from this range, cancel updating the
5789 * sg_idx so that on this lookup we are forced to linearly
5790 * scan onwards, but on future lookups we will try the
5791 * insertion again (in which case we need to be careful of
5792 * the error return reporting that we have already inserted
5793 * this index).
5794 */
5795 ret = radix_tree_insert(&iter->radix, idx, sg);
5796 if (ret && ret != -EEXIST)
5797 goto scan;
5798
5799 exception =
5800 RADIX_TREE_EXCEPTIONAL_ENTRY |
5801 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5802 for (i = 1; i < count; i++) {
5803 ret = radix_tree_insert(&iter->radix, idx + i,
5804 (void *)exception);
5805 if (ret && ret != -EEXIST)
5806 goto scan;
5807 }
5808
5809 idx += count;
5810 sg = ____sg_next(sg);
5811 count = __sg_page_count(sg);
5812 }
5813
5814scan:
5815 iter->sg_pos = sg;
5816 iter->sg_idx = idx;
5817
5818 mutex_unlock(&iter->lock);
5819
5820 if (unlikely(n < idx)) /* insertion completed by another thread */
5821 goto lookup;
5822
5823 /* In case we failed to insert the entry into the radixtree, we need
5824 * to look beyond the current sg.
5825 */
5826 while (idx + count <= n) {
5827 idx += count;
5828 sg = ____sg_next(sg);
5829 count = __sg_page_count(sg);
5830 }
5831
5832 *offset = n - idx;
5833 return sg;
5834
5835lookup:
5836 rcu_read_lock();
5837
5838 sg = radix_tree_lookup(&iter->radix, n);
5839 GEM_BUG_ON(!sg);
5840
5841 /* If this index is in the middle of multi-page sg entry,
5842 * the radixtree will contain an exceptional entry that points
5843 * to the start of that range. We will return the pointer to
5844 * the base page and the offset of this page within the
5845 * sg entry's range.
5846 */
5847 *offset = 0;
5848 if (unlikely(radix_tree_exception(sg))) {
5849 unsigned long base =
5850 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5851
5852 sg = radix_tree_lookup(&iter->radix, base);
5853 GEM_BUG_ON(!sg);
5854
5855 *offset = n - base;
5856 }
5857
5858 rcu_read_unlock();
5859
5860 return sg;
5861}
5862
5863struct page *
5864i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5865{
5866 struct scatterlist *sg;
5867 unsigned int offset;
5868
5869 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5870
5871 sg = i915_gem_object_get_sg(obj, n, &offset);
5872 return nth_page(sg_page(sg), offset);
5873}
5874
5875/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5876struct page *
5877i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5878 unsigned int n)
5879{
5880 struct page *page;
5881
5882 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005883 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005884 set_page_dirty(page);
5885
5886 return page;
5887}
5888
5889dma_addr_t
5890i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5891 unsigned long n)
5892{
5893 struct scatterlist *sg;
5894 unsigned int offset;
5895
5896 sg = i915_gem_object_get_sg(obj, n, &offset);
5897 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5898}
Chris Wilson935a2f72017-02-13 17:15:13 +00005899
Chris Wilson8eeb7902017-07-26 19:16:01 +01005900int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5901{
5902 struct sg_table *pages;
5903 int err;
5904
5905 if (align > obj->base.size)
5906 return -EINVAL;
5907
5908 if (obj->ops == &i915_gem_phys_ops)
5909 return 0;
5910
5911 if (obj->ops != &i915_gem_object_ops)
5912 return -EINVAL;
5913
5914 err = i915_gem_object_unbind(obj);
5915 if (err)
5916 return err;
5917
5918 mutex_lock(&obj->mm.lock);
5919
5920 if (obj->mm.madv != I915_MADV_WILLNEED) {
5921 err = -EFAULT;
5922 goto err_unlock;
5923 }
5924
5925 if (obj->mm.quirked) {
5926 err = -EFAULT;
5927 goto err_unlock;
5928 }
5929
5930 if (obj->mm.mapping) {
5931 err = -EBUSY;
5932 goto err_unlock;
5933 }
5934
Chris Wilsonf2123812017-10-16 12:40:37 +01005935 pages = fetch_and_zero(&obj->mm.pages);
5936 if (pages) {
5937 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5938
5939 __i915_gem_object_reset_page_iter(obj);
5940
5941 spin_lock(&i915->mm.obj_lock);
5942 list_del(&obj->mm.link);
5943 spin_unlock(&i915->mm.obj_lock);
5944 }
5945
Chris Wilson8eeb7902017-07-26 19:16:01 +01005946 obj->ops = &i915_gem_phys_ops;
5947
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005948 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005949 if (err)
5950 goto err_xfer;
5951
5952 /* Perma-pin (until release) the physical set of pages */
5953 __i915_gem_object_pin_pages(obj);
5954
5955 if (!IS_ERR_OR_NULL(pages))
5956 i915_gem_object_ops.put_pages(obj, pages);
5957 mutex_unlock(&obj->mm.lock);
5958 return 0;
5959
5960err_xfer:
5961 obj->ops = &i915_gem_object_ops;
5962 obj->mm.pages = pages;
5963err_unlock:
5964 mutex_unlock(&obj->mm.lock);
5965 return err;
5966}
5967
Chris Wilson935a2f72017-02-13 17:15:13 +00005968#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5969#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005970#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005971#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005972#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005973#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005974#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005975#endif