blob: 80b78fb5daac2b768df980183170a0506f9c55a0 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Matthew Auld465c4032017-10-06 23:18:14 +010038#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000039#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000040#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010041#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070042#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000044#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070045#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080046#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020047#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070048
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010049static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010050
Chris Wilson2c225692013-08-09 12:26:45 +010051static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
52{
Chris Wilsone27ab732017-06-15 13:38:49 +010053 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053054 return false;
55
Chris Wilsonb8f55be2017-08-11 12:11:16 +010056 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010057 return true;
58
Chris Wilsonbd3d2252017-10-13 21:26:14 +010059 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010060}
61
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053062static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010063insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053064 struct drm_mm_node *node, u32 size)
65{
66 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000067 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
68 size, 0, I915_COLOR_UNEVICTABLE,
69 0, ggtt->mappable_end,
70 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053071}
72
73static void
74remove_mappable_node(struct drm_mm_node *node)
75{
76 drm_mm_remove_node(node);
77}
78
Chris Wilson73aa8082010-09-30 11:46:12 +010079/* some bookkeeping */
80static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010081 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010082{
Daniel Vetterc20e8352013-07-24 22:40:23 +020083 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010084 dev_priv->mm.object_count++;
85 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020086 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010087}
88
89static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010090 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010091{
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093 dev_priv->mm.object_count--;
94 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096}
97
Chris Wilson21dd3732011-01-26 15:55:56 +000098static int
Daniel Vetter33196de2012-11-14 17:14:05 +010099i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101 int ret;
102
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100103 might_sleep();
104
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200105 /*
106 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
107 * userspace. If it takes that long something really bad is going on and
108 * we should simply try to bail out and fail as gracefully as possible.
109 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100110 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000111 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100112 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 if (ret == 0) {
114 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
115 return -EIO;
116 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100118 } else {
119 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200120 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121}
122
Chris Wilson54cf91d2010-11-25 18:00:26 +0000123int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100124{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100125 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100126 int ret;
127
Daniel Vetter33196de2012-11-14 17:14:05 +0100128 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 if (ret)
130 return ret;
131
132 ret = mutex_lock_interruptible(&dev->struct_mutex);
133 if (ret)
134 return ret;
135
Chris Wilson76c1dec2010-09-25 11:22:51 +0100136 return 0;
137}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100138
Eric Anholt673a3942008-07-30 12:06:12 -0700139int
Eric Anholt5a125c32008-10-22 21:40:13 -0700140i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000141 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700142{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300143 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200144 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300145 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100146 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800147 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700148
Weinan Liff8f7972017-05-31 10:35:52 +0800149 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100150 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000151 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100152 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100153 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000154 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100155 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100156 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100157 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300159 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000161
Eric Anholt5a125c32008-10-22 21:40:13 -0700162 return 0;
163}
164
Matthew Auldb91b09e2017-10-06 23:18:17 +0100165static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100166{
Al Viro93c76a32015-12-04 23:45:44 -0500167 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000168 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800169 struct sg_table *st;
170 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000171 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800172 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100173 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100174
Chris Wilson6a2c4232014-11-04 04:51:40 -0800175 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100176 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100177
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 /* Always aligning to the object size, allows a single allocation
179 * to handle all possible callers, and given typical object sizes,
180 * the alignment of the buddy allocation will naturally match.
181 */
182 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300183 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000184 roundup_pow_of_two(obj->base.size));
185 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100186 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000187
188 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800189 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
190 struct page *page;
191 char *src;
192
193 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000194 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100195 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000196 goto err_phys;
197 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800198
199 src = kmap_atomic(page);
200 memcpy(vaddr, src, PAGE_SIZE);
201 drm_clflush_virt_range(vaddr, PAGE_SIZE);
202 kunmap_atomic(src);
203
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300204 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800205 vaddr += PAGE_SIZE;
206 }
207
Chris Wilsonc0336662016-05-06 15:40:21 +0100208 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800209
210 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000211 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100212 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000213 goto err_phys;
214 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800215
216 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
217 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100218 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000219 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800220 }
221
222 sg = st->sgl;
223 sg->offset = 0;
224 sg->length = obj->base.size;
225
Chris Wilsondbb43512016-12-07 13:34:11 +0000226 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800227 sg_dma_len(sg) = obj->base.size;
228
Chris Wilsondbb43512016-12-07 13:34:11 +0000229 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100230
Matthew Aulda5c081662017-10-06 23:18:18 +0100231 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100232
233 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000234
235err_phys:
236 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100237
238 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239}
240
Chris Wilsone27ab732017-06-15 13:38:49 +0100241static void __start_cpu_write(struct drm_i915_gem_object *obj)
242{
243 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
244 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
245 if (cpu_write_needs_clflush(obj))
246 obj->cache_dirty = true;
247}
248
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000250__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000251 struct sg_table *pages,
252 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800253{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100254 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800255
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100256 if (obj->mm.madv == I915_MADV_DONTNEED)
257 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800258
Chris Wilsone5facdf2016-12-23 14:57:57 +0000259 if (needs_clflush &&
260 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100261 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000262 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100263
Chris Wilsone27ab732017-06-15 13:38:49 +0100264 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100265}
266
267static void
268i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
269 struct sg_table *pages)
270{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000271 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100272
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100273 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500274 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100276 int i;
277
278 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800279 struct page *page;
280 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100281
Chris Wilson6a2c4232014-11-04 04:51:40 -0800282 page = shmem_read_mapping_page(mapping, i);
283 if (IS_ERR(page))
284 continue;
285
286 dst = kmap_atomic(page);
287 drm_clflush_virt_range(vaddr, PAGE_SIZE);
288 memcpy(dst, vaddr, PAGE_SIZE);
289 kunmap_atomic(dst);
290
291 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100292 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100293 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100295 vaddr += PAGE_SIZE;
296 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100297 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100298 }
299
Chris Wilson03ac84f2016-10-28 13:58:36 +0100300 sg_free_table(pages);
301 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000302
303 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800304}
305
306static void
307i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
308{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100309 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800310}
311
312static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
313 .get_pages = i915_gem_object_get_pages_phys,
314 .put_pages = i915_gem_object_put_pages_phys,
315 .release = i915_gem_object_release_phys,
316};
317
Chris Wilson581ab1f2017-02-15 16:39:00 +0000318static const struct drm_i915_gem_object_ops i915_gem_object_ops;
319
Chris Wilson35a96112016-08-14 18:44:40 +0100320int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100321{
322 struct i915_vma *vma;
323 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100324 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100325
Chris Wilson02bef8f2016-08-14 18:44:41 +0100326 lockdep_assert_held(&obj->base.dev->struct_mutex);
327
328 /* Closed vma are removed from the obj->vma_list - but they may
329 * still have an active binding on the object. To remove those we
330 * must wait for all rendering to complete to the object (as unbinding
331 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100332 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000333 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100334 if (ret)
335 return ret;
336
Chris Wilsonaa653a62016-08-04 07:52:27 +0100337 while ((vma = list_first_entry_or_null(&obj->vma_list,
338 struct i915_vma,
339 obj_link))) {
340 list_move_tail(&vma->obj_link, &still_in_list);
341 ret = i915_vma_unbind(vma);
342 if (ret)
343 break;
344 }
345 list_splice(&still_in_list, &obj->vma_list);
346
347 return ret;
348}
349
Chris Wilsone95433c2016-10-28 13:58:27 +0100350static long
351i915_gem_object_wait_fence(struct dma_fence *fence,
352 unsigned int flags,
353 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100354 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100355{
356 struct drm_i915_gem_request *rq;
357
358 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
359
360 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
361 return timeout;
362
363 if (!dma_fence_is_i915(fence))
364 return dma_fence_wait_timeout(fence,
365 flags & I915_WAIT_INTERRUPTIBLE,
366 timeout);
367
368 rq = to_request(fence);
369 if (i915_gem_request_completed(rq))
370 goto out;
371
372 /* This client is about to stall waiting for the GPU. In many cases
373 * this is undesirable and limits the throughput of the system, as
374 * many clients cannot continue processing user input/output whilst
375 * blocked. RPS autotuning may take tens of milliseconds to respond
376 * to the GPU load and thus incurs additional latency for the client.
377 * We can circumvent that by promoting the GPU frequency to maximum
378 * before we wait. This makes the GPU throttle up much more quickly
379 * (good for benchmarks and user experience, e.g. window animations),
380 * but at a cost of spending more power processing the workload
381 * (bad for battery). Not all clients even want their results
382 * immediately and for them we should just let the GPU select its own
383 * frequency to maximise efficiency. To prevent a single client from
384 * forcing the clocks too high for the whole system, we only allow
385 * each client to waitboost once in a busy period.
386 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100387 if (rps_client) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100388 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100389 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100390 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100391 rps_client = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +0100392 }
393
394 timeout = i915_wait_request(rq, flags, timeout);
395
396out:
397 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
398 i915_gem_request_retire_upto(rq);
399
Chris Wilsone95433c2016-10-28 13:58:27 +0100400 return timeout;
401}
402
403static long
404i915_gem_object_wait_reservation(struct reservation_object *resv,
405 unsigned int flags,
406 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100407 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100408{
Chris Wilsone54ca972017-02-17 15:13:04 +0000409 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100410 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000411 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100412
413 if (flags & I915_WAIT_ALL) {
414 struct dma_fence **shared;
415 unsigned int count, i;
416 int ret;
417
418 ret = reservation_object_get_fences_rcu(resv,
419 &excl, &count, &shared);
420 if (ret)
421 return ret;
422
423 for (i = 0; i < count; i++) {
424 timeout = i915_gem_object_wait_fence(shared[i],
425 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100426 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000427 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100428 break;
429
430 dma_fence_put(shared[i]);
431 }
432
433 for (; i < count; i++)
434 dma_fence_put(shared[i]);
435 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000436
437 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100438 } else {
439 excl = reservation_object_get_excl_rcu(resv);
440 }
441
Chris Wilsone54ca972017-02-17 15:13:04 +0000442 if (excl && timeout >= 0) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100443 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
444 rps_client);
Chris Wilsone54ca972017-02-17 15:13:04 +0000445 prune_fences = timeout >= 0;
446 }
Chris Wilsone95433c2016-10-28 13:58:27 +0100447
448 dma_fence_put(excl);
449
Chris Wilson03d1cac2017-03-08 13:26:28 +0000450 /* Oportunistically prune the fences iff we know they have *all* been
451 * signaled and that the reservation object has not been changed (i.e.
452 * no new fences have been added).
453 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000454 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000455 if (reservation_object_trylock(resv)) {
456 if (!__read_seqcount_retry(&resv->seq, seq))
457 reservation_object_add_excl_fence(resv, NULL);
458 reservation_object_unlock(resv);
459 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000460 }
461
Chris Wilsone95433c2016-10-28 13:58:27 +0100462 return timeout;
463}
464
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000465static void __fence_set_priority(struct dma_fence *fence, int prio)
466{
467 struct drm_i915_gem_request *rq;
468 struct intel_engine_cs *engine;
469
470 if (!dma_fence_is_i915(fence))
471 return;
472
473 rq = to_request(fence);
474 engine = rq->engine;
475 if (!engine->schedule)
476 return;
477
478 engine->schedule(rq, prio);
479}
480
481static void fence_set_priority(struct dma_fence *fence, int prio)
482{
483 /* Recurse once into a fence-array */
484 if (dma_fence_is_array(fence)) {
485 struct dma_fence_array *array = to_dma_fence_array(fence);
486 int i;
487
488 for (i = 0; i < array->num_fences; i++)
489 __fence_set_priority(array->fences[i], prio);
490 } else {
491 __fence_set_priority(fence, prio);
492 }
493}
494
495int
496i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
497 unsigned int flags,
498 int prio)
499{
500 struct dma_fence *excl;
501
502 if (flags & I915_WAIT_ALL) {
503 struct dma_fence **shared;
504 unsigned int count, i;
505 int ret;
506
507 ret = reservation_object_get_fences_rcu(obj->resv,
508 &excl, &count, &shared);
509 if (ret)
510 return ret;
511
512 for (i = 0; i < count; i++) {
513 fence_set_priority(shared[i], prio);
514 dma_fence_put(shared[i]);
515 }
516
517 kfree(shared);
518 } else {
519 excl = reservation_object_get_excl_rcu(obj->resv);
520 }
521
522 if (excl) {
523 fence_set_priority(excl, prio);
524 dma_fence_put(excl);
525 }
526 return 0;
527}
528
Chris Wilson00e60f22016-08-04 16:32:40 +0100529/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100530 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100531 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100532 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
533 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000534 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100535 */
536int
Chris Wilsone95433c2016-10-28 13:58:27 +0100537i915_gem_object_wait(struct drm_i915_gem_object *obj,
538 unsigned int flags,
539 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100540 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100541{
Chris Wilsone95433c2016-10-28 13:58:27 +0100542 might_sleep();
543#if IS_ENABLED(CONFIG_LOCKDEP)
544 GEM_BUG_ON(debug_locks &&
545 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
546 !!(flags & I915_WAIT_LOCKED));
547#endif
548 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100549
Chris Wilsond07f0e52016-10-28 13:58:44 +0100550 timeout = i915_gem_object_wait_reservation(obj->resv,
551 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100552 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100553 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100554}
555
556static struct intel_rps_client *to_rps_client(struct drm_file *file)
557{
558 struct drm_i915_file_private *fpriv = file->driver_priv;
559
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100560 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100561}
562
Chris Wilson00731152014-05-21 12:42:56 +0100563static int
564i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
565 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100566 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100567{
Chris Wilson00731152014-05-21 12:42:56 +0100568 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300569 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800570
571 /* We manually control the domain here and pretend that it
572 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
573 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700574 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000575 if (copy_from_user(vaddr, user_data, args->size))
576 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100577
Chris Wilson6a2c4232014-11-04 04:51:40 -0800578 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000579 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200580
Chris Wilsond59b21e2017-02-22 11:40:49 +0000581 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000582 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100583}
584
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000585void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000586{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100587 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000588}
589
590void i915_gem_object_free(struct drm_i915_gem_object *obj)
591{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100592 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100593 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000594}
595
Dave Airlieff72145b2011-02-07 12:16:14 +1000596static int
597i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000598 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000599 uint64_t size,
600 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700601{
Chris Wilson05394f32010-11-08 19:18:58 +0000602 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300603 int ret;
604 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700605
Dave Airlieff72145b2011-02-07 12:16:14 +1000606 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200607 if (size == 0)
608 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700609
610 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000611 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100612 if (IS_ERR(obj))
613 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700614
Chris Wilson05394f32010-11-08 19:18:58 +0000615 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100616 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100617 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200618 if (ret)
619 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100620
Dave Airlieff72145b2011-02-07 12:16:14 +1000621 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700622 return 0;
623}
624
Dave Airlieff72145b2011-02-07 12:16:14 +1000625int
626i915_gem_dumb_create(struct drm_file *file,
627 struct drm_device *dev,
628 struct drm_mode_create_dumb *args)
629{
630 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300631 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000632 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000633 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000634 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000635}
636
Chris Wilsone27ab732017-06-15 13:38:49 +0100637static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
638{
639 return !(obj->cache_level == I915_CACHE_NONE ||
640 obj->cache_level == I915_CACHE_WT);
641}
642
Dave Airlieff72145b2011-02-07 12:16:14 +1000643/**
644 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100645 * @dev: drm device pointer
646 * @data: ioctl data blob
647 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000648 */
649int
650i915_gem_create_ioctl(struct drm_device *dev, void *data,
651 struct drm_file *file)
652{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000653 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000654 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200655
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000656 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100657
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000658 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000659 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000660}
661
Chris Wilsonef749212017-04-12 12:01:10 +0100662static inline enum fb_op_origin
663fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
664{
665 return (domain == I915_GEM_DOMAIN_GTT ?
666 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
667}
668
669static void
670flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
671{
672 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
673
674 if (!(obj->base.write_domain & flush_domains))
675 return;
676
677 /* No actual flushing is required for the GTT write domain. Writes
678 * to it "immediately" go to main memory as far as we know, so there's
679 * no chipset flush. It also doesn't land in render cache.
680 *
681 * However, we do have to enforce the order so that all writes through
682 * the GTT land before any writes to the device, such as updates to
683 * the GATT itself.
684 *
685 * We also have to wait a bit for the writes to land from the GTT.
686 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
687 * timing. This issue has only been observed when switching quickly
688 * between GTT writes and CPU reads from inside the kernel on recent hw,
689 * and it appears to only affect discrete GTT blocks (i.e. on LLC
690 * system agents we cannot reproduce this behaviour).
691 */
692 wmb();
693
694 switch (obj->base.write_domain) {
695 case I915_GEM_DOMAIN_GTT:
Chris Wilsonc5ba5b22017-09-07 19:45:20 +0100696 if (!HAS_LLC(dev_priv)) {
Chris Wilsonb69a7842017-08-29 20:25:46 +0100697 intel_runtime_pm_get(dev_priv);
698 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc5ba5b22017-09-07 19:45:20 +0100699 POSTING_READ_FW(RING_HEAD(dev_priv->engine[RCS]->mmio_base));
Chris Wilsonb69a7842017-08-29 20:25:46 +0100700 spin_unlock_irq(&dev_priv->uncore.lock);
701 intel_runtime_pm_put(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100702 }
703
704 intel_fb_obj_flush(obj,
705 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
706 break;
707
708 case I915_GEM_DOMAIN_CPU:
709 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
710 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100711
712 case I915_GEM_DOMAIN_RENDER:
713 if (gpu_write_needs_clflush(obj))
714 obj->cache_dirty = true;
715 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100716 }
717
718 obj->base.write_domain = 0;
719}
720
Daniel Vetter8c599672011-12-14 13:57:31 +0100721static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100722__copy_to_user_swizzled(char __user *cpu_vaddr,
723 const char *gpu_vaddr, int gpu_offset,
724 int length)
725{
726 int ret, cpu_offset = 0;
727
728 while (length > 0) {
729 int cacheline_end = ALIGN(gpu_offset + 1, 64);
730 int this_length = min(cacheline_end - gpu_offset, length);
731 int swizzled_gpu_offset = gpu_offset ^ 64;
732
733 ret = __copy_to_user(cpu_vaddr + cpu_offset,
734 gpu_vaddr + swizzled_gpu_offset,
735 this_length);
736 if (ret)
737 return ret + length;
738
739 cpu_offset += this_length;
740 gpu_offset += this_length;
741 length -= this_length;
742 }
743
744 return 0;
745}
746
747static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700748__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
749 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100750 int length)
751{
752 int ret, cpu_offset = 0;
753
754 while (length > 0) {
755 int cacheline_end = ALIGN(gpu_offset + 1, 64);
756 int this_length = min(cacheline_end - gpu_offset, length);
757 int swizzled_gpu_offset = gpu_offset ^ 64;
758
759 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
760 cpu_vaddr + cpu_offset,
761 this_length);
762 if (ret)
763 return ret + length;
764
765 cpu_offset += this_length;
766 gpu_offset += this_length;
767 length -= this_length;
768 }
769
770 return 0;
771}
772
Brad Volkin4c914c02014-02-18 10:15:45 -0800773/*
774 * Pins the specified object's pages and synchronizes the object with
775 * GPU accesses. Sets needs_clflush to non-zero if the caller should
776 * flush the object from the CPU cache.
777 */
778int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100779 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800780{
781 int ret;
782
Chris Wilsone95433c2016-10-28 13:58:27 +0100783 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800784
Chris Wilsone95433c2016-10-28 13:58:27 +0100785 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100786 if (!i915_gem_object_has_struct_page(obj))
787 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800788
Chris Wilsone95433c2016-10-28 13:58:27 +0100789 ret = i915_gem_object_wait(obj,
790 I915_WAIT_INTERRUPTIBLE |
791 I915_WAIT_LOCKED,
792 MAX_SCHEDULE_TIMEOUT,
793 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100794 if (ret)
795 return ret;
796
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100797 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100798 if (ret)
799 return ret;
800
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100801 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
802 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000803 ret = i915_gem_object_set_to_cpu_domain(obj, false);
804 if (ret)
805 goto err_unpin;
806 else
807 goto out;
808 }
809
Chris Wilsonef749212017-04-12 12:01:10 +0100810 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100811
Chris Wilson43394c72016-08-18 17:16:47 +0100812 /* If we're not in the cpu read domain, set ourself into the gtt
813 * read domain and manually flush cachelines (if required). This
814 * optimizes for the case when the gpu will dirty the data
815 * anyway again before the next pread happens.
816 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100817 if (!obj->cache_dirty &&
818 !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000819 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800820
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000821out:
Chris Wilson97649512016-08-18 17:16:50 +0100822 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100823 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100824
825err_unpin:
826 i915_gem_object_unpin_pages(obj);
827 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100828}
829
830int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
831 unsigned int *needs_clflush)
832{
833 int ret;
834
Chris Wilsone95433c2016-10-28 13:58:27 +0100835 lockdep_assert_held(&obj->base.dev->struct_mutex);
836
Chris Wilson43394c72016-08-18 17:16:47 +0100837 *needs_clflush = 0;
838 if (!i915_gem_object_has_struct_page(obj))
839 return -ENODEV;
840
Chris Wilsone95433c2016-10-28 13:58:27 +0100841 ret = i915_gem_object_wait(obj,
842 I915_WAIT_INTERRUPTIBLE |
843 I915_WAIT_LOCKED |
844 I915_WAIT_ALL,
845 MAX_SCHEDULE_TIMEOUT,
846 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100847 if (ret)
848 return ret;
849
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100850 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100851 if (ret)
852 return ret;
853
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100854 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
855 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000856 ret = i915_gem_object_set_to_cpu_domain(obj, true);
857 if (ret)
858 goto err_unpin;
859 else
860 goto out;
861 }
862
Chris Wilsonef749212017-04-12 12:01:10 +0100863 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100864
Chris Wilson43394c72016-08-18 17:16:47 +0100865 /* If we're not in the cpu write domain, set ourself into the
866 * gtt write domain and manually flush cachelines (as required).
867 * This optimizes for the case when the gpu will use the data
868 * right away and we therefore have to clflush anyway.
869 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100870 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000871 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100872
Chris Wilsone27ab732017-06-15 13:38:49 +0100873 /*
874 * Same trick applies to invalidate partially written
875 * cachelines read before writing.
876 */
877 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
878 *needs_clflush |= CLFLUSH_BEFORE;
879 }
Chris Wilson43394c72016-08-18 17:16:47 +0100880
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000881out:
Chris Wilson43394c72016-08-18 17:16:47 +0100882 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100883 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100884 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100885 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100886
887err_unpin:
888 i915_gem_object_unpin_pages(obj);
889 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800890}
891
Daniel Vetter23c18c72012-03-25 19:47:42 +0200892static void
893shmem_clflush_swizzled_range(char *addr, unsigned long length,
894 bool swizzled)
895{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200896 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200897 unsigned long start = (unsigned long) addr;
898 unsigned long end = (unsigned long) addr + length;
899
900 /* For swizzling simply ensure that we always flush both
901 * channels. Lame, but simple and it works. Swizzled
902 * pwrite/pread is far from a hotpath - current userspace
903 * doesn't use it at all. */
904 start = round_down(start, 128);
905 end = round_up(end, 128);
906
907 drm_clflush_virt_range((void *)start, end - start);
908 } else {
909 drm_clflush_virt_range(addr, length);
910 }
911
912}
913
Daniel Vetterd174bd62012-03-25 19:47:40 +0200914/* Only difference to the fast-path function is that this can handle bit17
915 * and uses non-atomic copy and kmap functions. */
916static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100917shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200918 char __user *user_data,
919 bool page_do_bit17_swizzling, bool needs_clflush)
920{
921 char *vaddr;
922 int ret;
923
924 vaddr = kmap(page);
925 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100926 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200927 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200928
929 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100930 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200931 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100932 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200933 kunmap(page);
934
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100935 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200936}
937
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100938static int
939shmem_pread(struct page *page, int offset, int length, char __user *user_data,
940 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530941{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100942 int ret;
943
944 ret = -ENODEV;
945 if (!page_do_bit17_swizzling) {
946 char *vaddr = kmap_atomic(page);
947
948 if (needs_clflush)
949 drm_clflush_virt_range(vaddr + offset, length);
950 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
951 kunmap_atomic(vaddr);
952 }
953 if (ret == 0)
954 return 0;
955
956 return shmem_pread_slow(page, offset, length, user_data,
957 page_do_bit17_swizzling, needs_clflush);
958}
959
960static int
961i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
962 struct drm_i915_gem_pread *args)
963{
964 char __user *user_data;
965 u64 remain;
966 unsigned int obj_do_bit17_swizzling;
967 unsigned int needs_clflush;
968 unsigned int idx, offset;
969 int ret;
970
971 obj_do_bit17_swizzling = 0;
972 if (i915_gem_object_needs_bit17_swizzle(obj))
973 obj_do_bit17_swizzling = BIT(17);
974
975 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
976 if (ret)
977 return ret;
978
979 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
980 mutex_unlock(&obj->base.dev->struct_mutex);
981 if (ret)
982 return ret;
983
984 remain = args->size;
985 user_data = u64_to_user_ptr(args->data_ptr);
986 offset = offset_in_page(args->offset);
987 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
988 struct page *page = i915_gem_object_get_page(obj, idx);
989 int length;
990
991 length = remain;
992 if (offset + length > PAGE_SIZE)
993 length = PAGE_SIZE - offset;
994
995 ret = shmem_pread(page, offset, length, user_data,
996 page_to_phys(page) & obj_do_bit17_swizzling,
997 needs_clflush);
998 if (ret)
999 break;
1000
1001 remain -= length;
1002 user_data += length;
1003 offset = 0;
1004 }
1005
1006 i915_gem_obj_finish_shmem_access(obj);
1007 return ret;
1008}
1009
1010static inline bool
1011gtt_user_read(struct io_mapping *mapping,
1012 loff_t base, int offset,
1013 char __user *user_data, int length)
1014{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001015 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001016 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301017
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301018 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001019 vaddr = io_mapping_map_atomic_wc(mapping, base);
1020 unwritten = __copy_to_user_inatomic(user_data,
1021 (void __force *)vaddr + offset,
1022 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001023 io_mapping_unmap_atomic(vaddr);
1024 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001025 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1026 unwritten = copy_to_user(user_data,
1027 (void __force *)vaddr + offset,
1028 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001029 io_mapping_unmap(vaddr);
1030 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301031 return unwritten;
1032}
1033
1034static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001035i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1036 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301037{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001038 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1039 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301040 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001041 struct i915_vma *vma;
1042 void __user *user_data;
1043 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301044 int ret;
1045
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001046 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1047 if (ret)
1048 return ret;
1049
1050 intel_runtime_pm_get(i915);
1051 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001052 PIN_MAPPABLE |
1053 PIN_NONFAULT |
1054 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001055 if (!IS_ERR(vma)) {
1056 node.start = i915_ggtt_offset(vma);
1057 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001058 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001059 if (ret) {
1060 i915_vma_unpin(vma);
1061 vma = ERR_PTR(ret);
1062 }
1063 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001064 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001065 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301066 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001067 goto out_unlock;
1068 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301069 }
1070
1071 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1072 if (ret)
1073 goto out_unpin;
1074
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001075 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301076
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001077 user_data = u64_to_user_ptr(args->data_ptr);
1078 remain = args->size;
1079 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301080
1081 while (remain > 0) {
1082 /* Operation in this page
1083 *
1084 * page_base = page offset within aperture
1085 * page_offset = offset within page
1086 * page_length = bytes to copy for this page
1087 */
1088 u32 page_base = node.start;
1089 unsigned page_offset = offset_in_page(offset);
1090 unsigned page_length = PAGE_SIZE - page_offset;
1091 page_length = remain < page_length ? remain : page_length;
1092 if (node.allocated) {
1093 wmb();
1094 ggtt->base.insert_page(&ggtt->base,
1095 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001096 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301097 wmb();
1098 } else {
1099 page_base += offset & PAGE_MASK;
1100 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001101
1102 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1103 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301104 ret = -EFAULT;
1105 break;
1106 }
1107
1108 remain -= page_length;
1109 user_data += page_length;
1110 offset += page_length;
1111 }
1112
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001113 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301114out_unpin:
1115 if (node.allocated) {
1116 wmb();
1117 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001118 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301119 remove_mappable_node(&node);
1120 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001121 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301122 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001123out_unlock:
1124 intel_runtime_pm_put(i915);
1125 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001126
Eric Anholteb014592009-03-10 11:44:52 -07001127 return ret;
1128}
1129
Eric Anholt673a3942008-07-30 12:06:12 -07001130/**
1131 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001132 * @dev: drm device pointer
1133 * @data: ioctl data blob
1134 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001135 *
1136 * On error, the contents of *data are undefined.
1137 */
1138int
1139i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001140 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001141{
1142 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001143 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001144 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001145
Chris Wilson51311d02010-11-17 09:10:42 +00001146 if (args->size == 0)
1147 return 0;
1148
1149 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001150 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001151 args->size))
1152 return -EFAULT;
1153
Chris Wilson03ac0642016-07-20 13:31:51 +01001154 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001155 if (!obj)
1156 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001157
Chris Wilson7dcd2492010-09-26 20:21:44 +01001158 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001159 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001160 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001161 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001162 }
1163
Chris Wilsondb53a302011-02-03 11:57:46 +00001164 trace_i915_gem_object_pread(obj, args->offset, args->size);
1165
Chris Wilsone95433c2016-10-28 13:58:27 +01001166 ret = i915_gem_object_wait(obj,
1167 I915_WAIT_INTERRUPTIBLE,
1168 MAX_SCHEDULE_TIMEOUT,
1169 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001170 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001171 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001172
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001173 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001174 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001175 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001176
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001177 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001178 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001179 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301180
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001181 i915_gem_object_unpin_pages(obj);
1182out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001183 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001184 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001185}
1186
Keith Packard0839ccb2008-10-30 19:38:48 -07001187/* This is the fast write path which cannot handle
1188 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001189 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001190
Chris Wilsonfe115622016-10-28 13:58:40 +01001191static inline bool
1192ggtt_write(struct io_mapping *mapping,
1193 loff_t base, int offset,
1194 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001195{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001196 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001197 unsigned long unwritten;
1198
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001199 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001200 vaddr = io_mapping_map_atomic_wc(mapping, base);
1201 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001202 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001203 io_mapping_unmap_atomic(vaddr);
1204 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001205 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1206 unwritten = copy_from_user((void __force *)vaddr + offset,
1207 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001208 io_mapping_unmap(vaddr);
1209 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001210
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001211 return unwritten;
1212}
1213
Eric Anholt3de09aa2009-03-09 09:42:23 -07001214/**
1215 * This is the fast pwrite path, where we copy the data directly from the
1216 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001217 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001218 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001219 */
Eric Anholt673a3942008-07-30 12:06:12 -07001220static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001221i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1222 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001223{
Chris Wilsonfe115622016-10-28 13:58:40 +01001224 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301225 struct i915_ggtt *ggtt = &i915->ggtt;
1226 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001227 struct i915_vma *vma;
1228 u64 remain, offset;
1229 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301230 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301231
Chris Wilsonfe115622016-10-28 13:58:40 +01001232 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1233 if (ret)
1234 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001235
Chris Wilson8bd818152017-10-19 07:37:33 +01001236 if (i915_gem_object_has_struct_page(obj)) {
1237 /*
1238 * Avoid waking the device up if we can fallback, as
1239 * waking/resuming is very slow (worst-case 10-100 ms
1240 * depending on PCI sleeps and our own resume time).
1241 * This easily dwarfs any performance advantage from
1242 * using the cache bypass of indirect GGTT access.
1243 */
1244 if (!intel_runtime_pm_get_if_in_use(i915)) {
1245 ret = -EFAULT;
1246 goto out_unlock;
1247 }
1248 } else {
1249 /* No backing pages, no fallback, we must force GGTT access */
1250 intel_runtime_pm_get(i915);
1251 }
1252
Chris Wilson058d88c2016-08-15 10:49:06 +01001253 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001254 PIN_MAPPABLE |
1255 PIN_NONFAULT |
1256 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001257 if (!IS_ERR(vma)) {
1258 node.start = i915_ggtt_offset(vma);
1259 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001260 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001261 if (ret) {
1262 i915_vma_unpin(vma);
1263 vma = ERR_PTR(ret);
1264 }
1265 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001266 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001267 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301268 if (ret)
Chris Wilson8bd818152017-10-19 07:37:33 +01001269 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001270 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301271 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001272
1273 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1274 if (ret)
1275 goto out_unpin;
1276
Chris Wilsonfe115622016-10-28 13:58:40 +01001277 mutex_unlock(&i915->drm.struct_mutex);
1278
Chris Wilsonb19482d2016-08-18 17:16:43 +01001279 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001280
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301281 user_data = u64_to_user_ptr(args->data_ptr);
1282 offset = args->offset;
1283 remain = args->size;
1284 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001285 /* Operation in this page
1286 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001287 * page_base = page offset within aperture
1288 * page_offset = offset within page
1289 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001290 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301291 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001292 unsigned int page_offset = offset_in_page(offset);
1293 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301294 page_length = remain < page_length ? remain : page_length;
1295 if (node.allocated) {
1296 wmb(); /* flush the write before we modify the GGTT */
1297 ggtt->base.insert_page(&ggtt->base,
1298 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1299 node.start, I915_CACHE_NONE, 0);
1300 wmb(); /* flush modifications to the GGTT (insert_page) */
1301 } else {
1302 page_base += offset & PAGE_MASK;
1303 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001304 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001305 * source page isn't available. Return the error and we'll
1306 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301307 * If the object is non-shmem backed, we retry again with the
1308 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001309 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001310 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1311 user_data, page_length)) {
1312 ret = -EFAULT;
1313 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001314 }
Eric Anholt673a3942008-07-30 12:06:12 -07001315
Keith Packard0839ccb2008-10-30 19:38:48 -07001316 remain -= page_length;
1317 user_data += page_length;
1318 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001319 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001320 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001321
1322 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001323out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301324 if (node.allocated) {
1325 wmb();
1326 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001327 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301328 remove_mappable_node(&node);
1329 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001330 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301331 }
Chris Wilson8bd818152017-10-19 07:37:33 +01001332out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001333 intel_runtime_pm_put(i915);
Chris Wilson8bd818152017-10-19 07:37:33 +01001334out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001335 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001336 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001337}
1338
Eric Anholt673a3942008-07-30 12:06:12 -07001339static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001340shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001341 char __user *user_data,
1342 bool page_do_bit17_swizzling,
1343 bool needs_clflush_before,
1344 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001345{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001346 char *vaddr;
1347 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001348
Daniel Vetterd174bd62012-03-25 19:47:40 +02001349 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001350 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001351 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001352 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001353 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001354 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1355 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001356 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001357 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001358 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001359 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001360 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001361 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001362
Chris Wilson755d2212012-09-04 21:02:55 +01001363 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001364}
1365
Chris Wilsonfe115622016-10-28 13:58:40 +01001366/* Per-page copy function for the shmem pwrite fastpath.
1367 * Flushes invalid cachelines before writing to the target if
1368 * needs_clflush_before is set and flushes out any written cachelines after
1369 * writing if needs_clflush is set.
1370 */
Eric Anholt40123c12009-03-09 13:42:30 -07001371static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001372shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1373 bool page_do_bit17_swizzling,
1374 bool needs_clflush_before,
1375 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001376{
Chris Wilsonfe115622016-10-28 13:58:40 +01001377 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001378
Chris Wilsonfe115622016-10-28 13:58:40 +01001379 ret = -ENODEV;
1380 if (!page_do_bit17_swizzling) {
1381 char *vaddr = kmap_atomic(page);
1382
1383 if (needs_clflush_before)
1384 drm_clflush_virt_range(vaddr + offset, len);
1385 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1386 if (needs_clflush_after)
1387 drm_clflush_virt_range(vaddr + offset, len);
1388
1389 kunmap_atomic(vaddr);
1390 }
1391 if (ret == 0)
1392 return ret;
1393
1394 return shmem_pwrite_slow(page, offset, len, user_data,
1395 page_do_bit17_swizzling,
1396 needs_clflush_before,
1397 needs_clflush_after);
1398}
1399
1400static int
1401i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1402 const struct drm_i915_gem_pwrite *args)
1403{
1404 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1405 void __user *user_data;
1406 u64 remain;
1407 unsigned int obj_do_bit17_swizzling;
1408 unsigned int partial_cacheline_write;
1409 unsigned int needs_clflush;
1410 unsigned int offset, idx;
1411 int ret;
1412
1413 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001414 if (ret)
1415 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001416
Chris Wilsonfe115622016-10-28 13:58:40 +01001417 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1418 mutex_unlock(&i915->drm.struct_mutex);
1419 if (ret)
1420 return ret;
1421
1422 obj_do_bit17_swizzling = 0;
1423 if (i915_gem_object_needs_bit17_swizzle(obj))
1424 obj_do_bit17_swizzling = BIT(17);
1425
1426 /* If we don't overwrite a cacheline completely we need to be
1427 * careful to have up-to-date data by first clflushing. Don't
1428 * overcomplicate things and flush the entire patch.
1429 */
1430 partial_cacheline_write = 0;
1431 if (needs_clflush & CLFLUSH_BEFORE)
1432 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1433
Chris Wilson43394c72016-08-18 17:16:47 +01001434 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001435 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001436 offset = offset_in_page(args->offset);
1437 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1438 struct page *page = i915_gem_object_get_page(obj, idx);
1439 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001440
Chris Wilsonfe115622016-10-28 13:58:40 +01001441 length = remain;
1442 if (offset + length > PAGE_SIZE)
1443 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001444
Chris Wilsonfe115622016-10-28 13:58:40 +01001445 ret = shmem_pwrite(page, offset, length, user_data,
1446 page_to_phys(page) & obj_do_bit17_swizzling,
1447 (offset | length) & partial_cacheline_write,
1448 needs_clflush & CLFLUSH_AFTER);
1449 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001450 break;
1451
Chris Wilsonfe115622016-10-28 13:58:40 +01001452 remain -= length;
1453 user_data += length;
1454 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001455 }
1456
Chris Wilsond59b21e2017-02-22 11:40:49 +00001457 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001458 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001459 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001460}
1461
1462/**
1463 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001464 * @dev: drm device
1465 * @data: ioctl data blob
1466 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001467 *
1468 * On error, the contents of the buffer that were to be modified are undefined.
1469 */
1470int
1471i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001472 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001473{
1474 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001475 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001476 int ret;
1477
1478 if (args->size == 0)
1479 return 0;
1480
1481 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001482 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001483 args->size))
1484 return -EFAULT;
1485
Chris Wilson03ac0642016-07-20 13:31:51 +01001486 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001487 if (!obj)
1488 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001489
Chris Wilson7dcd2492010-09-26 20:21:44 +01001490 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001491 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001492 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001493 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001494 }
1495
Chris Wilsondb53a302011-02-03 11:57:46 +00001496 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1497
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001498 ret = -ENODEV;
1499 if (obj->ops->pwrite)
1500 ret = obj->ops->pwrite(obj, args);
1501 if (ret != -ENODEV)
1502 goto err;
1503
Chris Wilsone95433c2016-10-28 13:58:27 +01001504 ret = i915_gem_object_wait(obj,
1505 I915_WAIT_INTERRUPTIBLE |
1506 I915_WAIT_ALL,
1507 MAX_SCHEDULE_TIMEOUT,
1508 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001509 if (ret)
1510 goto err;
1511
Chris Wilsonfe115622016-10-28 13:58:40 +01001512 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001513 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001514 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001515
Daniel Vetter935aaa62012-03-25 19:47:35 +02001516 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001517 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1518 * it would end up going through the fenced access, and we'll get
1519 * different detiling behavior between reading and writing.
1520 * pread/pwrite currently are reading and writing from the CPU
1521 * perspective, requiring manual detiling by the client.
1522 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001523 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001524 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001525 /* Note that the gtt paths might fail with non-page-backed user
1526 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001527 * textures). Fallback to the shmem path in that case.
1528 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001529 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001530
Chris Wilsond1054ee2016-07-16 18:42:36 +01001531 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001532 if (obj->phys_handle)
1533 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301534 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001535 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001536 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001537
Chris Wilsonfe115622016-10-28 13:58:40 +01001538 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001539err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001540 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001541 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001542}
1543
Chris Wilson40e62d52016-10-28 13:58:41 +01001544static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1545{
1546 struct drm_i915_private *i915;
1547 struct list_head *list;
1548 struct i915_vma *vma;
1549
Chris Wilsonf2123812017-10-16 12:40:37 +01001550 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1551
Chris Wilson40e62d52016-10-28 13:58:41 +01001552 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1553 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001554 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001555
1556 if (i915_vma_is_active(vma))
1557 continue;
1558
1559 if (!drm_mm_node_allocated(&vma->node))
1560 continue;
1561
1562 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1563 }
1564
1565 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001566 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001567 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001568 list_move_tail(&obj->mm.link, list);
1569 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001570}
1571
Eric Anholt673a3942008-07-30 12:06:12 -07001572/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001573 * Called when user space prepares to use an object with the CPU, either
1574 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001575 * @dev: drm device
1576 * @data: ioctl data blob
1577 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001578 */
1579int
1580i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001581 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001582{
1583 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001584 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001585 uint32_t read_domains = args->read_domains;
1586 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001587 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001588
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001589 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001590 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001591 return -EINVAL;
1592
1593 /* Having something in the write domain implies it's in the read
1594 * domain, and only that read domain. Enforce that in the request.
1595 */
1596 if (write_domain != 0 && read_domains != write_domain)
1597 return -EINVAL;
1598
Chris Wilson03ac0642016-07-20 13:31:51 +01001599 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001600 if (!obj)
1601 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001602
Chris Wilson3236f572012-08-24 09:35:09 +01001603 /* Try to flush the object off the GPU without holding the lock.
1604 * We will repeat the flush holding the lock in the normal manner
1605 * to catch cases where we are gazumped.
1606 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001607 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001608 I915_WAIT_INTERRUPTIBLE |
1609 (write_domain ? I915_WAIT_ALL : 0),
1610 MAX_SCHEDULE_TIMEOUT,
1611 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001612 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001613 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001614
Tina Zhanga03f3952017-11-14 10:25:13 +00001615 /*
1616 * Proxy objects do not control access to the backing storage, ergo
1617 * they cannot be used as a means to manipulate the cache domain
1618 * tracking for that backing storage. The proxy object is always
1619 * considered to be outside of any cache domain.
1620 */
1621 if (i915_gem_object_is_proxy(obj)) {
1622 err = -ENXIO;
1623 goto out;
1624 }
1625
1626 /*
1627 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001628 * direct access in memory with previous cached writes through
1629 * shmemfs and that our cache domain tracking remains valid.
1630 * For example, if the obj->filp was moved to swap without us
1631 * being notified and releasing the pages, we would mistakenly
1632 * continue to assume that the obj remained out of the CPU cached
1633 * domain.
1634 */
1635 err = i915_gem_object_pin_pages(obj);
1636 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001637 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001638
1639 err = i915_mutex_lock_interruptible(dev);
1640 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001641 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001642
Chris Wilsone22d8e32017-04-12 12:01:11 +01001643 if (read_domains & I915_GEM_DOMAIN_WC)
1644 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1645 else if (read_domains & I915_GEM_DOMAIN_GTT)
1646 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301647 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001648 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001649
1650 /* And bump the LRU for this access */
1651 i915_gem_object_bump_inactive_ggtt(obj);
1652
1653 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001654
Daniel Vetter031b6982015-06-26 19:35:16 +02001655 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001656 intel_fb_obj_invalidate(obj,
1657 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001658
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001659out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001660 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001661out:
1662 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001663 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001664}
1665
1666/**
1667 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001668 * @dev: drm device
1669 * @data: ioctl data blob
1670 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001671 */
1672int
1673i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001674 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001675{
1676 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001677 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001678
Chris Wilson03ac0642016-07-20 13:31:51 +01001679 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001680 if (!obj)
1681 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001682
Tina Zhanga03f3952017-11-14 10:25:13 +00001683 /*
1684 * Proxy objects are barred from CPU access, so there is no
1685 * need to ban sw_finish as it is a nop.
1686 */
1687
Eric Anholt673a3942008-07-30 12:06:12 -07001688 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001689 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001690 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001691
1692 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001693}
1694
1695/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001696 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1697 * it is mapped to.
1698 * @dev: drm device
1699 * @data: ioctl data blob
1700 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001701 *
1702 * While the mapping holds a reference on the contents of the object, it doesn't
1703 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001704 *
1705 * IMPORTANT:
1706 *
1707 * DRM driver writers who look a this function as an example for how to do GEM
1708 * mmap support, please don't implement mmap support like here. The modern way
1709 * to implement DRM mmap support is with an mmap offset ioctl (like
1710 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1711 * That way debug tooling like valgrind will understand what's going on, hiding
1712 * the mmap call in a driver private ioctl will break that. The i915 driver only
1713 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001714 */
1715int
1716i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001717 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001718{
1719 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001720 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001721 unsigned long addr;
1722
Akash Goel1816f922015-01-02 16:29:30 +05301723 if (args->flags & ~(I915_MMAP_WC))
1724 return -EINVAL;
1725
Borislav Petkov568a58e2016-03-29 17:42:01 +02001726 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301727 return -ENODEV;
1728
Chris Wilson03ac0642016-07-20 13:31:51 +01001729 obj = i915_gem_object_lookup(file, args->handle);
1730 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001731 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001732
Daniel Vetter1286ff72012-05-10 15:25:09 +02001733 /* prime objects have no backing filp to GEM mmap
1734 * pages from.
1735 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001736 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001737 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001738 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001739 }
1740
Chris Wilson03ac0642016-07-20 13:31:51 +01001741 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001742 PROT_READ | PROT_WRITE, MAP_SHARED,
1743 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301744 if (args->flags & I915_MMAP_WC) {
1745 struct mm_struct *mm = current->mm;
1746 struct vm_area_struct *vma;
1747
Michal Hocko80a89a52016-05-23 16:26:11 -07001748 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001749 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001750 return -EINTR;
1751 }
Akash Goel1816f922015-01-02 16:29:30 +05301752 vma = find_vma(mm, addr);
1753 if (vma)
1754 vma->vm_page_prot =
1755 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1756 else
1757 addr = -ENOMEM;
1758 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001759
1760 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001761 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301762 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001763 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001764 if (IS_ERR((void *)addr))
1765 return addr;
1766
1767 args->addr_ptr = (uint64_t) addr;
1768
1769 return 0;
1770}
1771
Chris Wilson03af84f2016-08-18 17:17:01 +01001772static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1773{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001774 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001775}
1776
Jesse Barnesde151cf2008-11-12 10:03:55 -08001777/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001778 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1779 *
1780 * A history of the GTT mmap interface:
1781 *
1782 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1783 * aligned and suitable for fencing, and still fit into the available
1784 * mappable space left by the pinned display objects. A classic problem
1785 * we called the page-fault-of-doom where we would ping-pong between
1786 * two objects that could not fit inside the GTT and so the memcpy
1787 * would page one object in at the expense of the other between every
1788 * single byte.
1789 *
1790 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1791 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1792 * object is too large for the available space (or simply too large
1793 * for the mappable aperture!), a view is created instead and faulted
1794 * into userspace. (This view is aligned and sized appropriately for
1795 * fenced access.)
1796 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001797 * 2 - Recognise WC as a separate cache domain so that we can flush the
1798 * delayed writes via GTT before performing direct access via WC.
1799 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001800 * Restrictions:
1801 *
1802 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1803 * hangs on some architectures, corruption on others. An attempt to service
1804 * a GTT page fault from a snoopable object will generate a SIGBUS.
1805 *
1806 * * the object must be able to fit into RAM (physical memory, though no
1807 * limited to the mappable aperture).
1808 *
1809 *
1810 * Caveats:
1811 *
1812 * * a new GTT page fault will synchronize rendering from the GPU and flush
1813 * all data to system memory. Subsequent access will not be synchronized.
1814 *
1815 * * all mappings are revoked on runtime device suspend.
1816 *
1817 * * there are only 8, 16 or 32 fence registers to share between all users
1818 * (older machines require fence register for display and blitter access
1819 * as well). Contention of the fence registers will cause the previous users
1820 * to be unmapped and any new access will generate new page faults.
1821 *
1822 * * running out of memory while servicing a fault may generate a SIGBUS,
1823 * rather than the expected SIGSEGV.
1824 */
1825int i915_gem_mmap_gtt_version(void)
1826{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001827 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001828}
1829
Chris Wilson2d4281b2017-01-10 09:56:32 +00001830static inline struct i915_ggtt_view
1831compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001832 pgoff_t page_offset,
1833 unsigned int chunk)
1834{
1835 struct i915_ggtt_view view;
1836
1837 if (i915_gem_object_is_tiled(obj))
1838 chunk = roundup(chunk, tile_row_pages(obj));
1839
Chris Wilson2d4281b2017-01-10 09:56:32 +00001840 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001841 view.partial.offset = rounddown(page_offset, chunk);
1842 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001843 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001844 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001845
1846 /* If the partial covers the entire object, just create a normal VMA. */
1847 if (chunk >= obj->base.size >> PAGE_SHIFT)
1848 view.type = I915_GGTT_VIEW_NORMAL;
1849
1850 return view;
1851}
1852
Chris Wilson4cc69072016-08-25 19:05:19 +01001853/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001854 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001855 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001856 *
1857 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1858 * from userspace. The fault handler takes care of binding the object to
1859 * the GTT (if needed), allocating and programming a fence register (again,
1860 * only if needed based on whether the old reg is still valid or the object
1861 * is tiled) and inserting a new PTE into the faulting process.
1862 *
1863 * Note that the faulting process may involve evicting existing objects
1864 * from the GTT and/or fence registers to make room. So performance may
1865 * suffer if the GTT working set is large or there are few fence registers
1866 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001867 *
1868 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1869 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001870 */
Dave Jiang11bac802017-02-24 14:56:41 -08001871int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001872{
Chris Wilson03af84f2016-08-18 17:17:01 +01001873#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001874 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001875 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001876 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001877 struct drm_i915_private *dev_priv = to_i915(dev);
1878 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001879 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001880 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001881 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001882 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001883 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001884
Jesse Barnesde151cf2008-11-12 10:03:55 -08001885 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001886 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001887
Chris Wilsondb53a302011-02-03 11:57:46 +00001888 trace_i915_gem_object_fault(obj, page_offset, true, write);
1889
Chris Wilson6e4930f2014-02-07 18:37:06 -02001890 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001891 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001892 * repeat the flush holding the lock in the normal manner to catch cases
1893 * where we are gazumped.
1894 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001895 ret = i915_gem_object_wait(obj,
1896 I915_WAIT_INTERRUPTIBLE,
1897 MAX_SCHEDULE_TIMEOUT,
1898 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001899 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001900 goto err;
1901
Chris Wilson40e62d52016-10-28 13:58:41 +01001902 ret = i915_gem_object_pin_pages(obj);
1903 if (ret)
1904 goto err;
1905
Chris Wilsonb8f90962016-08-05 10:14:07 +01001906 intel_runtime_pm_get(dev_priv);
1907
1908 ret = i915_mutex_lock_interruptible(dev);
1909 if (ret)
1910 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001911
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001912 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001913 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001914 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001915 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001916 }
1917
Chris Wilson82118872016-08-18 17:17:05 +01001918 /* If the object is smaller than a couple of partial vma, it is
1919 * not worth only creating a single partial vma - we may as well
1920 * clear enough space for the full object.
1921 */
1922 flags = PIN_MAPPABLE;
1923 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1924 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1925
Chris Wilsona61007a2016-08-18 17:17:02 +01001926 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001927 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001928 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001929 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001930 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001931 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001932
Chris Wilson50349242016-08-18 17:17:04 +01001933 /* Userspace is now writing through an untracked VMA, abandon
1934 * all hope that the hardware is able to track future writes.
1935 */
1936 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1937
Chris Wilsona61007a2016-08-18 17:17:02 +01001938 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1939 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001940 if (IS_ERR(vma)) {
1941 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001942 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001943 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001944
Chris Wilsonc9839302012-11-20 10:45:17 +00001945 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1946 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001947 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001948
Chris Wilson3bd40732017-10-09 09:43:56 +01001949 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001950 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001951 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001952
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001953 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001954 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001955 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001956 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1957 min_t(u64, vma->size, area->vm_end - area->vm_start),
1958 &ggtt->mappable);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001959 if (ret)
1960 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001961
Chris Wilsona65adaf2017-10-09 09:43:57 +01001962 /* Mark as being mmapped into userspace for later revocation */
1963 assert_rpm_wakelock_held(dev_priv);
1964 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1965 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1966 GEM_BUG_ON(!obj->userfault_count);
1967
1968err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001969 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001970err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001971 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001972err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001973 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001974err_rpm:
1975 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001976 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001977err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001978 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001979 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001980 /*
1981 * We eat errors when the gpu is terminally wedged to avoid
1982 * userspace unduly crashing (gl has no provisions for mmaps to
1983 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1984 * and so needs to be reported.
1985 */
1986 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001987 ret = VM_FAULT_SIGBUS;
1988 break;
1989 }
Chris Wilson045e7692010-11-07 09:18:22 +00001990 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001991 /*
1992 * EAGAIN means the gpu is hung and we'll wait for the error
1993 * handler to reset everything when re-faulting in
1994 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001995 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001996 case 0:
1997 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001998 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001999 case -EBUSY:
2000 /*
2001 * EBUSY is ok: this just means that another thread
2002 * already did the job.
2003 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002004 ret = VM_FAULT_NOPAGE;
2005 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002006 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002007 ret = VM_FAULT_OOM;
2008 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002009 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002010 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002011 ret = VM_FAULT_SIGBUS;
2012 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002013 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002014 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002015 ret = VM_FAULT_SIGBUS;
2016 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002017 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002018 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002019}
2020
Chris Wilsona65adaf2017-10-09 09:43:57 +01002021static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2022{
2023 struct i915_vma *vma;
2024
2025 GEM_BUG_ON(!obj->userfault_count);
2026
2027 obj->userfault_count = 0;
2028 list_del(&obj->userfault_link);
2029 drm_vma_node_unmap(&obj->base.vma_node,
2030 obj->base.dev->anon_inode->i_mapping);
2031
2032 list_for_each_entry(vma, &obj->vma_list, obj_link) {
2033 if (!i915_vma_is_ggtt(vma))
2034 break;
2035
2036 i915_vma_unset_userfault(vma);
2037 }
2038}
2039
Jesse Barnesde151cf2008-11-12 10:03:55 -08002040/**
Chris Wilson901782b2009-07-10 08:18:50 +01002041 * i915_gem_release_mmap - remove physical page mappings
2042 * @obj: obj in question
2043 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002044 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002045 * relinquish ownership of the pages back to the system.
2046 *
2047 * It is vital that we remove the page mapping if we have mapped a tiled
2048 * object through the GTT and then lose the fence register due to
2049 * resource pressure. Similarly if the object has been moved out of the
2050 * aperture, than pages mapped into userspace must be revoked. Removing the
2051 * mapping will then trigger a page fault on the next user access, allowing
2052 * fixup by i915_gem_fault().
2053 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002054void
Chris Wilson05394f32010-11-08 19:18:58 +00002055i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002056{
Chris Wilson275f0392016-10-24 13:42:14 +01002057 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002058
Chris Wilson349f2cc2016-04-13 17:35:12 +01002059 /* Serialisation between user GTT access and our code depends upon
2060 * revoking the CPU's PTE whilst the mutex is held. The next user
2061 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002062 *
2063 * Note that RPM complicates somewhat by adding an additional
2064 * requirement that operations to the GGTT be made holding the RPM
2065 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002066 */
Chris Wilson275f0392016-10-24 13:42:14 +01002067 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002068 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002069
Chris Wilsona65adaf2017-10-09 09:43:57 +01002070 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002071 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002072
Chris Wilsona65adaf2017-10-09 09:43:57 +01002073 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002074
2075 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2076 * memory transactions from userspace before we return. The TLB
2077 * flushing implied above by changing the PTE above *should* be
2078 * sufficient, an extra barrier here just provides us with a bit
2079 * of paranoid documentation about our requirement to serialise
2080 * memory writes before touching registers / GSM.
2081 */
2082 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002083
2084out:
2085 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002086}
2087
Chris Wilson7c108fd2016-10-24 13:42:18 +01002088void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002089{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002090 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002091 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002092
Chris Wilson3594a3e2016-10-24 13:42:16 +01002093 /*
2094 * Only called during RPM suspend. All users of the userfault_list
2095 * must be holding an RPM wakeref to ensure that this can not
2096 * run concurrently with themselves (and use the struct_mutex for
2097 * protection between themselves).
2098 */
2099
2100 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002101 &dev_priv->mm.userfault_list, userfault_link)
2102 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002103
2104 /* The fence will be lost when the device powers down. If any were
2105 * in use by hardware (i.e. they are pinned), we should not be powering
2106 * down! All other fences will be reacquired by the user upon waking.
2107 */
2108 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2109 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2110
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002111 /* Ideally we want to assert that the fence register is not
2112 * live at this point (i.e. that no piece of code will be
2113 * trying to write through fence + GTT, as that both violates
2114 * our tracking of activity and associated locking/barriers,
2115 * but also is illegal given that the hw is powered down).
2116 *
2117 * Previously we used reg->pin_count as a "liveness" indicator.
2118 * That is not sufficient, and we need a more fine-grained
2119 * tool if we want to have a sanity check here.
2120 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002121
2122 if (!reg->vma)
2123 continue;
2124
Chris Wilsona65adaf2017-10-09 09:43:57 +01002125 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002126 reg->dirty = true;
2127 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002128}
2129
Chris Wilsond8cb5082012-08-11 15:41:03 +01002130static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2131{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002132 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002133 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002134
Chris Wilsonf3f61842016-08-05 10:14:14 +01002135 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002136 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002137 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002138
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002139 /* Attempt to reap some mmap space from dead objects */
2140 do {
2141 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2142 if (err)
2143 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002144
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002145 i915_gem_drain_freed_objects(dev_priv);
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 (!err)
2148 break;
2149
2150 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002151
Chris Wilsonf3f61842016-08-05 10:14:14 +01002152 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002153}
2154
2155static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2156{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002157 drm_gem_free_mmap_offset(&obj->base);
2158}
2159
Dave Airlieda6b51d2014-12-24 13:11:17 +10002160int
Dave Airlieff72145b2011-02-07 12:16:14 +10002161i915_gem_mmap_gtt(struct drm_file *file,
2162 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002163 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002164 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002165{
Chris Wilson05394f32010-11-08 19:18:58 +00002166 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002167 int ret;
2168
Chris Wilson03ac0642016-07-20 13:31:51 +01002169 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002170 if (!obj)
2171 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002172
Chris Wilsond8cb5082012-08-11 15:41:03 +01002173 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002174 if (ret == 0)
2175 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002176
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002177 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002178 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002179}
2180
Dave Airlieff72145b2011-02-07 12:16:14 +10002181/**
2182 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2183 * @dev: DRM device
2184 * @data: GTT mapping ioctl data
2185 * @file: GEM object info
2186 *
2187 * Simply returns the fake offset to userspace so it can mmap it.
2188 * The mmap call will end up in drm_gem_mmap(), which will set things
2189 * up so we can get faults in the handler above.
2190 *
2191 * The fault handler will take care of binding the object into the GTT
2192 * (since it may have been evicted to make room for something), allocating
2193 * a fence register, and mapping the appropriate aperture address into
2194 * userspace.
2195 */
2196int
2197i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2198 struct drm_file *file)
2199{
2200 struct drm_i915_gem_mmap_gtt *args = data;
2201
Dave Airlieda6b51d2014-12-24 13:11:17 +10002202 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002203}
2204
Daniel Vetter225067e2012-08-20 10:23:20 +02002205/* Immediately discard the backing storage */
2206static void
2207i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002208{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002209 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002210
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002211 if (obj->base.filp == NULL)
2212 return;
2213
Daniel Vetter225067e2012-08-20 10:23:20 +02002214 /* Our goal here is to return as much of the memory as
2215 * is possible back to the system as we are called from OOM.
2216 * To do this we must instruct the shmfs to drop all of its
2217 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002218 */
Chris Wilson55372522014-03-25 13:23:06 +00002219 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002220 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002221 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002222}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002223
Chris Wilson55372522014-03-25 13:23:06 +00002224/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002225void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002226{
Chris Wilson55372522014-03-25 13:23:06 +00002227 struct address_space *mapping;
2228
Chris Wilson1233e2d2016-10-28 13:58:37 +01002229 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002230 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002231
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002232 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002233 case I915_MADV_DONTNEED:
2234 i915_gem_object_truncate(obj);
2235 case __I915_MADV_PURGED:
2236 return;
2237 }
2238
2239 if (obj->base.filp == NULL)
2240 return;
2241
Al Viro93c76a32015-12-04 23:45:44 -05002242 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002243 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002244}
2245
Chris Wilson5cdf5882010-09-27 15:51:07 +01002246static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002247i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2248 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002249{
Dave Gordon85d12252016-05-20 11:54:06 +01002250 struct sgt_iter sgt_iter;
2251 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002252
Chris Wilsone5facdf2016-12-23 14:57:57 +00002253 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002254
Chris Wilson03ac84f2016-10-28 13:58:36 +01002255 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002256
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002257 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002258 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002259
Chris Wilson03ac84f2016-10-28 13:58:36 +01002260 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002261 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002262 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002263
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002264 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002265 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002266
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002267 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002268 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002269 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002270
Chris Wilson03ac84f2016-10-28 13:58:36 +01002271 sg_free_table(pages);
2272 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002273}
2274
Chris Wilson96d77632016-10-28 13:58:33 +01002275static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2276{
2277 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002278 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002279
Chris Wilsonbea6e982017-10-26 14:00:31 +01002280 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002281 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2282 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002283 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002284}
2285
Chris Wilson548625e2016-11-01 12:11:34 +00002286void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2287 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002288{
Chris Wilsonf2123812017-10-16 12:40:37 +01002289 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002290 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002291
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002292 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002293 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002294
Chris Wilson15717de2016-08-04 07:52:26 +01002295 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002296 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002297 return;
2298
2299 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002300 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002301 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2302 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002303
Chris Wilsona2165e32012-12-03 11:49:00 +00002304 /* ->put_pages might need to allocate memory for the bit17 swizzle
2305 * array, hence protect them from being reaped by removing them from gtt
2306 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002307 pages = fetch_and_zero(&obj->mm.pages);
2308 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002309
Chris Wilsonf2123812017-10-16 12:40:37 +01002310 spin_lock(&i915->mm.obj_lock);
2311 list_del(&obj->mm.link);
2312 spin_unlock(&i915->mm.obj_lock);
2313
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002314 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002315 void *ptr;
2316
Chris Wilson0ce81782017-05-17 13:09:59 +01002317 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002318 if (is_vmalloc_addr(ptr))
2319 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002320 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002321 kunmap(kmap_to_page(ptr));
2322
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002323 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002324 }
2325
Chris Wilson96d77632016-10-28 13:58:33 +01002326 __i915_gem_object_reset_page_iter(obj);
2327
Chris Wilson4e5462e2017-03-07 13:20:31 +00002328 if (!IS_ERR(pages))
2329 obj->ops->put_pages(obj, pages);
2330
Matthew Aulda5c081662017-10-06 23:18:18 +01002331 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2332
Chris Wilson1233e2d2016-10-28 13:58:37 +01002333unlock:
2334 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002335}
2336
Chris Wilson935a2f72017-02-13 17:15:13 +00002337static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002338{
2339 struct sg_table new_st;
2340 struct scatterlist *sg, *new_sg;
2341 unsigned int i;
2342
2343 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002344 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002345
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002346 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002347 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002348
2349 new_sg = new_st.sgl;
2350 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2351 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2352 /* called before being DMA mapped, no need to copy sg->dma_* */
2353 new_sg = sg_next(new_sg);
2354 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002355 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002356
2357 sg_free_table(orig_st);
2358
2359 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002360 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002361}
2362
Matthew Auldb91b09e2017-10-06 23:18:17 +01002363static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002364{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002365 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002366 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2367 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002368 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002369 struct sg_table *st;
2370 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002371 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002372 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002373 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002374 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002375 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002376 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002377 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002378
Chris Wilson6c085a72012-08-20 11:40:46 +02002379 /* Assert that the object is not currently in any GPU domain. As it
2380 * wasn't in the GTT, there shouldn't be any way it could have been in
2381 * a GPU cache
2382 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002383 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2384 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002385
Chris Wilson9da3da62012-06-01 15:20:22 +01002386 st = kmalloc(sizeof(*st), GFP_KERNEL);
2387 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002388 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002389
Chris Wilsond766ef52016-12-19 12:43:45 +00002390rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002391 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002392 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002393 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002394 }
2395
2396 /* Get the list of pages out of our struct file. They'll be pinned
2397 * at this point until we release them.
2398 *
2399 * Fail silently without starting the shrinker
2400 */
Al Viro93c76a32015-12-04 23:45:44 -05002401 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002402 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002403 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2404
Imre Deak90797e62013-02-18 19:28:03 +02002405 sg = st->sgl;
2406 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002407 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002408 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002409 const unsigned int shrink[] = {
2410 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2411 0,
2412 }, *s = shrink;
2413 gfp_t gfp = noreclaim;
2414
2415 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002416 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002417 if (likely(!IS_ERR(page)))
2418 break;
2419
2420 if (!*s) {
2421 ret = PTR_ERR(page);
2422 goto err_sg;
2423 }
2424
Chris Wilson912d5722017-09-06 16:19:30 -07002425 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002426 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002427
Chris Wilson6c085a72012-08-20 11:40:46 +02002428 /* We've tried hard to allocate the memory by reaping
2429 * our own buffer, now let the real VM do its job and
2430 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002431 *
2432 * However, since graphics tend to be disposable,
2433 * defer the oom here by reporting the ENOMEM back
2434 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002435 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002436 if (!*s) {
2437 /* reclaim and warn, but no oom */
2438 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002439
2440 /* Our bo are always dirty and so we require
2441 * kswapd to reclaim our pages (direct reclaim
2442 * does not effectively begin pageout of our
2443 * buffers on its own). However, direct reclaim
2444 * only waits for kswapd when under allocation
2445 * congestion. So as a result __GFP_RECLAIM is
2446 * unreliable and fails to actually reclaim our
2447 * dirty pages -- unless you try over and over
2448 * again with !__GFP_NORETRY. However, we still
2449 * want to fail this allocation rather than
2450 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002451 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002452 */
Michal Hockodbb32952017-07-12 14:36:55 -07002453 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002454 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002455 } while (1);
2456
Chris Wilson871dfbd2016-10-11 09:20:21 +01002457 if (!i ||
2458 sg->length >= max_segment ||
2459 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002460 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002461 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002462 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002463 }
Imre Deak90797e62013-02-18 19:28:03 +02002464 st->nents++;
2465 sg_set_page(sg, page, PAGE_SIZE, 0);
2466 } else {
2467 sg->length += PAGE_SIZE;
2468 }
2469 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002470
2471 /* Check that the i965g/gm workaround works. */
2472 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002473 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002474 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002475 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002476 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002477 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002478
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002479 /* Trim unused sg entries to avoid wasting memory. */
2480 i915_sg_trim(st);
2481
Chris Wilson03ac84f2016-10-28 13:58:36 +01002482 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002483 if (ret) {
2484 /* DMA remapping failed? One possible cause is that
2485 * it could not reserve enough large entries, asking
2486 * for PAGE_SIZE chunks instead may be helpful.
2487 */
2488 if (max_segment > PAGE_SIZE) {
2489 for_each_sgt_page(page, sgt_iter, st)
2490 put_page(page);
2491 sg_free_table(st);
2492
2493 max_segment = PAGE_SIZE;
2494 goto rebuild_st;
2495 } else {
2496 dev_warn(&dev_priv->drm.pdev->dev,
2497 "Failed to DMA remap %lu pages\n",
2498 page_count);
2499 goto err_pages;
2500 }
2501 }
Imre Deake2273302015-07-09 12:59:05 +03002502
Eric Anholt673a3942008-07-30 12:06:12 -07002503 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002504 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002505
Matthew Auld84e89782017-10-09 12:00:24 +01002506 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002507
2508 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002509
Chris Wilsonb17993b2016-11-14 11:29:30 +00002510err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002511 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002512err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002513 for_each_sgt_page(page, sgt_iter, st)
2514 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002515 sg_free_table(st);
2516 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002517
2518 /* shmemfs first checks if there is enough memory to allocate the page
2519 * and reports ENOSPC should there be insufficient, along with the usual
2520 * ENOMEM for a genuine allocation failure.
2521 *
2522 * We use ENOSPC in our driver to mean that we have run out of aperture
2523 * space and so want to translate the error from shmemfs back to our
2524 * usual understanding of ENOMEM.
2525 */
Imre Deake2273302015-07-09 12:59:05 +03002526 if (ret == -ENOSPC)
2527 ret = -ENOMEM;
2528
Matthew Auldb91b09e2017-10-06 23:18:17 +01002529 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002530}
2531
2532void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002533 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002534 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002535{
Matthew Aulda5c081662017-10-06 23:18:18 +01002536 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2537 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2538 int i;
2539
Chris Wilson1233e2d2016-10-28 13:58:37 +01002540 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002541
2542 obj->mm.get_page.sg_pos = pages->sgl;
2543 obj->mm.get_page.sg_idx = 0;
2544
2545 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002546
2547 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002548 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002549 GEM_BUG_ON(obj->mm.quirked);
2550 __i915_gem_object_pin_pages(obj);
2551 obj->mm.quirked = true;
2552 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002553
Matthew Auld84e89782017-10-09 12:00:24 +01002554 GEM_BUG_ON(!sg_page_sizes);
2555 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002556
2557 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002558 * Calculate the supported page-sizes which fit into the given
2559 * sg_page_sizes. This will give us the page-sizes which we may be able
2560 * to use opportunistically when later inserting into the GTT. For
2561 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2562 * 64K or 4K pages, although in practice this will depend on a number of
2563 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002564 */
2565 obj->mm.page_sizes.sg = 0;
2566 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2567 if (obj->mm.page_sizes.phys & ~0u << i)
2568 obj->mm.page_sizes.sg |= BIT(i);
2569 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002570 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002571
2572 spin_lock(&i915->mm.obj_lock);
2573 list_add(&obj->mm.link, &i915->mm.unbound_list);
2574 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002575}
2576
2577static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2578{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002579 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002580
2581 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2582 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2583 return -EFAULT;
2584 }
2585
Matthew Auldb91b09e2017-10-06 23:18:17 +01002586 err = obj->ops->get_pages(obj);
2587 GEM_BUG_ON(!err && IS_ERR_OR_NULL(obj->mm.pages));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002588
Matthew Auldb91b09e2017-10-06 23:18:17 +01002589 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002590}
2591
Chris Wilson37e680a2012-06-07 15:38:42 +01002592/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002593 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002594 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002595 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002596 * either as a result of memory pressure (reaping pages under the shrinker)
2597 * or as the object is itself released.
2598 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002599int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002600{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002601 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002602
Chris Wilson1233e2d2016-10-28 13:58:37 +01002603 err = mutex_lock_interruptible(&obj->mm.lock);
2604 if (err)
2605 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002606
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002607 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002608 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2609
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002610 err = ____i915_gem_object_get_pages(obj);
2611 if (err)
2612 goto unlock;
2613
2614 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002615 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002616 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002617
Chris Wilson1233e2d2016-10-28 13:58:37 +01002618unlock:
2619 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002620 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002621}
2622
Dave Gordondd6034c2016-05-20 11:54:04 +01002623/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002624static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2625 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002626{
2627 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002628 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002629 struct sgt_iter sgt_iter;
2630 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002631 struct page *stack_pages[32];
2632 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002633 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002634 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002635 void *addr;
2636
2637 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002638 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002639 return kmap(sg_page(sgt->sgl));
2640
Dave Gordonb338fa42016-05-20 11:54:05 +01002641 if (n_pages > ARRAY_SIZE(stack_pages)) {
2642 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002643 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002644 if (!pages)
2645 return NULL;
2646 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002647
Dave Gordon85d12252016-05-20 11:54:06 +01002648 for_each_sgt_page(page, sgt_iter, sgt)
2649 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002650
2651 /* Check that we have the expected number of pages */
2652 GEM_BUG_ON(i != n_pages);
2653
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002654 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002655 default:
2656 MISSING_CASE(type);
2657 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002658 case I915_MAP_WB:
2659 pgprot = PAGE_KERNEL;
2660 break;
2661 case I915_MAP_WC:
2662 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2663 break;
2664 }
2665 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002666
Dave Gordonb338fa42016-05-20 11:54:05 +01002667 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002668 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002669
2670 return addr;
2671}
2672
2673/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002674void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2675 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002676{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002677 enum i915_map_type has_type;
2678 bool pinned;
2679 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002680 int ret;
2681
Tina Zhanga03f3952017-11-14 10:25:13 +00002682 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2683 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002684
Chris Wilson1233e2d2016-10-28 13:58:37 +01002685 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002686 if (ret)
2687 return ERR_PTR(ret);
2688
Chris Wilsona575c672017-08-28 11:46:31 +01002689 pinned = !(type & I915_MAP_OVERRIDE);
2690 type &= ~I915_MAP_OVERRIDE;
2691
Chris Wilson1233e2d2016-10-28 13:58:37 +01002692 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002693 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002694 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2695
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002696 ret = ____i915_gem_object_get_pages(obj);
2697 if (ret)
2698 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002699
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002700 smp_mb__before_atomic();
2701 }
2702 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002703 pinned = false;
2704 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002705 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002706
Chris Wilson0ce81782017-05-17 13:09:59 +01002707 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002708 if (ptr && has_type != type) {
2709 if (pinned) {
2710 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002711 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002712 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002713
2714 if (is_vmalloc_addr(ptr))
2715 vunmap(ptr);
2716 else
2717 kunmap(kmap_to_page(ptr));
2718
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002719 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002720 }
2721
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002722 if (!ptr) {
2723 ptr = i915_gem_object_map(obj, type);
2724 if (!ptr) {
2725 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002726 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002727 }
2728
Chris Wilson0ce81782017-05-17 13:09:59 +01002729 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002730 }
2731
Chris Wilson1233e2d2016-10-28 13:58:37 +01002732out_unlock:
2733 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002734 return ptr;
2735
Chris Wilson1233e2d2016-10-28 13:58:37 +01002736err_unpin:
2737 atomic_dec(&obj->mm.pages_pin_count);
2738err_unlock:
2739 ptr = ERR_PTR(ret);
2740 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002741}
2742
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002743static int
2744i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2745 const struct drm_i915_gem_pwrite *arg)
2746{
2747 struct address_space *mapping = obj->base.filp->f_mapping;
2748 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2749 u64 remain, offset;
2750 unsigned int pg;
2751
2752 /* Before we instantiate/pin the backing store for our use, we
2753 * can prepopulate the shmemfs filp efficiently using a write into
2754 * the pagecache. We avoid the penalty of instantiating all the
2755 * pages, important if the user is just writing to a few and never
2756 * uses the object on the GPU, and using a direct write into shmemfs
2757 * allows it to avoid the cost of retrieving a page (either swapin
2758 * or clearing-before-use) before it is overwritten.
2759 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002760 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002761 return -ENODEV;
2762
Chris Wilsona6d65e42017-10-16 21:27:32 +01002763 if (obj->mm.madv != I915_MADV_WILLNEED)
2764 return -EFAULT;
2765
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002766 /* Before the pages are instantiated the object is treated as being
2767 * in the CPU domain. The pages will be clflushed as required before
2768 * use, and we can freely write into the pages directly. If userspace
2769 * races pwrite with any other operation; corruption will ensue -
2770 * that is userspace's prerogative!
2771 */
2772
2773 remain = arg->size;
2774 offset = arg->offset;
2775 pg = offset_in_page(offset);
2776
2777 do {
2778 unsigned int len, unwritten;
2779 struct page *page;
2780 void *data, *vaddr;
2781 int err;
2782
2783 len = PAGE_SIZE - pg;
2784 if (len > remain)
2785 len = remain;
2786
2787 err = pagecache_write_begin(obj->base.filp, mapping,
2788 offset, len, 0,
2789 &page, &data);
2790 if (err < 0)
2791 return err;
2792
2793 vaddr = kmap(page);
2794 unwritten = copy_from_user(vaddr + pg, user_data, len);
2795 kunmap(page);
2796
2797 err = pagecache_write_end(obj->base.filp, mapping,
2798 offset, len, len - unwritten,
2799 page, data);
2800 if (err < 0)
2801 return err;
2802
2803 if (unwritten)
2804 return -EFAULT;
2805
2806 remain -= len;
2807 user_data += len;
2808 offset += len;
2809 pg = 0;
2810 } while (remain);
2811
2812 return 0;
2813}
2814
Chris Wilson77b25a92017-07-21 13:32:30 +01002815static bool ban_context(const struct i915_gem_context *ctx,
2816 unsigned int score)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002817{
Chris Wilson60958682016-12-31 11:20:11 +00002818 return (i915_gem_context_is_bannable(ctx) &&
Chris Wilson77b25a92017-07-21 13:32:30 +01002819 score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002820}
2821
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002822static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002823{
Chris Wilson77b25a92017-07-21 13:32:30 +01002824 unsigned int score;
2825 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002826
Chris Wilson77b25a92017-07-21 13:32:30 +01002827 atomic_inc(&ctx->guilty_count);
2828
2829 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
2830 banned = ban_context(ctx, score);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002831 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Chris Wilson77b25a92017-07-21 13:32:30 +01002832 ctx->name, score, yesno(banned));
2833 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002834 return;
2835
Chris Wilson77b25a92017-07-21 13:32:30 +01002836 i915_gem_context_set_banned(ctx);
2837 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2838 atomic_inc(&ctx->file_priv->context_bans);
2839 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2840 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2841 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002842}
2843
2844static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2845{
Chris Wilson77b25a92017-07-21 13:32:30 +01002846 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002847}
2848
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002849struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002850i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002851{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002852 struct drm_i915_gem_request *request, *active = NULL;
2853 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002854
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002855 /* We are called by the error capture and reset at a random
2856 * point in time. In particular, note that neither is crucially
2857 * ordered with an interrupt. After a hang, the GPU is dead and we
2858 * assume that no more writes can happen (we waited long enough for
2859 * all writes that were in transaction to be flushed) - adding an
2860 * extra delay for a recent interrupt is pointless. Hence, we do
2861 * not need an engine->irq_seqno_barrier() before the seqno reads.
2862 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002863 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002864 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002865 if (__i915_gem_request_completed(request,
2866 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002867 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002868
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002869 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002870 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2871 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002872
Chris Wilson754c9fd2017-02-23 07:44:14 +00002873 active = request;
2874 break;
2875 }
2876 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2877
2878 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002879}
2880
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002881static bool engine_stalled(struct intel_engine_cs *engine)
2882{
2883 if (!engine->hangcheck.stalled)
2884 return false;
2885
2886 /* Check for possible seqno movement after hang declaration */
2887 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2888 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2889 return false;
2890 }
2891
2892 return true;
2893}
2894
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002895/*
2896 * Ensure irq handler finishes, and not run again.
2897 * Also return the active request so that we only search for it once.
2898 */
2899struct drm_i915_gem_request *
2900i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2901{
2902 struct drm_i915_gem_request *request = NULL;
2903
Chris Wilson1749d902017-10-09 12:02:59 +01002904 /*
2905 * During the reset sequence, we must prevent the engine from
2906 * entering RC6. As the context state is undefined until we restart
2907 * the engine, if it does enter RC6 during the reset, the state
2908 * written to the powercontext is undefined and so we may lose
2909 * GPU state upon resume, i.e. fail to restart after a reset.
2910 */
2911 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2912
2913 /*
2914 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002915 * state (by calling dma_fence_signal) as we are processing
2916 * the reset. The write from the GPU of the seqno is
2917 * asynchronous and the signaler thread may see a different
2918 * value to us and declare the request complete, even though
2919 * the reset routine have picked that request as the active
2920 * (incomplete) request. This conflict is not handled
2921 * gracefully!
2922 */
2923 kthread_park(engine->breadcrumbs.signaler);
2924
Chris Wilson1749d902017-10-09 12:02:59 +01002925 /*
2926 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002927 * completed the reset in i915_gem_reset_finish(). If a request
2928 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302929 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002930 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302931 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002932 * prevents the race.
2933 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302934 tasklet_kill(&engine->execlists.tasklet);
2935 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002936
Michał Winiarskic41937f2017-10-26 15:35:58 +02002937 /*
2938 * We're using worker to queue preemption requests from the tasklet in
2939 * GuC submission mode.
2940 * Even though tasklet was disabled, we may still have a worker queued.
2941 * Let's make sure that all workers scheduled before disabling the
2942 * tasklet are completed before continuing with the reset.
2943 */
2944 if (engine->i915->guc.preempt_wq)
2945 flush_workqueue(engine->i915->guc.preempt_wq);
2946
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002947 if (engine->irq_seqno_barrier)
2948 engine->irq_seqno_barrier(engine);
2949
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002950 request = i915_gem_find_active_request(engine);
2951 if (request && request->fence.error == -EIO)
2952 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002953
2954 return request;
2955}
2956
Chris Wilson0e178ae2017-01-17 17:59:06 +02002957int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002958{
2959 struct intel_engine_cs *engine;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002960 struct drm_i915_gem_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002961 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002962 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002963
Chris Wilson0e178ae2017-01-17 17:59:06 +02002964 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002965 request = i915_gem_reset_prepare_engine(engine);
2966 if (IS_ERR(request)) {
2967 err = PTR_ERR(request);
2968 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002969 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002970
2971 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002972 }
2973
Chris Wilson4c965542017-01-17 17:59:01 +02002974 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002975
2976 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002977}
2978
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002979static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002980{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002981 void *vaddr = request->ring->vaddr;
2982 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002983
Chris Wilson821ed7d2016-09-09 14:11:53 +01002984 /* As this request likely depends on state from the lost
2985 * context, clear out all the user operations leaving the
2986 * breadcrumb at the end (so we get the fence notifications).
2987 */
2988 head = request->head;
2989 if (request->postfix < head) {
2990 memset(vaddr + head, 0, request->ring->size - head);
2991 head = 0;
2992 }
2993 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00002994
2995 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00002996}
2997
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002998static void engine_skip_context(struct drm_i915_gem_request *request)
2999{
3000 struct intel_engine_cs *engine = request->engine;
3001 struct i915_gem_context *hung_ctx = request->ctx;
3002 struct intel_timeline *timeline;
3003 unsigned long flags;
3004
3005 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3006
3007 spin_lock_irqsave(&engine->timeline->lock, flags);
3008 spin_lock(&timeline->lock);
3009
3010 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3011 if (request->ctx == hung_ctx)
3012 skip_request(request);
3013
3014 list_for_each_entry(request, &timeline->requests, link)
3015 skip_request(request);
3016
3017 spin_unlock(&timeline->lock);
3018 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3019}
3020
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003021/* Returns the request if it was guilty of the hang */
3022static struct drm_i915_gem_request *
3023i915_gem_reset_request(struct intel_engine_cs *engine,
3024 struct drm_i915_gem_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003025{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003026 /* The guilty request will get skipped on a hung engine.
3027 *
3028 * Users of client default contexts do not rely on logical
3029 * state preserved between batches so it is safe to execute
3030 * queued requests following the hang. Non default contexts
3031 * rely on preserved state, so skipping a batch loses the
3032 * evolution of the state and it needs to be considered corrupted.
3033 * Executing more queued batches on top of corrupted state is
3034 * risky. But we take the risk by trying to advance through
3035 * the queued requests in order to make the client behaviour
3036 * more predictable around resets, by not throwing away random
3037 * amount of batches it has prepared for execution. Sophisticated
3038 * clients can use gem_reset_stats_ioctl and dma fence status
3039 * (exported via sync_file info ioctl on explicit fences) to observe
3040 * when it loses the context state and should rebuild accordingly.
3041 *
3042 * The context ban, and ultimately the client ban, mechanism are safety
3043 * valves if client submission ends up resulting in nothing more than
3044 * subsequent hangs.
3045 */
3046
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003047 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003048 i915_gem_context_mark_guilty(request->ctx);
3049 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003050
3051 /* If this context is now banned, skip all pending requests. */
3052 if (i915_gem_context_is_banned(request->ctx))
3053 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003054 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003055 /*
3056 * Since this is not the hung engine, it may have advanced
3057 * since the hang declaration. Double check by refinding
3058 * the active request at the time of the reset.
3059 */
3060 request = i915_gem_find_active_request(engine);
3061 if (request) {
3062 i915_gem_context_mark_innocent(request->ctx);
3063 dma_fence_set_error(&request->fence, -EAGAIN);
3064
3065 /* Rewind the engine to replay the incomplete rq */
3066 spin_lock_irq(&engine->timeline->lock);
3067 request = list_prev_entry(request, link);
3068 if (&request->link == &engine->timeline->requests)
3069 request = NULL;
3070 spin_unlock_irq(&engine->timeline->lock);
3071 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003072 }
3073
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003074 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003075}
3076
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003077void i915_gem_reset_engine(struct intel_engine_cs *engine,
3078 struct drm_i915_gem_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003079{
Chris Wilsoned454f22017-07-21 13:32:29 +01003080 engine->irq_posted = 0;
3081
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003082 if (request)
3083 request = i915_gem_reset_request(engine, request);
3084
3085 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003086 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3087 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003088 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003089
3090 /* Setup the CS to resume from the breadcrumb of the hung request */
3091 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003092}
3093
Chris Wilsond8027092017-02-08 14:30:32 +00003094void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003095{
3096 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303097 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003098
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003099 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3100
Chris Wilson821ed7d2016-09-09 14:11:53 +01003101 i915_gem_retire_requests(dev_priv);
3102
Chris Wilson2ae55732017-02-12 17:20:02 +00003103 for_each_engine(engine, dev_priv, id) {
3104 struct i915_gem_context *ctx;
3105
Michel Thierryc64992e2017-06-20 10:57:44 +01003106 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003107 ctx = fetch_and_zero(&engine->last_retired_context);
3108 if (ctx)
3109 engine->context_unpin(engine, ctx);
3110 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003111
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003112 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003113
3114 if (dev_priv->gt.awake) {
3115 intel_sanitize_gt_powersave(dev_priv);
3116 intel_enable_gt_powersave(dev_priv);
3117 if (INTEL_GEN(dev_priv) >= 6)
3118 gen6_rps_busy(dev_priv);
3119 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003120}
3121
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003122void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3123{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303124 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003125 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003126
3127 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003128}
3129
Chris Wilsond8027092017-02-08 14:30:32 +00003130void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3131{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003132 struct intel_engine_cs *engine;
3133 enum intel_engine_id id;
3134
Chris Wilsond8027092017-02-08 14:30:32 +00003135 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003136
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003137 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003138 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003139 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003140 }
Chris Wilsond8027092017-02-08 14:30:32 +00003141}
3142
Chris Wilson821ed7d2016-09-09 14:11:53 +01003143static void nop_submit_request(struct drm_i915_gem_request *request)
3144{
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003145 dma_fence_set_error(&request->fence, -EIO);
3146
3147 i915_gem_request_submit(request);
3148}
3149
3150static void nop_complete_submit_request(struct drm_i915_gem_request *request)
3151{
Chris Wilson8d550822017-10-06 12:56:17 +01003152 unsigned long flags;
3153
Chris Wilson3cd94422017-01-10 17:22:45 +00003154 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003155
3156 spin_lock_irqsave(&request->engine->timeline->lock, flags);
3157 __i915_gem_request_submit(request);
Chris Wilson3dcf93f72016-11-22 14:41:20 +00003158 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003159 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003160}
3161
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003162void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003163{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003164 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303165 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003166
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003167 /*
3168 * First, stop submission to hw, but do not yet complete requests by
3169 * rolling the global seqno forward (since this would complete requests
3170 * for which we haven't set the fence error to EIO yet).
3171 */
Chris Wilson20e49332016-11-22 14:41:21 +00003172 for_each_engine(engine, i915, id)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003173 engine->submit_request = nop_submit_request;
3174
3175 /*
3176 * Make sure no one is running the old callback before we proceed with
3177 * cancelling requests and resetting the completion tracking. Otherwise
3178 * we might submit a request to the hardware which never completes.
3179 */
3180 synchronize_rcu();
3181
3182 for_each_engine(engine, i915, id) {
3183 /* Mark all executing requests as skipped */
3184 engine->cancel_requests(engine);
3185
3186 /*
3187 * Only once we've force-cancelled all in-flight requests can we
3188 * start to complete all requests.
3189 */
3190 engine->submit_request = nop_complete_submit_request;
3191 }
3192
3193 /*
3194 * Make sure no request can slip through without getting completed by
3195 * either this call here to intel_engine_init_global_seqno, or the one
3196 * in nop_complete_submit_request.
3197 */
3198 synchronize_rcu();
3199
3200 for_each_engine(engine, i915, id) {
3201 unsigned long flags;
3202
3203 /* Mark all pending requests as complete so that any concurrent
3204 * (lockless) lookup doesn't try and wait upon the request as we
3205 * reset it.
3206 */
3207 spin_lock_irqsave(&engine->timeline->lock, flags);
3208 intel_engine_init_global_seqno(engine,
3209 intel_engine_last_submit(engine));
3210 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3211 }
Chris Wilson20e49332016-11-22 14:41:21 +00003212
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003213 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3214 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003215}
3216
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003217bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3218{
3219 struct i915_gem_timeline *tl;
3220 int i;
3221
3222 lockdep_assert_held(&i915->drm.struct_mutex);
3223 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3224 return true;
3225
3226 /* Before unwedging, make sure that all pending operations
3227 * are flushed and errored out - we may have requests waiting upon
3228 * third party fences. We marked all inflight requests as EIO, and
3229 * every execbuf since returned EIO, for consistency we want all
3230 * the currently pending requests to also be marked as EIO, which
3231 * is done inside our nop_submit_request - and so we must wait.
3232 *
3233 * No more can be submitted until we reset the wedged bit.
3234 */
3235 list_for_each_entry(tl, &i915->gt.timelines, link) {
3236 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3237 struct drm_i915_gem_request *rq;
3238
3239 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3240 &i915->drm.struct_mutex);
3241 if (!rq)
3242 continue;
3243
3244 /* We can't use our normal waiter as we want to
3245 * avoid recursively trying to handle the current
3246 * reset. The basic dma_fence_default_wait() installs
3247 * a callback for dma_fence_signal(), which is
3248 * triggered by our nop handler (indirectly, the
3249 * callback enables the signaler thread which is
3250 * woken by the nop_submit_request() advancing the seqno
3251 * and when the seqno passes the fence, the signaler
3252 * then signals the fence waking us up).
3253 */
3254 if (dma_fence_default_wait(&rq->fence, true,
3255 MAX_SCHEDULE_TIMEOUT) < 0)
3256 return false;
3257 }
3258 }
3259
3260 /* Undo nop_submit_request. We prevent all new i915 requests from
3261 * being queued (by disallowing execbuf whilst wedged) so having
3262 * waited for all active requests above, we know the system is idle
3263 * and do not have to worry about a thread being inside
3264 * engine->submit_request() as we swap over. So unlike installing
3265 * the nop_submit_request on reset, we can do this from normal
3266 * context and do not require stop_machine().
3267 */
3268 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003269 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003270
3271 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3272 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3273
3274 return true;
3275}
3276
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003277static void
Eric Anholt673a3942008-07-30 12:06:12 -07003278i915_gem_retire_work_handler(struct work_struct *work)
3279{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003280 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003281 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003282 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003283
Chris Wilson891b48c2010-09-29 12:26:37 +01003284 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003285 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003286 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003287 mutex_unlock(&dev->struct_mutex);
3288 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003289
3290 /* Keep the retire handler running until we are finally idle.
3291 * We do not need to do this test under locking as in the worst-case
3292 * we queue the retire worker once too often.
3293 */
Chris Wilsonc9615612016-07-09 10:12:06 +01003294 if (READ_ONCE(dev_priv->gt.awake)) {
3295 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01003296 queue_delayed_work(dev_priv->wq,
3297 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003298 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01003299 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003300}
Chris Wilson891b48c2010-09-29 12:26:37 +01003301
Chris Wilson5427f202017-10-23 22:32:34 +01003302static inline bool
3303new_requests_since_last_retire(const struct drm_i915_private *i915)
3304{
3305 return (READ_ONCE(i915->gt.active_requests) ||
3306 work_pending(&i915->gt.idle_work.work));
3307}
3308
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003309static void
3310i915_gem_idle_work_handler(struct work_struct *work)
3311{
3312 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003313 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson67d97da2016-07-04 08:08:31 +01003314 bool rearm_hangcheck;
Chris Wilson5427f202017-10-23 22:32:34 +01003315 ktime_t end;
Chris Wilson67d97da2016-07-04 08:08:31 +01003316
3317 if (!READ_ONCE(dev_priv->gt.awake))
3318 return;
3319
Imre Deak0cb56702016-11-07 11:20:04 +02003320 /*
3321 * Wait for last execlists context complete, but bail out in case a
3322 * new request is submitted.
3323 */
Chris Wilson5427f202017-10-23 22:32:34 +01003324 end = ktime_add_ms(ktime_get(), 200);
3325 do {
3326 if (new_requests_since_last_retire(dev_priv))
3327 return;
3328
3329 if (intel_engines_are_idle(dev_priv))
3330 break;
3331
3332 usleep_range(100, 500);
3333 } while (ktime_before(ktime_get(), end));
Chris Wilson67d97da2016-07-04 08:08:31 +01003334
3335 rearm_hangcheck =
3336 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3337
Chris Wilson5427f202017-10-23 22:32:34 +01003338 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003339 /* Currently busy, come back later */
3340 mod_delayed_work(dev_priv->wq,
3341 &dev_priv->gt.idle_work,
3342 msecs_to_jiffies(50));
3343 goto out_rearm;
3344 }
3345
Imre Deak93c97dc2016-11-07 11:20:03 +02003346 /*
3347 * New request retired after this work handler started, extend active
3348 * period until next instance of the work.
3349 */
Chris Wilson5427f202017-10-23 22:32:34 +01003350 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003351 goto out_unlock;
3352
Chris Wilson5427f202017-10-23 22:32:34 +01003353 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003354 * Be paranoid and flush a concurrent interrupt to make sure
3355 * we don't reactivate any irq tasklets after parking.
3356 *
3357 * FIXME: Note that even though we have waited for execlists to be idle,
3358 * there may still be an in-flight interrupt even though the CSB
3359 * is now empty. synchronize_irq() makes sure that a residual interrupt
3360 * is completed before we continue, but it doesn't prevent the HW from
3361 * raising a spurious interrupt later. To complete the shield we should
3362 * coordinate disabling the CS irq with flushing the interrupts.
3363 */
3364 synchronize_irq(dev_priv->drm.irq);
3365
Chris Wilsonaba5e272017-10-25 15:39:41 +01003366 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003367 i915_gem_timelines_park(dev_priv);
3368
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003369 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003370
Chris Wilson67d97da2016-07-04 08:08:31 +01003371 GEM_BUG_ON(!dev_priv->gt.awake);
3372 dev_priv->gt.awake = false;
3373 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003374
Chris Wilson67d97da2016-07-04 08:08:31 +01003375 if (INTEL_GEN(dev_priv) >= 6)
3376 gen6_rps_idle(dev_priv);
3377 intel_runtime_pm_put(dev_priv);
3378out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003379 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003380
Chris Wilson67d97da2016-07-04 08:08:31 +01003381out_rearm:
3382 if (rearm_hangcheck) {
3383 GEM_BUG_ON(!dev_priv->gt.awake);
3384 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003385 }
Eric Anholt673a3942008-07-30 12:06:12 -07003386}
3387
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003388void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3389{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003390 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003391 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3392 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003393 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003394
Chris Wilsond1b48c12017-08-16 09:52:08 +01003395 mutex_lock(&i915->drm.struct_mutex);
3396
3397 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3398 struct i915_gem_context *ctx = lut->ctx;
3399 struct i915_vma *vma;
3400
Chris Wilson432295d2017-08-22 12:05:15 +01003401 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003402 if (ctx->file_priv != fpriv)
3403 continue;
3404
3405 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003406 GEM_BUG_ON(vma->obj != obj);
3407
3408 /* We allow the process to have multiple handles to the same
3409 * vma, in the same fd namespace, by virtue of flink/open.
3410 */
3411 GEM_BUG_ON(!vma->open_count);
3412 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003413 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003414
Chris Wilsond1b48c12017-08-16 09:52:08 +01003415 list_del(&lut->obj_link);
3416 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003417
Chris Wilsond1b48c12017-08-16 09:52:08 +01003418 kmem_cache_free(i915->luts, lut);
3419 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003420 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003421
3422 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003423}
3424
Chris Wilsone95433c2016-10-28 13:58:27 +01003425static unsigned long to_wait_timeout(s64 timeout_ns)
3426{
3427 if (timeout_ns < 0)
3428 return MAX_SCHEDULE_TIMEOUT;
3429
3430 if (timeout_ns == 0)
3431 return 0;
3432
3433 return nsecs_to_jiffies_timeout(timeout_ns);
3434}
3435
Ben Widawsky5816d642012-04-11 11:18:19 -07003436/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003437 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003438 * @dev: drm device pointer
3439 * @data: ioctl data blob
3440 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003441 *
3442 * Returns 0 if successful, else an error is returned with the remaining time in
3443 * the timeout parameter.
3444 * -ETIME: object is still busy after timeout
3445 * -ERESTARTSYS: signal interrupted the wait
3446 * -ENONENT: object doesn't exist
3447 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003448 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003449 * -ENOMEM: damn
3450 * -ENODEV: Internal IRQ fail
3451 * -E?: The add request failed
3452 *
3453 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3454 * non-zero timeout parameter the wait ioctl will wait for the given number of
3455 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3456 * without holding struct_mutex the object may become re-busied before this
3457 * function completes. A similar but shorter * race condition exists in the busy
3458 * ioctl
3459 */
3460int
3461i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3462{
3463 struct drm_i915_gem_wait *args = data;
3464 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003465 ktime_t start;
3466 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003467
Daniel Vetter11b5d512014-09-29 15:31:26 +02003468 if (args->flags != 0)
3469 return -EINVAL;
3470
Chris Wilson03ac0642016-07-20 13:31:51 +01003471 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003472 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003473 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003474
Chris Wilsone95433c2016-10-28 13:58:27 +01003475 start = ktime_get();
3476
3477 ret = i915_gem_object_wait(obj,
3478 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3479 to_wait_timeout(args->timeout_ns),
3480 to_rps_client(file));
3481
3482 if (args->timeout_ns > 0) {
3483 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3484 if (args->timeout_ns < 0)
3485 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003486
3487 /*
3488 * Apparently ktime isn't accurate enough and occasionally has a
3489 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3490 * things up to make the test happy. We allow up to 1 jiffy.
3491 *
3492 * This is a regression from the timespec->ktime conversion.
3493 */
3494 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3495 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003496
3497 /* Asked to wait beyond the jiffie/scheduler precision? */
3498 if (ret == -ETIME && args->timeout_ns)
3499 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003500 }
3501
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003502 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003503 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003504}
3505
Chris Wilson73cb9702016-10-28 13:58:46 +01003506static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003507{
Chris Wilson73cb9702016-10-28 13:58:46 +01003508 int ret, i;
3509
3510 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3511 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3512 if (ret)
3513 return ret;
3514 }
3515
3516 return 0;
3517}
3518
Chris Wilson25112b62017-03-30 15:50:39 +01003519static int wait_for_engines(struct drm_i915_private *i915)
3520{
Chris Wilsoncad99462017-08-26 12:09:33 +01003521 if (wait_for(intel_engines_are_idle(i915), 50)) {
3522 DRM_ERROR("Failed to idle engines, declaring wedged!\n");
3523 i915_gem_set_wedged(i915);
3524 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003525 }
3526
3527 return 0;
3528}
3529
Chris Wilson73cb9702016-10-28 13:58:46 +01003530int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3531{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003532 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003533
Chris Wilson863e9fd2017-05-30 13:13:32 +01003534 /* If the device is asleep, we have no requests outstanding */
3535 if (!READ_ONCE(i915->gt.awake))
3536 return 0;
3537
Chris Wilson9caa34a2016-11-11 14:58:08 +00003538 if (flags & I915_WAIT_LOCKED) {
3539 struct i915_gem_timeline *tl;
3540
3541 lockdep_assert_held(&i915->drm.struct_mutex);
3542
3543 list_for_each_entry(tl, &i915->gt.timelines, link) {
3544 ret = wait_for_timeline(tl, flags);
3545 if (ret)
3546 return ret;
3547 }
Chris Wilson72022a72017-03-30 15:50:38 +01003548
3549 i915_gem_retire_requests(i915);
3550 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003551
3552 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003553 } else {
3554 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003555 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003556
Chris Wilson25112b62017-03-30 15:50:39 +01003557 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003558}
3559
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003560static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3561{
Chris Wilsone27ab732017-06-15 13:38:49 +01003562 /*
3563 * We manually flush the CPU domain so that we can override and
3564 * force the flush for the display, and perform it asyncrhonously.
3565 */
3566 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3567 if (obj->cache_dirty)
3568 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003569 obj->base.write_domain = 0;
3570}
3571
3572void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3573{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003574 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003575 return;
3576
3577 mutex_lock(&obj->base.dev->struct_mutex);
3578 __i915_gem_object_flush_for_display(obj);
3579 mutex_unlock(&obj->base.dev->struct_mutex);
3580}
3581
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003582/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003583 * Moves a single object to the WC read, and possibly write domain.
3584 * @obj: object to act on
3585 * @write: ask for write access or read only
3586 *
3587 * This function returns when the move is complete, including waiting on
3588 * flushes to occur.
3589 */
3590int
3591i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3592{
3593 int ret;
3594
3595 lockdep_assert_held(&obj->base.dev->struct_mutex);
3596
3597 ret = i915_gem_object_wait(obj,
3598 I915_WAIT_INTERRUPTIBLE |
3599 I915_WAIT_LOCKED |
3600 (write ? I915_WAIT_ALL : 0),
3601 MAX_SCHEDULE_TIMEOUT,
3602 NULL);
3603 if (ret)
3604 return ret;
3605
3606 if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
3607 return 0;
3608
3609 /* Flush and acquire obj->pages so that we are coherent through
3610 * direct access in memory with previous cached writes through
3611 * shmemfs and that our cache domain tracking remains valid.
3612 * For example, if the obj->filp was moved to swap without us
3613 * being notified and releasing the pages, we would mistakenly
3614 * continue to assume that the obj remained out of the CPU cached
3615 * domain.
3616 */
3617 ret = i915_gem_object_pin_pages(obj);
3618 if (ret)
3619 return ret;
3620
3621 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3622
3623 /* Serialise direct access to this object with the barriers for
3624 * coherent writes from the GPU, by effectively invalidating the
3625 * WC domain upon first access.
3626 */
3627 if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
3628 mb();
3629
3630 /* It should now be out of any other write domains, and we can update
3631 * the domain values for our changes.
3632 */
3633 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3634 obj->base.read_domains |= I915_GEM_DOMAIN_WC;
3635 if (write) {
3636 obj->base.read_domains = I915_GEM_DOMAIN_WC;
3637 obj->base.write_domain = I915_GEM_DOMAIN_WC;
3638 obj->mm.dirty = true;
3639 }
3640
3641 i915_gem_object_unpin_pages(obj);
3642 return 0;
3643}
3644
3645/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003646 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003647 * @obj: object to act on
3648 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003649 *
3650 * This function returns when the move is complete, including waiting on
3651 * flushes to occur.
3652 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003653int
Chris Wilson20217462010-11-23 15:26:33 +00003654i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003655{
Eric Anholte47c68e2008-11-14 13:35:19 -08003656 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003657
Chris Wilsone95433c2016-10-28 13:58:27 +01003658 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003659
Chris Wilsone95433c2016-10-28 13:58:27 +01003660 ret = i915_gem_object_wait(obj,
3661 I915_WAIT_INTERRUPTIBLE |
3662 I915_WAIT_LOCKED |
3663 (write ? I915_WAIT_ALL : 0),
3664 MAX_SCHEDULE_TIMEOUT,
3665 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003666 if (ret)
3667 return ret;
3668
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003669 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3670 return 0;
3671
Chris Wilson43566de2015-01-02 16:29:29 +05303672 /* Flush and acquire obj->pages so that we are coherent through
3673 * direct access in memory with previous cached writes through
3674 * shmemfs and that our cache domain tracking remains valid.
3675 * For example, if the obj->filp was moved to swap without us
3676 * being notified and releasing the pages, we would mistakenly
3677 * continue to assume that the obj remained out of the CPU cached
3678 * domain.
3679 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003680 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303681 if (ret)
3682 return ret;
3683
Chris Wilsonef749212017-04-12 12:01:10 +01003684 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003685
Chris Wilsond0a57782012-10-09 19:24:37 +01003686 /* Serialise direct access to this object with the barriers for
3687 * coherent writes from the GPU, by effectively invalidating the
3688 * GTT domain upon first access.
3689 */
3690 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3691 mb();
3692
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003693 /* It should now be out of any other write domains, and we can update
3694 * the domain values for our changes.
3695 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003696 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003697 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003698 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003699 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3700 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003701 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003702 }
3703
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003704 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003705 return 0;
3706}
3707
Chris Wilsonef55f922015-10-09 14:11:27 +01003708/**
3709 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003710 * @obj: object to act on
3711 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003712 *
3713 * After this function returns, the object will be in the new cache-level
3714 * across all GTT and the contents of the backing storage will be coherent,
3715 * with respect to the new cache-level. In order to keep the backing storage
3716 * coherent for all users, we only allow a single cache level to be set
3717 * globally on the object and prevent it from being changed whilst the
3718 * hardware is reading from the object. That is if the object is currently
3719 * on the scanout it will be set to uncached (or equivalent display
3720 * cache coherency) and all non-MOCS GPU access will also be uncached so
3721 * that all direct access to the scanout remains coherent.
3722 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003723int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3724 enum i915_cache_level cache_level)
3725{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003726 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003727 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003728
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003729 lockdep_assert_held(&obj->base.dev->struct_mutex);
3730
Chris Wilsone4ffd172011-04-04 09:44:39 +01003731 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003732 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003733
Chris Wilsonef55f922015-10-09 14:11:27 +01003734 /* Inspect the list of currently bound VMA and unbind any that would
3735 * be invalid given the new cache-level. This is principally to
3736 * catch the issue of the CS prefetch crossing page boundaries and
3737 * reading an invalid PTE on older architectures.
3738 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003739restart:
3740 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003741 if (!drm_mm_node_allocated(&vma->node))
3742 continue;
3743
Chris Wilson20dfbde2016-08-04 16:32:30 +01003744 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003745 DRM_DEBUG("can not change the cache level of pinned objects\n");
3746 return -EBUSY;
3747 }
3748
Chris Wilsonaa653a62016-08-04 07:52:27 +01003749 if (i915_gem_valid_gtt_space(vma, cache_level))
3750 continue;
3751
3752 ret = i915_vma_unbind(vma);
3753 if (ret)
3754 return ret;
3755
3756 /* As unbinding may affect other elements in the
3757 * obj->vma_list (due to side-effects from retiring
3758 * an active vma), play safe and restart the iterator.
3759 */
3760 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003761 }
3762
Chris Wilsonef55f922015-10-09 14:11:27 +01003763 /* We can reuse the existing drm_mm nodes but need to change the
3764 * cache-level on the PTE. We could simply unbind them all and
3765 * rebind with the correct cache-level on next use. However since
3766 * we already have a valid slot, dma mapping, pages etc, we may as
3767 * rewrite the PTE in the belief that doing so tramples upon less
3768 * state and so involves less work.
3769 */
Chris Wilson15717de2016-08-04 07:52:26 +01003770 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003771 /* Before we change the PTE, the GPU must not be accessing it.
3772 * If we wait upon the object, we know that all the bound
3773 * VMA are no longer active.
3774 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003775 ret = i915_gem_object_wait(obj,
3776 I915_WAIT_INTERRUPTIBLE |
3777 I915_WAIT_LOCKED |
3778 I915_WAIT_ALL,
3779 MAX_SCHEDULE_TIMEOUT,
3780 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003781 if (ret)
3782 return ret;
3783
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003784 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3785 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003786 /* Access to snoopable pages through the GTT is
3787 * incoherent and on some machines causes a hard
3788 * lockup. Relinquish the CPU mmaping to force
3789 * userspace to refault in the pages and we can
3790 * then double check if the GTT mapping is still
3791 * valid for that pointer access.
3792 */
3793 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003794
Chris Wilsonef55f922015-10-09 14:11:27 +01003795 /* As we no longer need a fence for GTT access,
3796 * we can relinquish it now (and so prevent having
3797 * to steal a fence from someone else on the next
3798 * fence request). Note GPU activity would have
3799 * dropped the fence as all snoopable access is
3800 * supposed to be linear.
3801 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003802 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3803 ret = i915_vma_put_fence(vma);
3804 if (ret)
3805 return ret;
3806 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003807 } else {
3808 /* We either have incoherent backing store and
3809 * so no GTT access or the architecture is fully
3810 * coherent. In such cases, existing GTT mmaps
3811 * ignore the cache bit in the PTE and we can
3812 * rewrite it without confusing the GPU or having
3813 * to force userspace to fault back in its mmaps.
3814 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003815 }
3816
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003817 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003818 if (!drm_mm_node_allocated(&vma->node))
3819 continue;
3820
3821 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3822 if (ret)
3823 return ret;
3824 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003825 }
3826
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003827 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003828 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003829 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003830 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003831
Chris Wilsone4ffd172011-04-04 09:44:39 +01003832 return 0;
3833}
3834
Ben Widawsky199adf42012-09-21 17:01:20 -07003835int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3836 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003837{
Ben Widawsky199adf42012-09-21 17:01:20 -07003838 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003839 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003840 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003841
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003842 rcu_read_lock();
3843 obj = i915_gem_object_lookup_rcu(file, args->handle);
3844 if (!obj) {
3845 err = -ENOENT;
3846 goto out;
3847 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003848
Chris Wilson651d7942013-08-08 14:41:10 +01003849 switch (obj->cache_level) {
3850 case I915_CACHE_LLC:
3851 case I915_CACHE_L3_LLC:
3852 args->caching = I915_CACHING_CACHED;
3853 break;
3854
Chris Wilson4257d3b2013-08-08 14:41:11 +01003855 case I915_CACHE_WT:
3856 args->caching = I915_CACHING_DISPLAY;
3857 break;
3858
Chris Wilson651d7942013-08-08 14:41:10 +01003859 default:
3860 args->caching = I915_CACHING_NONE;
3861 break;
3862 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003863out:
3864 rcu_read_unlock();
3865 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003866}
3867
Ben Widawsky199adf42012-09-21 17:01:20 -07003868int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3869 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003870{
Chris Wilson9c870d02016-10-24 13:42:15 +01003871 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003872 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003873 struct drm_i915_gem_object *obj;
3874 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003875 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003876
Ben Widawsky199adf42012-09-21 17:01:20 -07003877 switch (args->caching) {
3878 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003879 level = I915_CACHE_NONE;
3880 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003881 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003882 /*
3883 * Due to a HW issue on BXT A stepping, GPU stores via a
3884 * snooped mapping may leave stale data in a corresponding CPU
3885 * cacheline, whereas normally such cachelines would get
3886 * invalidated.
3887 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003888 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003889 return -ENODEV;
3890
Chris Wilsone6994ae2012-07-10 10:27:08 +01003891 level = I915_CACHE_LLC;
3892 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003893 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003894 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003895 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003896 default:
3897 return -EINVAL;
3898 }
3899
Chris Wilsond65415d2017-01-19 08:22:10 +00003900 obj = i915_gem_object_lookup(file, args->handle);
3901 if (!obj)
3902 return -ENOENT;
3903
Tina Zhanga03f3952017-11-14 10:25:13 +00003904 /*
3905 * The caching mode of proxy object is handled by its generator, and
3906 * not allowed to be changed by userspace.
3907 */
3908 if (i915_gem_object_is_proxy(obj)) {
3909 ret = -ENXIO;
3910 goto out;
3911 }
3912
Chris Wilsond65415d2017-01-19 08:22:10 +00003913 if (obj->cache_level == level)
3914 goto out;
3915
3916 ret = i915_gem_object_wait(obj,
3917 I915_WAIT_INTERRUPTIBLE,
3918 MAX_SCHEDULE_TIMEOUT,
3919 to_rps_client(file));
3920 if (ret)
3921 goto out;
3922
Ben Widawsky3bc29132012-09-26 16:15:20 -07003923 ret = i915_mutex_lock_interruptible(dev);
3924 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003925 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003926
3927 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003928 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003929
3930out:
3931 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003932 return ret;
3933}
3934
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003935/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003936 * Prepare buffer for display plane (scanout, cursors, etc).
3937 * Can be called from an uninterruptible phase (modesetting) and allows
3938 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003939 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003940struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003941i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3942 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003943 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003944{
Chris Wilson058d88c2016-08-15 10:49:06 +01003945 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003946 int ret;
3947
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003948 lockdep_assert_held(&obj->base.dev->struct_mutex);
3949
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003950 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003951 * display coherency whilst setting up the cache domains.
3952 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003953 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003954
Eric Anholta7ef0642011-03-29 16:59:54 -07003955 /* The display engine is not coherent with the LLC cache on gen6. As
3956 * a result, we make sure that the pinning that is about to occur is
3957 * done with uncached PTEs. This is lowest common denominator for all
3958 * chipsets.
3959 *
3960 * However for gen6+, we could do better by using the GFDT bit instead
3961 * of uncaching, which would allow us to flush all the LLC-cached data
3962 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3963 */
Chris Wilson651d7942013-08-08 14:41:10 +01003964 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003965 HAS_WT(to_i915(obj->base.dev)) ?
3966 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003967 if (ret) {
3968 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003969 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003970 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003971
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003972 /* As the user may map the buffer once pinned in the display plane
3973 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003974 * always use map_and_fenceable for all scanout buffers. However,
3975 * it may simply be too big to fit into mappable, in which case
3976 * put it anyway and hope that userspace can cope (but always first
3977 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003978 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003979 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003980 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003981 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3982 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003983 if (IS_ERR(vma)) {
3984 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3985 unsigned int flags;
3986
3987 /* Valleyview is definitely limited to scanning out the first
3988 * 512MiB. Lets presume this behaviour was inherited from the
3989 * g4x display engine and that all earlier gen are similarly
3990 * limited. Testing suggests that it is a little more
3991 * complicated than this. For example, Cherryview appears quite
3992 * happy to scanout from anywhere within its global aperture.
3993 */
3994 flags = 0;
3995 if (HAS_GMCH_DISPLAY(i915))
3996 flags = PIN_MAPPABLE;
3997 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
3998 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003999 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004000 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004001
Chris Wilsond8923dc2016-08-18 17:17:07 +01004002 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4003
Chris Wilsona6a7cc42016-11-18 21:17:46 +00004004 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004005 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00004006 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004007
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004008 /* It should now be out of any other write domains, and we can update
4009 * the domain values for our changes.
4010 */
Chris Wilson05394f32010-11-08 19:18:58 +00004011 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004012
Chris Wilson058d88c2016-08-15 10:49:06 +01004013 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004014
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004015err_unpin_global:
4016 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004017 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004018}
4019
4020void
Chris Wilson058d88c2016-08-15 10:49:06 +01004021i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004022{
Chris Wilson49d73912016-11-29 09:50:08 +00004023 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004024
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004025 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004026 return;
4027
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004028 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004029 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004030
Chris Wilson383d5822016-08-18 17:17:08 +01004031 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004032 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004033
Chris Wilson058d88c2016-08-15 10:49:06 +01004034 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004035}
4036
Eric Anholte47c68e2008-11-14 13:35:19 -08004037/**
4038 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004039 * @obj: object to act on
4040 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004041 *
4042 * This function returns when the move is complete, including waiting on
4043 * flushes to occur.
4044 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004045int
Chris Wilson919926a2010-11-12 13:42:53 +00004046i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004047{
Eric Anholte47c68e2008-11-14 13:35:19 -08004048 int ret;
4049
Chris Wilsone95433c2016-10-28 13:58:27 +01004050 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004051
Chris Wilsone95433c2016-10-28 13:58:27 +01004052 ret = i915_gem_object_wait(obj,
4053 I915_WAIT_INTERRUPTIBLE |
4054 I915_WAIT_LOCKED |
4055 (write ? I915_WAIT_ALL : 0),
4056 MAX_SCHEDULE_TIMEOUT,
4057 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004058 if (ret)
4059 return ret;
4060
Chris Wilsonef749212017-04-12 12:01:10 +01004061 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004062
Eric Anholte47c68e2008-11-14 13:35:19 -08004063 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00004064 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004065 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00004066 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004067 }
4068
4069 /* It should now be out of any other write domains, and we can update
4070 * the domain values for our changes.
4071 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004072 GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004073
4074 /* If we're writing through the CPU, then the GPU read domains will
4075 * need to be invalidated at next use.
4076 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004077 if (write)
4078 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004079
4080 return 0;
4081}
4082
Eric Anholt673a3942008-07-30 12:06:12 -07004083/* Throttle our rendering by waiting until the ring has completed our requests
4084 * emitted over 20 msec ago.
4085 *
Eric Anholtb9624422009-06-03 07:27:35 +00004086 * Note that if we were to use the current jiffies each time around the loop,
4087 * we wouldn't escape the function with any frames outstanding if the time to
4088 * render a frame was over 20ms.
4089 *
Eric Anholt673a3942008-07-30 12:06:12 -07004090 * This should get us reasonable parallelism between CPU and GPU but also
4091 * relatively low latency when blocking on a particular request to finish.
4092 */
4093static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004094i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004095{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004096 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004097 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004098 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00004099 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004100 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004101
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004102 /* ABI: return -EIO if already wedged */
4103 if (i915_terminally_wedged(&dev_priv->gpu_error))
4104 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004105
Chris Wilson1c255952010-09-26 11:03:27 +01004106 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004107 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004108 if (time_after_eq(request->emitted_jiffies, recent_enough))
4109 break;
4110
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004111 if (target) {
4112 list_del(&target->client_link);
4113 target->file_priv = NULL;
4114 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004115
John Harrison54fb2412014-11-24 18:49:27 +00004116 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004117 }
John Harrisonff865882014-11-24 18:49:28 +00004118 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01004119 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004120 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004121
John Harrison54fb2412014-11-24 18:49:27 +00004122 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004123 return 0;
4124
Chris Wilsone95433c2016-10-28 13:58:27 +01004125 ret = i915_wait_request(target,
4126 I915_WAIT_INTERRUPTIBLE,
4127 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01004128 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004129
Chris Wilsone95433c2016-10-28 13:58:27 +01004130 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004131}
4132
Chris Wilson058d88c2016-08-15 10:49:06 +01004133struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004134i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4135 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004136 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004137 u64 alignment,
4138 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004139{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004140 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4141 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004142 struct i915_vma *vma;
4143 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004144
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004145 lockdep_assert_held(&obj->base.dev->struct_mutex);
4146
Chris Wilson43ae70d92017-10-09 09:44:01 +01004147 if (!view && flags & PIN_MAPPABLE) {
4148 /* If the required space is larger than the available
4149 * aperture, we will not able to find a slot for the
4150 * object and unbinding the object now will be in
4151 * vain. Worse, doing so may cause us to ping-pong
4152 * the object in and out of the Global GTT and
4153 * waste a lot of cycles under the mutex.
4154 */
4155 if (obj->base.size > dev_priv->ggtt.mappable_end)
4156 return ERR_PTR(-E2BIG);
4157
4158 /* If NONBLOCK is set the caller is optimistically
4159 * trying to cache the full object within the mappable
4160 * aperture, and *must* have a fallback in place for
4161 * situations where we cannot bind the object. We
4162 * can be a little more lax here and use the fallback
4163 * more often to avoid costly migrations of ourselves
4164 * and other objects within the aperture.
4165 *
4166 * Half-the-aperture is used as a simple heuristic.
4167 * More interesting would to do search for a free
4168 * block prior to making the commitment to unbind.
4169 * That caters for the self-harm case, and with a
4170 * little more heuristics (e.g. NOFAULT, NOEVICT)
4171 * we could try to minimise harm to others.
4172 */
4173 if (flags & PIN_NONBLOCK &&
4174 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4175 return ERR_PTR(-ENOSPC);
4176 }
4177
Chris Wilson718659a2017-01-16 15:21:28 +00004178 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004179 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004180 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004181
4182 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d92017-10-09 09:44:01 +01004183 if (flags & PIN_NONBLOCK) {
4184 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4185 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004186
Chris Wilson43ae70d92017-10-09 09:44:01 +01004187 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004188 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004189 return ERR_PTR(-ENOSPC);
4190 }
4191
Chris Wilson59bfa122016-08-04 16:32:31 +01004192 WARN(i915_vma_is_pinned(vma),
4193 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004194 " offset=%08x, req.alignment=%llx,"
4195 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4196 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004197 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004198 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004199 ret = i915_vma_unbind(vma);
4200 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004201 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004202 }
4203
Chris Wilson058d88c2016-08-15 10:49:06 +01004204 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4205 if (ret)
4206 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004207
Chris Wilson058d88c2016-08-15 10:49:06 +01004208 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004209}
4210
Chris Wilsonedf6b762016-08-09 09:23:33 +01004211static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004212{
4213 /* Note that we could alias engines in the execbuf API, but
4214 * that would be very unwise as it prevents userspace from
4215 * fine control over engine selection. Ahem.
4216 *
4217 * This should be something like EXEC_MAX_ENGINE instead of
4218 * I915_NUM_ENGINES.
4219 */
4220 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4221 return 0x10000 << id;
4222}
4223
4224static __always_inline unsigned int __busy_write_id(unsigned int id)
4225{
Chris Wilson70cb4722016-08-09 18:08:25 +01004226 /* The uABI guarantees an active writer is also amongst the read
4227 * engines. This would be true if we accessed the activity tracking
4228 * under the lock, but as we perform the lookup of the object and
4229 * its activity locklessly we can not guarantee that the last_write
4230 * being active implies that we have set the same engine flag from
4231 * last_read - hence we always set both read and write busy for
4232 * last_write.
4233 */
4234 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004235}
4236
Chris Wilsonedf6b762016-08-09 09:23:33 +01004237static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004238__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004239 unsigned int (*flag)(unsigned int id))
4240{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004241 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004242
Chris Wilsond07f0e52016-10-28 13:58:44 +01004243 /* We have to check the current hw status of the fence as the uABI
4244 * guarantees forward progress. We could rely on the idle worker
4245 * to eventually flush us, but to minimise latency just ask the
4246 * hardware.
4247 *
4248 * Note we only report on the status of native fences.
4249 */
4250 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004251 return 0;
4252
Chris Wilsond07f0e52016-10-28 13:58:44 +01004253 /* opencode to_request() in order to avoid const warnings */
4254 rq = container_of(fence, struct drm_i915_gem_request, fence);
4255 if (i915_gem_request_completed(rq))
4256 return 0;
4257
Chris Wilson1d39f282017-04-11 13:43:06 +01004258 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004259}
4260
Chris Wilsonedf6b762016-08-09 09:23:33 +01004261static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004262busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004263{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004264 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004265}
4266
Chris Wilsonedf6b762016-08-09 09:23:33 +01004267static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004268busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004269{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004270 if (!fence)
4271 return 0;
4272
4273 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004274}
4275
Eric Anholt673a3942008-07-30 12:06:12 -07004276int
Eric Anholt673a3942008-07-30 12:06:12 -07004277i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004278 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004279{
4280 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004281 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004282 struct reservation_object_list *list;
4283 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004284 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004285
Chris Wilsond07f0e52016-10-28 13:58:44 +01004286 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004287 rcu_read_lock();
4288 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004289 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004290 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004291
4292 /* A discrepancy here is that we do not report the status of
4293 * non-i915 fences, i.e. even though we may report the object as idle,
4294 * a call to set-domain may still stall waiting for foreign rendering.
4295 * This also means that wait-ioctl may report an object as busy,
4296 * where busy-ioctl considers it idle.
4297 *
4298 * We trade the ability to warn of foreign fences to report on which
4299 * i915 engines are active for the object.
4300 *
4301 * Alternatively, we can trade that extra information on read/write
4302 * activity with
4303 * args->busy =
4304 * !reservation_object_test_signaled_rcu(obj->resv, true);
4305 * to report the overall busyness. This is what the wait-ioctl does.
4306 *
4307 */
4308retry:
4309 seq = raw_read_seqcount(&obj->resv->seq);
4310
4311 /* Translate the exclusive fence to the READ *and* WRITE engine */
4312 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4313
4314 /* Translate shared fences to READ set of engines */
4315 list = rcu_dereference(obj->resv->fence);
4316 if (list) {
4317 unsigned int shared_count = list->shared_count, i;
4318
4319 for (i = 0; i < shared_count; ++i) {
4320 struct dma_fence *fence =
4321 rcu_dereference(list->shared[i]);
4322
4323 args->busy |= busy_check_reader(fence);
4324 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004325 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004326
Chris Wilsond07f0e52016-10-28 13:58:44 +01004327 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4328 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004329
Chris Wilsond07f0e52016-10-28 13:58:44 +01004330 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004331out:
4332 rcu_read_unlock();
4333 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004334}
4335
4336int
4337i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4338 struct drm_file *file_priv)
4339{
Akshay Joshi0206e352011-08-16 15:34:10 -04004340 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004341}
4342
Chris Wilson3ef94da2009-09-14 16:50:29 +01004343int
4344i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4345 struct drm_file *file_priv)
4346{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004347 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004348 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004349 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004350 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004351
4352 switch (args->madv) {
4353 case I915_MADV_DONTNEED:
4354 case I915_MADV_WILLNEED:
4355 break;
4356 default:
4357 return -EINVAL;
4358 }
4359
Chris Wilson03ac0642016-07-20 13:31:51 +01004360 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004361 if (!obj)
4362 return -ENOENT;
4363
4364 err = mutex_lock_interruptible(&obj->mm.lock);
4365 if (err)
4366 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004367
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004368 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004369 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004370 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004371 if (obj->mm.madv == I915_MADV_WILLNEED) {
4372 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004373 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004374 obj->mm.quirked = false;
4375 }
4376 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004377 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004378 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004379 obj->mm.quirked = true;
4380 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004381 }
4382
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004383 if (obj->mm.madv != __I915_MADV_PURGED)
4384 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004385
Chris Wilson6c085a72012-08-20 11:40:46 +02004386 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004387 if (obj->mm.madv == I915_MADV_DONTNEED &&
4388 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004389 i915_gem_object_truncate(obj);
4390
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004391 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004392 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004393
Chris Wilson1233e2d2016-10-28 13:58:37 +01004394out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004395 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004396 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004397}
4398
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004399static void
4400frontbuffer_retire(struct i915_gem_active *active,
4401 struct drm_i915_gem_request *request)
4402{
4403 struct drm_i915_gem_object *obj =
4404 container_of(active, typeof(*obj), frontbuffer_write);
4405
Chris Wilsond59b21e2017-02-22 11:40:49 +00004406 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004407}
4408
Chris Wilson37e680a2012-06-07 15:38:42 +01004409void i915_gem_object_init(struct drm_i915_gem_object *obj,
4410 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004411{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004412 mutex_init(&obj->mm.lock);
4413
Ben Widawsky2f633152013-07-17 12:19:03 -07004414 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004415 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004416 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004417
Chris Wilson37e680a2012-06-07 15:38:42 +01004418 obj->ops = ops;
4419
Chris Wilsond07f0e52016-10-28 13:58:44 +01004420 reservation_object_init(&obj->__builtin_resv);
4421 obj->resv = &obj->__builtin_resv;
4422
Chris Wilson50349242016-08-18 17:17:04 +01004423 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004424 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004425
4426 obj->mm.madv = I915_MADV_WILLNEED;
4427 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4428 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004429
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004430 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004431}
4432
Chris Wilson37e680a2012-06-07 15:38:42 +01004433static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004434 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4435 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004436
Chris Wilson37e680a2012-06-07 15:38:42 +01004437 .get_pages = i915_gem_object_get_pages_gtt,
4438 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004439
4440 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004441};
4442
Matthew Auld465c4032017-10-06 23:18:14 +01004443static int i915_gem_object_create_shmem(struct drm_device *dev,
4444 struct drm_gem_object *obj,
4445 size_t size)
4446{
4447 struct drm_i915_private *i915 = to_i915(dev);
4448 unsigned long flags = VM_NORESERVE;
4449 struct file *filp;
4450
4451 drm_gem_private_object_init(dev, obj, size);
4452
4453 if (i915->mm.gemfs)
4454 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4455 flags);
4456 else
4457 filp = shmem_file_setup("i915", size, flags);
4458
4459 if (IS_ERR(filp))
4460 return PTR_ERR(filp);
4461
4462 obj->filp = filp;
4463
4464 return 0;
4465}
4466
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004467struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004468i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004469{
Daniel Vetterc397b902010-04-09 19:05:07 +00004470 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004471 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004472 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004473 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004474 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004475
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004476 /* There is a prevalence of the assumption that we fit the object's
4477 * page count inside a 32bit _signed_ variable. Let's document this and
4478 * catch if we ever need to fix it. In the meantime, if you do spot
4479 * such a local variable, please consider fixing!
4480 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004481 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004482 return ERR_PTR(-E2BIG);
4483
4484 if (overflows_type(size, obj->base.size))
4485 return ERR_PTR(-E2BIG);
4486
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004487 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004488 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004489 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004490
Matthew Auld465c4032017-10-06 23:18:14 +01004491 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004492 if (ret)
4493 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004494
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004495 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004496 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004497 /* 965gm cannot relocate objects above 4GiB. */
4498 mask &= ~__GFP_HIGHMEM;
4499 mask |= __GFP_DMA32;
4500 }
4501
Al Viro93c76a32015-12-04 23:45:44 -05004502 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004503 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004504 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004505
Chris Wilson37e680a2012-06-07 15:38:42 +01004506 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004507
Daniel Vetterc397b902010-04-09 19:05:07 +00004508 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4509 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4510
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004511 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004512 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004513 * cache) for about a 10% performance improvement
4514 * compared to uncached. Graphics requests other than
4515 * display scanout are coherent with the CPU in
4516 * accessing this cache. This means in this mode we
4517 * don't need to clflush on the CPU side, and on the
4518 * GPU side we only need to flush internal caches to
4519 * get data visible to the CPU.
4520 *
4521 * However, we maintain the display planes as UC, and so
4522 * need to rebind when first used as such.
4523 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004524 cache_level = I915_CACHE_LLC;
4525 else
4526 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004527
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004528 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004529
Daniel Vetterd861e332013-07-24 23:25:03 +02004530 trace_i915_gem_object_create(obj);
4531
Chris Wilson05394f32010-11-08 19:18:58 +00004532 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004533
4534fail:
4535 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004536 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004537}
4538
Chris Wilson340fbd82014-05-22 09:16:52 +01004539static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4540{
4541 /* If we are the last user of the backing storage (be it shmemfs
4542 * pages or stolen etc), we know that the pages are going to be
4543 * immediately released. In this case, we can then skip copying
4544 * back the contents from the GPU.
4545 */
4546
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004547 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004548 return false;
4549
4550 if (obj->base.filp == NULL)
4551 return true;
4552
4553 /* At first glance, this looks racy, but then again so would be
4554 * userspace racing mmap against close. However, the first external
4555 * reference to the filp can only be obtained through the
4556 * i915_gem_mmap_ioctl() which safeguards us against the user
4557 * acquiring such a reference whilst we are in the middle of
4558 * freeing the object.
4559 */
4560 return atomic_long_read(&obj->base.filp->f_count) == 1;
4561}
4562
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004563static void __i915_gem_free_objects(struct drm_i915_private *i915,
4564 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004565{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004566 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004567
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004568 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004569 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004570 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004571
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004572 trace_i915_gem_object_destroy(obj);
4573
Chris Wilsoncc731f52017-10-13 21:26:21 +01004574 mutex_lock(&i915->drm.struct_mutex);
4575
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004576 GEM_BUG_ON(i915_gem_object_is_active(obj));
4577 list_for_each_entry_safe(vma, vn,
4578 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004579 GEM_BUG_ON(i915_vma_is_active(vma));
4580 vma->flags &= ~I915_VMA_PIN_MASK;
4581 i915_vma_close(vma);
4582 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004583 GEM_BUG_ON(!list_empty(&obj->vma_list));
4584 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004585
Chris Wilsonf2123812017-10-16 12:40:37 +01004586 /* This serializes freeing with the shrinker. Since the free
4587 * is delayed, first by RCU then by the workqueue, we want the
4588 * shrinker to be able to free pages of unreferenced objects,
4589 * or else we may oom whilst there are plenty of deferred
4590 * freed objects.
4591 */
4592 if (i915_gem_object_has_pages(obj)) {
4593 spin_lock(&i915->mm.obj_lock);
4594 list_del_init(&obj->mm.link);
4595 spin_unlock(&i915->mm.obj_lock);
4596 }
4597
Chris Wilsoncc731f52017-10-13 21:26:21 +01004598 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004599
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004600 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004601 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004602 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004603 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004604
4605 if (obj->ops->release)
4606 obj->ops->release(obj);
4607
4608 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4609 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004610 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004611 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004612
4613 if (obj->base.import_attach)
4614 drm_prime_gem_destroy(&obj->base, NULL);
4615
Chris Wilsond07f0e52016-10-28 13:58:44 +01004616 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004617 drm_gem_object_release(&obj->base);
4618 i915_gem_info_remove_obj(i915, obj->base.size);
4619
4620 kfree(obj->bit_17);
4621 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004622
4623 if (on)
4624 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004625 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004626 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004627}
4628
4629static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4630{
4631 struct llist_node *freed;
4632
Chris Wilson87701b42017-10-13 21:26:20 +01004633 /* Free the oldest, most stale object to keep the free_list short */
4634 freed = NULL;
4635 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4636 /* Only one consumer of llist_del_first() allowed */
4637 spin_lock(&i915->mm.free_lock);
4638 freed = llist_del_first(&i915->mm.free_list);
4639 spin_unlock(&i915->mm.free_lock);
4640 }
4641 if (unlikely(freed)) {
4642 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004643 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004644 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004645}
4646
4647static void __i915_gem_free_work(struct work_struct *work)
4648{
4649 struct drm_i915_private *i915 =
4650 container_of(work, struct drm_i915_private, mm.free_work);
4651 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004652
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004653 /* All file-owned VMA should have been released by this point through
4654 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4655 * However, the object may also be bound into the global GTT (e.g.
4656 * older GPUs without per-process support, or for direct access through
4657 * the GTT either for the user or for scanout). Those VMA still need to
4658 * unbound now.
4659 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004660
Chris Wilsonf991c492017-11-06 11:15:08 +00004661 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004662 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004663 spin_unlock(&i915->mm.free_lock);
4664
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004665 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004666 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004667 return;
4668
4669 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004670 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004671 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004672}
4673
4674static void __i915_gem_free_object_rcu(struct rcu_head *head)
4675{
4676 struct drm_i915_gem_object *obj =
4677 container_of(head, typeof(*obj), rcu);
4678 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4679
4680 /* We can't simply use call_rcu() from i915_gem_free_object()
4681 * as we need to block whilst unbinding, and the call_rcu
4682 * task may be called from softirq context. So we take a
4683 * detour through a worker.
4684 */
4685 if (llist_add(&obj->freed, &i915->mm.free_list))
4686 schedule_work(&i915->mm.free_work);
4687}
4688
4689void i915_gem_free_object(struct drm_gem_object *gem_obj)
4690{
4691 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4692
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004693 if (obj->mm.quirked)
4694 __i915_gem_object_unpin_pages(obj);
4695
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004696 if (discard_backing_storage(obj))
4697 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004698
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004699 /* Before we free the object, make sure any pure RCU-only
4700 * read-side critical sections are complete, e.g.
4701 * i915_gem_busy_ioctl(). For the corresponding synchronized
4702 * lookup see i915_gem_object_lookup_rcu().
4703 */
4704 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004705}
4706
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004707void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4708{
4709 lockdep_assert_held(&obj->base.dev->struct_mutex);
4710
Chris Wilsond1b48c12017-08-16 09:52:08 +01004711 if (!i915_gem_object_has_active_reference(obj) &&
4712 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004713 i915_gem_object_set_active_reference(obj);
4714 else
4715 i915_gem_object_put(obj);
4716}
4717
Chris Wilsonae6c4572017-11-10 14:26:28 +00004718static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004719{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004720 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004721 struct intel_engine_cs *engine;
4722 enum intel_engine_id id;
4723
Chris Wilsonae6c4572017-11-10 14:26:28 +00004724 for_each_engine(engine, i915, id) {
4725 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4726 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4727 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004728}
4729
Chris Wilson24145512017-01-24 11:01:35 +00004730void i915_gem_sanitize(struct drm_i915_private *i915)
4731{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004732 if (i915_terminally_wedged(&i915->gpu_error)) {
4733 mutex_lock(&i915->drm.struct_mutex);
4734 i915_gem_unset_wedged(i915);
4735 mutex_unlock(&i915->drm.struct_mutex);
4736 }
4737
Chris Wilson24145512017-01-24 11:01:35 +00004738 /*
4739 * If we inherit context state from the BIOS or earlier occupants
4740 * of the GPU, the GPU may be in an inconsistent state when we
4741 * try to take over. The only way to remove the earlier state
4742 * is by resetting. However, resetting on earlier gen is tricky as
4743 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004744 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004745 */
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004746 if (INTEL_GEN(i915) >= 5) {
Chris Wilson24145512017-01-24 11:01:35 +00004747 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4748 WARN_ON(reset && reset != -ENODEV);
4749 }
4750}
4751
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004752int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004753{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004754 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004755 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004756
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004757 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004758 intel_suspend_gt_powersave(dev_priv);
4759
Chris Wilson45c5f202013-10-16 11:50:01 +01004760 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004761
4762 /* We have to flush all the executing contexts to main memory so
4763 * that they can saved in the hibernation image. To ensure the last
4764 * context image is coherent, we have to switch away from it. That
4765 * leaves the dev_priv->kernel_context still active when
4766 * we actually suspend, and its image in memory may not match the GPU
4767 * state. Fortunately, the kernel_context is disposable and we do
4768 * not rely on its state.
4769 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004770 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4771 ret = i915_gem_switch_to_kernel_context(dev_priv);
4772 if (ret)
4773 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004774
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004775 ret = i915_gem_wait_for_idle(dev_priv,
4776 I915_WAIT_INTERRUPTIBLE |
4777 I915_WAIT_LOCKED);
4778 if (ret && ret != -EIO)
4779 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004780
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004781 assert_kernel_context_is_current(dev_priv);
4782 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004783 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004784 mutex_unlock(&dev->struct_mutex);
4785
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304786 intel_guc_suspend(dev_priv);
4787
Chris Wilson737b1502015-01-26 18:03:03 +02004788 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004789 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004790
4791 /* As the idle_work is rearming if it detects a race, play safe and
4792 * repeat the flush until it is definitely idle.
4793 */
Chris Wilson7c262402017-10-06 11:40:38 +01004794 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004795
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004796 /* Assert that we sucessfully flushed all the work and
4797 * reset the GPU back to its idle, low power state.
4798 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004799 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004800 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4801 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004802
Imre Deak1c777c52016-10-12 17:46:37 +03004803 /*
4804 * Neither the BIOS, ourselves or any other kernel
4805 * expects the system to be in execlists mode on startup,
4806 * so we need to reset the GPU back to legacy mode. And the only
4807 * known way to disable logical contexts is through a GPU reset.
4808 *
4809 * So in order to leave the system in a known default configuration,
4810 * always reset the GPU upon unload and suspend. Afterwards we then
4811 * clean up the GEM state tracking, flushing off the requests and
4812 * leaving the system in a known idle state.
4813 *
4814 * Note that is of the upmost importance that the GPU is idle and
4815 * all stray writes are flushed *before* we dismantle the backing
4816 * storage for the pinned objects.
4817 *
4818 * However, since we are uncertain that resetting the GPU on older
4819 * machines is a good idea, we don't - just in case it leaves the
4820 * machine in an unusable condition.
4821 */
Chris Wilson24145512017-01-24 11:01:35 +00004822 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004823
4824 intel_runtime_pm_put(dev_priv);
4825 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004826
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004827err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004828 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004829 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004830 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004831}
4832
Chris Wilson37cd3302017-11-12 11:27:38 +00004833void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004834{
Chris Wilson37cd3302017-11-12 11:27:38 +00004835 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004836
Chris Wilson37cd3302017-11-12 11:27:38 +00004837 mutex_lock(&i915->drm.struct_mutex);
4838 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004839
Chris Wilson37cd3302017-11-12 11:27:38 +00004840 i915_gem_restore_gtt_mappings(i915);
4841 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004842
4843 /* As we didn't flush the kernel context before suspend, we cannot
4844 * guarantee that the context image is complete. So let's just reset
4845 * it and start again.
4846 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004847 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004848
Chris Wilson37cd3302017-11-12 11:27:38 +00004849 if (i915_gem_init_hw(i915))
4850 goto err_wedged;
4851
Chris Wilson7469c622017-11-14 13:03:00 +00004852 intel_guc_resume(i915);
4853
Chris Wilson37cd3302017-11-12 11:27:38 +00004854 /* Always reload a context for powersaving. */
4855 if (i915_gem_switch_to_kernel_context(i915))
4856 goto err_wedged;
4857
4858out_unlock:
4859 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4860 mutex_unlock(&i915->drm.struct_mutex);
4861 return;
4862
4863err_wedged:
4864 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
4865 i915_gem_set_wedged(i915);
4866 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004867}
4868
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004869void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004870{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004871 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004872 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4873 return;
4874
4875 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4876 DISP_TILE_SURFACE_SWIZZLING);
4877
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004878 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004879 return;
4880
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004881 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004882 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004883 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004884 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004885 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004886 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004887 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004888 else
4889 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004890}
Daniel Vettere21af882012-02-09 20:53:27 +01004891
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004892static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004893{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004894 I915_WRITE(RING_CTL(base), 0);
4895 I915_WRITE(RING_HEAD(base), 0);
4896 I915_WRITE(RING_TAIL(base), 0);
4897 I915_WRITE(RING_START(base), 0);
4898}
4899
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004900static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004901{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004902 if (IS_I830(dev_priv)) {
4903 init_unused_ring(dev_priv, PRB1_BASE);
4904 init_unused_ring(dev_priv, SRB0_BASE);
4905 init_unused_ring(dev_priv, SRB1_BASE);
4906 init_unused_ring(dev_priv, SRB2_BASE);
4907 init_unused_ring(dev_priv, SRB3_BASE);
4908 } else if (IS_GEN2(dev_priv)) {
4909 init_unused_ring(dev_priv, SRB0_BASE);
4910 init_unused_ring(dev_priv, SRB1_BASE);
4911 } else if (IS_GEN3(dev_priv)) {
4912 init_unused_ring(dev_priv, PRB1_BASE);
4913 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004914 }
4915}
4916
Chris Wilson20a8a742017-02-08 14:30:31 +00004917static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004918{
Chris Wilson20a8a742017-02-08 14:30:31 +00004919 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004920 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304921 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004922 int err;
4923
4924 for_each_engine(engine, i915, id) {
4925 err = engine->init_hw(engine);
4926 if (err)
4927 return err;
4928 }
4929
4930 return 0;
4931}
4932
4933int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4934{
Chris Wilsond200cda2016-04-28 09:56:44 +01004935 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004936
Chris Wilsonde867c22016-10-25 13:16:02 +01004937 dev_priv->gt.last_init_time = ktime_get();
4938
Chris Wilson5e4f5182015-02-13 14:35:59 +00004939 /* Double layer security blanket, see i915_gem_init() */
4940 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4941
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004942 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004943 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004944
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004945 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004946 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004947 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004948
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004949 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004950 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004951 u32 temp = I915_READ(GEN7_MSG_CTL);
4952 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4953 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004954 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004955 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4956 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4957 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4958 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004959 }
4960
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004961 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004962
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004963 /*
4964 * At least 830 can leave some of the unused rings
4965 * "active" (ie. head != tail) after resume which
4966 * will prevent c3 entry. Makes sure all unused rings
4967 * are totally idle.
4968 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004969 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004970
Dave Gordoned54c1a2016-01-19 19:02:54 +00004971 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01004972 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
4973 ret = -EIO;
4974 goto out;
4975 }
John Harrison90638cc2015-05-29 17:43:37 +01004976
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004977 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004978 if (ret) {
4979 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4980 goto out;
4981 }
4982
Michał Winiarski9bdc3572017-10-25 18:25:19 +01004983 /* We can't enable contexts until all firmware is loaded */
4984 ret = intel_uc_init_hw(dev_priv);
4985 if (ret)
4986 goto out;
4987
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004988 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004989
Chris Wilson136109c2017-11-02 13:14:30 +00004990 /* Only when the HW is re-initialised, can we replay the requests */
4991 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00004992out:
4993 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004994 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004995}
4996
Chris Wilsond2b4b972017-11-10 14:26:33 +00004997static int __intel_engines_record_defaults(struct drm_i915_private *i915)
4998{
4999 struct i915_gem_context *ctx;
5000 struct intel_engine_cs *engine;
5001 enum intel_engine_id id;
5002 int err;
5003
5004 /*
5005 * As we reset the gpu during very early sanitisation, the current
5006 * register state on the GPU should reflect its defaults values.
5007 * We load a context onto the hw (with restore-inhibit), then switch
5008 * over to a second context to save that default register state. We
5009 * can then prime every new context with that state so they all start
5010 * from the same default HW values.
5011 */
5012
5013 ctx = i915_gem_context_create_kernel(i915, 0);
5014 if (IS_ERR(ctx))
5015 return PTR_ERR(ctx);
5016
5017 for_each_engine(engine, i915, id) {
5018 struct drm_i915_gem_request *rq;
5019
5020 rq = i915_gem_request_alloc(engine, ctx);
5021 if (IS_ERR(rq)) {
5022 err = PTR_ERR(rq);
5023 goto out_ctx;
5024 }
5025
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005026 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005027 if (engine->init_context)
5028 err = engine->init_context(rq);
5029
5030 __i915_add_request(rq, true);
5031 if (err)
5032 goto err_active;
5033 }
5034
5035 err = i915_gem_switch_to_kernel_context(i915);
5036 if (err)
5037 goto err_active;
5038
5039 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5040 if (err)
5041 goto err_active;
5042
5043 assert_kernel_context_is_current(i915);
5044
5045 for_each_engine(engine, i915, id) {
5046 struct i915_vma *state;
5047
5048 state = ctx->engine[id].state;
5049 if (!state)
5050 continue;
5051
5052 /*
5053 * As we will hold a reference to the logical state, it will
5054 * not be torn down with the context, and importantly the
5055 * object will hold onto its vma (making it possible for a
5056 * stray GTT write to corrupt our defaults). Unmap the vma
5057 * from the GTT to prevent such accidents and reclaim the
5058 * space.
5059 */
5060 err = i915_vma_unbind(state);
5061 if (err)
5062 goto err_active;
5063
5064 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5065 if (err)
5066 goto err_active;
5067
5068 engine->default_state = i915_gem_object_get(state->obj);
5069 }
5070
5071 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5072 unsigned int found = intel_engines_has_context_isolation(i915);
5073
5074 /*
5075 * Make sure that classes with multiple engine instances all
5076 * share the same basic configuration.
5077 */
5078 for_each_engine(engine, i915, id) {
5079 unsigned int bit = BIT(engine->uabi_class);
5080 unsigned int expected = engine->default_state ? bit : 0;
5081
5082 if ((found & bit) != expected) {
5083 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5084 engine->uabi_class, engine->name);
5085 }
5086 }
5087 }
5088
5089out_ctx:
5090 i915_gem_context_set_closed(ctx);
5091 i915_gem_context_put(ctx);
5092 return err;
5093
5094err_active:
5095 /*
5096 * If we have to abandon now, we expect the engines to be idle
5097 * and ready to be torn-down. First try to flush any remaining
5098 * request, ensure we are pointing at the kernel context and
5099 * then remove it.
5100 */
5101 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5102 goto out_ctx;
5103
5104 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5105 goto out_ctx;
5106
5107 i915_gem_contexts_lost(i915);
5108 goto out_ctx;
5109}
5110
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005111int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005112{
Chris Wilson1070a422012-04-24 15:47:41 +01005113 int ret;
5114
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005115 /*
5116 * We need to fallback to 4K pages since gvt gtt handling doesn't
5117 * support huge page entries - we will need to check either hypervisor
5118 * mm can support huge guest page or just do emulation in gvt.
5119 */
5120 if (intel_vgpu_active(dev_priv))
5121 mkwrite_device_info(dev_priv)->page_sizes =
5122 I915_GTT_PAGE_SIZE_4K;
5123
Chris Wilson94312822017-05-03 10:39:18 +01005124 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005125
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005126 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005127 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005128 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005129 } else {
5130 dev_priv->gt.resume = intel_legacy_submission_resume;
5131 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005132 }
5133
Chris Wilsonee487002017-11-22 17:26:21 +00005134 ret = i915_gem_init_userptr(dev_priv);
5135 if (ret)
5136 return ret;
5137
Chris Wilson5e4f5182015-02-13 14:35:59 +00005138 /* This is just a security blanket to placate dragons.
5139 * On some systems, we very sporadically observe that the first TLBs
5140 * used by the CS may be stale, despite us poking the TLB reset. If
5141 * we hold the forcewake during initialisation these problems
5142 * just magically go away.
5143 */
Chris Wilsonee487002017-11-22 17:26:21 +00005144 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005145 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5146
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005147 ret = i915_gem_init_ggtt(dev_priv);
5148 if (ret)
5149 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08005150
Chris Wilson829a0af2017-06-20 12:05:45 +01005151 ret = i915_gem_contexts_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02005152 if (ret)
5153 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005154
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005155 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01005156 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02005157 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005158
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005159 intel_init_gt_powersave(dev_priv);
5160
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005161 ret = i915_gem_init_hw(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005162 if (ret)
5163 goto out_unlock;
5164
5165 /*
5166 * Despite its name intel_init_clock_gating applies both display
5167 * clock gating workarounds; GT mmio workarounds and the occasional
5168 * GT power context workaround. Worse, sometimes it includes a context
5169 * register workaround which we need to apply before we record the
5170 * default HW state for all contexts.
5171 *
5172 * FIXME: break up the workarounds and apply them at the right time!
5173 */
5174 intel_init_clock_gating(dev_priv);
5175
Chris Wilsond2b4b972017-11-10 14:26:33 +00005176 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005177out_unlock:
Chris Wilson60990322014-04-09 09:19:42 +01005178 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01005179 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005180 * wedged. But we only want to do this where the GPU is angry,
5181 * for all other failure, such as an allocation failure, bail.
5182 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005183 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5184 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5185 i915_gem_set_wedged(dev_priv);
5186 }
Chris Wilson60990322014-04-09 09:19:42 +01005187 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005188 }
Chris Wilson5e4f5182015-02-13 14:35:59 +00005189 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005190 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005191
Chris Wilson60990322014-04-09 09:19:42 +01005192 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005193}
5194
Chris Wilson24145512017-01-24 11:01:35 +00005195void i915_gem_init_mmio(struct drm_i915_private *i915)
5196{
5197 i915_gem_sanitize(i915);
5198}
5199
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005200void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005201i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005202{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005203 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305204 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005205
Akash Goel3b3f1652016-10-13 22:44:48 +05305206 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005207 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005208}
5209
Eric Anholt673a3942008-07-30 12:06:12 -07005210void
Imre Deak40ae4e12016-03-16 14:54:03 +02005211i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5212{
Chris Wilson49ef5292016-08-18 17:17:00 +01005213 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005214
5215 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
5216 !IS_CHERRYVIEW(dev_priv))
5217 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02005218 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
5219 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5220 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005221 dev_priv->num_fence_regs = 16;
5222 else
5223 dev_priv->num_fence_regs = 8;
5224
Chris Wilsonc0336662016-05-06 15:40:21 +01005225 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005226 dev_priv->num_fence_regs =
5227 I915_READ(vgtif_reg(avail_rs.fence_num));
5228
5229 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005230 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5231 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5232
5233 fence->i915 = dev_priv;
5234 fence->id = i;
5235 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5236 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005237 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005238
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005239 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005240}
5241
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005242static void i915_gem_init__mm(struct drm_i915_private *i915)
5243{
5244 spin_lock_init(&i915->mm.object_stat_lock);
5245 spin_lock_init(&i915->mm.obj_lock);
5246 spin_lock_init(&i915->mm.free_lock);
5247
5248 init_llist_head(&i915->mm.free_list);
5249
5250 INIT_LIST_HEAD(&i915->mm.unbound_list);
5251 INIT_LIST_HEAD(&i915->mm.bound_list);
5252 INIT_LIST_HEAD(&i915->mm.fence_list);
5253 INIT_LIST_HEAD(&i915->mm.userfault_list);
5254
5255 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5256}
5257
Chris Wilson73cb9702016-10-28 13:58:46 +01005258int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005259i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005260{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005261 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005262
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005263 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5264 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005265 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005266
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005267 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5268 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005269 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005270
Chris Wilsond1b48c12017-08-16 09:52:08 +01005271 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5272 if (!dev_priv->luts)
5273 goto err_vmas;
5274
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005275 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
5276 SLAB_HWCACHE_ALIGN |
5277 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005278 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005279 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005280 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005281
Chris Wilson52e54202016-11-14 20:41:02 +00005282 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5283 SLAB_HWCACHE_ALIGN |
5284 SLAB_RECLAIM_ACCOUNT);
5285 if (!dev_priv->dependencies)
5286 goto err_requests;
5287
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005288 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5289 if (!dev_priv->priorities)
5290 goto err_dependencies;
5291
Chris Wilson73cb9702016-10-28 13:58:46 +01005292 mutex_lock(&dev_priv->drm.struct_mutex);
5293 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005294 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005295 mutex_unlock(&dev_priv->drm.struct_mutex);
5296 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005297 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005298
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005299 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005300
Chris Wilson67d97da2016-07-04 08:08:31 +01005301 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005302 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005303 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005304 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005305 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005306 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005307
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005308 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5309
Chris Wilsonb5add952016-08-04 16:32:36 +01005310 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005311
Matthew Auld465c4032017-10-06 23:18:14 +01005312 err = i915_gemfs_init(dev_priv);
5313 if (err)
5314 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5315
Chris Wilson73cb9702016-10-28 13:58:46 +01005316 return 0;
5317
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005318err_priorities:
5319 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005320err_dependencies:
5321 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005322err_requests:
5323 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005324err_luts:
5325 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005326err_vmas:
5327 kmem_cache_destroy(dev_priv->vmas);
5328err_objects:
5329 kmem_cache_destroy(dev_priv->objects);
5330err_out:
5331 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005332}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005333
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005334void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005335{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005336 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005337 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005338 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005339
Matthew Auldea84aa72016-11-17 21:04:11 +00005340 mutex_lock(&dev_priv->drm.struct_mutex);
5341 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5342 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5343 mutex_unlock(&dev_priv->drm.struct_mutex);
5344
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005345 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005346 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005347 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005348 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005349 kmem_cache_destroy(dev_priv->vmas);
5350 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005351
5352 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5353 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005354
5355 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005356}
5357
Chris Wilson6a800ea2016-09-21 14:51:07 +01005358int i915_gem_freeze(struct drm_i915_private *dev_priv)
5359{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005360 /* Discard all purgeable objects, let userspace recover those as
5361 * required after resuming.
5362 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005363 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005364
Chris Wilson6a800ea2016-09-21 14:51:07 +01005365 return 0;
5366}
5367
Chris Wilson461fb992016-05-14 07:26:33 +01005368int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5369{
5370 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005371 struct list_head *phases[] = {
5372 &dev_priv->mm.unbound_list,
5373 &dev_priv->mm.bound_list,
5374 NULL
5375 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005376
5377 /* Called just before we write the hibernation image.
5378 *
5379 * We need to update the domain tracking to reflect that the CPU
5380 * will be accessing all the pages to create and restore from the
5381 * hibernation, and so upon restoration those pages will be in the
5382 * CPU domain.
5383 *
5384 * To make sure the hibernation image contains the latest state,
5385 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005386 *
5387 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005388 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005389 */
5390
Chris Wilson912d5722017-09-06 16:19:30 -07005391 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005392 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005393
Chris Wilsonf2123812017-10-16 12:40:37 +01005394 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005395 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005396 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005397 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005398 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005399 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005400
5401 return 0;
5402}
5403
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005404void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005405{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005406 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01005407 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005408
5409 /* Clean up our request list when the client is going away, so that
5410 * later retire_requests won't dereference our soon-to-be-gone
5411 * file_priv.
5412 */
Chris Wilson1c255952010-09-26 11:03:27 +01005413 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005414 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005415 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005416 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005417}
5418
Chris Wilson829a0af2017-06-20 12:05:45 +01005419int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005420{
5421 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005422 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005423
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005424 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005425
5426 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5427 if (!file_priv)
5428 return -ENOMEM;
5429
5430 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005431 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005432 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005433
5434 spin_lock_init(&file_priv->mm.lock);
5435 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005436
Chris Wilsonc80ff162016-07-27 09:07:27 +01005437 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005438
Chris Wilson829a0af2017-06-20 12:05:45 +01005439 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005440 if (ret)
5441 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005442
Ben Widawskye422b882013-12-06 14:10:58 -08005443 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005444}
5445
Daniel Vetterb680c372014-09-19 18:27:27 +02005446/**
5447 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005448 * @old: current GEM buffer for the frontbuffer slots
5449 * @new: new GEM buffer for the frontbuffer slots
5450 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005451 *
5452 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5453 * from @old and setting them in @new. Both @old and @new can be NULL.
5454 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005455void i915_gem_track_fb(struct drm_i915_gem_object *old,
5456 struct drm_i915_gem_object *new,
5457 unsigned frontbuffer_bits)
5458{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005459 /* Control of individual bits within the mask are guarded by
5460 * the owning plane->mutex, i.e. we can never see concurrent
5461 * manipulation of individual bits. But since the bitfield as a whole
5462 * is updated using RMW, we need to use atomics in order to update
5463 * the bits.
5464 */
5465 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5466 sizeof(atomic_t) * BITS_PER_BYTE);
5467
Daniel Vettera071fa02014-06-18 23:28:09 +02005468 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005469 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5470 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005471 }
5472
5473 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005474 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5475 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005476 }
5477}
5478
Dave Gordonea702992015-07-09 19:29:02 +01005479/* Allocate a new GEM object and fill it with the supplied data */
5480struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005481i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005482 const void *data, size_t size)
5483{
5484 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005485 struct file *file;
5486 size_t offset;
5487 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005488
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005489 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005490 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005491 return obj;
5492
Chris Wilsonce8ff092017-03-17 19:46:47 +00005493 GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005494
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005495 file = obj->base.filp;
5496 offset = 0;
5497 do {
5498 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5499 struct page *page;
5500 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005501
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005502 err = pagecache_write_begin(file, file->f_mapping,
5503 offset, len, 0,
5504 &page, &pgdata);
5505 if (err < 0)
5506 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005507
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005508 vaddr = kmap(page);
5509 memcpy(vaddr, data, len);
5510 kunmap(page);
5511
5512 err = pagecache_write_end(file, file->f_mapping,
5513 offset, len, len,
5514 page, pgdata);
5515 if (err < 0)
5516 goto fail;
5517
5518 size -= len;
5519 data += len;
5520 offset += len;
5521 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005522
5523 return obj;
5524
5525fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005526 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005527 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005528}
Chris Wilson96d77632016-10-28 13:58:33 +01005529
5530struct scatterlist *
5531i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5532 unsigned int n,
5533 unsigned int *offset)
5534{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005535 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005536 struct scatterlist *sg;
5537 unsigned int idx, count;
5538
5539 might_sleep();
5540 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005541 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005542
5543 /* As we iterate forward through the sg, we record each entry in a
5544 * radixtree for quick repeated (backwards) lookups. If we have seen
5545 * this index previously, we will have an entry for it.
5546 *
5547 * Initial lookup is O(N), but this is amortized to O(1) for
5548 * sequential page access (where each new request is consecutive
5549 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5550 * i.e. O(1) with a large constant!
5551 */
5552 if (n < READ_ONCE(iter->sg_idx))
5553 goto lookup;
5554
5555 mutex_lock(&iter->lock);
5556
5557 /* We prefer to reuse the last sg so that repeated lookup of this
5558 * (or the subsequent) sg are fast - comparing against the last
5559 * sg is faster than going through the radixtree.
5560 */
5561
5562 sg = iter->sg_pos;
5563 idx = iter->sg_idx;
5564 count = __sg_page_count(sg);
5565
5566 while (idx + count <= n) {
5567 unsigned long exception, i;
5568 int ret;
5569
5570 /* If we cannot allocate and insert this entry, or the
5571 * individual pages from this range, cancel updating the
5572 * sg_idx so that on this lookup we are forced to linearly
5573 * scan onwards, but on future lookups we will try the
5574 * insertion again (in which case we need to be careful of
5575 * the error return reporting that we have already inserted
5576 * this index).
5577 */
5578 ret = radix_tree_insert(&iter->radix, idx, sg);
5579 if (ret && ret != -EEXIST)
5580 goto scan;
5581
5582 exception =
5583 RADIX_TREE_EXCEPTIONAL_ENTRY |
5584 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5585 for (i = 1; i < count; i++) {
5586 ret = radix_tree_insert(&iter->radix, idx + i,
5587 (void *)exception);
5588 if (ret && ret != -EEXIST)
5589 goto scan;
5590 }
5591
5592 idx += count;
5593 sg = ____sg_next(sg);
5594 count = __sg_page_count(sg);
5595 }
5596
5597scan:
5598 iter->sg_pos = sg;
5599 iter->sg_idx = idx;
5600
5601 mutex_unlock(&iter->lock);
5602
5603 if (unlikely(n < idx)) /* insertion completed by another thread */
5604 goto lookup;
5605
5606 /* In case we failed to insert the entry into the radixtree, we need
5607 * to look beyond the current sg.
5608 */
5609 while (idx + count <= n) {
5610 idx += count;
5611 sg = ____sg_next(sg);
5612 count = __sg_page_count(sg);
5613 }
5614
5615 *offset = n - idx;
5616 return sg;
5617
5618lookup:
5619 rcu_read_lock();
5620
5621 sg = radix_tree_lookup(&iter->radix, n);
5622 GEM_BUG_ON(!sg);
5623
5624 /* If this index is in the middle of multi-page sg entry,
5625 * the radixtree will contain an exceptional entry that points
5626 * to the start of that range. We will return the pointer to
5627 * the base page and the offset of this page within the
5628 * sg entry's range.
5629 */
5630 *offset = 0;
5631 if (unlikely(radix_tree_exception(sg))) {
5632 unsigned long base =
5633 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5634
5635 sg = radix_tree_lookup(&iter->radix, base);
5636 GEM_BUG_ON(!sg);
5637
5638 *offset = n - base;
5639 }
5640
5641 rcu_read_unlock();
5642
5643 return sg;
5644}
5645
5646struct page *
5647i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5648{
5649 struct scatterlist *sg;
5650 unsigned int offset;
5651
5652 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5653
5654 sg = i915_gem_object_get_sg(obj, n, &offset);
5655 return nth_page(sg_page(sg), offset);
5656}
5657
5658/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5659struct page *
5660i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5661 unsigned int n)
5662{
5663 struct page *page;
5664
5665 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005666 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005667 set_page_dirty(page);
5668
5669 return page;
5670}
5671
5672dma_addr_t
5673i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5674 unsigned long n)
5675{
5676 struct scatterlist *sg;
5677 unsigned int offset;
5678
5679 sg = i915_gem_object_get_sg(obj, n, &offset);
5680 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5681}
Chris Wilson935a2f72017-02-13 17:15:13 +00005682
Chris Wilson8eeb7902017-07-26 19:16:01 +01005683int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5684{
5685 struct sg_table *pages;
5686 int err;
5687
5688 if (align > obj->base.size)
5689 return -EINVAL;
5690
5691 if (obj->ops == &i915_gem_phys_ops)
5692 return 0;
5693
5694 if (obj->ops != &i915_gem_object_ops)
5695 return -EINVAL;
5696
5697 err = i915_gem_object_unbind(obj);
5698 if (err)
5699 return err;
5700
5701 mutex_lock(&obj->mm.lock);
5702
5703 if (obj->mm.madv != I915_MADV_WILLNEED) {
5704 err = -EFAULT;
5705 goto err_unlock;
5706 }
5707
5708 if (obj->mm.quirked) {
5709 err = -EFAULT;
5710 goto err_unlock;
5711 }
5712
5713 if (obj->mm.mapping) {
5714 err = -EBUSY;
5715 goto err_unlock;
5716 }
5717
Chris Wilsonf2123812017-10-16 12:40:37 +01005718 pages = fetch_and_zero(&obj->mm.pages);
5719 if (pages) {
5720 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5721
5722 __i915_gem_object_reset_page_iter(obj);
5723
5724 spin_lock(&i915->mm.obj_lock);
5725 list_del(&obj->mm.link);
5726 spin_unlock(&i915->mm.obj_lock);
5727 }
5728
Chris Wilson8eeb7902017-07-26 19:16:01 +01005729 obj->ops = &i915_gem_phys_ops;
5730
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005731 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005732 if (err)
5733 goto err_xfer;
5734
5735 /* Perma-pin (until release) the physical set of pages */
5736 __i915_gem_object_pin_pages(obj);
5737
5738 if (!IS_ERR_OR_NULL(pages))
5739 i915_gem_object_ops.put_pages(obj, pages);
5740 mutex_unlock(&obj->mm.lock);
5741 return 0;
5742
5743err_xfer:
5744 obj->ops = &i915_gem_object_ops;
5745 obj->mm.pages = pages;
5746err_unlock:
5747 mutex_unlock(&obj->mm.lock);
5748 return err;
5749}
5750
Chris Wilson935a2f72017-02-13 17:15:13 +00005751#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5752#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005753#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005754#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005755#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005756#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005757#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005758#endif