blob: 67dc11effc8e55c31fcc80960582cf6da85aacca [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
Chris Wilson71253972017-12-06 12:49:14 +0000669void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100670{
Chris Wilson71253972017-12-06 12:49:14 +0000671 /*
672 * No actual flushing is required for the GTT write domain for reads
673 * from the GTT domain. Writes to it "immediately" go to main memory
674 * as far as we know, so there's no chipset flush. It also doesn't
675 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100676 *
677 * However, we do have to enforce the order so that all writes through
678 * the GTT land before any writes to the device, such as updates to
679 * the GATT itself.
680 *
681 * We also have to wait a bit for the writes to land from the GTT.
682 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
683 * timing. This issue has only been observed when switching quickly
684 * between GTT writes and CPU reads from inside the kernel on recent hw,
685 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson71253972017-12-06 12:49:14 +0000686 * system agents we cannot reproduce this behaviour, until Cannonlake
687 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100688 */
Chris Wilson71253972017-12-06 12:49:14 +0000689
Chris Wilsonef749212017-04-12 12:01:10 +0100690 wmb();
691
Chris Wilson71253972017-12-06 12:49:14 +0000692 intel_runtime_pm_get(dev_priv);
693 spin_lock_irq(&dev_priv->uncore.lock);
694
695 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
696
697 spin_unlock_irq(&dev_priv->uncore.lock);
698 intel_runtime_pm_put(dev_priv);
699}
700
701static void
702flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
703{
704 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
705 struct i915_vma *vma;
706
707 if (!(obj->base.write_domain & flush_domains))
708 return;
709
Chris Wilsonef749212017-04-12 12:01:10 +0100710 switch (obj->base.write_domain) {
711 case I915_GEM_DOMAIN_GTT:
Chris Wilson71253972017-12-06 12:49:14 +0000712 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100713
714 intel_fb_obj_flush(obj,
715 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson71253972017-12-06 12:49:14 +0000716
717 list_for_each_entry(vma, &obj->vma_list, obj_link) {
718 if (!i915_vma_is_ggtt(vma))
719 break;
720
721 if (vma->iomap)
722 continue;
723
724 i915_vma_unset_ggtt_write(vma);
725 }
Chris Wilsonef749212017-04-12 12:01:10 +0100726 break;
727
728 case I915_GEM_DOMAIN_CPU:
729 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
730 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100731
732 case I915_GEM_DOMAIN_RENDER:
733 if (gpu_write_needs_clflush(obj))
734 obj->cache_dirty = true;
735 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100736 }
737
738 obj->base.write_domain = 0;
739}
740
Daniel Vetter8c599672011-12-14 13:57:31 +0100741static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100742__copy_to_user_swizzled(char __user *cpu_vaddr,
743 const char *gpu_vaddr, int gpu_offset,
744 int length)
745{
746 int ret, cpu_offset = 0;
747
748 while (length > 0) {
749 int cacheline_end = ALIGN(gpu_offset + 1, 64);
750 int this_length = min(cacheline_end - gpu_offset, length);
751 int swizzled_gpu_offset = gpu_offset ^ 64;
752
753 ret = __copy_to_user(cpu_vaddr + cpu_offset,
754 gpu_vaddr + swizzled_gpu_offset,
755 this_length);
756 if (ret)
757 return ret + length;
758
759 cpu_offset += this_length;
760 gpu_offset += this_length;
761 length -= this_length;
762 }
763
764 return 0;
765}
766
767static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700768__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
769 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100770 int length)
771{
772 int ret, cpu_offset = 0;
773
774 while (length > 0) {
775 int cacheline_end = ALIGN(gpu_offset + 1, 64);
776 int this_length = min(cacheline_end - gpu_offset, length);
777 int swizzled_gpu_offset = gpu_offset ^ 64;
778
779 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
780 cpu_vaddr + cpu_offset,
781 this_length);
782 if (ret)
783 return ret + length;
784
785 cpu_offset += this_length;
786 gpu_offset += this_length;
787 length -= this_length;
788 }
789
790 return 0;
791}
792
Brad Volkin4c914c02014-02-18 10:15:45 -0800793/*
794 * Pins the specified object's pages and synchronizes the object with
795 * GPU accesses. Sets needs_clflush to non-zero if the caller should
796 * flush the object from the CPU cache.
797 */
798int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100799 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800800{
801 int ret;
802
Chris Wilsone95433c2016-10-28 13:58:27 +0100803 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800804
Chris Wilsone95433c2016-10-28 13:58:27 +0100805 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100806 if (!i915_gem_object_has_struct_page(obj))
807 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800808
Chris Wilsone95433c2016-10-28 13:58:27 +0100809 ret = i915_gem_object_wait(obj,
810 I915_WAIT_INTERRUPTIBLE |
811 I915_WAIT_LOCKED,
812 MAX_SCHEDULE_TIMEOUT,
813 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100814 if (ret)
815 return ret;
816
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100817 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100818 if (ret)
819 return ret;
820
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100821 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
822 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000823 ret = i915_gem_object_set_to_cpu_domain(obj, false);
824 if (ret)
825 goto err_unpin;
826 else
827 goto out;
828 }
829
Chris Wilsonef749212017-04-12 12:01:10 +0100830 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100831
Chris Wilson43394c72016-08-18 17:16:47 +0100832 /* If we're not in the cpu read domain, set ourself into the gtt
833 * read domain and manually flush cachelines (if required). This
834 * optimizes for the case when the gpu will dirty the data
835 * anyway again before the next pread happens.
836 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100837 if (!obj->cache_dirty &&
838 !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000839 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800840
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000841out:
Chris Wilson97649512016-08-18 17:16:50 +0100842 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100843 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100844
845err_unpin:
846 i915_gem_object_unpin_pages(obj);
847 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100848}
849
850int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
851 unsigned int *needs_clflush)
852{
853 int ret;
854
Chris Wilsone95433c2016-10-28 13:58:27 +0100855 lockdep_assert_held(&obj->base.dev->struct_mutex);
856
Chris Wilson43394c72016-08-18 17:16:47 +0100857 *needs_clflush = 0;
858 if (!i915_gem_object_has_struct_page(obj))
859 return -ENODEV;
860
Chris Wilsone95433c2016-10-28 13:58:27 +0100861 ret = i915_gem_object_wait(obj,
862 I915_WAIT_INTERRUPTIBLE |
863 I915_WAIT_LOCKED |
864 I915_WAIT_ALL,
865 MAX_SCHEDULE_TIMEOUT,
866 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100867 if (ret)
868 return ret;
869
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100870 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100871 if (ret)
872 return ret;
873
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100874 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
875 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000876 ret = i915_gem_object_set_to_cpu_domain(obj, true);
877 if (ret)
878 goto err_unpin;
879 else
880 goto out;
881 }
882
Chris Wilsonef749212017-04-12 12:01:10 +0100883 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100884
Chris Wilson43394c72016-08-18 17:16:47 +0100885 /* If we're not in the cpu write domain, set ourself into the
886 * gtt write domain and manually flush cachelines (as required).
887 * This optimizes for the case when the gpu will use the data
888 * right away and we therefore have to clflush anyway.
889 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100890 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000891 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100892
Chris Wilsone27ab732017-06-15 13:38:49 +0100893 /*
894 * Same trick applies to invalidate partially written
895 * cachelines read before writing.
896 */
897 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
898 *needs_clflush |= CLFLUSH_BEFORE;
899 }
Chris Wilson43394c72016-08-18 17:16:47 +0100900
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000901out:
Chris Wilson43394c72016-08-18 17:16:47 +0100902 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100903 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100904 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100905 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100906
907err_unpin:
908 i915_gem_object_unpin_pages(obj);
909 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800910}
911
Daniel Vetter23c18c72012-03-25 19:47:42 +0200912static void
913shmem_clflush_swizzled_range(char *addr, unsigned long length,
914 bool swizzled)
915{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200916 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200917 unsigned long start = (unsigned long) addr;
918 unsigned long end = (unsigned long) addr + length;
919
920 /* For swizzling simply ensure that we always flush both
921 * channels. Lame, but simple and it works. Swizzled
922 * pwrite/pread is far from a hotpath - current userspace
923 * doesn't use it at all. */
924 start = round_down(start, 128);
925 end = round_up(end, 128);
926
927 drm_clflush_virt_range((void *)start, end - start);
928 } else {
929 drm_clflush_virt_range(addr, length);
930 }
931
932}
933
Daniel Vetterd174bd62012-03-25 19:47:40 +0200934/* Only difference to the fast-path function is that this can handle bit17
935 * and uses non-atomic copy and kmap functions. */
936static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100937shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200938 char __user *user_data,
939 bool page_do_bit17_swizzling, bool needs_clflush)
940{
941 char *vaddr;
942 int ret;
943
944 vaddr = kmap(page);
945 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100946 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200947 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200948
949 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100950 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200951 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100952 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200953 kunmap(page);
954
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100955 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200956}
957
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100958static int
959shmem_pread(struct page *page, int offset, int length, char __user *user_data,
960 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530961{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100962 int ret;
963
964 ret = -ENODEV;
965 if (!page_do_bit17_swizzling) {
966 char *vaddr = kmap_atomic(page);
967
968 if (needs_clflush)
969 drm_clflush_virt_range(vaddr + offset, length);
970 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
971 kunmap_atomic(vaddr);
972 }
973 if (ret == 0)
974 return 0;
975
976 return shmem_pread_slow(page, offset, length, user_data,
977 page_do_bit17_swizzling, needs_clflush);
978}
979
980static int
981i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
982 struct drm_i915_gem_pread *args)
983{
984 char __user *user_data;
985 u64 remain;
986 unsigned int obj_do_bit17_swizzling;
987 unsigned int needs_clflush;
988 unsigned int idx, offset;
989 int ret;
990
991 obj_do_bit17_swizzling = 0;
992 if (i915_gem_object_needs_bit17_swizzle(obj))
993 obj_do_bit17_swizzling = BIT(17);
994
995 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
996 if (ret)
997 return ret;
998
999 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1000 mutex_unlock(&obj->base.dev->struct_mutex);
1001 if (ret)
1002 return ret;
1003
1004 remain = args->size;
1005 user_data = u64_to_user_ptr(args->data_ptr);
1006 offset = offset_in_page(args->offset);
1007 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1008 struct page *page = i915_gem_object_get_page(obj, idx);
1009 int length;
1010
1011 length = remain;
1012 if (offset + length > PAGE_SIZE)
1013 length = PAGE_SIZE - offset;
1014
1015 ret = shmem_pread(page, offset, length, user_data,
1016 page_to_phys(page) & obj_do_bit17_swizzling,
1017 needs_clflush);
1018 if (ret)
1019 break;
1020
1021 remain -= length;
1022 user_data += length;
1023 offset = 0;
1024 }
1025
1026 i915_gem_obj_finish_shmem_access(obj);
1027 return ret;
1028}
1029
1030static inline bool
1031gtt_user_read(struct io_mapping *mapping,
1032 loff_t base, int offset,
1033 char __user *user_data, int length)
1034{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001035 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001036 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301037
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301038 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001039 vaddr = io_mapping_map_atomic_wc(mapping, base);
1040 unwritten = __copy_to_user_inatomic(user_data,
1041 (void __force *)vaddr + offset,
1042 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001043 io_mapping_unmap_atomic(vaddr);
1044 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001045 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1046 unwritten = copy_to_user(user_data,
1047 (void __force *)vaddr + offset,
1048 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001049 io_mapping_unmap(vaddr);
1050 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301051 return unwritten;
1052}
1053
1054static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001055i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1056 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301057{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001058 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1059 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301060 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001061 struct i915_vma *vma;
1062 void __user *user_data;
1063 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301064 int ret;
1065
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001066 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1067 if (ret)
1068 return ret;
1069
1070 intel_runtime_pm_get(i915);
1071 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001072 PIN_MAPPABLE |
1073 PIN_NONFAULT |
1074 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001075 if (!IS_ERR(vma)) {
1076 node.start = i915_ggtt_offset(vma);
1077 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001078 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001079 if (ret) {
1080 i915_vma_unpin(vma);
1081 vma = ERR_PTR(ret);
1082 }
1083 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001084 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001085 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301086 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001087 goto out_unlock;
1088 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301089 }
1090
1091 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1092 if (ret)
1093 goto out_unpin;
1094
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001095 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301096
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001097 user_data = u64_to_user_ptr(args->data_ptr);
1098 remain = args->size;
1099 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301100
1101 while (remain > 0) {
1102 /* Operation in this page
1103 *
1104 * page_base = page offset within aperture
1105 * page_offset = offset within page
1106 * page_length = bytes to copy for this page
1107 */
1108 u32 page_base = node.start;
1109 unsigned page_offset = offset_in_page(offset);
1110 unsigned page_length = PAGE_SIZE - page_offset;
1111 page_length = remain < page_length ? remain : page_length;
1112 if (node.allocated) {
1113 wmb();
1114 ggtt->base.insert_page(&ggtt->base,
1115 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001116 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301117 wmb();
1118 } else {
1119 page_base += offset & PAGE_MASK;
1120 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001121
1122 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1123 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301124 ret = -EFAULT;
1125 break;
1126 }
1127
1128 remain -= page_length;
1129 user_data += page_length;
1130 offset += page_length;
1131 }
1132
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001133 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301134out_unpin:
1135 if (node.allocated) {
1136 wmb();
1137 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001138 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301139 remove_mappable_node(&node);
1140 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001141 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301142 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001143out_unlock:
1144 intel_runtime_pm_put(i915);
1145 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001146
Eric Anholteb014592009-03-10 11:44:52 -07001147 return ret;
1148}
1149
Eric Anholt673a3942008-07-30 12:06:12 -07001150/**
1151 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001152 * @dev: drm device pointer
1153 * @data: ioctl data blob
1154 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001155 *
1156 * On error, the contents of *data are undefined.
1157 */
1158int
1159i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001160 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001161{
1162 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001163 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001164 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001165
Chris Wilson51311d02010-11-17 09:10:42 +00001166 if (args->size == 0)
1167 return 0;
1168
1169 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001170 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001171 args->size))
1172 return -EFAULT;
1173
Chris Wilson03ac0642016-07-20 13:31:51 +01001174 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001175 if (!obj)
1176 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001177
Chris Wilson7dcd2492010-09-26 20:21:44 +01001178 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001179 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001180 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001181 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001182 }
1183
Chris Wilsondb53a302011-02-03 11:57:46 +00001184 trace_i915_gem_object_pread(obj, args->offset, args->size);
1185
Chris Wilsone95433c2016-10-28 13:58:27 +01001186 ret = i915_gem_object_wait(obj,
1187 I915_WAIT_INTERRUPTIBLE,
1188 MAX_SCHEDULE_TIMEOUT,
1189 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001190 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001191 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001192
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001193 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001194 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001195 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001196
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001197 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001198 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001199 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301200
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001201 i915_gem_object_unpin_pages(obj);
1202out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001203 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001204 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001205}
1206
Keith Packard0839ccb2008-10-30 19:38:48 -07001207/* This is the fast write path which cannot handle
1208 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001209 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001210
Chris Wilsonfe115622016-10-28 13:58:40 +01001211static inline bool
1212ggtt_write(struct io_mapping *mapping,
1213 loff_t base, int offset,
1214 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001215{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001216 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001217 unsigned long unwritten;
1218
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001219 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001220 vaddr = io_mapping_map_atomic_wc(mapping, base);
1221 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001222 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001223 io_mapping_unmap_atomic(vaddr);
1224 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001225 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1226 unwritten = copy_from_user((void __force *)vaddr + offset,
1227 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001228 io_mapping_unmap(vaddr);
1229 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001230
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001231 return unwritten;
1232}
1233
Eric Anholt3de09aa2009-03-09 09:42:23 -07001234/**
1235 * This is the fast pwrite path, where we copy the data directly from the
1236 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001237 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001238 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001239 */
Eric Anholt673a3942008-07-30 12:06:12 -07001240static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001241i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1242 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001243{
Chris Wilsonfe115622016-10-28 13:58:40 +01001244 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301245 struct i915_ggtt *ggtt = &i915->ggtt;
1246 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001247 struct i915_vma *vma;
1248 u64 remain, offset;
1249 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301250 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301251
Chris Wilsonfe115622016-10-28 13:58:40 +01001252 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1253 if (ret)
1254 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001255
Chris Wilson8bd81812017-10-19 07:37:33 +01001256 if (i915_gem_object_has_struct_page(obj)) {
1257 /*
1258 * Avoid waking the device up if we can fallback, as
1259 * waking/resuming is very slow (worst-case 10-100 ms
1260 * depending on PCI sleeps and our own resume time).
1261 * This easily dwarfs any performance advantage from
1262 * using the cache bypass of indirect GGTT access.
1263 */
1264 if (!intel_runtime_pm_get_if_in_use(i915)) {
1265 ret = -EFAULT;
1266 goto out_unlock;
1267 }
1268 } else {
1269 /* No backing pages, no fallback, we must force GGTT access */
1270 intel_runtime_pm_get(i915);
1271 }
1272
Chris Wilson058d88c2016-08-15 10:49:06 +01001273 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001274 PIN_MAPPABLE |
1275 PIN_NONFAULT |
1276 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001277 if (!IS_ERR(vma)) {
1278 node.start = i915_ggtt_offset(vma);
1279 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001280 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001281 if (ret) {
1282 i915_vma_unpin(vma);
1283 vma = ERR_PTR(ret);
1284 }
1285 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001286 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001287 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301288 if (ret)
Chris Wilson8bd81812017-10-19 07:37:33 +01001289 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001290 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301291 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001292
1293 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1294 if (ret)
1295 goto out_unpin;
1296
Chris Wilsonfe115622016-10-28 13:58:40 +01001297 mutex_unlock(&i915->drm.struct_mutex);
1298
Chris Wilsonb19482d2016-08-18 17:16:43 +01001299 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001300
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301301 user_data = u64_to_user_ptr(args->data_ptr);
1302 offset = args->offset;
1303 remain = args->size;
1304 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001305 /* Operation in this page
1306 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001307 * page_base = page offset within aperture
1308 * page_offset = offset within page
1309 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001310 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301311 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001312 unsigned int page_offset = offset_in_page(offset);
1313 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301314 page_length = remain < page_length ? remain : page_length;
1315 if (node.allocated) {
1316 wmb(); /* flush the write before we modify the GGTT */
1317 ggtt->base.insert_page(&ggtt->base,
1318 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1319 node.start, I915_CACHE_NONE, 0);
1320 wmb(); /* flush modifications to the GGTT (insert_page) */
1321 } else {
1322 page_base += offset & PAGE_MASK;
1323 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001324 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001325 * source page isn't available. Return the error and we'll
1326 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301327 * If the object is non-shmem backed, we retry again with the
1328 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001329 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001330 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1331 user_data, page_length)) {
1332 ret = -EFAULT;
1333 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001334 }
Eric Anholt673a3942008-07-30 12:06:12 -07001335
Keith Packard0839ccb2008-10-30 19:38:48 -07001336 remain -= page_length;
1337 user_data += page_length;
1338 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001339 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001340 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001341
1342 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001343out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301344 if (node.allocated) {
1345 wmb();
1346 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001347 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301348 remove_mappable_node(&node);
1349 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001350 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301351 }
Chris Wilson8bd81812017-10-19 07:37:33 +01001352out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001353 intel_runtime_pm_put(i915);
Chris Wilson8bd81812017-10-19 07:37:33 +01001354out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001355 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001356 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001357}
1358
Eric Anholt673a3942008-07-30 12:06:12 -07001359static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001360shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001361 char __user *user_data,
1362 bool page_do_bit17_swizzling,
1363 bool needs_clflush_before,
1364 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001365{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001366 char *vaddr;
1367 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001368
Daniel Vetterd174bd62012-03-25 19:47:40 +02001369 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001370 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001371 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001372 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001373 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001374 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1375 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001376 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001377 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001378 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001379 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001380 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001381 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001382
Chris Wilson755d2212012-09-04 21:02:55 +01001383 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001384}
1385
Chris Wilsonfe115622016-10-28 13:58:40 +01001386/* Per-page copy function for the shmem pwrite fastpath.
1387 * Flushes invalid cachelines before writing to the target if
1388 * needs_clflush_before is set and flushes out any written cachelines after
1389 * writing if needs_clflush is set.
1390 */
Eric Anholt40123c12009-03-09 13:42:30 -07001391static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001392shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1393 bool page_do_bit17_swizzling,
1394 bool needs_clflush_before,
1395 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001396{
Chris Wilsonfe115622016-10-28 13:58:40 +01001397 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001398
Chris Wilsonfe115622016-10-28 13:58:40 +01001399 ret = -ENODEV;
1400 if (!page_do_bit17_swizzling) {
1401 char *vaddr = kmap_atomic(page);
1402
1403 if (needs_clflush_before)
1404 drm_clflush_virt_range(vaddr + offset, len);
1405 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1406 if (needs_clflush_after)
1407 drm_clflush_virt_range(vaddr + offset, len);
1408
1409 kunmap_atomic(vaddr);
1410 }
1411 if (ret == 0)
1412 return ret;
1413
1414 return shmem_pwrite_slow(page, offset, len, user_data,
1415 page_do_bit17_swizzling,
1416 needs_clflush_before,
1417 needs_clflush_after);
1418}
1419
1420static int
1421i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1422 const struct drm_i915_gem_pwrite *args)
1423{
1424 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1425 void __user *user_data;
1426 u64 remain;
1427 unsigned int obj_do_bit17_swizzling;
1428 unsigned int partial_cacheline_write;
1429 unsigned int needs_clflush;
1430 unsigned int offset, idx;
1431 int ret;
1432
1433 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001434 if (ret)
1435 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001436
Chris Wilsonfe115622016-10-28 13:58:40 +01001437 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1438 mutex_unlock(&i915->drm.struct_mutex);
1439 if (ret)
1440 return ret;
1441
1442 obj_do_bit17_swizzling = 0;
1443 if (i915_gem_object_needs_bit17_swizzle(obj))
1444 obj_do_bit17_swizzling = BIT(17);
1445
1446 /* If we don't overwrite a cacheline completely we need to be
1447 * careful to have up-to-date data by first clflushing. Don't
1448 * overcomplicate things and flush the entire patch.
1449 */
1450 partial_cacheline_write = 0;
1451 if (needs_clflush & CLFLUSH_BEFORE)
1452 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1453
Chris Wilson43394c72016-08-18 17:16:47 +01001454 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001455 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001456 offset = offset_in_page(args->offset);
1457 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1458 struct page *page = i915_gem_object_get_page(obj, idx);
1459 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001460
Chris Wilsonfe115622016-10-28 13:58:40 +01001461 length = remain;
1462 if (offset + length > PAGE_SIZE)
1463 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001464
Chris Wilsonfe115622016-10-28 13:58:40 +01001465 ret = shmem_pwrite(page, offset, length, user_data,
1466 page_to_phys(page) & obj_do_bit17_swizzling,
1467 (offset | length) & partial_cacheline_write,
1468 needs_clflush & CLFLUSH_AFTER);
1469 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001470 break;
1471
Chris Wilsonfe115622016-10-28 13:58:40 +01001472 remain -= length;
1473 user_data += length;
1474 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001475 }
1476
Chris Wilsond59b21e2017-02-22 11:40:49 +00001477 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001478 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001479 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001480}
1481
1482/**
1483 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001484 * @dev: drm device
1485 * @data: ioctl data blob
1486 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001487 *
1488 * On error, the contents of the buffer that were to be modified are undefined.
1489 */
1490int
1491i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001492 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001493{
1494 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001495 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001496 int ret;
1497
1498 if (args->size == 0)
1499 return 0;
1500
1501 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001502 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001503 args->size))
1504 return -EFAULT;
1505
Chris Wilson03ac0642016-07-20 13:31:51 +01001506 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001507 if (!obj)
1508 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001509
Chris Wilson7dcd2492010-09-26 20:21:44 +01001510 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001511 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001512 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001513 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001514 }
1515
Chris Wilsondb53a302011-02-03 11:57:46 +00001516 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1517
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001518 ret = -ENODEV;
1519 if (obj->ops->pwrite)
1520 ret = obj->ops->pwrite(obj, args);
1521 if (ret != -ENODEV)
1522 goto err;
1523
Chris Wilsone95433c2016-10-28 13:58:27 +01001524 ret = i915_gem_object_wait(obj,
1525 I915_WAIT_INTERRUPTIBLE |
1526 I915_WAIT_ALL,
1527 MAX_SCHEDULE_TIMEOUT,
1528 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001529 if (ret)
1530 goto err;
1531
Chris Wilsonfe115622016-10-28 13:58:40 +01001532 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001533 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001534 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001535
Daniel Vetter935aaa62012-03-25 19:47:35 +02001536 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001537 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1538 * it would end up going through the fenced access, and we'll get
1539 * different detiling behavior between reading and writing.
1540 * pread/pwrite currently are reading and writing from the CPU
1541 * perspective, requiring manual detiling by the client.
1542 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001543 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001544 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001545 /* Note that the gtt paths might fail with non-page-backed user
1546 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001547 * textures). Fallback to the shmem path in that case.
1548 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001549 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001550
Chris Wilsond1054ee2016-07-16 18:42:36 +01001551 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001552 if (obj->phys_handle)
1553 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301554 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001555 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001556 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001557
Chris Wilsonfe115622016-10-28 13:58:40 +01001558 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001559err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001560 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001561 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001562}
1563
Chris Wilson40e62d52016-10-28 13:58:41 +01001564static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1565{
1566 struct drm_i915_private *i915;
1567 struct list_head *list;
1568 struct i915_vma *vma;
1569
Chris Wilsonf2123812017-10-16 12:40:37 +01001570 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1571
Chris Wilson40e62d52016-10-28 13:58:41 +01001572 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1573 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001574 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001575
1576 if (i915_vma_is_active(vma))
1577 continue;
1578
1579 if (!drm_mm_node_allocated(&vma->node))
1580 continue;
1581
1582 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1583 }
1584
1585 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001586 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001587 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001588 list_move_tail(&obj->mm.link, list);
1589 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001590}
1591
Eric Anholt673a3942008-07-30 12:06:12 -07001592/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001593 * Called when user space prepares to use an object with the CPU, either
1594 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001595 * @dev: drm device
1596 * @data: ioctl data blob
1597 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001598 */
1599int
1600i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001601 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001602{
1603 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001604 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001605 uint32_t read_domains = args->read_domains;
1606 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001607 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001608
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001609 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001610 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001611 return -EINVAL;
1612
1613 /* Having something in the write domain implies it's in the read
1614 * domain, and only that read domain. Enforce that in the request.
1615 */
1616 if (write_domain != 0 && read_domains != write_domain)
1617 return -EINVAL;
1618
Chris Wilson03ac0642016-07-20 13:31:51 +01001619 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001620 if (!obj)
1621 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001622
Chris Wilson3236f572012-08-24 09:35:09 +01001623 /* Try to flush the object off the GPU without holding the lock.
1624 * We will repeat the flush holding the lock in the normal manner
1625 * to catch cases where we are gazumped.
1626 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001627 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001628 I915_WAIT_INTERRUPTIBLE |
1629 (write_domain ? I915_WAIT_ALL : 0),
1630 MAX_SCHEDULE_TIMEOUT,
1631 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001632 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001633 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001634
Tina Zhanga03f3952017-11-14 10:25:13 +00001635 /*
1636 * Proxy objects do not control access to the backing storage, ergo
1637 * they cannot be used as a means to manipulate the cache domain
1638 * tracking for that backing storage. The proxy object is always
1639 * considered to be outside of any cache domain.
1640 */
1641 if (i915_gem_object_is_proxy(obj)) {
1642 err = -ENXIO;
1643 goto out;
1644 }
1645
1646 /*
1647 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001648 * direct access in memory with previous cached writes through
1649 * shmemfs and that our cache domain tracking remains valid.
1650 * For example, if the obj->filp was moved to swap without us
1651 * being notified and releasing the pages, we would mistakenly
1652 * continue to assume that the obj remained out of the CPU cached
1653 * domain.
1654 */
1655 err = i915_gem_object_pin_pages(obj);
1656 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001657 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001658
1659 err = i915_mutex_lock_interruptible(dev);
1660 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001661 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001662
Chris Wilsone22d8e32017-04-12 12:01:11 +01001663 if (read_domains & I915_GEM_DOMAIN_WC)
1664 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1665 else if (read_domains & I915_GEM_DOMAIN_GTT)
1666 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301667 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001668 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001669
1670 /* And bump the LRU for this access */
1671 i915_gem_object_bump_inactive_ggtt(obj);
1672
1673 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001674
Daniel Vetter031b6982015-06-26 19:35:16 +02001675 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001676 intel_fb_obj_invalidate(obj,
1677 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001678
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001679out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001680 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001681out:
1682 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001683 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001684}
1685
1686/**
1687 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001688 * @dev: drm device
1689 * @data: ioctl data blob
1690 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001691 */
1692int
1693i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001694 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001695{
1696 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001697 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001698
Chris Wilson03ac0642016-07-20 13:31:51 +01001699 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001700 if (!obj)
1701 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001702
Tina Zhanga03f3952017-11-14 10:25:13 +00001703 /*
1704 * Proxy objects are barred from CPU access, so there is no
1705 * need to ban sw_finish as it is a nop.
1706 */
1707
Eric Anholt673a3942008-07-30 12:06:12 -07001708 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001709 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001710 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001711
1712 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001713}
1714
1715/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001716 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1717 * it is mapped to.
1718 * @dev: drm device
1719 * @data: ioctl data blob
1720 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001721 *
1722 * While the mapping holds a reference on the contents of the object, it doesn't
1723 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001724 *
1725 * IMPORTANT:
1726 *
1727 * DRM driver writers who look a this function as an example for how to do GEM
1728 * mmap support, please don't implement mmap support like here. The modern way
1729 * to implement DRM mmap support is with an mmap offset ioctl (like
1730 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1731 * That way debug tooling like valgrind will understand what's going on, hiding
1732 * the mmap call in a driver private ioctl will break that. The i915 driver only
1733 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001734 */
1735int
1736i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001737 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001738{
1739 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001740 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001741 unsigned long addr;
1742
Akash Goel1816f922015-01-02 16:29:30 +05301743 if (args->flags & ~(I915_MMAP_WC))
1744 return -EINVAL;
1745
Borislav Petkov568a58e2016-03-29 17:42:01 +02001746 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301747 return -ENODEV;
1748
Chris Wilson03ac0642016-07-20 13:31:51 +01001749 obj = i915_gem_object_lookup(file, args->handle);
1750 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001751 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001752
Daniel Vetter1286ff72012-05-10 15:25:09 +02001753 /* prime objects have no backing filp to GEM mmap
1754 * pages from.
1755 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001756 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001757 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001758 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001759 }
1760
Chris Wilson03ac0642016-07-20 13:31:51 +01001761 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001762 PROT_READ | PROT_WRITE, MAP_SHARED,
1763 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301764 if (args->flags & I915_MMAP_WC) {
1765 struct mm_struct *mm = current->mm;
1766 struct vm_area_struct *vma;
1767
Michal Hocko80a89a52016-05-23 16:26:11 -07001768 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001769 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001770 return -EINTR;
1771 }
Akash Goel1816f922015-01-02 16:29:30 +05301772 vma = find_vma(mm, addr);
1773 if (vma)
1774 vma->vm_page_prot =
1775 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1776 else
1777 addr = -ENOMEM;
1778 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001779
1780 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001781 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301782 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001783 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001784 if (IS_ERR((void *)addr))
1785 return addr;
1786
1787 args->addr_ptr = (uint64_t) addr;
1788
1789 return 0;
1790}
1791
Chris Wilson03af84f2016-08-18 17:17:01 +01001792static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1793{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001794 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001795}
1796
Jesse Barnesde151cf2008-11-12 10:03:55 -08001797/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001798 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1799 *
1800 * A history of the GTT mmap interface:
1801 *
1802 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1803 * aligned and suitable for fencing, and still fit into the available
1804 * mappable space left by the pinned display objects. A classic problem
1805 * we called the page-fault-of-doom where we would ping-pong between
1806 * two objects that could not fit inside the GTT and so the memcpy
1807 * would page one object in at the expense of the other between every
1808 * single byte.
1809 *
1810 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1811 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1812 * object is too large for the available space (or simply too large
1813 * for the mappable aperture!), a view is created instead and faulted
1814 * into userspace. (This view is aligned and sized appropriately for
1815 * fenced access.)
1816 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001817 * 2 - Recognise WC as a separate cache domain so that we can flush the
1818 * delayed writes via GTT before performing direct access via WC.
1819 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001820 * Restrictions:
1821 *
1822 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1823 * hangs on some architectures, corruption on others. An attempt to service
1824 * a GTT page fault from a snoopable object will generate a SIGBUS.
1825 *
1826 * * the object must be able to fit into RAM (physical memory, though no
1827 * limited to the mappable aperture).
1828 *
1829 *
1830 * Caveats:
1831 *
1832 * * a new GTT page fault will synchronize rendering from the GPU and flush
1833 * all data to system memory. Subsequent access will not be synchronized.
1834 *
1835 * * all mappings are revoked on runtime device suspend.
1836 *
1837 * * there are only 8, 16 or 32 fence registers to share between all users
1838 * (older machines require fence register for display and blitter access
1839 * as well). Contention of the fence registers will cause the previous users
1840 * to be unmapped and any new access will generate new page faults.
1841 *
1842 * * running out of memory while servicing a fault may generate a SIGBUS,
1843 * rather than the expected SIGSEGV.
1844 */
1845int i915_gem_mmap_gtt_version(void)
1846{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001847 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001848}
1849
Chris Wilson2d4281b2017-01-10 09:56:32 +00001850static inline struct i915_ggtt_view
1851compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001852 pgoff_t page_offset,
1853 unsigned int chunk)
1854{
1855 struct i915_ggtt_view view;
1856
1857 if (i915_gem_object_is_tiled(obj))
1858 chunk = roundup(chunk, tile_row_pages(obj));
1859
Chris Wilson2d4281b2017-01-10 09:56:32 +00001860 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001861 view.partial.offset = rounddown(page_offset, chunk);
1862 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001863 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001864 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001865
1866 /* If the partial covers the entire object, just create a normal VMA. */
1867 if (chunk >= obj->base.size >> PAGE_SHIFT)
1868 view.type = I915_GGTT_VIEW_NORMAL;
1869
1870 return view;
1871}
1872
Chris Wilson4cc69072016-08-25 19:05:19 +01001873/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001874 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001875 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001876 *
1877 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1878 * from userspace. The fault handler takes care of binding the object to
1879 * the GTT (if needed), allocating and programming a fence register (again,
1880 * only if needed based on whether the old reg is still valid or the object
1881 * is tiled) and inserting a new PTE into the faulting process.
1882 *
1883 * Note that the faulting process may involve evicting existing objects
1884 * from the GTT and/or fence registers to make room. So performance may
1885 * suffer if the GTT working set is large or there are few fence registers
1886 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001887 *
1888 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1889 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001890 */
Dave Jiang11bac802017-02-24 14:56:41 -08001891int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001892{
Chris Wilson03af84f2016-08-18 17:17:01 +01001893#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001894 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001895 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001896 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001897 struct drm_i915_private *dev_priv = to_i915(dev);
1898 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001899 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001900 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001901 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001902 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001903 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001904
Jesse Barnesde151cf2008-11-12 10:03:55 -08001905 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001906 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001907
Chris Wilsondb53a302011-02-03 11:57:46 +00001908 trace_i915_gem_object_fault(obj, page_offset, true, write);
1909
Chris Wilson6e4930f2014-02-07 18:37:06 -02001910 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001911 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001912 * repeat the flush holding the lock in the normal manner to catch cases
1913 * where we are gazumped.
1914 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001915 ret = i915_gem_object_wait(obj,
1916 I915_WAIT_INTERRUPTIBLE,
1917 MAX_SCHEDULE_TIMEOUT,
1918 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001919 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001920 goto err;
1921
Chris Wilson40e62d52016-10-28 13:58:41 +01001922 ret = i915_gem_object_pin_pages(obj);
1923 if (ret)
1924 goto err;
1925
Chris Wilsonb8f90962016-08-05 10:14:07 +01001926 intel_runtime_pm_get(dev_priv);
1927
1928 ret = i915_mutex_lock_interruptible(dev);
1929 if (ret)
1930 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001931
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001932 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001933 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001934 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001935 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001936 }
1937
Chris Wilson82118872016-08-18 17:17:05 +01001938 /* If the object is smaller than a couple of partial vma, it is
1939 * not worth only creating a single partial vma - we may as well
1940 * clear enough space for the full object.
1941 */
1942 flags = PIN_MAPPABLE;
1943 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1944 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1945
Chris Wilsona61007a2016-08-18 17:17:02 +01001946 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001947 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001948 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001949 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001950 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001951 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001952
Chris Wilson50349242016-08-18 17:17:04 +01001953 /* Userspace is now writing through an untracked VMA, abandon
1954 * all hope that the hardware is able to track future writes.
1955 */
1956 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1957
Chris Wilsona61007a2016-08-18 17:17:02 +01001958 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1959 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001960 if (IS_ERR(vma)) {
1961 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001962 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001963 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001964
Chris Wilsonc9839302012-11-20 10:45:17 +00001965 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1966 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001967 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001968
Chris Wilson3bd40732017-10-09 09:43:56 +01001969 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001970 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001971 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001972
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001973 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001974 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001975 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001976 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1977 min_t(u64, vma->size, area->vm_end - area->vm_start),
1978 &ggtt->mappable);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001979 if (ret)
1980 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001981
Chris Wilsona65adaf2017-10-09 09:43:57 +01001982 /* Mark as being mmapped into userspace for later revocation */
1983 assert_rpm_wakelock_held(dev_priv);
1984 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1985 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1986 GEM_BUG_ON(!obj->userfault_count);
1987
Chris Wilson71253972017-12-06 12:49:14 +00001988 i915_vma_set_ggtt_write(vma);
1989
Chris Wilsona65adaf2017-10-09 09:43:57 +01001990err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001991 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001992err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001993 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001994err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001995 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001996err_rpm:
1997 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001998 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001999err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002000 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002001 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002002 /*
2003 * We eat errors when the gpu is terminally wedged to avoid
2004 * userspace unduly crashing (gl has no provisions for mmaps to
2005 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2006 * and so needs to be reported.
2007 */
2008 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002009 ret = VM_FAULT_SIGBUS;
2010 break;
2011 }
Chris Wilson045e7692010-11-07 09:18:22 +00002012 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002013 /*
2014 * EAGAIN means the gpu is hung and we'll wait for the error
2015 * handler to reset everything when re-faulting in
2016 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002017 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002018 case 0:
2019 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002020 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002021 case -EBUSY:
2022 /*
2023 * EBUSY is ok: this just means that another thread
2024 * already did the job.
2025 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002026 ret = VM_FAULT_NOPAGE;
2027 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002028 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002029 ret = VM_FAULT_OOM;
2030 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002031 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002032 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002033 ret = VM_FAULT_SIGBUS;
2034 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002035 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002036 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002037 ret = VM_FAULT_SIGBUS;
2038 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002039 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002040 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002041}
2042
Chris Wilsona65adaf2017-10-09 09:43:57 +01002043static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2044{
2045 struct i915_vma *vma;
2046
2047 GEM_BUG_ON(!obj->userfault_count);
2048
2049 obj->userfault_count = 0;
2050 list_del(&obj->userfault_link);
2051 drm_vma_node_unmap(&obj->base.vma_node,
2052 obj->base.dev->anon_inode->i_mapping);
2053
2054 list_for_each_entry(vma, &obj->vma_list, obj_link) {
2055 if (!i915_vma_is_ggtt(vma))
2056 break;
2057
2058 i915_vma_unset_userfault(vma);
2059 }
2060}
2061
Jesse Barnesde151cf2008-11-12 10:03:55 -08002062/**
Chris Wilson901782b2009-07-10 08:18:50 +01002063 * i915_gem_release_mmap - remove physical page mappings
2064 * @obj: obj in question
2065 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002066 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002067 * relinquish ownership of the pages back to the system.
2068 *
2069 * It is vital that we remove the page mapping if we have mapped a tiled
2070 * object through the GTT and then lose the fence register due to
2071 * resource pressure. Similarly if the object has been moved out of the
2072 * aperture, than pages mapped into userspace must be revoked. Removing the
2073 * mapping will then trigger a page fault on the next user access, allowing
2074 * fixup by i915_gem_fault().
2075 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002076void
Chris Wilson05394f32010-11-08 19:18:58 +00002077i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002078{
Chris Wilson275f0392016-10-24 13:42:14 +01002079 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002080
Chris Wilson349f2cc2016-04-13 17:35:12 +01002081 /* Serialisation between user GTT access and our code depends upon
2082 * revoking the CPU's PTE whilst the mutex is held. The next user
2083 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002084 *
2085 * Note that RPM complicates somewhat by adding an additional
2086 * requirement that operations to the GGTT be made holding the RPM
2087 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002088 */
Chris Wilson275f0392016-10-24 13:42:14 +01002089 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002090 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002091
Chris Wilsona65adaf2017-10-09 09:43:57 +01002092 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002093 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002094
Chris Wilsona65adaf2017-10-09 09:43:57 +01002095 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002096
2097 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2098 * memory transactions from userspace before we return. The TLB
2099 * flushing implied above by changing the PTE above *should* be
2100 * sufficient, an extra barrier here just provides us with a bit
2101 * of paranoid documentation about our requirement to serialise
2102 * memory writes before touching registers / GSM.
2103 */
2104 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002105
2106out:
2107 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002108}
2109
Chris Wilson7c108fd2016-10-24 13:42:18 +01002110void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002111{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002112 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002113 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002114
Chris Wilson3594a3e2016-10-24 13:42:16 +01002115 /*
2116 * Only called during RPM suspend. All users of the userfault_list
2117 * must be holding an RPM wakeref to ensure that this can not
2118 * run concurrently with themselves (and use the struct_mutex for
2119 * protection between themselves).
2120 */
2121
2122 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002123 &dev_priv->mm.userfault_list, userfault_link)
2124 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002125
2126 /* The fence will be lost when the device powers down. If any were
2127 * in use by hardware (i.e. they are pinned), we should not be powering
2128 * down! All other fences will be reacquired by the user upon waking.
2129 */
2130 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2131 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2132
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002133 /* Ideally we want to assert that the fence register is not
2134 * live at this point (i.e. that no piece of code will be
2135 * trying to write through fence + GTT, as that both violates
2136 * our tracking of activity and associated locking/barriers,
2137 * but also is illegal given that the hw is powered down).
2138 *
2139 * Previously we used reg->pin_count as a "liveness" indicator.
2140 * That is not sufficient, and we need a more fine-grained
2141 * tool if we want to have a sanity check here.
2142 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002143
2144 if (!reg->vma)
2145 continue;
2146
Chris Wilsona65adaf2017-10-09 09:43:57 +01002147 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002148 reg->dirty = true;
2149 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002150}
2151
Chris Wilsond8cb5082012-08-11 15:41:03 +01002152static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2153{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002154 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002155 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002156
Chris Wilsonf3f61842016-08-05 10:14:14 +01002157 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002158 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002159 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002160
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002161 /* Attempt to reap some mmap space from dead objects */
2162 do {
2163 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2164 if (err)
2165 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002166
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002167 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002168 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002169 if (!err)
2170 break;
2171
2172 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002173
Chris Wilsonf3f61842016-08-05 10:14:14 +01002174 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002175}
2176
2177static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2178{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002179 drm_gem_free_mmap_offset(&obj->base);
2180}
2181
Dave Airlieda6b51d2014-12-24 13:11:17 +10002182int
Dave Airlieff72145b2011-02-07 12:16:14 +10002183i915_gem_mmap_gtt(struct drm_file *file,
2184 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002185 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002186 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002187{
Chris Wilson05394f32010-11-08 19:18:58 +00002188 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002189 int ret;
2190
Chris Wilson03ac0642016-07-20 13:31:51 +01002191 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002192 if (!obj)
2193 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002194
Chris Wilsond8cb5082012-08-11 15:41:03 +01002195 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002196 if (ret == 0)
2197 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002198
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002199 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002200 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002201}
2202
Dave Airlieff72145b2011-02-07 12:16:14 +10002203/**
2204 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2205 * @dev: DRM device
2206 * @data: GTT mapping ioctl data
2207 * @file: GEM object info
2208 *
2209 * Simply returns the fake offset to userspace so it can mmap it.
2210 * The mmap call will end up in drm_gem_mmap(), which will set things
2211 * up so we can get faults in the handler above.
2212 *
2213 * The fault handler will take care of binding the object into the GTT
2214 * (since it may have been evicted to make room for something), allocating
2215 * a fence register, and mapping the appropriate aperture address into
2216 * userspace.
2217 */
2218int
2219i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2220 struct drm_file *file)
2221{
2222 struct drm_i915_gem_mmap_gtt *args = data;
2223
Dave Airlieda6b51d2014-12-24 13:11:17 +10002224 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002225}
2226
Daniel Vetter225067e2012-08-20 10:23:20 +02002227/* Immediately discard the backing storage */
2228static void
2229i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002230{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002231 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002232
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002233 if (obj->base.filp == NULL)
2234 return;
2235
Daniel Vetter225067e2012-08-20 10:23:20 +02002236 /* Our goal here is to return as much of the memory as
2237 * is possible back to the system as we are called from OOM.
2238 * To do this we must instruct the shmfs to drop all of its
2239 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002240 */
Chris Wilson55372522014-03-25 13:23:06 +00002241 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002242 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002243 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002244}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002245
Chris Wilson55372522014-03-25 13:23:06 +00002246/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002247void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002248{
Chris Wilson55372522014-03-25 13:23:06 +00002249 struct address_space *mapping;
2250
Chris Wilson1233e2d2016-10-28 13:58:37 +01002251 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002252 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002253
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002254 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002255 case I915_MADV_DONTNEED:
2256 i915_gem_object_truncate(obj);
2257 case __I915_MADV_PURGED:
2258 return;
2259 }
2260
2261 if (obj->base.filp == NULL)
2262 return;
2263
Al Viro93c76a32015-12-04 23:45:44 -05002264 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002265 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002266}
2267
Chris Wilson5cdf5882010-09-27 15:51:07 +01002268static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002269i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2270 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002271{
Dave Gordon85d12252016-05-20 11:54:06 +01002272 struct sgt_iter sgt_iter;
2273 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002274
Chris Wilsone5facdf2016-12-23 14:57:57 +00002275 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002276
Chris Wilson03ac84f2016-10-28 13:58:36 +01002277 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002278
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002279 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002280 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002281
Chris Wilson03ac84f2016-10-28 13:58:36 +01002282 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002283 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002284 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002285
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002286 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002287 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002288
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002289 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002290 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002291 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002292
Chris Wilson03ac84f2016-10-28 13:58:36 +01002293 sg_free_table(pages);
2294 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002295}
2296
Chris Wilson96d77632016-10-28 13:58:33 +01002297static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2298{
2299 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002300 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002301
Chris Wilsonbea6e982017-10-26 14:00:31 +01002302 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002303 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2304 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002305 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002306}
2307
Chris Wilson548625e2016-11-01 12:11:34 +00002308void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2309 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002310{
Chris Wilsonf2123812017-10-16 12:40:37 +01002311 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002312 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002313
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002314 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002315 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002316
Chris Wilson15717de2016-08-04 07:52:26 +01002317 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002318 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002319 return;
2320
2321 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002322 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002323 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2324 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002325
Chris Wilsona2165e32012-12-03 11:49:00 +00002326 /* ->put_pages might need to allocate memory for the bit17 swizzle
2327 * array, hence protect them from being reaped by removing them from gtt
2328 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002329 pages = fetch_and_zero(&obj->mm.pages);
2330 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002331
Chris Wilsonf2123812017-10-16 12:40:37 +01002332 spin_lock(&i915->mm.obj_lock);
2333 list_del(&obj->mm.link);
2334 spin_unlock(&i915->mm.obj_lock);
2335
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002336 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002337 void *ptr;
2338
Chris Wilson0ce81782017-05-17 13:09:59 +01002339 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002340 if (is_vmalloc_addr(ptr))
2341 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002342 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002343 kunmap(kmap_to_page(ptr));
2344
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002345 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002346 }
2347
Chris Wilson96d77632016-10-28 13:58:33 +01002348 __i915_gem_object_reset_page_iter(obj);
2349
Chris Wilson4e5462e2017-03-07 13:20:31 +00002350 if (!IS_ERR(pages))
2351 obj->ops->put_pages(obj, pages);
2352
Matthew Aulda5c081662017-10-06 23:18:18 +01002353 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2354
Chris Wilson1233e2d2016-10-28 13:58:37 +01002355unlock:
2356 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002357}
2358
Chris Wilson935a2f72017-02-13 17:15:13 +00002359static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002360{
2361 struct sg_table new_st;
2362 struct scatterlist *sg, *new_sg;
2363 unsigned int i;
2364
2365 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002366 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002367
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002368 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002369 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002370
2371 new_sg = new_st.sgl;
2372 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2373 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2374 /* called before being DMA mapped, no need to copy sg->dma_* */
2375 new_sg = sg_next(new_sg);
2376 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002377 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002378
2379 sg_free_table(orig_st);
2380
2381 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002382 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002383}
2384
Matthew Auldb91b09e2017-10-06 23:18:17 +01002385static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002386{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002387 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002388 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2389 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002390 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002391 struct sg_table *st;
2392 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002393 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002394 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002395 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002396 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002397 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002398 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002399 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002400
Chris Wilson6c085a72012-08-20 11:40:46 +02002401 /* Assert that the object is not currently in any GPU domain. As it
2402 * wasn't in the GTT, there shouldn't be any way it could have been in
2403 * a GPU cache
2404 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002405 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2406 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002407
Chris Wilson9da3da62012-06-01 15:20:22 +01002408 st = kmalloc(sizeof(*st), GFP_KERNEL);
2409 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002410 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002411
Chris Wilsond766ef52016-12-19 12:43:45 +00002412rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002413 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002414 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002415 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002416 }
2417
2418 /* Get the list of pages out of our struct file. They'll be pinned
2419 * at this point until we release them.
2420 *
2421 * Fail silently without starting the shrinker
2422 */
Al Viro93c76a32015-12-04 23:45:44 -05002423 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002424 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002425 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2426
Imre Deak90797e62013-02-18 19:28:03 +02002427 sg = st->sgl;
2428 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002429 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002430 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002431 const unsigned int shrink[] = {
2432 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2433 0,
2434 }, *s = shrink;
2435 gfp_t gfp = noreclaim;
2436
2437 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002438 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002439 if (likely(!IS_ERR(page)))
2440 break;
2441
2442 if (!*s) {
2443 ret = PTR_ERR(page);
2444 goto err_sg;
2445 }
2446
Chris Wilson912d5722017-09-06 16:19:30 -07002447 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002448 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002449
Chris Wilson6c085a72012-08-20 11:40:46 +02002450 /* We've tried hard to allocate the memory by reaping
2451 * our own buffer, now let the real VM do its job and
2452 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002453 *
2454 * However, since graphics tend to be disposable,
2455 * defer the oom here by reporting the ENOMEM back
2456 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002457 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002458 if (!*s) {
2459 /* reclaim and warn, but no oom */
2460 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002461
2462 /* Our bo are always dirty and so we require
2463 * kswapd to reclaim our pages (direct reclaim
2464 * does not effectively begin pageout of our
2465 * buffers on its own). However, direct reclaim
2466 * only waits for kswapd when under allocation
2467 * congestion. So as a result __GFP_RECLAIM is
2468 * unreliable and fails to actually reclaim our
2469 * dirty pages -- unless you try over and over
2470 * again with !__GFP_NORETRY. However, we still
2471 * want to fail this allocation rather than
2472 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002473 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002474 */
Michal Hockodbb32952017-07-12 14:36:55 -07002475 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002476 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002477 } while (1);
2478
Chris Wilson871dfbd2016-10-11 09:20:21 +01002479 if (!i ||
2480 sg->length >= max_segment ||
2481 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002482 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002483 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002484 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002485 }
Imre Deak90797e62013-02-18 19:28:03 +02002486 st->nents++;
2487 sg_set_page(sg, page, PAGE_SIZE, 0);
2488 } else {
2489 sg->length += PAGE_SIZE;
2490 }
2491 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002492
2493 /* Check that the i965g/gm workaround works. */
2494 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002495 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002496 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002497 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002498 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002499 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002500
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002501 /* Trim unused sg entries to avoid wasting memory. */
2502 i915_sg_trim(st);
2503
Chris Wilson03ac84f2016-10-28 13:58:36 +01002504 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002505 if (ret) {
2506 /* DMA remapping failed? One possible cause is that
2507 * it could not reserve enough large entries, asking
2508 * for PAGE_SIZE chunks instead may be helpful.
2509 */
2510 if (max_segment > PAGE_SIZE) {
2511 for_each_sgt_page(page, sgt_iter, st)
2512 put_page(page);
2513 sg_free_table(st);
2514
2515 max_segment = PAGE_SIZE;
2516 goto rebuild_st;
2517 } else {
2518 dev_warn(&dev_priv->drm.pdev->dev,
2519 "Failed to DMA remap %lu pages\n",
2520 page_count);
2521 goto err_pages;
2522 }
2523 }
Imre Deake2273302015-07-09 12:59:05 +03002524
Eric Anholt673a3942008-07-30 12:06:12 -07002525 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002526 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002527
Matthew Auld84e89782017-10-09 12:00:24 +01002528 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002529
2530 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002531
Chris Wilsonb17993b2016-11-14 11:29:30 +00002532err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002533 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002534err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002535 for_each_sgt_page(page, sgt_iter, st)
2536 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002537 sg_free_table(st);
2538 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002539
2540 /* shmemfs first checks if there is enough memory to allocate the page
2541 * and reports ENOSPC should there be insufficient, along with the usual
2542 * ENOMEM for a genuine allocation failure.
2543 *
2544 * We use ENOSPC in our driver to mean that we have run out of aperture
2545 * space and so want to translate the error from shmemfs back to our
2546 * usual understanding of ENOMEM.
2547 */
Imre Deake2273302015-07-09 12:59:05 +03002548 if (ret == -ENOSPC)
2549 ret = -ENOMEM;
2550
Matthew Auldb91b09e2017-10-06 23:18:17 +01002551 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002552}
2553
2554void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002555 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002556 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002557{
Matthew Aulda5c081662017-10-06 23:18:18 +01002558 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2559 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2560 int i;
2561
Chris Wilson1233e2d2016-10-28 13:58:37 +01002562 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002563
2564 obj->mm.get_page.sg_pos = pages->sgl;
2565 obj->mm.get_page.sg_idx = 0;
2566
2567 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002568
2569 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002570 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002571 GEM_BUG_ON(obj->mm.quirked);
2572 __i915_gem_object_pin_pages(obj);
2573 obj->mm.quirked = true;
2574 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002575
Matthew Auld84e89782017-10-09 12:00:24 +01002576 GEM_BUG_ON(!sg_page_sizes);
2577 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002578
2579 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002580 * Calculate the supported page-sizes which fit into the given
2581 * sg_page_sizes. This will give us the page-sizes which we may be able
2582 * to use opportunistically when later inserting into the GTT. For
2583 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2584 * 64K or 4K pages, although in practice this will depend on a number of
2585 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002586 */
2587 obj->mm.page_sizes.sg = 0;
2588 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2589 if (obj->mm.page_sizes.phys & ~0u << i)
2590 obj->mm.page_sizes.sg |= BIT(i);
2591 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002592 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002593
2594 spin_lock(&i915->mm.obj_lock);
2595 list_add(&obj->mm.link, &i915->mm.unbound_list);
2596 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002597}
2598
2599static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2600{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002601 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002602
2603 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2604 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2605 return -EFAULT;
2606 }
2607
Matthew Auldb91b09e2017-10-06 23:18:17 +01002608 err = obj->ops->get_pages(obj);
2609 GEM_BUG_ON(!err && IS_ERR_OR_NULL(obj->mm.pages));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002610
Matthew Auldb91b09e2017-10-06 23:18:17 +01002611 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002612}
2613
Chris Wilson37e680a2012-06-07 15:38:42 +01002614/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002615 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002616 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002617 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002618 * either as a result of memory pressure (reaping pages under the shrinker)
2619 * or as the object is itself released.
2620 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002621int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002622{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002623 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002624
Chris Wilson1233e2d2016-10-28 13:58:37 +01002625 err = mutex_lock_interruptible(&obj->mm.lock);
2626 if (err)
2627 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002628
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002629 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002630 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2631
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002632 err = ____i915_gem_object_get_pages(obj);
2633 if (err)
2634 goto unlock;
2635
2636 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002637 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002638 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002639
Chris Wilson1233e2d2016-10-28 13:58:37 +01002640unlock:
2641 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002642 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002643}
2644
Dave Gordondd6034c2016-05-20 11:54:04 +01002645/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002646static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2647 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002648{
2649 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002650 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002651 struct sgt_iter sgt_iter;
2652 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002653 struct page *stack_pages[32];
2654 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002655 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002656 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002657 void *addr;
2658
2659 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002660 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002661 return kmap(sg_page(sgt->sgl));
2662
Dave Gordonb338fa42016-05-20 11:54:05 +01002663 if (n_pages > ARRAY_SIZE(stack_pages)) {
2664 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002665 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002666 if (!pages)
2667 return NULL;
2668 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002669
Dave Gordon85d12252016-05-20 11:54:06 +01002670 for_each_sgt_page(page, sgt_iter, sgt)
2671 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002672
2673 /* Check that we have the expected number of pages */
2674 GEM_BUG_ON(i != n_pages);
2675
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002676 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002677 default:
2678 MISSING_CASE(type);
2679 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002680 case I915_MAP_WB:
2681 pgprot = PAGE_KERNEL;
2682 break;
2683 case I915_MAP_WC:
2684 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2685 break;
2686 }
2687 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002688
Dave Gordonb338fa42016-05-20 11:54:05 +01002689 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002690 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002691
2692 return addr;
2693}
2694
2695/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002696void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2697 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002698{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002699 enum i915_map_type has_type;
2700 bool pinned;
2701 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002702 int ret;
2703
Tina Zhanga03f3952017-11-14 10:25:13 +00002704 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2705 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002706
Chris Wilson1233e2d2016-10-28 13:58:37 +01002707 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002708 if (ret)
2709 return ERR_PTR(ret);
2710
Chris Wilsona575c672017-08-28 11:46:31 +01002711 pinned = !(type & I915_MAP_OVERRIDE);
2712 type &= ~I915_MAP_OVERRIDE;
2713
Chris Wilson1233e2d2016-10-28 13:58:37 +01002714 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002715 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002716 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2717
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002718 ret = ____i915_gem_object_get_pages(obj);
2719 if (ret)
2720 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002721
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002722 smp_mb__before_atomic();
2723 }
2724 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002725 pinned = false;
2726 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002727 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002728
Chris Wilson0ce81782017-05-17 13:09:59 +01002729 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002730 if (ptr && has_type != type) {
2731 if (pinned) {
2732 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002733 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002734 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002735
2736 if (is_vmalloc_addr(ptr))
2737 vunmap(ptr);
2738 else
2739 kunmap(kmap_to_page(ptr));
2740
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002741 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002742 }
2743
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002744 if (!ptr) {
2745 ptr = i915_gem_object_map(obj, type);
2746 if (!ptr) {
2747 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002748 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002749 }
2750
Chris Wilson0ce81782017-05-17 13:09:59 +01002751 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002752 }
2753
Chris Wilson1233e2d2016-10-28 13:58:37 +01002754out_unlock:
2755 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002756 return ptr;
2757
Chris Wilson1233e2d2016-10-28 13:58:37 +01002758err_unpin:
2759 atomic_dec(&obj->mm.pages_pin_count);
2760err_unlock:
2761 ptr = ERR_PTR(ret);
2762 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002763}
2764
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002765static int
2766i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2767 const struct drm_i915_gem_pwrite *arg)
2768{
2769 struct address_space *mapping = obj->base.filp->f_mapping;
2770 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2771 u64 remain, offset;
2772 unsigned int pg;
2773
2774 /* Before we instantiate/pin the backing store for our use, we
2775 * can prepopulate the shmemfs filp efficiently using a write into
2776 * the pagecache. We avoid the penalty of instantiating all the
2777 * pages, important if the user is just writing to a few and never
2778 * uses the object on the GPU, and using a direct write into shmemfs
2779 * allows it to avoid the cost of retrieving a page (either swapin
2780 * or clearing-before-use) before it is overwritten.
2781 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002782 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002783 return -ENODEV;
2784
Chris Wilsona6d65e42017-10-16 21:27:32 +01002785 if (obj->mm.madv != I915_MADV_WILLNEED)
2786 return -EFAULT;
2787
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002788 /* Before the pages are instantiated the object is treated as being
2789 * in the CPU domain. The pages will be clflushed as required before
2790 * use, and we can freely write into the pages directly. If userspace
2791 * races pwrite with any other operation; corruption will ensue -
2792 * that is userspace's prerogative!
2793 */
2794
2795 remain = arg->size;
2796 offset = arg->offset;
2797 pg = offset_in_page(offset);
2798
2799 do {
2800 unsigned int len, unwritten;
2801 struct page *page;
2802 void *data, *vaddr;
2803 int err;
2804
2805 len = PAGE_SIZE - pg;
2806 if (len > remain)
2807 len = remain;
2808
2809 err = pagecache_write_begin(obj->base.filp, mapping,
2810 offset, len, 0,
2811 &page, &data);
2812 if (err < 0)
2813 return err;
2814
2815 vaddr = kmap(page);
2816 unwritten = copy_from_user(vaddr + pg, user_data, len);
2817 kunmap(page);
2818
2819 err = pagecache_write_end(obj->base.filp, mapping,
2820 offset, len, len - unwritten,
2821 page, data);
2822 if (err < 0)
2823 return err;
2824
2825 if (unwritten)
2826 return -EFAULT;
2827
2828 remain -= len;
2829 user_data += len;
2830 offset += len;
2831 pg = 0;
2832 } while (remain);
2833
2834 return 0;
2835}
2836
Chris Wilson77b25a92017-07-21 13:32:30 +01002837static bool ban_context(const struct i915_gem_context *ctx,
2838 unsigned int score)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002839{
Chris Wilson60958682016-12-31 11:20:11 +00002840 return (i915_gem_context_is_bannable(ctx) &&
Chris Wilson77b25a92017-07-21 13:32:30 +01002841 score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002842}
2843
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002844static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002845{
Chris Wilson77b25a92017-07-21 13:32:30 +01002846 unsigned int score;
2847 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002848
Chris Wilson77b25a92017-07-21 13:32:30 +01002849 atomic_inc(&ctx->guilty_count);
2850
2851 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
2852 banned = ban_context(ctx, score);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002853 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Chris Wilson77b25a92017-07-21 13:32:30 +01002854 ctx->name, score, yesno(banned));
2855 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002856 return;
2857
Chris Wilson77b25a92017-07-21 13:32:30 +01002858 i915_gem_context_set_banned(ctx);
2859 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2860 atomic_inc(&ctx->file_priv->context_bans);
2861 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2862 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2863 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002864}
2865
2866static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2867{
Chris Wilson77b25a92017-07-21 13:32:30 +01002868 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002869}
2870
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002871struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002872i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002873{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002874 struct drm_i915_gem_request *request, *active = NULL;
2875 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002876
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002877 /* We are called by the error capture and reset at a random
2878 * point in time. In particular, note that neither is crucially
2879 * ordered with an interrupt. After a hang, the GPU is dead and we
2880 * assume that no more writes can happen (we waited long enough for
2881 * all writes that were in transaction to be flushed) - adding an
2882 * extra delay for a recent interrupt is pointless. Hence, we do
2883 * not need an engine->irq_seqno_barrier() before the seqno reads.
2884 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002885 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002886 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002887 if (__i915_gem_request_completed(request,
2888 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002889 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002890
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002891 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002892 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2893 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002894
Chris Wilson754c9fd2017-02-23 07:44:14 +00002895 active = request;
2896 break;
2897 }
2898 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2899
2900 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002901}
2902
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002903static bool engine_stalled(struct intel_engine_cs *engine)
2904{
2905 if (!engine->hangcheck.stalled)
2906 return false;
2907
2908 /* Check for possible seqno movement after hang declaration */
2909 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2910 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2911 return false;
2912 }
2913
2914 return true;
2915}
2916
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002917/*
2918 * Ensure irq handler finishes, and not run again.
2919 * Also return the active request so that we only search for it once.
2920 */
2921struct drm_i915_gem_request *
2922i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2923{
2924 struct drm_i915_gem_request *request = NULL;
2925
Chris Wilson1749d902017-10-09 12:02:59 +01002926 /*
2927 * During the reset sequence, we must prevent the engine from
2928 * entering RC6. As the context state is undefined until we restart
2929 * the engine, if it does enter RC6 during the reset, the state
2930 * written to the powercontext is undefined and so we may lose
2931 * GPU state upon resume, i.e. fail to restart after a reset.
2932 */
2933 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2934
2935 /*
2936 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002937 * state (by calling dma_fence_signal) as we are processing
2938 * the reset. The write from the GPU of the seqno is
2939 * asynchronous and the signaler thread may see a different
2940 * value to us and declare the request complete, even though
2941 * the reset routine have picked that request as the active
2942 * (incomplete) request. This conflict is not handled
2943 * gracefully!
2944 */
2945 kthread_park(engine->breadcrumbs.signaler);
2946
Chris Wilson1749d902017-10-09 12:02:59 +01002947 /*
2948 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002949 * completed the reset in i915_gem_reset_finish(). If a request
2950 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302951 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002952 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302953 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002954 * prevents the race.
2955 */
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302956 tasklet_kill(&engine->execlists.tasklet);
2957 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002958
Michał Winiarskic41937f2017-10-26 15:35:58 +02002959 /*
2960 * We're using worker to queue preemption requests from the tasklet in
2961 * GuC submission mode.
2962 * Even though tasklet was disabled, we may still have a worker queued.
2963 * Let's make sure that all workers scheduled before disabling the
2964 * tasklet are completed before continuing with the reset.
2965 */
2966 if (engine->i915->guc.preempt_wq)
2967 flush_workqueue(engine->i915->guc.preempt_wq);
2968
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002969 if (engine->irq_seqno_barrier)
2970 engine->irq_seqno_barrier(engine);
2971
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002972 request = i915_gem_find_active_request(engine);
2973 if (request && request->fence.error == -EIO)
2974 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002975
2976 return request;
2977}
2978
Chris Wilson0e178ae2017-01-17 17:59:06 +02002979int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002980{
2981 struct intel_engine_cs *engine;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002982 struct drm_i915_gem_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002983 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002984 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002985
Chris Wilson0e178ae2017-01-17 17:59:06 +02002986 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002987 request = i915_gem_reset_prepare_engine(engine);
2988 if (IS_ERR(request)) {
2989 err = PTR_ERR(request);
2990 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002991 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002992
2993 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002994 }
2995
Chris Wilson4c965542017-01-17 17:59:01 +02002996 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002997
2998 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002999}
3000
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003001static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003002{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003003 void *vaddr = request->ring->vaddr;
3004 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003005
Chris Wilson821ed7d2016-09-09 14:11:53 +01003006 /* As this request likely depends on state from the lost
3007 * context, clear out all the user operations leaving the
3008 * breadcrumb at the end (so we get the fence notifications).
3009 */
3010 head = request->head;
3011 if (request->postfix < head) {
3012 memset(vaddr + head, 0, request->ring->size - head);
3013 head = 0;
3014 }
3015 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003016
3017 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003018}
3019
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003020static void engine_skip_context(struct drm_i915_gem_request *request)
3021{
3022 struct intel_engine_cs *engine = request->engine;
3023 struct i915_gem_context *hung_ctx = request->ctx;
3024 struct intel_timeline *timeline;
3025 unsigned long flags;
3026
3027 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3028
3029 spin_lock_irqsave(&engine->timeline->lock, flags);
3030 spin_lock(&timeline->lock);
3031
3032 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3033 if (request->ctx == hung_ctx)
3034 skip_request(request);
3035
3036 list_for_each_entry(request, &timeline->requests, link)
3037 skip_request(request);
3038
3039 spin_unlock(&timeline->lock);
3040 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3041}
3042
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003043/* Returns the request if it was guilty of the hang */
3044static struct drm_i915_gem_request *
3045i915_gem_reset_request(struct intel_engine_cs *engine,
3046 struct drm_i915_gem_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003047{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003048 /* The guilty request will get skipped on a hung engine.
3049 *
3050 * Users of client default contexts do not rely on logical
3051 * state preserved between batches so it is safe to execute
3052 * queued requests following the hang. Non default contexts
3053 * rely on preserved state, so skipping a batch loses the
3054 * evolution of the state and it needs to be considered corrupted.
3055 * Executing more queued batches on top of corrupted state is
3056 * risky. But we take the risk by trying to advance through
3057 * the queued requests in order to make the client behaviour
3058 * more predictable around resets, by not throwing away random
3059 * amount of batches it has prepared for execution. Sophisticated
3060 * clients can use gem_reset_stats_ioctl and dma fence status
3061 * (exported via sync_file info ioctl on explicit fences) to observe
3062 * when it loses the context state and should rebuild accordingly.
3063 *
3064 * The context ban, and ultimately the client ban, mechanism are safety
3065 * valves if client submission ends up resulting in nothing more than
3066 * subsequent hangs.
3067 */
3068
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003069 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003070 i915_gem_context_mark_guilty(request->ctx);
3071 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003072
3073 /* If this context is now banned, skip all pending requests. */
3074 if (i915_gem_context_is_banned(request->ctx))
3075 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003076 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003077 /*
3078 * Since this is not the hung engine, it may have advanced
3079 * since the hang declaration. Double check by refinding
3080 * the active request at the time of the reset.
3081 */
3082 request = i915_gem_find_active_request(engine);
3083 if (request) {
3084 i915_gem_context_mark_innocent(request->ctx);
3085 dma_fence_set_error(&request->fence, -EAGAIN);
3086
3087 /* Rewind the engine to replay the incomplete rq */
3088 spin_lock_irq(&engine->timeline->lock);
3089 request = list_prev_entry(request, link);
3090 if (&request->link == &engine->timeline->requests)
3091 request = NULL;
3092 spin_unlock_irq(&engine->timeline->lock);
3093 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003094 }
3095
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003096 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003097}
3098
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003099void i915_gem_reset_engine(struct intel_engine_cs *engine,
3100 struct drm_i915_gem_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003101{
Chris Wilsoned454f22017-07-21 13:32:29 +01003102 engine->irq_posted = 0;
3103
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003104 if (request)
3105 request = i915_gem_reset_request(engine, request);
3106
3107 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003108 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3109 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003110 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003111
3112 /* Setup the CS to resume from the breadcrumb of the hung request */
3113 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003114}
3115
Chris Wilsond8027092017-02-08 14:30:32 +00003116void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003117{
3118 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303119 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003120
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003121 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3122
Chris Wilson821ed7d2016-09-09 14:11:53 +01003123 i915_gem_retire_requests(dev_priv);
3124
Chris Wilson2ae55732017-02-12 17:20:02 +00003125 for_each_engine(engine, dev_priv, id) {
3126 struct i915_gem_context *ctx;
3127
Michel Thierryc64992e2017-06-20 10:57:44 +01003128 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003129 ctx = fetch_and_zero(&engine->last_retired_context);
3130 if (ctx)
3131 engine->context_unpin(engine, ctx);
3132 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003133
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003134 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003135
3136 if (dev_priv->gt.awake) {
3137 intel_sanitize_gt_powersave(dev_priv);
3138 intel_enable_gt_powersave(dev_priv);
3139 if (INTEL_GEN(dev_priv) >= 6)
3140 gen6_rps_busy(dev_priv);
3141 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003142}
3143
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003144void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3145{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303146 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003147 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003148
3149 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003150}
3151
Chris Wilsond8027092017-02-08 14:30:32 +00003152void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3153{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003154 struct intel_engine_cs *engine;
3155 enum intel_engine_id id;
3156
Chris Wilsond8027092017-02-08 14:30:32 +00003157 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003158
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003159 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003160 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003161 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003162 }
Chris Wilsond8027092017-02-08 14:30:32 +00003163}
3164
Chris Wilson821ed7d2016-09-09 14:11:53 +01003165static void nop_submit_request(struct drm_i915_gem_request *request)
3166{
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003167 dma_fence_set_error(&request->fence, -EIO);
3168
3169 i915_gem_request_submit(request);
3170}
3171
3172static void nop_complete_submit_request(struct drm_i915_gem_request *request)
3173{
Chris Wilson8d550822017-10-06 12:56:17 +01003174 unsigned long flags;
3175
Chris Wilson3cd94422017-01-10 17:22:45 +00003176 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003177
3178 spin_lock_irqsave(&request->engine->timeline->lock, flags);
3179 __i915_gem_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003180 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003181 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003182}
3183
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003184void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003185{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003186 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303187 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003188
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003189 /*
3190 * First, stop submission to hw, but do not yet complete requests by
3191 * rolling the global seqno forward (since this would complete requests
3192 * for which we haven't set the fence error to EIO yet).
3193 */
Chris Wilson20e49332016-11-22 14:41:21 +00003194 for_each_engine(engine, i915, id)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003195 engine->submit_request = nop_submit_request;
3196
3197 /*
3198 * Make sure no one is running the old callback before we proceed with
3199 * cancelling requests and resetting the completion tracking. Otherwise
3200 * we might submit a request to the hardware which never completes.
3201 */
3202 synchronize_rcu();
3203
3204 for_each_engine(engine, i915, id) {
3205 /* Mark all executing requests as skipped */
3206 engine->cancel_requests(engine);
3207
3208 /*
3209 * Only once we've force-cancelled all in-flight requests can we
3210 * start to complete all requests.
3211 */
3212 engine->submit_request = nop_complete_submit_request;
3213 }
3214
3215 /*
3216 * Make sure no request can slip through without getting completed by
3217 * either this call here to intel_engine_init_global_seqno, or the one
3218 * in nop_complete_submit_request.
3219 */
3220 synchronize_rcu();
3221
3222 for_each_engine(engine, i915, id) {
3223 unsigned long flags;
3224
3225 /* Mark all pending requests as complete so that any concurrent
3226 * (lockless) lookup doesn't try and wait upon the request as we
3227 * reset it.
3228 */
3229 spin_lock_irqsave(&engine->timeline->lock, flags);
3230 intel_engine_init_global_seqno(engine,
3231 intel_engine_last_submit(engine));
3232 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3233 }
Chris Wilson20e49332016-11-22 14:41:21 +00003234
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003235 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3236 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003237}
3238
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003239bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3240{
3241 struct i915_gem_timeline *tl;
3242 int i;
3243
3244 lockdep_assert_held(&i915->drm.struct_mutex);
3245 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3246 return true;
3247
3248 /* Before unwedging, make sure that all pending operations
3249 * are flushed and errored out - we may have requests waiting upon
3250 * third party fences. We marked all inflight requests as EIO, and
3251 * every execbuf since returned EIO, for consistency we want all
3252 * the currently pending requests to also be marked as EIO, which
3253 * is done inside our nop_submit_request - and so we must wait.
3254 *
3255 * No more can be submitted until we reset the wedged bit.
3256 */
3257 list_for_each_entry(tl, &i915->gt.timelines, link) {
3258 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3259 struct drm_i915_gem_request *rq;
3260
3261 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3262 &i915->drm.struct_mutex);
3263 if (!rq)
3264 continue;
3265
3266 /* We can't use our normal waiter as we want to
3267 * avoid recursively trying to handle the current
3268 * reset. The basic dma_fence_default_wait() installs
3269 * a callback for dma_fence_signal(), which is
3270 * triggered by our nop handler (indirectly, the
3271 * callback enables the signaler thread which is
3272 * woken by the nop_submit_request() advancing the seqno
3273 * and when the seqno passes the fence, the signaler
3274 * then signals the fence waking us up).
3275 */
3276 if (dma_fence_default_wait(&rq->fence, true,
3277 MAX_SCHEDULE_TIMEOUT) < 0)
3278 return false;
3279 }
3280 }
3281
3282 /* Undo nop_submit_request. We prevent all new i915 requests from
3283 * being queued (by disallowing execbuf whilst wedged) so having
3284 * waited for all active requests above, we know the system is idle
3285 * and do not have to worry about a thread being inside
3286 * engine->submit_request() as we swap over. So unlike installing
3287 * the nop_submit_request on reset, we can do this from normal
3288 * context and do not require stop_machine().
3289 */
3290 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003291 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003292
3293 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3294 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3295
3296 return true;
3297}
3298
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003299static void
Eric Anholt673a3942008-07-30 12:06:12 -07003300i915_gem_retire_work_handler(struct work_struct *work)
3301{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003302 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003303 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003304 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003305
Chris Wilson891b48c2010-09-29 12:26:37 +01003306 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003307 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003308 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003309 mutex_unlock(&dev->struct_mutex);
3310 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003311
3312 /* Keep the retire handler running until we are finally idle.
3313 * We do not need to do this test under locking as in the worst-case
3314 * we queue the retire worker once too often.
3315 */
Chris Wilsonc9615612016-07-09 10:12:06 +01003316 if (READ_ONCE(dev_priv->gt.awake)) {
3317 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01003318 queue_delayed_work(dev_priv->wq,
3319 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003320 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01003321 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003322}
Chris Wilson891b48c2010-09-29 12:26:37 +01003323
Chris Wilson5427f202017-10-23 22:32:34 +01003324static inline bool
3325new_requests_since_last_retire(const struct drm_i915_private *i915)
3326{
3327 return (READ_ONCE(i915->gt.active_requests) ||
3328 work_pending(&i915->gt.idle_work.work));
3329}
3330
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003331static void
3332i915_gem_idle_work_handler(struct work_struct *work)
3333{
3334 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003335 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson67d97da2016-07-04 08:08:31 +01003336 bool rearm_hangcheck;
Chris Wilson5427f202017-10-23 22:32:34 +01003337 ktime_t end;
Chris Wilson67d97da2016-07-04 08:08:31 +01003338
3339 if (!READ_ONCE(dev_priv->gt.awake))
3340 return;
3341
Imre Deak0cb56702016-11-07 11:20:04 +02003342 /*
3343 * Wait for last execlists context complete, but bail out in case a
3344 * new request is submitted.
3345 */
Chris Wilson5427f202017-10-23 22:32:34 +01003346 end = ktime_add_ms(ktime_get(), 200);
3347 do {
3348 if (new_requests_since_last_retire(dev_priv))
3349 return;
3350
3351 if (intel_engines_are_idle(dev_priv))
3352 break;
3353
3354 usleep_range(100, 500);
3355 } while (ktime_before(ktime_get(), end));
Chris Wilson67d97da2016-07-04 08:08:31 +01003356
3357 rearm_hangcheck =
3358 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3359
Chris Wilson5427f202017-10-23 22:32:34 +01003360 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003361 /* Currently busy, come back later */
3362 mod_delayed_work(dev_priv->wq,
3363 &dev_priv->gt.idle_work,
3364 msecs_to_jiffies(50));
3365 goto out_rearm;
3366 }
3367
Imre Deak93c97dc2016-11-07 11:20:03 +02003368 /*
3369 * New request retired after this work handler started, extend active
3370 * period until next instance of the work.
3371 */
Chris Wilson5427f202017-10-23 22:32:34 +01003372 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003373 goto out_unlock;
3374
Chris Wilson5427f202017-10-23 22:32:34 +01003375 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003376 * Be paranoid and flush a concurrent interrupt to make sure
3377 * we don't reactivate any irq tasklets after parking.
3378 *
3379 * FIXME: Note that even though we have waited for execlists to be idle,
3380 * there may still be an in-flight interrupt even though the CSB
3381 * is now empty. synchronize_irq() makes sure that a residual interrupt
3382 * is completed before we continue, but it doesn't prevent the HW from
3383 * raising a spurious interrupt later. To complete the shield we should
3384 * coordinate disabling the CS irq with flushing the interrupts.
3385 */
3386 synchronize_irq(dev_priv->drm.irq);
3387
Chris Wilsonaba5e272017-10-25 15:39:41 +01003388 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003389 i915_gem_timelines_park(dev_priv);
3390
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003391 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003392
Chris Wilson67d97da2016-07-04 08:08:31 +01003393 GEM_BUG_ON(!dev_priv->gt.awake);
3394 dev_priv->gt.awake = false;
3395 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003396
Chris Wilson67d97da2016-07-04 08:08:31 +01003397 if (INTEL_GEN(dev_priv) >= 6)
3398 gen6_rps_idle(dev_priv);
3399 intel_runtime_pm_put(dev_priv);
3400out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003401 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003402
Chris Wilson67d97da2016-07-04 08:08:31 +01003403out_rearm:
3404 if (rearm_hangcheck) {
3405 GEM_BUG_ON(!dev_priv->gt.awake);
3406 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003407 }
Eric Anholt673a3942008-07-30 12:06:12 -07003408}
3409
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003410void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3411{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003412 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003413 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3414 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003415 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003416
Chris Wilsond1b48c12017-08-16 09:52:08 +01003417 mutex_lock(&i915->drm.struct_mutex);
3418
3419 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3420 struct i915_gem_context *ctx = lut->ctx;
3421 struct i915_vma *vma;
3422
Chris Wilson432295d2017-08-22 12:05:15 +01003423 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003424 if (ctx->file_priv != fpriv)
3425 continue;
3426
3427 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003428 GEM_BUG_ON(vma->obj != obj);
3429
3430 /* We allow the process to have multiple handles to the same
3431 * vma, in the same fd namespace, by virtue of flink/open.
3432 */
3433 GEM_BUG_ON(!vma->open_count);
3434 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003435 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003436
Chris Wilsond1b48c12017-08-16 09:52:08 +01003437 list_del(&lut->obj_link);
3438 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003439
Chris Wilsond1b48c12017-08-16 09:52:08 +01003440 kmem_cache_free(i915->luts, lut);
3441 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003442 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003443
3444 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003445}
3446
Chris Wilsone95433c2016-10-28 13:58:27 +01003447static unsigned long to_wait_timeout(s64 timeout_ns)
3448{
3449 if (timeout_ns < 0)
3450 return MAX_SCHEDULE_TIMEOUT;
3451
3452 if (timeout_ns == 0)
3453 return 0;
3454
3455 return nsecs_to_jiffies_timeout(timeout_ns);
3456}
3457
Ben Widawsky5816d642012-04-11 11:18:19 -07003458/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003459 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003460 * @dev: drm device pointer
3461 * @data: ioctl data blob
3462 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003463 *
3464 * Returns 0 if successful, else an error is returned with the remaining time in
3465 * the timeout parameter.
3466 * -ETIME: object is still busy after timeout
3467 * -ERESTARTSYS: signal interrupted the wait
3468 * -ENONENT: object doesn't exist
3469 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003470 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003471 * -ENOMEM: damn
3472 * -ENODEV: Internal IRQ fail
3473 * -E?: The add request failed
3474 *
3475 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3476 * non-zero timeout parameter the wait ioctl will wait for the given number of
3477 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3478 * without holding struct_mutex the object may become re-busied before this
3479 * function completes. A similar but shorter * race condition exists in the busy
3480 * ioctl
3481 */
3482int
3483i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3484{
3485 struct drm_i915_gem_wait *args = data;
3486 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003487 ktime_t start;
3488 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003489
Daniel Vetter11b5d512014-09-29 15:31:26 +02003490 if (args->flags != 0)
3491 return -EINVAL;
3492
Chris Wilson03ac0642016-07-20 13:31:51 +01003493 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003494 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003495 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003496
Chris Wilsone95433c2016-10-28 13:58:27 +01003497 start = ktime_get();
3498
3499 ret = i915_gem_object_wait(obj,
3500 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3501 to_wait_timeout(args->timeout_ns),
3502 to_rps_client(file));
3503
3504 if (args->timeout_ns > 0) {
3505 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3506 if (args->timeout_ns < 0)
3507 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003508
3509 /*
3510 * Apparently ktime isn't accurate enough and occasionally has a
3511 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3512 * things up to make the test happy. We allow up to 1 jiffy.
3513 *
3514 * This is a regression from the timespec->ktime conversion.
3515 */
3516 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3517 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003518
3519 /* Asked to wait beyond the jiffie/scheduler precision? */
3520 if (ret == -ETIME && args->timeout_ns)
3521 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003522 }
3523
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003524 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003525 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003526}
3527
Chris Wilson73cb9702016-10-28 13:58:46 +01003528static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003529{
Chris Wilson73cb9702016-10-28 13:58:46 +01003530 int ret, i;
3531
3532 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3533 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3534 if (ret)
3535 return ret;
3536 }
3537
3538 return 0;
3539}
3540
Chris Wilson25112b62017-03-30 15:50:39 +01003541static int wait_for_engines(struct drm_i915_private *i915)
3542{
Chris Wilsoncad99462017-08-26 12:09:33 +01003543 if (wait_for(intel_engines_are_idle(i915), 50)) {
3544 DRM_ERROR("Failed to idle engines, declaring wedged!\n");
3545 i915_gem_set_wedged(i915);
3546 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003547 }
3548
3549 return 0;
3550}
3551
Chris Wilson73cb9702016-10-28 13:58:46 +01003552int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3553{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003554 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003555
Chris Wilson863e9fd2017-05-30 13:13:32 +01003556 /* If the device is asleep, we have no requests outstanding */
3557 if (!READ_ONCE(i915->gt.awake))
3558 return 0;
3559
Chris Wilson9caa34a2016-11-11 14:58:08 +00003560 if (flags & I915_WAIT_LOCKED) {
3561 struct i915_gem_timeline *tl;
3562
3563 lockdep_assert_held(&i915->drm.struct_mutex);
3564
3565 list_for_each_entry(tl, &i915->gt.timelines, link) {
3566 ret = wait_for_timeline(tl, flags);
3567 if (ret)
3568 return ret;
3569 }
Chris Wilson72022a72017-03-30 15:50:38 +01003570
3571 i915_gem_retire_requests(i915);
3572 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003573
3574 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003575 } else {
3576 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003577 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003578
Chris Wilson25112b62017-03-30 15:50:39 +01003579 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003580}
3581
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003582static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3583{
Chris Wilsone27ab732017-06-15 13:38:49 +01003584 /*
3585 * We manually flush the CPU domain so that we can override and
3586 * force the flush for the display, and perform it asyncrhonously.
3587 */
3588 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3589 if (obj->cache_dirty)
3590 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003591 obj->base.write_domain = 0;
3592}
3593
3594void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3595{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003596 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003597 return;
3598
3599 mutex_lock(&obj->base.dev->struct_mutex);
3600 __i915_gem_object_flush_for_display(obj);
3601 mutex_unlock(&obj->base.dev->struct_mutex);
3602}
3603
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003604/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003605 * Moves a single object to the WC read, and possibly write domain.
3606 * @obj: object to act on
3607 * @write: ask for write access or read only
3608 *
3609 * This function returns when the move is complete, including waiting on
3610 * flushes to occur.
3611 */
3612int
3613i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3614{
3615 int ret;
3616
3617 lockdep_assert_held(&obj->base.dev->struct_mutex);
3618
3619 ret = i915_gem_object_wait(obj,
3620 I915_WAIT_INTERRUPTIBLE |
3621 I915_WAIT_LOCKED |
3622 (write ? I915_WAIT_ALL : 0),
3623 MAX_SCHEDULE_TIMEOUT,
3624 NULL);
3625 if (ret)
3626 return ret;
3627
3628 if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
3629 return 0;
3630
3631 /* Flush and acquire obj->pages so that we are coherent through
3632 * direct access in memory with previous cached writes through
3633 * shmemfs and that our cache domain tracking remains valid.
3634 * For example, if the obj->filp was moved to swap without us
3635 * being notified and releasing the pages, we would mistakenly
3636 * continue to assume that the obj remained out of the CPU cached
3637 * domain.
3638 */
3639 ret = i915_gem_object_pin_pages(obj);
3640 if (ret)
3641 return ret;
3642
3643 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3644
3645 /* Serialise direct access to this object with the barriers for
3646 * coherent writes from the GPU, by effectively invalidating the
3647 * WC domain upon first access.
3648 */
3649 if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
3650 mb();
3651
3652 /* It should now be out of any other write domains, and we can update
3653 * the domain values for our changes.
3654 */
3655 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3656 obj->base.read_domains |= I915_GEM_DOMAIN_WC;
3657 if (write) {
3658 obj->base.read_domains = I915_GEM_DOMAIN_WC;
3659 obj->base.write_domain = I915_GEM_DOMAIN_WC;
3660 obj->mm.dirty = true;
3661 }
3662
3663 i915_gem_object_unpin_pages(obj);
3664 return 0;
3665}
3666
3667/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003668 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003669 * @obj: object to act on
3670 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003671 *
3672 * This function returns when the move is complete, including waiting on
3673 * flushes to occur.
3674 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003675int
Chris Wilson20217462010-11-23 15:26:33 +00003676i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003677{
Eric Anholte47c68e2008-11-14 13:35:19 -08003678 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003679
Chris Wilsone95433c2016-10-28 13:58:27 +01003680 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003681
Chris Wilsone95433c2016-10-28 13:58:27 +01003682 ret = i915_gem_object_wait(obj,
3683 I915_WAIT_INTERRUPTIBLE |
3684 I915_WAIT_LOCKED |
3685 (write ? I915_WAIT_ALL : 0),
3686 MAX_SCHEDULE_TIMEOUT,
3687 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003688 if (ret)
3689 return ret;
3690
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003691 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3692 return 0;
3693
Chris Wilson43566de2015-01-02 16:29:29 +05303694 /* Flush and acquire obj->pages so that we are coherent through
3695 * direct access in memory with previous cached writes through
3696 * shmemfs and that our cache domain tracking remains valid.
3697 * For example, if the obj->filp was moved to swap without us
3698 * being notified and releasing the pages, we would mistakenly
3699 * continue to assume that the obj remained out of the CPU cached
3700 * domain.
3701 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003702 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303703 if (ret)
3704 return ret;
3705
Chris Wilsonef749212017-04-12 12:01:10 +01003706 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003707
Chris Wilsond0a57782012-10-09 19:24:37 +01003708 /* Serialise direct access to this object with the barriers for
3709 * coherent writes from the GPU, by effectively invalidating the
3710 * GTT domain upon first access.
3711 */
3712 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3713 mb();
3714
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003715 /* It should now be out of any other write domains, and we can update
3716 * the domain values for our changes.
3717 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003718 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003719 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003720 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003721 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3722 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003723 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003724 }
3725
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003726 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003727 return 0;
3728}
3729
Chris Wilsonef55f922015-10-09 14:11:27 +01003730/**
3731 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003732 * @obj: object to act on
3733 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003734 *
3735 * After this function returns, the object will be in the new cache-level
3736 * across all GTT and the contents of the backing storage will be coherent,
3737 * with respect to the new cache-level. In order to keep the backing storage
3738 * coherent for all users, we only allow a single cache level to be set
3739 * globally on the object and prevent it from being changed whilst the
3740 * hardware is reading from the object. That is if the object is currently
3741 * on the scanout it will be set to uncached (or equivalent display
3742 * cache coherency) and all non-MOCS GPU access will also be uncached so
3743 * that all direct access to the scanout remains coherent.
3744 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003745int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3746 enum i915_cache_level cache_level)
3747{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003748 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003749 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003750
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003751 lockdep_assert_held(&obj->base.dev->struct_mutex);
3752
Chris Wilsone4ffd172011-04-04 09:44:39 +01003753 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003754 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003755
Chris Wilsonef55f922015-10-09 14:11:27 +01003756 /* Inspect the list of currently bound VMA and unbind any that would
3757 * be invalid given the new cache-level. This is principally to
3758 * catch the issue of the CS prefetch crossing page boundaries and
3759 * reading an invalid PTE on older architectures.
3760 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003761restart:
3762 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003763 if (!drm_mm_node_allocated(&vma->node))
3764 continue;
3765
Chris Wilson20dfbde2016-08-04 16:32:30 +01003766 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003767 DRM_DEBUG("can not change the cache level of pinned objects\n");
3768 return -EBUSY;
3769 }
3770
Chris Wilson010e3e62017-12-06 12:49:13 +00003771 if (!i915_vma_is_closed(vma) &&
3772 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003773 continue;
3774
3775 ret = i915_vma_unbind(vma);
3776 if (ret)
3777 return ret;
3778
3779 /* As unbinding may affect other elements in the
3780 * obj->vma_list (due to side-effects from retiring
3781 * an active vma), play safe and restart the iterator.
3782 */
3783 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003784 }
3785
Chris Wilsonef55f922015-10-09 14:11:27 +01003786 /* We can reuse the existing drm_mm nodes but need to change the
3787 * cache-level on the PTE. We could simply unbind them all and
3788 * rebind with the correct cache-level on next use. However since
3789 * we already have a valid slot, dma mapping, pages etc, we may as
3790 * rewrite the PTE in the belief that doing so tramples upon less
3791 * state and so involves less work.
3792 */
Chris Wilson15717de2016-08-04 07:52:26 +01003793 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003794 /* Before we change the PTE, the GPU must not be accessing it.
3795 * If we wait upon the object, we know that all the bound
3796 * VMA are no longer active.
3797 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003798 ret = i915_gem_object_wait(obj,
3799 I915_WAIT_INTERRUPTIBLE |
3800 I915_WAIT_LOCKED |
3801 I915_WAIT_ALL,
3802 MAX_SCHEDULE_TIMEOUT,
3803 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003804 if (ret)
3805 return ret;
3806
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003807 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3808 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003809 /* Access to snoopable pages through the GTT is
3810 * incoherent and on some machines causes a hard
3811 * lockup. Relinquish the CPU mmaping to force
3812 * userspace to refault in the pages and we can
3813 * then double check if the GTT mapping is still
3814 * valid for that pointer access.
3815 */
3816 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003817
Chris Wilsonef55f922015-10-09 14:11:27 +01003818 /* As we no longer need a fence for GTT access,
3819 * we can relinquish it now (and so prevent having
3820 * to steal a fence from someone else on the next
3821 * fence request). Note GPU activity would have
3822 * dropped the fence as all snoopable access is
3823 * supposed to be linear.
3824 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003825 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3826 ret = i915_vma_put_fence(vma);
3827 if (ret)
3828 return ret;
3829 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003830 } else {
3831 /* We either have incoherent backing store and
3832 * so no GTT access or the architecture is fully
3833 * coherent. In such cases, existing GTT mmaps
3834 * ignore the cache bit in the PTE and we can
3835 * rewrite it without confusing the GPU or having
3836 * to force userspace to fault back in its mmaps.
3837 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003838 }
3839
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003840 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003841 if (!drm_mm_node_allocated(&vma->node))
3842 continue;
3843
3844 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3845 if (ret)
3846 return ret;
3847 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003848 }
3849
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003850 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003851 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003852 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003853 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003854
Chris Wilsone4ffd172011-04-04 09:44:39 +01003855 return 0;
3856}
3857
Ben Widawsky199adf42012-09-21 17:01:20 -07003858int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3859 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003860{
Ben Widawsky199adf42012-09-21 17:01:20 -07003861 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003862 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003863 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003864
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003865 rcu_read_lock();
3866 obj = i915_gem_object_lookup_rcu(file, args->handle);
3867 if (!obj) {
3868 err = -ENOENT;
3869 goto out;
3870 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003871
Chris Wilson651d7942013-08-08 14:41:10 +01003872 switch (obj->cache_level) {
3873 case I915_CACHE_LLC:
3874 case I915_CACHE_L3_LLC:
3875 args->caching = I915_CACHING_CACHED;
3876 break;
3877
Chris Wilson4257d3b2013-08-08 14:41:11 +01003878 case I915_CACHE_WT:
3879 args->caching = I915_CACHING_DISPLAY;
3880 break;
3881
Chris Wilson651d7942013-08-08 14:41:10 +01003882 default:
3883 args->caching = I915_CACHING_NONE;
3884 break;
3885 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003886out:
3887 rcu_read_unlock();
3888 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003889}
3890
Ben Widawsky199adf42012-09-21 17:01:20 -07003891int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3892 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003893{
Chris Wilson9c870d02016-10-24 13:42:15 +01003894 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003895 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003896 struct drm_i915_gem_object *obj;
3897 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003898 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003899
Ben Widawsky199adf42012-09-21 17:01:20 -07003900 switch (args->caching) {
3901 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003902 level = I915_CACHE_NONE;
3903 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003904 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003905 /*
3906 * Due to a HW issue on BXT A stepping, GPU stores via a
3907 * snooped mapping may leave stale data in a corresponding CPU
3908 * cacheline, whereas normally such cachelines would get
3909 * invalidated.
3910 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003911 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003912 return -ENODEV;
3913
Chris Wilsone6994ae2012-07-10 10:27:08 +01003914 level = I915_CACHE_LLC;
3915 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003916 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003917 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003918 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003919 default:
3920 return -EINVAL;
3921 }
3922
Chris Wilsond65415d2017-01-19 08:22:10 +00003923 obj = i915_gem_object_lookup(file, args->handle);
3924 if (!obj)
3925 return -ENOENT;
3926
Tina Zhanga03f3952017-11-14 10:25:13 +00003927 /*
3928 * The caching mode of proxy object is handled by its generator, and
3929 * not allowed to be changed by userspace.
3930 */
3931 if (i915_gem_object_is_proxy(obj)) {
3932 ret = -ENXIO;
3933 goto out;
3934 }
3935
Chris Wilsond65415d2017-01-19 08:22:10 +00003936 if (obj->cache_level == level)
3937 goto out;
3938
3939 ret = i915_gem_object_wait(obj,
3940 I915_WAIT_INTERRUPTIBLE,
3941 MAX_SCHEDULE_TIMEOUT,
3942 to_rps_client(file));
3943 if (ret)
3944 goto out;
3945
Ben Widawsky3bc29132012-09-26 16:15:20 -07003946 ret = i915_mutex_lock_interruptible(dev);
3947 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003948 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003949
3950 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003951 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003952
3953out:
3954 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003955 return ret;
3956}
3957
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003958/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003959 * Prepare buffer for display plane (scanout, cursors, etc).
3960 * Can be called from an uninterruptible phase (modesetting) and allows
3961 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003962 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003963struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003964i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3965 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003966 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003967{
Chris Wilson058d88c2016-08-15 10:49:06 +01003968 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003969 int ret;
3970
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003971 lockdep_assert_held(&obj->base.dev->struct_mutex);
3972
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003973 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01003974 * display coherency whilst setting up the cache domains.
3975 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003976 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003977
Eric Anholta7ef0642011-03-29 16:59:54 -07003978 /* The display engine is not coherent with the LLC cache on gen6. As
3979 * a result, we make sure that the pinning that is about to occur is
3980 * done with uncached PTEs. This is lowest common denominator for all
3981 * chipsets.
3982 *
3983 * However for gen6+, we could do better by using the GFDT bit instead
3984 * of uncaching, which would allow us to flush all the LLC-cached data
3985 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3986 */
Chris Wilson651d7942013-08-08 14:41:10 +01003987 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003988 HAS_WT(to_i915(obj->base.dev)) ?
3989 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003990 if (ret) {
3991 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003992 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01003993 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003994
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003995 /* As the user may map the buffer once pinned in the display plane
3996 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003997 * always use map_and_fenceable for all scanout buffers. However,
3998 * it may simply be too big to fit into mappable, in which case
3999 * put it anyway and hope that userspace can cope (but always first
4000 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004001 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004002 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00004003 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01004004 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
4005 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00004006 if (IS_ERR(vma)) {
4007 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4008 unsigned int flags;
4009
4010 /* Valleyview is definitely limited to scanning out the first
4011 * 512MiB. Lets presume this behaviour was inherited from the
4012 * g4x display engine and that all earlier gen are similarly
4013 * limited. Testing suggests that it is a little more
4014 * complicated than this. For example, Cherryview appears quite
4015 * happy to scanout from anywhere within its global aperture.
4016 */
4017 flags = 0;
4018 if (HAS_GMCH_DISPLAY(i915))
4019 flags = PIN_MAPPABLE;
4020 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
4021 }
Chris Wilson058d88c2016-08-15 10:49:06 +01004022 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004023 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004024
Chris Wilsond8923dc2016-08-18 17:17:07 +01004025 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4026
Chris Wilsona6a7cc42016-11-18 21:17:46 +00004027 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004028 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00004029 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004030
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004031 /* It should now be out of any other write domains, and we can update
4032 * the domain values for our changes.
4033 */
Chris Wilson05394f32010-11-08 19:18:58 +00004034 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004035
Chris Wilson058d88c2016-08-15 10:49:06 +01004036 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004037
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004038err_unpin_global:
4039 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004040 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004041}
4042
4043void
Chris Wilson058d88c2016-08-15 10:49:06 +01004044i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004045{
Chris Wilson49d73912016-11-29 09:50:08 +00004046 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004047
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004048 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004049 return;
4050
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004051 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004052 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004053
Chris Wilson383d5822016-08-18 17:17:08 +01004054 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004055 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004056
Chris Wilson058d88c2016-08-15 10:49:06 +01004057 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004058}
4059
Eric Anholte47c68e2008-11-14 13:35:19 -08004060/**
4061 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004062 * @obj: object to act on
4063 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004064 *
4065 * This function returns when the move is complete, including waiting on
4066 * flushes to occur.
4067 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004068int
Chris Wilson919926a2010-11-12 13:42:53 +00004069i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004070{
Eric Anholte47c68e2008-11-14 13:35:19 -08004071 int ret;
4072
Chris Wilsone95433c2016-10-28 13:58:27 +01004073 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004074
Chris Wilsone95433c2016-10-28 13:58:27 +01004075 ret = i915_gem_object_wait(obj,
4076 I915_WAIT_INTERRUPTIBLE |
4077 I915_WAIT_LOCKED |
4078 (write ? I915_WAIT_ALL : 0),
4079 MAX_SCHEDULE_TIMEOUT,
4080 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004081 if (ret)
4082 return ret;
4083
Chris Wilsonef749212017-04-12 12:01:10 +01004084 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004085
Eric Anholte47c68e2008-11-14 13:35:19 -08004086 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00004087 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004088 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00004089 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004090 }
4091
4092 /* It should now be out of any other write domains, and we can update
4093 * the domain values for our changes.
4094 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004095 GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004096
4097 /* If we're writing through the CPU, then the GPU read domains will
4098 * need to be invalidated at next use.
4099 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004100 if (write)
4101 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004102
4103 return 0;
4104}
4105
Eric Anholt673a3942008-07-30 12:06:12 -07004106/* Throttle our rendering by waiting until the ring has completed our requests
4107 * emitted over 20 msec ago.
4108 *
Eric Anholtb9624422009-06-03 07:27:35 +00004109 * Note that if we were to use the current jiffies each time around the loop,
4110 * we wouldn't escape the function with any frames outstanding if the time to
4111 * render a frame was over 20ms.
4112 *
Eric Anholt673a3942008-07-30 12:06:12 -07004113 * This should get us reasonable parallelism between CPU and GPU but also
4114 * relatively low latency when blocking on a particular request to finish.
4115 */
4116static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004117i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004118{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004119 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004120 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004121 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00004122 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004123 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004124
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004125 /* ABI: return -EIO if already wedged */
4126 if (i915_terminally_wedged(&dev_priv->gpu_error))
4127 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004128
Chris Wilson1c255952010-09-26 11:03:27 +01004129 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004130 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004131 if (time_after_eq(request->emitted_jiffies, recent_enough))
4132 break;
4133
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004134 if (target) {
4135 list_del(&target->client_link);
4136 target->file_priv = NULL;
4137 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004138
John Harrison54fb2412014-11-24 18:49:27 +00004139 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004140 }
John Harrisonff865882014-11-24 18:49:28 +00004141 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01004142 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004143 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004144
John Harrison54fb2412014-11-24 18:49:27 +00004145 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004146 return 0;
4147
Chris Wilsone95433c2016-10-28 13:58:27 +01004148 ret = i915_wait_request(target,
4149 I915_WAIT_INTERRUPTIBLE,
4150 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01004151 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004152
Chris Wilsone95433c2016-10-28 13:58:27 +01004153 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004154}
4155
Chris Wilson058d88c2016-08-15 10:49:06 +01004156struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004157i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4158 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004159 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004160 u64 alignment,
4161 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004162{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004163 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4164 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004165 struct i915_vma *vma;
4166 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004167
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004168 lockdep_assert_held(&obj->base.dev->struct_mutex);
4169
Chris Wilson43ae70d2017-10-09 09:44:01 +01004170 if (!view && flags & PIN_MAPPABLE) {
4171 /* If the required space is larger than the available
4172 * aperture, we will not able to find a slot for the
4173 * object and unbinding the object now will be in
4174 * vain. Worse, doing so may cause us to ping-pong
4175 * the object in and out of the Global GTT and
4176 * waste a lot of cycles under the mutex.
4177 */
4178 if (obj->base.size > dev_priv->ggtt.mappable_end)
4179 return ERR_PTR(-E2BIG);
4180
4181 /* If NONBLOCK is set the caller is optimistically
4182 * trying to cache the full object within the mappable
4183 * aperture, and *must* have a fallback in place for
4184 * situations where we cannot bind the object. We
4185 * can be a little more lax here and use the fallback
4186 * more often to avoid costly migrations of ourselves
4187 * and other objects within the aperture.
4188 *
4189 * Half-the-aperture is used as a simple heuristic.
4190 * More interesting would to do search for a free
4191 * block prior to making the commitment to unbind.
4192 * That caters for the self-harm case, and with a
4193 * little more heuristics (e.g. NOFAULT, NOEVICT)
4194 * we could try to minimise harm to others.
4195 */
4196 if (flags & PIN_NONBLOCK &&
4197 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4198 return ERR_PTR(-ENOSPC);
4199 }
4200
Chris Wilson718659a2017-01-16 15:21:28 +00004201 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004202 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004203 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004204
4205 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004206 if (flags & PIN_NONBLOCK) {
4207 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4208 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004209
Chris Wilson43ae70d2017-10-09 09:44:01 +01004210 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004211 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004212 return ERR_PTR(-ENOSPC);
4213 }
4214
Chris Wilson59bfa122016-08-04 16:32:31 +01004215 WARN(i915_vma_is_pinned(vma),
4216 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004217 " offset=%08x, req.alignment=%llx,"
4218 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4219 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004220 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004221 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004222 ret = i915_vma_unbind(vma);
4223 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004224 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004225 }
4226
Chris Wilson058d88c2016-08-15 10:49:06 +01004227 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4228 if (ret)
4229 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004230
Chris Wilson058d88c2016-08-15 10:49:06 +01004231 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004232}
4233
Chris Wilsonedf6b762016-08-09 09:23:33 +01004234static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004235{
4236 /* Note that we could alias engines in the execbuf API, but
4237 * that would be very unwise as it prevents userspace from
4238 * fine control over engine selection. Ahem.
4239 *
4240 * This should be something like EXEC_MAX_ENGINE instead of
4241 * I915_NUM_ENGINES.
4242 */
4243 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4244 return 0x10000 << id;
4245}
4246
4247static __always_inline unsigned int __busy_write_id(unsigned int id)
4248{
Chris Wilson70cb4722016-08-09 18:08:25 +01004249 /* The uABI guarantees an active writer is also amongst the read
4250 * engines. This would be true if we accessed the activity tracking
4251 * under the lock, but as we perform the lookup of the object and
4252 * its activity locklessly we can not guarantee that the last_write
4253 * being active implies that we have set the same engine flag from
4254 * last_read - hence we always set both read and write busy for
4255 * last_write.
4256 */
4257 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004258}
4259
Chris Wilsonedf6b762016-08-09 09:23:33 +01004260static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004261__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004262 unsigned int (*flag)(unsigned int id))
4263{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004264 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004265
Chris Wilsond07f0e52016-10-28 13:58:44 +01004266 /* We have to check the current hw status of the fence as the uABI
4267 * guarantees forward progress. We could rely on the idle worker
4268 * to eventually flush us, but to minimise latency just ask the
4269 * hardware.
4270 *
4271 * Note we only report on the status of native fences.
4272 */
4273 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004274 return 0;
4275
Chris Wilsond07f0e52016-10-28 13:58:44 +01004276 /* opencode to_request() in order to avoid const warnings */
4277 rq = container_of(fence, struct drm_i915_gem_request, fence);
4278 if (i915_gem_request_completed(rq))
4279 return 0;
4280
Chris Wilson1d39f282017-04-11 13:43:06 +01004281 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004282}
4283
Chris Wilsonedf6b762016-08-09 09:23:33 +01004284static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004285busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004286{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004287 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004288}
4289
Chris Wilsonedf6b762016-08-09 09:23:33 +01004290static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004291busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004292{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004293 if (!fence)
4294 return 0;
4295
4296 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004297}
4298
Eric Anholt673a3942008-07-30 12:06:12 -07004299int
Eric Anholt673a3942008-07-30 12:06:12 -07004300i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004301 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004302{
4303 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004304 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004305 struct reservation_object_list *list;
4306 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004307 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004308
Chris Wilsond07f0e52016-10-28 13:58:44 +01004309 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004310 rcu_read_lock();
4311 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004312 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004313 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004314
4315 /* A discrepancy here is that we do not report the status of
4316 * non-i915 fences, i.e. even though we may report the object as idle,
4317 * a call to set-domain may still stall waiting for foreign rendering.
4318 * This also means that wait-ioctl may report an object as busy,
4319 * where busy-ioctl considers it idle.
4320 *
4321 * We trade the ability to warn of foreign fences to report on which
4322 * i915 engines are active for the object.
4323 *
4324 * Alternatively, we can trade that extra information on read/write
4325 * activity with
4326 * args->busy =
4327 * !reservation_object_test_signaled_rcu(obj->resv, true);
4328 * to report the overall busyness. This is what the wait-ioctl does.
4329 *
4330 */
4331retry:
4332 seq = raw_read_seqcount(&obj->resv->seq);
4333
4334 /* Translate the exclusive fence to the READ *and* WRITE engine */
4335 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4336
4337 /* Translate shared fences to READ set of engines */
4338 list = rcu_dereference(obj->resv->fence);
4339 if (list) {
4340 unsigned int shared_count = list->shared_count, i;
4341
4342 for (i = 0; i < shared_count; ++i) {
4343 struct dma_fence *fence =
4344 rcu_dereference(list->shared[i]);
4345
4346 args->busy |= busy_check_reader(fence);
4347 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004348 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004349
Chris Wilsond07f0e52016-10-28 13:58:44 +01004350 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4351 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004352
Chris Wilsond07f0e52016-10-28 13:58:44 +01004353 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004354out:
4355 rcu_read_unlock();
4356 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004357}
4358
4359int
4360i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4361 struct drm_file *file_priv)
4362{
Akshay Joshi0206e352011-08-16 15:34:10 -04004363 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004364}
4365
Chris Wilson3ef94da2009-09-14 16:50:29 +01004366int
4367i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4368 struct drm_file *file_priv)
4369{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004370 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004371 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004372 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004373 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004374
4375 switch (args->madv) {
4376 case I915_MADV_DONTNEED:
4377 case I915_MADV_WILLNEED:
4378 break;
4379 default:
4380 return -EINVAL;
4381 }
4382
Chris Wilson03ac0642016-07-20 13:31:51 +01004383 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004384 if (!obj)
4385 return -ENOENT;
4386
4387 err = mutex_lock_interruptible(&obj->mm.lock);
4388 if (err)
4389 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004390
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004391 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004392 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004393 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004394 if (obj->mm.madv == I915_MADV_WILLNEED) {
4395 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004396 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004397 obj->mm.quirked = false;
4398 }
4399 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004400 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004401 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004402 obj->mm.quirked = true;
4403 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004404 }
4405
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004406 if (obj->mm.madv != __I915_MADV_PURGED)
4407 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004408
Chris Wilson6c085a72012-08-20 11:40:46 +02004409 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004410 if (obj->mm.madv == I915_MADV_DONTNEED &&
4411 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004412 i915_gem_object_truncate(obj);
4413
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004414 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004415 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004416
Chris Wilson1233e2d2016-10-28 13:58:37 +01004417out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004418 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004419 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004420}
4421
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004422static void
4423frontbuffer_retire(struct i915_gem_active *active,
4424 struct drm_i915_gem_request *request)
4425{
4426 struct drm_i915_gem_object *obj =
4427 container_of(active, typeof(*obj), frontbuffer_write);
4428
Chris Wilsond59b21e2017-02-22 11:40:49 +00004429 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004430}
4431
Chris Wilson37e680a2012-06-07 15:38:42 +01004432void i915_gem_object_init(struct drm_i915_gem_object *obj,
4433 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004434{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004435 mutex_init(&obj->mm.lock);
4436
Ben Widawsky2f633152013-07-17 12:19:03 -07004437 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004438 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004439 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004440
Chris Wilson37e680a2012-06-07 15:38:42 +01004441 obj->ops = ops;
4442
Chris Wilsond07f0e52016-10-28 13:58:44 +01004443 reservation_object_init(&obj->__builtin_resv);
4444 obj->resv = &obj->__builtin_resv;
4445
Chris Wilson50349242016-08-18 17:17:04 +01004446 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004447 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004448
4449 obj->mm.madv = I915_MADV_WILLNEED;
4450 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4451 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004452
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004453 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004454}
4455
Chris Wilson37e680a2012-06-07 15:38:42 +01004456static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004457 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4458 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004459
Chris Wilson37e680a2012-06-07 15:38:42 +01004460 .get_pages = i915_gem_object_get_pages_gtt,
4461 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004462
4463 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004464};
4465
Matthew Auld465c4032017-10-06 23:18:14 +01004466static int i915_gem_object_create_shmem(struct drm_device *dev,
4467 struct drm_gem_object *obj,
4468 size_t size)
4469{
4470 struct drm_i915_private *i915 = to_i915(dev);
4471 unsigned long flags = VM_NORESERVE;
4472 struct file *filp;
4473
4474 drm_gem_private_object_init(dev, obj, size);
4475
4476 if (i915->mm.gemfs)
4477 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4478 flags);
4479 else
4480 filp = shmem_file_setup("i915", size, flags);
4481
4482 if (IS_ERR(filp))
4483 return PTR_ERR(filp);
4484
4485 obj->filp = filp;
4486
4487 return 0;
4488}
4489
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004490struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004491i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004492{
Daniel Vetterc397b902010-04-09 19:05:07 +00004493 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004494 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004495 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004496 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004497 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004498
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004499 /* There is a prevalence of the assumption that we fit the object's
4500 * page count inside a 32bit _signed_ variable. Let's document this and
4501 * catch if we ever need to fix it. In the meantime, if you do spot
4502 * such a local variable, please consider fixing!
4503 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004504 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004505 return ERR_PTR(-E2BIG);
4506
4507 if (overflows_type(size, obj->base.size))
4508 return ERR_PTR(-E2BIG);
4509
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004510 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004511 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004512 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004513
Matthew Auld465c4032017-10-06 23:18:14 +01004514 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004515 if (ret)
4516 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004517
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004518 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004519 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004520 /* 965gm cannot relocate objects above 4GiB. */
4521 mask &= ~__GFP_HIGHMEM;
4522 mask |= __GFP_DMA32;
4523 }
4524
Al Viro93c76a32015-12-04 23:45:44 -05004525 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004526 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004527 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004528
Chris Wilson37e680a2012-06-07 15:38:42 +01004529 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004530
Daniel Vetterc397b902010-04-09 19:05:07 +00004531 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4532 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4533
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004534 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004535 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004536 * cache) for about a 10% performance improvement
4537 * compared to uncached. Graphics requests other than
4538 * display scanout are coherent with the CPU in
4539 * accessing this cache. This means in this mode we
4540 * don't need to clflush on the CPU side, and on the
4541 * GPU side we only need to flush internal caches to
4542 * get data visible to the CPU.
4543 *
4544 * However, we maintain the display planes as UC, and so
4545 * need to rebind when first used as such.
4546 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004547 cache_level = I915_CACHE_LLC;
4548 else
4549 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004550
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004551 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004552
Daniel Vetterd861e332013-07-24 23:25:03 +02004553 trace_i915_gem_object_create(obj);
4554
Chris Wilson05394f32010-11-08 19:18:58 +00004555 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004556
4557fail:
4558 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004559 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004560}
4561
Chris Wilson340fbd82014-05-22 09:16:52 +01004562static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4563{
4564 /* If we are the last user of the backing storage (be it shmemfs
4565 * pages or stolen etc), we know that the pages are going to be
4566 * immediately released. In this case, we can then skip copying
4567 * back the contents from the GPU.
4568 */
4569
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004570 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004571 return false;
4572
4573 if (obj->base.filp == NULL)
4574 return true;
4575
4576 /* At first glance, this looks racy, but then again so would be
4577 * userspace racing mmap against close. However, the first external
4578 * reference to the filp can only be obtained through the
4579 * i915_gem_mmap_ioctl() which safeguards us against the user
4580 * acquiring such a reference whilst we are in the middle of
4581 * freeing the object.
4582 */
4583 return atomic_long_read(&obj->base.filp->f_count) == 1;
4584}
4585
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004586static void __i915_gem_free_objects(struct drm_i915_private *i915,
4587 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004588{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004589 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004590
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004591 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004592 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004593 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004594
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004595 trace_i915_gem_object_destroy(obj);
4596
Chris Wilsoncc731f52017-10-13 21:26:21 +01004597 mutex_lock(&i915->drm.struct_mutex);
4598
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004599 GEM_BUG_ON(i915_gem_object_is_active(obj));
4600 list_for_each_entry_safe(vma, vn,
4601 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004602 GEM_BUG_ON(i915_vma_is_active(vma));
4603 vma->flags &= ~I915_VMA_PIN_MASK;
4604 i915_vma_close(vma);
4605 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004606 GEM_BUG_ON(!list_empty(&obj->vma_list));
4607 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004608
Chris Wilsonf2123812017-10-16 12:40:37 +01004609 /* This serializes freeing with the shrinker. Since the free
4610 * is delayed, first by RCU then by the workqueue, we want the
4611 * shrinker to be able to free pages of unreferenced objects,
4612 * or else we may oom whilst there are plenty of deferred
4613 * freed objects.
4614 */
4615 if (i915_gem_object_has_pages(obj)) {
4616 spin_lock(&i915->mm.obj_lock);
4617 list_del_init(&obj->mm.link);
4618 spin_unlock(&i915->mm.obj_lock);
4619 }
4620
Chris Wilsoncc731f52017-10-13 21:26:21 +01004621 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004622
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004623 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004624 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004625 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004626 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004627
4628 if (obj->ops->release)
4629 obj->ops->release(obj);
4630
4631 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4632 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004633 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004634 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004635
4636 if (obj->base.import_attach)
4637 drm_prime_gem_destroy(&obj->base, NULL);
4638
Chris Wilsond07f0e52016-10-28 13:58:44 +01004639 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004640 drm_gem_object_release(&obj->base);
4641 i915_gem_info_remove_obj(i915, obj->base.size);
4642
4643 kfree(obj->bit_17);
4644 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004645
4646 if (on)
4647 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004648 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004649 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004650}
4651
4652static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4653{
4654 struct llist_node *freed;
4655
Chris Wilson87701b42017-10-13 21:26:20 +01004656 /* Free the oldest, most stale object to keep the free_list short */
4657 freed = NULL;
4658 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4659 /* Only one consumer of llist_del_first() allowed */
4660 spin_lock(&i915->mm.free_lock);
4661 freed = llist_del_first(&i915->mm.free_list);
4662 spin_unlock(&i915->mm.free_lock);
4663 }
4664 if (unlikely(freed)) {
4665 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004666 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004667 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004668}
4669
4670static void __i915_gem_free_work(struct work_struct *work)
4671{
4672 struct drm_i915_private *i915 =
4673 container_of(work, struct drm_i915_private, mm.free_work);
4674 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004675
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004676 /* All file-owned VMA should have been released by this point through
4677 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4678 * However, the object may also be bound into the global GTT (e.g.
4679 * older GPUs without per-process support, or for direct access through
4680 * the GTT either for the user or for scanout). Those VMA still need to
4681 * unbound now.
4682 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004683
Chris Wilsonf991c492017-11-06 11:15:08 +00004684 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004685 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004686 spin_unlock(&i915->mm.free_lock);
4687
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004688 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004689 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004690 return;
4691
4692 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004693 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004694 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004695}
4696
4697static void __i915_gem_free_object_rcu(struct rcu_head *head)
4698{
4699 struct drm_i915_gem_object *obj =
4700 container_of(head, typeof(*obj), rcu);
4701 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4702
4703 /* We can't simply use call_rcu() from i915_gem_free_object()
4704 * as we need to block whilst unbinding, and the call_rcu
4705 * task may be called from softirq context. So we take a
4706 * detour through a worker.
4707 */
4708 if (llist_add(&obj->freed, &i915->mm.free_list))
4709 schedule_work(&i915->mm.free_work);
4710}
4711
4712void i915_gem_free_object(struct drm_gem_object *gem_obj)
4713{
4714 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4715
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004716 if (obj->mm.quirked)
4717 __i915_gem_object_unpin_pages(obj);
4718
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004719 if (discard_backing_storage(obj))
4720 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004721
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004722 /* Before we free the object, make sure any pure RCU-only
4723 * read-side critical sections are complete, e.g.
4724 * i915_gem_busy_ioctl(). For the corresponding synchronized
4725 * lookup see i915_gem_object_lookup_rcu().
4726 */
4727 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004728}
4729
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004730void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4731{
4732 lockdep_assert_held(&obj->base.dev->struct_mutex);
4733
Chris Wilsond1b48c12017-08-16 09:52:08 +01004734 if (!i915_gem_object_has_active_reference(obj) &&
4735 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004736 i915_gem_object_set_active_reference(obj);
4737 else
4738 i915_gem_object_put(obj);
4739}
4740
Chris Wilsonae6c4572017-11-10 14:26:28 +00004741static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004742{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004743 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004744 struct intel_engine_cs *engine;
4745 enum intel_engine_id id;
4746
Chris Wilsonae6c4572017-11-10 14:26:28 +00004747 for_each_engine(engine, i915, id) {
4748 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4749 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4750 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004751}
4752
Chris Wilson24145512017-01-24 11:01:35 +00004753void i915_gem_sanitize(struct drm_i915_private *i915)
4754{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004755 if (i915_terminally_wedged(&i915->gpu_error)) {
4756 mutex_lock(&i915->drm.struct_mutex);
4757 i915_gem_unset_wedged(i915);
4758 mutex_unlock(&i915->drm.struct_mutex);
4759 }
4760
Chris Wilson24145512017-01-24 11:01:35 +00004761 /*
4762 * If we inherit context state from the BIOS or earlier occupants
4763 * of the GPU, the GPU may be in an inconsistent state when we
4764 * try to take over. The only way to remove the earlier state
4765 * is by resetting. However, resetting on earlier gen is tricky as
4766 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004767 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004768 */
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004769 if (INTEL_GEN(i915) >= 5) {
Chris Wilson24145512017-01-24 11:01:35 +00004770 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4771 WARN_ON(reset && reset != -ENODEV);
4772 }
4773}
4774
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004775int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004776{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004777 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004778 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004779
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004780 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004781 intel_suspend_gt_powersave(dev_priv);
4782
Chris Wilson45c5f202013-10-16 11:50:01 +01004783 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004784
4785 /* We have to flush all the executing contexts to main memory so
4786 * that they can saved in the hibernation image. To ensure the last
4787 * context image is coherent, we have to switch away from it. That
4788 * leaves the dev_priv->kernel_context still active when
4789 * we actually suspend, and its image in memory may not match the GPU
4790 * state. Fortunately, the kernel_context is disposable and we do
4791 * not rely on its state.
4792 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004793 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4794 ret = i915_gem_switch_to_kernel_context(dev_priv);
4795 if (ret)
4796 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004797
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004798 ret = i915_gem_wait_for_idle(dev_priv,
4799 I915_WAIT_INTERRUPTIBLE |
4800 I915_WAIT_LOCKED);
4801 if (ret && ret != -EIO)
4802 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004803
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004804 assert_kernel_context_is_current(dev_priv);
4805 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004806 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004807 mutex_unlock(&dev->struct_mutex);
4808
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304809 intel_guc_suspend(dev_priv);
4810
Chris Wilson737b1502015-01-26 18:03:03 +02004811 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004812 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004813
4814 /* As the idle_work is rearming if it detects a race, play safe and
4815 * repeat the flush until it is definitely idle.
4816 */
Chris Wilson7c262402017-10-06 11:40:38 +01004817 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004818
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004819 /* Assert that we sucessfully flushed all the work and
4820 * reset the GPU back to its idle, low power state.
4821 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004822 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004823 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4824 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004825
Imre Deak1c777c52016-10-12 17:46:37 +03004826 /*
4827 * Neither the BIOS, ourselves or any other kernel
4828 * expects the system to be in execlists mode on startup,
4829 * so we need to reset the GPU back to legacy mode. And the only
4830 * known way to disable logical contexts is through a GPU reset.
4831 *
4832 * So in order to leave the system in a known default configuration,
4833 * always reset the GPU upon unload and suspend. Afterwards we then
4834 * clean up the GEM state tracking, flushing off the requests and
4835 * leaving the system in a known idle state.
4836 *
4837 * Note that is of the upmost importance that the GPU is idle and
4838 * all stray writes are flushed *before* we dismantle the backing
4839 * storage for the pinned objects.
4840 *
4841 * However, since we are uncertain that resetting the GPU on older
4842 * machines is a good idea, we don't - just in case it leaves the
4843 * machine in an unusable condition.
4844 */
Chris Wilson24145512017-01-24 11:01:35 +00004845 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004846
4847 intel_runtime_pm_put(dev_priv);
4848 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004849
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004850err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004851 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004852 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004853 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004854}
4855
Chris Wilson37cd3302017-11-12 11:27:38 +00004856void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004857{
Chris Wilson37cd3302017-11-12 11:27:38 +00004858 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004859
Chris Wilson37cd3302017-11-12 11:27:38 +00004860 mutex_lock(&i915->drm.struct_mutex);
4861 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02004862
Chris Wilson37cd3302017-11-12 11:27:38 +00004863 i915_gem_restore_gtt_mappings(i915);
4864 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004865
4866 /* As we didn't flush the kernel context before suspend, we cannot
4867 * guarantee that the context image is complete. So let's just reset
4868 * it and start again.
4869 */
Chris Wilson37cd3302017-11-12 11:27:38 +00004870 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004871
Chris Wilson37cd3302017-11-12 11:27:38 +00004872 if (i915_gem_init_hw(i915))
4873 goto err_wedged;
4874
Chris Wilson7469c622017-11-14 13:03:00 +00004875 intel_guc_resume(i915);
4876
Chris Wilson37cd3302017-11-12 11:27:38 +00004877 /* Always reload a context for powersaving. */
4878 if (i915_gem_switch_to_kernel_context(i915))
4879 goto err_wedged;
4880
4881out_unlock:
4882 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
4883 mutex_unlock(&i915->drm.struct_mutex);
4884 return;
4885
4886err_wedged:
4887 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
4888 i915_gem_set_wedged(i915);
4889 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004890}
4891
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004892void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004893{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004894 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004895 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4896 return;
4897
4898 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4899 DISP_TILE_SURFACE_SWIZZLING);
4900
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004901 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004902 return;
4903
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004904 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004905 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004906 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004907 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004908 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004909 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004910 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004911 else
4912 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004913}
Daniel Vettere21af882012-02-09 20:53:27 +01004914
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004915static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004916{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004917 I915_WRITE(RING_CTL(base), 0);
4918 I915_WRITE(RING_HEAD(base), 0);
4919 I915_WRITE(RING_TAIL(base), 0);
4920 I915_WRITE(RING_START(base), 0);
4921}
4922
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004923static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004924{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004925 if (IS_I830(dev_priv)) {
4926 init_unused_ring(dev_priv, PRB1_BASE);
4927 init_unused_ring(dev_priv, SRB0_BASE);
4928 init_unused_ring(dev_priv, SRB1_BASE);
4929 init_unused_ring(dev_priv, SRB2_BASE);
4930 init_unused_ring(dev_priv, SRB3_BASE);
4931 } else if (IS_GEN2(dev_priv)) {
4932 init_unused_ring(dev_priv, SRB0_BASE);
4933 init_unused_ring(dev_priv, SRB1_BASE);
4934 } else if (IS_GEN3(dev_priv)) {
4935 init_unused_ring(dev_priv, PRB1_BASE);
4936 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004937 }
4938}
4939
Chris Wilson20a8a742017-02-08 14:30:31 +00004940static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004941{
Chris Wilson20a8a742017-02-08 14:30:31 +00004942 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004943 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304944 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004945 int err;
4946
4947 for_each_engine(engine, i915, id) {
4948 err = engine->init_hw(engine);
4949 if (err)
4950 return err;
4951 }
4952
4953 return 0;
4954}
4955
4956int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4957{
Chris Wilsond200cda2016-04-28 09:56:44 +01004958 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004959
Chris Wilsonde867c22016-10-25 13:16:02 +01004960 dev_priv->gt.last_init_time = ktime_get();
4961
Chris Wilson5e4f5182015-02-13 14:35:59 +00004962 /* Double layer security blanket, see i915_gem_init() */
4963 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4964
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004965 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004966 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004967
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004968 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004969 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004970 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004971
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004972 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004973 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004974 u32 temp = I915_READ(GEN7_MSG_CTL);
4975 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4976 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004977 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004978 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4979 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4980 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4981 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004982 }
4983
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004984 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004985
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004986 /*
4987 * At least 830 can leave some of the unused rings
4988 * "active" (ie. head != tail) after resume which
4989 * will prevent c3 entry. Makes sure all unused rings
4990 * are totally idle.
4991 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004992 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004993
Dave Gordoned54c1a2016-01-19 19:02:54 +00004994 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01004995 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
4996 ret = -EIO;
4997 goto out;
4998 }
John Harrison90638cc2015-05-29 17:43:37 +01004999
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005000 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005001 if (ret) {
5002 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
5003 goto out;
5004 }
5005
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005006 /* We can't enable contexts until all firmware is loaded */
5007 ret = intel_uc_init_hw(dev_priv);
5008 if (ret)
5009 goto out;
5010
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005011 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005012
Chris Wilson136109c2017-11-02 13:14:30 +00005013 /* Only when the HW is re-initialised, can we replay the requests */
5014 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005015out:
5016 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005017 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005018}
5019
Chris Wilsond2b4b972017-11-10 14:26:33 +00005020static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5021{
5022 struct i915_gem_context *ctx;
5023 struct intel_engine_cs *engine;
5024 enum intel_engine_id id;
5025 int err;
5026
5027 /*
5028 * As we reset the gpu during very early sanitisation, the current
5029 * register state on the GPU should reflect its defaults values.
5030 * We load a context onto the hw (with restore-inhibit), then switch
5031 * over to a second context to save that default register state. We
5032 * can then prime every new context with that state so they all start
5033 * from the same default HW values.
5034 */
5035
5036 ctx = i915_gem_context_create_kernel(i915, 0);
5037 if (IS_ERR(ctx))
5038 return PTR_ERR(ctx);
5039
5040 for_each_engine(engine, i915, id) {
5041 struct drm_i915_gem_request *rq;
5042
5043 rq = i915_gem_request_alloc(engine, ctx);
5044 if (IS_ERR(rq)) {
5045 err = PTR_ERR(rq);
5046 goto out_ctx;
5047 }
5048
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005049 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005050 if (engine->init_context)
5051 err = engine->init_context(rq);
5052
5053 __i915_add_request(rq, true);
5054 if (err)
5055 goto err_active;
5056 }
5057
5058 err = i915_gem_switch_to_kernel_context(i915);
5059 if (err)
5060 goto err_active;
5061
5062 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5063 if (err)
5064 goto err_active;
5065
5066 assert_kernel_context_is_current(i915);
5067
5068 for_each_engine(engine, i915, id) {
5069 struct i915_vma *state;
5070
5071 state = ctx->engine[id].state;
5072 if (!state)
5073 continue;
5074
5075 /*
5076 * As we will hold a reference to the logical state, it will
5077 * not be torn down with the context, and importantly the
5078 * object will hold onto its vma (making it possible for a
5079 * stray GTT write to corrupt our defaults). Unmap the vma
5080 * from the GTT to prevent such accidents and reclaim the
5081 * space.
5082 */
5083 err = i915_vma_unbind(state);
5084 if (err)
5085 goto err_active;
5086
5087 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5088 if (err)
5089 goto err_active;
5090
5091 engine->default_state = i915_gem_object_get(state->obj);
5092 }
5093
5094 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5095 unsigned int found = intel_engines_has_context_isolation(i915);
5096
5097 /*
5098 * Make sure that classes with multiple engine instances all
5099 * share the same basic configuration.
5100 */
5101 for_each_engine(engine, i915, id) {
5102 unsigned int bit = BIT(engine->uabi_class);
5103 unsigned int expected = engine->default_state ? bit : 0;
5104
5105 if ((found & bit) != expected) {
5106 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5107 engine->uabi_class, engine->name);
5108 }
5109 }
5110 }
5111
5112out_ctx:
5113 i915_gem_context_set_closed(ctx);
5114 i915_gem_context_put(ctx);
5115 return err;
5116
5117err_active:
5118 /*
5119 * If we have to abandon now, we expect the engines to be idle
5120 * and ready to be torn-down. First try to flush any remaining
5121 * request, ensure we are pointing at the kernel context and
5122 * then remove it.
5123 */
5124 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5125 goto out_ctx;
5126
5127 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5128 goto out_ctx;
5129
5130 i915_gem_contexts_lost(i915);
5131 goto out_ctx;
5132}
5133
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005134int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005135{
Chris Wilson1070a422012-04-24 15:47:41 +01005136 int ret;
5137
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005138 /*
5139 * We need to fallback to 4K pages since gvt gtt handling doesn't
5140 * support huge page entries - we will need to check either hypervisor
5141 * mm can support huge guest page or just do emulation in gvt.
5142 */
5143 if (intel_vgpu_active(dev_priv))
5144 mkwrite_device_info(dev_priv)->page_sizes =
5145 I915_GTT_PAGE_SIZE_4K;
5146
Chris Wilson94312822017-05-03 10:39:18 +01005147 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005148
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005149 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005150 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005151 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005152 } else {
5153 dev_priv->gt.resume = intel_legacy_submission_resume;
5154 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005155 }
5156
Chris Wilsonee487002017-11-22 17:26:21 +00005157 ret = i915_gem_init_userptr(dev_priv);
5158 if (ret)
5159 return ret;
5160
Chris Wilson5e4f5182015-02-13 14:35:59 +00005161 /* This is just a security blanket to placate dragons.
5162 * On some systems, we very sporadically observe that the first TLBs
5163 * used by the CS may be stale, despite us poking the TLB reset. If
5164 * we hold the forcewake during initialisation these problems
5165 * just magically go away.
5166 */
Chris Wilsonee487002017-11-22 17:26:21 +00005167 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005168 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5169
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005170 ret = i915_gem_init_ggtt(dev_priv);
5171 if (ret)
5172 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08005173
Chris Wilson829a0af2017-06-20 12:05:45 +01005174 ret = i915_gem_contexts_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02005175 if (ret)
5176 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005177
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005178 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01005179 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02005180 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005181
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005182 intel_init_gt_powersave(dev_priv);
5183
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005184 ret = i915_gem_init_hw(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005185 if (ret)
5186 goto out_unlock;
5187
5188 /*
5189 * Despite its name intel_init_clock_gating applies both display
5190 * clock gating workarounds; GT mmio workarounds and the occasional
5191 * GT power context workaround. Worse, sometimes it includes a context
5192 * register workaround which we need to apply before we record the
5193 * default HW state for all contexts.
5194 *
5195 * FIXME: break up the workarounds and apply them at the right time!
5196 */
5197 intel_init_clock_gating(dev_priv);
5198
Chris Wilsond2b4b972017-11-10 14:26:33 +00005199 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005200out_unlock:
Chris Wilson60990322014-04-09 09:19:42 +01005201 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01005202 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005203 * wedged. But we only want to do this where the GPU is angry,
5204 * for all other failure, such as an allocation failure, bail.
5205 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005206 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5207 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5208 i915_gem_set_wedged(dev_priv);
5209 }
Chris Wilson60990322014-04-09 09:19:42 +01005210 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005211 }
Chris Wilson5e4f5182015-02-13 14:35:59 +00005212 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005213 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005214
Chris Wilson60990322014-04-09 09:19:42 +01005215 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005216}
5217
Chris Wilson24145512017-01-24 11:01:35 +00005218void i915_gem_init_mmio(struct drm_i915_private *i915)
5219{
5220 i915_gem_sanitize(i915);
5221}
5222
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005223void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005224i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005225{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005226 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305227 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005228
Akash Goel3b3f1652016-10-13 22:44:48 +05305229 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005230 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005231}
5232
Eric Anholt673a3942008-07-30 12:06:12 -07005233void
Imre Deak40ae4e12016-03-16 14:54:03 +02005234i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5235{
Chris Wilson49ef5292016-08-18 17:17:00 +01005236 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005237
5238 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
5239 !IS_CHERRYVIEW(dev_priv))
5240 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02005241 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
5242 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5243 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005244 dev_priv->num_fence_regs = 16;
5245 else
5246 dev_priv->num_fence_regs = 8;
5247
Chris Wilsonc0336662016-05-06 15:40:21 +01005248 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005249 dev_priv->num_fence_regs =
5250 I915_READ(vgtif_reg(avail_rs.fence_num));
5251
5252 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005253 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5254 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5255
5256 fence->i915 = dev_priv;
5257 fence->id = i;
5258 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5259 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005260 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005261
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005262 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005263}
5264
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005265static void i915_gem_init__mm(struct drm_i915_private *i915)
5266{
5267 spin_lock_init(&i915->mm.object_stat_lock);
5268 spin_lock_init(&i915->mm.obj_lock);
5269 spin_lock_init(&i915->mm.free_lock);
5270
5271 init_llist_head(&i915->mm.free_list);
5272
5273 INIT_LIST_HEAD(&i915->mm.unbound_list);
5274 INIT_LIST_HEAD(&i915->mm.bound_list);
5275 INIT_LIST_HEAD(&i915->mm.fence_list);
5276 INIT_LIST_HEAD(&i915->mm.userfault_list);
5277
5278 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5279}
5280
Chris Wilson73cb9702016-10-28 13:58:46 +01005281int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005282i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005283{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005284 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005285
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005286 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5287 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005288 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005289
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005290 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5291 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005292 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005293
Chris Wilsond1b48c12017-08-16 09:52:08 +01005294 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5295 if (!dev_priv->luts)
5296 goto err_vmas;
5297
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005298 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
5299 SLAB_HWCACHE_ALIGN |
5300 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005301 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005302 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005303 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005304
Chris Wilson52e54202016-11-14 20:41:02 +00005305 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5306 SLAB_HWCACHE_ALIGN |
5307 SLAB_RECLAIM_ACCOUNT);
5308 if (!dev_priv->dependencies)
5309 goto err_requests;
5310
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005311 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5312 if (!dev_priv->priorities)
5313 goto err_dependencies;
5314
Chris Wilson73cb9702016-10-28 13:58:46 +01005315 mutex_lock(&dev_priv->drm.struct_mutex);
5316 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005317 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005318 mutex_unlock(&dev_priv->drm.struct_mutex);
5319 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005320 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005321
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005322 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005323
Chris Wilson67d97da2016-07-04 08:08:31 +01005324 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005325 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005326 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005327 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005328 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005329 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005330
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005331 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5332
Chris Wilsonb5add952016-08-04 16:32:36 +01005333 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005334
Matthew Auld465c4032017-10-06 23:18:14 +01005335 err = i915_gemfs_init(dev_priv);
5336 if (err)
5337 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5338
Chris Wilson73cb9702016-10-28 13:58:46 +01005339 return 0;
5340
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005341err_priorities:
5342 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005343err_dependencies:
5344 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005345err_requests:
5346 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005347err_luts:
5348 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005349err_vmas:
5350 kmem_cache_destroy(dev_priv->vmas);
5351err_objects:
5352 kmem_cache_destroy(dev_priv->objects);
5353err_out:
5354 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005355}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005356
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005357void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005358{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005359 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005360 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005361 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005362
Matthew Auldea84aa72016-11-17 21:04:11 +00005363 mutex_lock(&dev_priv->drm.struct_mutex);
5364 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5365 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5366 mutex_unlock(&dev_priv->drm.struct_mutex);
5367
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005368 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005369 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005370 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005371 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005372 kmem_cache_destroy(dev_priv->vmas);
5373 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005374
5375 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5376 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005377
5378 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005379}
5380
Chris Wilson6a800ea2016-09-21 14:51:07 +01005381int i915_gem_freeze(struct drm_i915_private *dev_priv)
5382{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005383 /* Discard all purgeable objects, let userspace recover those as
5384 * required after resuming.
5385 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005386 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005387
Chris Wilson6a800ea2016-09-21 14:51:07 +01005388 return 0;
5389}
5390
Chris Wilson461fb992016-05-14 07:26:33 +01005391int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5392{
5393 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005394 struct list_head *phases[] = {
5395 &dev_priv->mm.unbound_list,
5396 &dev_priv->mm.bound_list,
5397 NULL
5398 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005399
5400 /* Called just before we write the hibernation image.
5401 *
5402 * We need to update the domain tracking to reflect that the CPU
5403 * will be accessing all the pages to create and restore from the
5404 * hibernation, and so upon restoration those pages will be in the
5405 * CPU domain.
5406 *
5407 * To make sure the hibernation image contains the latest state,
5408 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005409 *
5410 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005411 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005412 */
5413
Chris Wilson912d5722017-09-06 16:19:30 -07005414 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005415 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005416
Chris Wilsonf2123812017-10-16 12:40:37 +01005417 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005418 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005419 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005420 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005421 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005422 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005423
5424 return 0;
5425}
5426
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005427void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005428{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005429 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01005430 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005431
5432 /* Clean up our request list when the client is going away, so that
5433 * later retire_requests won't dereference our soon-to-be-gone
5434 * file_priv.
5435 */
Chris Wilson1c255952010-09-26 11:03:27 +01005436 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005437 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005438 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005439 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005440}
5441
Chris Wilson829a0af2017-06-20 12:05:45 +01005442int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005443{
5444 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005445 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005446
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005447 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005448
5449 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5450 if (!file_priv)
5451 return -ENOMEM;
5452
5453 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005454 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005455 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005456
5457 spin_lock_init(&file_priv->mm.lock);
5458 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005459
Chris Wilsonc80ff162016-07-27 09:07:27 +01005460 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005461
Chris Wilson829a0af2017-06-20 12:05:45 +01005462 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005463 if (ret)
5464 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005465
Ben Widawskye422b882013-12-06 14:10:58 -08005466 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005467}
5468
Daniel Vetterb680c372014-09-19 18:27:27 +02005469/**
5470 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005471 * @old: current GEM buffer for the frontbuffer slots
5472 * @new: new GEM buffer for the frontbuffer slots
5473 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005474 *
5475 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5476 * from @old and setting them in @new. Both @old and @new can be NULL.
5477 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005478void i915_gem_track_fb(struct drm_i915_gem_object *old,
5479 struct drm_i915_gem_object *new,
5480 unsigned frontbuffer_bits)
5481{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005482 /* Control of individual bits within the mask are guarded by
5483 * the owning plane->mutex, i.e. we can never see concurrent
5484 * manipulation of individual bits. But since the bitfield as a whole
5485 * is updated using RMW, we need to use atomics in order to update
5486 * the bits.
5487 */
5488 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5489 sizeof(atomic_t) * BITS_PER_BYTE);
5490
Daniel Vettera071fa02014-06-18 23:28:09 +02005491 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005492 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5493 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005494 }
5495
5496 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005497 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5498 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005499 }
5500}
5501
Dave Gordonea702992015-07-09 19:29:02 +01005502/* Allocate a new GEM object and fill it with the supplied data */
5503struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005504i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005505 const void *data, size_t size)
5506{
5507 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005508 struct file *file;
5509 size_t offset;
5510 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005511
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005512 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005513 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005514 return obj;
5515
Chris Wilsonce8ff092017-03-17 19:46:47 +00005516 GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005517
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005518 file = obj->base.filp;
5519 offset = 0;
5520 do {
5521 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5522 struct page *page;
5523 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005524
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005525 err = pagecache_write_begin(file, file->f_mapping,
5526 offset, len, 0,
5527 &page, &pgdata);
5528 if (err < 0)
5529 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005530
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005531 vaddr = kmap(page);
5532 memcpy(vaddr, data, len);
5533 kunmap(page);
5534
5535 err = pagecache_write_end(file, file->f_mapping,
5536 offset, len, len,
5537 page, pgdata);
5538 if (err < 0)
5539 goto fail;
5540
5541 size -= len;
5542 data += len;
5543 offset += len;
5544 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005545
5546 return obj;
5547
5548fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005549 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005550 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005551}
Chris Wilson96d77632016-10-28 13:58:33 +01005552
5553struct scatterlist *
5554i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5555 unsigned int n,
5556 unsigned int *offset)
5557{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005558 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005559 struct scatterlist *sg;
5560 unsigned int idx, count;
5561
5562 might_sleep();
5563 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005564 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005565
5566 /* As we iterate forward through the sg, we record each entry in a
5567 * radixtree for quick repeated (backwards) lookups. If we have seen
5568 * this index previously, we will have an entry for it.
5569 *
5570 * Initial lookup is O(N), but this is amortized to O(1) for
5571 * sequential page access (where each new request is consecutive
5572 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5573 * i.e. O(1) with a large constant!
5574 */
5575 if (n < READ_ONCE(iter->sg_idx))
5576 goto lookup;
5577
5578 mutex_lock(&iter->lock);
5579
5580 /* We prefer to reuse the last sg so that repeated lookup of this
5581 * (or the subsequent) sg are fast - comparing against the last
5582 * sg is faster than going through the radixtree.
5583 */
5584
5585 sg = iter->sg_pos;
5586 idx = iter->sg_idx;
5587 count = __sg_page_count(sg);
5588
5589 while (idx + count <= n) {
5590 unsigned long exception, i;
5591 int ret;
5592
5593 /* If we cannot allocate and insert this entry, or the
5594 * individual pages from this range, cancel updating the
5595 * sg_idx so that on this lookup we are forced to linearly
5596 * scan onwards, but on future lookups we will try the
5597 * insertion again (in which case we need to be careful of
5598 * the error return reporting that we have already inserted
5599 * this index).
5600 */
5601 ret = radix_tree_insert(&iter->radix, idx, sg);
5602 if (ret && ret != -EEXIST)
5603 goto scan;
5604
5605 exception =
5606 RADIX_TREE_EXCEPTIONAL_ENTRY |
5607 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5608 for (i = 1; i < count; i++) {
5609 ret = radix_tree_insert(&iter->radix, idx + i,
5610 (void *)exception);
5611 if (ret && ret != -EEXIST)
5612 goto scan;
5613 }
5614
5615 idx += count;
5616 sg = ____sg_next(sg);
5617 count = __sg_page_count(sg);
5618 }
5619
5620scan:
5621 iter->sg_pos = sg;
5622 iter->sg_idx = idx;
5623
5624 mutex_unlock(&iter->lock);
5625
5626 if (unlikely(n < idx)) /* insertion completed by another thread */
5627 goto lookup;
5628
5629 /* In case we failed to insert the entry into the radixtree, we need
5630 * to look beyond the current sg.
5631 */
5632 while (idx + count <= n) {
5633 idx += count;
5634 sg = ____sg_next(sg);
5635 count = __sg_page_count(sg);
5636 }
5637
5638 *offset = n - idx;
5639 return sg;
5640
5641lookup:
5642 rcu_read_lock();
5643
5644 sg = radix_tree_lookup(&iter->radix, n);
5645 GEM_BUG_ON(!sg);
5646
5647 /* If this index is in the middle of multi-page sg entry,
5648 * the radixtree will contain an exceptional entry that points
5649 * to the start of that range. We will return the pointer to
5650 * the base page and the offset of this page within the
5651 * sg entry's range.
5652 */
5653 *offset = 0;
5654 if (unlikely(radix_tree_exception(sg))) {
5655 unsigned long base =
5656 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5657
5658 sg = radix_tree_lookup(&iter->radix, base);
5659 GEM_BUG_ON(!sg);
5660
5661 *offset = n - base;
5662 }
5663
5664 rcu_read_unlock();
5665
5666 return sg;
5667}
5668
5669struct page *
5670i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5671{
5672 struct scatterlist *sg;
5673 unsigned int offset;
5674
5675 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5676
5677 sg = i915_gem_object_get_sg(obj, n, &offset);
5678 return nth_page(sg_page(sg), offset);
5679}
5680
5681/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5682struct page *
5683i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5684 unsigned int n)
5685{
5686 struct page *page;
5687
5688 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005689 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005690 set_page_dirty(page);
5691
5692 return page;
5693}
5694
5695dma_addr_t
5696i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5697 unsigned long n)
5698{
5699 struct scatterlist *sg;
5700 unsigned int offset;
5701
5702 sg = i915_gem_object_get_sg(obj, n, &offset);
5703 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5704}
Chris Wilson935a2f72017-02-13 17:15:13 +00005705
Chris Wilson8eeb7902017-07-26 19:16:01 +01005706int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5707{
5708 struct sg_table *pages;
5709 int err;
5710
5711 if (align > obj->base.size)
5712 return -EINVAL;
5713
5714 if (obj->ops == &i915_gem_phys_ops)
5715 return 0;
5716
5717 if (obj->ops != &i915_gem_object_ops)
5718 return -EINVAL;
5719
5720 err = i915_gem_object_unbind(obj);
5721 if (err)
5722 return err;
5723
5724 mutex_lock(&obj->mm.lock);
5725
5726 if (obj->mm.madv != I915_MADV_WILLNEED) {
5727 err = -EFAULT;
5728 goto err_unlock;
5729 }
5730
5731 if (obj->mm.quirked) {
5732 err = -EFAULT;
5733 goto err_unlock;
5734 }
5735
5736 if (obj->mm.mapping) {
5737 err = -EBUSY;
5738 goto err_unlock;
5739 }
5740
Chris Wilsonf2123812017-10-16 12:40:37 +01005741 pages = fetch_and_zero(&obj->mm.pages);
5742 if (pages) {
5743 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5744
5745 __i915_gem_object_reset_page_iter(obj);
5746
5747 spin_lock(&i915->mm.obj_lock);
5748 list_del(&obj->mm.link);
5749 spin_unlock(&i915->mm.obj_lock);
5750 }
5751
Chris Wilson8eeb7902017-07-26 19:16:01 +01005752 obj->ops = &i915_gem_phys_ops;
5753
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005754 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005755 if (err)
5756 goto err_xfer;
5757
5758 /* Perma-pin (until release) the physical set of pages */
5759 __i915_gem_object_pin_pages(obj);
5760
5761 if (!IS_ERR_OR_NULL(pages))
5762 i915_gem_object_ops.put_pages(obj, pages);
5763 mutex_unlock(&obj->mm.lock);
5764 return 0;
5765
5766err_xfer:
5767 obj->ops = &i915_gem_object_ops;
5768 obj->mm.pages = pages;
5769err_unlock:
5770 mutex_unlock(&obj->mm.lock);
5771 return err;
5772}
5773
Chris Wilson935a2f72017-02-13 17:15:13 +00005774#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5775#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005776#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005777#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005778#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005779#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005780#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005781#endif