blob: 20fcac37c85a0f2332a84c8d4922e26dc88b7466 [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
59 return obj->pin_display;
60}
61
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053062static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +010063insert_mappable_node(struct i915_ggtt *ggtt,
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053064 struct drm_mm_node *node, u32 size)
65{
66 memset(node, 0, sizeof(*node));
Chris Wilson4e64e552017-02-02 21:04:38 +000067 return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
68 size, 0, I915_COLOR_UNEVICTABLE,
69 0, ggtt->mappable_end,
70 DRM_MM_INSERT_LOW);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +053071}
72
73static void
74remove_mappable_node(struct drm_mm_node *node)
75{
76 drm_mm_remove_node(node);
77}
78
Chris Wilson73aa8082010-09-30 11:46:12 +010079/* some bookkeeping */
80static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010081 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010082{
Daniel Vetterc20e8352013-07-24 22:40:23 +020083 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010084 dev_priv->mm.object_count++;
85 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020086 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010087}
88
89static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
Chris Wilson3ef7f222016-10-18 13:02:48 +010090 u64 size)
Chris Wilson73aa8082010-09-30 11:46:12 +010091{
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093 dev_priv->mm.object_count--;
94 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096}
97
Chris Wilson21dd3732011-01-26 15:55:56 +000098static int
Daniel Vetter33196de2012-11-14 17:14:05 +010099i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100101 int ret;
102
Chris Wilson4c7d62c2016-10-28 13:58:32 +0100103 might_sleep();
104
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200105 /*
106 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
107 * userspace. If it takes that long something really bad is going on and
108 * we should simply try to bail out and fail as gracefully as possible.
109 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100110 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilson8c185ec2017-03-16 17:13:02 +0000111 !i915_reset_backoff(error),
Chris Wilsonb52992c2016-10-28 13:58:24 +0100112 I915_RESET_TIMEOUT);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 if (ret == 0) {
114 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
115 return -EIO;
116 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100118 } else {
119 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200120 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100121}
122
Chris Wilson54cf91d2010-11-25 18:00:26 +0000123int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100124{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100125 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100126 int ret;
127
Daniel Vetter33196de2012-11-14 17:14:05 +0100128 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 if (ret)
130 return ret;
131
132 ret = mutex_lock_interruptible(&dev->struct_mutex);
133 if (ret)
134 return ret;
135
Chris Wilson76c1dec2010-09-25 11:22:51 +0100136 return 0;
137}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100138
Eric Anholt673a3942008-07-30 12:06:12 -0700139int
Eric Anholt5a125c32008-10-22 21:40:13 -0700140i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000141 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700142{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300143 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200144 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300145 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100146 struct i915_vma *vma;
Weinan Liff8f7972017-05-31 10:35:52 +0800147 u64 pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700148
Weinan Liff8f7972017-05-31 10:35:52 +0800149 pinned = ggtt->base.reserved;
Chris Wilson73aa8082010-09-30 11:46:12 +0100150 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000151 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100152 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100153 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000154 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Chris Wilson20dfbde2016-08-04 16:32:30 +0100155 if (i915_vma_is_pinned(vma))
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100156 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100157 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700158
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300159 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000161
Eric Anholt5a125c32008-10-22 21:40:13 -0700162 return 0;
163}
164
Matthew Auldb91b09e2017-10-06 23:18:17 +0100165static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100166{
Al Viro93c76a32015-12-04 23:45:44 -0500167 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilsondbb43512016-12-07 13:34:11 +0000168 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800169 struct sg_table *st;
170 struct scatterlist *sg;
Chris Wilsondbb43512016-12-07 13:34:11 +0000171 char *vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800172 int i;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100173 int err;
Chris Wilson00731152014-05-21 12:42:56 +0100174
Chris Wilson6a2c4232014-11-04 04:51:40 -0800175 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
Matthew Auldb91b09e2017-10-06 23:18:17 +0100176 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100177
Chris Wilsondbb43512016-12-07 13:34:11 +0000178 /* Always aligning to the object size, allows a single allocation
179 * to handle all possible callers, and given typical object sizes,
180 * the alignment of the buddy allocation will naturally match.
181 */
182 phys = drm_pci_alloc(obj->base.dev,
Ville Syrjälä750fae22017-09-07 17:32:03 +0300183 roundup_pow_of_two(obj->base.size),
Chris Wilsondbb43512016-12-07 13:34:11 +0000184 roundup_pow_of_two(obj->base.size));
185 if (!phys)
Matthew Auldb91b09e2017-10-06 23:18:17 +0100186 return -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000187
188 vaddr = phys->vaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800189 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
190 struct page *page;
191 char *src;
192
193 page = shmem_read_mapping_page(mapping, i);
Chris Wilsondbb43512016-12-07 13:34:11 +0000194 if (IS_ERR(page)) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100195 err = PTR_ERR(page);
Chris Wilsondbb43512016-12-07 13:34:11 +0000196 goto err_phys;
197 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800198
199 src = kmap_atomic(page);
200 memcpy(vaddr, src, PAGE_SIZE);
201 drm_clflush_virt_range(vaddr, PAGE_SIZE);
202 kunmap_atomic(src);
203
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300204 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800205 vaddr += PAGE_SIZE;
206 }
207
Chris Wilsonc0336662016-05-06 15:40:21 +0100208 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800209
210 st = kmalloc(sizeof(*st), GFP_KERNEL);
Chris Wilsondbb43512016-12-07 13:34:11 +0000211 if (!st) {
Matthew Auldb91b09e2017-10-06 23:18:17 +0100212 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000213 goto err_phys;
214 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800215
216 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
217 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100218 err = -ENOMEM;
Chris Wilsondbb43512016-12-07 13:34:11 +0000219 goto err_phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800220 }
221
222 sg = st->sgl;
223 sg->offset = 0;
224 sg->length = obj->base.size;
225
Chris Wilsondbb43512016-12-07 13:34:11 +0000226 sg_dma_address(sg) = phys->busaddr;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800227 sg_dma_len(sg) = obj->base.size;
228
Chris Wilsondbb43512016-12-07 13:34:11 +0000229 obj->phys_handle = phys;
Matthew Auldb91b09e2017-10-06 23:18:17 +0100230
Matthew Aulda5c081662017-10-06 23:18:18 +0100231 __i915_gem_object_set_pages(obj, st, sg->length);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100232
233 return 0;
Chris Wilsondbb43512016-12-07 13:34:11 +0000234
235err_phys:
236 drm_pci_free(obj->base.dev, phys);
Matthew Auldb91b09e2017-10-06 23:18:17 +0100237
238 return err;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800239}
240
Chris Wilsone27ab732017-06-15 13:38:49 +0100241static void __start_cpu_write(struct drm_i915_gem_object *obj)
242{
243 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
244 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
245 if (cpu_write_needs_clflush(obj))
246 obj->cache_dirty = true;
247}
248
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249static void
Chris Wilson2b3c8312016-11-11 14:58:09 +0000250__i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
Chris Wilsone5facdf2016-12-23 14:57:57 +0000251 struct sg_table *pages,
252 bool needs_clflush)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800253{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100254 GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800255
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100256 if (obj->mm.madv == I915_MADV_DONTNEED)
257 obj->mm.dirty = false;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800258
Chris Wilsone5facdf2016-12-23 14:57:57 +0000259 if (needs_clflush &&
260 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100261 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
Chris Wilson2b3c8312016-11-11 14:58:09 +0000262 drm_clflush_sg(pages);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100263
Chris Wilsone27ab732017-06-15 13:38:49 +0100264 __start_cpu_write(obj);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100265}
266
267static void
268i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
269 struct sg_table *pages)
270{
Chris Wilsone5facdf2016-12-23 14:57:57 +0000271 __i915_gem_object_release_shmem(obj, pages, false);
Chris Wilson03ac84f2016-10-28 13:58:36 +0100272
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100273 if (obj->mm.dirty) {
Al Viro93c76a32015-12-04 23:45:44 -0500274 struct address_space *mapping = obj->base.filp->f_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800275 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100276 int i;
277
278 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800279 struct page *page;
280 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100281
Chris Wilson6a2c4232014-11-04 04:51:40 -0800282 page = shmem_read_mapping_page(mapping, i);
283 if (IS_ERR(page))
284 continue;
285
286 dst = kmap_atomic(page);
287 drm_clflush_virt_range(vaddr, PAGE_SIZE);
288 memcpy(dst, vaddr, PAGE_SIZE);
289 kunmap_atomic(dst);
290
291 set_page_dirty(page);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100292 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100293 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300294 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100295 vaddr += PAGE_SIZE;
296 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100297 obj->mm.dirty = false;
Chris Wilson00731152014-05-21 12:42:56 +0100298 }
299
Chris Wilson03ac84f2016-10-28 13:58:36 +0100300 sg_free_table(pages);
301 kfree(pages);
Chris Wilsondbb43512016-12-07 13:34:11 +0000302
303 drm_pci_free(obj->base.dev, obj->phys_handle);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800304}
305
306static void
307i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
308{
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100309 i915_gem_object_unpin_pages(obj);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800310}
311
312static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
313 .get_pages = i915_gem_object_get_pages_phys,
314 .put_pages = i915_gem_object_put_pages_phys,
315 .release = i915_gem_object_release_phys,
316};
317
Chris Wilson581ab1f2017-02-15 16:39:00 +0000318static const struct drm_i915_gem_object_ops i915_gem_object_ops;
319
Chris Wilson35a96112016-08-14 18:44:40 +0100320int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Chris Wilsonaa653a62016-08-04 07:52:27 +0100321{
322 struct i915_vma *vma;
323 LIST_HEAD(still_in_list);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100324 int ret;
Chris Wilsonaa653a62016-08-04 07:52:27 +0100325
Chris Wilson02bef8f2016-08-14 18:44:41 +0100326 lockdep_assert_held(&obj->base.dev->struct_mutex);
327
328 /* Closed vma are removed from the obj->vma_list - but they may
329 * still have an active binding on the object. To remove those we
330 * must wait for all rendering to complete to the object (as unbinding
331 * must anyway), and retire the requests.
Chris Wilsonaa653a62016-08-04 07:52:27 +0100332 */
Chris Wilsone95433c2016-10-28 13:58:27 +0100333 ret = i915_gem_object_wait(obj,
334 I915_WAIT_INTERRUPTIBLE |
335 I915_WAIT_LOCKED |
336 I915_WAIT_ALL,
337 MAX_SCHEDULE_TIMEOUT,
338 NULL);
Chris Wilson02bef8f2016-08-14 18:44:41 +0100339 if (ret)
340 return ret;
341
342 i915_gem_retire_requests(to_i915(obj->base.dev));
343
Chris Wilsonaa653a62016-08-04 07:52:27 +0100344 while ((vma = list_first_entry_or_null(&obj->vma_list,
345 struct i915_vma,
346 obj_link))) {
347 list_move_tail(&vma->obj_link, &still_in_list);
348 ret = i915_vma_unbind(vma);
349 if (ret)
350 break;
351 }
352 list_splice(&still_in_list, &obj->vma_list);
353
354 return ret;
355}
356
Chris Wilsone95433c2016-10-28 13:58:27 +0100357static long
358i915_gem_object_wait_fence(struct dma_fence *fence,
359 unsigned int flags,
360 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100361 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100362{
363 struct drm_i915_gem_request *rq;
364
365 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
366
367 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
368 return timeout;
369
370 if (!dma_fence_is_i915(fence))
371 return dma_fence_wait_timeout(fence,
372 flags & I915_WAIT_INTERRUPTIBLE,
373 timeout);
374
375 rq = to_request(fence);
376 if (i915_gem_request_completed(rq))
377 goto out;
378
379 /* This client is about to stall waiting for the GPU. In many cases
380 * this is undesirable and limits the throughput of the system, as
381 * many clients cannot continue processing user input/output whilst
382 * blocked. RPS autotuning may take tens of milliseconds to respond
383 * to the GPU load and thus incurs additional latency for the client.
384 * We can circumvent that by promoting the GPU frequency to maximum
385 * before we wait. This makes the GPU throttle up much more quickly
386 * (good for benchmarks and user experience, e.g. window animations),
387 * but at a cost of spending more power processing the workload
388 * (bad for battery). Not all clients even want their results
389 * immediately and for them we should just let the GPU select its own
390 * frequency to maximise efficiency. To prevent a single client from
391 * forcing the clocks too high for the whole system, we only allow
392 * each client to waitboost once in a busy period.
393 */
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100394 if (rps_client) {
Chris Wilsone95433c2016-10-28 13:58:27 +0100395 if (INTEL_GEN(rq->i915) >= 6)
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100396 gen6_rps_boost(rq, rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100397 else
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100398 rps_client = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +0100399 }
400
401 timeout = i915_wait_request(rq, flags, timeout);
402
403out:
404 if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
405 i915_gem_request_retire_upto(rq);
406
Chris Wilsone95433c2016-10-28 13:58:27 +0100407 return timeout;
408}
409
410static long
411i915_gem_object_wait_reservation(struct reservation_object *resv,
412 unsigned int flags,
413 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100414 struct intel_rps_client *rps_client)
Chris Wilsone95433c2016-10-28 13:58:27 +0100415{
Chris Wilsone54ca972017-02-17 15:13:04 +0000416 unsigned int seq = __read_seqcount_begin(&resv->seq);
Chris Wilsone95433c2016-10-28 13:58:27 +0100417 struct dma_fence *excl;
Chris Wilsone54ca972017-02-17 15:13:04 +0000418 bool prune_fences = false;
Chris Wilsone95433c2016-10-28 13:58:27 +0100419
420 if (flags & I915_WAIT_ALL) {
421 struct dma_fence **shared;
422 unsigned int count, i;
423 int ret;
424
425 ret = reservation_object_get_fences_rcu(resv,
426 &excl, &count, &shared);
427 if (ret)
428 return ret;
429
430 for (i = 0; i < count; i++) {
431 timeout = i915_gem_object_wait_fence(shared[i],
432 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100433 rps_client);
Chris Wilsond892e932017-02-12 21:53:43 +0000434 if (timeout < 0)
Chris Wilsone95433c2016-10-28 13:58:27 +0100435 break;
436
437 dma_fence_put(shared[i]);
438 }
439
440 for (; i < count; i++)
441 dma_fence_put(shared[i]);
442 kfree(shared);
Chris Wilsone54ca972017-02-17 15:13:04 +0000443
444 prune_fences = count && timeout >= 0;
Chris Wilsone95433c2016-10-28 13:58:27 +0100445 } else {
446 excl = reservation_object_get_excl_rcu(resv);
447 }
448
Chris Wilsone54ca972017-02-17 15:13:04 +0000449 if (excl && timeout >= 0) {
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100450 timeout = i915_gem_object_wait_fence(excl, flags, timeout,
451 rps_client);
Chris Wilsone54ca972017-02-17 15:13:04 +0000452 prune_fences = timeout >= 0;
453 }
Chris Wilsone95433c2016-10-28 13:58:27 +0100454
455 dma_fence_put(excl);
456
Chris Wilson03d1cac2017-03-08 13:26:28 +0000457 /* Oportunistically prune the fences iff we know they have *all* been
458 * 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{
474 struct drm_i915_gem_request *rq;
475 struct intel_engine_cs *engine;
476
477 if (!dma_fence_is_i915(fence))
478 return;
479
480 rq = to_request(fence);
481 engine = rq->engine;
482 if (!engine->schedule)
483 return;
484
485 engine->schedule(rq, prio);
486}
487
488static void fence_set_priority(struct dma_fence *fence, int prio)
489{
490 /* Recurse once into a fence-array */
491 if (dma_fence_is_array(fence)) {
492 struct dma_fence_array *array = to_dma_fence_array(fence);
493 int i;
494
495 for (i = 0; i < array->num_fences; i++)
496 __fence_set_priority(array->fences[i], prio);
497 } else {
498 __fence_set_priority(fence, prio);
499 }
500}
501
502int
503i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
504 unsigned int flags,
505 int prio)
506{
507 struct dma_fence *excl;
508
509 if (flags & I915_WAIT_ALL) {
510 struct dma_fence **shared;
511 unsigned int count, i;
512 int ret;
513
514 ret = reservation_object_get_fences_rcu(obj->resv,
515 &excl, &count, &shared);
516 if (ret)
517 return ret;
518
519 for (i = 0; i < count; i++) {
520 fence_set_priority(shared[i], prio);
521 dma_fence_put(shared[i]);
522 }
523
524 kfree(shared);
525 } else {
526 excl = reservation_object_get_excl_rcu(obj->resv);
527 }
528
529 if (excl) {
530 fence_set_priority(excl, prio);
531 dma_fence_put(excl);
532 }
533 return 0;
534}
535
Chris Wilson00e60f22016-08-04 16:32:40 +0100536/**
Chris Wilsone95433c2016-10-28 13:58:27 +0100537 * Waits for rendering to the object to be completed
Chris Wilson00e60f22016-08-04 16:32:40 +0100538 * @obj: i915 gem object
Chris Wilsone95433c2016-10-28 13:58:27 +0100539 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
540 * @timeout: how long to wait
541 * @rps: client (user process) to charge for any waitboosting
Chris Wilson00e60f22016-08-04 16:32:40 +0100542 */
543int
Chris Wilsone95433c2016-10-28 13:58:27 +0100544i915_gem_object_wait(struct drm_i915_gem_object *obj,
545 unsigned int flags,
546 long timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100547 struct intel_rps_client *rps_client)
Chris Wilson00e60f22016-08-04 16:32:40 +0100548{
Chris Wilsone95433c2016-10-28 13:58:27 +0100549 might_sleep();
550#if IS_ENABLED(CONFIG_LOCKDEP)
551 GEM_BUG_ON(debug_locks &&
552 !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
553 !!(flags & I915_WAIT_LOCKED));
554#endif
555 GEM_BUG_ON(timeout < 0);
Chris Wilson00e60f22016-08-04 16:32:40 +0100556
Chris Wilsond07f0e52016-10-28 13:58:44 +0100557 timeout = i915_gem_object_wait_reservation(obj->resv,
558 flags, timeout,
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100559 rps_client);
Chris Wilsone95433c2016-10-28 13:58:27 +0100560 return timeout < 0 ? timeout : 0;
Chris Wilson00e60f22016-08-04 16:32:40 +0100561}
562
563static struct intel_rps_client *to_rps_client(struct drm_file *file)
564{
565 struct drm_i915_file_private *fpriv = file->driver_priv;
566
Sagar Arun Kamble562d9ba2017-10-10 22:30:06 +0100567 return &fpriv->rps_client;
Chris Wilson00e60f22016-08-04 16:32:40 +0100568}
569
Chris Wilson00731152014-05-21 12:42:56 +0100570static int
571i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
572 struct drm_i915_gem_pwrite *args,
Chris Wilson03ac84f2016-10-28 13:58:36 +0100573 struct drm_file *file)
Chris Wilson00731152014-05-21 12:42:56 +0100574{
Chris Wilson00731152014-05-21 12:42:56 +0100575 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300576 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800577
578 /* We manually control the domain here and pretend that it
579 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
580 */
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700581 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000582 if (copy_from_user(vaddr, user_data, args->size))
583 return -EFAULT;
Chris Wilson00731152014-05-21 12:42:56 +0100584
Chris Wilson6a2c4232014-11-04 04:51:40 -0800585 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson10466d22017-01-06 15:22:38 +0000586 i915_gem_chipset_flush(to_i915(obj->base.dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200587
Chris Wilsond59b21e2017-02-22 11:40:49 +0000588 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilson10466d22017-01-06 15:22:38 +0000589 return 0;
Chris Wilson00731152014-05-21 12:42:56 +0100590}
591
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000592void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
Chris Wilson42dcedd2012-11-15 11:32:30 +0000593{
Chris Wilsonefab6d82015-04-07 16:20:57 +0100594 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000595}
596
597void i915_gem_object_free(struct drm_i915_gem_object *obj)
598{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100599 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonefab6d82015-04-07 16:20:57 +0100600 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000601}
602
Dave Airlieff72145b2011-02-07 12:16:14 +1000603static int
604i915_gem_create(struct drm_file *file,
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000605 struct drm_i915_private *dev_priv,
Dave Airlieff72145b2011-02-07 12:16:14 +1000606 uint64_t size,
607 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700608{
Chris Wilson05394f32010-11-08 19:18:58 +0000609 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300610 int ret;
611 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700612
Dave Airlieff72145b2011-02-07 12:16:14 +1000613 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200614 if (size == 0)
615 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700616
617 /* Allocate the new object */
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000618 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100619 if (IS_ERR(obj))
620 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700621
Chris Wilson05394f32010-11-08 19:18:58 +0000622 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100623 /* drop reference from allocate - handle holds it now */
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100624 i915_gem_object_put(obj);
Daniel Vetterd861e332013-07-24 23:25:03 +0200625 if (ret)
626 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100627
Dave Airlieff72145b2011-02-07 12:16:14 +1000628 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700629 return 0;
630}
631
Dave Airlieff72145b2011-02-07 12:16:14 +1000632int
633i915_gem_dumb_create(struct drm_file *file,
634 struct drm_device *dev,
635 struct drm_mode_create_dumb *args)
636{
637 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300638 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000639 args->size = args->pitch * args->height;
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000640 return i915_gem_create(file, to_i915(dev),
Dave Airlieda6b51d2014-12-24 13:11:17 +1000641 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000642}
643
Chris Wilsone27ab732017-06-15 13:38:49 +0100644static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
645{
646 return !(obj->cache_level == I915_CACHE_NONE ||
647 obj->cache_level == I915_CACHE_WT);
648}
649
Dave Airlieff72145b2011-02-07 12:16:14 +1000650/**
651 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100652 * @dev: drm device pointer
653 * @data: ioctl data blob
654 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000655 */
656int
657i915_gem_create_ioctl(struct drm_device *dev, void *data,
658 struct drm_file *file)
659{
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000660 struct drm_i915_private *dev_priv = to_i915(dev);
Dave Airlieff72145b2011-02-07 12:16:14 +1000661 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200662
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000663 i915_gem_flush_free_objects(dev_priv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100664
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000665 return i915_gem_create(file, dev_priv,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000666 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000667}
668
Chris Wilsonef749212017-04-12 12:01:10 +0100669static inline enum fb_op_origin
670fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
671{
672 return (domain == I915_GEM_DOMAIN_GTT ?
673 obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
674}
675
676static void
677flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
678{
679 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
680
681 if (!(obj->base.write_domain & flush_domains))
682 return;
683
684 /* No actual flushing is required for the GTT write domain. Writes
685 * to it "immediately" go to main memory as far as we know, so there's
686 * no chipset flush. It also doesn't land in render cache.
687 *
688 * However, we do have to enforce the order so that all writes through
689 * the GTT land before any writes to the device, such as updates to
690 * the GATT itself.
691 *
692 * We also have to wait a bit for the writes to land from the GTT.
693 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
694 * timing. This issue has only been observed when switching quickly
695 * between GTT writes and CPU reads from inside the kernel on recent hw,
696 * and it appears to only affect discrete GTT blocks (i.e. on LLC
697 * system agents we cannot reproduce this behaviour).
698 */
699 wmb();
700
701 switch (obj->base.write_domain) {
702 case I915_GEM_DOMAIN_GTT:
Chris Wilsonc5ba5b22017-09-07 19:45:20 +0100703 if (!HAS_LLC(dev_priv)) {
Chris Wilsonb69a7842017-08-29 20:25:46 +0100704 intel_runtime_pm_get(dev_priv);
705 spin_lock_irq(&dev_priv->uncore.lock);
Chris Wilsonc5ba5b22017-09-07 19:45:20 +0100706 POSTING_READ_FW(RING_HEAD(dev_priv->engine[RCS]->mmio_base));
Chris Wilsonb69a7842017-08-29 20:25:46 +0100707 spin_unlock_irq(&dev_priv->uncore.lock);
708 intel_runtime_pm_put(dev_priv);
Chris Wilsonef749212017-04-12 12:01:10 +0100709 }
710
711 intel_fb_obj_flush(obj,
712 fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
713 break;
714
715 case I915_GEM_DOMAIN_CPU:
716 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
717 break;
Chris Wilsone27ab732017-06-15 13:38:49 +0100718
719 case I915_GEM_DOMAIN_RENDER:
720 if (gpu_write_needs_clflush(obj))
721 obj->cache_dirty = true;
722 break;
Chris Wilsonef749212017-04-12 12:01:10 +0100723 }
724
725 obj->base.write_domain = 0;
726}
727
Daniel Vetter8c599672011-12-14 13:57:31 +0100728static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100729__copy_to_user_swizzled(char __user *cpu_vaddr,
730 const char *gpu_vaddr, int gpu_offset,
731 int length)
732{
733 int ret, cpu_offset = 0;
734
735 while (length > 0) {
736 int cacheline_end = ALIGN(gpu_offset + 1, 64);
737 int this_length = min(cacheline_end - gpu_offset, length);
738 int swizzled_gpu_offset = gpu_offset ^ 64;
739
740 ret = __copy_to_user(cpu_vaddr + cpu_offset,
741 gpu_vaddr + swizzled_gpu_offset,
742 this_length);
743 if (ret)
744 return ret + length;
745
746 cpu_offset += this_length;
747 gpu_offset += this_length;
748 length -= this_length;
749 }
750
751 return 0;
752}
753
754static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700755__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
756 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100757 int length)
758{
759 int ret, cpu_offset = 0;
760
761 while (length > 0) {
762 int cacheline_end = ALIGN(gpu_offset + 1, 64);
763 int this_length = min(cacheline_end - gpu_offset, length);
764 int swizzled_gpu_offset = gpu_offset ^ 64;
765
766 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
767 cpu_vaddr + cpu_offset,
768 this_length);
769 if (ret)
770 return ret + length;
771
772 cpu_offset += this_length;
773 gpu_offset += this_length;
774 length -= this_length;
775 }
776
777 return 0;
778}
779
Brad Volkin4c914c02014-02-18 10:15:45 -0800780/*
781 * Pins the specified object's pages and synchronizes the object with
782 * GPU accesses. Sets needs_clflush to non-zero if the caller should
783 * flush the object from the CPU cache.
784 */
785int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
Chris Wilson43394c72016-08-18 17:16:47 +0100786 unsigned int *needs_clflush)
Brad Volkin4c914c02014-02-18 10:15:45 -0800787{
788 int ret;
789
Chris Wilsone95433c2016-10-28 13:58:27 +0100790 lockdep_assert_held(&obj->base.dev->struct_mutex);
Brad Volkin4c914c02014-02-18 10:15:45 -0800791
Chris Wilsone95433c2016-10-28 13:58:27 +0100792 *needs_clflush = 0;
Chris Wilson43394c72016-08-18 17:16:47 +0100793 if (!i915_gem_object_has_struct_page(obj))
794 return -ENODEV;
Brad Volkin4c914c02014-02-18 10:15:45 -0800795
Chris Wilsone95433c2016-10-28 13:58:27 +0100796 ret = i915_gem_object_wait(obj,
797 I915_WAIT_INTERRUPTIBLE |
798 I915_WAIT_LOCKED,
799 MAX_SCHEDULE_TIMEOUT,
800 NULL);
Chris Wilsonc13d87e2016-07-20 09:21:15 +0100801 if (ret)
802 return ret;
803
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100804 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100805 if (ret)
806 return ret;
807
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100808 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
809 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000810 ret = i915_gem_object_set_to_cpu_domain(obj, false);
811 if (ret)
812 goto err_unpin;
813 else
814 goto out;
815 }
816
Chris Wilsonef749212017-04-12 12:01:10 +0100817 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100818
Chris Wilson43394c72016-08-18 17:16:47 +0100819 /* If we're not in the cpu read domain, set ourself into the gtt
820 * read domain and manually flush cachelines (if required). This
821 * optimizes for the case when the gpu will dirty the data
822 * anyway again before the next pread happens.
823 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100824 if (!obj->cache_dirty &&
825 !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000826 *needs_clflush = CLFLUSH_BEFORE;
Brad Volkin4c914c02014-02-18 10:15:45 -0800827
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000828out:
Chris Wilson97649512016-08-18 17:16:50 +0100829 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100830 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100831
832err_unpin:
833 i915_gem_object_unpin_pages(obj);
834 return ret;
Chris Wilson43394c72016-08-18 17:16:47 +0100835}
836
837int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
838 unsigned int *needs_clflush)
839{
840 int ret;
841
Chris Wilsone95433c2016-10-28 13:58:27 +0100842 lockdep_assert_held(&obj->base.dev->struct_mutex);
843
Chris Wilson43394c72016-08-18 17:16:47 +0100844 *needs_clflush = 0;
845 if (!i915_gem_object_has_struct_page(obj))
846 return -ENODEV;
847
Chris Wilsone95433c2016-10-28 13:58:27 +0100848 ret = i915_gem_object_wait(obj,
849 I915_WAIT_INTERRUPTIBLE |
850 I915_WAIT_LOCKED |
851 I915_WAIT_ALL,
852 MAX_SCHEDULE_TIMEOUT,
853 NULL);
Chris Wilson43394c72016-08-18 17:16:47 +0100854 if (ret)
855 return ret;
856
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100857 ret = i915_gem_object_pin_pages(obj);
Chris Wilson97649512016-08-18 17:16:50 +0100858 if (ret)
859 return ret;
860
Chris Wilsonb8f55be2017-08-11 12:11:16 +0100861 if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
862 !static_cpu_has(X86_FEATURE_CLFLUSH)) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000863 ret = i915_gem_object_set_to_cpu_domain(obj, true);
864 if (ret)
865 goto err_unpin;
866 else
867 goto out;
868 }
869
Chris Wilsonef749212017-04-12 12:01:10 +0100870 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Chris Wilsona314d5c2016-08-18 17:16:48 +0100871
Chris Wilson43394c72016-08-18 17:16:47 +0100872 /* If we're not in the cpu write domain, set ourself into the
873 * gtt write domain and manually flush cachelines (as required).
874 * This optimizes for the case when the gpu will use the data
875 * right away and we therefore have to clflush anyway.
876 */
Chris Wilsone27ab732017-06-15 13:38:49 +0100877 if (!obj->cache_dirty) {
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000878 *needs_clflush |= CLFLUSH_AFTER;
Chris Wilson43394c72016-08-18 17:16:47 +0100879
Chris Wilsone27ab732017-06-15 13:38:49 +0100880 /*
881 * Same trick applies to invalidate partially written
882 * cachelines read before writing.
883 */
884 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
885 *needs_clflush |= CLFLUSH_BEFORE;
886 }
Chris Wilson43394c72016-08-18 17:16:47 +0100887
Chris Wilson7f5f95d2017-03-10 00:09:42 +0000888out:
Chris Wilson43394c72016-08-18 17:16:47 +0100889 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100890 obj->mm.dirty = true;
Chris Wilson97649512016-08-18 17:16:50 +0100891 /* return with the pages pinned */
Chris Wilson43394c72016-08-18 17:16:47 +0100892 return 0;
Chris Wilson97649512016-08-18 17:16:50 +0100893
894err_unpin:
895 i915_gem_object_unpin_pages(obj);
896 return ret;
Brad Volkin4c914c02014-02-18 10:15:45 -0800897}
898
Daniel Vetter23c18c72012-03-25 19:47:42 +0200899static void
900shmem_clflush_swizzled_range(char *addr, unsigned long length,
901 bool swizzled)
902{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200903 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200904 unsigned long start = (unsigned long) addr;
905 unsigned long end = (unsigned long) addr + length;
906
907 /* For swizzling simply ensure that we always flush both
908 * channels. Lame, but simple and it works. Swizzled
909 * pwrite/pread is far from a hotpath - current userspace
910 * doesn't use it at all. */
911 start = round_down(start, 128);
912 end = round_up(end, 128);
913
914 drm_clflush_virt_range((void *)start, end - start);
915 } else {
916 drm_clflush_virt_range(addr, length);
917 }
918
919}
920
Daniel Vetterd174bd62012-03-25 19:47:40 +0200921/* Only difference to the fast-path function is that this can handle bit17
922 * and uses non-atomic copy and kmap functions. */
923static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100924shmem_pread_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +0200925 char __user *user_data,
926 bool page_do_bit17_swizzling, bool needs_clflush)
927{
928 char *vaddr;
929 int ret;
930
931 vaddr = kmap(page);
932 if (needs_clflush)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100933 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +0200934 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200935
936 if (page_do_bit17_swizzling)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100937 ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200938 else
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100939 ret = __copy_to_user(user_data, vaddr + offset, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200940 kunmap(page);
941
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100942 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200943}
944
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100945static int
946shmem_pread(struct page *page, int offset, int length, char __user *user_data,
947 bool page_do_bit17_swizzling, bool needs_clflush)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +0530948{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +0100949 int ret;
950
951 ret = -ENODEV;
952 if (!page_do_bit17_swizzling) {
953 char *vaddr = kmap_atomic(page);
954
955 if (needs_clflush)
956 drm_clflush_virt_range(vaddr + offset, length);
957 ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
958 kunmap_atomic(vaddr);
959 }
960 if (ret == 0)
961 return 0;
962
963 return shmem_pread_slow(page, offset, length, user_data,
964 page_do_bit17_swizzling, needs_clflush);
965}
966
967static int
968i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
969 struct drm_i915_gem_pread *args)
970{
971 char __user *user_data;
972 u64 remain;
973 unsigned int obj_do_bit17_swizzling;
974 unsigned int needs_clflush;
975 unsigned int idx, offset;
976 int ret;
977
978 obj_do_bit17_swizzling = 0;
979 if (i915_gem_object_needs_bit17_swizzle(obj))
980 obj_do_bit17_swizzling = BIT(17);
981
982 ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
983 if (ret)
984 return ret;
985
986 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
987 mutex_unlock(&obj->base.dev->struct_mutex);
988 if (ret)
989 return ret;
990
991 remain = args->size;
992 user_data = u64_to_user_ptr(args->data_ptr);
993 offset = offset_in_page(args->offset);
994 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
995 struct page *page = i915_gem_object_get_page(obj, idx);
996 int length;
997
998 length = remain;
999 if (offset + length > PAGE_SIZE)
1000 length = PAGE_SIZE - offset;
1001
1002 ret = shmem_pread(page, offset, length, user_data,
1003 page_to_phys(page) & obj_do_bit17_swizzling,
1004 needs_clflush);
1005 if (ret)
1006 break;
1007
1008 remain -= length;
1009 user_data += length;
1010 offset = 0;
1011 }
1012
1013 i915_gem_obj_finish_shmem_access(obj);
1014 return ret;
1015}
1016
1017static inline bool
1018gtt_user_read(struct io_mapping *mapping,
1019 loff_t base, int offset,
1020 char __user *user_data, int length)
1021{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001022 void __iomem *vaddr;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001023 unsigned long unwritten;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301024
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301025 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001026 vaddr = io_mapping_map_atomic_wc(mapping, base);
1027 unwritten = __copy_to_user_inatomic(user_data,
1028 (void __force *)vaddr + offset,
1029 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001030 io_mapping_unmap_atomic(vaddr);
1031 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001032 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1033 unwritten = copy_to_user(user_data,
1034 (void __force *)vaddr + offset,
1035 length);
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001036 io_mapping_unmap(vaddr);
1037 }
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301038 return unwritten;
1039}
1040
1041static int
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001042i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
1043 const struct drm_i915_gem_pread *args)
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301044{
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001045 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1046 struct i915_ggtt *ggtt = &i915->ggtt;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301047 struct drm_mm_node node;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001048 struct i915_vma *vma;
1049 void __user *user_data;
1050 u64 remain, offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301051 int ret;
1052
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001053 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1054 if (ret)
1055 return ret;
1056
1057 intel_runtime_pm_get(i915);
1058 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001059 PIN_MAPPABLE |
1060 PIN_NONFAULT |
1061 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001062 if (!IS_ERR(vma)) {
1063 node.start = i915_ggtt_offset(vma);
1064 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001065 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001066 if (ret) {
1067 i915_vma_unpin(vma);
1068 vma = ERR_PTR(ret);
1069 }
1070 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001071 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001072 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301073 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001074 goto out_unlock;
1075 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301076 }
1077
1078 ret = i915_gem_object_set_to_gtt_domain(obj, false);
1079 if (ret)
1080 goto out_unpin;
1081
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001082 mutex_unlock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301083
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001084 user_data = u64_to_user_ptr(args->data_ptr);
1085 remain = args->size;
1086 offset = args->offset;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301087
1088 while (remain > 0) {
1089 /* Operation in this page
1090 *
1091 * page_base = page offset within aperture
1092 * page_offset = offset within page
1093 * page_length = bytes to copy for this page
1094 */
1095 u32 page_base = node.start;
1096 unsigned page_offset = offset_in_page(offset);
1097 unsigned page_length = PAGE_SIZE - page_offset;
1098 page_length = remain < page_length ? remain : page_length;
1099 if (node.allocated) {
1100 wmb();
1101 ggtt->base.insert_page(&ggtt->base,
1102 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001103 node.start, I915_CACHE_NONE, 0);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301104 wmb();
1105 } else {
1106 page_base += offset & PAGE_MASK;
1107 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001108
1109 if (gtt_user_read(&ggtt->mappable, page_base, page_offset,
1110 user_data, page_length)) {
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301111 ret = -EFAULT;
1112 break;
1113 }
1114
1115 remain -= page_length;
1116 user_data += page_length;
1117 offset += page_length;
1118 }
1119
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001120 mutex_lock(&i915->drm.struct_mutex);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301121out_unpin:
1122 if (node.allocated) {
1123 wmb();
1124 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001125 node.start, node.size);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301126 remove_mappable_node(&node);
1127 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001128 i915_vma_unpin(vma);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301129 }
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001130out_unlock:
1131 intel_runtime_pm_put(i915);
1132 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +01001133
Eric Anholteb014592009-03-10 11:44:52 -07001134 return ret;
1135}
1136
Eric Anholt673a3942008-07-30 12:06:12 -07001137/**
1138 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001139 * @dev: drm device pointer
1140 * @data: ioctl data blob
1141 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -07001142 *
1143 * On error, the contents of *data are undefined.
1144 */
1145int
1146i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001147 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001148{
1149 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001150 struct drm_i915_gem_object *obj;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001151 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001152
Chris Wilson51311d02010-11-17 09:10:42 +00001153 if (args->size == 0)
1154 return 0;
1155
1156 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001157 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001158 args->size))
1159 return -EFAULT;
1160
Chris Wilson03ac0642016-07-20 13:31:51 +01001161 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001162 if (!obj)
1163 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001164
Chris Wilson7dcd2492010-09-26 20:21:44 +01001165 /* Bounds check source. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001166 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001167 ret = -EINVAL;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001168 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001169 }
1170
Chris Wilsondb53a302011-02-03 11:57:46 +00001171 trace_i915_gem_object_pread(obj, args->offset, args->size);
1172
Chris Wilsone95433c2016-10-28 13:58:27 +01001173 ret = i915_gem_object_wait(obj,
1174 I915_WAIT_INTERRUPTIBLE,
1175 MAX_SCHEDULE_TIMEOUT,
1176 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001177 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001178 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001179
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001180 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001181 if (ret)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001182 goto out;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001183
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001184 ret = i915_gem_shmem_pread(obj, args);
Chris Wilson9c870d02016-10-24 13:42:15 +01001185 if (ret == -EFAULT || ret == -ENODEV)
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001186 ret = i915_gem_gtt_pread(obj, args);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301187
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001188 i915_gem_object_unpin_pages(obj);
1189out:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001190 i915_gem_object_put(obj);
Eric Anholteb014592009-03-10 11:44:52 -07001191 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001192}
1193
Keith Packard0839ccb2008-10-30 19:38:48 -07001194/* This is the fast write path which cannot handle
1195 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001196 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -07001197
Chris Wilsonfe115622016-10-28 13:58:40 +01001198static inline bool
1199ggtt_write(struct io_mapping *mapping,
1200 loff_t base, int offset,
1201 char __user *user_data, int length)
Keith Packard0839ccb2008-10-30 19:38:48 -07001202{
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001203 void __iomem *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -07001204 unsigned long unwritten;
1205
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -07001206 /* We can use the cpu mem copy function because this is X86. */
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001207 vaddr = io_mapping_map_atomic_wc(mapping, base);
1208 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
Keith Packard0839ccb2008-10-30 19:38:48 -07001209 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001210 io_mapping_unmap_atomic(vaddr);
1211 if (unwritten) {
Ville Syrjäläafe722b2017-09-01 20:12:52 +03001212 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
1213 unwritten = copy_from_user((void __force *)vaddr + offset,
1214 user_data, length);
Chris Wilsonfe115622016-10-28 13:58:40 +01001215 io_mapping_unmap(vaddr);
1216 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001217
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001218 return unwritten;
1219}
1220
Eric Anholt3de09aa2009-03-09 09:42:23 -07001221/**
1222 * This is the fast pwrite path, where we copy the data directly from the
1223 * user into the GTT, uncached.
Chris Wilsonfe115622016-10-28 13:58:40 +01001224 * @obj: i915 GEM object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001225 * @args: pwrite arguments structure
Eric Anholt3de09aa2009-03-09 09:42:23 -07001226 */
Eric Anholt673a3942008-07-30 12:06:12 -07001227static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001228i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
1229 const struct drm_i915_gem_pwrite *args)
Eric Anholt673a3942008-07-30 12:06:12 -07001230{
Chris Wilsonfe115622016-10-28 13:58:40 +01001231 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301232 struct i915_ggtt *ggtt = &i915->ggtt;
1233 struct drm_mm_node node;
Chris Wilsonfe115622016-10-28 13:58:40 +01001234 struct i915_vma *vma;
1235 u64 remain, offset;
1236 void __user *user_data;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301237 int ret;
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301238
Chris Wilsonfe115622016-10-28 13:58:40 +01001239 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
1240 if (ret)
1241 return ret;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001242
Chris Wilson9c870d02016-10-24 13:42:15 +01001243 intel_runtime_pm_get(i915);
Chris Wilson058d88c2016-08-15 10:49:06 +01001244 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
Chris Wilsona3259ca2017-10-09 09:44:00 +01001245 PIN_MAPPABLE |
1246 PIN_NONFAULT |
1247 PIN_NONBLOCK);
Chris Wilson18034582016-08-18 17:16:45 +01001248 if (!IS_ERR(vma)) {
1249 node.start = i915_ggtt_offset(vma);
1250 node.allocated = false;
Chris Wilson49ef5292016-08-18 17:17:00 +01001251 ret = i915_vma_put_fence(vma);
Chris Wilson18034582016-08-18 17:16:45 +01001252 if (ret) {
1253 i915_vma_unpin(vma);
1254 vma = ERR_PTR(ret);
1255 }
1256 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001257 if (IS_ERR(vma)) {
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001258 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301259 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001260 goto out_unlock;
1261 GEM_BUG_ON(!node.allocated);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301262 }
Daniel Vetter935aaa62012-03-25 19:47:35 +02001263
1264 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1265 if (ret)
1266 goto out_unpin;
1267
Chris Wilsonfe115622016-10-28 13:58:40 +01001268 mutex_unlock(&i915->drm.struct_mutex);
1269
Chris Wilsonb19482d2016-08-18 17:16:43 +01001270 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001271
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301272 user_data = u64_to_user_ptr(args->data_ptr);
1273 offset = args->offset;
1274 remain = args->size;
1275 while (remain) {
Eric Anholt673a3942008-07-30 12:06:12 -07001276 /* Operation in this page
1277 *
Keith Packard0839ccb2008-10-30 19:38:48 -07001278 * page_base = page offset within aperture
1279 * page_offset = offset within page
1280 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -07001281 */
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301282 u32 page_base = node.start;
Chris Wilsonbb6dc8d2016-10-28 13:58:39 +01001283 unsigned int page_offset = offset_in_page(offset);
1284 unsigned int page_length = PAGE_SIZE - page_offset;
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301285 page_length = remain < page_length ? remain : page_length;
1286 if (node.allocated) {
1287 wmb(); /* flush the write before we modify the GGTT */
1288 ggtt->base.insert_page(&ggtt->base,
1289 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
1290 node.start, I915_CACHE_NONE, 0);
1291 wmb(); /* flush modifications to the GGTT (insert_page) */
1292 } else {
1293 page_base += offset & PAGE_MASK;
1294 }
Keith Packard0839ccb2008-10-30 19:38:48 -07001295 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -07001296 * source page isn't available. Return the error and we'll
1297 * retry in the slow path.
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301298 * If the object is non-shmem backed, we retry again with the
1299 * path that handles page fault.
Keith Packard0839ccb2008-10-30 19:38:48 -07001300 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001301 if (ggtt_write(&ggtt->mappable, page_base, page_offset,
1302 user_data, page_length)) {
1303 ret = -EFAULT;
1304 break;
Daniel Vetter935aaa62012-03-25 19:47:35 +02001305 }
Eric Anholt673a3942008-07-30 12:06:12 -07001306
Keith Packard0839ccb2008-10-30 19:38:48 -07001307 remain -= page_length;
1308 user_data += page_length;
1309 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -07001310 }
Chris Wilsond59b21e2017-02-22 11:40:49 +00001311 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001312
1313 mutex_lock(&i915->drm.struct_mutex);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001314out_unpin:
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301315 if (node.allocated) {
1316 wmb();
1317 ggtt->base.clear_range(&ggtt->base,
Michał Winiarski4fb84d92016-10-13 14:02:40 +02001318 node.start, node.size);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301319 remove_mappable_node(&node);
1320 } else {
Chris Wilson058d88c2016-08-15 10:49:06 +01001321 i915_vma_unpin(vma);
Ankitprasad Sharma4f1959e2016-06-10 14:23:01 +05301322 }
Chris Wilsonfe115622016-10-28 13:58:40 +01001323out_unlock:
Chris Wilson9c870d02016-10-24 13:42:15 +01001324 intel_runtime_pm_put(i915);
Chris Wilsonfe115622016-10-28 13:58:40 +01001325 mutex_unlock(&i915->drm.struct_mutex);
Eric Anholt3de09aa2009-03-09 09:42:23 -07001326 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001327}
1328
Eric Anholt673a3942008-07-30 12:06:12 -07001329static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001330shmem_pwrite_slow(struct page *page, int offset, int length,
Daniel Vetterd174bd62012-03-25 19:47:40 +02001331 char __user *user_data,
1332 bool page_do_bit17_swizzling,
1333 bool needs_clflush_before,
1334 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -07001335{
Daniel Vetterd174bd62012-03-25 19:47:40 +02001336 char *vaddr;
1337 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001338
Daniel Vetterd174bd62012-03-25 19:47:40 +02001339 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +02001340 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Chris Wilsonfe115622016-10-28 13:58:40 +01001341 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001342 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001343 if (page_do_bit17_swizzling)
Chris Wilsonfe115622016-10-28 13:58:40 +01001344 ret = __copy_from_user_swizzled(vaddr, offset, user_data,
1345 length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001346 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001347 ret = __copy_from_user(vaddr + offset, user_data, length);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001348 if (needs_clflush_after)
Chris Wilsonfe115622016-10-28 13:58:40 +01001349 shmem_clflush_swizzled_range(vaddr + offset, length,
Daniel Vetter23c18c72012-03-25 19:47:42 +02001350 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +02001351 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +01001352
Chris Wilson755d2212012-09-04 21:02:55 +01001353 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001354}
1355
Chris Wilsonfe115622016-10-28 13:58:40 +01001356/* Per-page copy function for the shmem pwrite fastpath.
1357 * Flushes invalid cachelines before writing to the target if
1358 * needs_clflush_before is set and flushes out any written cachelines after
1359 * writing if needs_clflush is set.
1360 */
Eric Anholt40123c12009-03-09 13:42:30 -07001361static int
Chris Wilsonfe115622016-10-28 13:58:40 +01001362shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
1363 bool page_do_bit17_swizzling,
1364 bool needs_clflush_before,
1365 bool needs_clflush_after)
Eric Anholt40123c12009-03-09 13:42:30 -07001366{
Chris Wilsonfe115622016-10-28 13:58:40 +01001367 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001368
Chris Wilsonfe115622016-10-28 13:58:40 +01001369 ret = -ENODEV;
1370 if (!page_do_bit17_swizzling) {
1371 char *vaddr = kmap_atomic(page);
1372
1373 if (needs_clflush_before)
1374 drm_clflush_virt_range(vaddr + offset, len);
1375 ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
1376 if (needs_clflush_after)
1377 drm_clflush_virt_range(vaddr + offset, len);
1378
1379 kunmap_atomic(vaddr);
1380 }
1381 if (ret == 0)
1382 return ret;
1383
1384 return shmem_pwrite_slow(page, offset, len, user_data,
1385 page_do_bit17_swizzling,
1386 needs_clflush_before,
1387 needs_clflush_after);
1388}
1389
1390static int
1391i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
1392 const struct drm_i915_gem_pwrite *args)
1393{
1394 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1395 void __user *user_data;
1396 u64 remain;
1397 unsigned int obj_do_bit17_swizzling;
1398 unsigned int partial_cacheline_write;
1399 unsigned int needs_clflush;
1400 unsigned int offset, idx;
1401 int ret;
1402
1403 ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
Chris Wilson43394c72016-08-18 17:16:47 +01001404 if (ret)
1405 return ret;
Eric Anholt40123c12009-03-09 13:42:30 -07001406
Chris Wilsonfe115622016-10-28 13:58:40 +01001407 ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
1408 mutex_unlock(&i915->drm.struct_mutex);
1409 if (ret)
1410 return ret;
1411
1412 obj_do_bit17_swizzling = 0;
1413 if (i915_gem_object_needs_bit17_swizzle(obj))
1414 obj_do_bit17_swizzling = BIT(17);
1415
1416 /* If we don't overwrite a cacheline completely we need to be
1417 * careful to have up-to-date data by first clflushing. Don't
1418 * overcomplicate things and flush the entire patch.
1419 */
1420 partial_cacheline_write = 0;
1421 if (needs_clflush & CLFLUSH_BEFORE)
1422 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
1423
Chris Wilson43394c72016-08-18 17:16:47 +01001424 user_data = u64_to_user_ptr(args->data_ptr);
Chris Wilson43394c72016-08-18 17:16:47 +01001425 remain = args->size;
Chris Wilsonfe115622016-10-28 13:58:40 +01001426 offset = offset_in_page(args->offset);
1427 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
1428 struct page *page = i915_gem_object_get_page(obj, idx);
1429 int length;
Eric Anholt40123c12009-03-09 13:42:30 -07001430
Chris Wilsonfe115622016-10-28 13:58:40 +01001431 length = remain;
1432 if (offset + length > PAGE_SIZE)
1433 length = PAGE_SIZE - offset;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001434
Chris Wilsonfe115622016-10-28 13:58:40 +01001435 ret = shmem_pwrite(page, offset, length, user_data,
1436 page_to_phys(page) & obj_do_bit17_swizzling,
1437 (offset | length) & partial_cacheline_write,
1438 needs_clflush & CLFLUSH_AFTER);
1439 if (ret)
Chris Wilson9da3da62012-06-01 15:20:22 +01001440 break;
1441
Chris Wilsonfe115622016-10-28 13:58:40 +01001442 remain -= length;
1443 user_data += length;
1444 offset = 0;
Eric Anholt40123c12009-03-09 13:42:30 -07001445 }
1446
Chris Wilsond59b21e2017-02-22 11:40:49 +00001447 intel_fb_obj_flush(obj, ORIGIN_CPU);
Chris Wilsonfe115622016-10-28 13:58:40 +01001448 i915_gem_obj_finish_shmem_access(obj);
Eric Anholt40123c12009-03-09 13:42:30 -07001449 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001450}
1451
1452/**
1453 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001454 * @dev: drm device
1455 * @data: ioctl data blob
1456 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001457 *
1458 * On error, the contents of the buffer that were to be modified are undefined.
1459 */
1460int
1461i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001462 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001463{
1464 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001465 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001466 int ret;
1467
1468 if (args->size == 0)
1469 return 0;
1470
1471 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001472 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001473 args->size))
1474 return -EFAULT;
1475
Chris Wilson03ac0642016-07-20 13:31:51 +01001476 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001477 if (!obj)
1478 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001479
Chris Wilson7dcd2492010-09-26 20:21:44 +01001480 /* Bounds check destination. */
Matthew Auld966d5bf2016-12-13 20:32:22 +00001481 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001482 ret = -EINVAL;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001483 goto err;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001484 }
1485
Chris Wilsondb53a302011-02-03 11:57:46 +00001486 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1487
Chris Wilson7c55e2c2017-03-07 12:03:38 +00001488 ret = -ENODEV;
1489 if (obj->ops->pwrite)
1490 ret = obj->ops->pwrite(obj, args);
1491 if (ret != -ENODEV)
1492 goto err;
1493
Chris Wilsone95433c2016-10-28 13:58:27 +01001494 ret = i915_gem_object_wait(obj,
1495 I915_WAIT_INTERRUPTIBLE |
1496 I915_WAIT_ALL,
1497 MAX_SCHEDULE_TIMEOUT,
1498 to_rps_client(file));
Chris Wilson258a5ed2016-08-05 10:14:16 +01001499 if (ret)
1500 goto err;
1501
Chris Wilsonfe115622016-10-28 13:58:40 +01001502 ret = i915_gem_object_pin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001503 if (ret)
Chris Wilsonfe115622016-10-28 13:58:40 +01001504 goto err;
Chris Wilson258a5ed2016-08-05 10:14:16 +01001505
Daniel Vetter935aaa62012-03-25 19:47:35 +02001506 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001507 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1508 * it would end up going through the fenced access, and we'll get
1509 * different detiling behavior between reading and writing.
1510 * pread/pwrite currently are reading and writing from the CPU
1511 * perspective, requiring manual detiling by the client.
1512 */
Chris Wilson6eae0052016-06-20 15:05:52 +01001513 if (!i915_gem_object_has_struct_page(obj) ||
Chris Wilson9c870d02016-10-24 13:42:15 +01001514 cpu_write_needs_clflush(obj))
Daniel Vetter935aaa62012-03-25 19:47:35 +02001515 /* Note that the gtt paths might fail with non-page-backed user
1516 * pointers (e.g. gtt mappings when moving data between
Chris Wilson9c870d02016-10-24 13:42:15 +01001517 * textures). Fallback to the shmem path in that case.
1518 */
Chris Wilsonfe115622016-10-28 13:58:40 +01001519 ret = i915_gem_gtt_pwrite_fast(obj, args);
Eric Anholt673a3942008-07-30 12:06:12 -07001520
Chris Wilsond1054ee2016-07-16 18:42:36 +01001521 if (ret == -EFAULT || ret == -ENOSPC) {
Chris Wilson6a2c4232014-11-04 04:51:40 -08001522 if (obj->phys_handle)
1523 ret = i915_gem_phys_pwrite(obj, args, file);
Ankitprasad Sharmab50a5372016-06-10 14:23:03 +05301524 else
Chris Wilsonfe115622016-10-28 13:58:40 +01001525 ret = i915_gem_shmem_pwrite(obj, args);
Chris Wilson6a2c4232014-11-04 04:51:40 -08001526 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001527
Chris Wilsonfe115622016-10-28 13:58:40 +01001528 i915_gem_object_unpin_pages(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001529err:
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001530 i915_gem_object_put(obj);
Chris Wilson258a5ed2016-08-05 10:14:16 +01001531 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001532}
1533
Chris Wilson40e62d52016-10-28 13:58:41 +01001534static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
1535{
1536 struct drm_i915_private *i915;
1537 struct list_head *list;
1538 struct i915_vma *vma;
1539
1540 list_for_each_entry(vma, &obj->vma_list, obj_link) {
1541 if (!i915_vma_is_ggtt(vma))
Chris Wilson28f412e2016-12-23 14:57:55 +00001542 break;
Chris Wilson40e62d52016-10-28 13:58:41 +01001543
1544 if (i915_vma_is_active(vma))
1545 continue;
1546
1547 if (!drm_mm_node_allocated(&vma->node))
1548 continue;
1549
1550 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
1551 }
1552
1553 i915 = to_i915(obj->base.dev);
1554 list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
Joonas Lahtinen56cea322016-11-02 12:16:04 +02001555 list_move_tail(&obj->global_link, list);
Chris Wilson40e62d52016-10-28 13:58:41 +01001556}
1557
Eric Anholt673a3942008-07-30 12:06:12 -07001558/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001559 * Called when user space prepares to use an object with the CPU, either
1560 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001561 * @dev: drm device
1562 * @data: ioctl data blob
1563 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001564 */
1565int
1566i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001567 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001568{
1569 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001570 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001571 uint32_t read_domains = args->read_domains;
1572 uint32_t write_domain = args->write_domain;
Chris Wilson40e62d52016-10-28 13:58:41 +01001573 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07001574
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001575 /* Only handle setting domains to types used by the CPU. */
Chris Wilsonb8f90962016-08-05 10:14:07 +01001576 if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001577 return -EINVAL;
1578
1579 /* Having something in the write domain implies it's in the read
1580 * domain, and only that read domain. Enforce that in the request.
1581 */
1582 if (write_domain != 0 && read_domains != write_domain)
1583 return -EINVAL;
1584
Chris Wilson03ac0642016-07-20 13:31:51 +01001585 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001586 if (!obj)
1587 return -ENOENT;
Jesse Barnes652c3932009-08-17 13:31:43 -07001588
Chris Wilson3236f572012-08-24 09:35:09 +01001589 /* Try to flush the object off the GPU without holding the lock.
1590 * We will repeat the flush holding the lock in the normal manner
1591 * to catch cases where we are gazumped.
1592 */
Chris Wilson40e62d52016-10-28 13:58:41 +01001593 err = i915_gem_object_wait(obj,
Chris Wilsone95433c2016-10-28 13:58:27 +01001594 I915_WAIT_INTERRUPTIBLE |
1595 (write_domain ? I915_WAIT_ALL : 0),
1596 MAX_SCHEDULE_TIMEOUT,
1597 to_rps_client(file));
Chris Wilson40e62d52016-10-28 13:58:41 +01001598 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001599 goto out;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001600
Chris Wilson40e62d52016-10-28 13:58:41 +01001601 /* Flush and acquire obj->pages so that we are coherent through
1602 * direct access in memory with previous cached writes through
1603 * shmemfs and that our cache domain tracking remains valid.
1604 * For example, if the obj->filp was moved to swap without us
1605 * being notified and releasing the pages, we would mistakenly
1606 * continue to assume that the obj remained out of the CPU cached
1607 * domain.
1608 */
1609 err = i915_gem_object_pin_pages(obj);
1610 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001611 goto out;
Chris Wilson40e62d52016-10-28 13:58:41 +01001612
1613 err = i915_mutex_lock_interruptible(dev);
1614 if (err)
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001615 goto out_unpin;
Chris Wilson3236f572012-08-24 09:35:09 +01001616
Chris Wilsone22d8e32017-04-12 12:01:11 +01001617 if (read_domains & I915_GEM_DOMAIN_WC)
1618 err = i915_gem_object_set_to_wc_domain(obj, write_domain);
1619 else if (read_domains & I915_GEM_DOMAIN_GTT)
1620 err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
Chris Wilson43566de2015-01-02 16:29:29 +05301621 else
Chris Wilsone22d8e32017-04-12 12:01:11 +01001622 err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
Chris Wilson40e62d52016-10-28 13:58:41 +01001623
1624 /* And bump the LRU for this access */
1625 i915_gem_object_bump_inactive_ggtt(obj);
1626
1627 mutex_unlock(&dev->struct_mutex);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001628
Daniel Vetter031b6982015-06-26 19:35:16 +02001629 if (write_domain != 0)
Chris Wilsonef749212017-04-12 12:01:10 +01001630 intel_fb_obj_invalidate(obj,
1631 fb_write_origin(obj, write_domain));
Daniel Vetter031b6982015-06-26 19:35:16 +02001632
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001633out_unpin:
Chris Wilson40e62d52016-10-28 13:58:41 +01001634 i915_gem_object_unpin_pages(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001635out:
1636 i915_gem_object_put(obj);
Chris Wilson40e62d52016-10-28 13:58:41 +01001637 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07001638}
1639
1640/**
1641 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001642 * @dev: drm device
1643 * @data: ioctl data blob
1644 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001645 */
1646int
1647i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001648 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001649{
1650 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001651 struct drm_i915_gem_object *obj;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001652
Chris Wilson03ac0642016-07-20 13:31:51 +01001653 obj = i915_gem_object_lookup(file, args->handle);
Chris Wilsonc21724c2016-08-05 10:14:19 +01001654 if (!obj)
1655 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001656
Eric Anholt673a3942008-07-30 12:06:12 -07001657 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001658 i915_gem_object_flush_if_display(obj);
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001659 i915_gem_object_put(obj);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00001660
1661 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001662}
1663
1664/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001665 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1666 * it is mapped to.
1667 * @dev: drm device
1668 * @data: ioctl data blob
1669 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001670 *
1671 * While the mapping holds a reference on the contents of the object, it doesn't
1672 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001673 *
1674 * IMPORTANT:
1675 *
1676 * DRM driver writers who look a this function as an example for how to do GEM
1677 * mmap support, please don't implement mmap support like here. The modern way
1678 * to implement DRM mmap support is with an mmap offset ioctl (like
1679 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1680 * That way debug tooling like valgrind will understand what's going on, hiding
1681 * the mmap call in a driver private ioctl will break that. The i915 driver only
1682 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001683 */
1684int
1685i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001686 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001687{
1688 struct drm_i915_gem_mmap *args = data;
Chris Wilson03ac0642016-07-20 13:31:51 +01001689 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001690 unsigned long addr;
1691
Akash Goel1816f922015-01-02 16:29:30 +05301692 if (args->flags & ~(I915_MMAP_WC))
1693 return -EINVAL;
1694
Borislav Petkov568a58e2016-03-29 17:42:01 +02001695 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301696 return -ENODEV;
1697
Chris Wilson03ac0642016-07-20 13:31:51 +01001698 obj = i915_gem_object_lookup(file, args->handle);
1699 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001700 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001701
Daniel Vetter1286ff72012-05-10 15:25:09 +02001702 /* prime objects have no backing filp to GEM mmap
1703 * pages from.
1704 */
Chris Wilson03ac0642016-07-20 13:31:51 +01001705 if (!obj->base.filp) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001706 i915_gem_object_put(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001707 return -EINVAL;
1708 }
1709
Chris Wilson03ac0642016-07-20 13:31:51 +01001710 addr = vm_mmap(obj->base.filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001711 PROT_READ | PROT_WRITE, MAP_SHARED,
1712 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301713 if (args->flags & I915_MMAP_WC) {
1714 struct mm_struct *mm = current->mm;
1715 struct vm_area_struct *vma;
1716
Michal Hocko80a89a52016-05-23 16:26:11 -07001717 if (down_write_killable(&mm->mmap_sem)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001718 i915_gem_object_put(obj);
Michal Hocko80a89a52016-05-23 16:26:11 -07001719 return -EINTR;
1720 }
Akash Goel1816f922015-01-02 16:29:30 +05301721 vma = find_vma(mm, addr);
1722 if (vma)
1723 vma->vm_page_prot =
1724 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1725 else
1726 addr = -ENOMEM;
1727 up_write(&mm->mmap_sem);
Chris Wilsonaeecc962016-06-17 14:46:39 -03001728
1729 /* This may race, but that's ok, it only gets set */
Chris Wilson50349242016-08-18 17:17:04 +01001730 WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
Akash Goel1816f922015-01-02 16:29:30 +05301731 }
Chris Wilsonf0cd5182016-10-28 13:58:43 +01001732 i915_gem_object_put(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001733 if (IS_ERR((void *)addr))
1734 return addr;
1735
1736 args->addr_ptr = (uint64_t) addr;
1737
1738 return 0;
1739}
1740
Chris Wilson03af84f2016-08-18 17:17:01 +01001741static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
1742{
Chris Wilson6649a0b2017-01-09 16:16:08 +00001743 return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
Chris Wilson03af84f2016-08-18 17:17:01 +01001744}
1745
Jesse Barnesde151cf2008-11-12 10:03:55 -08001746/**
Chris Wilson4cc69072016-08-25 19:05:19 +01001747 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1748 *
1749 * A history of the GTT mmap interface:
1750 *
1751 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1752 * aligned and suitable for fencing, and still fit into the available
1753 * mappable space left by the pinned display objects. A classic problem
1754 * we called the page-fault-of-doom where we would ping-pong between
1755 * two objects that could not fit inside the GTT and so the memcpy
1756 * would page one object in at the expense of the other between every
1757 * single byte.
1758 *
1759 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1760 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1761 * object is too large for the available space (or simply too large
1762 * for the mappable aperture!), a view is created instead and faulted
1763 * into userspace. (This view is aligned and sized appropriately for
1764 * fenced access.)
1765 *
Chris Wilsone22d8e32017-04-12 12:01:11 +01001766 * 2 - Recognise WC as a separate cache domain so that we can flush the
1767 * delayed writes via GTT before performing direct access via WC.
1768 *
Chris Wilson4cc69072016-08-25 19:05:19 +01001769 * Restrictions:
1770 *
1771 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1772 * hangs on some architectures, corruption on others. An attempt to service
1773 * a GTT page fault from a snoopable object will generate a SIGBUS.
1774 *
1775 * * the object must be able to fit into RAM (physical memory, though no
1776 * limited to the mappable aperture).
1777 *
1778 *
1779 * Caveats:
1780 *
1781 * * a new GTT page fault will synchronize rendering from the GPU and flush
1782 * all data to system memory. Subsequent access will not be synchronized.
1783 *
1784 * * all mappings are revoked on runtime device suspend.
1785 *
1786 * * there are only 8, 16 or 32 fence registers to share between all users
1787 * (older machines require fence register for display and blitter access
1788 * as well). Contention of the fence registers will cause the previous users
1789 * to be unmapped and any new access will generate new page faults.
1790 *
1791 * * running out of memory while servicing a fault may generate a SIGBUS,
1792 * rather than the expected SIGSEGV.
1793 */
1794int i915_gem_mmap_gtt_version(void)
1795{
Chris Wilsone22d8e32017-04-12 12:01:11 +01001796 return 2;
Chris Wilson4cc69072016-08-25 19:05:19 +01001797}
1798
Chris Wilson2d4281b2017-01-10 09:56:32 +00001799static inline struct i915_ggtt_view
1800compute_partial_view(struct drm_i915_gem_object *obj,
Chris Wilson2d4281b2017-01-10 09:56:32 +00001801 pgoff_t page_offset,
1802 unsigned int chunk)
1803{
1804 struct i915_ggtt_view view;
1805
1806 if (i915_gem_object_is_tiled(obj))
1807 chunk = roundup(chunk, tile_row_pages(obj));
1808
Chris Wilson2d4281b2017-01-10 09:56:32 +00001809 view.type = I915_GGTT_VIEW_PARTIAL;
Chris Wilson8bab11932017-01-14 00:28:25 +00001810 view.partial.offset = rounddown(page_offset, chunk);
1811 view.partial.size =
Chris Wilson2d4281b2017-01-10 09:56:32 +00001812 min_t(unsigned int, chunk,
Chris Wilson8bab11932017-01-14 00:28:25 +00001813 (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
Chris Wilson2d4281b2017-01-10 09:56:32 +00001814
1815 /* If the partial covers the entire object, just create a normal VMA. */
1816 if (chunk >= obj->base.size >> PAGE_SHIFT)
1817 view.type = I915_GGTT_VIEW_NORMAL;
1818
1819 return view;
1820}
1821
Chris Wilson4cc69072016-08-25 19:05:19 +01001822/**
Jesse Barnesde151cf2008-11-12 10:03:55 -08001823 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001824 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001825 *
1826 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1827 * from userspace. The fault handler takes care of binding the object to
1828 * the GTT (if needed), allocating and programming a fence register (again,
1829 * only if needed based on whether the old reg is still valid or the object
1830 * is tiled) and inserting a new PTE into the faulting process.
1831 *
1832 * Note that the faulting process may involve evicting existing objects
1833 * from the GTT and/or fence registers to make room. So performance may
1834 * suffer if the GTT working set is large or there are few fence registers
1835 * left.
Chris Wilson4cc69072016-08-25 19:05:19 +01001836 *
1837 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1838 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
Jesse Barnesde151cf2008-11-12 10:03:55 -08001839 */
Dave Jiang11bac802017-02-24 14:56:41 -08001840int i915_gem_fault(struct vm_fault *vmf)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001841{
Chris Wilson03af84f2016-08-18 17:17:01 +01001842#define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
Dave Jiang11bac802017-02-24 14:56:41 -08001843 struct vm_area_struct *area = vmf->vma;
Chris Wilson058d88c2016-08-15 10:49:06 +01001844 struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
Chris Wilson05394f32010-11-08 19:18:58 +00001845 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001846 struct drm_i915_private *dev_priv = to_i915(dev);
1847 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001848 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Chris Wilson058d88c2016-08-15 10:49:06 +01001849 struct i915_vma *vma;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001850 pgoff_t page_offset;
Chris Wilson82118872016-08-18 17:17:05 +01001851 unsigned int flags;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001852 int ret;
Paulo Zanonif65c9162013-11-27 18:20:34 -02001853
Jesse Barnesde151cf2008-11-12 10:03:55 -08001854 /* We don't use vmf->pgoff since that has the fake offset */
Jan Kara1a29d852016-12-14 15:07:01 -08001855 page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001856
Chris Wilsondb53a302011-02-03 11:57:46 +00001857 trace_i915_gem_object_fault(obj, page_offset, true, write);
1858
Chris Wilson6e4930f2014-02-07 18:37:06 -02001859 /* Try to flush the object off the GPU first without holding the lock.
Chris Wilsonb8f90962016-08-05 10:14:07 +01001860 * Upon acquiring the lock, we will perform our sanity checks and then
Chris Wilson6e4930f2014-02-07 18:37:06 -02001861 * repeat the flush holding the lock in the normal manner to catch cases
1862 * where we are gazumped.
1863 */
Chris Wilsone95433c2016-10-28 13:58:27 +01001864 ret = i915_gem_object_wait(obj,
1865 I915_WAIT_INTERRUPTIBLE,
1866 MAX_SCHEDULE_TIMEOUT,
1867 NULL);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001868 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001869 goto err;
1870
Chris Wilson40e62d52016-10-28 13:58:41 +01001871 ret = i915_gem_object_pin_pages(obj);
1872 if (ret)
1873 goto err;
1874
Chris Wilsonb8f90962016-08-05 10:14:07 +01001875 intel_runtime_pm_get(dev_priv);
1876
1877 ret = i915_mutex_lock_interruptible(dev);
1878 if (ret)
1879 goto err_rpm;
Chris Wilson6e4930f2014-02-07 18:37:06 -02001880
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001881 /* Access to snoopable pages through the GTT is incoherent. */
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00001882 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001883 ret = -EFAULT;
Chris Wilsonb8f90962016-08-05 10:14:07 +01001884 goto err_unlock;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001885 }
1886
Chris Wilson82118872016-08-18 17:17:05 +01001887 /* If the object is smaller than a couple of partial vma, it is
1888 * not worth only creating a single partial vma - we may as well
1889 * clear enough space for the full object.
1890 */
1891 flags = PIN_MAPPABLE;
1892 if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
1893 flags |= PIN_NONBLOCK | PIN_NONFAULT;
1894
Chris Wilsona61007a2016-08-18 17:17:02 +01001895 /* Now pin it into the GTT as needed */
Chris Wilson82118872016-08-18 17:17:05 +01001896 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
Chris Wilsona61007a2016-08-18 17:17:02 +01001897 if (IS_ERR(vma)) {
Chris Wilsona61007a2016-08-18 17:17:02 +01001898 /* Use a partial view if it is bigger than available space */
Chris Wilson2d4281b2017-01-10 09:56:32 +00001899 struct i915_ggtt_view view =
Chris Wilson8201c1f2017-01-10 09:56:33 +00001900 compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
Chris Wilsonaa136d92016-08-18 17:17:03 +01001901
Chris Wilson50349242016-08-18 17:17:04 +01001902 /* Userspace is now writing through an untracked VMA, abandon
1903 * all hope that the hardware is able to track future writes.
1904 */
1905 obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
1906
Chris Wilsona61007a2016-08-18 17:17:02 +01001907 vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
1908 }
Chris Wilson058d88c2016-08-15 10:49:06 +01001909 if (IS_ERR(vma)) {
1910 ret = PTR_ERR(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001911 goto err_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +01001912 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001913
Chris Wilsonc9839302012-11-20 10:45:17 +00001914 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1915 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001916 goto err_unpin;
Chris Wilsonc9839302012-11-20 10:45:17 +00001917
Chris Wilson3bd40732017-10-09 09:43:56 +01001918 ret = i915_vma_pin_fence(vma);
Chris Wilsonc9839302012-11-20 10:45:17 +00001919 if (ret)
Chris Wilsonb8f90962016-08-05 10:14:07 +01001920 goto err_unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001921
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001922 /* Finally, remap it using the new GTT offset */
Chris Wilsonc58305a2016-08-19 16:54:28 +01001923 ret = remap_io_mapping(area,
Chris Wilson8bab11932017-01-14 00:28:25 +00001924 area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
Chris Wilsonc58305a2016-08-19 16:54:28 +01001925 (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
1926 min_t(u64, vma->size, area->vm_end - area->vm_start),
1927 &ggtt->mappable);
Chris Wilsona65adaf2017-10-09 09:43:57 +01001928 if (ret)
1929 goto err_fence;
Chris Wilsona61007a2016-08-18 17:17:02 +01001930
Chris Wilsona65adaf2017-10-09 09:43:57 +01001931 /* Mark as being mmapped into userspace for later revocation */
1932 assert_rpm_wakelock_held(dev_priv);
1933 if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
1934 list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
1935 GEM_BUG_ON(!obj->userfault_count);
1936
1937err_fence:
Chris Wilson3bd40732017-10-09 09:43:56 +01001938 i915_vma_unpin_fence(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001939err_unpin:
Chris Wilson058d88c2016-08-15 10:49:06 +01001940 __i915_vma_unpin(vma);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001941err_unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001942 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001943err_rpm:
1944 intel_runtime_pm_put(dev_priv);
Chris Wilson40e62d52016-10-28 13:58:41 +01001945 i915_gem_object_unpin_pages(obj);
Chris Wilsonb8f90962016-08-05 10:14:07 +01001946err:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001947 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001948 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001949 /*
1950 * We eat errors when the gpu is terminally wedged to avoid
1951 * userspace unduly crashing (gl has no provisions for mmaps to
1952 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1953 * and so needs to be reported.
1954 */
1955 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001956 ret = VM_FAULT_SIGBUS;
1957 break;
1958 }
Chris Wilson045e7692010-11-07 09:18:22 +00001959 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001960 /*
1961 * EAGAIN means the gpu is hung and we'll wait for the error
1962 * handler to reset everything when re-faulting in
1963 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001964 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001965 case 0:
1966 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001967 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001968 case -EBUSY:
1969 /*
1970 * EBUSY is ok: this just means that another thread
1971 * already did the job.
1972 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001973 ret = VM_FAULT_NOPAGE;
1974 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001975 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001976 ret = VM_FAULT_OOM;
1977 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001978 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001979 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001980 ret = VM_FAULT_SIGBUS;
1981 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001982 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001983 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001984 ret = VM_FAULT_SIGBUS;
1985 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001986 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001987 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001988}
1989
Chris Wilsona65adaf2017-10-09 09:43:57 +01001990static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
1991{
1992 struct i915_vma *vma;
1993
1994 GEM_BUG_ON(!obj->userfault_count);
1995
1996 obj->userfault_count = 0;
1997 list_del(&obj->userfault_link);
1998 drm_vma_node_unmap(&obj->base.vma_node,
1999 obj->base.dev->anon_inode->i_mapping);
2000
2001 list_for_each_entry(vma, &obj->vma_list, obj_link) {
2002 if (!i915_vma_is_ggtt(vma))
2003 break;
2004
2005 i915_vma_unset_userfault(vma);
2006 }
2007}
2008
Jesse Barnesde151cf2008-11-12 10:03:55 -08002009/**
Chris Wilson901782b2009-07-10 08:18:50 +01002010 * i915_gem_release_mmap - remove physical page mappings
2011 * @obj: obj in question
2012 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002013 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01002014 * relinquish ownership of the pages back to the system.
2015 *
2016 * It is vital that we remove the page mapping if we have mapped a tiled
2017 * object through the GTT and then lose the fence register due to
2018 * resource pressure. Similarly if the object has been moved out of the
2019 * aperture, than pages mapped into userspace must be revoked. Removing the
2020 * mapping will then trigger a page fault on the next user access, allowing
2021 * fixup by i915_gem_fault().
2022 */
Eric Anholtd05ca302009-07-10 13:02:26 -07002023void
Chris Wilson05394f32010-11-08 19:18:58 +00002024i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01002025{
Chris Wilson275f0392016-10-24 13:42:14 +01002026 struct drm_i915_private *i915 = to_i915(obj->base.dev);
Chris Wilson275f0392016-10-24 13:42:14 +01002027
Chris Wilson349f2cc2016-04-13 17:35:12 +01002028 /* Serialisation between user GTT access and our code depends upon
2029 * revoking the CPU's PTE whilst the mutex is held. The next user
2030 * pagefault then has to wait until we release the mutex.
Chris Wilson9c870d02016-10-24 13:42:15 +01002031 *
2032 * Note that RPM complicates somewhat by adding an additional
2033 * requirement that operations to the GGTT be made holding the RPM
2034 * wakeref.
Chris Wilson349f2cc2016-04-13 17:35:12 +01002035 */
Chris Wilson275f0392016-10-24 13:42:14 +01002036 lockdep_assert_held(&i915->drm.struct_mutex);
Chris Wilson9c870d02016-10-24 13:42:15 +01002037 intel_runtime_pm_get(i915);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002038
Chris Wilsona65adaf2017-10-09 09:43:57 +01002039 if (!obj->userfault_count)
Chris Wilson9c870d02016-10-24 13:42:15 +01002040 goto out;
Chris Wilson901782b2009-07-10 08:18:50 +01002041
Chris Wilsona65adaf2017-10-09 09:43:57 +01002042 __i915_gem_object_release_mmap(obj);
Chris Wilson349f2cc2016-04-13 17:35:12 +01002043
2044 /* Ensure that the CPU's PTE are revoked and there are not outstanding
2045 * memory transactions from userspace before we return. The TLB
2046 * flushing implied above by changing the PTE above *should* be
2047 * sufficient, an extra barrier here just provides us with a bit
2048 * of paranoid documentation about our requirement to serialise
2049 * memory writes before touching registers / GSM.
2050 */
2051 wmb();
Chris Wilson9c870d02016-10-24 13:42:15 +01002052
2053out:
2054 intel_runtime_pm_put(i915);
Chris Wilson901782b2009-07-10 08:18:50 +01002055}
2056
Chris Wilson7c108fd2016-10-24 13:42:18 +01002057void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002058{
Chris Wilson3594a3e2016-10-24 13:42:16 +01002059 struct drm_i915_gem_object *obj, *on;
Chris Wilson7c108fd2016-10-24 13:42:18 +01002060 int i;
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002061
Chris Wilson3594a3e2016-10-24 13:42:16 +01002062 /*
2063 * Only called during RPM suspend. All users of the userfault_list
2064 * must be holding an RPM wakeref to ensure that this can not
2065 * run concurrently with themselves (and use the struct_mutex for
2066 * protection between themselves).
2067 */
2068
2069 list_for_each_entry_safe(obj, on,
Chris Wilsona65adaf2017-10-09 09:43:57 +01002070 &dev_priv->mm.userfault_list, userfault_link)
2071 __i915_gem_object_release_mmap(obj);
Chris Wilson7c108fd2016-10-24 13:42:18 +01002072
2073 /* The fence will be lost when the device powers down. If any were
2074 * in use by hardware (i.e. they are pinned), we should not be powering
2075 * down! All other fences will be reacquired by the user upon waking.
2076 */
2077 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2078 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2079
Chris Wilsone0ec3ec2017-02-03 12:57:17 +00002080 /* Ideally we want to assert that the fence register is not
2081 * live at this point (i.e. that no piece of code will be
2082 * trying to write through fence + GTT, as that both violates
2083 * our tracking of activity and associated locking/barriers,
2084 * but also is illegal given that the hw is powered down).
2085 *
2086 * Previously we used reg->pin_count as a "liveness" indicator.
2087 * That is not sufficient, and we need a more fine-grained
2088 * tool if we want to have a sanity check here.
2089 */
Chris Wilson7c108fd2016-10-24 13:42:18 +01002090
2091 if (!reg->vma)
2092 continue;
2093
Chris Wilsona65adaf2017-10-09 09:43:57 +01002094 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
Chris Wilson7c108fd2016-10-24 13:42:18 +01002095 reg->dirty = true;
2096 }
Chris Wilsoneedd10f2014-06-16 08:57:44 +01002097}
2098
Chris Wilsond8cb5082012-08-11 15:41:03 +01002099static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2100{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002101 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002102 int err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002103
Chris Wilsonf3f61842016-08-05 10:14:14 +01002104 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002105 if (likely(!err))
Chris Wilsonf3f61842016-08-05 10:14:14 +01002106 return 0;
Daniel Vetterda494d72012-12-20 15:11:16 +01002107
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002108 /* Attempt to reap some mmap space from dead objects */
2109 do {
2110 err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
2111 if (err)
2112 break;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002113
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002114 i915_gem_drain_freed_objects(dev_priv);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002115 err = drm_gem_create_mmap_offset(&obj->base);
Chris Wilsonb42a13d2017-01-06 15:22:40 +00002116 if (!err)
2117 break;
2118
2119 } while (flush_delayed_work(&dev_priv->gt.retire_work));
Daniel Vetterda494d72012-12-20 15:11:16 +01002120
Chris Wilsonf3f61842016-08-05 10:14:14 +01002121 return err;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002122}
2123
2124static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2125{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002126 drm_gem_free_mmap_offset(&obj->base);
2127}
2128
Dave Airlieda6b51d2014-12-24 13:11:17 +10002129int
Dave Airlieff72145b2011-02-07 12:16:14 +10002130i915_gem_mmap_gtt(struct drm_file *file,
2131 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002132 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002133 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002134{
Chris Wilson05394f32010-11-08 19:18:58 +00002135 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002136 int ret;
2137
Chris Wilson03ac0642016-07-20 13:31:51 +01002138 obj = i915_gem_object_lookup(file, handle);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002139 if (!obj)
2140 return -ENOENT;
Chris Wilsonab182822009-09-22 18:46:17 +01002141
Chris Wilsond8cb5082012-08-11 15:41:03 +01002142 ret = i915_gem_object_create_mmap_offset(obj);
Chris Wilsonf3f61842016-08-05 10:14:14 +01002143 if (ret == 0)
2144 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002145
Chris Wilsonf0cd5182016-10-28 13:58:43 +01002146 i915_gem_object_put(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002147 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002148}
2149
Dave Airlieff72145b2011-02-07 12:16:14 +10002150/**
2151 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2152 * @dev: DRM device
2153 * @data: GTT mapping ioctl data
2154 * @file: GEM object info
2155 *
2156 * Simply returns the fake offset to userspace so it can mmap it.
2157 * The mmap call will end up in drm_gem_mmap(), which will set things
2158 * up so we can get faults in the handler above.
2159 *
2160 * The fault handler will take care of binding the object into the GTT
2161 * (since it may have been evicted to make room for something), allocating
2162 * a fence register, and mapping the appropriate aperture address into
2163 * userspace.
2164 */
2165int
2166i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2167 struct drm_file *file)
2168{
2169 struct drm_i915_gem_mmap_gtt *args = data;
2170
Dave Airlieda6b51d2014-12-24 13:11:17 +10002171 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002172}
2173
Daniel Vetter225067e2012-08-20 10:23:20 +02002174/* Immediately discard the backing storage */
2175static void
2176i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002177{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002178 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002179
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002180 if (obj->base.filp == NULL)
2181 return;
2182
Daniel Vetter225067e2012-08-20 10:23:20 +02002183 /* Our goal here is to return as much of the memory as
2184 * is possible back to the system as we are called from OOM.
2185 * To do this we must instruct the shmfs to drop all of its
2186 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002187 */
Chris Wilson55372522014-03-25 13:23:06 +00002188 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002189 obj->mm.madv = __I915_MADV_PURGED;
Chris Wilson4e5462e2017-03-07 13:20:31 +00002190 obj->mm.pages = ERR_PTR(-EFAULT);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002191}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002192
Chris Wilson55372522014-03-25 13:23:06 +00002193/* Try to discard unwanted pages */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002194void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002195{
Chris Wilson55372522014-03-25 13:23:06 +00002196 struct address_space *mapping;
2197
Chris Wilson1233e2d2016-10-28 13:58:37 +01002198 lockdep_assert_held(&obj->mm.lock);
2199 GEM_BUG_ON(obj->mm.pages);
2200
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002201 switch (obj->mm.madv) {
Chris Wilson55372522014-03-25 13:23:06 +00002202 case I915_MADV_DONTNEED:
2203 i915_gem_object_truncate(obj);
2204 case __I915_MADV_PURGED:
2205 return;
2206 }
2207
2208 if (obj->base.filp == NULL)
2209 return;
2210
Al Viro93c76a32015-12-04 23:45:44 -05002211 mapping = obj->base.filp->f_mapping,
Chris Wilson55372522014-03-25 13:23:06 +00002212 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002213}
2214
Chris Wilson5cdf5882010-09-27 15:51:07 +01002215static void
Chris Wilson03ac84f2016-10-28 13:58:36 +01002216i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
2217 struct sg_table *pages)
Eric Anholt673a3942008-07-30 12:06:12 -07002218{
Dave Gordon85d12252016-05-20 11:54:06 +01002219 struct sgt_iter sgt_iter;
2220 struct page *page;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002221
Chris Wilsone5facdf2016-12-23 14:57:57 +00002222 __i915_gem_object_release_shmem(obj, pages, true);
Eric Anholt856fa192009-03-19 14:10:50 -07002223
Chris Wilson03ac84f2016-10-28 13:58:36 +01002224 i915_gem_gtt_finish_pages(obj, pages);
Imre Deake2273302015-07-09 12:59:05 +03002225
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002226 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002227 i915_gem_object_save_bit_17_swizzle(obj, pages);
Eric Anholt280b7132009-03-12 16:56:27 -07002228
Chris Wilson03ac84f2016-10-28 13:58:36 +01002229 for_each_sgt_page(page, sgt_iter, pages) {
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002230 if (obj->mm.dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002231 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002232
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002233 if (obj->mm.madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002234 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002235
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002236 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002237 }
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002238 obj->mm.dirty = false;
Eric Anholt673a3942008-07-30 12:06:12 -07002239
Chris Wilson03ac84f2016-10-28 13:58:36 +01002240 sg_free_table(pages);
2241 kfree(pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002242}
2243
Chris Wilson96d77632016-10-28 13:58:33 +01002244static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
2245{
2246 struct radix_tree_iter iter;
Ville Syrjäläc23aa712017-09-01 20:12:51 +03002247 void __rcu **slot;
Chris Wilson96d77632016-10-28 13:58:33 +01002248
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002249 radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
2250 radix_tree_delete(&obj->mm.get_page.radix, iter.index);
Chris Wilson96d77632016-10-28 13:58:33 +01002251}
2252
Chris Wilson548625e2016-11-01 12:11:34 +00002253void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
2254 enum i915_mm_subclass subclass)
Chris Wilson37e680a2012-06-07 15:38:42 +01002255{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002256 struct sg_table *pages;
Chris Wilson37e680a2012-06-07 15:38:42 +01002257
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002258 if (i915_gem_object_has_pinned_pages(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002259 return;
Chris Wilsona5570172012-09-04 21:02:54 +01002260
Chris Wilson15717de2016-08-04 07:52:26 +01002261 GEM_BUG_ON(obj->bind_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002262 if (!READ_ONCE(obj->mm.pages))
2263 return;
2264
2265 /* May be called by shrinker from within get_pages() (on another bo) */
Chris Wilson548625e2016-11-01 12:11:34 +00002266 mutex_lock_nested(&obj->mm.lock, subclass);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002267 if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
2268 goto unlock;
Ben Widawsky3e123022013-07-31 17:00:04 -07002269
Chris Wilsona2165e32012-12-03 11:49:00 +00002270 /* ->put_pages might need to allocate memory for the bit17 swizzle
2271 * array, hence protect them from being reaped by removing them from gtt
2272 * lists early. */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002273 pages = fetch_and_zero(&obj->mm.pages);
2274 GEM_BUG_ON(!pages);
Chris Wilsona2165e32012-12-03 11:49:00 +00002275
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002276 if (obj->mm.mapping) {
Chris Wilson4b30cb22016-08-18 17:16:42 +01002277 void *ptr;
2278
Chris Wilson0ce81782017-05-17 13:09:59 +01002279 ptr = page_mask_bits(obj->mm.mapping);
Chris Wilson4b30cb22016-08-18 17:16:42 +01002280 if (is_vmalloc_addr(ptr))
2281 vunmap(ptr);
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002282 else
Chris Wilson4b30cb22016-08-18 17:16:42 +01002283 kunmap(kmap_to_page(ptr));
2284
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002285 obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002286 }
2287
Chris Wilson96d77632016-10-28 13:58:33 +01002288 __i915_gem_object_reset_page_iter(obj);
2289
Chris Wilson4e5462e2017-03-07 13:20:31 +00002290 if (!IS_ERR(pages))
2291 obj->ops->put_pages(obj, pages);
2292
Matthew Aulda5c081662017-10-06 23:18:18 +01002293 obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
2294
Chris Wilson1233e2d2016-10-28 13:58:37 +01002295unlock:
2296 mutex_unlock(&obj->mm.lock);
Chris Wilson6c085a72012-08-20 11:40:46 +02002297}
2298
Chris Wilson935a2f72017-02-13 17:15:13 +00002299static bool i915_sg_trim(struct sg_table *orig_st)
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002300{
2301 struct sg_table new_st;
2302 struct scatterlist *sg, *new_sg;
2303 unsigned int i;
2304
2305 if (orig_st->nents == orig_st->orig_nents)
Chris Wilson935a2f72017-02-13 17:15:13 +00002306 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002307
Chris Wilson8bfc478f2016-12-23 14:57:58 +00002308 if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
Chris Wilson935a2f72017-02-13 17:15:13 +00002309 return false;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002310
2311 new_sg = new_st.sgl;
2312 for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
2313 sg_set_page(new_sg, sg_page(sg), sg->length, 0);
2314 /* called before being DMA mapped, no need to copy sg->dma_* */
2315 new_sg = sg_next(new_sg);
2316 }
Chris Wilsonc2dc6cc2016-12-19 12:43:46 +00002317 GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002318
2319 sg_free_table(orig_st);
2320
2321 *orig_st = new_st;
Chris Wilson935a2f72017-02-13 17:15:13 +00002322 return true;
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002323}
2324
Matthew Auldb91b09e2017-10-06 23:18:17 +01002325static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002326{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002327 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilsond766ef52016-12-19 12:43:45 +00002328 const unsigned long page_count = obj->base.size / PAGE_SIZE;
2329 unsigned long i;
Eric Anholt673a3942008-07-30 12:06:12 -07002330 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002331 struct sg_table *st;
2332 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002333 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002334 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002335 unsigned long last_pfn = 0; /* suppress gcc warning */
Tvrtko Ursulin56024522017-08-03 10:14:17 +01002336 unsigned int max_segment = i915_sg_segment_size();
Matthew Auld84e89782017-10-09 12:00:24 +01002337 unsigned int sg_page_sizes;
Chris Wilson4846bf02017-06-09 12:03:46 +01002338 gfp_t noreclaim;
Imre Deake2273302015-07-09 12:59:05 +03002339 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002340
Chris Wilson6c085a72012-08-20 11:40:46 +02002341 /* Assert that the object is not currently in any GPU domain. As it
2342 * wasn't in the GTT, there shouldn't be any way it could have been in
2343 * a GPU cache
2344 */
Chris Wilson03ac84f2016-10-28 13:58:36 +01002345 GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2346 GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
Chris Wilson6c085a72012-08-20 11:40:46 +02002347
Chris Wilson9da3da62012-06-01 15:20:22 +01002348 st = kmalloc(sizeof(*st), GFP_KERNEL);
2349 if (st == NULL)
Matthew Auldb91b09e2017-10-06 23:18:17 +01002350 return -ENOMEM;
Eric Anholt673a3942008-07-30 12:06:12 -07002351
Chris Wilsond766ef52016-12-19 12:43:45 +00002352rebuild_st:
Chris Wilson9da3da62012-06-01 15:20:22 +01002353 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002354 kfree(st);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002355 return -ENOMEM;
Chris Wilson9da3da62012-06-01 15:20:22 +01002356 }
2357
2358 /* Get the list of pages out of our struct file. They'll be pinned
2359 * at this point until we release them.
2360 *
2361 * Fail silently without starting the shrinker
2362 */
Al Viro93c76a32015-12-04 23:45:44 -05002363 mapping = obj->base.filp->f_mapping;
Chris Wilson0f6ab552017-06-09 12:03:48 +01002364 noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
Chris Wilson4846bf02017-06-09 12:03:46 +01002365 noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
2366
Imre Deak90797e62013-02-18 19:28:03 +02002367 sg = st->sgl;
2368 st->nents = 0;
Matthew Auld84e89782017-10-09 12:00:24 +01002369 sg_page_sizes = 0;
Imre Deak90797e62013-02-18 19:28:03 +02002370 for (i = 0; i < page_count; i++) {
Chris Wilson4846bf02017-06-09 12:03:46 +01002371 const unsigned int shrink[] = {
2372 I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
2373 0,
2374 }, *s = shrink;
2375 gfp_t gfp = noreclaim;
2376
2377 do {
Chris Wilson6c085a72012-08-20 11:40:46 +02002378 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
Chris Wilson4846bf02017-06-09 12:03:46 +01002379 if (likely(!IS_ERR(page)))
2380 break;
2381
2382 if (!*s) {
2383 ret = PTR_ERR(page);
2384 goto err_sg;
2385 }
2386
Chris Wilson912d5722017-09-06 16:19:30 -07002387 i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
Chris Wilson4846bf02017-06-09 12:03:46 +01002388 cond_resched();
Chris Wilson24f8e002017-03-22 11:05:21 +00002389
Chris Wilson6c085a72012-08-20 11:40:46 +02002390 /* We've tried hard to allocate the memory by reaping
2391 * our own buffer, now let the real VM do its job and
2392 * go down in flames if truly OOM.
Chris Wilson24f8e002017-03-22 11:05:21 +00002393 *
2394 * However, since graphics tend to be disposable,
2395 * defer the oom here by reporting the ENOMEM back
2396 * to userspace.
Chris Wilson6c085a72012-08-20 11:40:46 +02002397 */
Chris Wilson4846bf02017-06-09 12:03:46 +01002398 if (!*s) {
2399 /* reclaim and warn, but no oom */
2400 gfp = mapping_gfp_mask(mapping);
Chris Wilsoneaf41802017-06-09 12:03:47 +01002401
2402 /* Our bo are always dirty and so we require
2403 * kswapd to reclaim our pages (direct reclaim
2404 * does not effectively begin pageout of our
2405 * buffers on its own). However, direct reclaim
2406 * only waits for kswapd when under allocation
2407 * congestion. So as a result __GFP_RECLAIM is
2408 * unreliable and fails to actually reclaim our
2409 * dirty pages -- unless you try over and over
2410 * again with !__GFP_NORETRY. However, we still
2411 * want to fail this allocation rather than
2412 * trigger the out-of-memory killer and for
Michal Hockodbb32952017-07-12 14:36:55 -07002413 * this we want __GFP_RETRY_MAYFAIL.
Chris Wilsoneaf41802017-06-09 12:03:47 +01002414 */
Michal Hockodbb32952017-07-12 14:36:55 -07002415 gfp |= __GFP_RETRY_MAYFAIL;
Imre Deake2273302015-07-09 12:59:05 +03002416 }
Chris Wilson4846bf02017-06-09 12:03:46 +01002417 } while (1);
2418
Chris Wilson871dfbd2016-10-11 09:20:21 +01002419 if (!i ||
2420 sg->length >= max_segment ||
2421 page_to_pfn(page) != last_pfn + 1) {
Matthew Aulda5c081662017-10-06 23:18:18 +01002422 if (i) {
Matthew Auld84e89782017-10-09 12:00:24 +01002423 sg_page_sizes |= sg->length;
Imre Deak90797e62013-02-18 19:28:03 +02002424 sg = sg_next(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002425 }
Imre Deak90797e62013-02-18 19:28:03 +02002426 st->nents++;
2427 sg_set_page(sg, page, PAGE_SIZE, 0);
2428 } else {
2429 sg->length += PAGE_SIZE;
2430 }
2431 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002432
2433 /* Check that the i965g/gm workaround works. */
2434 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002435 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002436 if (sg) { /* loop terminated early; short sg table */
Matthew Auld84e89782017-10-09 12:00:24 +01002437 sg_page_sizes |= sg->length;
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002438 sg_mark_end(sg);
Matthew Aulda5c081662017-10-06 23:18:18 +01002439 }
Chris Wilson74ce6b62012-10-19 15:51:06 +01002440
Tvrtko Ursulin0c40ce12016-11-09 15:13:43 +00002441 /* Trim unused sg entries to avoid wasting memory. */
2442 i915_sg_trim(st);
2443
Chris Wilson03ac84f2016-10-28 13:58:36 +01002444 ret = i915_gem_gtt_prepare_pages(obj, st);
Chris Wilsond766ef52016-12-19 12:43:45 +00002445 if (ret) {
2446 /* DMA remapping failed? One possible cause is that
2447 * it could not reserve enough large entries, asking
2448 * for PAGE_SIZE chunks instead may be helpful.
2449 */
2450 if (max_segment > PAGE_SIZE) {
2451 for_each_sgt_page(page, sgt_iter, st)
2452 put_page(page);
2453 sg_free_table(st);
2454
2455 max_segment = PAGE_SIZE;
2456 goto rebuild_st;
2457 } else {
2458 dev_warn(&dev_priv->drm.pdev->dev,
2459 "Failed to DMA remap %lu pages\n",
2460 page_count);
2461 goto err_pages;
2462 }
2463 }
Imre Deake2273302015-07-09 12:59:05 +03002464
Eric Anholt673a3942008-07-30 12:06:12 -07002465 if (i915_gem_object_needs_bit17_swizzle(obj))
Chris Wilson03ac84f2016-10-28 13:58:36 +01002466 i915_gem_object_do_bit_17_swizzle(obj, st);
Eric Anholt673a3942008-07-30 12:06:12 -07002467
Matthew Auld84e89782017-10-09 12:00:24 +01002468 __i915_gem_object_set_pages(obj, st, sg_page_sizes);
Matthew Auldb91b09e2017-10-06 23:18:17 +01002469
2470 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002471
Chris Wilsonb17993b2016-11-14 11:29:30 +00002472err_sg:
Imre Deak90797e62013-02-18 19:28:03 +02002473 sg_mark_end(sg);
Chris Wilsonb17993b2016-11-14 11:29:30 +00002474err_pages:
Dave Gordon85d12252016-05-20 11:54:06 +01002475 for_each_sgt_page(page, sgt_iter, st)
2476 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002477 sg_free_table(st);
2478 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002479
2480 /* shmemfs first checks if there is enough memory to allocate the page
2481 * and reports ENOSPC should there be insufficient, along with the usual
2482 * ENOMEM for a genuine allocation failure.
2483 *
2484 * We use ENOSPC in our driver to mean that we have run out of aperture
2485 * space and so want to translate the error from shmemfs back to our
2486 * usual understanding of ENOMEM.
2487 */
Imre Deake2273302015-07-09 12:59:05 +03002488 if (ret == -ENOSPC)
2489 ret = -ENOMEM;
2490
Matthew Auldb91b09e2017-10-06 23:18:17 +01002491 return ret;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002492}
2493
2494void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
Matthew Aulda5c081662017-10-06 23:18:18 +01002495 struct sg_table *pages,
Matthew Auld84e89782017-10-09 12:00:24 +01002496 unsigned int sg_page_sizes)
Chris Wilson03ac84f2016-10-28 13:58:36 +01002497{
Matthew Aulda5c081662017-10-06 23:18:18 +01002498 struct drm_i915_private *i915 = to_i915(obj->base.dev);
2499 unsigned long supported = INTEL_INFO(i915)->page_sizes;
2500 int i;
2501
Chris Wilson1233e2d2016-10-28 13:58:37 +01002502 lockdep_assert_held(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002503
2504 obj->mm.get_page.sg_pos = pages->sgl;
2505 obj->mm.get_page.sg_idx = 0;
2506
2507 obj->mm.pages = pages;
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002508
2509 if (i915_gem_object_is_tiled(obj) &&
2510 to_i915(obj->base.dev)->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
2511 GEM_BUG_ON(obj->mm.quirked);
2512 __i915_gem_object_pin_pages(obj);
2513 obj->mm.quirked = true;
2514 }
Matthew Aulda5c081662017-10-06 23:18:18 +01002515
Matthew Auld84e89782017-10-09 12:00:24 +01002516 GEM_BUG_ON(!sg_page_sizes);
2517 obj->mm.page_sizes.phys = sg_page_sizes;
Matthew Aulda5c081662017-10-06 23:18:18 +01002518
2519 /*
Matthew Auld84e89782017-10-09 12:00:24 +01002520 * Calculate the supported page-sizes which fit into the given
2521 * sg_page_sizes. This will give us the page-sizes which we may be able
2522 * to use opportunistically when later inserting into the GTT. For
2523 * example if phys=2G, then in theory we should be able to use 1G, 2M,
2524 * 64K or 4K pages, although in practice this will depend on a number of
2525 * other factors.
Matthew Aulda5c081662017-10-06 23:18:18 +01002526 */
2527 obj->mm.page_sizes.sg = 0;
2528 for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
2529 if (obj->mm.page_sizes.phys & ~0u << i)
2530 obj->mm.page_sizes.sg |= BIT(i);
2531 }
2532
2533 GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002534}
2535
2536static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2537{
Matthew Auldb91b09e2017-10-06 23:18:17 +01002538 int err;
Chris Wilson03ac84f2016-10-28 13:58:36 +01002539
2540 if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
2541 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2542 return -EFAULT;
2543 }
2544
Matthew Auldb91b09e2017-10-06 23:18:17 +01002545 err = obj->ops->get_pages(obj);
2546 GEM_BUG_ON(!err && IS_ERR_OR_NULL(obj->mm.pages));
Chris Wilson03ac84f2016-10-28 13:58:36 +01002547
Matthew Auldb91b09e2017-10-06 23:18:17 +01002548 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002549}
2550
Chris Wilson37e680a2012-06-07 15:38:42 +01002551/* Ensure that the associated pages are gathered from the backing storage
Chris Wilson1233e2d2016-10-28 13:58:37 +01002552 * and pinned into our object. i915_gem_object_pin_pages() may be called
Chris Wilson37e680a2012-06-07 15:38:42 +01002553 * multiple times before they are released by a single call to
Chris Wilson1233e2d2016-10-28 13:58:37 +01002554 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
Chris Wilson37e680a2012-06-07 15:38:42 +01002555 * either as a result of memory pressure (reaping pages under the shrinker)
2556 * or as the object is itself released.
2557 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002558int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
Chris Wilson37e680a2012-06-07 15:38:42 +01002559{
Chris Wilson03ac84f2016-10-28 13:58:36 +01002560 int err;
Chris Wilson37e680a2012-06-07 15:38:42 +01002561
Chris Wilson1233e2d2016-10-28 13:58:37 +01002562 err = mutex_lock_interruptible(&obj->mm.lock);
2563 if (err)
2564 return err;
Chris Wilson4c7d62c2016-10-28 13:58:32 +01002565
Chris Wilson4e5462e2017-03-07 13:20:31 +00002566 if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002567 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2568
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002569 err = ____i915_gem_object_get_pages(obj);
2570 if (err)
2571 goto unlock;
2572
2573 smp_mb__before_atomic();
Chris Wilson1233e2d2016-10-28 13:58:37 +01002574 }
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002575 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson43e28f02013-01-08 10:53:09 +00002576
Chris Wilson1233e2d2016-10-28 13:58:37 +01002577unlock:
2578 mutex_unlock(&obj->mm.lock);
Chris Wilson03ac84f2016-10-28 13:58:36 +01002579 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07002580}
2581
Dave Gordondd6034c2016-05-20 11:54:04 +01002582/* The 'mapping' part of i915_gem_object_pin_map() below */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002583static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
2584 enum i915_map_type type)
Dave Gordondd6034c2016-05-20 11:54:04 +01002585{
2586 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002587 struct sg_table *sgt = obj->mm.pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002588 struct sgt_iter sgt_iter;
2589 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002590 struct page *stack_pages[32];
2591 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002592 unsigned long i = 0;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002593 pgprot_t pgprot;
Dave Gordondd6034c2016-05-20 11:54:04 +01002594 void *addr;
2595
2596 /* A single page can always be kmapped */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002597 if (n_pages == 1 && type == I915_MAP_WB)
Dave Gordondd6034c2016-05-20 11:54:04 +01002598 return kmap(sg_page(sgt->sgl));
2599
Dave Gordonb338fa42016-05-20 11:54:05 +01002600 if (n_pages > ARRAY_SIZE(stack_pages)) {
2601 /* Too big for stack -- allocate temporary array instead */
Michal Hocko0ee931c2017-09-13 16:28:29 -07002602 pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
Dave Gordonb338fa42016-05-20 11:54:05 +01002603 if (!pages)
2604 return NULL;
2605 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002606
Dave Gordon85d12252016-05-20 11:54:06 +01002607 for_each_sgt_page(page, sgt_iter, sgt)
2608 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002609
2610 /* Check that we have the expected number of pages */
2611 GEM_BUG_ON(i != n_pages);
2612
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002613 switch (type) {
Chris Wilsona575c672017-08-28 11:46:31 +01002614 default:
2615 MISSING_CASE(type);
2616 /* fallthrough to use PAGE_KERNEL anyway */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002617 case I915_MAP_WB:
2618 pgprot = PAGE_KERNEL;
2619 break;
2620 case I915_MAP_WC:
2621 pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
2622 break;
2623 }
2624 addr = vmap(pages, n_pages, 0, pgprot);
Dave Gordondd6034c2016-05-20 11:54:04 +01002625
Dave Gordonb338fa42016-05-20 11:54:05 +01002626 if (pages != stack_pages)
Michal Hocko20981052017-05-17 14:23:12 +02002627 kvfree(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002628
2629 return addr;
2630}
2631
2632/* get, pin, and map the pages of the object into kernel space */
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002633void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
2634 enum i915_map_type type)
Chris Wilson0a798eb2016-04-08 12:11:11 +01002635{
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002636 enum i915_map_type has_type;
2637 bool pinned;
2638 void *ptr;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002639 int ret;
2640
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002641 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002642
Chris Wilson1233e2d2016-10-28 13:58:37 +01002643 ret = mutex_lock_interruptible(&obj->mm.lock);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002644 if (ret)
2645 return ERR_PTR(ret);
2646
Chris Wilsona575c672017-08-28 11:46:31 +01002647 pinned = !(type & I915_MAP_OVERRIDE);
2648 type &= ~I915_MAP_OVERRIDE;
2649
Chris Wilson1233e2d2016-10-28 13:58:37 +01002650 if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
Chris Wilson4e5462e2017-03-07 13:20:31 +00002651 if (unlikely(IS_ERR_OR_NULL(obj->mm.pages))) {
Chris Wilson88c880b2017-09-06 14:52:20 +01002652 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
2653
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002654 ret = ____i915_gem_object_get_pages(obj);
2655 if (ret)
2656 goto err_unlock;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002657
Chris Wilson2c3a3f42016-11-04 10:30:01 +00002658 smp_mb__before_atomic();
2659 }
2660 atomic_inc(&obj->mm.pages_pin_count);
Chris Wilson1233e2d2016-10-28 13:58:37 +01002661 pinned = false;
2662 }
2663 GEM_BUG_ON(!obj->mm.pages);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002664
Chris Wilson0ce81782017-05-17 13:09:59 +01002665 ptr = page_unpack_bits(obj->mm.mapping, &has_type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002666 if (ptr && has_type != type) {
2667 if (pinned) {
2668 ret = -EBUSY;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002669 goto err_unpin;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002670 }
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002671
2672 if (is_vmalloc_addr(ptr))
2673 vunmap(ptr);
2674 else
2675 kunmap(kmap_to_page(ptr));
2676
Chris Wilsona4f5ea62016-10-28 13:58:35 +01002677 ptr = obj->mm.mapping = NULL;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002678 }
2679
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002680 if (!ptr) {
2681 ptr = i915_gem_object_map(obj, type);
2682 if (!ptr) {
2683 ret = -ENOMEM;
Chris Wilson1233e2d2016-10-28 13:58:37 +01002684 goto err_unpin;
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002685 }
2686
Chris Wilson0ce81782017-05-17 13:09:59 +01002687 obj->mm.mapping = page_pack_bits(ptr, type);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002688 }
2689
Chris Wilson1233e2d2016-10-28 13:58:37 +01002690out_unlock:
2691 mutex_unlock(&obj->mm.lock);
Chris Wilsond31d7cb2016-08-12 12:39:58 +01002692 return ptr;
2693
Chris Wilson1233e2d2016-10-28 13:58:37 +01002694err_unpin:
2695 atomic_dec(&obj->mm.pages_pin_count);
2696err_unlock:
2697 ptr = ERR_PTR(ret);
2698 goto out_unlock;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002699}
2700
Chris Wilson7c55e2c2017-03-07 12:03:38 +00002701static int
2702i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
2703 const struct drm_i915_gem_pwrite *arg)
2704{
2705 struct address_space *mapping = obj->base.filp->f_mapping;
2706 char __user *user_data = u64_to_user_ptr(arg->data_ptr);
2707 u64 remain, offset;
2708 unsigned int pg;
2709
2710 /* Before we instantiate/pin the backing store for our use, we
2711 * can prepopulate the shmemfs filp efficiently using a write into
2712 * the pagecache. We avoid the penalty of instantiating all the
2713 * pages, important if the user is just writing to a few and never
2714 * uses the object on the GPU, and using a direct write into shmemfs
2715 * allows it to avoid the cost of retrieving a page (either swapin
2716 * or clearing-before-use) before it is overwritten.
2717 */
2718 if (READ_ONCE(obj->mm.pages))
2719 return -ENODEV;
2720
2721 /* Before the pages are instantiated the object is treated as being
2722 * in the CPU domain. The pages will be clflushed as required before
2723 * use, and we can freely write into the pages directly. If userspace
2724 * races pwrite with any other operation; corruption will ensue -
2725 * that is userspace's prerogative!
2726 */
2727
2728 remain = arg->size;
2729 offset = arg->offset;
2730 pg = offset_in_page(offset);
2731
2732 do {
2733 unsigned int len, unwritten;
2734 struct page *page;
2735 void *data, *vaddr;
2736 int err;
2737
2738 len = PAGE_SIZE - pg;
2739 if (len > remain)
2740 len = remain;
2741
2742 err = pagecache_write_begin(obj->base.filp, mapping,
2743 offset, len, 0,
2744 &page, &data);
2745 if (err < 0)
2746 return err;
2747
2748 vaddr = kmap(page);
2749 unwritten = copy_from_user(vaddr + pg, user_data, len);
2750 kunmap(page);
2751
2752 err = pagecache_write_end(obj->base.filp, mapping,
2753 offset, len, len - unwritten,
2754 page, data);
2755 if (err < 0)
2756 return err;
2757
2758 if (unwritten)
2759 return -EFAULT;
2760
2761 remain -= len;
2762 user_data += len;
2763 offset += len;
2764 pg = 0;
2765 } while (remain);
2766
2767 return 0;
2768}
2769
Chris Wilson77b25a92017-07-21 13:32:30 +01002770static bool ban_context(const struct i915_gem_context *ctx,
2771 unsigned int score)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002772{
Chris Wilson60958682016-12-31 11:20:11 +00002773 return (i915_gem_context_is_bannable(ctx) &&
Chris Wilson77b25a92017-07-21 13:32:30 +01002774 score >= CONTEXT_SCORE_BAN_THRESHOLD);
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002775}
2776
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002777static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002778{
Chris Wilson77b25a92017-07-21 13:32:30 +01002779 unsigned int score;
2780 bool banned;
Mika Kuoppalab083a082016-11-18 15:10:47 +02002781
Chris Wilson77b25a92017-07-21 13:32:30 +01002782 atomic_inc(&ctx->guilty_count);
2783
2784 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
2785 banned = ban_context(ctx, score);
Mika Kuoppalab083a082016-11-18 15:10:47 +02002786 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
Chris Wilson77b25a92017-07-21 13:32:30 +01002787 ctx->name, score, yesno(banned));
2788 if (!banned)
Mika Kuoppalab083a082016-11-18 15:10:47 +02002789 return;
2790
Chris Wilson77b25a92017-07-21 13:32:30 +01002791 i915_gem_context_set_banned(ctx);
2792 if (!IS_ERR_OR_NULL(ctx->file_priv)) {
2793 atomic_inc(&ctx->file_priv->context_bans);
2794 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2795 ctx->name, atomic_read(&ctx->file_priv->context_bans));
2796 }
Mika Kuoppalae5e1fc42016-11-16 17:20:31 +02002797}
2798
2799static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
2800{
Chris Wilson77b25a92017-07-21 13:32:30 +01002801 atomic_inc(&ctx->active_count);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002802}
2803
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002804struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002805i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002806{
Chris Wilson754c9fd2017-02-23 07:44:14 +00002807 struct drm_i915_gem_request *request, *active = NULL;
2808 unsigned long flags;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002809
Chris Wilsonf69a02c2016-07-01 17:23:16 +01002810 /* We are called by the error capture and reset at a random
2811 * point in time. In particular, note that neither is crucially
2812 * ordered with an interrupt. After a hang, the GPU is dead and we
2813 * assume that no more writes can happen (we waited long enough for
2814 * all writes that were in transaction to be flushed) - adding an
2815 * extra delay for a recent interrupt is pointless. Hence, we do
2816 * not need an engine->irq_seqno_barrier() before the seqno reads.
2817 */
Chris Wilson754c9fd2017-02-23 07:44:14 +00002818 spin_lock_irqsave(&engine->timeline->lock, flags);
Chris Wilson73cb9702016-10-28 13:58:46 +01002819 list_for_each_entry(request, &engine->timeline->requests, link) {
Chris Wilson754c9fd2017-02-23 07:44:14 +00002820 if (__i915_gem_request_completed(request,
2821 request->global_seqno))
Chris Wilson4db080f2013-12-04 11:37:09 +00002822 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002823
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002824 GEM_BUG_ON(request->engine != engine);
Chris Wilsonc00122f32017-02-12 17:19:58 +00002825 GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
2826 &request->fence.flags));
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002827
Chris Wilson754c9fd2017-02-23 07:44:14 +00002828 active = request;
2829 break;
2830 }
2831 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2832
2833 return active;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002834}
2835
Mika Kuoppalabf2f0432017-01-17 17:59:04 +02002836static bool engine_stalled(struct intel_engine_cs *engine)
2837{
2838 if (!engine->hangcheck.stalled)
2839 return false;
2840
2841 /* Check for possible seqno movement after hang declaration */
2842 if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
2843 DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
2844 return false;
2845 }
2846
2847 return true;
2848}
2849
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002850/*
2851 * Ensure irq handler finishes, and not run again.
2852 * Also return the active request so that we only search for it once.
2853 */
2854struct drm_i915_gem_request *
2855i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
2856{
2857 struct drm_i915_gem_request *request = NULL;
2858
Chris Wilson1749d902017-10-09 12:02:59 +01002859 /*
2860 * During the reset sequence, we must prevent the engine from
2861 * entering RC6. As the context state is undefined until we restart
2862 * the engine, if it does enter RC6 during the reset, the state
2863 * written to the powercontext is undefined and so we may lose
2864 * GPU state upon resume, i.e. fail to restart after a reset.
2865 */
2866 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
2867
2868 /*
2869 * Prevent the signaler thread from updating the request
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002870 * state (by calling dma_fence_signal) as we are processing
2871 * the reset. The write from the GPU of the seqno is
2872 * asynchronous and the signaler thread may see a different
2873 * value to us and declare the request complete, even though
2874 * the reset routine have picked that request as the active
2875 * (incomplete) request. This conflict is not handled
2876 * gracefully!
2877 */
2878 kthread_park(engine->breadcrumbs.signaler);
2879
Chris Wilson1749d902017-10-09 12:02:59 +01002880 /*
2881 * Prevent request submission to the hardware until we have
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002882 * completed the reset in i915_gem_reset_finish(). If a request
2883 * is completed by one engine, it may then queue a request
2884 * to a second via its engine->irq_tasklet *just* as we are
2885 * calling engine->init_hw() and also writing the ELSP.
2886 * Turning off the engine->irq_tasklet until the reset is over
2887 * prevents the race.
2888 */
Mika Kuoppalab620e872017-09-22 15:43:03 +03002889 tasklet_kill(&engine->execlists.irq_tasklet);
2890 tasklet_disable(&engine->execlists.irq_tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002891
2892 if (engine->irq_seqno_barrier)
2893 engine->irq_seqno_barrier(engine);
2894
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002895 request = i915_gem_find_active_request(engine);
2896 if (request && request->fence.error == -EIO)
2897 request = ERR_PTR(-EIO); /* Previous reset failed! */
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002898
2899 return request;
2900}
2901
Chris Wilson0e178ae2017-01-17 17:59:06 +02002902int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
Chris Wilson4c965542017-01-17 17:59:01 +02002903{
2904 struct intel_engine_cs *engine;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002905 struct drm_i915_gem_request *request;
Chris Wilson4c965542017-01-17 17:59:01 +02002906 enum intel_engine_id id;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002907 int err = 0;
Chris Wilson4c965542017-01-17 17:59:01 +02002908
Chris Wilson0e178ae2017-01-17 17:59:06 +02002909 for_each_engine(engine, dev_priv, id) {
Michel Thierrya1ef70e2017-06-20 10:57:47 +01002910 request = i915_gem_reset_prepare_engine(engine);
2911 if (IS_ERR(request)) {
2912 err = PTR_ERR(request);
2913 continue;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002914 }
Michel Thierryc64992e2017-06-20 10:57:44 +01002915
2916 engine->hangcheck.active_request = request;
Chris Wilson0e178ae2017-01-17 17:59:06 +02002917 }
2918
Chris Wilson4c965542017-01-17 17:59:01 +02002919 i915_gem_revoke_fences(dev_priv);
Chris Wilson0e178ae2017-01-17 17:59:06 +02002920
2921 return err;
Chris Wilson4c965542017-01-17 17:59:01 +02002922}
2923
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002924static void skip_request(struct drm_i915_gem_request *request)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002925{
Chris Wilson821ed7d2016-09-09 14:11:53 +01002926 void *vaddr = request->ring->vaddr;
2927 u32 head;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002928
Chris Wilson821ed7d2016-09-09 14:11:53 +01002929 /* As this request likely depends on state from the lost
2930 * context, clear out all the user operations leaving the
2931 * breadcrumb at the end (so we get the fence notifications).
2932 */
2933 head = request->head;
2934 if (request->postfix < head) {
2935 memset(vaddr + head, 0, request->ring->size - head);
2936 head = 0;
2937 }
2938 memset(vaddr + head, 0, request->postfix - head);
Chris Wilsonc0d5f322017-01-10 17:22:43 +00002939
2940 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson4db080f2013-12-04 11:37:09 +00002941}
2942
Mika Kuoppala36193ac2017-01-17 17:59:02 +02002943static void engine_skip_context(struct drm_i915_gem_request *request)
2944{
2945 struct intel_engine_cs *engine = request->engine;
2946 struct i915_gem_context *hung_ctx = request->ctx;
2947 struct intel_timeline *timeline;
2948 unsigned long flags;
2949
2950 timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
2951
2952 spin_lock_irqsave(&engine->timeline->lock, flags);
2953 spin_lock(&timeline->lock);
2954
2955 list_for_each_entry_continue(request, &engine->timeline->requests, link)
2956 if (request->ctx == hung_ctx)
2957 skip_request(request);
2958
2959 list_for_each_entry(request, &timeline->requests, link)
2960 skip_request(request);
2961
2962 spin_unlock(&timeline->lock);
2963 spin_unlock_irqrestore(&engine->timeline->lock, flags);
2964}
2965
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002966/* Returns the request if it was guilty of the hang */
2967static struct drm_i915_gem_request *
2968i915_gem_reset_request(struct intel_engine_cs *engine,
2969 struct drm_i915_gem_request *request)
Mika Kuoppala61da5362017-01-17 17:59:05 +02002970{
Mika Kuoppala71895a02017-01-17 17:59:07 +02002971 /* The guilty request will get skipped on a hung engine.
2972 *
2973 * Users of client default contexts do not rely on logical
2974 * state preserved between batches so it is safe to execute
2975 * queued requests following the hang. Non default contexts
2976 * rely on preserved state, so skipping a batch loses the
2977 * evolution of the state and it needs to be considered corrupted.
2978 * Executing more queued batches on top of corrupted state is
2979 * risky. But we take the risk by trying to advance through
2980 * the queued requests in order to make the client behaviour
2981 * more predictable around resets, by not throwing away random
2982 * amount of batches it has prepared for execution. Sophisticated
2983 * clients can use gem_reset_stats_ioctl and dma fence status
2984 * (exported via sync_file info ioctl on explicit fences) to observe
2985 * when it loses the context state and should rebuild accordingly.
2986 *
2987 * The context ban, and ultimately the client ban, mechanism are safety
2988 * valves if client submission ends up resulting in nothing more than
2989 * subsequent hangs.
2990 */
2991
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002992 if (engine_stalled(engine)) {
Mika Kuoppala61da5362017-01-17 17:59:05 +02002993 i915_gem_context_mark_guilty(request->ctx);
2994 skip_request(request);
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01002995
2996 /* If this context is now banned, skip all pending requests. */
2997 if (i915_gem_context_is_banned(request->ctx))
2998 engine_skip_context(request);
Mika Kuoppala61da5362017-01-17 17:59:05 +02002999 } else {
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003000 /*
3001 * Since this is not the hung engine, it may have advanced
3002 * since the hang declaration. Double check by refinding
3003 * the active request at the time of the reset.
3004 */
3005 request = i915_gem_find_active_request(engine);
3006 if (request) {
3007 i915_gem_context_mark_innocent(request->ctx);
3008 dma_fence_set_error(&request->fence, -EAGAIN);
3009
3010 /* Rewind the engine to replay the incomplete rq */
3011 spin_lock_irq(&engine->timeline->lock);
3012 request = list_prev_entry(request, link);
3013 if (&request->link == &engine->timeline->requests)
3014 request = NULL;
3015 spin_unlock_irq(&engine->timeline->lock);
3016 }
Mika Kuoppala61da5362017-01-17 17:59:05 +02003017 }
3018
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003019 return request;
Mika Kuoppala61da5362017-01-17 17:59:05 +02003020}
3021
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003022void i915_gem_reset_engine(struct intel_engine_cs *engine,
3023 struct drm_i915_gem_request *request)
Chris Wilson4db080f2013-12-04 11:37:09 +00003024{
Chris Wilsoned454f22017-07-21 13:32:29 +01003025 engine->irq_posted = 0;
3026
Chris Wilsond1d1ebf42017-07-21 13:32:33 +01003027 if (request)
3028 request = i915_gem_reset_request(engine, request);
3029
3030 if (request) {
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003031 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
3032 engine->name, request->global_seqno);
Chris Wilsonc0dcb202017-02-07 15:24:37 +00003033 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003034
3035 /* Setup the CS to resume from the breadcrumb of the hung request */
3036 engine->reset_hw(engine, request);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003037}
3038
Chris Wilsond8027092017-02-08 14:30:32 +00003039void i915_gem_reset(struct drm_i915_private *dev_priv)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003040{
3041 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303042 enum intel_engine_id id;
Chris Wilson821ed7d2016-09-09 14:11:53 +01003043
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003044 lockdep_assert_held(&dev_priv->drm.struct_mutex);
3045
Chris Wilson821ed7d2016-09-09 14:11:53 +01003046 i915_gem_retire_requests(dev_priv);
3047
Chris Wilson2ae55732017-02-12 17:20:02 +00003048 for_each_engine(engine, dev_priv, id) {
3049 struct i915_gem_context *ctx;
3050
Michel Thierryc64992e2017-06-20 10:57:44 +01003051 i915_gem_reset_engine(engine, engine->hangcheck.active_request);
Chris Wilson2ae55732017-02-12 17:20:02 +00003052 ctx = fetch_and_zero(&engine->last_retired_context);
3053 if (ctx)
3054 engine->context_unpin(engine, ctx);
3055 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003056
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00003057 i915_gem_restore_fences(dev_priv);
Chris Wilsonf2a91d12016-09-21 14:51:06 +01003058
3059 if (dev_priv->gt.awake) {
3060 intel_sanitize_gt_powersave(dev_priv);
3061 intel_enable_gt_powersave(dev_priv);
3062 if (INTEL_GEN(dev_priv) >= 6)
3063 gen6_rps_busy(dev_priv);
3064 }
Chris Wilson821ed7d2016-09-09 14:11:53 +01003065}
3066
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003067void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
3068{
Mika Kuoppalab620e872017-09-22 15:43:03 +03003069 tasklet_enable(&engine->execlists.irq_tasklet);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003070 kthread_unpark(engine->breadcrumbs.signaler);
Chris Wilson1749d902017-10-09 12:02:59 +01003071
3072 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003073}
3074
Chris Wilsond8027092017-02-08 14:30:32 +00003075void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
3076{
Chris Wilson1f7b8472017-02-08 14:30:33 +00003077 struct intel_engine_cs *engine;
3078 enum intel_engine_id id;
3079
Chris Wilsond8027092017-02-08 14:30:32 +00003080 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Chris Wilson1f7b8472017-02-08 14:30:33 +00003081
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003082 for_each_engine(engine, dev_priv, id) {
Michel Thierryc64992e2017-06-20 10:57:44 +01003083 engine->hangcheck.active_request = NULL;
Michel Thierrya1ef70e2017-06-20 10:57:47 +01003084 i915_gem_reset_finish_engine(engine);
Chris Wilsonfe3288b2017-02-12 17:20:01 +00003085 }
Chris Wilsond8027092017-02-08 14:30:32 +00003086}
3087
Chris Wilson821ed7d2016-09-09 14:11:53 +01003088static void nop_submit_request(struct drm_i915_gem_request *request)
3089{
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003090 GEM_BUG_ON(!i915_terminally_wedged(&request->i915->gpu_error));
3091 dma_fence_set_error(&request->fence, -EIO);
3092
3093 i915_gem_request_submit(request);
3094}
3095
3096static void nop_complete_submit_request(struct drm_i915_gem_request *request)
3097{
Chris Wilson8d550822017-10-06 12:56:17 +01003098 unsigned long flags;
3099
Chris Wilsonbf2eac32017-07-21 13:32:28 +01003100 GEM_BUG_ON(!i915_terminally_wedged(&request->i915->gpu_error));
Chris Wilson3cd94422017-01-10 17:22:45 +00003101 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003102
3103 spin_lock_irqsave(&request->engine->timeline->lock, flags);
3104 __i915_gem_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003105 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003106 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003107}
3108
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003109void i915_gem_set_wedged(struct drm_i915_private *i915)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003110{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003111 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303112 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003113
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003114 /*
3115 * First, stop submission to hw, but do not yet complete requests by
3116 * rolling the global seqno forward (since this would complete requests
3117 * for which we haven't set the fence error to EIO yet).
3118 */
Chris Wilson20e49332016-11-22 14:41:21 +00003119 for_each_engine(engine, i915, id)
Daniel Vetteraf7a8ff2017-10-11 11:10:19 +02003120 engine->submit_request = nop_submit_request;
3121
3122 /*
3123 * Make sure no one is running the old callback before we proceed with
3124 * cancelling requests and resetting the completion tracking. Otherwise
3125 * we might submit a request to the hardware which never completes.
3126 */
3127 synchronize_rcu();
3128
3129 for_each_engine(engine, i915, id) {
3130 /* Mark all executing requests as skipped */
3131 engine->cancel_requests(engine);
3132
3133 /*
3134 * Only once we've force-cancelled all in-flight requests can we
3135 * start to complete all requests.
3136 */
3137 engine->submit_request = nop_complete_submit_request;
3138 }
3139
3140 /*
3141 * Make sure no request can slip through without getting completed by
3142 * either this call here to intel_engine_init_global_seqno, or the one
3143 * in nop_complete_submit_request.
3144 */
3145 synchronize_rcu();
3146
3147 for_each_engine(engine, i915, id) {
3148 unsigned long flags;
3149
3150 /* Mark all pending requests as complete so that any concurrent
3151 * (lockless) lookup doesn't try and wait upon the request as we
3152 * reset it.
3153 */
3154 spin_lock_irqsave(&engine->timeline->lock, flags);
3155 intel_engine_init_global_seqno(engine,
3156 intel_engine_last_submit(engine));
3157 spin_unlock_irqrestore(&engine->timeline->lock, flags);
3158 }
Chris Wilson20e49332016-11-22 14:41:21 +00003159
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003160 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3161 wake_up_all(&i915->gpu_error.reset_queue);
Eric Anholt673a3942008-07-30 12:06:12 -07003162}
3163
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003164bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3165{
3166 struct i915_gem_timeline *tl;
3167 int i;
3168
3169 lockdep_assert_held(&i915->drm.struct_mutex);
3170 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3171 return true;
3172
3173 /* Before unwedging, make sure that all pending operations
3174 * are flushed and errored out - we may have requests waiting upon
3175 * third party fences. We marked all inflight requests as EIO, and
3176 * every execbuf since returned EIO, for consistency we want all
3177 * the currently pending requests to also be marked as EIO, which
3178 * is done inside our nop_submit_request - and so we must wait.
3179 *
3180 * No more can be submitted until we reset the wedged bit.
3181 */
3182 list_for_each_entry(tl, &i915->gt.timelines, link) {
3183 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3184 struct drm_i915_gem_request *rq;
3185
3186 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3187 &i915->drm.struct_mutex);
3188 if (!rq)
3189 continue;
3190
3191 /* We can't use our normal waiter as we want to
3192 * avoid recursively trying to handle the current
3193 * reset. The basic dma_fence_default_wait() installs
3194 * a callback for dma_fence_signal(), which is
3195 * triggered by our nop handler (indirectly, the
3196 * callback enables the signaler thread which is
3197 * woken by the nop_submit_request() advancing the seqno
3198 * and when the seqno passes the fence, the signaler
3199 * then signals the fence waking us up).
3200 */
3201 if (dma_fence_default_wait(&rq->fence, true,
3202 MAX_SCHEDULE_TIMEOUT) < 0)
3203 return false;
3204 }
3205 }
3206
3207 /* Undo nop_submit_request. We prevent all new i915 requests from
3208 * being queued (by disallowing execbuf whilst wedged) so having
3209 * waited for all active requests above, we know the system is idle
3210 * and do not have to worry about a thread being inside
3211 * engine->submit_request() as we swap over. So unlike installing
3212 * the nop_submit_request on reset, we can do this from normal
3213 * context and do not require stop_machine().
3214 */
3215 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003216 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003217
3218 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3219 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3220
3221 return true;
3222}
3223
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003224static void
Eric Anholt673a3942008-07-30 12:06:12 -07003225i915_gem_retire_work_handler(struct work_struct *work)
3226{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003227 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003228 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003229 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003230
Chris Wilson891b48c2010-09-29 12:26:37 +01003231 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003232 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003233 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003234 mutex_unlock(&dev->struct_mutex);
3235 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003236
3237 /* Keep the retire handler running until we are finally idle.
3238 * We do not need to do this test under locking as in the worst-case
3239 * we queue the retire worker once too often.
3240 */
Chris Wilsonc9615612016-07-09 10:12:06 +01003241 if (READ_ONCE(dev_priv->gt.awake)) {
3242 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01003243 queue_delayed_work(dev_priv->wq,
3244 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003245 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01003246 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003247}
Chris Wilson891b48c2010-09-29 12:26:37 +01003248
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003249static void
3250i915_gem_idle_work_handler(struct work_struct *work)
3251{
3252 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003253 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003254 struct drm_device *dev = &dev_priv->drm;
Chris Wilson67d97da2016-07-04 08:08:31 +01003255 bool rearm_hangcheck;
3256
3257 if (!READ_ONCE(dev_priv->gt.awake))
3258 return;
3259
Imre Deak0cb56702016-11-07 11:20:04 +02003260 /*
3261 * Wait for last execlists context complete, but bail out in case a
3262 * new request is submitted.
3263 */
Chris Wilson8490ae202017-03-30 15:50:37 +01003264 wait_for(intel_engines_are_idle(dev_priv), 10);
Chris Wilson28176ef2016-10-28 13:58:56 +01003265 if (READ_ONCE(dev_priv->gt.active_requests))
Chris Wilson67d97da2016-07-04 08:08:31 +01003266 return;
3267
3268 rearm_hangcheck =
3269 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3270
3271 if (!mutex_trylock(&dev->struct_mutex)) {
3272 /* Currently busy, come back later */
3273 mod_delayed_work(dev_priv->wq,
3274 &dev_priv->gt.idle_work,
3275 msecs_to_jiffies(50));
3276 goto out_rearm;
3277 }
3278
Imre Deak93c97dc2016-11-07 11:20:03 +02003279 /*
3280 * New request retired after this work handler started, extend active
3281 * period until next instance of the work.
3282 */
3283 if (work_pending(work))
3284 goto out_unlock;
3285
Chris Wilson28176ef2016-10-28 13:58:56 +01003286 if (dev_priv->gt.active_requests)
Chris Wilson67d97da2016-07-04 08:08:31 +01003287 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003288
Chris Wilson05425242017-03-03 12:19:47 +00003289 if (wait_for(intel_engines_are_idle(dev_priv), 10))
Imre Deak0cb56702016-11-07 11:20:04 +02003290 DRM_ERROR("Timeout waiting for engines to idle\n");
3291
Chris Wilson6c067572017-05-17 13:10:03 +01003292 intel_engines_mark_idle(dev_priv);
Chris Wilson47979482017-05-03 10:39:21 +01003293 i915_gem_timelines_mark_idle(dev_priv);
Zou Nan hai852835f2010-05-21 09:08:56 +08003294
Chris Wilson67d97da2016-07-04 08:08:31 +01003295 GEM_BUG_ON(!dev_priv->gt.awake);
3296 dev_priv->gt.awake = false;
3297 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003298
Chris Wilson67d97da2016-07-04 08:08:31 +01003299 if (INTEL_GEN(dev_priv) >= 6)
3300 gen6_rps_idle(dev_priv);
3301 intel_runtime_pm_put(dev_priv);
3302out_unlock:
3303 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003304
Chris Wilson67d97da2016-07-04 08:08:31 +01003305out_rearm:
3306 if (rearm_hangcheck) {
3307 GEM_BUG_ON(!dev_priv->gt.awake);
3308 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003309 }
Eric Anholt673a3942008-07-30 12:06:12 -07003310}
3311
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003312void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3313{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003314 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003315 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3316 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003317 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003318
Chris Wilsond1b48c12017-08-16 09:52:08 +01003319 mutex_lock(&i915->drm.struct_mutex);
3320
3321 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3322 struct i915_gem_context *ctx = lut->ctx;
3323 struct i915_vma *vma;
3324
Chris Wilson432295d2017-08-22 12:05:15 +01003325 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003326 if (ctx->file_priv != fpriv)
3327 continue;
3328
3329 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003330 GEM_BUG_ON(vma->obj != obj);
3331
3332 /* We allow the process to have multiple handles to the same
3333 * vma, in the same fd namespace, by virtue of flink/open.
3334 */
3335 GEM_BUG_ON(!vma->open_count);
3336 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003337 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003338
Chris Wilsond1b48c12017-08-16 09:52:08 +01003339 list_del(&lut->obj_link);
3340 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003341
Chris Wilsond1b48c12017-08-16 09:52:08 +01003342 kmem_cache_free(i915->luts, lut);
3343 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003344 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003345
3346 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003347}
3348
Chris Wilsone95433c2016-10-28 13:58:27 +01003349static unsigned long to_wait_timeout(s64 timeout_ns)
3350{
3351 if (timeout_ns < 0)
3352 return MAX_SCHEDULE_TIMEOUT;
3353
3354 if (timeout_ns == 0)
3355 return 0;
3356
3357 return nsecs_to_jiffies_timeout(timeout_ns);
3358}
3359
Ben Widawsky5816d642012-04-11 11:18:19 -07003360/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003361 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003362 * @dev: drm device pointer
3363 * @data: ioctl data blob
3364 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003365 *
3366 * Returns 0 if successful, else an error is returned with the remaining time in
3367 * the timeout parameter.
3368 * -ETIME: object is still busy after timeout
3369 * -ERESTARTSYS: signal interrupted the wait
3370 * -ENONENT: object doesn't exist
3371 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003372 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003373 * -ENOMEM: damn
3374 * -ENODEV: Internal IRQ fail
3375 * -E?: The add request failed
3376 *
3377 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3378 * non-zero timeout parameter the wait ioctl will wait for the given number of
3379 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3380 * without holding struct_mutex the object may become re-busied before this
3381 * function completes. A similar but shorter * race condition exists in the busy
3382 * ioctl
3383 */
3384int
3385i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3386{
3387 struct drm_i915_gem_wait *args = data;
3388 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003389 ktime_t start;
3390 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003391
Daniel Vetter11b5d512014-09-29 15:31:26 +02003392 if (args->flags != 0)
3393 return -EINVAL;
3394
Chris Wilson03ac0642016-07-20 13:31:51 +01003395 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003396 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003397 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003398
Chris Wilsone95433c2016-10-28 13:58:27 +01003399 start = ktime_get();
3400
3401 ret = i915_gem_object_wait(obj,
3402 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3403 to_wait_timeout(args->timeout_ns),
3404 to_rps_client(file));
3405
3406 if (args->timeout_ns > 0) {
3407 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3408 if (args->timeout_ns < 0)
3409 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003410
3411 /*
3412 * Apparently ktime isn't accurate enough and occasionally has a
3413 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3414 * things up to make the test happy. We allow up to 1 jiffy.
3415 *
3416 * This is a regression from the timespec->ktime conversion.
3417 */
3418 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3419 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003420
3421 /* Asked to wait beyond the jiffie/scheduler precision? */
3422 if (ret == -ETIME && args->timeout_ns)
3423 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003424 }
3425
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003426 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003427 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003428}
3429
Chris Wilson73cb9702016-10-28 13:58:46 +01003430static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003431{
Chris Wilson73cb9702016-10-28 13:58:46 +01003432 int ret, i;
3433
3434 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3435 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3436 if (ret)
3437 return ret;
3438 }
3439
3440 return 0;
3441}
3442
Chris Wilson25112b62017-03-30 15:50:39 +01003443static int wait_for_engines(struct drm_i915_private *i915)
3444{
Chris Wilsoncad99462017-08-26 12:09:33 +01003445 if (wait_for(intel_engines_are_idle(i915), 50)) {
3446 DRM_ERROR("Failed to idle engines, declaring wedged!\n");
3447 i915_gem_set_wedged(i915);
3448 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003449 }
3450
3451 return 0;
3452}
3453
Chris Wilson73cb9702016-10-28 13:58:46 +01003454int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3455{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003456 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003457
Chris Wilson863e9fd2017-05-30 13:13:32 +01003458 /* If the device is asleep, we have no requests outstanding */
3459 if (!READ_ONCE(i915->gt.awake))
3460 return 0;
3461
Chris Wilson9caa34a2016-11-11 14:58:08 +00003462 if (flags & I915_WAIT_LOCKED) {
3463 struct i915_gem_timeline *tl;
3464
3465 lockdep_assert_held(&i915->drm.struct_mutex);
3466
3467 list_for_each_entry(tl, &i915->gt.timelines, link) {
3468 ret = wait_for_timeline(tl, flags);
3469 if (ret)
3470 return ret;
3471 }
Chris Wilson72022a72017-03-30 15:50:38 +01003472
3473 i915_gem_retire_requests(i915);
3474 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003475
3476 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003477 } else {
3478 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003479 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003480
Chris Wilson25112b62017-03-30 15:50:39 +01003481 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003482}
3483
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003484static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3485{
Chris Wilsone27ab732017-06-15 13:38:49 +01003486 /*
3487 * We manually flush the CPU domain so that we can override and
3488 * force the flush for the display, and perform it asyncrhonously.
3489 */
3490 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3491 if (obj->cache_dirty)
3492 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003493 obj->base.write_domain = 0;
3494}
3495
3496void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3497{
3498 if (!READ_ONCE(obj->pin_display))
3499 return;
3500
3501 mutex_lock(&obj->base.dev->struct_mutex);
3502 __i915_gem_object_flush_for_display(obj);
3503 mutex_unlock(&obj->base.dev->struct_mutex);
3504}
3505
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003506/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003507 * Moves a single object to the WC read, and possibly write domain.
3508 * @obj: object to act on
3509 * @write: ask for write access or read only
3510 *
3511 * This function returns when the move is complete, including waiting on
3512 * flushes to occur.
3513 */
3514int
3515i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3516{
3517 int ret;
3518
3519 lockdep_assert_held(&obj->base.dev->struct_mutex);
3520
3521 ret = i915_gem_object_wait(obj,
3522 I915_WAIT_INTERRUPTIBLE |
3523 I915_WAIT_LOCKED |
3524 (write ? I915_WAIT_ALL : 0),
3525 MAX_SCHEDULE_TIMEOUT,
3526 NULL);
3527 if (ret)
3528 return ret;
3529
3530 if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
3531 return 0;
3532
3533 /* Flush and acquire obj->pages so that we are coherent through
3534 * direct access in memory with previous cached writes through
3535 * shmemfs and that our cache domain tracking remains valid.
3536 * For example, if the obj->filp was moved to swap without us
3537 * being notified and releasing the pages, we would mistakenly
3538 * continue to assume that the obj remained out of the CPU cached
3539 * domain.
3540 */
3541 ret = i915_gem_object_pin_pages(obj);
3542 if (ret)
3543 return ret;
3544
3545 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3546
3547 /* Serialise direct access to this object with the barriers for
3548 * coherent writes from the GPU, by effectively invalidating the
3549 * WC domain upon first access.
3550 */
3551 if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
3552 mb();
3553
3554 /* It should now be out of any other write domains, and we can update
3555 * the domain values for our changes.
3556 */
3557 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3558 obj->base.read_domains |= I915_GEM_DOMAIN_WC;
3559 if (write) {
3560 obj->base.read_domains = I915_GEM_DOMAIN_WC;
3561 obj->base.write_domain = I915_GEM_DOMAIN_WC;
3562 obj->mm.dirty = true;
3563 }
3564
3565 i915_gem_object_unpin_pages(obj);
3566 return 0;
3567}
3568
3569/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003570 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003571 * @obj: object to act on
3572 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003573 *
3574 * This function returns when the move is complete, including waiting on
3575 * flushes to occur.
3576 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003577int
Chris Wilson20217462010-11-23 15:26:33 +00003578i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003579{
Eric Anholte47c68e2008-11-14 13:35:19 -08003580 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003581
Chris Wilsone95433c2016-10-28 13:58:27 +01003582 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003583
Chris Wilsone95433c2016-10-28 13:58:27 +01003584 ret = i915_gem_object_wait(obj,
3585 I915_WAIT_INTERRUPTIBLE |
3586 I915_WAIT_LOCKED |
3587 (write ? I915_WAIT_ALL : 0),
3588 MAX_SCHEDULE_TIMEOUT,
3589 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003590 if (ret)
3591 return ret;
3592
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003593 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3594 return 0;
3595
Chris Wilson43566de2015-01-02 16:29:29 +05303596 /* Flush and acquire obj->pages so that we are coherent through
3597 * direct access in memory with previous cached writes through
3598 * shmemfs and that our cache domain tracking remains valid.
3599 * For example, if the obj->filp was moved to swap without us
3600 * being notified and releasing the pages, we would mistakenly
3601 * continue to assume that the obj remained out of the CPU cached
3602 * domain.
3603 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003604 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303605 if (ret)
3606 return ret;
3607
Chris Wilsonef749212017-04-12 12:01:10 +01003608 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003609
Chris Wilsond0a57782012-10-09 19:24:37 +01003610 /* Serialise direct access to this object with the barriers for
3611 * coherent writes from the GPU, by effectively invalidating the
3612 * GTT domain upon first access.
3613 */
3614 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3615 mb();
3616
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003617 /* It should now be out of any other write domains, and we can update
3618 * the domain values for our changes.
3619 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003620 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003621 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003622 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003623 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3624 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003625 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003626 }
3627
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003628 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003629 return 0;
3630}
3631
Chris Wilsonef55f922015-10-09 14:11:27 +01003632/**
3633 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003634 * @obj: object to act on
3635 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003636 *
3637 * After this function returns, the object will be in the new cache-level
3638 * across all GTT and the contents of the backing storage will be coherent,
3639 * with respect to the new cache-level. In order to keep the backing storage
3640 * coherent for all users, we only allow a single cache level to be set
3641 * globally on the object and prevent it from being changed whilst the
3642 * hardware is reading from the object. That is if the object is currently
3643 * on the scanout it will be set to uncached (or equivalent display
3644 * cache coherency) and all non-MOCS GPU access will also be uncached so
3645 * that all direct access to the scanout remains coherent.
3646 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003647int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3648 enum i915_cache_level cache_level)
3649{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003650 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003651 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003652
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003653 lockdep_assert_held(&obj->base.dev->struct_mutex);
3654
Chris Wilsone4ffd172011-04-04 09:44:39 +01003655 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003656 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003657
Chris Wilsonef55f922015-10-09 14:11:27 +01003658 /* Inspect the list of currently bound VMA and unbind any that would
3659 * be invalid given the new cache-level. This is principally to
3660 * catch the issue of the CS prefetch crossing page boundaries and
3661 * reading an invalid PTE on older architectures.
3662 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003663restart:
3664 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003665 if (!drm_mm_node_allocated(&vma->node))
3666 continue;
3667
Chris Wilson20dfbde2016-08-04 16:32:30 +01003668 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003669 DRM_DEBUG("can not change the cache level of pinned objects\n");
3670 return -EBUSY;
3671 }
3672
Chris Wilsonaa653a62016-08-04 07:52:27 +01003673 if (i915_gem_valid_gtt_space(vma, cache_level))
3674 continue;
3675
3676 ret = i915_vma_unbind(vma);
3677 if (ret)
3678 return ret;
3679
3680 /* As unbinding may affect other elements in the
3681 * obj->vma_list (due to side-effects from retiring
3682 * an active vma), play safe and restart the iterator.
3683 */
3684 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003685 }
3686
Chris Wilsonef55f922015-10-09 14:11:27 +01003687 /* We can reuse the existing drm_mm nodes but need to change the
3688 * cache-level on the PTE. We could simply unbind them all and
3689 * rebind with the correct cache-level on next use. However since
3690 * we already have a valid slot, dma mapping, pages etc, we may as
3691 * rewrite the PTE in the belief that doing so tramples upon less
3692 * state and so involves less work.
3693 */
Chris Wilson15717de2016-08-04 07:52:26 +01003694 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003695 /* Before we change the PTE, the GPU must not be accessing it.
3696 * If we wait upon the object, we know that all the bound
3697 * VMA are no longer active.
3698 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003699 ret = i915_gem_object_wait(obj,
3700 I915_WAIT_INTERRUPTIBLE |
3701 I915_WAIT_LOCKED |
3702 I915_WAIT_ALL,
3703 MAX_SCHEDULE_TIMEOUT,
3704 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003705 if (ret)
3706 return ret;
3707
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003708 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3709 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003710 /* Access to snoopable pages through the GTT is
3711 * incoherent and on some machines causes a hard
3712 * lockup. Relinquish the CPU mmaping to force
3713 * userspace to refault in the pages and we can
3714 * then double check if the GTT mapping is still
3715 * valid for that pointer access.
3716 */
3717 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003718
Chris Wilsonef55f922015-10-09 14:11:27 +01003719 /* As we no longer need a fence for GTT access,
3720 * we can relinquish it now (and so prevent having
3721 * to steal a fence from someone else on the next
3722 * fence request). Note GPU activity would have
3723 * dropped the fence as all snoopable access is
3724 * supposed to be linear.
3725 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003726 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3727 ret = i915_vma_put_fence(vma);
3728 if (ret)
3729 return ret;
3730 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003731 } else {
3732 /* We either have incoherent backing store and
3733 * so no GTT access or the architecture is fully
3734 * coherent. In such cases, existing GTT mmaps
3735 * ignore the cache bit in the PTE and we can
3736 * rewrite it without confusing the GPU or having
3737 * to force userspace to fault back in its mmaps.
3738 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003739 }
3740
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003741 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003742 if (!drm_mm_node_allocated(&vma->node))
3743 continue;
3744
3745 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3746 if (ret)
3747 return ret;
3748 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003749 }
3750
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003751 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003752 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003753 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003754 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003755
Chris Wilsone4ffd172011-04-04 09:44:39 +01003756 return 0;
3757}
3758
Ben Widawsky199adf42012-09-21 17:01:20 -07003759int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3760 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003761{
Ben Widawsky199adf42012-09-21 17:01:20 -07003762 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003763 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003764 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003765
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003766 rcu_read_lock();
3767 obj = i915_gem_object_lookup_rcu(file, args->handle);
3768 if (!obj) {
3769 err = -ENOENT;
3770 goto out;
3771 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003772
Chris Wilson651d7942013-08-08 14:41:10 +01003773 switch (obj->cache_level) {
3774 case I915_CACHE_LLC:
3775 case I915_CACHE_L3_LLC:
3776 args->caching = I915_CACHING_CACHED;
3777 break;
3778
Chris Wilson4257d3b2013-08-08 14:41:11 +01003779 case I915_CACHE_WT:
3780 args->caching = I915_CACHING_DISPLAY;
3781 break;
3782
Chris Wilson651d7942013-08-08 14:41:10 +01003783 default:
3784 args->caching = I915_CACHING_NONE;
3785 break;
3786 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003787out:
3788 rcu_read_unlock();
3789 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003790}
3791
Ben Widawsky199adf42012-09-21 17:01:20 -07003792int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3793 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003794{
Chris Wilson9c870d02016-10-24 13:42:15 +01003795 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003796 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003797 struct drm_i915_gem_object *obj;
3798 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003799 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003800
Ben Widawsky199adf42012-09-21 17:01:20 -07003801 switch (args->caching) {
3802 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003803 level = I915_CACHE_NONE;
3804 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003805 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003806 /*
3807 * Due to a HW issue on BXT A stepping, GPU stores via a
3808 * snooped mapping may leave stale data in a corresponding CPU
3809 * cacheline, whereas normally such cachelines would get
3810 * invalidated.
3811 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003812 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003813 return -ENODEV;
3814
Chris Wilsone6994ae2012-07-10 10:27:08 +01003815 level = I915_CACHE_LLC;
3816 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003817 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003818 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003819 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003820 default:
3821 return -EINVAL;
3822 }
3823
Chris Wilsond65415d2017-01-19 08:22:10 +00003824 obj = i915_gem_object_lookup(file, args->handle);
3825 if (!obj)
3826 return -ENOENT;
3827
3828 if (obj->cache_level == level)
3829 goto out;
3830
3831 ret = i915_gem_object_wait(obj,
3832 I915_WAIT_INTERRUPTIBLE,
3833 MAX_SCHEDULE_TIMEOUT,
3834 to_rps_client(file));
3835 if (ret)
3836 goto out;
3837
Ben Widawsky3bc29132012-09-26 16:15:20 -07003838 ret = i915_mutex_lock_interruptible(dev);
3839 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003840 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003841
3842 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003843 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003844
3845out:
3846 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003847 return ret;
3848}
3849
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003850/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003851 * Prepare buffer for display plane (scanout, cursors, etc).
3852 * Can be called from an uninterruptible phase (modesetting) and allows
3853 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003854 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003855struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003856i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3857 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003858 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003859{
Chris Wilson058d88c2016-08-15 10:49:06 +01003860 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003861 int ret;
3862
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003863 lockdep_assert_held(&obj->base.dev->struct_mutex);
3864
Chris Wilsoncc98b412013-08-09 12:25:09 +01003865 /* Mark the pin_display early so that we account for the
3866 * display coherency whilst setting up the cache domains.
3867 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003868 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003869
Eric Anholta7ef0642011-03-29 16:59:54 -07003870 /* The display engine is not coherent with the LLC cache on gen6. As
3871 * a result, we make sure that the pinning that is about to occur is
3872 * done with uncached PTEs. This is lowest common denominator for all
3873 * chipsets.
3874 *
3875 * However for gen6+, we could do better by using the GFDT bit instead
3876 * of uncaching, which would allow us to flush all the LLC-cached data
3877 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3878 */
Chris Wilson651d7942013-08-08 14:41:10 +01003879 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003880 HAS_WT(to_i915(obj->base.dev)) ?
3881 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003882 if (ret) {
3883 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003884 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003885 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003886
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003887 /* As the user may map the buffer once pinned in the display plane
3888 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003889 * always use map_and_fenceable for all scanout buffers. However,
3890 * it may simply be too big to fit into mappable, in which case
3891 * put it anyway and hope that userspace can cope (but always first
3892 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003893 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003894 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003895 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003896 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3897 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003898 if (IS_ERR(vma)) {
3899 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3900 unsigned int flags;
3901
3902 /* Valleyview is definitely limited to scanning out the first
3903 * 512MiB. Lets presume this behaviour was inherited from the
3904 * g4x display engine and that all earlier gen are similarly
3905 * limited. Testing suggests that it is a little more
3906 * complicated than this. For example, Cherryview appears quite
3907 * happy to scanout from anywhere within its global aperture.
3908 */
3909 flags = 0;
3910 if (HAS_GMCH_DISPLAY(i915))
3911 flags = PIN_MAPPABLE;
3912 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
3913 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003914 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003915 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003916
Chris Wilsond8923dc2016-08-18 17:17:07 +01003917 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3918
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003919 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003920 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00003921 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003922
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003923 /* It should now be out of any other write domains, and we can update
3924 * the domain values for our changes.
3925 */
Chris Wilson05394f32010-11-08 19:18:58 +00003926 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003927
Chris Wilson058d88c2016-08-15 10:49:06 +01003928 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003929
3930err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003931 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003932 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003933}
3934
3935void
Chris Wilson058d88c2016-08-15 10:49:06 +01003936i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003937{
Chris Wilson49d73912016-11-29 09:50:08 +00003938 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003939
Chris Wilson058d88c2016-08-15 10:49:06 +01003940 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003941 return;
3942
Chris Wilsond8923dc2016-08-18 17:17:07 +01003943 if (--vma->obj->pin_display == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003944 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003945
Chris Wilson383d5822016-08-18 17:17:08 +01003946 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003947 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003948
Chris Wilson058d88c2016-08-15 10:49:06 +01003949 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003950}
3951
Eric Anholte47c68e2008-11-14 13:35:19 -08003952/**
3953 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003954 * @obj: object to act on
3955 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003956 *
3957 * This function returns when the move is complete, including waiting on
3958 * flushes to occur.
3959 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003960int
Chris Wilson919926a2010-11-12 13:42:53 +00003961i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003962{
Eric Anholte47c68e2008-11-14 13:35:19 -08003963 int ret;
3964
Chris Wilsone95433c2016-10-28 13:58:27 +01003965 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003966
Chris Wilsone95433c2016-10-28 13:58:27 +01003967 ret = i915_gem_object_wait(obj,
3968 I915_WAIT_INTERRUPTIBLE |
3969 I915_WAIT_LOCKED |
3970 (write ? I915_WAIT_ALL : 0),
3971 MAX_SCHEDULE_TIMEOUT,
3972 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003973 if (ret)
3974 return ret;
3975
Chris Wilsonef749212017-04-12 12:01:10 +01003976 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003977
Eric Anholte47c68e2008-11-14 13:35:19 -08003978 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003979 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003980 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00003981 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003982 }
3983
3984 /* It should now be out of any other write domains, and we can update
3985 * the domain values for our changes.
3986 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003987 GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003988
3989 /* If we're writing through the CPU, then the GPU read domains will
3990 * need to be invalidated at next use.
3991 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003992 if (write)
3993 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003994
3995 return 0;
3996}
3997
Eric Anholt673a3942008-07-30 12:06:12 -07003998/* Throttle our rendering by waiting until the ring has completed our requests
3999 * emitted over 20 msec ago.
4000 *
Eric Anholtb9624422009-06-03 07:27:35 +00004001 * Note that if we were to use the current jiffies each time around the loop,
4002 * we wouldn't escape the function with any frames outstanding if the time to
4003 * render a frame was over 20ms.
4004 *
Eric Anholt673a3942008-07-30 12:06:12 -07004005 * This should get us reasonable parallelism between CPU and GPU but also
4006 * relatively low latency when blocking on a particular request to finish.
4007 */
4008static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004009i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004010{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004011 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004012 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004013 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00004014 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01004015 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004016
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004017 /* ABI: return -EIO if already wedged */
4018 if (i915_terminally_wedged(&dev_priv->gpu_error))
4019 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004020
Chris Wilson1c255952010-09-26 11:03:27 +01004021 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004022 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004023 if (time_after_eq(request->emitted_jiffies, recent_enough))
4024 break;
4025
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004026 if (target) {
4027 list_del(&target->client_link);
4028 target->file_priv = NULL;
4029 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004030
John Harrison54fb2412014-11-24 18:49:27 +00004031 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004032 }
John Harrisonff865882014-11-24 18:49:28 +00004033 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01004034 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004035 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004036
John Harrison54fb2412014-11-24 18:49:27 +00004037 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004038 return 0;
4039
Chris Wilsone95433c2016-10-28 13:58:27 +01004040 ret = i915_wait_request(target,
4041 I915_WAIT_INTERRUPTIBLE,
4042 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01004043 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004044
Chris Wilsone95433c2016-10-28 13:58:27 +01004045 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004046}
4047
Chris Wilson058d88c2016-08-15 10:49:06 +01004048struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004049i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4050 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004051 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004052 u64 alignment,
4053 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004054{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004055 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4056 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004057 struct i915_vma *vma;
4058 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004059
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004060 lockdep_assert_held(&obj->base.dev->struct_mutex);
4061
Chris Wilson43ae70d2017-10-09 09:44:01 +01004062 if (!view && flags & PIN_MAPPABLE) {
4063 /* If the required space is larger than the available
4064 * aperture, we will not able to find a slot for the
4065 * object and unbinding the object now will be in
4066 * vain. Worse, doing so may cause us to ping-pong
4067 * the object in and out of the Global GTT and
4068 * waste a lot of cycles under the mutex.
4069 */
4070 if (obj->base.size > dev_priv->ggtt.mappable_end)
4071 return ERR_PTR(-E2BIG);
4072
4073 /* If NONBLOCK is set the caller is optimistically
4074 * trying to cache the full object within the mappable
4075 * aperture, and *must* have a fallback in place for
4076 * situations where we cannot bind the object. We
4077 * can be a little more lax here and use the fallback
4078 * more often to avoid costly migrations of ourselves
4079 * and other objects within the aperture.
4080 *
4081 * Half-the-aperture is used as a simple heuristic.
4082 * More interesting would to do search for a free
4083 * block prior to making the commitment to unbind.
4084 * That caters for the self-harm case, and with a
4085 * little more heuristics (e.g. NOFAULT, NOEVICT)
4086 * we could try to minimise harm to others.
4087 */
4088 if (flags & PIN_NONBLOCK &&
4089 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4090 return ERR_PTR(-ENOSPC);
4091 }
4092
Chris Wilson718659a2017-01-16 15:21:28 +00004093 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004094 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004095 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004096
4097 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004098 if (flags & PIN_NONBLOCK) {
4099 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4100 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004101
Chris Wilson43ae70d2017-10-09 09:44:01 +01004102 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004103 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004104 return ERR_PTR(-ENOSPC);
4105 }
4106
Chris Wilson59bfa122016-08-04 16:32:31 +01004107 WARN(i915_vma_is_pinned(vma),
4108 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004109 " offset=%08x, req.alignment=%llx,"
4110 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4111 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004112 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004113 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004114 ret = i915_vma_unbind(vma);
4115 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004116 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004117 }
4118
Chris Wilson058d88c2016-08-15 10:49:06 +01004119 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4120 if (ret)
4121 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004122
Chris Wilson058d88c2016-08-15 10:49:06 +01004123 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004124}
4125
Chris Wilsonedf6b762016-08-09 09:23:33 +01004126static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004127{
4128 /* Note that we could alias engines in the execbuf API, but
4129 * that would be very unwise as it prevents userspace from
4130 * fine control over engine selection. Ahem.
4131 *
4132 * This should be something like EXEC_MAX_ENGINE instead of
4133 * I915_NUM_ENGINES.
4134 */
4135 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4136 return 0x10000 << id;
4137}
4138
4139static __always_inline unsigned int __busy_write_id(unsigned int id)
4140{
Chris Wilson70cb4722016-08-09 18:08:25 +01004141 /* The uABI guarantees an active writer is also amongst the read
4142 * engines. This would be true if we accessed the activity tracking
4143 * under the lock, but as we perform the lookup of the object and
4144 * its activity locklessly we can not guarantee that the last_write
4145 * being active implies that we have set the same engine flag from
4146 * last_read - hence we always set both read and write busy for
4147 * last_write.
4148 */
4149 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004150}
4151
Chris Wilsonedf6b762016-08-09 09:23:33 +01004152static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004153__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004154 unsigned int (*flag)(unsigned int id))
4155{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004156 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004157
Chris Wilsond07f0e52016-10-28 13:58:44 +01004158 /* We have to check the current hw status of the fence as the uABI
4159 * guarantees forward progress. We could rely on the idle worker
4160 * to eventually flush us, but to minimise latency just ask the
4161 * hardware.
4162 *
4163 * Note we only report on the status of native fences.
4164 */
4165 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004166 return 0;
4167
Chris Wilsond07f0e52016-10-28 13:58:44 +01004168 /* opencode to_request() in order to avoid const warnings */
4169 rq = container_of(fence, struct drm_i915_gem_request, fence);
4170 if (i915_gem_request_completed(rq))
4171 return 0;
4172
Chris Wilson1d39f282017-04-11 13:43:06 +01004173 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004174}
4175
Chris Wilsonedf6b762016-08-09 09:23:33 +01004176static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004177busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004178{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004179 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004180}
4181
Chris Wilsonedf6b762016-08-09 09:23:33 +01004182static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004183busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004184{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004185 if (!fence)
4186 return 0;
4187
4188 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004189}
4190
Eric Anholt673a3942008-07-30 12:06:12 -07004191int
Eric Anholt673a3942008-07-30 12:06:12 -07004192i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004193 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004194{
4195 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004196 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004197 struct reservation_object_list *list;
4198 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004199 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004200
Chris Wilsond07f0e52016-10-28 13:58:44 +01004201 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004202 rcu_read_lock();
4203 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004204 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004205 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004206
4207 /* A discrepancy here is that we do not report the status of
4208 * non-i915 fences, i.e. even though we may report the object as idle,
4209 * a call to set-domain may still stall waiting for foreign rendering.
4210 * This also means that wait-ioctl may report an object as busy,
4211 * where busy-ioctl considers it idle.
4212 *
4213 * We trade the ability to warn of foreign fences to report on which
4214 * i915 engines are active for the object.
4215 *
4216 * Alternatively, we can trade that extra information on read/write
4217 * activity with
4218 * args->busy =
4219 * !reservation_object_test_signaled_rcu(obj->resv, true);
4220 * to report the overall busyness. This is what the wait-ioctl does.
4221 *
4222 */
4223retry:
4224 seq = raw_read_seqcount(&obj->resv->seq);
4225
4226 /* Translate the exclusive fence to the READ *and* WRITE engine */
4227 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4228
4229 /* Translate shared fences to READ set of engines */
4230 list = rcu_dereference(obj->resv->fence);
4231 if (list) {
4232 unsigned int shared_count = list->shared_count, i;
4233
4234 for (i = 0; i < shared_count; ++i) {
4235 struct dma_fence *fence =
4236 rcu_dereference(list->shared[i]);
4237
4238 args->busy |= busy_check_reader(fence);
4239 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004240 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004241
Chris Wilsond07f0e52016-10-28 13:58:44 +01004242 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4243 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004244
Chris Wilsond07f0e52016-10-28 13:58:44 +01004245 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004246out:
4247 rcu_read_unlock();
4248 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004249}
4250
4251int
4252i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4253 struct drm_file *file_priv)
4254{
Akshay Joshi0206e352011-08-16 15:34:10 -04004255 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004256}
4257
Chris Wilson3ef94da2009-09-14 16:50:29 +01004258int
4259i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4260 struct drm_file *file_priv)
4261{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004262 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004263 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004264 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004265 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004266
4267 switch (args->madv) {
4268 case I915_MADV_DONTNEED:
4269 case I915_MADV_WILLNEED:
4270 break;
4271 default:
4272 return -EINVAL;
4273 }
4274
Chris Wilson03ac0642016-07-20 13:31:51 +01004275 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004276 if (!obj)
4277 return -ENOENT;
4278
4279 err = mutex_lock_interruptible(&obj->mm.lock);
4280 if (err)
4281 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004282
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004283 if (obj->mm.pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004284 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004285 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004286 if (obj->mm.madv == I915_MADV_WILLNEED) {
4287 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004288 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004289 obj->mm.quirked = false;
4290 }
4291 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004292 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004293 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004294 obj->mm.quirked = true;
4295 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004296 }
4297
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004298 if (obj->mm.madv != __I915_MADV_PURGED)
4299 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004300
Chris Wilson6c085a72012-08-20 11:40:46 +02004301 /* if the object is no longer attached, discard its backing storage */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004302 if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004303 i915_gem_object_truncate(obj);
4304
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004305 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004306 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004307
Chris Wilson1233e2d2016-10-28 13:58:37 +01004308out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004309 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004310 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004311}
4312
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004313static void
4314frontbuffer_retire(struct i915_gem_active *active,
4315 struct drm_i915_gem_request *request)
4316{
4317 struct drm_i915_gem_object *obj =
4318 container_of(active, typeof(*obj), frontbuffer_write);
4319
Chris Wilsond59b21e2017-02-22 11:40:49 +00004320 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004321}
4322
Chris Wilson37e680a2012-06-07 15:38:42 +01004323void i915_gem_object_init(struct drm_i915_gem_object *obj,
4324 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004325{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004326 mutex_init(&obj->mm.lock);
4327
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004328 INIT_LIST_HEAD(&obj->global_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004329 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004330 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004331 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004332
Chris Wilson37e680a2012-06-07 15:38:42 +01004333 obj->ops = ops;
4334
Chris Wilsond07f0e52016-10-28 13:58:44 +01004335 reservation_object_init(&obj->__builtin_resv);
4336 obj->resv = &obj->__builtin_resv;
4337
Chris Wilson50349242016-08-18 17:17:04 +01004338 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004339 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004340
4341 obj->mm.madv = I915_MADV_WILLNEED;
4342 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4343 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004344
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004345 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004346}
4347
Chris Wilson37e680a2012-06-07 15:38:42 +01004348static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004349 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4350 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004351
Chris Wilson37e680a2012-06-07 15:38:42 +01004352 .get_pages = i915_gem_object_get_pages_gtt,
4353 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004354
4355 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004356};
4357
Matthew Auld465c4032017-10-06 23:18:14 +01004358static int i915_gem_object_create_shmem(struct drm_device *dev,
4359 struct drm_gem_object *obj,
4360 size_t size)
4361{
4362 struct drm_i915_private *i915 = to_i915(dev);
4363 unsigned long flags = VM_NORESERVE;
4364 struct file *filp;
4365
4366 drm_gem_private_object_init(dev, obj, size);
4367
4368 if (i915->mm.gemfs)
4369 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4370 flags);
4371 else
4372 filp = shmem_file_setup("i915", size, flags);
4373
4374 if (IS_ERR(filp))
4375 return PTR_ERR(filp);
4376
4377 obj->filp = filp;
4378
4379 return 0;
4380}
4381
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004382struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004383i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004384{
Daniel Vetterc397b902010-04-09 19:05:07 +00004385 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004386 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004387 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004388 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004389 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004390
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004391 /* There is a prevalence of the assumption that we fit the object's
4392 * page count inside a 32bit _signed_ variable. Let's document this and
4393 * catch if we ever need to fix it. In the meantime, if you do spot
4394 * such a local variable, please consider fixing!
4395 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004396 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004397 return ERR_PTR(-E2BIG);
4398
4399 if (overflows_type(size, obj->base.size))
4400 return ERR_PTR(-E2BIG);
4401
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004402 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004403 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004404 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004405
Matthew Auld465c4032017-10-06 23:18:14 +01004406 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004407 if (ret)
4408 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004409
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004410 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004411 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004412 /* 965gm cannot relocate objects above 4GiB. */
4413 mask &= ~__GFP_HIGHMEM;
4414 mask |= __GFP_DMA32;
4415 }
4416
Al Viro93c76a32015-12-04 23:45:44 -05004417 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004418 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004419 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004420
Chris Wilson37e680a2012-06-07 15:38:42 +01004421 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004422
Daniel Vetterc397b902010-04-09 19:05:07 +00004423 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4424 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4425
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004426 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004427 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004428 * cache) for about a 10% performance improvement
4429 * compared to uncached. Graphics requests other than
4430 * display scanout are coherent with the CPU in
4431 * accessing this cache. This means in this mode we
4432 * don't need to clflush on the CPU side, and on the
4433 * GPU side we only need to flush internal caches to
4434 * get data visible to the CPU.
4435 *
4436 * However, we maintain the display planes as UC, and so
4437 * need to rebind when first used as such.
4438 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004439 cache_level = I915_CACHE_LLC;
4440 else
4441 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004442
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004443 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004444
Daniel Vetterd861e332013-07-24 23:25:03 +02004445 trace_i915_gem_object_create(obj);
4446
Chris Wilson05394f32010-11-08 19:18:58 +00004447 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004448
4449fail:
4450 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004451 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004452}
4453
Chris Wilson340fbd82014-05-22 09:16:52 +01004454static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4455{
4456 /* If we are the last user of the backing storage (be it shmemfs
4457 * pages or stolen etc), we know that the pages are going to be
4458 * immediately released. In this case, we can then skip copying
4459 * back the contents from the GPU.
4460 */
4461
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004462 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004463 return false;
4464
4465 if (obj->base.filp == NULL)
4466 return true;
4467
4468 /* At first glance, this looks racy, but then again so would be
4469 * userspace racing mmap against close. However, the first external
4470 * reference to the filp can only be obtained through the
4471 * i915_gem_mmap_ioctl() which safeguards us against the user
4472 * acquiring such a reference whilst we are in the middle of
4473 * freeing the object.
4474 */
4475 return atomic_long_read(&obj->base.filp->f_count) == 1;
4476}
4477
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004478static void __i915_gem_free_objects(struct drm_i915_private *i915,
4479 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004480{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004481 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004482
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004483 mutex_lock(&i915->drm.struct_mutex);
4484 intel_runtime_pm_get(i915);
4485 llist_for_each_entry(obj, freed, freed) {
4486 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004487
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004488 trace_i915_gem_object_destroy(obj);
4489
4490 GEM_BUG_ON(i915_gem_object_is_active(obj));
4491 list_for_each_entry_safe(vma, vn,
4492 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004493 GEM_BUG_ON(i915_vma_is_active(vma));
4494 vma->flags &= ~I915_VMA_PIN_MASK;
4495 i915_vma_close(vma);
4496 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004497 GEM_BUG_ON(!list_empty(&obj->vma_list));
4498 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004499
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004500 list_del(&obj->global_link);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004501 }
4502 intel_runtime_pm_put(i915);
4503 mutex_unlock(&i915->drm.struct_mutex);
4504
Chris Wilsonf2be9d62017-04-07 11:25:52 +01004505 cond_resched();
4506
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004507 llist_for_each_entry_safe(obj, on, freed, freed) {
4508 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004509 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004510 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004511 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004512
4513 if (obj->ops->release)
4514 obj->ops->release(obj);
4515
4516 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4517 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004518 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004519 GEM_BUG_ON(obj->mm.pages);
4520
4521 if (obj->base.import_attach)
4522 drm_prime_gem_destroy(&obj->base, NULL);
4523
Chris Wilsond07f0e52016-10-28 13:58:44 +01004524 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004525 drm_gem_object_release(&obj->base);
4526 i915_gem_info_remove_obj(i915, obj->base.size);
4527
4528 kfree(obj->bit_17);
4529 i915_gem_object_free(obj);
4530 }
4531}
4532
4533static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4534{
4535 struct llist_node *freed;
4536
4537 freed = llist_del_all(&i915->mm.free_list);
4538 if (unlikely(freed))
4539 __i915_gem_free_objects(i915, freed);
4540}
4541
4542static void __i915_gem_free_work(struct work_struct *work)
4543{
4544 struct drm_i915_private *i915 =
4545 container_of(work, struct drm_i915_private, mm.free_work);
4546 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004547
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004548 /* All file-owned VMA should have been released by this point through
4549 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4550 * However, the object may also be bound into the global GTT (e.g.
4551 * older GPUs without per-process support, or for direct access through
4552 * the GTT either for the user or for scanout). Those VMA still need to
4553 * unbound now.
4554 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004555
Chris Wilson5ad08be2017-04-07 11:25:51 +01004556 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004557 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004558 if (need_resched())
4559 break;
4560 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004561}
4562
4563static void __i915_gem_free_object_rcu(struct rcu_head *head)
4564{
4565 struct drm_i915_gem_object *obj =
4566 container_of(head, typeof(*obj), rcu);
4567 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4568
4569 /* We can't simply use call_rcu() from i915_gem_free_object()
4570 * as we need to block whilst unbinding, and the call_rcu
4571 * task may be called from softirq context. So we take a
4572 * detour through a worker.
4573 */
4574 if (llist_add(&obj->freed, &i915->mm.free_list))
4575 schedule_work(&i915->mm.free_work);
4576}
4577
4578void i915_gem_free_object(struct drm_gem_object *gem_obj)
4579{
4580 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4581
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004582 if (obj->mm.quirked)
4583 __i915_gem_object_unpin_pages(obj);
4584
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004585 if (discard_backing_storage(obj))
4586 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004587
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004588 /* Before we free the object, make sure any pure RCU-only
4589 * read-side critical sections are complete, e.g.
4590 * i915_gem_busy_ioctl(). For the corresponding synchronized
4591 * lookup see i915_gem_object_lookup_rcu().
4592 */
4593 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004594}
4595
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004596void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4597{
4598 lockdep_assert_held(&obj->base.dev->struct_mutex);
4599
Chris Wilsond1b48c12017-08-16 09:52:08 +01004600 if (!i915_gem_object_has_active_reference(obj) &&
4601 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004602 i915_gem_object_set_active_reference(obj);
4603 else
4604 i915_gem_object_put(obj);
4605}
4606
Chris Wilson3033aca2016-10-28 13:58:47 +01004607static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
4608{
4609 struct intel_engine_cs *engine;
4610 enum intel_engine_id id;
4611
4612 for_each_engine(engine, dev_priv, id)
Chris Wilsonf131e352016-12-29 14:40:37 +00004613 GEM_BUG_ON(engine->last_retired_context &&
4614 !i915_gem_context_is_kernel(engine->last_retired_context));
Chris Wilson3033aca2016-10-28 13:58:47 +01004615}
4616
Chris Wilson24145512017-01-24 11:01:35 +00004617void i915_gem_sanitize(struct drm_i915_private *i915)
4618{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004619 if (i915_terminally_wedged(&i915->gpu_error)) {
4620 mutex_lock(&i915->drm.struct_mutex);
4621 i915_gem_unset_wedged(i915);
4622 mutex_unlock(&i915->drm.struct_mutex);
4623 }
4624
Chris Wilson24145512017-01-24 11:01:35 +00004625 /*
4626 * If we inherit context state from the BIOS or earlier occupants
4627 * of the GPU, the GPU may be in an inconsistent state when we
4628 * try to take over. The only way to remove the earlier state
4629 * is by resetting. However, resetting on earlier gen is tricky as
4630 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004631 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004632 */
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004633 if (INTEL_GEN(i915) >= 5) {
Chris Wilson24145512017-01-24 11:01:35 +00004634 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4635 WARN_ON(reset && reset != -ENODEV);
4636 }
4637}
4638
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004639int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004640{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004641 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004642 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004643
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004644 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004645 intel_suspend_gt_powersave(dev_priv);
4646
Chris Wilson45c5f202013-10-16 11:50:01 +01004647 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004648
4649 /* We have to flush all the executing contexts to main memory so
4650 * that they can saved in the hibernation image. To ensure the last
4651 * context image is coherent, we have to switch away from it. That
4652 * leaves the dev_priv->kernel_context still active when
4653 * we actually suspend, and its image in memory may not match the GPU
4654 * state. Fortunately, the kernel_context is disposable and we do
4655 * not rely on its state.
4656 */
4657 ret = i915_gem_switch_to_kernel_context(dev_priv);
4658 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004659 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004660
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004661 ret = i915_gem_wait_for_idle(dev_priv,
4662 I915_WAIT_INTERRUPTIBLE |
4663 I915_WAIT_LOCKED);
Chris Wilsoncad99462017-08-26 12:09:33 +01004664 if (ret && ret != -EIO)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004665 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004666
Chris Wilson3033aca2016-10-28 13:58:47 +01004667 assert_kernel_context_is_current(dev_priv);
Chris Wilson829a0af2017-06-20 12:05:45 +01004668 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004669 mutex_unlock(&dev->struct_mutex);
4670
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304671 intel_guc_suspend(dev_priv);
4672
Chris Wilson737b1502015-01-26 18:03:03 +02004673 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004674 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004675
4676 /* As the idle_work is rearming if it detects a race, play safe and
4677 * repeat the flush until it is definitely idle.
4678 */
Chris Wilson7c262402017-10-06 11:40:38 +01004679 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004680
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004681 /* Assert that we sucessfully flushed all the work and
4682 * reset the GPU back to its idle, low power state.
4683 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004684 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004685 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4686 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004687
Imre Deak1c777c52016-10-12 17:46:37 +03004688 /*
4689 * Neither the BIOS, ourselves or any other kernel
4690 * expects the system to be in execlists mode on startup,
4691 * so we need to reset the GPU back to legacy mode. And the only
4692 * known way to disable logical contexts is through a GPU reset.
4693 *
4694 * So in order to leave the system in a known default configuration,
4695 * always reset the GPU upon unload and suspend. Afterwards we then
4696 * clean up the GEM state tracking, flushing off the requests and
4697 * leaving the system in a known idle state.
4698 *
4699 * Note that is of the upmost importance that the GPU is idle and
4700 * all stray writes are flushed *before* we dismantle the backing
4701 * storage for the pinned objects.
4702 *
4703 * However, since we are uncertain that resetting the GPU on older
4704 * machines is a good idea, we don't - just in case it leaves the
4705 * machine in an unusable condition.
4706 */
Chris Wilson24145512017-01-24 11:01:35 +00004707 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004708
4709 intel_runtime_pm_put(dev_priv);
4710 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004711
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004712err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004713 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004714 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004715 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004716}
4717
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004718void i915_gem_resume(struct drm_i915_private *dev_priv)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004719{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004720 struct drm_device *dev = &dev_priv->drm;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004721
Imre Deak31ab49a2016-11-07 11:20:05 +02004722 WARN_ON(dev_priv->gt.awake);
4723
Chris Wilson5ab57c72016-07-15 14:56:20 +01004724 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00004725 i915_gem_restore_gtt_mappings(dev_priv);
Sagar Arun Kamble269e6ea2017-09-29 10:28:36 +05304726 i915_gem_restore_fences(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004727
4728 /* As we didn't flush the kernel context before suspend, we cannot
4729 * guarantee that the context image is complete. So let's just reset
4730 * it and start again.
4731 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004732 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004733
4734 mutex_unlock(&dev->struct_mutex);
4735}
4736
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004737void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004738{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004739 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004740 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4741 return;
4742
4743 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4744 DISP_TILE_SURFACE_SWIZZLING);
4745
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004746 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004747 return;
4748
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004749 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004750 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004751 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004752 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004753 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004754 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004755 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004756 else
4757 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004758}
Daniel Vettere21af882012-02-09 20:53:27 +01004759
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004760static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004761{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004762 I915_WRITE(RING_CTL(base), 0);
4763 I915_WRITE(RING_HEAD(base), 0);
4764 I915_WRITE(RING_TAIL(base), 0);
4765 I915_WRITE(RING_START(base), 0);
4766}
4767
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004768static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004769{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004770 if (IS_I830(dev_priv)) {
4771 init_unused_ring(dev_priv, PRB1_BASE);
4772 init_unused_ring(dev_priv, SRB0_BASE);
4773 init_unused_ring(dev_priv, SRB1_BASE);
4774 init_unused_ring(dev_priv, SRB2_BASE);
4775 init_unused_ring(dev_priv, SRB3_BASE);
4776 } else if (IS_GEN2(dev_priv)) {
4777 init_unused_ring(dev_priv, SRB0_BASE);
4778 init_unused_ring(dev_priv, SRB1_BASE);
4779 } else if (IS_GEN3(dev_priv)) {
4780 init_unused_ring(dev_priv, PRB1_BASE);
4781 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004782 }
4783}
4784
Chris Wilson20a8a742017-02-08 14:30:31 +00004785static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004786{
Chris Wilson20a8a742017-02-08 14:30:31 +00004787 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004788 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304789 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004790 int err;
4791
4792 for_each_engine(engine, i915, id) {
4793 err = engine->init_hw(engine);
4794 if (err)
4795 return err;
4796 }
4797
4798 return 0;
4799}
4800
4801int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4802{
Chris Wilsond200cda2016-04-28 09:56:44 +01004803 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004804
Chris Wilsonde867c22016-10-25 13:16:02 +01004805 dev_priv->gt.last_init_time = ktime_get();
4806
Chris Wilson5e4f5182015-02-13 14:35:59 +00004807 /* Double layer security blanket, see i915_gem_init() */
4808 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4809
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004810 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004811 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004812
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004813 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004814 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004815 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004816
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004817 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004818 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004819 u32 temp = I915_READ(GEN7_MSG_CTL);
4820 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4821 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004822 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004823 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4824 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4825 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4826 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004827 }
4828
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004829 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004830
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004831 /*
4832 * At least 830 can leave some of the unused rings
4833 * "active" (ie. head != tail) after resume which
4834 * will prevent c3 entry. Makes sure all unused rings
4835 * are totally idle.
4836 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004837 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004838
Dave Gordoned54c1a2016-01-19 19:02:54 +00004839 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004840
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004841 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004842 if (ret) {
4843 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4844 goto out;
4845 }
4846
4847 /* Need to do basic initialisation of all rings first: */
Chris Wilson20a8a742017-02-08 14:30:31 +00004848 ret = __i915_gem_restart_engines(dev_priv);
4849 if (ret)
4850 goto out;
Mika Kuoppala99433932013-01-22 14:12:17 +02004851
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004852 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004853
Oscar Mateob8991402017-03-28 09:53:47 -07004854 /* We can't enable contexts until all firmware is loaded */
4855 ret = intel_uc_init_hw(dev_priv);
4856 if (ret)
4857 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004858
Chris Wilson5e4f5182015-02-13 14:35:59 +00004859out:
4860 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004861 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004862}
4863
Chris Wilson39df9192016-07-20 13:31:57 +01004864bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4865{
4866 if (INTEL_INFO(dev_priv)->gen < 6)
4867 return false;
4868
4869 /* TODO: make semaphores and Execlists play nicely together */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00004870 if (i915_modparams.enable_execlists)
Chris Wilson39df9192016-07-20 13:31:57 +01004871 return false;
4872
4873 if (value >= 0)
4874 return value;
4875
Chris Wilson39df9192016-07-20 13:31:57 +01004876 /* Enable semaphores on SNB when IO remapping is off */
Chris Wilson80debff2017-05-25 13:16:12 +01004877 if (IS_GEN6(dev_priv) && intel_vtd_active())
Chris Wilson39df9192016-07-20 13:31:57 +01004878 return false;
Chris Wilson39df9192016-07-20 13:31:57 +01004879
4880 return true;
4881}
4882
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004883int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004884{
Chris Wilson1070a422012-04-24 15:47:41 +01004885 int ret;
4886
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004887 mutex_lock(&dev_priv->drm.struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004888
Matthew Auldda9fe3f32017-10-06 23:18:31 +01004889 /*
4890 * We need to fallback to 4K pages since gvt gtt handling doesn't
4891 * support huge page entries - we will need to check either hypervisor
4892 * mm can support huge guest page or just do emulation in gvt.
4893 */
4894 if (intel_vgpu_active(dev_priv))
4895 mkwrite_device_info(dev_priv)->page_sizes =
4896 I915_GTT_PAGE_SIZE_4K;
4897
Chris Wilson94312822017-05-03 10:39:18 +01004898 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004899
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00004900 if (!i915_modparams.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004901 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004902 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004903 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004904 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004905 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004906 }
4907
Chris Wilson5e4f5182015-02-13 14:35:59 +00004908 /* This is just a security blanket to placate dragons.
4909 * On some systems, we very sporadically observe that the first TLBs
4910 * used by the CS may be stale, despite us poking the TLB reset. If
4911 * we hold the forcewake during initialisation these problems
4912 * just magically go away.
4913 */
4914 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4915
Chris Wilson8a2421b2017-06-16 15:05:22 +01004916 ret = i915_gem_init_userptr(dev_priv);
4917 if (ret)
4918 goto out_unlock;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004919
4920 ret = i915_gem_init_ggtt(dev_priv);
4921 if (ret)
4922 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004923
Chris Wilson829a0af2017-06-20 12:05:45 +01004924 ret = i915_gem_contexts_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004925 if (ret)
4926 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004927
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004928 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004929 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004930 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004931
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004932 ret = i915_gem_init_hw(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004933 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004934 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004935 * wedged. But we only want to do this where the GPU is angry,
4936 * for all other failure, such as an allocation failure, bail.
4937 */
4938 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004939 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004940 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004941 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004942
4943out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004944 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004945 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004946
Chris Wilson60990322014-04-09 09:19:42 +01004947 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004948}
4949
Chris Wilson24145512017-01-24 11:01:35 +00004950void i915_gem_init_mmio(struct drm_i915_private *i915)
4951{
4952 i915_gem_sanitize(i915);
4953}
4954
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004955void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004956i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004957{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004958 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304959 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004960
Akash Goel3b3f1652016-10-13 22:44:48 +05304961 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004962 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004963}
4964
Eric Anholt673a3942008-07-30 12:06:12 -07004965void
Imre Deak40ae4e12016-03-16 14:54:03 +02004966i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4967{
Chris Wilson49ef5292016-08-18 17:17:00 +01004968 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004969
4970 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4971 !IS_CHERRYVIEW(dev_priv))
4972 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02004973 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
4974 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4975 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004976 dev_priv->num_fence_regs = 16;
4977 else
4978 dev_priv->num_fence_regs = 8;
4979
Chris Wilsonc0336662016-05-06 15:40:21 +01004980 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004981 dev_priv->num_fence_regs =
4982 I915_READ(vgtif_reg(avail_rs.fence_num));
4983
4984 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004985 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4986 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4987
4988 fence->i915 = dev_priv;
4989 fence->id = i;
4990 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4991 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004992 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004993
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004994 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004995}
4996
Chris Wilson73cb9702016-10-28 13:58:46 +01004997int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004998i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004999{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005000 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005001
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005002 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
5003 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01005004 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01005005
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005006 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
5007 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01005008 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01005009
Chris Wilsond1b48c12017-08-16 09:52:08 +01005010 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
5011 if (!dev_priv->luts)
5012 goto err_vmas;
5013
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005014 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
5015 SLAB_HWCACHE_ALIGN |
5016 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08005017 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00005018 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01005019 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01005020
Chris Wilson52e54202016-11-14 20:41:02 +00005021 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5022 SLAB_HWCACHE_ALIGN |
5023 SLAB_RECLAIM_ACCOUNT);
5024 if (!dev_priv->dependencies)
5025 goto err_requests;
5026
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005027 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5028 if (!dev_priv->priorities)
5029 goto err_dependencies;
5030
Chris Wilson73cb9702016-10-28 13:58:46 +01005031 mutex_lock(&dev_priv->drm.struct_mutex);
5032 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005033 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005034 mutex_unlock(&dev_priv->drm.struct_mutex);
5035 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005036 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005037
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01005038 INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
5039 init_llist_head(&dev_priv->mm.free_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02005040 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
5041 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07005042 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01005043 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01005044 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005045 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005046 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005047 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005048 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005049 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005050
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005051 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5052
Chris Wilsonb5add952016-08-04 16:32:36 +01005053 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005054
Matthew Auld465c4032017-10-06 23:18:14 +01005055 err = i915_gemfs_init(dev_priv);
5056 if (err)
5057 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5058
Chris Wilson73cb9702016-10-28 13:58:46 +01005059 return 0;
5060
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005061err_priorities:
5062 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005063err_dependencies:
5064 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005065err_requests:
5066 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005067err_luts:
5068 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005069err_vmas:
5070 kmem_cache_destroy(dev_priv->vmas);
5071err_objects:
5072 kmem_cache_destroy(dev_priv->objects);
5073err_out:
5074 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005075}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005076
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005077void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005078{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005079 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005080 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005081 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005082
Matthew Auldea84aa72016-11-17 21:04:11 +00005083 mutex_lock(&dev_priv->drm.struct_mutex);
5084 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5085 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5086 mutex_unlock(&dev_priv->drm.struct_mutex);
5087
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005088 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005089 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005090 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005091 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005092 kmem_cache_destroy(dev_priv->vmas);
5093 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005094
5095 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5096 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005097
5098 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005099}
5100
Chris Wilson6a800ea2016-09-21 14:51:07 +01005101int i915_gem_freeze(struct drm_i915_private *dev_priv)
5102{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005103 /* Discard all purgeable objects, let userspace recover those as
5104 * required after resuming.
5105 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005106 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005107
Chris Wilson6a800ea2016-09-21 14:51:07 +01005108 return 0;
5109}
5110
Chris Wilson461fb992016-05-14 07:26:33 +01005111int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5112{
5113 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005114 struct list_head *phases[] = {
5115 &dev_priv->mm.unbound_list,
5116 &dev_priv->mm.bound_list,
5117 NULL
5118 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005119
5120 /* Called just before we write the hibernation image.
5121 *
5122 * We need to update the domain tracking to reflect that the CPU
5123 * will be accessing all the pages to create and restore from the
5124 * hibernation, and so upon restoration those pages will be in the
5125 * CPU domain.
5126 *
5127 * To make sure the hibernation image contains the latest state,
5128 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005129 *
5130 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005131 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005132 */
5133
Chris Wilson912d5722017-09-06 16:19:30 -07005134 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005135 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005136
Chris Wilsond0aa3012017-04-07 11:25:49 +01005137 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005138 for (p = phases; *p; p++) {
Chris Wilsone27ab732017-06-15 13:38:49 +01005139 list_for_each_entry(obj, *p, global_link)
5140 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005141 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01005142 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005143
5144 return 0;
5145}
5146
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005147void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005148{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005149 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01005150 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005151
5152 /* Clean up our request list when the client is going away, so that
5153 * later retire_requests won't dereference our soon-to-be-gone
5154 * file_priv.
5155 */
Chris Wilson1c255952010-09-26 11:03:27 +01005156 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005157 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005158 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005159 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005160}
5161
Chris Wilson829a0af2017-06-20 12:05:45 +01005162int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005163{
5164 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005165 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005166
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005167 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005168
5169 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5170 if (!file_priv)
5171 return -ENOMEM;
5172
5173 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005174 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005175 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005176
5177 spin_lock_init(&file_priv->mm.lock);
5178 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005179
Chris Wilsonc80ff162016-07-27 09:07:27 +01005180 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005181
Chris Wilson829a0af2017-06-20 12:05:45 +01005182 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005183 if (ret)
5184 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005185
Ben Widawskye422b882013-12-06 14:10:58 -08005186 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005187}
5188
Daniel Vetterb680c372014-09-19 18:27:27 +02005189/**
5190 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005191 * @old: current GEM buffer for the frontbuffer slots
5192 * @new: new GEM buffer for the frontbuffer slots
5193 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005194 *
5195 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5196 * from @old and setting them in @new. Both @old and @new can be NULL.
5197 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005198void i915_gem_track_fb(struct drm_i915_gem_object *old,
5199 struct drm_i915_gem_object *new,
5200 unsigned frontbuffer_bits)
5201{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005202 /* Control of individual bits within the mask are guarded by
5203 * the owning plane->mutex, i.e. we can never see concurrent
5204 * manipulation of individual bits. But since the bitfield as a whole
5205 * is updated using RMW, we need to use atomics in order to update
5206 * the bits.
5207 */
5208 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5209 sizeof(atomic_t) * BITS_PER_BYTE);
5210
Daniel Vettera071fa02014-06-18 23:28:09 +02005211 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005212 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5213 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005214 }
5215
5216 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005217 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5218 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005219 }
5220}
5221
Dave Gordonea702992015-07-09 19:29:02 +01005222/* Allocate a new GEM object and fill it with the supplied data */
5223struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005224i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005225 const void *data, size_t size)
5226{
5227 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005228 struct file *file;
5229 size_t offset;
5230 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005231
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005232 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005233 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005234 return obj;
5235
Chris Wilsonce8ff092017-03-17 19:46:47 +00005236 GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005237
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005238 file = obj->base.filp;
5239 offset = 0;
5240 do {
5241 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5242 struct page *page;
5243 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005244
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005245 err = pagecache_write_begin(file, file->f_mapping,
5246 offset, len, 0,
5247 &page, &pgdata);
5248 if (err < 0)
5249 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005250
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005251 vaddr = kmap(page);
5252 memcpy(vaddr, data, len);
5253 kunmap(page);
5254
5255 err = pagecache_write_end(file, file->f_mapping,
5256 offset, len, len,
5257 page, pgdata);
5258 if (err < 0)
5259 goto fail;
5260
5261 size -= len;
5262 data += len;
5263 offset += len;
5264 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005265
5266 return obj;
5267
5268fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005269 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005270 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005271}
Chris Wilson96d77632016-10-28 13:58:33 +01005272
5273struct scatterlist *
5274i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5275 unsigned int n,
5276 unsigned int *offset)
5277{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005278 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005279 struct scatterlist *sg;
5280 unsigned int idx, count;
5281
5282 might_sleep();
5283 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005284 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005285
5286 /* As we iterate forward through the sg, we record each entry in a
5287 * radixtree for quick repeated (backwards) lookups. If we have seen
5288 * this index previously, we will have an entry for it.
5289 *
5290 * Initial lookup is O(N), but this is amortized to O(1) for
5291 * sequential page access (where each new request is consecutive
5292 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5293 * i.e. O(1) with a large constant!
5294 */
5295 if (n < READ_ONCE(iter->sg_idx))
5296 goto lookup;
5297
5298 mutex_lock(&iter->lock);
5299
5300 /* We prefer to reuse the last sg so that repeated lookup of this
5301 * (or the subsequent) sg are fast - comparing against the last
5302 * sg is faster than going through the radixtree.
5303 */
5304
5305 sg = iter->sg_pos;
5306 idx = iter->sg_idx;
5307 count = __sg_page_count(sg);
5308
5309 while (idx + count <= n) {
5310 unsigned long exception, i;
5311 int ret;
5312
5313 /* If we cannot allocate and insert this entry, or the
5314 * individual pages from this range, cancel updating the
5315 * sg_idx so that on this lookup we are forced to linearly
5316 * scan onwards, but on future lookups we will try the
5317 * insertion again (in which case we need to be careful of
5318 * the error return reporting that we have already inserted
5319 * this index).
5320 */
5321 ret = radix_tree_insert(&iter->radix, idx, sg);
5322 if (ret && ret != -EEXIST)
5323 goto scan;
5324
5325 exception =
5326 RADIX_TREE_EXCEPTIONAL_ENTRY |
5327 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5328 for (i = 1; i < count; i++) {
5329 ret = radix_tree_insert(&iter->radix, idx + i,
5330 (void *)exception);
5331 if (ret && ret != -EEXIST)
5332 goto scan;
5333 }
5334
5335 idx += count;
5336 sg = ____sg_next(sg);
5337 count = __sg_page_count(sg);
5338 }
5339
5340scan:
5341 iter->sg_pos = sg;
5342 iter->sg_idx = idx;
5343
5344 mutex_unlock(&iter->lock);
5345
5346 if (unlikely(n < idx)) /* insertion completed by another thread */
5347 goto lookup;
5348
5349 /* In case we failed to insert the entry into the radixtree, we need
5350 * to look beyond the current sg.
5351 */
5352 while (idx + count <= n) {
5353 idx += count;
5354 sg = ____sg_next(sg);
5355 count = __sg_page_count(sg);
5356 }
5357
5358 *offset = n - idx;
5359 return sg;
5360
5361lookup:
5362 rcu_read_lock();
5363
5364 sg = radix_tree_lookup(&iter->radix, n);
5365 GEM_BUG_ON(!sg);
5366
5367 /* If this index is in the middle of multi-page sg entry,
5368 * the radixtree will contain an exceptional entry that points
5369 * to the start of that range. We will return the pointer to
5370 * the base page and the offset of this page within the
5371 * sg entry's range.
5372 */
5373 *offset = 0;
5374 if (unlikely(radix_tree_exception(sg))) {
5375 unsigned long base =
5376 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5377
5378 sg = radix_tree_lookup(&iter->radix, base);
5379 GEM_BUG_ON(!sg);
5380
5381 *offset = n - base;
5382 }
5383
5384 rcu_read_unlock();
5385
5386 return sg;
5387}
5388
5389struct page *
5390i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5391{
5392 struct scatterlist *sg;
5393 unsigned int offset;
5394
5395 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5396
5397 sg = i915_gem_object_get_sg(obj, n, &offset);
5398 return nth_page(sg_page(sg), offset);
5399}
5400
5401/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5402struct page *
5403i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5404 unsigned int n)
5405{
5406 struct page *page;
5407
5408 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005409 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005410 set_page_dirty(page);
5411
5412 return page;
5413}
5414
5415dma_addr_t
5416i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5417 unsigned long n)
5418{
5419 struct scatterlist *sg;
5420 unsigned int offset;
5421
5422 sg = i915_gem_object_get_sg(obj, n, &offset);
5423 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5424}
Chris Wilson935a2f72017-02-13 17:15:13 +00005425
Chris Wilson8eeb7902017-07-26 19:16:01 +01005426int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5427{
5428 struct sg_table *pages;
5429 int err;
5430
5431 if (align > obj->base.size)
5432 return -EINVAL;
5433
5434 if (obj->ops == &i915_gem_phys_ops)
5435 return 0;
5436
5437 if (obj->ops != &i915_gem_object_ops)
5438 return -EINVAL;
5439
5440 err = i915_gem_object_unbind(obj);
5441 if (err)
5442 return err;
5443
5444 mutex_lock(&obj->mm.lock);
5445
5446 if (obj->mm.madv != I915_MADV_WILLNEED) {
5447 err = -EFAULT;
5448 goto err_unlock;
5449 }
5450
5451 if (obj->mm.quirked) {
5452 err = -EFAULT;
5453 goto err_unlock;
5454 }
5455
5456 if (obj->mm.mapping) {
5457 err = -EBUSY;
5458 goto err_unlock;
5459 }
5460
5461 pages = obj->mm.pages;
5462 obj->ops = &i915_gem_phys_ops;
5463
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005464 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005465 if (err)
5466 goto err_xfer;
5467
5468 /* Perma-pin (until release) the physical set of pages */
5469 __i915_gem_object_pin_pages(obj);
5470
5471 if (!IS_ERR_OR_NULL(pages))
5472 i915_gem_object_ops.put_pages(obj, pages);
5473 mutex_unlock(&obj->mm.lock);
5474 return 0;
5475
5476err_xfer:
5477 obj->ops = &i915_gem_object_ops;
5478 obj->mm.pages = pages;
5479err_unlock:
5480 mutex_unlock(&obj->mm.lock);
5481 return err;
5482}
5483
Chris Wilson935a2f72017-02-13 17:15:13 +00005484#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5485#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005486#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005487#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005488#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005489#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005490#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005491#endif