blob: 9650a7b10c5f8fe418ebb81ef97d1add84c05d50 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
Daniel Vetterbe6a0372015-03-18 10:46:04 +01002 * Copyright © 2008-2015 Intel Corporation
Eric Anholt673a3942008-07-30 12:06:12 -07003 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson57822dc2017-02-22 11:40:48 +000032#include "i915_gem_clflush.h"
Yu Zhangeb822892015-02-10 19:05:49 +080033#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010034#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070035#include "intel_drv.h"
Chris Wilson5d723d72016-08-04 16:32:35 +010036#include "intel_frontbuffer.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010037#include "intel_mocs.h"
Matthew Auld465c4032017-10-06 23:18:14 +010038#include "i915_gemfs.h"
Chris Wilson6b5e90f2016-11-14 20:41:05 +000039#include <linux/dma-fence-array.h>
Chris Wilsonfe3288b2017-02-12 17:20:01 +000040#include <linux/kthread.h>
Chris Wilsonc13d87e2016-07-20 09:21:15 +010041#include <linux/reservation.h>
Hugh Dickins5949eac2011-06-27 16:18:18 -070042#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Chris Wilson20e49332016-11-22 14:41:21 +000044#include <linux/stop_machine.h>
Eric Anholt673a3942008-07-30 12:06:12 -070045#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080046#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020047#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070048
Chris Wilsonfbbd37b2016-10-28 13:58:42 +010049static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
Chris Wilson61050802012-04-17 15:31:31 +010050
Chris Wilson2c225692013-08-09 12:26:45 +010051static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
52{
Chris Wilsone27ab732017-06-15 13:38:49 +010053 if (obj->cache_dirty)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +053054 return false;
55
Chris Wilsonb8f55be2017-08-11 12:11:16 +010056 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
Chris Wilson2c225692013-08-09 12:26:45 +010057 return true;
58
Chris Wilsonbd3d2252017-10-13 21:26:14 +010059 return obj->pin_global; /* currently in use by HW, keep flushed */
Chris Wilson2c225692013-08-09 12:26:45 +010060}
61
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053062static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010063insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053064 struct drm_mm_node *node, u32 size)
65{
66 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000067 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
68 size, 0, I915_COLOR_UNEVICTABLE,
69 0, ggtt->mappable_end,
70 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053071}
72
73static void
74remove_mappable_node(struct drm_mm_node *node)
75{
76 drm_mm_remove_node(node);
77}
78
Chris Wilson73aa8082010-09-30 11:46:12 +010079/* some bookkeeping */
80static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010081 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010082{
Daniel Vetterc20e8352013-07-24 22:40:23 +020083 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010084 dev_priv->mm.object_count++;
85 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020086 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010087}
88
89static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010090 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010091{
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093 dev_priv->mm.object_count--;
94 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096}
97
Chris Wilson21dd3732011-01-26 15:55:56 +000098static int
Daniel Vetter33196de2012-11-14 17:14:05 +010099i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101 int ret;
102
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100103 might_sleep();
104
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200105 /*
106 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
107 * userspace. If it takes that long something really bad is going on and
108 * we should simply try to bail out and fail as gracefully as possible.
109 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100110 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000111 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100112 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 if (ret == 0) {
114 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
115 return -EIO;
116 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100118 } else {
119 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200120 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121}
122
Chris Wilson54cf91d2010-11-25 18:00:26 +0000123int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100124{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100125 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100126 int ret;
127
Daniel Vetter33196de2012-11-14 17:14:05 +0100128 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 if (ret)
130 return ret;
131
132 ret = mutex_lock_interruptible(&dev->struct_mutex);
133 if (ret)
134 return ret;
135
Chris Wilson76c1dec2010-09-25 11:22:51 +0100136 return 0;
137}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100138
Eric Anholt673a3942008-07-30 12:06:12 -0700139int
Eric Anholt5a125c32008-10-22 21:40:13 -0700140i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000141 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700142{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300143 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200144 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300145 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100146 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800147 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700148
Weinan Liff8f7972017-05-31 10:35:52 +0800149 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100150 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000151 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100152 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100153 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000154 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100155 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100156 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100157 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300159 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000161
Eric Anholt5a125c32008-10-22 21:40:13 -0700162 return 0;
163}
164
Matthew Auldb91b09e2017-10-06 23:18:17 +0100165static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100166{
Al Viro93c76a32015-12-04 23:45:44 -0500167 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000168 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800169 struct sg_table *st;
170 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000171 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800172 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100173 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100174
Chris Wilson6a2c4232014-11-04 04:51:40 -0800175 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100176 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100177
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 /* Always aligning to the object size, allows a single allocation
179 * to handle all possible callers, and given typical object sizes,
180 * the alignment of the buddy allocation will naturally match.
181 */
182 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300183 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000184 roundup_pow_of_two(obj->base.size));
185 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100186 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000187
188 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800189 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
190 struct page *page;
191 char *src;
192
193 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000194 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100195 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000196 goto err_phys;
197 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800198
199 src = kmap_atomic(page);
200 memcpy(vaddr, src, PAGE_SIZE);
201 drm_clflush_virt_range(vaddr, PAGE_SIZE);
202 kunmap_atomic(src);
203
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300204 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800205 vaddr += PAGE_SIZE;
206 }
207
Chris Wilsonc0336662016-05-06 15:40:21 +0100208 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800209
210 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000211 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100212 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000213 goto err_phys;
214 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800215
216 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
217 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100218 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000219 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800220 }
221
222 sg = st->sgl;
223 sg->offset = 0;
224 sg->length = obj->base.size;
225
Chris Wilsondbb43512016-12-07 13:34:11 +0000226 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800227 sg_dma_len(sg) = obj->base.size;
228
Chris Wilsondbb43512016-12-07 13:34:11 +0000229 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100230
Matthew Aulda5c081662017-10-06 23:18:18 +0100231 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100232
233 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000234
235err_phys:
236 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100237
238 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239}
240
Chris Wilsone27ab732017-06-15 13:38:49 +0100241static void __start_cpu_write(struct drm_i915_gem_object *obj)
242{
Christian Königc0a51fd2018-02-16 13:43:38 +0100243 obj->read_domains = I915_GEM_DOMAIN_CPU;
244 obj->write_domain = I915_GEM_DOMAIN_CPU;
Chris Wilsone27ab732017-06-15 13:38:49 +0100245 if (cpu_write_needs_clflush(obj))
246 obj->cache_dirty = true;
247}
248
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000250__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000251 struct sg_table *pages,
252 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800253{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100254 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800255
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100256 if (obj->mm.madv == I915_MADV_DONTNEED)
257 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800258
Chris Wilsone5facdf2016-12-23 14:57:57 +0000259 if (needs_clflush &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100260 (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100261 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000262 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100263
Chris Wilsone27ab732017-06-15 13:38:49 +0100264 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100265}
266
267static void
268i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
269 struct sg_table *pages)
270{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000271 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100272
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100273 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500274 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100276 int i;
277
278 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800279 struct page *page;
280 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100281
Chris Wilson6a2c4232014-11-04 04:51:40 -0800282 page = shmem_read_mapping_page(mapping, i);
283 if (IS_ERR(page))
284 continue;
285
286 dst = kmap_atomic(page);
287 drm_clflush_virt_range(vaddr, PAGE_SIZE);
288 memcpy(dst, vaddr, PAGE_SIZE);
289 kunmap_atomic(dst);
290
291 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100292 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100293 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100295 vaddr += PAGE_SIZE;
296 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100297 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100298 }
299
Chris Wilson03ac84f2016-10-28 13:58:36 +0100300 sg_free_table(pages);
301 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000302
303 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800304}
305
306static void
307i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
308{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100309 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800310}
311
312static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
313 .get_pages = i915_gem_object_get_pages_phys,
314 .put_pages = i915_gem_object_put_pages_phys,
315 .release = i915_gem_object_release_phys,
316};
317
Chris Wilson581ab1f2017-02-15 16:39:00 +0000318static const struct drm_i915_gem_object_ops i915_gem_object_ops;
319
Chris Wilson35a96112016-08-14 18:44:40 +0100320int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100321{
322 struct i915_vma *vma;
323 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100324 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100325
Chris Wilson02bef8f2016-08-14 18:44:41 +0100326 lockdep_assert_held(&obj->base.dev->struct_mutex);
327
328 /* Closed vma are removed from the obj->vma_list - but they may
329 * still have an active binding on the object. To remove those we
330 * must wait for all rendering to complete to the object (as unbinding
331 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100332 */
Chris Wilson5888fc92017-12-04 13:25:13 +0000333 ret = i915_gem_object_set_to_cpu_domain(obj, false);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100334 if (ret)
335 return ret;
336
Chris Wilsonaa653a62016-08-04 07:52:27 +0100337 while ((vma = list_first_entry_or_null(&obj->vma_list,
338 struct i915_vma,
339 obj_link))) {
340 list_move_tail(&vma->obj_link, &still_in_list);
341 ret = i915_vma_unbind(vma);
342 if (ret)
343 break;
344 }
345 list_splice(&still_in_list, &obj->vma_list);
346
347 return ret;
348}
349
Chris Wilsone95433c2016-10-28 13:58:27 +0100350static long
351i915_gem_object_wait_fence(struct dma_fence *fence,
352 unsigned int flags,
353 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100354 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100355{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000356 struct i915_request *rq;
Chris Wilsone95433c2016-10-28 13:58:27 +0100357
358 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
359
360 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
361 return timeout;
362
363 if (!dma_fence_is_i915(fence))
364 return dma_fence_wait_timeout(fence,
365 flags & I915_WAIT_INTERRUPTIBLE,
366 timeout);
367
368 rq = to_request(fence);
Chris Wilsone61e0f52018-02-21 09:56:36 +0000369 if (i915_request_completed(rq))
Chris Wilsone95433c2016-10-28 13:58:27 +0100370 goto out;
371
Chris Wilsone9af4ea2018-01-18 13:16:09 +0000372 /*
373 * This client is about to stall waiting for the GPU. In many cases
Chris Wilsone95433c2016-10-28 13:58:27 +0100374 * this is undesirable and limits the throughput of the system, as
375 * many clients cannot continue processing user input/output whilst
376 * blocked. RPS autotuning may take tens of milliseconds to respond
377 * to the GPU load and thus incurs additional latency for the client.
378 * We can circumvent that by promoting the GPU frequency to maximum
379 * before we wait. This makes the GPU throttle up much more quickly
380 * (good for benchmarks and user experience, e.g. window animations),
381 * but at a cost of spending more power processing the workload
382 * (bad for battery). Not all clients even want their results
383 * immediately and for them we should just let the GPU select its own
384 * frequency to maximise efficiency. To prevent a single client from
385 * forcing the clocks too high for the whole system, we only allow
386 * each client to waitboost once in a busy period.
387 */
Chris Wilsone61e0f52018-02-21 09:56:36 +0000388 if (rps_client && !i915_request_started(rq)) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100389 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100390 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100391 }
392
Chris Wilsone61e0f52018-02-21 09:56:36 +0000393 timeout = i915_request_wait(rq, flags, timeout);
Chris Wilsone95433c2016-10-28 13:58:27 +0100394
395out:
Chris Wilsone61e0f52018-02-21 09:56:36 +0000396 if (flags & I915_WAIT_LOCKED && i915_request_completed(rq))
397 i915_request_retire_upto(rq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100398
Chris Wilsone95433c2016-10-28 13:58:27 +0100399 return timeout;
400}
401
402static long
403i915_gem_object_wait_reservation(struct reservation_object *resv,
404 unsigned int flags,
405 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100406 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100407{
Chris Wilsone54ca972017-02-17 15:13:04 +0000408 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100409 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000410 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100411
412 if (flags & I915_WAIT_ALL) {
413 struct dma_fence **shared;
414 unsigned int count, i;
415 int ret;
416
417 ret = reservation_object_get_fences_rcu(resv,
418 &excl, &count, &shared);
419 if (ret)
420 return ret;
421
422 for (i = 0; i < count; i++) {
423 timeout = i915_gem_object_wait_fence(shared[i],
424 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100425 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000426 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100427 break;
428
429 dma_fence_put(shared[i]);
430 }
431
432 for (; i < count; i++)
433 dma_fence_put(shared[i]);
434 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000435
Chris Wilsonfa730552018-03-07 17:13:03 +0000436 /*
437 * If both shared fences and an exclusive fence exist,
438 * then by construction the shared fences must be later
439 * than the exclusive fence. If we successfully wait for
440 * all the shared fences, we know that the exclusive fence
441 * must all be signaled. If all the shared fences are
442 * signaled, we can prune the array and recover the
443 * floating references on the fences/requests.
444 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000445 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100446 } else {
447 excl = reservation_object_get_excl_rcu(resv);
448 }
449
Chris Wilsonfa730552018-03-07 17:13:03 +0000450 if (excl && timeout >= 0)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100451 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
452 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100453
454 dma_fence_put(excl);
455
Chris Wilsonfa730552018-03-07 17:13:03 +0000456 /*
457 * Opportunistically prune the fences iff we know they have *all* been
Chris Wilson03d1cac2017-03-08 13:26:28 +0000458 * signaled and that the reservation object has not been changed (i.e.
459 * no new fences have been added).
460 */
Chris Wilsone54ca972017-02-17 15:13:04 +0000461 if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
Chris Wilson03d1cac2017-03-08 13:26:28 +0000462 if (reservation_object_trylock(resv)) {
463 if (!__read_seqcount_retry(&resv->seq, seq))
464 reservation_object_add_excl_fence(resv, NULL);
465 reservation_object_unlock(resv);
466 }
Chris Wilsone54ca972017-02-17 15:13:04 +0000467 }
468
Chris Wilsone95433c2016-10-28 13:58:27 +0100469 return timeout;
470}
471
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000472static void __fence_set_priority(struct dma_fence *fence, int prio)
473{
Chris Wilsone61e0f52018-02-21 09:56:36 +0000474 struct i915_request *rq;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000475 struct intel_engine_cs *engine;
476
Chris Wilsonc218ee02018-01-06 10:56:18 +0000477 if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000478 return;
479
480 rq = to_request(fence);
481 engine = rq->engine;
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000482
Chris Wilson47650db2018-03-07 13:42:25 +0000483 rcu_read_lock();
484 if (engine->schedule)
485 engine->schedule(rq, prio);
486 rcu_read_unlock();
Chris Wilson6b5e90f2016-11-14 20:41:05 +0000487}
488
489static void fence_set_priority(struct dma_fence *fence, int prio)
490{
491 /* Recurse once into a fence-array */
492 if (dma_fence_is_array(fence)) {
493 struct dma_fence_array *array = to_dma_fence_array(fence);
494 int i;
495
496 for (i = 0; i < array->num_fences; i++)
497 __fence_set_priority(array->fences[i], prio);
498 } else {
499 __fence_set_priority(fence, prio);
500 }
501}
502
503int
504i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
505 unsigned int flags,
506 int prio)
507{
508 struct dma_fence *excl;
509
510 if (flags & I915_WAIT_ALL) {
511 struct dma_fence **shared;
512 unsigned int count, i;
513 int ret;
514
515 ret = reservation_object_get_fences_rcu(obj->resv,
516 &excl, &count, &shared);
517 if (ret)
518 return ret;
519
520 for (i = 0; i < count; i++) {
521 fence_set_priority(shared[i], prio);
522 dma_fence_put(shared[i]);
523 }
524
525 kfree(shared);
526 } else {
527 excl = reservation_object_get_excl_rcu(obj->resv);
528 }
529
530 if (excl) {
531 fence_set_priority(excl, prio);
532 dma_fence_put(excl);
533 }
534 return 0;
535}
536
Chris Wilson00e60f22016-08-04 16:32:40 +0100537/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100538 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100539 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100540 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
541 * @timeout: how long to wait
Chris Wilsona0a8b1c2017-11-09 14:06:44 +0000542 * @rps_client: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100543 */
544int
Chris Wilsone95433c2016-10-28 13:58:27 +0100545i915_gem_object_wait(struct drm_i915_gem_object *obj,
546 unsigned int flags,
547 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100548 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100549{
Chris Wilsone95433c2016-10-28 13:58:27 +0100550 might_sleep();
551#if IS_ENABLED(CONFIG_LOCKDEP)
552 GEM_BUG_ON(debug_locks &&
553 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
554 !!(flags & I915_WAIT_LOCKED));
555#endif
556 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100557
Chris Wilsond07f0e52016-10-28 13:58:44 +0100558 timeout = i915_gem_object_wait_reservation(obj->resv,
559 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100560 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100561 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100562}
563
564static struct intel_rps_client *to_rps_client(struct drm_file *file)
565{
566 struct drm_i915_file_private *fpriv = file->driver_priv;
567
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100568 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100569}
570
Chris Wilson00731152014-05-21 12:42:56 +0100571static int
572i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
573 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100574 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100575{
Chris Wilson00731152014-05-21 12:42:56 +0100576 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300577 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800578
579 /* We manually control the domain here and pretend that it
580 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
581 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700582 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000583 if (copy_from_user(vaddr, user_data, args->size))
584 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100585
Chris Wilson6a2c4232014-11-04 04:51:40 -0800586 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000587 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200588
Chris Wilsond59b21e2017-02-22 11:40:49 +0000589 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000590 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100591}
592
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000593void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000594{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100595 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000596}
597
598void i915_gem_object_free(struct drm_i915_gem_object *obj)
599{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100600 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100601 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000602}
603
Dave Airlieff72145b2011-02-07 12:16:14 +1000604static int
605i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000606 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000607 uint64_t size,
608 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700609{
Chris Wilson05394f32010-11-08 19:18:58 +0000610 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300611 int ret;
612 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700613
Dave Airlieff72145b2011-02-07 12:16:14 +1000614 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200615 if (size == 0)
616 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700617
618 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000619 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100620 if (IS_ERR(obj))
621 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700622
Chris Wilson05394f32010-11-08 19:18:58 +0000623 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100624 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100625 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200626 if (ret)
627 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100628
Dave Airlieff72145b2011-02-07 12:16:14 +1000629 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700630 return 0;
631}
632
Dave Airlieff72145b2011-02-07 12:16:14 +1000633int
634i915_gem_dumb_create(struct drm_file *file,
635 struct drm_device *dev,
636 struct drm_mode_create_dumb *args)
637{
638 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300639 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000640 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000641 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000642 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000643}
644
Chris Wilsone27ab732017-06-15 13:38:49 +0100645static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
646{
647 return !(obj->cache_level == I915_CACHE_NONE ||
648 obj->cache_level == I915_CACHE_WT);
649}
650
Dave Airlieff72145b2011-02-07 12:16:14 +1000651/**
652 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100653 * @dev: drm device pointer
654 * @data: ioctl data blob
655 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000656 */
657int
658i915_gem_create_ioctl(struct drm_device *dev, void *data,
659 struct drm_file *file)
660{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000661 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000662 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200663
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000664 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100665
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000666 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000667 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000668}
669
Chris Wilsonef749212017-04-12 12:01:10 +0100670static inline enum fb_op_origin
671fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
672{
673 return (domain == I915_GEM_DOMAIN_GTT ?
674 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
675}
676
Chris Wilson71253972017-12-06 12:49:14 +0000677void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
Chris Wilsonef749212017-04-12 12:01:10 +0100678{
Chris Wilson71253972017-12-06 12:49:14 +0000679 /*
680 * No actual flushing is required for the GTT write domain for reads
681 * from the GTT domain. Writes to it "immediately" go to main memory
682 * as far as we know, so there's no chipset flush. It also doesn't
683 * land in the GPU render cache.
Chris Wilsonef749212017-04-12 12:01:10 +0100684 *
685 * However, we do have to enforce the order so that all writes through
686 * the GTT land before any writes to the device, such as updates to
687 * the GATT itself.
688 *
689 * We also have to wait a bit for the writes to land from the GTT.
690 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
691 * timing. This issue has only been observed when switching quickly
692 * between GTT writes and CPU reads from inside the kernel on recent hw,
693 * and it appears to only affect discrete GTT blocks (i.e. on LLC
Chris Wilson71253972017-12-06 12:49:14 +0000694 * system agents we cannot reproduce this behaviour, until Cannonlake
695 * that was!).
Chris Wilsonef749212017-04-12 12:01:10 +0100696 */
Chris Wilson71253972017-12-06 12:49:14 +0000697
Chris Wilsonef749212017-04-12 12:01:10 +0100698 wmb();
699
Chris Wilson71253972017-12-06 12:49:14 +0000700 intel_runtime_pm_get(dev_priv);
701 spin_lock_irq(&dev_priv->uncore.lock);
702
703 POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
704
705 spin_unlock_irq(&dev_priv->uncore.lock);
706 intel_runtime_pm_put(dev_priv);
707}
708
709static void
710flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
711{
712 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
713 struct i915_vma *vma;
714
Christian Königc0a51fd2018-02-16 13:43:38 +0100715 if (!(obj->write_domain & flush_domains))
Chris Wilson71253972017-12-06 12:49:14 +0000716 return;
717
Christian Königc0a51fd2018-02-16 13:43:38 +0100718 switch (obj->write_domain) {
Chris Wilsonef749212017-04-12 12:01:10 +0100719 case I915_GEM_DOMAIN_GTT:
Chris Wilson71253972017-12-06 12:49:14 +0000720 i915_gem_flush_ggtt_writes(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100721
722 intel_fb_obj_flush(obj,
723 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
Chris Wilson71253972017-12-06 12:49:14 +0000724
Chris Wilsone2189dd2017-12-07 21:14:07 +0000725 for_each_ggtt_vma(vma, obj) {
Chris Wilson71253972017-12-06 12:49:14 +0000726 if (vma->iomap)
727 continue;
728
729 i915_vma_unset_ggtt_write(vma);
730 }
Chris Wilsonef749212017-04-12 12:01:10 +0100731 break;
732
733 case I915_GEM_DOMAIN_CPU:
734 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
735 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100736
737 case I915_GEM_DOMAIN_RENDER:
738 if (gpu_write_needs_clflush(obj))
739 obj->cache_dirty = true;
740 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100741 }
742
Christian Königc0a51fd2018-02-16 13:43:38 +0100743 obj->write_domain = 0;
Chris Wilsonef749212017-04-12 12:01:10 +0100744}
745
Daniel Vetter8c599672011-12-14 13:57:31 +0100746static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100747__copy_to_user_swizzled(char __user *cpu_vaddr,
748 const char *gpu_vaddr, int gpu_offset,
749 int length)
750{
751 int ret, cpu_offset = 0;
752
753 while (length > 0) {
754 int cacheline_end = ALIGN(gpu_offset + 1, 64);
755 int this_length = min(cacheline_end - gpu_offset, length);
756 int swizzled_gpu_offset = gpu_offset ^ 64;
757
758 ret = __copy_to_user(cpu_vaddr + cpu_offset,
759 gpu_vaddr + swizzled_gpu_offset,
760 this_length);
761 if (ret)
762 return ret + length;
763
764 cpu_offset += this_length;
765 gpu_offset += this_length;
766 length -= this_length;
767 }
768
769 return 0;
770}
771
772static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700773__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
774 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100775 int length)
776{
777 int ret, cpu_offset = 0;
778
779 while (length > 0) {
780 int cacheline_end = ALIGN(gpu_offset + 1, 64);
781 int this_length = min(cacheline_end - gpu_offset, length);
782 int swizzled_gpu_offset = gpu_offset ^ 64;
783
784 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
785 cpu_vaddr + cpu_offset,
786 this_length);
787 if (ret)
788 return ret + length;
789
790 cpu_offset += this_length;
791 gpu_offset += this_length;
792 length -= this_length;
793 }
794
795 return 0;
796}
797
Brad Volkin4c914c02014-02-18 10:15:45 -0800798/*
799 * Pins the specified object's pages and synchronizes the object with
800 * GPU accesses. Sets needs_clflush to non-zero if the caller should
801 * flush the object from the CPU cache.
802 */
803int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100804 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800805{
806 int ret;
807
Chris Wilsone95433c2016-10-28 13:58:27 +0100808 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800809
Chris Wilsone95433c2016-10-28 13:58:27 +0100810 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100811 if (!i915_gem_object_has_struct_page(obj))
812 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800813
Chris Wilsone95433c2016-10-28 13:58:27 +0100814 ret = i915_gem_object_wait(obj,
815 I915_WAIT_INTERRUPTIBLE |
816 I915_WAIT_LOCKED,
817 MAX_SCHEDULE_TIMEOUT,
818 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100819 if (ret)
820 return ret;
821
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100822 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100823 if (ret)
824 return ret;
825
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100826 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
827 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000828 ret = i915_gem_object_set_to_cpu_domain(obj, false);
829 if (ret)
830 goto err_unpin;
831 else
832 goto out;
833 }
834
Chris Wilsonef749212017-04-12 12:01:10 +0100835 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100836
Chris Wilson43394c72016-08-18 17:16:47 +0100837 /* If we're not in the cpu read domain, set ourself into the gtt
838 * read domain and manually flush cachelines (if required). This
839 * optimizes for the case when the gpu will dirty the data
840 * anyway again before the next pread happens.
841 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100842 if (!obj->cache_dirty &&
Christian Königc0a51fd2018-02-16 13:43:38 +0100843 !(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000844 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800845
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000846out:
Chris Wilson97649512016-08-18 17:16:50 +0100847 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100848 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100849
850err_unpin:
851 i915_gem_object_unpin_pages(obj);
852 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100853}
854
855int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
856 unsigned int *needs_clflush)
857{
858 int ret;
859
Chris Wilsone95433c2016-10-28 13:58:27 +0100860 lockdep_assert_held(&obj->base.dev->struct_mutex);
861
Chris Wilson43394c72016-08-18 17:16:47 +0100862 *needs_clflush = 0;
863 if (!i915_gem_object_has_struct_page(obj))
864 return -ENODEV;
865
Chris Wilsone95433c2016-10-28 13:58:27 +0100866 ret = i915_gem_object_wait(obj,
867 I915_WAIT_INTERRUPTIBLE |
868 I915_WAIT_LOCKED |
869 I915_WAIT_ALL,
870 MAX_SCHEDULE_TIMEOUT,
871 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100872 if (ret)
873 return ret;
874
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100875 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100876 if (ret)
877 return ret;
878
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100879 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
880 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000881 ret = i915_gem_object_set_to_cpu_domain(obj, true);
882 if (ret)
883 goto err_unpin;
884 else
885 goto out;
886 }
887
Chris Wilsonef749212017-04-12 12:01:10 +0100888 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100889
Chris Wilson43394c72016-08-18 17:16:47 +0100890 /* If we're not in the cpu write domain, set ourself into the
891 * gtt write domain and manually flush cachelines (as required).
892 * This optimizes for the case when the gpu will use the data
893 * right away and we therefore have to clflush anyway.
894 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100895 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000896 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100897
Chris Wilsone27ab732017-06-15 13:38:49 +0100898 /*
899 * Same trick applies to invalidate partially written
900 * cachelines read before writing.
901 */
Christian Königc0a51fd2018-02-16 13:43:38 +0100902 if (!(obj->read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilsone27ab732017-06-15 13:38:49 +0100903 *needs_clflush |= CLFLUSH_BEFORE;
904 }
Chris Wilson43394c72016-08-18 17:16:47 +0100905
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000906out:
Chris Wilson43394c72016-08-18 17:16:47 +0100907 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100908 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100909 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100910 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100911
912err_unpin:
913 i915_gem_object_unpin_pages(obj);
914 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800915}
916
Daniel Vetter23c18c72012-03-25 19:47:42 +0200917static void
918shmem_clflush_swizzled_range(char *addr, unsigned long length,
919 bool swizzled)
920{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200921 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200922 unsigned long start = (unsigned long) addr;
923 unsigned long end = (unsigned long) addr + length;
924
925 /* For swizzling simply ensure that we always flush both
926 * channels. Lame, but simple and it works. Swizzled
927 * pwrite/pread is far from a hotpath - current userspace
928 * doesn't use it at all. */
929 start = round_down(start, 128);
930 end = round_up(end, 128);
931
932 drm_clflush_virt_range((void *)start, end - start);
933 } else {
934 drm_clflush_virt_range(addr, length);
935 }
936
937}
938
Daniel Vetterd174bd62012-03-25 19:47:40 +0200939/* Only difference to the fast-path function is that this can handle bit17
940 * and uses non-atomic copy and kmap functions. */
941static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100942shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200943 char __user *user_data,
944 bool page_do_bit17_swizzling, bool needs_clflush)
945{
946 char *vaddr;
947 int ret;
948
949 vaddr = kmap(page);
950 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100951 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200952 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200953
954 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100955 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200956 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100957 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200958 kunmap(page);
959
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100960 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200961}
962
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100963static int
964shmem_pread(struct page *page, int offset, int length, char __user *user_data,
965 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530966{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100967 int ret;
968
969 ret = -ENODEV;
970 if (!page_do_bit17_swizzling) {
971 char *vaddr = kmap_atomic(page);
972
973 if (needs_clflush)
974 drm_clflush_virt_range(vaddr + offset, length);
975 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
976 kunmap_atomic(vaddr);
977 }
978 if (ret == 0)
979 return 0;
980
981 return shmem_pread_slow(page, offset, length, user_data,
982 page_do_bit17_swizzling, needs_clflush);
983}
984
985static int
986i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
987 struct drm_i915_gem_pread *args)
988{
989 char __user *user_data;
990 u64 remain;
991 unsigned int obj_do_bit17_swizzling;
992 unsigned int needs_clflush;
993 unsigned int idx, offset;
994 int ret;
995
996 obj_do_bit17_swizzling = 0;
997 if (i915_gem_object_needs_bit17_swizzle(obj))
998 obj_do_bit17_swizzling = BIT(17);
999
1000 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
1001 if (ret)
1002 return ret;
1003
1004 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
1005 mutex_unlock(&obj->base.dev->struct_mutex);
1006 if (ret)
1007 return ret;
1008
1009 remain = args->size;
1010 user_data = u64_to_user_ptr(args->data_ptr);
1011 offset = offset_in_page(args->offset);
1012 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1013 struct page *page = i915_gem_object_get_page(obj, idx);
1014 int length;
1015
1016 length = remain;
1017 if (offset + length > PAGE_SIZE)
1018 length = PAGE_SIZE - offset;
1019
1020 ret = shmem_pread(page, offset, length, user_data,
1021 page_to_phys(page) & obj_do_bit17_swizzling,
1022 needs_clflush);
1023 if (ret)
1024 break;
1025
1026 remain -= length;
1027 user_data += length;
1028 offset = 0;
1029 }
1030
1031 i915_gem_obj_finish_shmem_access(obj);
1032 return ret;
1033}
1034
1035static inline bool
1036gtt_user_read(struct io_mapping *mapping,
1037 loff_t base, int offset,
1038 char __user *user_data, int length)
1039{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001040 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001041 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301042
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301043 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001044 vaddr = io_mapping_map_atomic_wc(mapping, base);
1045 unwritten = __copy_to_user_inatomic(user_data,
1046 (void __force *)vaddr + offset,
1047 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001048 io_mapping_unmap_atomic(vaddr);
1049 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001050 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1051 unwritten = copy_to_user(user_data,
1052 (void __force *)vaddr + offset,
1053 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001054 io_mapping_unmap(vaddr);
1055 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301056 return unwritten;
1057}
1058
1059static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001060i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1061 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301062{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001063 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1064 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301065 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001066 struct i915_vma *vma;
1067 void __user *user_data;
1068 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301069 int ret;
1070
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001071 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1072 if (ret)
1073 return ret;
1074
1075 intel_runtime_pm_get(i915);
1076 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001077 PIN_MAPPABLE |
1078 PIN_NONFAULT |
1079 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001080 if (!IS_ERR(vma)) {
1081 node.start = i915_ggtt_offset(vma);
1082 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001083 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001084 if (ret) {
1085 i915_vma_unpin(vma);
1086 vma = ERR_PTR(ret);
1087 }
1088 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001089 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001090 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301091 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001092 goto out_unlock;
1093 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301094 }
1095
1096 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1097 if (ret)
1098 goto out_unpin;
1099
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001100 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301101
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001102 user_data = u64_to_user_ptr(args->data_ptr);
1103 remain = args->size;
1104 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301105
1106 while (remain > 0) {
1107 /* Operation in this page
1108 *
1109 * page_base = page offset within aperture
1110 * page_offset = offset within page
1111 * page_length = bytes to copy for this page
1112 */
1113 u32 page_base = node.start;
1114 unsigned page_offset = offset_in_page(offset);
1115 unsigned page_length = PAGE_SIZE - page_offset;
1116 page_length = remain < page_length ? remain : page_length;
1117 if (node.allocated) {
1118 wmb();
1119 ggtt->base.insert_page(&ggtt->base,
1120 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001121 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301122 wmb();
1123 } else {
1124 page_base += offset & PAGE_MASK;
1125 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001126
Matthew Auld73ebd502017-12-11 15:18:20 +00001127 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001128 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301129 ret = -EFAULT;
1130 break;
1131 }
1132
1133 remain -= page_length;
1134 user_data += page_length;
1135 offset += page_length;
1136 }
1137
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001138 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301139out_unpin:
1140 if (node.allocated) {
1141 wmb();
1142 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001143 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301144 remove_mappable_node(&node);
1145 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001146 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301147 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001148out_unlock:
1149 intel_runtime_pm_put(i915);
1150 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001151
Eric Anholteb014592009-03-10 11:44:52 -07001152 return ret;
1153}
1154
Eric Anholt673a3942008-07-30 12:06:12 -07001155/**
1156 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001157 * @dev: drm device pointer
1158 * @data: ioctl data blob
1159 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001160 *
1161 * On error, the contents of *data are undefined.
1162 */
1163int
1164i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001165 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001166{
1167 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001168 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001169 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001170
Chris Wilson51311d02010-11-17 09:10:42 +00001171 if (args->size == 0)
1172 return 0;
1173
1174 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001175 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001176 args->size))
1177 return -EFAULT;
1178
Chris Wilson03ac0642016-07-20 13:31:51 +01001179 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001180 if (!obj)
1181 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001182
Chris Wilson7dcd2492010-09-26 20:21:44 +01001183 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001184 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001185 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001186 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001187 }
1188
Chris Wilsondb53a302011-02-03 11:57:46 +00001189 trace_i915_gem_object_pread(obj, args->offset, args->size);
1190
Chris Wilsone95433c2016-10-28 13:58:27 +01001191 ret = i915_gem_object_wait(obj,
1192 I915_WAIT_INTERRUPTIBLE,
1193 MAX_SCHEDULE_TIMEOUT,
1194 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001195 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001196 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001197
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001198 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001199 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001200 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001201
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001202 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001203 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001204 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301205
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001206 i915_gem_object_unpin_pages(obj);
1207out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001208 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001209 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001210}
1211
Keith Packard0839ccb2008-10-30 19:38:48 -07001212/* This is the fast write path which cannot handle
1213 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001214 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001215
Chris Wilsonfe115622016-10-28 13:58:40 +01001216static inline bool
1217ggtt_write(struct io_mapping *mapping,
1218 loff_t base, int offset,
1219 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001220{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001221 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001222 unsigned long unwritten;
1223
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001224 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001225 vaddr = io_mapping_map_atomic_wc(mapping, base);
1226 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001227 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001228 io_mapping_unmap_atomic(vaddr);
1229 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001230 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1231 unwritten = copy_from_user((void __force *)vaddr + offset,
1232 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001233 io_mapping_unmap(vaddr);
1234 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001235
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001236 return unwritten;
1237}
1238
Eric Anholt3de09aa2009-03-09 09:42:23 -07001239/**
1240 * This is the fast pwrite path, where we copy the data directly from the
1241 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001242 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001243 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001244 */
Eric Anholt673a3942008-07-30 12:06:12 -07001245static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001246i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1247 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001248{
Chris Wilsonfe115622016-10-28 13:58:40 +01001249 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301250 struct i915_ggtt *ggtt = &i915->ggtt;
1251 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001252 struct i915_vma *vma;
1253 u64 remain, offset;
1254 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301255 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301256
Chris Wilsonfe115622016-10-28 13:58:40 +01001257 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1258 if (ret)
1259 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001260
Chris Wilson8bd81812017-10-19 07:37:33 +01001261 if (i915_gem_object_has_struct_page(obj)) {
1262 /*
1263 * Avoid waking the device up if we can fallback, as
1264 * waking/resuming is very slow (worst-case 10-100 ms
1265 * depending on PCI sleeps and our own resume time).
1266 * This easily dwarfs any performance advantage from
1267 * using the cache bypass of indirect GGTT access.
1268 */
1269 if (!intel_runtime_pm_get_if_in_use(i915)) {
1270 ret = -EFAULT;
1271 goto out_unlock;
1272 }
1273 } else {
1274 /* No backing pages, no fallback, we must force GGTT access */
1275 intel_runtime_pm_get(i915);
1276 }
1277
Chris Wilson058d88c2016-08-15 10:49:06 +01001278 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001279 PIN_MAPPABLE |
1280 PIN_NONFAULT |
1281 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001282 if (!IS_ERR(vma)) {
1283 node.start = i915_ggtt_offset(vma);
1284 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001285 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001286 if (ret) {
1287 i915_vma_unpin(vma);
1288 vma = ERR_PTR(ret);
1289 }
1290 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001291 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001292 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301293 if (ret)
Chris Wilson8bd81812017-10-19 07:37:33 +01001294 goto out_rpm;
Chris Wilsonfe115622016-10-28 13:58:40 +01001295 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301296 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001297
1298 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1299 if (ret)
1300 goto out_unpin;
1301
Chris Wilsonfe115622016-10-28 13:58:40 +01001302 mutex_unlock(&i915->drm.struct_mutex);
1303
Chris Wilsonb19482d2016-08-18 17:16:43 +01001304 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001305
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301306 user_data = u64_to_user_ptr(args->data_ptr);
1307 offset = args->offset;
1308 remain = args->size;
1309 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001310 /* Operation in this page
1311 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001312 * page_base = page offset within aperture
1313 * page_offset = offset within page
1314 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001315 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301316 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001317 unsigned int page_offset = offset_in_page(offset);
1318 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301319 page_length = remain < page_length ? remain : page_length;
1320 if (node.allocated) {
1321 wmb(); /* flush the write before we modify the GGTT */
1322 ggtt->base.insert_page(&ggtt->base,
1323 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1324 node.start, I915_CACHE_NONE, 0);
1325 wmb(); /* flush modifications to the GGTT (insert_page) */
1326 } else {
1327 page_base += offset & PAGE_MASK;
1328 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001329 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001330 * source page isn't available. Return the error and we'll
1331 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301332 * If the object is non-shmem backed, we retry again with the
1333 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001334 */
Matthew Auld73ebd502017-12-11 15:18:20 +00001335 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
Chris Wilsonfe115622016-10-28 13:58:40 +01001336 user_data, page_length)) {
1337 ret = -EFAULT;
1338 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001339 }
Eric Anholt673a3942008-07-30 12:06:12 -07001340
Keith Packard0839ccb2008-10-30 19:38:48 -07001341 remain -= page_length;
1342 user_data += page_length;
1343 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001344 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001345 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001346
1347 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001348out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301349 if (node.allocated) {
1350 wmb();
1351 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001352 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301353 remove_mappable_node(&node);
1354 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001355 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301356 }
Chris Wilson8bd81812017-10-19 07:37:33 +01001357out_rpm:
Chris Wilson9c870d02016-10-24 13:42:15 +01001358 intel_runtime_pm_put(i915);
Chris Wilson8bd81812017-10-19 07:37:33 +01001359out_unlock:
Chris Wilsonfe115622016-10-28 13:58:40 +01001360 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001361 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001362}
1363
Eric Anholt673a3942008-07-30 12:06:12 -07001364static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001365shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001366 char __user *user_data,
1367 bool page_do_bit17_swizzling,
1368 bool needs_clflush_before,
1369 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001370{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001371 char *vaddr;
1372 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001373
Daniel Vetterd174bd62012-03-25 19:47:40 +02001374 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001375 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001376 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001377 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001378 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001379 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1380 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001381 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001382 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001383 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001384 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001385 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001386 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001387
Chris Wilson755d2212012-09-04 21:02:55 +01001388 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001389}
1390
Chris Wilsonfe115622016-10-28 13:58:40 +01001391/* Per-page copy function for the shmem pwrite fastpath.
1392 * Flushes invalid cachelines before writing to the target if
1393 * needs_clflush_before is set and flushes out any written cachelines after
1394 * writing if needs_clflush is set.
1395 */
Eric Anholt40123c12009-03-09 13:42:30 -07001396static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001397shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1398 bool page_do_bit17_swizzling,
1399 bool needs_clflush_before,
1400 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001401{
Chris Wilsonfe115622016-10-28 13:58:40 +01001402 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001403
Chris Wilsonfe115622016-10-28 13:58:40 +01001404 ret = -ENODEV;
1405 if (!page_do_bit17_swizzling) {
1406 char *vaddr = kmap_atomic(page);
1407
1408 if (needs_clflush_before)
1409 drm_clflush_virt_range(vaddr + offset, len);
1410 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1411 if (needs_clflush_after)
1412 drm_clflush_virt_range(vaddr + offset, len);
1413
1414 kunmap_atomic(vaddr);
1415 }
1416 if (ret == 0)
1417 return ret;
1418
1419 return shmem_pwrite_slow(page, offset, len, user_data,
1420 page_do_bit17_swizzling,
1421 needs_clflush_before,
1422 needs_clflush_after);
1423}
1424
1425static int
1426i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1427 const struct drm_i915_gem_pwrite *args)
1428{
1429 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1430 void __user *user_data;
1431 u64 remain;
1432 unsigned int obj_do_bit17_swizzling;
1433 unsigned int partial_cacheline_write;
1434 unsigned int needs_clflush;
1435 unsigned int offset, idx;
1436 int ret;
1437
1438 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001439 if (ret)
1440 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001441
Chris Wilsonfe115622016-10-28 13:58:40 +01001442 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1443 mutex_unlock(&i915->drm.struct_mutex);
1444 if (ret)
1445 return ret;
1446
1447 obj_do_bit17_swizzling = 0;
1448 if (i915_gem_object_needs_bit17_swizzle(obj))
1449 obj_do_bit17_swizzling = BIT(17);
1450
1451 /* If we don't overwrite a cacheline completely we need to be
1452 * careful to have up-to-date data by first clflushing. Don't
1453 * overcomplicate things and flush the entire patch.
1454 */
1455 partial_cacheline_write = 0;
1456 if (needs_clflush & CLFLUSH_BEFORE)
1457 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1458
Chris Wilson43394c72016-08-18 17:16:47 +01001459 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001460 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001461 offset = offset_in_page(args->offset);
1462 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1463 struct page *page = i915_gem_object_get_page(obj, idx);
1464 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001465
Chris Wilsonfe115622016-10-28 13:58:40 +01001466 length = remain;
1467 if (offset + length > PAGE_SIZE)
1468 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001469
Chris Wilsonfe115622016-10-28 13:58:40 +01001470 ret = shmem_pwrite(page, offset, length, user_data,
1471 page_to_phys(page) & obj_do_bit17_swizzling,
1472 (offset | length) & partial_cacheline_write,
1473 needs_clflush & CLFLUSH_AFTER);
1474 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001475 break;
1476
Chris Wilsonfe115622016-10-28 13:58:40 +01001477 remain -= length;
1478 user_data += length;
1479 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001480 }
1481
Chris Wilsond59b21e2017-02-22 11:40:49 +00001482 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001483 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001484 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001485}
1486
1487/**
1488 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001489 * @dev: drm device
1490 * @data: ioctl data blob
1491 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001492 *
1493 * On error, the contents of the buffer that were to be modified are undefined.
1494 */
1495int
1496i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001497 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001498{
1499 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001500 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001501 int ret;
1502
1503 if (args->size == 0)
1504 return 0;
1505
1506 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001507 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001508 args->size))
1509 return -EFAULT;
1510
Chris Wilson03ac0642016-07-20 13:31:51 +01001511 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001512 if (!obj)
1513 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001514
Chris Wilson7dcd2492010-09-26 20:21:44 +01001515 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001516 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001517 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001518 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001519 }
1520
Chris Wilsondb53a302011-02-03 11:57:46 +00001521 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1522
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001523 ret = -ENODEV;
1524 if (obj->ops->pwrite)
1525 ret = obj->ops->pwrite(obj, args);
1526 if (ret != -ENODEV)
1527 goto err;
1528
Chris Wilsone95433c2016-10-28 13:58:27 +01001529 ret = i915_gem_object_wait(obj,
1530 I915_WAIT_INTERRUPTIBLE |
1531 I915_WAIT_ALL,
1532 MAX_SCHEDULE_TIMEOUT,
1533 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001534 if (ret)
1535 goto err;
1536
Chris Wilsonfe115622016-10-28 13:58:40 +01001537 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001538 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001539 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001540
Daniel Vetter935aaa62012-03-25 19:47:35 +02001541 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001542 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1543 * it would end up going through the fenced access, and we'll get
1544 * different detiling behavior between reading and writing.
1545 * pread/pwrite currently are reading and writing from the CPU
1546 * perspective, requiring manual detiling by the client.
1547 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001548 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001549 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001550 /* Note that the gtt paths might fail with non-page-backed user
1551 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001552 * textures). Fallback to the shmem path in that case.
1553 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001554 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001555
Chris Wilsond1054ee2016-07-16 18:42:36 +01001556 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001557 if (obj->phys_handle)
1558 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301559 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001560 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001561 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001562
Chris Wilsonfe115622016-10-28 13:58:40 +01001563 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001564err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001565 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001566 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001567}
1568
Chris Wilson40e62d52016-10-28 13:58:41 +01001569static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1570{
1571 struct drm_i915_private *i915;
1572 struct list_head *list;
1573 struct i915_vma *vma;
1574
Chris Wilsonf2123812017-10-16 12:40:37 +01001575 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
1576
Chris Wilsone2189dd2017-12-07 21:14:07 +00001577 for_each_ggtt_vma(vma, obj) {
Chris Wilson40e62d52016-10-28 13:58:41 +01001578 if (i915_vma_is_active(vma))
1579 continue;
1580
1581 if (!drm_mm_node_allocated(&vma->node))
1582 continue;
1583
1584 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1585 }
1586
1587 i915 = to_i915(obj->base.dev);
Chris Wilsonf2123812017-10-16 12:40:37 +01001588 spin_lock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001589 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Chris Wilsonf2123812017-10-16 12:40:37 +01001590 list_move_tail(&obj->mm.link, list);
1591 spin_unlock(&i915->mm.obj_lock);
Chris Wilson40e62d52016-10-28 13:58:41 +01001592}
1593
Eric Anholt673a3942008-07-30 12:06:12 -07001594/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001595 * Called when user space prepares to use an object with the CPU, either
1596 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001597 * @dev: drm device
1598 * @data: ioctl data blob
1599 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001600 */
1601int
1602i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001603 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001604{
1605 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001606 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001607 uint32_t read_domains = args->read_domains;
1608 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001609 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001610
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001611 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001612 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001613 return -EINVAL;
1614
1615 /* Having something in the write domain implies it's in the read
1616 * domain, and only that read domain. Enforce that in the request.
1617 */
1618 if (write_domain != 0 && read_domains != write_domain)
1619 return -EINVAL;
1620
Chris Wilson03ac0642016-07-20 13:31:51 +01001621 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001622 if (!obj)
1623 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001624
Chris Wilson3236f572012-08-24 09:35:09 +01001625 /* Try to flush the object off the GPU without holding the lock.
1626 * We will repeat the flush holding the lock in the normal manner
1627 * to catch cases where we are gazumped.
1628 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001629 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001630 I915_WAIT_INTERRUPTIBLE |
1631 (write_domain ? I915_WAIT_ALL : 0),
1632 MAX_SCHEDULE_TIMEOUT,
1633 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001634 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001635 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001636
Tina Zhanga03f3952017-11-14 10:25:13 +00001637 /*
1638 * Proxy objects do not control access to the backing storage, ergo
1639 * they cannot be used as a means to manipulate the cache domain
1640 * tracking for that backing storage. The proxy object is always
1641 * considered to be outside of any cache domain.
1642 */
1643 if (i915_gem_object_is_proxy(obj)) {
1644 err = -ENXIO;
1645 goto out;
1646 }
1647
1648 /*
1649 * Flush and acquire obj->pages so that we are coherent through
Chris Wilson40e62d52016-10-28 13:58:41 +01001650 * direct access in memory with previous cached writes through
1651 * shmemfs and that our cache domain tracking remains valid.
1652 * For example, if the obj->filp was moved to swap without us
1653 * being notified and releasing the pages, we would mistakenly
1654 * continue to assume that the obj remained out of the CPU cached
1655 * domain.
1656 */
1657 err = i915_gem_object_pin_pages(obj);
1658 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001659 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001660
1661 err = i915_mutex_lock_interruptible(dev);
1662 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001663 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001664
Chris Wilsone22d8e32017-04-12 12:01:11 +01001665 if (read_domains & I915_GEM_DOMAIN_WC)
1666 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1667 else if (read_domains & I915_GEM_DOMAIN_GTT)
1668 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301669 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001670 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001671
1672 /* And bump the LRU for this access */
1673 i915_gem_object_bump_inactive_ggtt(obj);
1674
1675 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001676
Daniel Vetter031b6982015-06-26 19:35:16 +02001677 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001678 intel_fb_obj_invalidate(obj,
1679 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001680
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001681out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001682 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001683out:
1684 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001685 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001686}
1687
1688/**
1689 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001690 * @dev: drm device
1691 * @data: ioctl data blob
1692 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001693 */
1694int
1695i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001696 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001697{
1698 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001699 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001700
Chris Wilson03ac0642016-07-20 13:31:51 +01001701 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001702 if (!obj)
1703 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001704
Tina Zhanga03f3952017-11-14 10:25:13 +00001705 /*
1706 * Proxy objects are barred from CPU access, so there is no
1707 * need to ban sw_finish as it is a nop.
1708 */
1709
Eric Anholt673a3942008-07-30 12:06:12 -07001710 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001711 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001712 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001713
1714 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001715}
1716
1717/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001718 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1719 * it is mapped to.
1720 * @dev: drm device
1721 * @data: ioctl data blob
1722 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001723 *
1724 * While the mapping holds a reference on the contents of the object, it doesn't
1725 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001726 *
1727 * IMPORTANT:
1728 *
1729 * DRM driver writers who look a this function as an example for how to do GEM
1730 * mmap support, please don't implement mmap support like here. The modern way
1731 * to implement DRM mmap support is with an mmap offset ioctl (like
1732 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1733 * That way debug tooling like valgrind will understand what's going on, hiding
1734 * the mmap call in a driver private ioctl will break that. The i915 driver only
1735 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001736 */
1737int
1738i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001739 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001740{
1741 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001742 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001743 unsigned long addr;
1744
Akash Goel1816f922015-01-02 16:29:30 +05301745 if (args->flags & ~(I915_MMAP_WC))
1746 return -EINVAL;
1747
Borislav Petkov568a58e2016-03-29 17:42:01 +02001748 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301749 return -ENODEV;
1750
Chris Wilson03ac0642016-07-20 13:31:51 +01001751 obj = i915_gem_object_lookup(file, args->handle);
1752 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001753 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001754
Daniel Vetter1286ff72012-05-10 15:25:09 +02001755 /* prime objects have no backing filp to GEM mmap
1756 * pages from.
1757 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001758 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001759 i915_gem_object_put(obj);
Tina Zhang274b2462017-11-14 10:25:12 +00001760 return -ENXIO;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001761 }
1762
Chris Wilson03ac0642016-07-20 13:31:51 +01001763 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001764 PROT_READ | PROT_WRITE, MAP_SHARED,
1765 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301766 if (args->flags & I915_MMAP_WC) {
1767 struct mm_struct *mm = current->mm;
1768 struct vm_area_struct *vma;
1769
Michal Hocko80a89a52016-05-23 16:26:11 -07001770 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001771 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001772 return -EINTR;
1773 }
Akash Goel1816f922015-01-02 16:29:30 +05301774 vma = find_vma(mm, addr);
1775 if (vma)
1776 vma->vm_page_prot =
1777 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1778 else
1779 addr = -ENOMEM;
1780 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001781
1782 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001783 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301784 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001785 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001786 if (IS_ERR((void *)addr))
1787 return addr;
1788
1789 args->addr_ptr = (uint64_t) addr;
1790
1791 return 0;
1792}
1793
Chris Wilson03af84f2016-08-18 17:17:01 +01001794static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1795{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001796 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001797}
1798
Jesse Barnesde151cf2008-11-12 10:03:55 -08001799/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001800 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1801 *
1802 * A history of the GTT mmap interface:
1803 *
1804 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1805 * aligned and suitable for fencing, and still fit into the available
1806 * mappable space left by the pinned display objects. A classic problem
1807 * we called the page-fault-of-doom where we would ping-pong between
1808 * two objects that could not fit inside the GTT and so the memcpy
1809 * would page one object in at the expense of the other between every
1810 * single byte.
1811 *
1812 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1813 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1814 * object is too large for the available space (or simply too large
1815 * for the mappable aperture!), a view is created instead and faulted
1816 * into userspace. (This view is aligned and sized appropriately for
1817 * fenced access.)
1818 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001819 * 2 - Recognise WC as a separate cache domain so that we can flush the
1820 * delayed writes via GTT before performing direct access via WC.
1821 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001822 * Restrictions:
1823 *
1824 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1825 * hangs on some architectures, corruption on others. An attempt to service
1826 * a GTT page fault from a snoopable object will generate a SIGBUS.
1827 *
1828 * * the object must be able to fit into RAM (physical memory, though no
1829 * limited to the mappable aperture).
1830 *
1831 *
1832 * Caveats:
1833 *
1834 * * a new GTT page fault will synchronize rendering from the GPU and flush
1835 * all data to system memory. Subsequent access will not be synchronized.
1836 *
1837 * * all mappings are revoked on runtime device suspend.
1838 *
1839 * * there are only 8, 16 or 32 fence registers to share between all users
1840 * (older machines require fence register for display and blitter access
1841 * as well). Contention of the fence registers will cause the previous users
1842 * to be unmapped and any new access will generate new page faults.
1843 *
1844 * * running out of memory while servicing a fault may generate a SIGBUS,
1845 * rather than the expected SIGSEGV.
1846 */
1847int i915_gem_mmap_gtt_version(void)
1848{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001849 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001850}
1851
Chris Wilson2d4281b2017-01-10 09:56:32 +00001852static inline struct i915_ggtt_view
1853compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001854 pgoff_t page_offset,
1855 unsigned int chunk)
1856{
1857 struct i915_ggtt_view view;
1858
1859 if (i915_gem_object_is_tiled(obj))
1860 chunk = roundup(chunk, tile_row_pages(obj));
1861
Chris Wilson2d4281b2017-01-10 09:56:32 +00001862 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001863 view.partial.offset = rounddown(page_offset, chunk);
1864 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001865 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001866 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001867
1868 /* If the partial covers the entire object, just create a normal VMA. */
1869 if (chunk >= obj->base.size >> PAGE_SHIFT)
1870 view.type = I915_GGTT_VIEW_NORMAL;
1871
1872 return view;
1873}
1874
Chris Wilson4cc69072016-08-25 19:05:19 +01001875/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001876 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001877 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001878 *
1879 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1880 * from userspace. The fault handler takes care of binding the object to
1881 * the GTT (if needed), allocating and programming a fence register (again,
1882 * only if needed based on whether the old reg is still valid or the object
1883 * is tiled) and inserting a new PTE into the faulting process.
1884 *
1885 * Note that the faulting process may involve evicting existing objects
1886 * from the GTT and/or fence registers to make room. So performance may
1887 * suffer if the GTT working set is large or there are few fence registers
1888 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001889 *
1890 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1891 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001892 */
Dave Jiang11bac802017-02-24 14:56:41 -08001893int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001894{
Chris Wilson03af84f2016-08-18 17:17:01 +01001895#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001896 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001897 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001898 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001899 struct drm_i915_private *dev_priv = to_i915(dev);
1900 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001901 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001902 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001903 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001904 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001905 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001906
Jesse Barnesde151cf2008-11-12 10:03:55 -08001907 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001908 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001909
Chris Wilsondb53a302011-02-03 11:57:46 +00001910 trace_i915_gem_object_fault(obj, page_offset, true, write);
1911
Chris Wilson6e4930f2014-02-07 18:37:06 -02001912 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001913 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001914 * repeat the flush holding the lock in the normal manner to catch cases
1915 * where we are gazumped.
1916 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001917 ret = i915_gem_object_wait(obj,
1918 I915_WAIT_INTERRUPTIBLE,
1919 MAX_SCHEDULE_TIMEOUT,
1920 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001921 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001922 goto err;
1923
Chris Wilson40e62d52016-10-28 13:58:41 +01001924 ret = i915_gem_object_pin_pages(obj);
1925 if (ret)
1926 goto err;
1927
Chris Wilsonb8f90962016-08-05 10:14:07 +01001928 intel_runtime_pm_get(dev_priv);
1929
1930 ret = i915_mutex_lock_interruptible(dev);
1931 if (ret)
1932 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001933
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001934 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001935 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001936 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001937 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001938 }
1939
Chris Wilson82118872016-08-18 17:17:05 +01001940 /* If the object is smaller than a couple of partial vma, it is
1941 * not worth only creating a single partial vma - we may as well
1942 * clear enough space for the full object.
1943 */
1944 flags = PIN_MAPPABLE;
1945 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1946 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1947
Chris Wilsona61007a2016-08-18 17:17:02 +01001948 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001949 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001950 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001951 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001952 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001953 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001954
Chris Wilson50349242016-08-18 17:17:04 +01001955 /* Userspace is now writing through an untracked VMA, abandon
1956 * all hope that the hardware is able to track future writes.
1957 */
1958 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1959
Chris Wilsona61007a2016-08-18 17:17:02 +01001960 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1961 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001962 if (IS_ERR(vma)) {
1963 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001964 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001965 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001966
Chris Wilsonc9839302012-11-20 10:45:17 +00001967 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1968 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001969 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001970
Chris Wilson3bd40732017-10-09 09:43:56 +01001971 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001972 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001973 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001974
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001975 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001976 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001977 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Matthew Auld73ebd502017-12-11 15:18:20 +00001978 (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
Chris Wilsonc58305a2016-08-19 16:54:28 +01001979 min_t(u64, vma->size, area->vm_end - area->vm_start),
Matthew Auld73ebd502017-12-11 15:18:20 +00001980 &ggtt->iomap);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001981 if (ret)
1982 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001983
Chris Wilsona65adaf2017-10-09 09:43:57 +01001984 /* Mark as being mmapped into userspace for later revocation */
1985 assert_rpm_wakelock_held(dev_priv);
1986 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1987 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1988 GEM_BUG_ON(!obj->userfault_count);
1989
Chris Wilson71253972017-12-06 12:49:14 +00001990 i915_vma_set_ggtt_write(vma);
1991
Chris Wilsona65adaf2017-10-09 09:43:57 +01001992err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001993 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001994err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001995 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001996err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001997 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001998err_rpm:
1999 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01002000 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01002001err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002002 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002003 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02002004 /*
2005 * We eat errors when the gpu is terminally wedged to avoid
2006 * userspace unduly crashing (gl has no provisions for mmaps to
2007 * fail). But any other -EIO isn't ours (e.g. swap in failure)
2008 * and so needs to be reported.
2009 */
2010 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02002011 ret = VM_FAULT_SIGBUS;
2012 break;
2013 }
Chris Wilson045e7692010-11-07 09:18:22 +00002014 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02002015 /*
2016 * EAGAIN means the gpu is hung and we'll wait for the error
2017 * handler to reset everything when re-faulting in
2018 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00002019 */
Chris Wilsonc7150892009-09-23 00:43:56 +01002020 case 0:
2021 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00002022 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03002023 case -EBUSY:
2024 /*
2025 * EBUSY is ok: this just means that another thread
2026 * already did the job.
2027 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02002028 ret = VM_FAULT_NOPAGE;
2029 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002030 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002031 ret = VM_FAULT_OOM;
2032 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002033 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00002034 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02002035 ret = VM_FAULT_SIGBUS;
2036 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002037 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02002038 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02002039 ret = VM_FAULT_SIGBUS;
2040 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002041 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02002042 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002043}
2044
Chris Wilsona65adaf2017-10-09 09:43:57 +01002045static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
2046{
2047 struct i915_vma *vma;
2048
2049 GEM_BUG_ON(!obj->userfault_count);
2050
2051 obj->userfault_count = 0;
2052 list_del(&obj->userfault_link);
2053 drm_vma_node_unmap(&obj->base.vma_node,
2054 obj->base.dev->anon_inode->i_mapping);
2055
Chris Wilsone2189dd2017-12-07 21:14:07 +00002056 for_each_ggtt_vma(vma, obj)
Chris Wilsona65adaf2017-10-09 09:43:57 +01002057 i915_vma_unset_userfault(vma);
Chris Wilsona65adaf2017-10-09 09:43:57 +01002058}
2059
Jesse Barnesde151cf2008-11-12 10:03:55 -08002060/**
Chris Wilson901782b2009-07-10 08:18:50 +01002061 * i915_gem_release_mmap - remove physical page mappings
2062 * @obj: obj in question
2063 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002064 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002065 * relinquish ownership of the pages back to the system.
2066 *
2067 * It is vital that we remove the page mapping if we have mapped a tiled
2068 * object through the GTT and then lose the fence register due to
2069 * resource pressure. Similarly if the object has been moved out of the
2070 * aperture, than pages mapped into userspace must be revoked. Removing the
2071 * mapping will then trigger a page fault on the next user access, allowing
2072 * fixup by i915_gem_fault().
2073 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002074void
Chris Wilson05394f32010-11-08 19:18:58 +00002075i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002076{
Chris Wilson275f0392016-10-24 13:42:14 +01002077 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002078
Chris Wilson349f2cc2016-04-13 17:35:12 +01002079 /* Serialisation between user GTT access and our code depends upon
2080 * revoking the CPU's PTE whilst the mutex is held. The next user
2081 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002082 *
2083 * Note that RPM complicates somewhat by adding an additional
2084 * requirement that operations to the GGTT be made holding the RPM
2085 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002086 */
Chris Wilson275f0392016-10-24 13:42:14 +01002087 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002088 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002089
Chris Wilsona65adaf2017-10-09 09:43:57 +01002090 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002091 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002092
Chris Wilsona65adaf2017-10-09 09:43:57 +01002093 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002094
2095 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2096 * memory transactions from userspace before we return. The TLB
2097 * flushing implied above by changing the PTE above *should* be
2098 * sufficient, an extra barrier here just provides us with a bit
2099 * of paranoid documentation about our requirement to serialise
2100 * memory writes before touching registers / GSM.
2101 */
2102 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002103
2104out:
2105 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002106}
2107
Chris Wilson7c108fd2016-10-24 13:42:18 +01002108void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002109{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002110 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002111 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002112
Chris Wilson3594a3e2016-10-24 13:42:16 +01002113 /*
2114 * Only called during RPM suspend. All users of the userfault_list
2115 * must be holding an RPM wakeref to ensure that this can not
2116 * run concurrently with themselves (and use the struct_mutex for
2117 * protection between themselves).
2118 */
2119
2120 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002121 &dev_priv->mm.userfault_list, userfault_link)
2122 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002123
2124 /* The fence will be lost when the device powers down. If any were
2125 * in use by hardware (i.e. they are pinned), we should not be powering
2126 * down! All other fences will be reacquired by the user upon waking.
2127 */
2128 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2129 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2130
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002131 /* Ideally we want to assert that the fence register is not
2132 * live at this point (i.e. that no piece of code will be
2133 * trying to write through fence + GTT, as that both violates
2134 * our tracking of activity and associated locking/barriers,
2135 * but also is illegal given that the hw is powered down).
2136 *
2137 * Previously we used reg->pin_count as a "liveness" indicator.
2138 * That is not sufficient, and we need a more fine-grained
2139 * tool if we want to have a sanity check here.
2140 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002141
2142 if (!reg->vma)
2143 continue;
2144
Chris Wilsona65adaf2017-10-09 09:43:57 +01002145 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002146 reg->dirty = true;
2147 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002148}
2149
Chris Wilsond8cb5082012-08-11 15:41:03 +01002150static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2151{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002152 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002153 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002154
Chris Wilsonf3f61842016-08-05 10:14:14 +01002155 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002156 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002157 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002158
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002159 /* Attempt to reap some mmap space from dead objects */
2160 do {
2161 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2162 if (err)
2163 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002164
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002165 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002166 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002167 if (!err)
2168 break;
2169
2170 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002171
Chris Wilsonf3f61842016-08-05 10:14:14 +01002172 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002173}
2174
2175static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2176{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002177 drm_gem_free_mmap_offset(&obj->base);
2178}
2179
Dave Airlieda6b51d2014-12-24 13:11:17 +10002180int
Dave Airlieff72145b2011-02-07 12:16:14 +10002181i915_gem_mmap_gtt(struct drm_file *file,
2182 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002183 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002184 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002185{
Chris Wilson05394f32010-11-08 19:18:58 +00002186 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002187 int ret;
2188
Chris Wilson03ac0642016-07-20 13:31:51 +01002189 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002190 if (!obj)
2191 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002192
Chris Wilsond8cb5082012-08-11 15:41:03 +01002193 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002194 if (ret == 0)
2195 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002196
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002197 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002198 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002199}
2200
Dave Airlieff72145b2011-02-07 12:16:14 +10002201/**
2202 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2203 * @dev: DRM device
2204 * @data: GTT mapping ioctl data
2205 * @file: GEM object info
2206 *
2207 * Simply returns the fake offset to userspace so it can mmap it.
2208 * The mmap call will end up in drm_gem_mmap(), which will set things
2209 * up so we can get faults in the handler above.
2210 *
2211 * The fault handler will take care of binding the object into the GTT
2212 * (since it may have been evicted to make room for something), allocating
2213 * a fence register, and mapping the appropriate aperture address into
2214 * userspace.
2215 */
2216int
2217i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2218 struct drm_file *file)
2219{
2220 struct drm_i915_gem_mmap_gtt *args = data;
2221
Dave Airlieda6b51d2014-12-24 13:11:17 +10002222 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002223}
2224
Daniel Vetter225067e2012-08-20 10:23:20 +02002225/* Immediately discard the backing storage */
2226static void
2227i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002228{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002229 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002230
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002231 if (obj->base.filp == NULL)
2232 return;
2233
Daniel Vetter225067e2012-08-20 10:23:20 +02002234 /* Our goal here is to return as much of the memory as
2235 * is possible back to the system as we are called from OOM.
2236 * To do this we must instruct the shmfs to drop all of its
2237 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002238 */
Chris Wilson55372522014-03-25 13:23:06 +00002239 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002240 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002241 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002242}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002243
Chris Wilson55372522014-03-25 13:23:06 +00002244/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002245void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002246{
Chris Wilson55372522014-03-25 13:23:06 +00002247 struct address_space *mapping;
2248
Chris Wilson1233e2d2016-10-28 13:58:37 +01002249 lockdep_assert_held(&obj->mm.lock);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002250 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilson1233e2d2016-10-28 13:58:37 +01002251
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002252 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002253 case I915_MADV_DONTNEED:
2254 i915_gem_object_truncate(obj);
2255 case __I915_MADV_PURGED:
2256 return;
2257 }
2258
2259 if (obj->base.filp == NULL)
2260 return;
2261
Al Viro93c76a32015-12-04 23:45:44 -05002262 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002263 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002264}
2265
Chris Wilson5cdf5882010-09-27 15:51:07 +01002266static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002267i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2268 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002269{
Dave Gordon85d12252016-05-20 11:54:06 +01002270 struct sgt_iter sgt_iter;
2271 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002272
Chris Wilsone5facdf2016-12-23 14:57:57 +00002273 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002274
Chris Wilson03ac84f2016-10-28 13:58:36 +01002275 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002276
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002277 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002278 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002279
Chris Wilson03ac84f2016-10-28 13:58:36 +01002280 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002281 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002282 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002283
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002284 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002285 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002286
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002287 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002288 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002289 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002290
Chris Wilson03ac84f2016-10-28 13:58:36 +01002291 sg_free_table(pages);
2292 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002293}
2294
Chris Wilson96d77632016-10-28 13:58:33 +01002295static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2296{
2297 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002298 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002299
Chris Wilsonbea6e982017-10-26 14:00:31 +01002300 rcu_read_lock();
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002301 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2302 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilsonbea6e982017-10-26 14:00:31 +01002303 rcu_read_unlock();
Chris Wilson96d77632016-10-28 13:58:33 +01002304}
2305
Chris Wilson548625e2016-11-01 12:11:34 +00002306void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2307 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002308{
Chris Wilsonf2123812017-10-16 12:40:37 +01002309 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002310 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002311
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002312 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002313 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002314
Chris Wilson15717de2016-08-04 07:52:26 +01002315 GEM_BUG_ON(obj->bind_count);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002316 if (!i915_gem_object_has_pages(obj))
Chris Wilson1233e2d2016-10-28 13:58:37 +01002317 return;
2318
2319 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002320 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002321 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2322 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002323
Chris Wilsona2165e32012-12-03 11:49:00 +00002324 /* ->put_pages might need to allocate memory for the bit17 swizzle
2325 * array, hence protect them from being reaped by removing them from gtt
2326 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002327 pages = fetch_and_zero(&obj->mm.pages);
2328 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002329
Chris Wilsonf2123812017-10-16 12:40:37 +01002330 spin_lock(&i915->mm.obj_lock);
2331 list_del(&obj->mm.link);
2332 spin_unlock(&i915->mm.obj_lock);
2333
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002334 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002335 void *ptr;
2336
Chris Wilson0ce81782017-05-17 13:09:59 +01002337 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002338 if (is_vmalloc_addr(ptr))
2339 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002340 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002341 kunmap(kmap_to_page(ptr));
2342
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002343 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002344 }
2345
Chris Wilson96d77632016-10-28 13:58:33 +01002346 __i915_gem_object_reset_page_iter(obj);
2347
Chris Wilson4e5462e2017-03-07 13:20:31 +00002348 if (!IS_ERR(pages))
2349 obj->ops->put_pages(obj, pages);
2350
Matthew Aulda5c081662017-10-06 23:18:18 +01002351 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2352
Chris Wilson1233e2d2016-10-28 13:58:37 +01002353unlock:
2354 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002355}
2356
Chris Wilson935a2f72017-02-13 17:15:13 +00002357static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002358{
2359 struct sg_table new_st;
2360 struct scatterlist *sg, *new_sg;
2361 unsigned int i;
2362
2363 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002364 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002365
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002366 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002367 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002368
2369 new_sg = new_st.sgl;
2370 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2371 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2372 /* called before being DMA mapped, no need to copy sg->dma_* */
2373 new_sg = sg_next(new_sg);
2374 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002375 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002376
2377 sg_free_table(orig_st);
2378
2379 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002380 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002381}
2382
Matthew Auldb91b09e2017-10-06 23:18:17 +01002383static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002384{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002385 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002386 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2387 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002388 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002389 struct sg_table *st;
2390 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002391 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002392 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002393 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002394 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002395 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002396 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002397 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002398
Chris Wilson6c085a72012-08-20 11:40:46 +02002399 /* Assert that the object is not currently in any GPU domain. As it
2400 * wasn't in the GTT, there shouldn't be any way it could have been in
2401 * a GPU cache
2402 */
Christian Königc0a51fd2018-02-16 13:43:38 +01002403 GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2404 GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002405
Chris Wilson9da3da62012-06-01 15:20:22 +01002406 st = kmalloc(sizeof(*st), GFP_KERNEL);
2407 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002408 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002409
Chris Wilsond766ef52016-12-19 12:43:45 +00002410rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002411 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002412 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002413 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002414 }
2415
2416 /* Get the list of pages out of our struct file. They'll be pinned
2417 * at this point until we release them.
2418 *
2419 * Fail silently without starting the shrinker
2420 */
Al Viro93c76a32015-12-04 23:45:44 -05002421 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002422 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002423 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2424
Imre Deak90797e62013-02-18 19:28:03 +02002425 sg = st->sgl;
2426 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002427 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002428 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002429 const unsigned int shrink[] = {
2430 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2431 0,
2432 }, *s = shrink;
2433 gfp_t gfp = noreclaim;
2434
2435 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002436 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002437 if (likely(!IS_ERR(page)))
2438 break;
2439
2440 if (!*s) {
2441 ret = PTR_ERR(page);
2442 goto err_sg;
2443 }
2444
Chris Wilson912d5722017-09-06 16:19:30 -07002445 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002446 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002447
Chris Wilson6c085a72012-08-20 11:40:46 +02002448 /* We've tried hard to allocate the memory by reaping
2449 * our own buffer, now let the real VM do its job and
2450 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002451 *
2452 * However, since graphics tend to be disposable,
2453 * defer the oom here by reporting the ENOMEM back
2454 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002455 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002456 if (!*s) {
2457 /* reclaim and warn, but no oom */
2458 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002459
2460 /* Our bo are always dirty and so we require
2461 * kswapd to reclaim our pages (direct reclaim
2462 * does not effectively begin pageout of our
2463 * buffers on its own). However, direct reclaim
2464 * only waits for kswapd when under allocation
2465 * congestion. So as a result __GFP_RECLAIM is
2466 * unreliable and fails to actually reclaim our
2467 * dirty pages -- unless you try over and over
2468 * again with !__GFP_NORETRY. However, we still
2469 * want to fail this allocation rather than
2470 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002471 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002472 */
Michal Hockodbb32952017-07-12 14:36:55 -07002473 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002474 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002475 } while (1);
2476
Chris Wilson871dfbd2016-10-11 09:20:21 +01002477 if (!i ||
2478 sg->length >= max_segment ||
2479 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002480 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002481 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002482 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002483 }
Imre Deak90797e62013-02-18 19:28:03 +02002484 st->nents++;
2485 sg_set_page(sg, page, PAGE_SIZE, 0);
2486 } else {
2487 sg->length += PAGE_SIZE;
2488 }
2489 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002490
2491 /* Check that the i965g/gm workaround works. */
2492 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002493 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002494 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002495 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002496 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002497 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002498
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002499 /* Trim unused sg entries to avoid wasting memory. */
2500 i915_sg_trim(st);
2501
Chris Wilson03ac84f2016-10-28 13:58:36 +01002502 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002503 if (ret) {
2504 /* DMA remapping failed? One possible cause is that
2505 * it could not reserve enough large entries, asking
2506 * for PAGE_SIZE chunks instead may be helpful.
2507 */
2508 if (max_segment > PAGE_SIZE) {
2509 for_each_sgt_page(page, sgt_iter, st)
2510 put_page(page);
2511 sg_free_table(st);
2512
2513 max_segment = PAGE_SIZE;
2514 goto rebuild_st;
2515 } else {
2516 dev_warn(&dev_priv->drm.pdev->dev,
2517 "Failed to DMA remap %lu pages\n",
2518 page_count);
2519 goto err_pages;
2520 }
2521 }
Imre Deake2273302015-07-09 12:59:05 +03002522
Eric Anholt673a3942008-07-30 12:06:12 -07002523 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002524 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002525
Matthew Auld84e89782017-10-09 12:00:24 +01002526 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002527
2528 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002529
Chris Wilsonb17993b2016-11-14 11:29:30 +00002530err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002531 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002532err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002533 for_each_sgt_page(page, sgt_iter, st)
2534 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002535 sg_free_table(st);
2536 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002537
2538 /* shmemfs first checks if there is enough memory to allocate the page
2539 * and reports ENOSPC should there be insufficient, along with the usual
2540 * ENOMEM for a genuine allocation failure.
2541 *
2542 * We use ENOSPC in our driver to mean that we have run out of aperture
2543 * space and so want to translate the error from shmemfs back to our
2544 * usual understanding of ENOMEM.
2545 */
Imre Deake2273302015-07-09 12:59:05 +03002546 if (ret == -ENOSPC)
2547 ret = -ENOMEM;
2548
Matthew Auldb91b09e2017-10-06 23:18:17 +01002549 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002550}
2551
2552void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002553 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002554 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002555{
Matthew Aulda5c081662017-10-06 23:18:18 +01002556 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2557 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2558 int i;
2559
Chris Wilson1233e2d2016-10-28 13:58:37 +01002560 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002561
2562 obj->mm.get_page.sg_pos = pages->sgl;
2563 obj->mm.get_page.sg_idx = 0;
2564
2565 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002566
2567 if (i915_gem_object_is_tiled(obj) &&
Chris Wilsonf2123812017-10-16 12:40:37 +01002568 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002569 GEM_BUG_ON(obj->mm.quirked);
2570 __i915_gem_object_pin_pages(obj);
2571 obj->mm.quirked = true;
2572 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002573
Matthew Auld84e89782017-10-09 12:00:24 +01002574 GEM_BUG_ON(!sg_page_sizes);
2575 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002576
2577 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002578 * Calculate the supported page-sizes which fit into the given
2579 * sg_page_sizes. This will give us the page-sizes which we may be able
2580 * to use opportunistically when later inserting into the GTT. For
2581 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2582 * 64K or 4K pages, although in practice this will depend on a number of
2583 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002584 */
2585 obj->mm.page_sizes.sg = 0;
2586 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2587 if (obj->mm.page_sizes.phys & ~0u << i)
2588 obj->mm.page_sizes.sg |= BIT(i);
2589 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002590 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilsonf2123812017-10-16 12:40:37 +01002591
2592 spin_lock(&i915->mm.obj_lock);
2593 list_add(&obj->mm.link, &i915->mm.unbound_list);
2594 spin_unlock(&i915->mm.obj_lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002595}
2596
2597static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2598{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002599 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002600
2601 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2602 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2603 return -EFAULT;
2604 }
2605
Matthew Auldb91b09e2017-10-06 23:18:17 +01002606 err = obj->ops->get_pages(obj);
Matthew Auldb65a9b92017-12-18 10:38:55 +00002607 GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002608
Matthew Auldb91b09e2017-10-06 23:18:17 +01002609 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002610}
2611
Chris Wilson37e680a2012-06-07 15:38:42 +01002612/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002613 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002614 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002615 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002616 * either as a result of memory pressure (reaping pages under the shrinker)
2617 * or as the object is itself released.
2618 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002619int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002620{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002621 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002622
Chris Wilson1233e2d2016-10-28 13:58:37 +01002623 err = mutex_lock_interruptible(&obj->mm.lock);
2624 if (err)
2625 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002626
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002627 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002628 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2629
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002630 err = ____i915_gem_object_get_pages(obj);
2631 if (err)
2632 goto unlock;
2633
2634 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002635 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002636 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002637
Chris Wilson1233e2d2016-10-28 13:58:37 +01002638unlock:
2639 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002640 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002641}
2642
Dave Gordondd6034c2016-05-20 11:54:04 +01002643/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002644static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2645 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002646{
2647 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002648 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002649 struct sgt_iter sgt_iter;
2650 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002651 struct page *stack_pages[32];
2652 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002653 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002654 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002655 void *addr;
2656
2657 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002658 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002659 return kmap(sg_page(sgt->sgl));
2660
Dave Gordonb338fa42016-05-20 11:54:05 +01002661 if (n_pages > ARRAY_SIZE(stack_pages)) {
2662 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002663 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002664 if (!pages)
2665 return NULL;
2666 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002667
Dave Gordon85d12252016-05-20 11:54:06 +01002668 for_each_sgt_page(page, sgt_iter, sgt)
2669 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002670
2671 /* Check that we have the expected number of pages */
2672 GEM_BUG_ON(i != n_pages);
2673
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002674 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002675 default:
2676 MISSING_CASE(type);
2677 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002678 case I915_MAP_WB:
2679 pgprot = PAGE_KERNEL;
2680 break;
2681 case I915_MAP_WC:
2682 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2683 break;
2684 }
2685 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002686
Dave Gordonb338fa42016-05-20 11:54:05 +01002687 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002688 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002689
2690 return addr;
2691}
2692
2693/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002694void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2695 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002696{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002697 enum i915_map_type has_type;
2698 bool pinned;
2699 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002700 int ret;
2701
Tina Zhanga03f3952017-11-14 10:25:13 +00002702 if (unlikely(!i915_gem_object_has_struct_page(obj)))
2703 return ERR_PTR(-ENXIO);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002704
Chris Wilson1233e2d2016-10-28 13:58:37 +01002705 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002706 if (ret)
2707 return ERR_PTR(ret);
2708
Chris Wilsona575c672017-08-28 11:46:31 +01002709 pinned = !(type & I915_MAP_OVERRIDE);
2710 type &= ~I915_MAP_OVERRIDE;
2711
Chris Wilson1233e2d2016-10-28 13:58:37 +01002712 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002713 if (unlikely(!i915_gem_object_has_pages(obj))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002714 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2715
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002716 ret = ____i915_gem_object_get_pages(obj);
2717 if (ret)
2718 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002719
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002720 smp_mb__before_atomic();
2721 }
2722 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002723 pinned = false;
2724 }
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002725 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002726
Chris Wilson0ce81782017-05-17 13:09:59 +01002727 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002728 if (ptr && has_type != type) {
2729 if (pinned) {
2730 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002731 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002732 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002733
2734 if (is_vmalloc_addr(ptr))
2735 vunmap(ptr);
2736 else
2737 kunmap(kmap_to_page(ptr));
2738
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002739 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002740 }
2741
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002742 if (!ptr) {
2743 ptr = i915_gem_object_map(obj, type);
2744 if (!ptr) {
2745 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002746 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002747 }
2748
Chris Wilson0ce81782017-05-17 13:09:59 +01002749 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002750 }
2751
Chris Wilson1233e2d2016-10-28 13:58:37 +01002752out_unlock:
2753 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002754 return ptr;
2755
Chris Wilson1233e2d2016-10-28 13:58:37 +01002756err_unpin:
2757 atomic_dec(&obj->mm.pages_pin_count);
2758err_unlock:
2759 ptr = ERR_PTR(ret);
2760 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002761}
2762
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002763static int
2764i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2765 const struct drm_i915_gem_pwrite *arg)
2766{
2767 struct address_space *mapping = obj->base.filp->f_mapping;
2768 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2769 u64 remain, offset;
2770 unsigned int pg;
2771
2772 /* Before we instantiate/pin the backing store for our use, we
2773 * can prepopulate the shmemfs filp efficiently using a write into
2774 * the pagecache. We avoid the penalty of instantiating all the
2775 * pages, important if the user is just writing to a few and never
2776 * uses the object on the GPU, and using a direct write into shmemfs
2777 * allows it to avoid the cost of retrieving a page (either swapin
2778 * or clearing-before-use) before it is overwritten.
2779 */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01002780 if (i915_gem_object_has_pages(obj))
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002781 return -ENODEV;
2782
Chris Wilsona6d65e42017-10-16 21:27:32 +01002783 if (obj->mm.madv != I915_MADV_WILLNEED)
2784 return -EFAULT;
2785
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002786 /* Before the pages are instantiated the object is treated as being
2787 * in the CPU domain. The pages will be clflushed as required before
2788 * use, and we can freely write into the pages directly. If userspace
2789 * races pwrite with any other operation; corruption will ensue -
2790 * that is userspace's prerogative!
2791 */
2792
2793 remain = arg->size;
2794 offset = arg->offset;
2795 pg = offset_in_page(offset);
2796
2797 do {
2798 unsigned int len, unwritten;
2799 struct page *page;
2800 void *data, *vaddr;
2801 int err;
2802
2803 len = PAGE_SIZE - pg;
2804 if (len > remain)
2805 len = remain;
2806
2807 err = pagecache_write_begin(obj->base.filp, mapping,
2808 offset, len, 0,
2809 &page, &data);
2810 if (err < 0)
2811 return err;
2812
2813 vaddr = kmap(page);
2814 unwritten = copy_from_user(vaddr + pg, user_data, len);
2815 kunmap(page);
2816
2817 err = pagecache_write_end(obj->base.filp, mapping,
2818 offset, len, len - unwritten,
2819 page, data);
2820 if (err < 0)
2821 return err;
2822
2823 if (unwritten)
2824 return -EFAULT;
2825
2826 remain -= len;
2827 user_data += len;
2828 offset += len;
2829 pg = 0;
2830 } while (remain);
2831
2832 return 0;
2833}
2834
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002835static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002836{
Chris Wilson77b25a92017-07-21 13:32:30 +01002837 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002838
Chris Wilson77b25a92017-07-21 13:32:30 +01002839 atomic_inc(&ctx->guilty_count);
2840
Chris Wilson24eae082018-02-05 09:22:01 +00002841 banned = false;
2842 if (i915_gem_context_is_bannable(ctx)) {
2843 unsigned int score;
2844
2845 score = atomic_add_return(CONTEXT_SCORE_GUILTY,
2846 &ctx->ban_score);
2847 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
2848
2849 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2850 ctx->name, score, yesno(banned));
2851 }
Chris Wilson77b25a92017-07-21 13:32:30 +01002852 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002853 return;
2854
Chris Wilson77b25a92017-07-21 13:32:30 +01002855 i915_gem_context_set_banned(ctx);
2856 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2857 atomic_inc(&ctx->file_priv->context_bans);
2858 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2859 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2860 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002861}
2862
2863static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2864{
Chris Wilson77b25a92017-07-21 13:32:30 +01002865 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002866}
2867
Chris Wilsone61e0f52018-02-21 09:56:36 +00002868struct i915_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002869i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002870{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002871 struct i915_request *request, *active = NULL;
Chris Wilson754c9fd2017-02-23 07:44:14 +00002872 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002873
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002874 /* We are called by the error capture and reset at a random
2875 * point in time. In particular, note that neither is crucially
2876 * ordered with an interrupt. After a hang, the GPU is dead and we
2877 * assume that no more writes can happen (we waited long enough for
2878 * all writes that were in transaction to be flushed) - adding an
2879 * extra delay for a recent interrupt is pointless. Hence, we do
2880 * not need an engine->irq_seqno_barrier() before the seqno reads.
2881 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002882 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002883 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00002884 if (__i915_request_completed(request, request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002885 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002886
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002887 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002888 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2889 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002890
Chris Wilson754c9fd2017-02-23 07:44:14 +00002891 active = request;
2892 break;
2893 }
2894 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2895
2896 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002897}
2898
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002899static bool engine_stalled(struct intel_engine_cs *engine)
2900{
2901 if (!engine->hangcheck.stalled)
2902 return false;
2903
2904 /* Check for possible seqno movement after hang declaration */
2905 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2906 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2907 return false;
2908 }
2909
2910 return true;
2911}
2912
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002913/*
2914 * Ensure irq handler finishes, and not run again.
2915 * Also return the active request so that we only search for it once.
2916 */
Chris Wilsone61e0f52018-02-21 09:56:36 +00002917struct i915_request *
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002918i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2919{
Chris Wilsone61e0f52018-02-21 09:56:36 +00002920 struct i915_request *request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002921
Chris Wilson1749d902017-10-09 12:02:59 +01002922 /*
2923 * During the reset sequence, we must prevent the engine from
2924 * entering RC6. As the context state is undefined until we restart
2925 * the engine, if it does enter RC6 during the reset, the state
2926 * written to the powercontext is undefined and so we may lose
2927 * GPU state upon resume, i.e. fail to restart after a reset.
2928 */
2929 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2930
2931 /*
2932 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002933 * state (by calling dma_fence_signal) as we are processing
2934 * the reset. The write from the GPU of the seqno is
2935 * asynchronous and the signaler thread may see a different
2936 * value to us and declare the request complete, even though
2937 * the reset routine have picked that request as the active
2938 * (incomplete) request. This conflict is not handled
2939 * gracefully!
2940 */
2941 kthread_park(engine->breadcrumbs.signaler);
2942
Chris Wilson1749d902017-10-09 12:02:59 +01002943 /*
2944 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002945 * completed the reset in i915_gem_reset_finish(). If a request
2946 * is completed by one engine, it may then queue a request
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302947 * to a second via its execlists->tasklet *just* as we are
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002948 * calling engine->init_hw() and also writing the ELSP.
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302949 * Turning off the execlists->tasklet until the reset is over
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002950 * prevents the race.
Chris Wilson68ad3612018-03-07 13:42:26 +00002951 *
2952 * Note that this needs to be a single atomic operation on the
2953 * tasklet (flush existing tasks, prevent new tasks) to prevent
2954 * a race between reset and set-wedged. It is not, so we do the best
2955 * we can atm and make sure we don't lock the machine up in the more
2956 * common case of recursively being called from set-wedged from inside
2957 * i915_reset.
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002958 */
Chris Wilson68ad3612018-03-07 13:42:26 +00002959 if (!atomic_read(&engine->execlists.tasklet.count))
2960 tasklet_kill(&engine->execlists.tasklet);
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05302961 tasklet_disable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002962
Michał Winiarskic41937f2017-10-26 15:35:58 +02002963 /*
2964 * We're using worker to queue preemption requests from the tasklet in
2965 * GuC submission mode.
2966 * Even though tasklet was disabled, we may still have a worker queued.
2967 * Let's make sure that all workers scheduled before disabling the
2968 * tasklet are completed before continuing with the reset.
2969 */
2970 if (engine->i915->guc.preempt_wq)
2971 flush_workqueue(engine->i915->guc.preempt_wq);
2972
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002973 if (engine->irq_seqno_barrier)
2974 engine->irq_seqno_barrier(engine);
2975
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002976 request = i915_gem_find_active_request(engine);
2977 if (request && request->fence.error == -EIO)
2978 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002979
2980 return request;
2981}
2982
Chris Wilson0e178ae2017-01-17 17:59:06 +02002983int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002984{
2985 struct intel_engine_cs *engine;
Chris Wilsone61e0f52018-02-21 09:56:36 +00002986 struct i915_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002987 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002988 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002989
Chris Wilson0e178ae2017-01-17 17:59:06 +02002990 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002991 request = i915_gem_reset_prepare_engine(engine);
2992 if (IS_ERR(request)) {
2993 err = PTR_ERR(request);
2994 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002995 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002996
2997 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002998 }
2999
Chris Wilson4c965542017-01-17 17:59:01 +02003000 i915_gem_revoke_fences(dev_priv);
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00003001 intel_uc_sanitize(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02003002
3003 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02003004}
3005
Chris Wilsone61e0f52018-02-21 09:56:36 +00003006static void skip_request(struct i915_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003007{
Chris Wilson821ed7d2016-09-09 14:11:53 +01003008 void *vaddr = request->ring->vaddr;
3009 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02003010
Chris Wilson821ed7d2016-09-09 14:11:53 +01003011 /* As this request likely depends on state from the lost
3012 * context, clear out all the user operations leaving the
3013 * breadcrumb at the end (so we get the fence notifications).
3014 */
3015 head = request->head;
3016 if (request->postfix < head) {
3017 memset(vaddr + head, 0, request->ring->size - head);
3018 head = 0;
3019 }
3020 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00003021
3022 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00003023}
3024
Chris Wilsone61e0f52018-02-21 09:56:36 +00003025static void engine_skip_context(struct i915_request *request)
Mika Kuoppala36193ac2017-01-17 17:59:02 +02003026{
3027 struct intel_engine_cs *engine = request->engine;
3028 struct i915_gem_context *hung_ctx = request->ctx;
3029 struct intel_timeline *timeline;
3030 unsigned long flags;
3031
3032 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
3033
3034 spin_lock_irqsave(&engine->timeline->lock, flags);
3035 spin_lock(&timeline->lock);
3036
3037 list_for_each_entry_continue(request, &engine->timeline->requests, link)
3038 if (request->ctx == hung_ctx)
3039 skip_request(request);
3040
3041 list_for_each_entry(request, &timeline->requests, link)
3042 skip_request(request);
3043
3044 spin_unlock(&timeline->lock);
3045 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3046}
3047
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003048/* Returns the request if it was guilty of the hang */
Chris Wilsone61e0f52018-02-21 09:56:36 +00003049static struct i915_request *
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003050i915_gem_reset_request(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +00003051 struct i915_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02003052{
Mika Kuoppala71895a02017-01-17 17:59:07 +02003053 /* The guilty request will get skipped on a hung engine.
3054 *
3055 * Users of client default contexts do not rely on logical
3056 * state preserved between batches so it is safe to execute
3057 * queued requests following the hang. Non default contexts
3058 * rely on preserved state, so skipping a batch loses the
3059 * evolution of the state and it needs to be considered corrupted.
3060 * Executing more queued batches on top of corrupted state is
3061 * risky. But we take the risk by trying to advance through
3062 * the queued requests in order to make the client behaviour
3063 * more predictable around resets, by not throwing away random
3064 * amount of batches it has prepared for execution. Sophisticated
3065 * clients can use gem_reset_stats_ioctl and dma fence status
3066 * (exported via sync_file info ioctl on explicit fences) to observe
3067 * when it loses the context state and should rebuild accordingly.
3068 *
3069 * The context ban, and ultimately the client ban, mechanism are safety
3070 * valves if client submission ends up resulting in nothing more than
3071 * subsequent hangs.
3072 */
3073
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003074 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02003075 i915_gem_context_mark_guilty(request->ctx);
3076 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003077
3078 /* If this context is now banned, skip all pending requests. */
3079 if (i915_gem_context_is_banned(request->ctx))
3080 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02003081 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003082 /*
3083 * Since this is not the hung engine, it may have advanced
3084 * since the hang declaration. Double check by refinding
3085 * the active request at the time of the reset.
3086 */
3087 request = i915_gem_find_active_request(engine);
3088 if (request) {
3089 i915_gem_context_mark_innocent(request->ctx);
3090 dma_fence_set_error(&request->fence, -EAGAIN);
3091
3092 /* Rewind the engine to replay the incomplete rq */
3093 spin_lock_irq(&engine->timeline->lock);
3094 request = list_prev_entry(request, link);
3095 if (&request->link == &engine->timeline->requests)
3096 request = NULL;
3097 spin_unlock_irq(&engine->timeline->lock);
3098 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003099 }
3100
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003101 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003102}
3103
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003104void i915_gem_reset_engine(struct intel_engine_cs *engine,
Chris Wilsone61e0f52018-02-21 09:56:36 +00003105 struct i915_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003106{
Chris Wilsonfcb1de52017-12-19 09:01:10 +00003107 /*
3108 * Make sure this write is visible before we re-enable the interrupt
3109 * handlers on another CPU, as tasklet_enable() resolves to just
3110 * a compiler barrier which is insufficient for our purpose here.
3111 */
3112 smp_store_mb(engine->irq_posted, 0);
Chris Wilsoned454f22017-07-21 13:32:29 +01003113
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003114 if (request)
3115 request = i915_gem_reset_request(engine, request);
3116
3117 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003118 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3119 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003120 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003121
3122 /* Setup the CS to resume from the breadcrumb of the hung request */
3123 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003124}
3125
Chris Wilsond8027092017-02-08 14:30:32 +00003126void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003127{
3128 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303129 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003130
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003131 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3132
Chris Wilsone61e0f52018-02-21 09:56:36 +00003133 i915_retire_requests(dev_priv);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003134
Chris Wilson2ae55732017-02-12 17:20:02 +00003135 for_each_engine(engine, dev_priv, id) {
3136 struct i915_gem_context *ctx;
3137
Michel Thierryc64992e2017-06-20 10:57:44 +01003138 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003139 ctx = fetch_and_zero(&engine->last_retired_context);
3140 if (ctx)
3141 engine->context_unpin(engine, ctx);
Chris Wilson7b6da812017-12-16 00:03:34 +00003142
3143 /*
3144 * Ostensibily, we always want a context loaded for powersaving,
3145 * so if the engine is idle after the reset, send a request
3146 * to load our scratch kernel_context.
3147 *
3148 * More mysteriously, if we leave the engine idle after a reset,
3149 * the next userspace batch may hang, with what appears to be
3150 * an incoherent read by the CS (presumably stale TLB). An
3151 * empty request appears sufficient to paper over the glitch.
3152 */
Chris Wilson01b8fdc2018-02-05 15:24:31 +00003153 if (intel_engine_is_idle(engine)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003154 struct i915_request *rq;
Chris Wilson7b6da812017-12-16 00:03:34 +00003155
Chris Wilsone61e0f52018-02-21 09:56:36 +00003156 rq = i915_request_alloc(engine,
3157 dev_priv->kernel_context);
Chris Wilson7b6da812017-12-16 00:03:34 +00003158 if (!IS_ERR(rq))
Chris Wilsone61e0f52018-02-21 09:56:36 +00003159 __i915_request_add(rq, false);
Chris Wilson7b6da812017-12-16 00:03:34 +00003160 }
Chris Wilson2ae55732017-02-12 17:20:02 +00003161 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003162
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003163 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003164
3165 if (dev_priv->gt.awake) {
3166 intel_sanitize_gt_powersave(dev_priv);
3167 intel_enable_gt_powersave(dev_priv);
3168 if (INTEL_GEN(dev_priv) >= 6)
3169 gen6_rps_busy(dev_priv);
3170 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003171}
3172
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003173void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3174{
Sagar Arun Kamblec6dce8f2017-11-16 19:02:37 +05303175 tasklet_enable(&engine->execlists.tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003176 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003177
3178 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003179}
3180
Chris Wilsond8027092017-02-08 14:30:32 +00003181void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3182{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003183 struct intel_engine_cs *engine;
3184 enum intel_engine_id id;
3185
Chris Wilsond8027092017-02-08 14:30:32 +00003186 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003187
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003188 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003189 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003190 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003191 }
Chris Wilsond8027092017-02-08 14:30:32 +00003192}
3193
Chris Wilsone61e0f52018-02-21 09:56:36 +00003194static void nop_submit_request(struct i915_request *request)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003195{
Chris Wilsond9b13c42018-03-15 13:14:50 +00003196 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3197 request->engine->name,
3198 request->fence.context, request->fence.seqno);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003199 dma_fence_set_error(&request->fence, -EIO);
3200
Chris Wilsone61e0f52018-02-21 09:56:36 +00003201 i915_request_submit(request);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003202}
3203
Chris Wilsone61e0f52018-02-21 09:56:36 +00003204static void nop_complete_submit_request(struct i915_request *request)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003205{
Chris Wilson8d550822017-10-06 12:56:17 +01003206 unsigned long flags;
3207
Chris Wilsond9b13c42018-03-15 13:14:50 +00003208 GEM_TRACE("%s fence %llx:%d -> -EIO\n",
3209 request->engine->name,
3210 request->fence.context, request->fence.seqno);
Chris Wilson3cd94422017-01-10 17:22:45 +00003211 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003212
3213 spin_lock_irqsave(&request->engine->timeline->lock, flags);
Chris Wilsone61e0f52018-02-21 09:56:36 +00003214 __i915_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003215 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003216 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003217}
3218
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003219void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003220{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003221 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303222 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003223
Chris Wilsond9b13c42018-03-15 13:14:50 +00003224 GEM_TRACE("start\n");
3225
Chris Wilson559e0402018-02-05 09:21:59 +00003226 if (drm_debug & DRM_UT_DRIVER) {
3227 struct drm_printer p = drm_debug_printer(__func__);
3228
3229 for_each_engine(engine, i915, id)
3230 intel_engine_dump(engine, &p, "%s\n", engine->name);
3231 }
3232
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003233 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3234 smp_mb__after_atomic();
3235
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003236 /*
3237 * First, stop submission to hw, but do not yet complete requests by
3238 * rolling the global seqno forward (since this would complete requests
3239 * for which we haven't set the fence error to EIO yet).
3240 */
Chris Wilson963ddd62018-03-02 11:33:24 +00003241 for_each_engine(engine, i915, id) {
3242 i915_gem_reset_prepare_engine(engine);
Chris Wilson47650db2018-03-07 13:42:25 +00003243
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003244 engine->submit_request = nop_submit_request;
Chris Wilson47650db2018-03-07 13:42:25 +00003245 engine->schedule = NULL;
Chris Wilson963ddd62018-03-02 11:33:24 +00003246 }
Chris Wilson47650db2018-03-07 13:42:25 +00003247 i915->caps.scheduler = 0;
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003248
Chris Wilsonac697ae2018-03-15 15:10:15 +00003249 /* Even if the GPU reset fails, it should still stop the engines */
3250 intel_gpu_reset(i915, ALL_ENGINES);
3251
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003252 /*
3253 * Make sure no one is running the old callback before we proceed with
3254 * cancelling requests and resetting the completion tracking. Otherwise
3255 * we might submit a request to the hardware which never completes.
3256 */
3257 synchronize_rcu();
3258
3259 for_each_engine(engine, i915, id) {
3260 /* Mark all executing requests as skipped */
3261 engine->cancel_requests(engine);
3262
3263 /*
3264 * Only once we've force-cancelled all in-flight requests can we
3265 * start to complete all requests.
3266 */
3267 engine->submit_request = nop_complete_submit_request;
3268 }
3269
3270 /*
3271 * Make sure no request can slip through without getting completed by
3272 * either this call here to intel_engine_init_global_seqno, or the one
3273 * in nop_complete_submit_request.
3274 */
3275 synchronize_rcu();
3276
3277 for_each_engine(engine, i915, id) {
3278 unsigned long flags;
3279
Chris Wilson0d73e7a2018-02-07 15:13:50 +00003280 /*
3281 * Mark all pending requests as complete so that any concurrent
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003282 * (lockless) lookup doesn't try and wait upon the request as we
3283 * reset it.
3284 */
3285 spin_lock_irqsave(&engine->timeline->lock, flags);
3286 intel_engine_init_global_seqno(engine,
3287 intel_engine_last_submit(engine));
3288 spin_unlock_irqrestore(&engine->timeline->lock, flags);
Chris Wilson963ddd62018-03-02 11:33:24 +00003289
3290 i915_gem_reset_finish_engine(engine);
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003291 }
Chris Wilson20e49332016-11-22 14:41:21 +00003292
Chris Wilsond9b13c42018-03-15 13:14:50 +00003293 GEM_TRACE("end\n");
3294
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003295 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003296}
3297
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003298bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3299{
3300 struct i915_gem_timeline *tl;
3301 int i;
3302
3303 lockdep_assert_held(&i915->drm.struct_mutex);
3304 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3305 return true;
3306
Chris Wilsond9b13c42018-03-15 13:14:50 +00003307 GEM_TRACE("start\n");
3308
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003309 /*
3310 * Before unwedging, make sure that all pending operations
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003311 * are flushed and errored out - we may have requests waiting upon
3312 * third party fences. We marked all inflight requests as EIO, and
3313 * every execbuf since returned EIO, for consistency we want all
3314 * the currently pending requests to also be marked as EIO, which
3315 * is done inside our nop_submit_request - and so we must wait.
3316 *
3317 * No more can be submitted until we reset the wedged bit.
3318 */
3319 list_for_each_entry(tl, &i915->gt.timelines, link) {
3320 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003321 struct i915_request *rq;
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003322
3323 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3324 &i915->drm.struct_mutex);
3325 if (!rq)
3326 continue;
3327
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003328 /*
3329 * We can't use our normal waiter as we want to
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003330 * avoid recursively trying to handle the current
3331 * reset. The basic dma_fence_default_wait() installs
3332 * a callback for dma_fence_signal(), which is
3333 * triggered by our nop handler (indirectly, the
3334 * callback enables the signaler thread which is
3335 * woken by the nop_submit_request() advancing the seqno
3336 * and when the seqno passes the fence, the signaler
3337 * then signals the fence waking us up).
3338 */
3339 if (dma_fence_default_wait(&rq->fence, true,
3340 MAX_SCHEDULE_TIMEOUT) < 0)
3341 return false;
3342 }
3343 }
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003344 i915_retire_requests(i915);
3345 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003346
Chris Wilson2d4ecac2018-03-07 13:42:21 +00003347 /*
3348 * Undo nop_submit_request. We prevent all new i915 requests from
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003349 * being queued (by disallowing execbuf whilst wedged) so having
3350 * waited for all active requests above, we know the system is idle
3351 * and do not have to worry about a thread being inside
3352 * engine->submit_request() as we swap over. So unlike installing
3353 * the nop_submit_request on reset, we can do this from normal
3354 * context and do not require stop_machine().
3355 */
3356 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003357 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003358
Chris Wilsond9b13c42018-03-15 13:14:50 +00003359 GEM_TRACE("end\n");
3360
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003361 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3362 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3363
3364 return true;
3365}
3366
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003367static void
Eric Anholt673a3942008-07-30 12:06:12 -07003368i915_gem_retire_work_handler(struct work_struct *work)
3369{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003370 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003371 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003372 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003373
Chris Wilson891b48c2010-09-29 12:26:37 +01003374 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003375 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00003376 i915_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003377 mutex_unlock(&dev->struct_mutex);
3378 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003379
Chris Wilson88923042018-01-29 14:41:04 +00003380 /*
3381 * Keep the retire handler running until we are finally idle.
Chris Wilson67d97da2016-07-04 08:08:31 +01003382 * We do not need to do this test under locking as in the worst-case
3383 * we queue the retire worker once too often.
3384 */
Chris Wilson88923042018-01-29 14:41:04 +00003385 if (READ_ONCE(dev_priv->gt.awake))
Chris Wilson67d97da2016-07-04 08:08:31 +01003386 queue_delayed_work(dev_priv->wq,
3387 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003388 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003389}
Chris Wilson891b48c2010-09-29 12:26:37 +01003390
Chris Wilson84a10742018-01-24 11:36:08 +00003391static void shrink_caches(struct drm_i915_private *i915)
3392{
3393 /*
3394 * kmem_cache_shrink() discards empty slabs and reorders partially
3395 * filled slabs to prioritise allocating from the mostly full slabs,
3396 * with the aim of reducing fragmentation.
3397 */
3398 kmem_cache_shrink(i915->priorities);
3399 kmem_cache_shrink(i915->dependencies);
3400 kmem_cache_shrink(i915->requests);
3401 kmem_cache_shrink(i915->luts);
3402 kmem_cache_shrink(i915->vmas);
3403 kmem_cache_shrink(i915->objects);
3404}
3405
3406struct sleep_rcu_work {
3407 union {
3408 struct rcu_head rcu;
3409 struct work_struct work;
3410 };
3411 struct drm_i915_private *i915;
3412 unsigned int epoch;
3413};
3414
3415static inline bool
3416same_epoch(struct drm_i915_private *i915, unsigned int epoch)
3417{
3418 /*
3419 * There is a small chance that the epoch wrapped since we started
3420 * sleeping. If we assume that epoch is at least a u32, then it will
3421 * take at least 2^32 * 100ms for it to wrap, or about 326 years.
3422 */
3423 return epoch == READ_ONCE(i915->gt.epoch);
3424}
3425
3426static void __sleep_work(struct work_struct *work)
3427{
3428 struct sleep_rcu_work *s = container_of(work, typeof(*s), work);
3429 struct drm_i915_private *i915 = s->i915;
3430 unsigned int epoch = s->epoch;
3431
3432 kfree(s);
3433 if (same_epoch(i915, epoch))
3434 shrink_caches(i915);
3435}
3436
3437static void __sleep_rcu(struct rcu_head *rcu)
3438{
3439 struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu);
3440 struct drm_i915_private *i915 = s->i915;
3441
3442 if (same_epoch(i915, s->epoch)) {
3443 INIT_WORK(&s->work, __sleep_work);
3444 queue_work(i915->wq, &s->work);
3445 } else {
3446 kfree(s);
3447 }
3448}
3449
Chris Wilson5427f202017-10-23 22:32:34 +01003450static inline bool
3451new_requests_since_last_retire(const struct drm_i915_private *i915)
3452{
3453 return (READ_ONCE(i915->gt.active_requests) ||
3454 work_pending(&i915->gt.idle_work.work));
3455}
3456
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003457static void
3458i915_gem_idle_work_handler(struct work_struct *work)
3459{
3460 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003461 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson84a10742018-01-24 11:36:08 +00003462 unsigned int epoch = I915_EPOCH_INVALID;
Chris Wilson67d97da2016-07-04 08:08:31 +01003463 bool rearm_hangcheck;
3464
3465 if (!READ_ONCE(dev_priv->gt.awake))
3466 return;
3467
Imre Deak0cb56702016-11-07 11:20:04 +02003468 /*
3469 * Wait for last execlists context complete, but bail out in case a
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003470 * new request is submitted. As we don't trust the hardware, we
3471 * continue on if the wait times out. This is necessary to allow
3472 * the machine to suspend even if the hardware dies, and we will
3473 * try to recover in resume (after depriving the hardware of power,
3474 * it may be in a better mmod).
Imre Deak0cb56702016-11-07 11:20:04 +02003475 */
Chris Wilsonffed7bd2018-03-01 10:33:38 +00003476 __wait_for(if (new_requests_since_last_retire(dev_priv)) return,
3477 intel_engines_are_idle(dev_priv),
3478 I915_IDLE_ENGINES_TIMEOUT * 1000,
3479 10, 500);
Chris Wilson67d97da2016-07-04 08:08:31 +01003480
3481 rearm_hangcheck =
3482 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3483
Chris Wilson5427f202017-10-23 22:32:34 +01003484 if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003485 /* Currently busy, come back later */
3486 mod_delayed_work(dev_priv->wq,
3487 &dev_priv->gt.idle_work,
3488 msecs_to_jiffies(50));
3489 goto out_rearm;
3490 }
3491
Imre Deak93c97dc2016-11-07 11:20:03 +02003492 /*
3493 * New request retired after this work handler started, extend active
3494 * period until next instance of the work.
3495 */
Chris Wilson5427f202017-10-23 22:32:34 +01003496 if (new_requests_since_last_retire(dev_priv))
Imre Deak93c97dc2016-11-07 11:20:03 +02003497 goto out_unlock;
3498
Chris Wilson5427f202017-10-23 22:32:34 +01003499 /*
Chris Wilsonff320d62017-10-23 22:32:35 +01003500 * Be paranoid and flush a concurrent interrupt to make sure
3501 * we don't reactivate any irq tasklets after parking.
3502 *
3503 * FIXME: Note that even though we have waited for execlists to be idle,
3504 * there may still be an in-flight interrupt even though the CSB
3505 * is now empty. synchronize_irq() makes sure that a residual interrupt
3506 * is completed before we continue, but it doesn't prevent the HW from
3507 * raising a spurious interrupt later. To complete the shield we should
3508 * coordinate disabling the CS irq with flushing the interrupts.
3509 */
3510 synchronize_irq(dev_priv->drm.irq);
3511
Chris Wilsonaba5e272017-10-25 15:39:41 +01003512 intel_engines_park(dev_priv);
Chris Wilsond02a1d82017-11-27 12:30:54 +00003513 i915_gem_timelines_park(dev_priv);
3514
Tvrtko Ursulinfeff0dc2017-11-21 18:18:46 +00003515 i915_pmu_gt_parked(dev_priv);
Zou Nan haid1b851f2010-05-21 09:08:57 +08003516
Chris Wilson67d97da2016-07-04 08:08:31 +01003517 GEM_BUG_ON(!dev_priv->gt.awake);
3518 dev_priv->gt.awake = false;
Chris Wilson84a10742018-01-24 11:36:08 +00003519 epoch = dev_priv->gt.epoch;
3520 GEM_BUG_ON(epoch == I915_EPOCH_INVALID);
Chris Wilson67d97da2016-07-04 08:08:31 +01003521 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003522
Chris Wilson67d97da2016-07-04 08:08:31 +01003523 if (INTEL_GEN(dev_priv) >= 6)
3524 gen6_rps_idle(dev_priv);
Tvrtko Ursulinb6876372017-12-05 13:28:54 +00003525
3526 intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ);
3527
Chris Wilson67d97da2016-07-04 08:08:31 +01003528 intel_runtime_pm_put(dev_priv);
3529out_unlock:
Chris Wilson5427f202017-10-23 22:32:34 +01003530 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003531
Chris Wilson67d97da2016-07-04 08:08:31 +01003532out_rearm:
3533 if (rearm_hangcheck) {
3534 GEM_BUG_ON(!dev_priv->gt.awake);
3535 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003536 }
Chris Wilson84a10742018-01-24 11:36:08 +00003537
3538 /*
3539 * When we are idle, it is an opportune time to reap our caches.
3540 * However, we have many objects that utilise RCU and the ordered
3541 * i915->wq that this work is executing on. To try and flush any
3542 * pending frees now we are idle, we first wait for an RCU grace
3543 * period, and then queue a task (that will run last on the wq) to
3544 * shrink and re-optimize the caches.
3545 */
3546 if (same_epoch(dev_priv, epoch)) {
3547 struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL);
3548 if (s) {
3549 s->i915 = dev_priv;
3550 s->epoch = epoch;
3551 call_rcu(&s->rcu, __sleep_rcu);
3552 }
3553 }
Eric Anholt673a3942008-07-30 12:06:12 -07003554}
3555
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003556void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3557{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003558 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003559 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3560 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003561 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003562
Chris Wilsond1b48c12017-08-16 09:52:08 +01003563 mutex_lock(&i915->drm.struct_mutex);
3564
3565 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3566 struct i915_gem_context *ctx = lut->ctx;
3567 struct i915_vma *vma;
3568
Chris Wilson432295d2017-08-22 12:05:15 +01003569 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003570 if (ctx->file_priv != fpriv)
3571 continue;
3572
3573 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003574 GEM_BUG_ON(vma->obj != obj);
3575
3576 /* We allow the process to have multiple handles to the same
3577 * vma, in the same fd namespace, by virtue of flink/open.
3578 */
3579 GEM_BUG_ON(!vma->open_count);
3580 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003581 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003582
Chris Wilsond1b48c12017-08-16 09:52:08 +01003583 list_del(&lut->obj_link);
3584 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003585
Chris Wilsond1b48c12017-08-16 09:52:08 +01003586 kmem_cache_free(i915->luts, lut);
3587 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003588 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003589
3590 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003591}
3592
Chris Wilsone95433c2016-10-28 13:58:27 +01003593static unsigned long to_wait_timeout(s64 timeout_ns)
3594{
3595 if (timeout_ns < 0)
3596 return MAX_SCHEDULE_TIMEOUT;
3597
3598 if (timeout_ns == 0)
3599 return 0;
3600
3601 return nsecs_to_jiffies_timeout(timeout_ns);
3602}
3603
Ben Widawsky5816d642012-04-11 11:18:19 -07003604/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003605 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003606 * @dev: drm device pointer
3607 * @data: ioctl data blob
3608 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003609 *
3610 * Returns 0 if successful, else an error is returned with the remaining time in
3611 * the timeout parameter.
3612 * -ETIME: object is still busy after timeout
3613 * -ERESTARTSYS: signal interrupted the wait
3614 * -ENONENT: object doesn't exist
3615 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003616 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003617 * -ENOMEM: damn
3618 * -ENODEV: Internal IRQ fail
3619 * -E?: The add request failed
3620 *
3621 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3622 * non-zero timeout parameter the wait ioctl will wait for the given number of
3623 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3624 * without holding struct_mutex the object may become re-busied before this
3625 * function completes. A similar but shorter * race condition exists in the busy
3626 * ioctl
3627 */
3628int
3629i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3630{
3631 struct drm_i915_gem_wait *args = data;
3632 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003633 ktime_t start;
3634 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003635
Daniel Vetter11b5d512014-09-29 15:31:26 +02003636 if (args->flags != 0)
3637 return -EINVAL;
3638
Chris Wilson03ac0642016-07-20 13:31:51 +01003639 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003640 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003641 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003642
Chris Wilsone95433c2016-10-28 13:58:27 +01003643 start = ktime_get();
3644
3645 ret = i915_gem_object_wait(obj,
3646 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3647 to_wait_timeout(args->timeout_ns),
3648 to_rps_client(file));
3649
3650 if (args->timeout_ns > 0) {
3651 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3652 if (args->timeout_ns < 0)
3653 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003654
3655 /*
3656 * Apparently ktime isn't accurate enough and occasionally has a
3657 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3658 * things up to make the test happy. We allow up to 1 jiffy.
3659 *
3660 * This is a regression from the timespec->ktime conversion.
3661 */
3662 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3663 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003664
3665 /* Asked to wait beyond the jiffie/scheduler precision? */
3666 if (ret == -ETIME && args->timeout_ns)
3667 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003668 }
3669
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003670 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003671 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003672}
3673
Chris Wilson73cb9702016-10-28 13:58:46 +01003674static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003675{
Chris Wilson73cb9702016-10-28 13:58:46 +01003676 int ret, i;
3677
3678 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3679 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3680 if (ret)
3681 return ret;
3682 }
3683
3684 return 0;
3685}
3686
Chris Wilson25112b62017-03-30 15:50:39 +01003687static int wait_for_engines(struct drm_i915_private *i915)
3688{
Chris Wilsonee42c002017-12-11 19:41:34 +00003689 if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
Chris Wilson59e4b192017-12-11 19:41:35 +00003690 dev_err(i915->drm.dev,
3691 "Failed to idle engines, declaring wedged!\n");
Chris Wilson629820f2018-03-09 10:11:14 +00003692 GEM_TRACE_DUMP();
Chris Wilsoncad99462017-08-26 12:09:33 +01003693 i915_gem_set_wedged(i915);
3694 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003695 }
3696
3697 return 0;
3698}
3699
Chris Wilson73cb9702016-10-28 13:58:46 +01003700int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3701{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003702 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003703
Chris Wilson863e9fd2017-05-30 13:13:32 +01003704 /* If the device is asleep, we have no requests outstanding */
3705 if (!READ_ONCE(i915->gt.awake))
3706 return 0;
3707
Chris Wilson9caa34a2016-11-11 14:58:08 +00003708 if (flags & I915_WAIT_LOCKED) {
3709 struct i915_gem_timeline *tl;
3710
3711 lockdep_assert_held(&i915->drm.struct_mutex);
3712
3713 list_for_each_entry(tl, &i915->gt.timelines, link) {
3714 ret = wait_for_timeline(tl, flags);
3715 if (ret)
3716 return ret;
3717 }
Chris Wilsone61e0f52018-02-21 09:56:36 +00003718 i915_retire_requests(i915);
Chris Wilson25112b62017-03-30 15:50:39 +01003719
3720 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003721 } else {
3722 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003723 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003724
Chris Wilson25112b62017-03-30 15:50:39 +01003725 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003726}
3727
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003728static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3729{
Chris Wilsone27ab732017-06-15 13:38:49 +01003730 /*
3731 * We manually flush the CPU domain so that we can override and
3732 * force the flush for the display, and perform it asyncrhonously.
3733 */
3734 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3735 if (obj->cache_dirty)
3736 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Christian Königc0a51fd2018-02-16 13:43:38 +01003737 obj->write_domain = 0;
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003738}
3739
3740void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3741{
Chris Wilsonbd3d2252017-10-13 21:26:14 +01003742 if (!READ_ONCE(obj->pin_global))
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003743 return;
3744
3745 mutex_lock(&obj->base.dev->struct_mutex);
3746 __i915_gem_object_flush_for_display(obj);
3747 mutex_unlock(&obj->base.dev->struct_mutex);
3748}
3749
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003750/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003751 * Moves a single object to the WC read, and possibly write domain.
3752 * @obj: object to act on
3753 * @write: ask for write access or read only
3754 *
3755 * This function returns when the move is complete, including waiting on
3756 * flushes to occur.
3757 */
3758int
3759i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3760{
3761 int ret;
3762
3763 lockdep_assert_held(&obj->base.dev->struct_mutex);
3764
3765 ret = i915_gem_object_wait(obj,
3766 I915_WAIT_INTERRUPTIBLE |
3767 I915_WAIT_LOCKED |
3768 (write ? I915_WAIT_ALL : 0),
3769 MAX_SCHEDULE_TIMEOUT,
3770 NULL);
3771 if (ret)
3772 return ret;
3773
Christian Königc0a51fd2018-02-16 13:43:38 +01003774 if (obj->write_domain == I915_GEM_DOMAIN_WC)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003775 return 0;
3776
3777 /* Flush and acquire obj->pages so that we are coherent through
3778 * direct access in memory with previous cached writes through
3779 * shmemfs and that our cache domain tracking remains valid.
3780 * For example, if the obj->filp was moved to swap without us
3781 * being notified and releasing the pages, we would mistakenly
3782 * continue to assume that the obj remained out of the CPU cached
3783 * domain.
3784 */
3785 ret = i915_gem_object_pin_pages(obj);
3786 if (ret)
3787 return ret;
3788
3789 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3790
3791 /* Serialise direct access to this object with the barriers for
3792 * coherent writes from the GPU, by effectively invalidating the
3793 * WC domain upon first access.
3794 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003795 if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0)
Chris Wilsone22d8e32017-04-12 12:01:11 +01003796 mb();
3797
3798 /* It should now be out of any other write domains, and we can update
3799 * the domain values for our changes.
3800 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003801 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3802 obj->read_domains |= I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003803 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003804 obj->read_domains = I915_GEM_DOMAIN_WC;
3805 obj->write_domain = I915_GEM_DOMAIN_WC;
Chris Wilsone22d8e32017-04-12 12:01:11 +01003806 obj->mm.dirty = true;
3807 }
3808
3809 i915_gem_object_unpin_pages(obj);
3810 return 0;
3811}
3812
3813/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003814 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003815 * @obj: object to act on
3816 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003817 *
3818 * This function returns when the move is complete, including waiting on
3819 * flushes to occur.
3820 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003821int
Chris Wilson20217462010-11-23 15:26:33 +00003822i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003823{
Eric Anholte47c68e2008-11-14 13:35:19 -08003824 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003825
Chris Wilsone95433c2016-10-28 13:58:27 +01003826 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003827
Chris Wilsone95433c2016-10-28 13:58:27 +01003828 ret = i915_gem_object_wait(obj,
3829 I915_WAIT_INTERRUPTIBLE |
3830 I915_WAIT_LOCKED |
3831 (write ? I915_WAIT_ALL : 0),
3832 MAX_SCHEDULE_TIMEOUT,
3833 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003834 if (ret)
3835 return ret;
3836
Christian Königc0a51fd2018-02-16 13:43:38 +01003837 if (obj->write_domain == I915_GEM_DOMAIN_GTT)
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003838 return 0;
3839
Chris Wilson43566de2015-01-02 16:29:29 +05303840 /* Flush and acquire obj->pages so that we are coherent through
3841 * direct access in memory with previous cached writes through
3842 * shmemfs and that our cache domain tracking remains valid.
3843 * For example, if the obj->filp was moved to swap without us
3844 * being notified and releasing the pages, we would mistakenly
3845 * continue to assume that the obj remained out of the CPU cached
3846 * domain.
3847 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003848 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303849 if (ret)
3850 return ret;
3851
Chris Wilsonef749212017-04-12 12:01:10 +01003852 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003853
Chris Wilsond0a57782012-10-09 19:24:37 +01003854 /* Serialise direct access to this object with the barriers for
3855 * coherent writes from the GPU, by effectively invalidating the
3856 * GTT domain upon first access.
3857 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003858 if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0)
Chris Wilsond0a57782012-10-09 19:24:37 +01003859 mb();
3860
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003861 /* It should now be out of any other write domains, and we can update
3862 * the domain values for our changes.
3863 */
Christian Königc0a51fd2018-02-16 13:43:38 +01003864 GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3865 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003866 if (write) {
Christian Königc0a51fd2018-02-16 13:43:38 +01003867 obj->read_domains = I915_GEM_DOMAIN_GTT;
3868 obj->write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003869 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003870 }
3871
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003872 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003873 return 0;
3874}
3875
Chris Wilsonef55f922015-10-09 14:11:27 +01003876/**
3877 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003878 * @obj: object to act on
3879 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003880 *
3881 * After this function returns, the object will be in the new cache-level
3882 * across all GTT and the contents of the backing storage will be coherent,
3883 * with respect to the new cache-level. In order to keep the backing storage
3884 * coherent for all users, we only allow a single cache level to be set
3885 * globally on the object and prevent it from being changed whilst the
3886 * hardware is reading from the object. That is if the object is currently
3887 * on the scanout it will be set to uncached (or equivalent display
3888 * cache coherency) and all non-MOCS GPU access will also be uncached so
3889 * that all direct access to the scanout remains coherent.
3890 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003891int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3892 enum i915_cache_level cache_level)
3893{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003894 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003895 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003896
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003897 lockdep_assert_held(&obj->base.dev->struct_mutex);
3898
Chris Wilsone4ffd172011-04-04 09:44:39 +01003899 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003900 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003901
Chris Wilsonef55f922015-10-09 14:11:27 +01003902 /* Inspect the list of currently bound VMA and unbind any that would
3903 * be invalid given the new cache-level. This is principally to
3904 * catch the issue of the CS prefetch crossing page boundaries and
3905 * reading an invalid PTE on older architectures.
3906 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003907restart:
3908 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003909 if (!drm_mm_node_allocated(&vma->node))
3910 continue;
3911
Chris Wilson20dfbde2016-08-04 16:32:30 +01003912 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003913 DRM_DEBUG("can not change the cache level of pinned objects\n");
3914 return -EBUSY;
3915 }
3916
Chris Wilson010e3e62017-12-06 12:49:13 +00003917 if (!i915_vma_is_closed(vma) &&
3918 i915_gem_valid_gtt_space(vma, cache_level))
Chris Wilsonaa653a62016-08-04 07:52:27 +01003919 continue;
3920
3921 ret = i915_vma_unbind(vma);
3922 if (ret)
3923 return ret;
3924
3925 /* As unbinding may affect other elements in the
3926 * obj->vma_list (due to side-effects from retiring
3927 * an active vma), play safe and restart the iterator.
3928 */
3929 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003930 }
3931
Chris Wilsonef55f922015-10-09 14:11:27 +01003932 /* We can reuse the existing drm_mm nodes but need to change the
3933 * cache-level on the PTE. We could simply unbind them all and
3934 * rebind with the correct cache-level on next use. However since
3935 * we already have a valid slot, dma mapping, pages etc, we may as
3936 * rewrite the PTE in the belief that doing so tramples upon less
3937 * state and so involves less work.
3938 */
Chris Wilson15717de2016-08-04 07:52:26 +01003939 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003940 /* Before we change the PTE, the GPU must not be accessing it.
3941 * If we wait upon the object, we know that all the bound
3942 * VMA are no longer active.
3943 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003944 ret = i915_gem_object_wait(obj,
3945 I915_WAIT_INTERRUPTIBLE |
3946 I915_WAIT_LOCKED |
3947 I915_WAIT_ALL,
3948 MAX_SCHEDULE_TIMEOUT,
3949 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003950 if (ret)
3951 return ret;
3952
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003953 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3954 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003955 /* Access to snoopable pages through the GTT is
3956 * incoherent and on some machines causes a hard
3957 * lockup. Relinquish the CPU mmaping to force
3958 * userspace to refault in the pages and we can
3959 * then double check if the GTT mapping is still
3960 * valid for that pointer access.
3961 */
3962 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003963
Chris Wilsonef55f922015-10-09 14:11:27 +01003964 /* As we no longer need a fence for GTT access,
3965 * we can relinquish it now (and so prevent having
3966 * to steal a fence from someone else on the next
3967 * fence request). Note GPU activity would have
3968 * dropped the fence as all snoopable access is
3969 * supposed to be linear.
3970 */
Chris Wilsone2189dd2017-12-07 21:14:07 +00003971 for_each_ggtt_vma(vma, obj) {
Chris Wilson49ef5292016-08-18 17:17:00 +01003972 ret = i915_vma_put_fence(vma);
3973 if (ret)
3974 return ret;
3975 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003976 } else {
3977 /* We either have incoherent backing store and
3978 * so no GTT access or the architecture is fully
3979 * coherent. In such cases, existing GTT mmaps
3980 * ignore the cache bit in the PTE and we can
3981 * rewrite it without confusing the GPU or having
3982 * to force userspace to fault back in its mmaps.
3983 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003984 }
3985
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003986 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003987 if (!drm_mm_node_allocated(&vma->node))
3988 continue;
3989
3990 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3991 if (ret)
3992 return ret;
3993 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003994 }
3995
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003996 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003997 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003998 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003999 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01004000
Chris Wilsone4ffd172011-04-04 09:44:39 +01004001 return 0;
4002}
4003
Ben Widawsky199adf42012-09-21 17:01:20 -07004004int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
4005 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004006{
Ben Widawsky199adf42012-09-21 17:01:20 -07004007 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004008 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004009 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004010
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004011 rcu_read_lock();
4012 obj = i915_gem_object_lookup_rcu(file, args->handle);
4013 if (!obj) {
4014 err = -ENOENT;
4015 goto out;
4016 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004017
Chris Wilson651d7942013-08-08 14:41:10 +01004018 switch (obj->cache_level) {
4019 case I915_CACHE_LLC:
4020 case I915_CACHE_L3_LLC:
4021 args->caching = I915_CACHING_CACHED;
4022 break;
4023
Chris Wilson4257d3b2013-08-08 14:41:11 +01004024 case I915_CACHE_WT:
4025 args->caching = I915_CACHING_DISPLAY;
4026 break;
4027
Chris Wilson651d7942013-08-08 14:41:10 +01004028 default:
4029 args->caching = I915_CACHING_NONE;
4030 break;
4031 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004032out:
4033 rcu_read_unlock();
4034 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004035}
4036
Ben Widawsky199adf42012-09-21 17:01:20 -07004037int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4038 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004039{
Chris Wilson9c870d02016-10-24 13:42:15 +01004040 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07004041 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004042 struct drm_i915_gem_object *obj;
4043 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00004044 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004045
Ben Widawsky199adf42012-09-21 17:01:20 -07004046 switch (args->caching) {
4047 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004048 level = I915_CACHE_NONE;
4049 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004050 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004051 /*
4052 * Due to a HW issue on BXT A stepping, GPU stores via a
4053 * snooped mapping may leave stale data in a corresponding CPU
4054 * cacheline, whereas normally such cachelines would get
4055 * invalidated.
4056 */
Chris Wilson9c870d02016-10-24 13:42:15 +01004057 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03004058 return -ENODEV;
4059
Chris Wilsone6994ae2012-07-10 10:27:08 +01004060 level = I915_CACHE_LLC;
4061 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004062 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01004063 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004064 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004065 default:
4066 return -EINVAL;
4067 }
4068
Chris Wilsond65415d2017-01-19 08:22:10 +00004069 obj = i915_gem_object_lookup(file, args->handle);
4070 if (!obj)
4071 return -ENOENT;
4072
Tina Zhanga03f3952017-11-14 10:25:13 +00004073 /*
4074 * The caching mode of proxy object is handled by its generator, and
4075 * not allowed to be changed by userspace.
4076 */
4077 if (i915_gem_object_is_proxy(obj)) {
4078 ret = -ENXIO;
4079 goto out;
4080 }
4081
Chris Wilsond65415d2017-01-19 08:22:10 +00004082 if (obj->cache_level == level)
4083 goto out;
4084
4085 ret = i915_gem_object_wait(obj,
4086 I915_WAIT_INTERRUPTIBLE,
4087 MAX_SCHEDULE_TIMEOUT,
4088 to_rps_client(file));
4089 if (ret)
4090 goto out;
4091
Ben Widawsky3bc29132012-09-26 16:15:20 -07004092 ret = i915_mutex_lock_interruptible(dev);
4093 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00004094 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004095
4096 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004097 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00004098
4099out:
4100 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01004101 return ret;
4102}
4103
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004104/*
Dhinakaran Pandiyan07bcd992018-03-06 19:34:18 -08004105 * Prepare buffer for display plane (scanout, cursors, etc). Can be called from
4106 * an uninterruptible phase (modesetting) and allows any flushes to be pipelined
4107 * (for pageflips). We only flush the caches while preparing the buffer for
4108 * display, the callers are responsible for frontbuffer flush.
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004109 */
Chris Wilson058d88c2016-08-15 10:49:06 +01004110struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004111i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4112 u32 alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004113 const struct i915_ggtt_view *view,
4114 unsigned int flags)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004115{
Chris Wilson058d88c2016-08-15 10:49:06 +01004116 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004117 int ret;
4118
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004119 lockdep_assert_held(&obj->base.dev->struct_mutex);
4120
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004121 /* Mark the global pin early so that we account for the
Chris Wilsoncc98b412013-08-09 12:25:09 +01004122 * display coherency whilst setting up the cache domains.
4123 */
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004124 obj->pin_global++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004125
Eric Anholta7ef0642011-03-29 16:59:54 -07004126 /* The display engine is not coherent with the LLC cache on gen6. As
4127 * a result, we make sure that the pinning that is about to occur is
4128 * done with uncached PTEs. This is lowest common denominator for all
4129 * chipsets.
4130 *
4131 * However for gen6+, we could do better by using the GFDT bit instead
4132 * of uncaching, which would allow us to flush all the LLC-cached data
4133 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4134 */
Chris Wilson651d7942013-08-08 14:41:10 +01004135 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004136 HAS_WT(to_i915(obj->base.dev)) ?
4137 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01004138 if (ret) {
4139 vma = ERR_PTR(ret);
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004140 goto err_unpin_global;
Chris Wilson058d88c2016-08-15 10:49:06 +01004141 }
Eric Anholta7ef0642011-03-29 16:59:54 -07004142
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004143 /* As the user may map the buffer once pinned in the display plane
4144 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01004145 * always use map_and_fenceable for all scanout buffers. However,
4146 * it may simply be too big to fit into mappable, in which case
4147 * put it anyway and hope that userspace can cope (but always first
4148 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004149 */
Chris Wilson2efb8132016-08-18 17:17:06 +01004150 vma = ERR_PTR(-ENOSPC);
Chris Wilson59354852018-02-20 13:42:06 +00004151 if ((flags & PIN_MAPPABLE) == 0 &&
4152 (!view || view->type == I915_GGTT_VIEW_NORMAL))
Chris Wilson2efb8132016-08-18 17:17:06 +01004153 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
Chris Wilson59354852018-02-20 13:42:06 +00004154 flags |
4155 PIN_MAPPABLE |
4156 PIN_NONBLOCK);
4157 if (IS_ERR(vma))
Chris Wilson767a2222016-11-07 11:01:28 +00004158 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
Chris Wilson058d88c2016-08-15 10:49:06 +01004159 if (IS_ERR(vma))
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004160 goto err_unpin_global;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004161
Chris Wilsond8923dc2016-08-18 17:17:07 +01004162 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
4163
Chris Wilson5a97bcc2017-02-22 11:40:46 +00004164 __i915_gem_object_flush_for_display(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004165
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004166 /* It should now be out of any other write domains, and we can update
4167 * the domain values for our changes.
4168 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004169 obj->read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004170
Chris Wilson058d88c2016-08-15 10:49:06 +01004171 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004172
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004173err_unpin_global:
4174 obj->pin_global--;
Chris Wilson058d88c2016-08-15 10:49:06 +01004175 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004176}
4177
4178void
Chris Wilson058d88c2016-08-15 10:49:06 +01004179i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004180{
Chris Wilson49d73912016-11-29 09:50:08 +00004181 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004182
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004183 if (WARN_ON(vma->obj->pin_global == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004184 return;
4185
Chris Wilsonbd3d2252017-10-13 21:26:14 +01004186 if (--vma->obj->pin_global == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00004187 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004188
Chris Wilson383d5822016-08-18 17:17:08 +01004189 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00004190 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01004191
Chris Wilson058d88c2016-08-15 10:49:06 +01004192 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004193}
4194
Eric Anholte47c68e2008-11-14 13:35:19 -08004195/**
4196 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004197 * @obj: object to act on
4198 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004199 *
4200 * This function returns when the move is complete, including waiting on
4201 * flushes to occur.
4202 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004203int
Chris Wilson919926a2010-11-12 13:42:53 +00004204i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004205{
Eric Anholte47c68e2008-11-14 13:35:19 -08004206 int ret;
4207
Chris Wilsone95433c2016-10-28 13:58:27 +01004208 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004209
Chris Wilsone95433c2016-10-28 13:58:27 +01004210 ret = i915_gem_object_wait(obj,
4211 I915_WAIT_INTERRUPTIBLE |
4212 I915_WAIT_LOCKED |
4213 (write ? I915_WAIT_ALL : 0),
4214 MAX_SCHEDULE_TIMEOUT,
4215 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00004216 if (ret)
4217 return ret;
4218
Chris Wilsonef749212017-04-12 12:01:10 +01004219 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004220
Eric Anholte47c68e2008-11-14 13:35:19 -08004221 /* Flush the CPU cache if it's still invalid. */
Christian Königc0a51fd2018-02-16 13:43:38 +01004222 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00004223 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Christian Königc0a51fd2018-02-16 13:43:38 +01004224 obj->read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004225 }
4226
4227 /* It should now be out of any other write domains, and we can update
4228 * the domain values for our changes.
4229 */
Christian Königc0a51fd2018-02-16 13:43:38 +01004230 GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08004231
4232 /* If we're writing through the CPU, then the GPU read domains will
4233 * need to be invalidated at next use.
4234 */
Chris Wilsone27ab732017-06-15 13:38:49 +01004235 if (write)
4236 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004237
4238 return 0;
4239}
4240
Eric Anholt673a3942008-07-30 12:06:12 -07004241/* Throttle our rendering by waiting until the ring has completed our requests
4242 * emitted over 20 msec ago.
4243 *
Eric Anholtb9624422009-06-03 07:27:35 +00004244 * Note that if we were to use the current jiffies each time around the loop,
4245 * we wouldn't escape the function with any frames outstanding if the time to
4246 * render a frame was over 20ms.
4247 *
Eric Anholt673a3942008-07-30 12:06:12 -07004248 * This should get us reasonable parallelism between CPU and GPU but also
4249 * relatively low latency when blocking on a particular request to finish.
4250 */
4251static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004252i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004253{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004254 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004255 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004256 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
Chris Wilsone61e0f52018-02-21 09:56:36 +00004257 struct i915_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004258 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004259
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004260 /* ABI: return -EIO if already wedged */
4261 if (i915_terminally_wedged(&dev_priv->gpu_error))
4262 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004263
Chris Wilson1c255952010-09-26 11:03:27 +01004264 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004265 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004266 if (time_after_eq(request->emitted_jiffies, recent_enough))
4267 break;
4268
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004269 if (target) {
4270 list_del(&target->client_link);
4271 target->file_priv = NULL;
4272 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004273
John Harrison54fb2412014-11-24 18:49:27 +00004274 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004275 }
John Harrisonff865882014-11-24 18:49:28 +00004276 if (target)
Chris Wilsone61e0f52018-02-21 09:56:36 +00004277 i915_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004278 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004279
John Harrison54fb2412014-11-24 18:49:27 +00004280 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004281 return 0;
4282
Chris Wilsone61e0f52018-02-21 09:56:36 +00004283 ret = i915_request_wait(target,
Chris Wilsone95433c2016-10-28 13:58:27 +01004284 I915_WAIT_INTERRUPTIBLE,
4285 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone61e0f52018-02-21 09:56:36 +00004286 i915_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004287
Chris Wilsone95433c2016-10-28 13:58:27 +01004288 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004289}
4290
Chris Wilson058d88c2016-08-15 10:49:06 +01004291struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004292i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4293 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004294 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004295 u64 alignment,
4296 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004297{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004298 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4299 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004300 struct i915_vma *vma;
4301 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004302
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004303 lockdep_assert_held(&obj->base.dev->struct_mutex);
4304
Chris Wilsonac87a6fd2018-02-20 13:42:05 +00004305 if (flags & PIN_MAPPABLE &&
4306 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004307 /* If the required space is larger than the available
4308 * aperture, we will not able to find a slot for the
4309 * object and unbinding the object now will be in
4310 * vain. Worse, doing so may cause us to ping-pong
4311 * the object in and out of the Global GTT and
4312 * waste a lot of cycles under the mutex.
4313 */
4314 if (obj->base.size > dev_priv->ggtt.mappable_end)
4315 return ERR_PTR(-E2BIG);
4316
4317 /* If NONBLOCK is set the caller is optimistically
4318 * trying to cache the full object within the mappable
4319 * aperture, and *must* have a fallback in place for
4320 * situations where we cannot bind the object. We
4321 * can be a little more lax here and use the fallback
4322 * more often to avoid costly migrations of ourselves
4323 * and other objects within the aperture.
4324 *
4325 * Half-the-aperture is used as a simple heuristic.
4326 * More interesting would to do search for a free
4327 * block prior to making the commitment to unbind.
4328 * That caters for the self-harm case, and with a
4329 * little more heuristics (e.g. NOFAULT, NOEVICT)
4330 * we could try to minimise harm to others.
4331 */
4332 if (flags & PIN_NONBLOCK &&
4333 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4334 return ERR_PTR(-ENOSPC);
4335 }
4336
Chris Wilson718659a2017-01-16 15:21:28 +00004337 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004338 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004339 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004340
4341 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004342 if (flags & PIN_NONBLOCK) {
4343 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4344 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004345
Chris Wilson43ae70d2017-10-09 09:44:01 +01004346 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004347 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004348 return ERR_PTR(-ENOSPC);
4349 }
4350
Chris Wilson59bfa122016-08-04 16:32:31 +01004351 WARN(i915_vma_is_pinned(vma),
4352 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004353 " offset=%08x, req.alignment=%llx,"
4354 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4355 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004356 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004357 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004358 ret = i915_vma_unbind(vma);
4359 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004360 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004361 }
4362
Chris Wilson058d88c2016-08-15 10:49:06 +01004363 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4364 if (ret)
4365 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004366
Chris Wilson058d88c2016-08-15 10:49:06 +01004367 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004368}
4369
Chris Wilsonedf6b762016-08-09 09:23:33 +01004370static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004371{
4372 /* Note that we could alias engines in the execbuf API, but
4373 * that would be very unwise as it prevents userspace from
4374 * fine control over engine selection. Ahem.
4375 *
4376 * This should be something like EXEC_MAX_ENGINE instead of
4377 * I915_NUM_ENGINES.
4378 */
4379 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4380 return 0x10000 << id;
4381}
4382
4383static __always_inline unsigned int __busy_write_id(unsigned int id)
4384{
Chris Wilson70cb4722016-08-09 18:08:25 +01004385 /* The uABI guarantees an active writer is also amongst the read
4386 * engines. This would be true if we accessed the activity tracking
4387 * under the lock, but as we perform the lookup of the object and
4388 * its activity locklessly we can not guarantee that the last_write
4389 * being active implies that we have set the same engine flag from
4390 * last_read - hence we always set both read and write busy for
4391 * last_write.
4392 */
4393 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004394}
4395
Chris Wilsonedf6b762016-08-09 09:23:33 +01004396static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004397__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004398 unsigned int (*flag)(unsigned int id))
4399{
Chris Wilsone61e0f52018-02-21 09:56:36 +00004400 struct i915_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004401
Chris Wilsond07f0e52016-10-28 13:58:44 +01004402 /* We have to check the current hw status of the fence as the uABI
4403 * guarantees forward progress. We could rely on the idle worker
4404 * to eventually flush us, but to minimise latency just ask the
4405 * hardware.
4406 *
4407 * Note we only report on the status of native fences.
4408 */
4409 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004410 return 0;
4411
Chris Wilsond07f0e52016-10-28 13:58:44 +01004412 /* opencode to_request() in order to avoid const warnings */
Chris Wilsone61e0f52018-02-21 09:56:36 +00004413 rq = container_of(fence, struct i915_request, fence);
4414 if (i915_request_completed(rq))
Chris Wilsond07f0e52016-10-28 13:58:44 +01004415 return 0;
4416
Chris Wilson1d39f282017-04-11 13:43:06 +01004417 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004418}
4419
Chris Wilsonedf6b762016-08-09 09:23:33 +01004420static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004421busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004422{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004423 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004424}
4425
Chris Wilsonedf6b762016-08-09 09:23:33 +01004426static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004427busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004428{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004429 if (!fence)
4430 return 0;
4431
4432 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004433}
4434
Eric Anholt673a3942008-07-30 12:06:12 -07004435int
Eric Anholt673a3942008-07-30 12:06:12 -07004436i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004437 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004438{
4439 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004440 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004441 struct reservation_object_list *list;
4442 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004443 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004444
Chris Wilsond07f0e52016-10-28 13:58:44 +01004445 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004446 rcu_read_lock();
4447 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004448 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004449 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004450
4451 /* A discrepancy here is that we do not report the status of
4452 * non-i915 fences, i.e. even though we may report the object as idle,
4453 * a call to set-domain may still stall waiting for foreign rendering.
4454 * This also means that wait-ioctl may report an object as busy,
4455 * where busy-ioctl considers it idle.
4456 *
4457 * We trade the ability to warn of foreign fences to report on which
4458 * i915 engines are active for the object.
4459 *
4460 * Alternatively, we can trade that extra information on read/write
4461 * activity with
4462 * args->busy =
4463 * !reservation_object_test_signaled_rcu(obj->resv, true);
4464 * to report the overall busyness. This is what the wait-ioctl does.
4465 *
4466 */
4467retry:
4468 seq = raw_read_seqcount(&obj->resv->seq);
4469
4470 /* Translate the exclusive fence to the READ *and* WRITE engine */
4471 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4472
4473 /* Translate shared fences to READ set of engines */
4474 list = rcu_dereference(obj->resv->fence);
4475 if (list) {
4476 unsigned int shared_count = list->shared_count, i;
4477
4478 for (i = 0; i < shared_count; ++i) {
4479 struct dma_fence *fence =
4480 rcu_dereference(list->shared[i]);
4481
4482 args->busy |= busy_check_reader(fence);
4483 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004484 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004485
Chris Wilsond07f0e52016-10-28 13:58:44 +01004486 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4487 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004488
Chris Wilsond07f0e52016-10-28 13:58:44 +01004489 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004490out:
4491 rcu_read_unlock();
4492 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004493}
4494
4495int
4496i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4497 struct drm_file *file_priv)
4498{
Akshay Joshi0206e352011-08-16 15:34:10 -04004499 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004500}
4501
Chris Wilson3ef94da2009-09-14 16:50:29 +01004502int
4503i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4504 struct drm_file *file_priv)
4505{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004506 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004507 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004508 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004509 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004510
4511 switch (args->madv) {
4512 case I915_MADV_DONTNEED:
4513 case I915_MADV_WILLNEED:
4514 break;
4515 default:
4516 return -EINVAL;
4517 }
4518
Chris Wilson03ac0642016-07-20 13:31:51 +01004519 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004520 if (!obj)
4521 return -ENOENT;
4522
4523 err = mutex_lock_interruptible(&obj->mm.lock);
4524 if (err)
4525 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004526
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004527 if (i915_gem_object_has_pages(obj) &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004528 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004529 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004530 if (obj->mm.madv == I915_MADV_WILLNEED) {
4531 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004532 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004533 obj->mm.quirked = false;
4534 }
4535 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004536 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004537 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004538 obj->mm.quirked = true;
4539 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004540 }
4541
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004542 if (obj->mm.madv != __I915_MADV_PURGED)
4543 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004544
Chris Wilson6c085a72012-08-20 11:40:46 +02004545 /* if the object is no longer attached, discard its backing storage */
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004546 if (obj->mm.madv == I915_MADV_DONTNEED &&
4547 !i915_gem_object_has_pages(obj))
Chris Wilson2d7ef392009-09-20 23:13:10 +01004548 i915_gem_object_truncate(obj);
4549
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004550 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004551 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004552
Chris Wilson1233e2d2016-10-28 13:58:37 +01004553out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004554 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004555 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004556}
4557
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004558static void
Chris Wilsone61e0f52018-02-21 09:56:36 +00004559frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request)
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004560{
4561 struct drm_i915_gem_object *obj =
4562 container_of(active, typeof(*obj), frontbuffer_write);
4563
Chris Wilsond59b21e2017-02-22 11:40:49 +00004564 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004565}
4566
Chris Wilson37e680a2012-06-07 15:38:42 +01004567void i915_gem_object_init(struct drm_i915_gem_object *obj,
4568 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004569{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004570 mutex_init(&obj->mm.lock);
4571
Ben Widawsky2f633152013-07-17 12:19:03 -07004572 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004573 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004574 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004575
Chris Wilson37e680a2012-06-07 15:38:42 +01004576 obj->ops = ops;
4577
Chris Wilsond07f0e52016-10-28 13:58:44 +01004578 reservation_object_init(&obj->__builtin_resv);
4579 obj->resv = &obj->__builtin_resv;
4580
Chris Wilson50349242016-08-18 17:17:04 +01004581 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004582 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004583
4584 obj->mm.madv = I915_MADV_WILLNEED;
4585 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4586 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004587
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004588 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004589}
4590
Chris Wilson37e680a2012-06-07 15:38:42 +01004591static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004592 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4593 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004594
Chris Wilson37e680a2012-06-07 15:38:42 +01004595 .get_pages = i915_gem_object_get_pages_gtt,
4596 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004597
4598 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004599};
4600
Matthew Auld465c4032017-10-06 23:18:14 +01004601static int i915_gem_object_create_shmem(struct drm_device *dev,
4602 struct drm_gem_object *obj,
4603 size_t size)
4604{
4605 struct drm_i915_private *i915 = to_i915(dev);
4606 unsigned long flags = VM_NORESERVE;
4607 struct file *filp;
4608
4609 drm_gem_private_object_init(dev, obj, size);
4610
4611 if (i915->mm.gemfs)
4612 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4613 flags);
4614 else
4615 filp = shmem_file_setup("i915", size, flags);
4616
4617 if (IS_ERR(filp))
4618 return PTR_ERR(filp);
4619
4620 obj->filp = filp;
4621
4622 return 0;
4623}
4624
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004625struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004626i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004627{
Daniel Vetterc397b902010-04-09 19:05:07 +00004628 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004629 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004630 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004631 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004632 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004633
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004634 /* There is a prevalence of the assumption that we fit the object's
4635 * page count inside a 32bit _signed_ variable. Let's document this and
4636 * catch if we ever need to fix it. In the meantime, if you do spot
4637 * such a local variable, please consider fixing!
4638 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004639 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004640 return ERR_PTR(-E2BIG);
4641
4642 if (overflows_type(size, obj->base.size))
4643 return ERR_PTR(-E2BIG);
4644
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004645 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004646 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004647 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004648
Matthew Auld465c4032017-10-06 23:18:14 +01004649 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004650 if (ret)
4651 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004652
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004653 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004654 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004655 /* 965gm cannot relocate objects above 4GiB. */
4656 mask &= ~__GFP_HIGHMEM;
4657 mask |= __GFP_DMA32;
4658 }
4659
Al Viro93c76a32015-12-04 23:45:44 -05004660 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004661 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004662 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004663
Chris Wilson37e680a2012-06-07 15:38:42 +01004664 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004665
Christian Königc0a51fd2018-02-16 13:43:38 +01004666 obj->write_domain = I915_GEM_DOMAIN_CPU;
4667 obj->read_domains = I915_GEM_DOMAIN_CPU;
Daniel Vetterc397b902010-04-09 19:05:07 +00004668
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004669 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004670 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004671 * cache) for about a 10% performance improvement
4672 * compared to uncached. Graphics requests other than
4673 * display scanout are coherent with the CPU in
4674 * accessing this cache. This means in this mode we
4675 * don't need to clflush on the CPU side, and on the
4676 * GPU side we only need to flush internal caches to
4677 * get data visible to the CPU.
4678 *
4679 * However, we maintain the display planes as UC, and so
4680 * need to rebind when first used as such.
4681 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004682 cache_level = I915_CACHE_LLC;
4683 else
4684 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004685
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004686 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004687
Daniel Vetterd861e332013-07-24 23:25:03 +02004688 trace_i915_gem_object_create(obj);
4689
Chris Wilson05394f32010-11-08 19:18:58 +00004690 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004691
4692fail:
4693 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004694 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004695}
4696
Chris Wilson340fbd82014-05-22 09:16:52 +01004697static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4698{
4699 /* If we are the last user of the backing storage (be it shmemfs
4700 * pages or stolen etc), we know that the pages are going to be
4701 * immediately released. In this case, we can then skip copying
4702 * back the contents from the GPU.
4703 */
4704
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004705 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004706 return false;
4707
4708 if (obj->base.filp == NULL)
4709 return true;
4710
4711 /* At first glance, this looks racy, but then again so would be
4712 * userspace racing mmap against close. However, the first external
4713 * reference to the filp can only be obtained through the
4714 * i915_gem_mmap_ioctl() which safeguards us against the user
4715 * acquiring such a reference whilst we are in the middle of
4716 * freeing the object.
4717 */
4718 return atomic_long_read(&obj->base.filp->f_count) == 1;
4719}
4720
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004721static void __i915_gem_free_objects(struct drm_i915_private *i915,
4722 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004723{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004724 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004725
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004726 intel_runtime_pm_get(i915);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004727 llist_for_each_entry_safe(obj, on, freed, freed) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004728 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004729
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004730 trace_i915_gem_object_destroy(obj);
4731
Chris Wilsoncc731f52017-10-13 21:26:21 +01004732 mutex_lock(&i915->drm.struct_mutex);
4733
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004734 GEM_BUG_ON(i915_gem_object_is_active(obj));
4735 list_for_each_entry_safe(vma, vn,
4736 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004737 GEM_BUG_ON(i915_vma_is_active(vma));
4738 vma->flags &= ~I915_VMA_PIN_MASK;
4739 i915_vma_close(vma);
4740 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004741 GEM_BUG_ON(!list_empty(&obj->vma_list));
4742 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004743
Chris Wilsonf2123812017-10-16 12:40:37 +01004744 /* This serializes freeing with the shrinker. Since the free
4745 * is delayed, first by RCU then by the workqueue, we want the
4746 * shrinker to be able to free pages of unreferenced objects,
4747 * or else we may oom whilst there are plenty of deferred
4748 * freed objects.
4749 */
4750 if (i915_gem_object_has_pages(obj)) {
4751 spin_lock(&i915->mm.obj_lock);
4752 list_del_init(&obj->mm.link);
4753 spin_unlock(&i915->mm.obj_lock);
4754 }
4755
Chris Wilsoncc731f52017-10-13 21:26:21 +01004756 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004757
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004758 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004759 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004760 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004761 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004762
4763 if (obj->ops->release)
4764 obj->ops->release(obj);
4765
4766 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4767 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004768 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonf1fa4f42017-10-13 21:26:13 +01004769 GEM_BUG_ON(i915_gem_object_has_pages(obj));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004770
4771 if (obj->base.import_attach)
4772 drm_prime_gem_destroy(&obj->base, NULL);
4773
Chris Wilsond07f0e52016-10-28 13:58:44 +01004774 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004775 drm_gem_object_release(&obj->base);
4776 i915_gem_info_remove_obj(i915, obj->base.size);
4777
4778 kfree(obj->bit_17);
4779 i915_gem_object_free(obj);
Chris Wilsoncc731f52017-10-13 21:26:21 +01004780
Chris Wilsonc9c70472018-02-19 22:06:31 +00004781 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
4782 atomic_dec(&i915->mm.free_count);
4783
Chris Wilsoncc731f52017-10-13 21:26:21 +01004784 if (on)
4785 cond_resched();
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004786 }
Chris Wilsoncc731f52017-10-13 21:26:21 +01004787 intel_runtime_pm_put(i915);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004788}
4789
4790static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4791{
4792 struct llist_node *freed;
4793
Chris Wilson87701b42017-10-13 21:26:20 +01004794 /* Free the oldest, most stale object to keep the free_list short */
4795 freed = NULL;
4796 if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
4797 /* Only one consumer of llist_del_first() allowed */
4798 spin_lock(&i915->mm.free_lock);
4799 freed = llist_del_first(&i915->mm.free_list);
4800 spin_unlock(&i915->mm.free_lock);
4801 }
4802 if (unlikely(freed)) {
4803 freed->next = NULL;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004804 __i915_gem_free_objects(i915, freed);
Chris Wilson87701b42017-10-13 21:26:20 +01004805 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004806}
4807
4808static void __i915_gem_free_work(struct work_struct *work)
4809{
4810 struct drm_i915_private *i915 =
4811 container_of(work, struct drm_i915_private, mm.free_work);
4812 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004813
Chris Wilson2ef1e722018-01-15 20:57:59 +00004814 /*
4815 * All file-owned VMA should have been released by this point through
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004816 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4817 * However, the object may also be bound into the global GTT (e.g.
4818 * older GPUs without per-process support, or for direct access through
4819 * the GTT either for the user or for scanout). Those VMA still need to
4820 * unbound now.
4821 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004822
Chris Wilsonf991c492017-11-06 11:15:08 +00004823 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004824 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonf991c492017-11-06 11:15:08 +00004825 spin_unlock(&i915->mm.free_lock);
4826
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004827 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004828 if (need_resched())
Chris Wilsonf991c492017-11-06 11:15:08 +00004829 return;
4830
4831 spin_lock(&i915->mm.free_lock);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004832 }
Chris Wilsonf991c492017-11-06 11:15:08 +00004833 spin_unlock(&i915->mm.free_lock);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004834}
4835
4836static void __i915_gem_free_object_rcu(struct rcu_head *head)
4837{
4838 struct drm_i915_gem_object *obj =
4839 container_of(head, typeof(*obj), rcu);
4840 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4841
Chris Wilson2ef1e722018-01-15 20:57:59 +00004842 /*
4843 * Since we require blocking on struct_mutex to unbind the freed
4844 * object from the GPU before releasing resources back to the
4845 * system, we can not do that directly from the RCU callback (which may
4846 * be a softirq context), but must instead then defer that work onto a
4847 * kthread. We use the RCU callback rather than move the freed object
4848 * directly onto the work queue so that we can mix between using the
4849 * worker and performing frees directly from subsequent allocations for
4850 * crude but effective memory throttling.
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004851 */
4852 if (llist_add(&obj->freed, &i915->mm.free_list))
Chris Wilsonbeacbd12018-01-15 12:28:45 +00004853 queue_work(i915->wq, &i915->mm.free_work);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004854}
4855
4856void i915_gem_free_object(struct drm_gem_object *gem_obj)
4857{
4858 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4859
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004860 if (obj->mm.quirked)
4861 __i915_gem_object_unpin_pages(obj);
4862
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004863 if (discard_backing_storage(obj))
4864 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004865
Chris Wilson2ef1e722018-01-15 20:57:59 +00004866 /*
4867 * Before we free the object, make sure any pure RCU-only
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004868 * read-side critical sections are complete, e.g.
4869 * i915_gem_busy_ioctl(). For the corresponding synchronized
4870 * lookup see i915_gem_object_lookup_rcu().
4871 */
Chris Wilsonc9c70472018-02-19 22:06:31 +00004872 atomic_inc(&to_i915(obj->base.dev)->mm.free_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004873 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004874}
4875
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004876void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4877{
4878 lockdep_assert_held(&obj->base.dev->struct_mutex);
4879
Chris Wilsond1b48c12017-08-16 09:52:08 +01004880 if (!i915_gem_object_has_active_reference(obj) &&
4881 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004882 i915_gem_object_set_active_reference(obj);
4883 else
4884 i915_gem_object_put(obj);
4885}
4886
Chris Wilsonae6c4572017-11-10 14:26:28 +00004887static void assert_kernel_context_is_current(struct drm_i915_private *i915)
Chris Wilson3033aca2016-10-28 13:58:47 +01004888{
Chris Wilsonae6c4572017-11-10 14:26:28 +00004889 struct i915_gem_context *kernel_context = i915->kernel_context;
Chris Wilson3033aca2016-10-28 13:58:47 +01004890 struct intel_engine_cs *engine;
4891 enum intel_engine_id id;
4892
Chris Wilsonae6c4572017-11-10 14:26:28 +00004893 for_each_engine(engine, i915, id) {
4894 GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
4895 GEM_BUG_ON(engine->last_retired_context != kernel_context);
4896 }
Chris Wilson3033aca2016-10-28 13:58:47 +01004897}
4898
Chris Wilson24145512017-01-24 11:01:35 +00004899void i915_gem_sanitize(struct drm_i915_private *i915)
4900{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004901 if (i915_terminally_wedged(&i915->gpu_error)) {
4902 mutex_lock(&i915->drm.struct_mutex);
4903 i915_gem_unset_wedged(i915);
4904 mutex_unlock(&i915->drm.struct_mutex);
4905 }
4906
Chris Wilson24145512017-01-24 11:01:35 +00004907 /*
4908 * If we inherit context state from the BIOS or earlier occupants
4909 * of the GPU, the GPU may be in an inconsistent state when we
4910 * try to take over. The only way to remove the earlier state
4911 * is by resetting. However, resetting on earlier gen is tricky as
4912 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004913 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004914 */
Daniele Ceraolo Spurioce1599a2018-02-07 13:24:40 -08004915 if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915))
4916 WARN_ON(intel_gpu_reset(i915, ALL_ENGINES));
Chris Wilson24145512017-01-24 11:01:35 +00004917}
4918
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004919int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004920{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004921 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004922 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004923
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004924 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004925 intel_suspend_gt_powersave(dev_priv);
4926
Chris Wilson45c5f202013-10-16 11:50:01 +01004927 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004928
4929 /* We have to flush all the executing contexts to main memory so
4930 * that they can saved in the hibernation image. To ensure the last
4931 * context image is coherent, we have to switch away from it. That
4932 * leaves the dev_priv->kernel_context still active when
4933 * we actually suspend, and its image in memory may not match the GPU
4934 * state. Fortunately, the kernel_context is disposable and we do
4935 * not rely on its state.
4936 */
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004937 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
4938 ret = i915_gem_switch_to_kernel_context(dev_priv);
4939 if (ret)
4940 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004941
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004942 ret = i915_gem_wait_for_idle(dev_priv,
4943 I915_WAIT_INTERRUPTIBLE |
4944 I915_WAIT_LOCKED);
4945 if (ret && ret != -EIO)
4946 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004947
Chris Wilsonecf73eb2017-11-30 10:29:51 +00004948 assert_kernel_context_is_current(dev_priv);
4949 }
Chris Wilson829a0af2017-06-20 12:05:45 +01004950 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004951 mutex_unlock(&dev->struct_mutex);
4952
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00004953 intel_uc_suspend(dev_priv);
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304954
Chris Wilson737b1502015-01-26 18:03:03 +02004955 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004956 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004957
4958 /* As the idle_work is rearming if it detects a race, play safe and
4959 * repeat the flush until it is definitely idle.
4960 */
Chris Wilson7c262402017-10-06 11:40:38 +01004961 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004962
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004963 /* Assert that we sucessfully flushed all the work and
4964 * reset the GPU back to its idle, low power state.
4965 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004966 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004967 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4968 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004969
Imre Deak1c777c52016-10-12 17:46:37 +03004970 /*
4971 * Neither the BIOS, ourselves or any other kernel
4972 * expects the system to be in execlists mode on startup,
4973 * so we need to reset the GPU back to legacy mode. And the only
4974 * known way to disable logical contexts is through a GPU reset.
4975 *
4976 * So in order to leave the system in a known default configuration,
4977 * always reset the GPU upon unload and suspend. Afterwards we then
4978 * clean up the GEM state tracking, flushing off the requests and
4979 * leaving the system in a known idle state.
4980 *
4981 * Note that is of the upmost importance that the GPU is idle and
4982 * all stray writes are flushed *before* we dismantle the backing
4983 * storage for the pinned objects.
4984 *
4985 * However, since we are uncertain that resetting the GPU on older
4986 * machines is a good idea, we don't - just in case it leaves the
4987 * machine in an unusable condition.
4988 */
Michal Wajdeczkoc37d5722018-03-12 13:03:07 +00004989 intel_uc_sanitize(dev_priv);
Chris Wilson24145512017-01-24 11:01:35 +00004990 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004991
4992 intel_runtime_pm_put(dev_priv);
4993 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004994
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004995err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004996 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004997 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004998 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004999}
5000
Chris Wilson37cd3302017-11-12 11:27:38 +00005001void i915_gem_resume(struct drm_i915_private *i915)
Chris Wilson5ab57c72016-07-15 14:56:20 +01005002{
Chris Wilson37cd3302017-11-12 11:27:38 +00005003 WARN_ON(i915->gt.awake);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005004
Chris Wilson37cd3302017-11-12 11:27:38 +00005005 mutex_lock(&i915->drm.struct_mutex);
5006 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
Imre Deak31ab49a2016-11-07 11:20:05 +02005007
Chris Wilson37cd3302017-11-12 11:27:38 +00005008 i915_gem_restore_gtt_mappings(i915);
5009 i915_gem_restore_fences(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005010
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005011 /*
5012 * As we didn't flush the kernel context before suspend, we cannot
Chris Wilson5ab57c72016-07-15 14:56:20 +01005013 * guarantee that the context image is complete. So let's just reset
5014 * it and start again.
5015 */
Chris Wilson37cd3302017-11-12 11:27:38 +00005016 i915->gt.resume(i915);
Chris Wilson5ab57c72016-07-15 14:56:20 +01005017
Chris Wilson37cd3302017-11-12 11:27:38 +00005018 if (i915_gem_init_hw(i915))
5019 goto err_wedged;
5020
Michal Wajdeczko7cfca4a2018-03-02 11:15:49 +00005021 intel_uc_resume(i915);
Chris Wilson7469c622017-11-14 13:03:00 +00005022
Chris Wilson37cd3302017-11-12 11:27:38 +00005023 /* Always reload a context for powersaving. */
5024 if (i915_gem_switch_to_kernel_context(i915))
5025 goto err_wedged;
5026
5027out_unlock:
5028 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
5029 mutex_unlock(&i915->drm.struct_mutex);
5030 return;
5031
5032err_wedged:
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005033 if (!i915_terminally_wedged(&i915->gpu_error)) {
5034 DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
5035 i915_gem_set_wedged(i915);
5036 }
Chris Wilson37cd3302017-11-12 11:27:38 +00005037 goto out_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01005038}
5039
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005040void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005041{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005042 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005043 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
5044 return;
5045
5046 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
5047 DISP_TILE_SURFACE_SWIZZLING);
5048
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005049 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01005050 return;
5051
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005052 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005053 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005054 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005055 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02005056 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01005057 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07005058 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08005059 else
5060 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01005061}
Daniel Vettere21af882012-02-09 20:53:27 +01005062
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005063static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005064{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005065 I915_WRITE(RING_CTL(base), 0);
5066 I915_WRITE(RING_HEAD(base), 0);
5067 I915_WRITE(RING_TAIL(base), 0);
5068 I915_WRITE(RING_START(base), 0);
5069}
5070
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005071static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005072{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005073 if (IS_I830(dev_priv)) {
5074 init_unused_ring(dev_priv, PRB1_BASE);
5075 init_unused_ring(dev_priv, SRB0_BASE);
5076 init_unused_ring(dev_priv, SRB1_BASE);
5077 init_unused_ring(dev_priv, SRB2_BASE);
5078 init_unused_ring(dev_priv, SRB3_BASE);
5079 } else if (IS_GEN2(dev_priv)) {
5080 init_unused_ring(dev_priv, SRB0_BASE);
5081 init_unused_ring(dev_priv, SRB1_BASE);
5082 } else if (IS_GEN3(dev_priv)) {
5083 init_unused_ring(dev_priv, PRB1_BASE);
5084 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03005085 }
5086}
5087
Chris Wilson20a8a742017-02-08 14:30:31 +00005088static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005089{
Chris Wilson20a8a742017-02-08 14:30:31 +00005090 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005091 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305092 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00005093 int err;
5094
5095 for_each_engine(engine, i915, id) {
5096 err = engine->init_hw(engine);
Chris Wilson8177e112018-02-07 11:15:45 +00005097 if (err) {
5098 DRM_ERROR("Failed to restart %s (%d)\n",
5099 engine->name, err);
Chris Wilson20a8a742017-02-08 14:30:31 +00005100 return err;
Chris Wilson8177e112018-02-07 11:15:45 +00005101 }
Chris Wilson20a8a742017-02-08 14:30:31 +00005102 }
5103
5104 return 0;
5105}
5106
5107int i915_gem_init_hw(struct drm_i915_private *dev_priv)
5108{
Chris Wilsond200cda2016-04-28 09:56:44 +01005109 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005110
Chris Wilsonde867c22016-10-25 13:16:02 +01005111 dev_priv->gt.last_init_time = ktime_get();
5112
Chris Wilson5e4f5182015-02-13 14:35:59 +00005113 /* Double layer security blanket, see i915_gem_init() */
5114 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5115
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00005116 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07005117 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005118
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01005119 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005120 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02005121 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03005122
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01005123 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01005124 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005125 u32 temp = I915_READ(GEN7_MSG_CTL);
5126 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
5127 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005128 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01005129 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
5130 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
5131 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
5132 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07005133 }
5134
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005135 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08005136
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005137 /*
5138 * At least 830 can leave some of the unused rings
5139 * "active" (ie. head != tail) after resume which
5140 * will prevent c3 entry. Makes sure all unused rings
5141 * are totally idle.
5142 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01005143 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01005144
Dave Gordoned54c1a2016-01-19 19:02:54 +00005145 BUG_ON(!dev_priv->kernel_context);
Chris Wilson6f74b362017-10-15 15:37:25 +01005146 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
5147 ret = -EIO;
5148 goto out;
5149 }
John Harrison90638cc2015-05-29 17:43:37 +01005150
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00005151 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01005152 if (ret) {
Chris Wilson8177e112018-02-07 11:15:45 +00005153 DRM_ERROR("Enabling PPGTT failed (%d)\n", ret);
John Harrison4ad2fd82015-06-18 13:11:20 +01005154 goto out;
5155 }
5156
Jackie Lif08e2032018-03-13 17:32:53 -07005157 ret = intel_wopcm_init_hw(&dev_priv->wopcm);
5158 if (ret) {
5159 DRM_ERROR("Enabling WOPCM failed (%d)\n", ret);
5160 goto out;
5161 }
5162
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005163 /* We can't enable contexts until all firmware is loaded */
5164 ret = intel_uc_init_hw(dev_priv);
Chris Wilson8177e112018-02-07 11:15:45 +00005165 if (ret) {
5166 DRM_ERROR("Enabling uc failed (%d)\n", ret);
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005167 goto out;
Chris Wilson8177e112018-02-07 11:15:45 +00005168 }
Michał Winiarski9bdc3572017-10-25 18:25:19 +01005169
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005170 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01005171
Chris Wilson136109c2017-11-02 13:14:30 +00005172 /* Only when the HW is re-initialised, can we replay the requests */
5173 ret = __i915_gem_restart_engines(dev_priv);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005174out:
5175 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005176 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005177}
5178
Chris Wilsond2b4b972017-11-10 14:26:33 +00005179static int __intel_engines_record_defaults(struct drm_i915_private *i915)
5180{
5181 struct i915_gem_context *ctx;
5182 struct intel_engine_cs *engine;
5183 enum intel_engine_id id;
5184 int err;
5185
5186 /*
5187 * As we reset the gpu during very early sanitisation, the current
5188 * register state on the GPU should reflect its defaults values.
5189 * We load a context onto the hw (with restore-inhibit), then switch
5190 * over to a second context to save that default register state. We
5191 * can then prime every new context with that state so they all start
5192 * from the same default HW values.
5193 */
5194
5195 ctx = i915_gem_context_create_kernel(i915, 0);
5196 if (IS_ERR(ctx))
5197 return PTR_ERR(ctx);
5198
5199 for_each_engine(engine, i915, id) {
Chris Wilsone61e0f52018-02-21 09:56:36 +00005200 struct i915_request *rq;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005201
Chris Wilsone61e0f52018-02-21 09:56:36 +00005202 rq = i915_request_alloc(engine, ctx);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005203 if (IS_ERR(rq)) {
5204 err = PTR_ERR(rq);
5205 goto out_ctx;
5206 }
5207
Chris Wilson3fef5cd2017-11-20 10:20:02 +00005208 err = 0;
Chris Wilsond2b4b972017-11-10 14:26:33 +00005209 if (engine->init_context)
5210 err = engine->init_context(rq);
5211
Chris Wilsone61e0f52018-02-21 09:56:36 +00005212 __i915_request_add(rq, true);
Chris Wilsond2b4b972017-11-10 14:26:33 +00005213 if (err)
5214 goto err_active;
5215 }
5216
5217 err = i915_gem_switch_to_kernel_context(i915);
5218 if (err)
5219 goto err_active;
5220
5221 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
5222 if (err)
5223 goto err_active;
5224
5225 assert_kernel_context_is_current(i915);
5226
5227 for_each_engine(engine, i915, id) {
5228 struct i915_vma *state;
5229
5230 state = ctx->engine[id].state;
5231 if (!state)
5232 continue;
5233
5234 /*
5235 * As we will hold a reference to the logical state, it will
5236 * not be torn down with the context, and importantly the
5237 * object will hold onto its vma (making it possible for a
5238 * stray GTT write to corrupt our defaults). Unmap the vma
5239 * from the GTT to prevent such accidents and reclaim the
5240 * space.
5241 */
5242 err = i915_vma_unbind(state);
5243 if (err)
5244 goto err_active;
5245
5246 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
5247 if (err)
5248 goto err_active;
5249
5250 engine->default_state = i915_gem_object_get(state->obj);
5251 }
5252
5253 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
5254 unsigned int found = intel_engines_has_context_isolation(i915);
5255
5256 /*
5257 * Make sure that classes with multiple engine instances all
5258 * share the same basic configuration.
5259 */
5260 for_each_engine(engine, i915, id) {
5261 unsigned int bit = BIT(engine->uabi_class);
5262 unsigned int expected = engine->default_state ? bit : 0;
5263
5264 if ((found & bit) != expected) {
5265 DRM_ERROR("mismatching default context state for class %d on engine %s\n",
5266 engine->uabi_class, engine->name);
5267 }
5268 }
5269 }
5270
5271out_ctx:
5272 i915_gem_context_set_closed(ctx);
5273 i915_gem_context_put(ctx);
5274 return err;
5275
5276err_active:
5277 /*
5278 * If we have to abandon now, we expect the engines to be idle
5279 * and ready to be torn-down. First try to flush any remaining
5280 * request, ensure we are pointing at the kernel context and
5281 * then remove it.
5282 */
5283 if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
5284 goto out_ctx;
5285
5286 if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
5287 goto out_ctx;
5288
5289 i915_gem_contexts_lost(i915);
5290 goto out_ctx;
5291}
5292
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005293int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01005294{
Chris Wilson1070a422012-04-24 15:47:41 +01005295 int ret;
5296
Matthew Auldda9fe3f32017-10-06 23:18:31 +01005297 /*
5298 * We need to fallback to 4K pages since gvt gtt handling doesn't
5299 * support huge page entries - we will need to check either hypervisor
5300 * mm can support huge guest page or just do emulation in gvt.
5301 */
5302 if (intel_vgpu_active(dev_priv))
5303 mkwrite_device_info(dev_priv)->page_sizes =
5304 I915_GTT_PAGE_SIZE_4K;
5305
Chris Wilson94312822017-05-03 10:39:18 +01005306 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00005307
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005308 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01005309 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005310 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Chris Wilsonfb5c5512017-11-20 20:55:00 +00005311 } else {
5312 dev_priv->gt.resume = intel_legacy_submission_resume;
5313 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005314 }
5315
Chris Wilsonee487002017-11-22 17:26:21 +00005316 ret = i915_gem_init_userptr(dev_priv);
5317 if (ret)
5318 return ret;
5319
Jackie Li6b0478f2018-03-13 17:32:50 -07005320 ret = intel_wopcm_init(&dev_priv->wopcm);
5321 if (ret)
5322 return ret;
5323
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305324 ret = intel_uc_init_misc(dev_priv);
Michał Winiarski3176ff42017-12-13 23:13:47 +01005325 if (ret)
5326 return ret;
5327
Chris Wilson5e4f5182015-02-13 14:35:59 +00005328 /* This is just a security blanket to placate dragons.
5329 * On some systems, we very sporadically observe that the first TLBs
5330 * used by the CS may be stale, despite us poking the TLB reset. If
5331 * we hold the forcewake during initialisation these problems
5332 * just magically go away.
5333 */
Chris Wilsonee487002017-11-22 17:26:21 +00005334 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson5e4f5182015-02-13 14:35:59 +00005335 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5336
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01005337 ret = i915_gem_init_ggtt(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005338 if (ret) {
5339 GEM_BUG_ON(ret == -EIO);
5340 goto err_unlock;
5341 }
Jesse Barnesd62b4892013-03-08 10:45:53 -08005342
Chris Wilson829a0af2017-06-20 12:05:45 +01005343 ret = i915_gem_contexts_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005344 if (ret) {
5345 GEM_BUG_ON(ret == -EIO);
5346 goto err_ggtt;
5347 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005348
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00005349 ret = intel_engines_init(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005350 if (ret) {
5351 GEM_BUG_ON(ret == -EIO);
5352 goto err_context;
5353 }
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005354
Chris Wilsonf58d13d2017-11-10 14:26:29 +00005355 intel_init_gt_powersave(dev_priv);
5356
Michał Winiarski61b5c152017-12-13 23:13:48 +01005357 ret = intel_uc_init(dev_priv);
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005358 if (ret)
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005359 goto err_pm;
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005360
Michał Winiarski61b5c152017-12-13 23:13:48 +01005361 ret = i915_gem_init_hw(dev_priv);
5362 if (ret)
5363 goto err_uc_init;
5364
Chris Wilsoncc6a8182017-11-10 14:26:30 +00005365 /*
5366 * Despite its name intel_init_clock_gating applies both display
5367 * clock gating workarounds; GT mmio workarounds and the occasional
5368 * GT power context workaround. Worse, sometimes it includes a context
5369 * register workaround which we need to apply before we record the
5370 * default HW state for all contexts.
5371 *
5372 * FIXME: break up the workarounds and apply them at the right time!
5373 */
5374 intel_init_clock_gating(dev_priv);
5375
Chris Wilsond2b4b972017-11-10 14:26:33 +00005376 ret = __intel_engines_record_defaults(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005377 if (ret)
5378 goto err_init_hw;
5379
5380 if (i915_inject_load_failure()) {
5381 ret = -ENODEV;
5382 goto err_init_hw;
5383 }
5384
5385 if (i915_inject_load_failure()) {
5386 ret = -EIO;
5387 goto err_init_hw;
5388 }
5389
5390 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5391 mutex_unlock(&dev_priv->drm.struct_mutex);
5392
5393 return 0;
5394
5395 /*
5396 * Unwinding is complicated by that we want to handle -EIO to mean
5397 * disable GPU submission but keep KMS alive. We want to mark the
5398 * HW as irrevisibly wedged, but keep enough state around that the
5399 * driver doesn't explode during runtime.
5400 */
5401err_init_hw:
5402 i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
5403 i915_gem_contexts_lost(dev_priv);
5404 intel_uc_fini_hw(dev_priv);
Michał Winiarski61b5c152017-12-13 23:13:48 +01005405err_uc_init:
5406 intel_uc_fini(dev_priv);
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005407err_pm:
5408 if (ret != -EIO) {
5409 intel_cleanup_gt_powersave(dev_priv);
5410 i915_gem_cleanup_engines(dev_priv);
5411 }
5412err_context:
5413 if (ret != -EIO)
5414 i915_gem_contexts_fini(dev_priv);
5415err_ggtt:
5416err_unlock:
5417 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5418 mutex_unlock(&dev_priv->drm.struct_mutex);
5419
Sagar Arun Kamble70deead2018-01-24 21:16:58 +05305420 intel_uc_fini_misc(dev_priv);
Sagar Arun Kambleda943b52018-01-10 18:24:16 +05305421
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005422 if (ret != -EIO)
5423 i915_gem_cleanup_userptr(dev_priv);
5424
Chris Wilson60990322014-04-09 09:19:42 +01005425 if (ret == -EIO) {
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005426 /*
5427 * Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01005428 * wedged. But we only want to do this where the GPU is angry,
5429 * for all other failure, such as an allocation failure, bail.
5430 */
Chris Wilson6f74b362017-10-15 15:37:25 +01005431 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
5432 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
5433 i915_gem_set_wedged(dev_priv);
5434 }
Chris Wilson60990322014-04-09 09:19:42 +01005435 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005436 }
5437
Chris Wilson6ca9a2b2017-12-13 13:43:47 +00005438 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01005439 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005440}
5441
Chris Wilson24145512017-01-24 11:01:35 +00005442void i915_gem_init_mmio(struct drm_i915_private *i915)
5443{
5444 i915_gem_sanitize(i915);
5445}
5446
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005447void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005448i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005449{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005450 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305451 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005452
Akash Goel3b3f1652016-10-13 22:44:48 +05305453 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005454 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005455}
5456
Eric Anholt673a3942008-07-30 12:06:12 -07005457void
Imre Deak40ae4e12016-03-16 14:54:03 +02005458i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5459{
Chris Wilson49ef5292016-08-18 17:17:00 +01005460 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02005461
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005462 if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) &&
Imre Deak40ae4e12016-03-16 14:54:03 +02005463 !IS_CHERRYVIEW(dev_priv))
5464 dev_priv->num_fence_regs = 32;
Tvrtko Ursulinc56b89f2018-02-09 21:58:46 +00005465 else if (INTEL_GEN(dev_priv) >= 4 ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02005466 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5467 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005468 dev_priv->num_fence_regs = 16;
5469 else
5470 dev_priv->num_fence_regs = 8;
5471
Chris Wilsonc0336662016-05-06 15:40:21 +01005472 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005473 dev_priv->num_fence_regs =
5474 I915_READ(vgtif_reg(avail_rs.fence_num));
5475
5476 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01005477 for (i = 0; i < dev_priv->num_fence_regs; i++) {
5478 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
5479
5480 fence->i915 = dev_priv;
5481 fence->id = i;
5482 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
5483 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005484 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005485
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00005486 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02005487}
5488
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005489static void i915_gem_init__mm(struct drm_i915_private *i915)
5490{
5491 spin_lock_init(&i915->mm.object_stat_lock);
5492 spin_lock_init(&i915->mm.obj_lock);
5493 spin_lock_init(&i915->mm.free_lock);
5494
5495 init_llist_head(&i915->mm.free_list);
5496
5497 INIT_LIST_HEAD(&i915->mm.unbound_list);
5498 INIT_LIST_HEAD(&i915->mm.bound_list);
5499 INIT_LIST_HEAD(&i915->mm.fence_list);
5500 INIT_LIST_HEAD(&i915->mm.userfault_list);
5501
5502 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
5503}
5504
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005505int i915_gem_init_early(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07005506{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005507 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005508
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005509 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5510 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005511 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005512
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005513 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5514 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005515 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005516
Chris Wilsond1b48c12017-08-16 09:52:08 +01005517 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5518 if (!dev_priv->luts)
5519 goto err_vmas;
5520
Chris Wilsone61e0f52018-02-21 09:56:36 +00005521 dev_priv->requests = KMEM_CACHE(i915_request,
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005522 SLAB_HWCACHE_ALIGN |
5523 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005524 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005525 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005526 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005527
Chris Wilson52e54202016-11-14 20:41:02 +00005528 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5529 SLAB_HWCACHE_ALIGN |
5530 SLAB_RECLAIM_ACCOUNT);
5531 if (!dev_priv->dependencies)
5532 goto err_requests;
5533
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005534 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5535 if (!dev_priv->priorities)
5536 goto err_dependencies;
5537
Chris Wilson73cb9702016-10-28 13:58:46 +01005538 mutex_lock(&dev_priv->drm.struct_mutex);
5539 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005540 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005541 mutex_unlock(&dev_priv->drm.struct_mutex);
5542 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005543 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005544
Chris Wilson9c52d1c2017-11-10 23:24:47 +00005545 i915_gem_init__mm(dev_priv);
Chris Wilsonf2123812017-10-16 12:40:37 +01005546
Chris Wilson67d97da2016-07-04 08:08:31 +01005547 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005548 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005549 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005550 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005551 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005552 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005553
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005554 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5555
Chris Wilsonb5add952016-08-04 16:32:36 +01005556 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005557
Matthew Auld465c4032017-10-06 23:18:14 +01005558 err = i915_gemfs_init(dev_priv);
5559 if (err)
5560 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5561
Chris Wilson73cb9702016-10-28 13:58:46 +01005562 return 0;
5563
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005564err_priorities:
5565 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005566err_dependencies:
5567 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005568err_requests:
5569 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005570err_luts:
5571 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005572err_vmas:
5573 kmem_cache_destroy(dev_priv->vmas);
5574err_objects:
5575 kmem_cache_destroy(dev_priv->objects);
5576err_out:
5577 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005578}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005579
Michal Wajdeczkoa0de9082018-03-23 12:34:49 +00005580void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005581{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005582 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonc9c70472018-02-19 22:06:31 +00005583 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
5584 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005585 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005586
Matthew Auldea84aa72016-11-17 21:04:11 +00005587 mutex_lock(&dev_priv->drm.struct_mutex);
5588 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5589 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5590 mutex_unlock(&dev_priv->drm.struct_mutex);
5591
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005592 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005593 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005594 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005595 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005596 kmem_cache_destroy(dev_priv->vmas);
5597 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005598
5599 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5600 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005601
5602 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005603}
5604
Chris Wilson6a800ea2016-09-21 14:51:07 +01005605int i915_gem_freeze(struct drm_i915_private *dev_priv)
5606{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005607 /* Discard all purgeable objects, let userspace recover those as
5608 * required after resuming.
5609 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005610 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005611
Chris Wilson6a800ea2016-09-21 14:51:07 +01005612 return 0;
5613}
5614
Chris Wilson461fb992016-05-14 07:26:33 +01005615int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5616{
5617 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005618 struct list_head *phases[] = {
5619 &dev_priv->mm.unbound_list,
5620 &dev_priv->mm.bound_list,
5621 NULL
5622 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005623
5624 /* Called just before we write the hibernation image.
5625 *
5626 * We need to update the domain tracking to reflect that the CPU
5627 * will be accessing all the pages to create and restore from the
5628 * hibernation, and so upon restoration those pages will be in the
5629 * CPU domain.
5630 *
5631 * To make sure the hibernation image contains the latest state,
5632 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005633 *
5634 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005635 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005636 */
5637
Chris Wilson912d5722017-09-06 16:19:30 -07005638 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005639 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005640
Chris Wilsonf2123812017-10-16 12:40:37 +01005641 spin_lock(&dev_priv->mm.obj_lock);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005642 for (p = phases; *p; p++) {
Chris Wilsonf2123812017-10-16 12:40:37 +01005643 list_for_each_entry(obj, *p, mm.link)
Chris Wilsone27ab732017-06-15 13:38:49 +01005644 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005645 }
Chris Wilsonf2123812017-10-16 12:40:37 +01005646 spin_unlock(&dev_priv->mm.obj_lock);
Chris Wilson461fb992016-05-14 07:26:33 +01005647
5648 return 0;
5649}
5650
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005651void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005652{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005653 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsone61e0f52018-02-21 09:56:36 +00005654 struct i915_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005655
5656 /* Clean up our request list when the client is going away, so that
5657 * later retire_requests won't dereference our soon-to-be-gone
5658 * file_priv.
5659 */
Chris Wilson1c255952010-09-26 11:03:27 +01005660 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005661 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005662 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005663 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005664}
5665
Chris Wilson829a0af2017-06-20 12:05:45 +01005666int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005667{
5668 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005669 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005670
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005671 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005672
5673 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5674 if (!file_priv)
5675 return -ENOMEM;
5676
5677 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005678 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005679 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005680
5681 spin_lock_init(&file_priv->mm.lock);
5682 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005683
Chris Wilsonc80ff162016-07-27 09:07:27 +01005684 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005685
Chris Wilson829a0af2017-06-20 12:05:45 +01005686 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005687 if (ret)
5688 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005689
Ben Widawskye422b882013-12-06 14:10:58 -08005690 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005691}
5692
Daniel Vetterb680c372014-09-19 18:27:27 +02005693/**
5694 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005695 * @old: current GEM buffer for the frontbuffer slots
5696 * @new: new GEM buffer for the frontbuffer slots
5697 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005698 *
5699 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5700 * from @old and setting them in @new. Both @old and @new can be NULL.
5701 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005702void i915_gem_track_fb(struct drm_i915_gem_object *old,
5703 struct drm_i915_gem_object *new,
5704 unsigned frontbuffer_bits)
5705{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005706 /* Control of individual bits within the mask are guarded by
5707 * the owning plane->mutex, i.e. we can never see concurrent
5708 * manipulation of individual bits. But since the bitfield as a whole
5709 * is updated using RMW, we need to use atomics in order to update
5710 * the bits.
5711 */
5712 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5713 sizeof(atomic_t) * BITS_PER_BYTE);
5714
Daniel Vettera071fa02014-06-18 23:28:09 +02005715 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005716 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5717 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005718 }
5719
5720 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005721 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5722 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005723 }
5724}
5725
Dave Gordonea702992015-07-09 19:29:02 +01005726/* Allocate a new GEM object and fill it with the supplied data */
5727struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005728i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005729 const void *data, size_t size)
5730{
5731 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005732 struct file *file;
5733 size_t offset;
5734 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005735
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005736 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005737 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005738 return obj;
5739
Christian Königc0a51fd2018-02-16 13:43:38 +01005740 GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005741
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005742 file = obj->base.filp;
5743 offset = 0;
5744 do {
5745 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5746 struct page *page;
5747 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005748
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005749 err = pagecache_write_begin(file, file->f_mapping,
5750 offset, len, 0,
5751 &page, &pgdata);
5752 if (err < 0)
5753 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005754
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005755 vaddr = kmap(page);
5756 memcpy(vaddr, data, len);
5757 kunmap(page);
5758
5759 err = pagecache_write_end(file, file->f_mapping,
5760 offset, len, len,
5761 page, pgdata);
5762 if (err < 0)
5763 goto fail;
5764
5765 size -= len;
5766 data += len;
5767 offset += len;
5768 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005769
5770 return obj;
5771
5772fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005773 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005774 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005775}
Chris Wilson96d77632016-10-28 13:58:33 +01005776
5777struct scatterlist *
5778i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5779 unsigned int n,
5780 unsigned int *offset)
5781{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005782 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005783 struct scatterlist *sg;
5784 unsigned int idx, count;
5785
5786 might_sleep();
5787 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005788 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005789
5790 /* As we iterate forward through the sg, we record each entry in a
5791 * radixtree for quick repeated (backwards) lookups. If we have seen
5792 * this index previously, we will have an entry for it.
5793 *
5794 * Initial lookup is O(N), but this is amortized to O(1) for
5795 * sequential page access (where each new request is consecutive
5796 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5797 * i.e. O(1) with a large constant!
5798 */
5799 if (n < READ_ONCE(iter->sg_idx))
5800 goto lookup;
5801
5802 mutex_lock(&iter->lock);
5803
5804 /* We prefer to reuse the last sg so that repeated lookup of this
5805 * (or the subsequent) sg are fast - comparing against the last
5806 * sg is faster than going through the radixtree.
5807 */
5808
5809 sg = iter->sg_pos;
5810 idx = iter->sg_idx;
5811 count = __sg_page_count(sg);
5812
5813 while (idx + count <= n) {
5814 unsigned long exception, i;
5815 int ret;
5816
5817 /* If we cannot allocate and insert this entry, or the
5818 * individual pages from this range, cancel updating the
5819 * sg_idx so that on this lookup we are forced to linearly
5820 * scan onwards, but on future lookups we will try the
5821 * insertion again (in which case we need to be careful of
5822 * the error return reporting that we have already inserted
5823 * this index).
5824 */
5825 ret = radix_tree_insert(&iter->radix, idx, sg);
5826 if (ret && ret != -EEXIST)
5827 goto scan;
5828
5829 exception =
5830 RADIX_TREE_EXCEPTIONAL_ENTRY |
5831 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5832 for (i = 1; i < count; i++) {
5833 ret = radix_tree_insert(&iter->radix, idx + i,
5834 (void *)exception);
5835 if (ret && ret != -EEXIST)
5836 goto scan;
5837 }
5838
5839 idx += count;
5840 sg = ____sg_next(sg);
5841 count = __sg_page_count(sg);
5842 }
5843
5844scan:
5845 iter->sg_pos = sg;
5846 iter->sg_idx = idx;
5847
5848 mutex_unlock(&iter->lock);
5849
5850 if (unlikely(n < idx)) /* insertion completed by another thread */
5851 goto lookup;
5852
5853 /* In case we failed to insert the entry into the radixtree, we need
5854 * to look beyond the current sg.
5855 */
5856 while (idx + count <= n) {
5857 idx += count;
5858 sg = ____sg_next(sg);
5859 count = __sg_page_count(sg);
5860 }
5861
5862 *offset = n - idx;
5863 return sg;
5864
5865lookup:
5866 rcu_read_lock();
5867
5868 sg = radix_tree_lookup(&iter->radix, n);
5869 GEM_BUG_ON(!sg);
5870
5871 /* If this index is in the middle of multi-page sg entry,
5872 * the radixtree will contain an exceptional entry that points
5873 * to the start of that range. We will return the pointer to
5874 * the base page and the offset of this page within the
5875 * sg entry's range.
5876 */
5877 *offset = 0;
5878 if (unlikely(radix_tree_exception(sg))) {
5879 unsigned long base =
5880 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5881
5882 sg = radix_tree_lookup(&iter->radix, base);
5883 GEM_BUG_ON(!sg);
5884
5885 *offset = n - base;
5886 }
5887
5888 rcu_read_unlock();
5889
5890 return sg;
5891}
5892
5893struct page *
5894i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5895{
5896 struct scatterlist *sg;
5897 unsigned int offset;
5898
5899 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5900
5901 sg = i915_gem_object_get_sg(obj, n, &offset);
5902 return nth_page(sg_page(sg), offset);
5903}
5904
5905/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5906struct page *
5907i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5908 unsigned int n)
5909{
5910 struct page *page;
5911
5912 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005913 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005914 set_page_dirty(page);
5915
5916 return page;
5917}
5918
5919dma_addr_t
5920i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5921 unsigned long n)
5922{
5923 struct scatterlist *sg;
5924 unsigned int offset;
5925
5926 sg = i915_gem_object_get_sg(obj, n, &offset);
5927 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5928}
Chris Wilson935a2f72017-02-13 17:15:13 +00005929
Chris Wilson8eeb7902017-07-26 19:16:01 +01005930int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5931{
5932 struct sg_table *pages;
5933 int err;
5934
5935 if (align > obj->base.size)
5936 return -EINVAL;
5937
5938 if (obj->ops == &i915_gem_phys_ops)
5939 return 0;
5940
5941 if (obj->ops != &i915_gem_object_ops)
5942 return -EINVAL;
5943
5944 err = i915_gem_object_unbind(obj);
5945 if (err)
5946 return err;
5947
5948 mutex_lock(&obj->mm.lock);
5949
5950 if (obj->mm.madv != I915_MADV_WILLNEED) {
5951 err = -EFAULT;
5952 goto err_unlock;
5953 }
5954
5955 if (obj->mm.quirked) {
5956 err = -EFAULT;
5957 goto err_unlock;
5958 }
5959
5960 if (obj->mm.mapping) {
5961 err = -EBUSY;
5962 goto err_unlock;
5963 }
5964
Chris Wilsonf2123812017-10-16 12:40:37 +01005965 pages = fetch_and_zero(&obj->mm.pages);
5966 if (pages) {
5967 struct drm_i915_private *i915 = to_i915(obj->base.dev);
5968
5969 __i915_gem_object_reset_page_iter(obj);
5970
5971 spin_lock(&i915->mm.obj_lock);
5972 list_del(&obj->mm.link);
5973 spin_unlock(&i915->mm.obj_lock);
5974 }
5975
Chris Wilson8eeb7902017-07-26 19:16:01 +01005976 obj->ops = &i915_gem_phys_ops;
5977
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005978 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005979 if (err)
5980 goto err_xfer;
5981
5982 /* Perma-pin (until release) the physical set of pages */
5983 __i915_gem_object_pin_pages(obj);
5984
5985 if (!IS_ERR_OR_NULL(pages))
5986 i915_gem_object_ops.put_pages(obj, pages);
5987 mutex_unlock(&obj->mm.lock);
5988 return 0;
5989
5990err_xfer:
5991 obj->ops = &i915_gem_object_ops;
5992 obj->mm.pages = pages;
5993err_unlock:
5994 mutex_unlock(&obj->mm.lock);
5995 return err;
5996}
5997
Chris Wilson935a2f72017-02-13 17:15:13 +00005998#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5999#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00006000#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00006001#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01006002#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00006003#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00006004#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00006005#endif