blob: f76890b74d0003d8df9c901ae71a93f956dc7ff7 [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{
Chris Wilson8d550822017-10-06 12:56:17 +01003090 unsigned long flags;
3091
Chris Wilsonbf2eac32017-07-21 13:32:28 +01003092 GEM_BUG_ON(!i915_terminally_wedged(&request->i915->gpu_error));
Chris Wilson3cd94422017-01-10 17:22:45 +00003093 dma_fence_set_error(&request->fence, -EIO);
Chris Wilson8d550822017-10-06 12:56:17 +01003094
3095 spin_lock_irqsave(&request->engine->timeline->lock, flags);
3096 __i915_gem_request_submit(request);
Chris Wilson3dcf93f2016-11-22 14:41:20 +00003097 intel_engine_init_global_seqno(request->engine, request->global_seqno);
Chris Wilson8d550822017-10-06 12:56:17 +01003098 spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
Chris Wilson821ed7d2016-09-09 14:11:53 +01003099}
3100
Chris Wilson2a20d6f2017-01-10 17:22:46 +00003101static void engine_set_wedged(struct intel_engine_cs *engine)
Chris Wilson821ed7d2016-09-09 14:11:53 +01003102{
Chris Wilson20e49332016-11-22 14:41:21 +00003103 /* We need to be sure that no thread is running the old callback as
3104 * we install the nop handler (otherwise we would submit a request
3105 * to hardware that will never complete). In order to prevent this
3106 * race, we wait until the machine is idle before making the swap
3107 * (using stop_machine()).
3108 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01003109 engine->submit_request = nop_submit_request;
Chris Wilson70c2a242016-09-09 14:11:46 +01003110
Chris Wilson3cd94422017-01-10 17:22:45 +00003111 /* Mark all executing requests as skipped */
Chris Wilson27a5f612017-09-15 18:31:00 +01003112 engine->cancel_requests(engine);
Chris Wilson5e32d742017-07-21 13:32:25 +01003113
3114 /* Mark all pending requests as complete so that any concurrent
3115 * (lockless) lookup doesn't try and wait upon the request as we
3116 * reset it.
3117 */
3118 intel_engine_init_global_seqno(engine,
3119 intel_engine_last_submit(engine));
Eric Anholt673a3942008-07-30 12:06:12 -07003120}
3121
Chris Wilson20e49332016-11-22 14:41:21 +00003122static int __i915_gem_set_wedged_BKL(void *data)
Eric Anholt673a3942008-07-30 12:06:12 -07003123{
Chris Wilson20e49332016-11-22 14:41:21 +00003124 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003125 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05303126 enum intel_engine_id id;
Eric Anholt673a3942008-07-30 12:06:12 -07003127
Chris Wilson20e49332016-11-22 14:41:21 +00003128 for_each_engine(engine, i915, id)
Chris Wilson2a20d6f2017-01-10 17:22:46 +00003129 engine_set_wedged(engine);
Chris Wilson20e49332016-11-22 14:41:21 +00003130
Chris Wilson3d7adbb2017-07-21 13:32:27 +01003131 set_bit(I915_WEDGED, &i915->gpu_error.flags);
3132 wake_up_all(&i915->gpu_error.reset_queue);
3133
Chris Wilson20e49332016-11-22 14:41:21 +00003134 return 0;
3135}
3136
3137void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
3138{
Chris Wilson20e49332016-11-22 14:41:21 +00003139 stop_machine(__i915_gem_set_wedged_BKL, dev_priv, NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07003140}
3141
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003142bool i915_gem_unset_wedged(struct drm_i915_private *i915)
3143{
3144 struct i915_gem_timeline *tl;
3145 int i;
3146
3147 lockdep_assert_held(&i915->drm.struct_mutex);
3148 if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
3149 return true;
3150
3151 /* Before unwedging, make sure that all pending operations
3152 * are flushed and errored out - we may have requests waiting upon
3153 * third party fences. We marked all inflight requests as EIO, and
3154 * every execbuf since returned EIO, for consistency we want all
3155 * the currently pending requests to also be marked as EIO, which
3156 * is done inside our nop_submit_request - and so we must wait.
3157 *
3158 * No more can be submitted until we reset the wedged bit.
3159 */
3160 list_for_each_entry(tl, &i915->gt.timelines, link) {
3161 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3162 struct drm_i915_gem_request *rq;
3163
3164 rq = i915_gem_active_peek(&tl->engine[i].last_request,
3165 &i915->drm.struct_mutex);
3166 if (!rq)
3167 continue;
3168
3169 /* We can't use our normal waiter as we want to
3170 * avoid recursively trying to handle the current
3171 * reset. The basic dma_fence_default_wait() installs
3172 * a callback for dma_fence_signal(), which is
3173 * triggered by our nop handler (indirectly, the
3174 * callback enables the signaler thread which is
3175 * woken by the nop_submit_request() advancing the seqno
3176 * and when the seqno passes the fence, the signaler
3177 * then signals the fence waking us up).
3178 */
3179 if (dma_fence_default_wait(&rq->fence, true,
3180 MAX_SCHEDULE_TIMEOUT) < 0)
3181 return false;
3182 }
3183 }
3184
3185 /* Undo nop_submit_request. We prevent all new i915 requests from
3186 * being queued (by disallowing execbuf whilst wedged) so having
3187 * waited for all active requests above, we know the system is idle
3188 * and do not have to worry about a thread being inside
3189 * engine->submit_request() as we swap over. So unlike installing
3190 * the nop_submit_request on reset, we can do this from normal
3191 * context and do not require stop_machine().
3192 */
3193 intel_engines_reset_default_submission(i915);
Chris Wilson36703e72017-06-22 11:56:25 +01003194 i915_gem_contexts_lost(i915);
Chris Wilson2e8f9d32017-03-16 17:13:04 +00003195
3196 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
3197 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
3198
3199 return true;
3200}
3201
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003202static void
Eric Anholt673a3942008-07-30 12:06:12 -07003203i915_gem_retire_work_handler(struct work_struct *work)
3204{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003205 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003206 container_of(work, typeof(*dev_priv), gt.retire_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003207 struct drm_device *dev = &dev_priv->drm;
Eric Anholt673a3942008-07-30 12:06:12 -07003208
Chris Wilson891b48c2010-09-29 12:26:37 +01003209 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003210 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilson67d97da2016-07-04 08:08:31 +01003211 i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003212 mutex_unlock(&dev->struct_mutex);
3213 }
Chris Wilson67d97da2016-07-04 08:08:31 +01003214
3215 /* Keep the retire handler running until we are finally idle.
3216 * We do not need to do this test under locking as in the worst-case
3217 * we queue the retire worker once too often.
3218 */
Chris Wilsonc9615612016-07-09 10:12:06 +01003219 if (READ_ONCE(dev_priv->gt.awake)) {
3220 i915_queue_hangcheck(dev_priv);
Chris Wilson67d97da2016-07-04 08:08:31 +01003221 queue_delayed_work(dev_priv->wq,
3222 &dev_priv->gt.retire_work,
Chris Wilsonbcb45082012-10-05 17:02:57 +01003223 round_jiffies_up_relative(HZ));
Chris Wilsonc9615612016-07-09 10:12:06 +01003224 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003225}
Chris Wilson891b48c2010-09-29 12:26:37 +01003226
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003227static void
3228i915_gem_idle_work_handler(struct work_struct *work)
3229{
3230 struct drm_i915_private *dev_priv =
Chris Wilson67d97da2016-07-04 08:08:31 +01003231 container_of(work, typeof(*dev_priv), gt.idle_work.work);
Chris Wilson91c8a322016-07-05 10:40:23 +01003232 struct drm_device *dev = &dev_priv->drm;
Chris Wilson67d97da2016-07-04 08:08:31 +01003233 bool rearm_hangcheck;
3234
3235 if (!READ_ONCE(dev_priv->gt.awake))
3236 return;
3237
Imre Deak0cb56702016-11-07 11:20:04 +02003238 /*
3239 * Wait for last execlists context complete, but bail out in case a
3240 * new request is submitted.
3241 */
Chris Wilson8490ae202017-03-30 15:50:37 +01003242 wait_for(intel_engines_are_idle(dev_priv), 10);
Chris Wilson28176ef2016-10-28 13:58:56 +01003243 if (READ_ONCE(dev_priv->gt.active_requests))
Chris Wilson67d97da2016-07-04 08:08:31 +01003244 return;
3245
3246 rearm_hangcheck =
3247 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
3248
3249 if (!mutex_trylock(&dev->struct_mutex)) {
3250 /* Currently busy, come back later */
3251 mod_delayed_work(dev_priv->wq,
3252 &dev_priv->gt.idle_work,
3253 msecs_to_jiffies(50));
3254 goto out_rearm;
3255 }
3256
Imre Deak93c97dc2016-11-07 11:20:03 +02003257 /*
3258 * New request retired after this work handler started, extend active
3259 * period until next instance of the work.
3260 */
3261 if (work_pending(work))
3262 goto out_unlock;
3263
Chris Wilson28176ef2016-10-28 13:58:56 +01003264 if (dev_priv->gt.active_requests)
Chris Wilson67d97da2016-07-04 08:08:31 +01003265 goto out_unlock;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003266
Chris Wilson05425242017-03-03 12:19:47 +00003267 if (wait_for(intel_engines_are_idle(dev_priv), 10))
Imre Deak0cb56702016-11-07 11:20:04 +02003268 DRM_ERROR("Timeout waiting for engines to idle\n");
3269
Chris Wilson6c067572017-05-17 13:10:03 +01003270 intel_engines_mark_idle(dev_priv);
Chris Wilson47979482017-05-03 10:39:21 +01003271 i915_gem_timelines_mark_idle(dev_priv);
Zou Nan hai852835f2010-05-21 09:08:56 +08003272
Chris Wilson67d97da2016-07-04 08:08:31 +01003273 GEM_BUG_ON(!dev_priv->gt.awake);
3274 dev_priv->gt.awake = false;
3275 rearm_hangcheck = false;
Daniel Vetter30ecad72015-12-09 09:29:36 +01003276
Chris Wilson67d97da2016-07-04 08:08:31 +01003277 if (INTEL_GEN(dev_priv) >= 6)
3278 gen6_rps_idle(dev_priv);
3279 intel_runtime_pm_put(dev_priv);
3280out_unlock:
3281 mutex_unlock(&dev->struct_mutex);
Chris Wilson35c94182015-04-07 16:20:37 +01003282
Chris Wilson67d97da2016-07-04 08:08:31 +01003283out_rearm:
3284 if (rearm_hangcheck) {
3285 GEM_BUG_ON(!dev_priv->gt.awake);
3286 i915_queue_hangcheck(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003287 }
Eric Anholt673a3942008-07-30 12:06:12 -07003288}
3289
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003290void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
3291{
Chris Wilsond1b48c12017-08-16 09:52:08 +01003292 struct drm_i915_private *i915 = to_i915(gem->dev);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003293 struct drm_i915_gem_object *obj = to_intel_bo(gem);
3294 struct drm_i915_file_private *fpriv = file->driver_priv;
Chris Wilsond1b48c12017-08-16 09:52:08 +01003295 struct i915_lut_handle *lut, *ln;
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003296
Chris Wilsond1b48c12017-08-16 09:52:08 +01003297 mutex_lock(&i915->drm.struct_mutex);
3298
3299 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
3300 struct i915_gem_context *ctx = lut->ctx;
3301 struct i915_vma *vma;
3302
Chris Wilson432295d2017-08-22 12:05:15 +01003303 GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
Chris Wilsond1b48c12017-08-16 09:52:08 +01003304 if (ctx->file_priv != fpriv)
3305 continue;
3306
3307 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
Chris Wilson3ffff012017-08-22 12:05:17 +01003308 GEM_BUG_ON(vma->obj != obj);
3309
3310 /* We allow the process to have multiple handles to the same
3311 * vma, in the same fd namespace, by virtue of flink/open.
3312 */
3313 GEM_BUG_ON(!vma->open_count);
3314 if (!--vma->open_count && !i915_vma_is_ggtt(vma))
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003315 i915_vma_close(vma);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003316
Chris Wilsond1b48c12017-08-16 09:52:08 +01003317 list_del(&lut->obj_link);
3318 list_del(&lut->ctx_link);
Chris Wilson4ff4b442017-06-16 15:05:16 +01003319
Chris Wilsond1b48c12017-08-16 09:52:08 +01003320 kmem_cache_free(i915->luts, lut);
3321 __i915_gem_object_release_unless_active(obj);
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01003322 }
Chris Wilsond1b48c12017-08-16 09:52:08 +01003323
3324 mutex_unlock(&i915->drm.struct_mutex);
Chris Wilsonb1f788c2016-08-04 07:52:45 +01003325}
3326
Chris Wilsone95433c2016-10-28 13:58:27 +01003327static unsigned long to_wait_timeout(s64 timeout_ns)
3328{
3329 if (timeout_ns < 0)
3330 return MAX_SCHEDULE_TIMEOUT;
3331
3332 if (timeout_ns == 0)
3333 return 0;
3334
3335 return nsecs_to_jiffies_timeout(timeout_ns);
3336}
3337
Ben Widawsky5816d642012-04-11 11:18:19 -07003338/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003339 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003340 * @dev: drm device pointer
3341 * @data: ioctl data blob
3342 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003343 *
3344 * Returns 0 if successful, else an error is returned with the remaining time in
3345 * the timeout parameter.
3346 * -ETIME: object is still busy after timeout
3347 * -ERESTARTSYS: signal interrupted the wait
3348 * -ENONENT: object doesn't exist
3349 * Also possible, but rare:
Chris Wilsonb8050142017-08-11 11:57:31 +01003350 * -EAGAIN: incomplete, restart syscall
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003351 * -ENOMEM: damn
3352 * -ENODEV: Internal IRQ fail
3353 * -E?: The add request failed
3354 *
3355 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3356 * non-zero timeout parameter the wait ioctl will wait for the given number of
3357 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3358 * without holding struct_mutex the object may become re-busied before this
3359 * function completes. A similar but shorter * race condition exists in the busy
3360 * ioctl
3361 */
3362int
3363i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3364{
3365 struct drm_i915_gem_wait *args = data;
3366 struct drm_i915_gem_object *obj;
Chris Wilsone95433c2016-10-28 13:58:27 +01003367 ktime_t start;
3368 long ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003369
Daniel Vetter11b5d512014-09-29 15:31:26 +02003370 if (args->flags != 0)
3371 return -EINVAL;
3372
Chris Wilson03ac0642016-07-20 13:31:51 +01003373 obj = i915_gem_object_lookup(file, args->bo_handle);
Chris Wilson033d5492016-08-05 10:14:17 +01003374 if (!obj)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003375 return -ENOENT;
Chris Wilson033d5492016-08-05 10:14:17 +01003376
Chris Wilsone95433c2016-10-28 13:58:27 +01003377 start = ktime_get();
3378
3379 ret = i915_gem_object_wait(obj,
3380 I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
3381 to_wait_timeout(args->timeout_ns),
3382 to_rps_client(file));
3383
3384 if (args->timeout_ns > 0) {
3385 args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
3386 if (args->timeout_ns < 0)
3387 args->timeout_ns = 0;
Chris Wilsonc1d20612017-02-16 12:54:41 +00003388
3389 /*
3390 * Apparently ktime isn't accurate enough and occasionally has a
3391 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
3392 * things up to make the test happy. We allow up to 1 jiffy.
3393 *
3394 * This is a regression from the timespec->ktime conversion.
3395 */
3396 if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
3397 args->timeout_ns = 0;
Chris Wilsonb8050142017-08-11 11:57:31 +01003398
3399 /* Asked to wait beyond the jiffie/scheduler precision? */
3400 if (ret == -ETIME && args->timeout_ns)
3401 ret = -EAGAIN;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003402 }
3403
Chris Wilsonf0cd5182016-10-28 13:58:43 +01003404 i915_gem_object_put(obj);
John Harrisonff865882014-11-24 18:49:28 +00003405 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003406}
3407
Chris Wilson73cb9702016-10-28 13:58:46 +01003408static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003409{
Chris Wilson73cb9702016-10-28 13:58:46 +01003410 int ret, i;
3411
3412 for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
3413 ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
3414 if (ret)
3415 return ret;
3416 }
3417
3418 return 0;
3419}
3420
Chris Wilson25112b62017-03-30 15:50:39 +01003421static int wait_for_engines(struct drm_i915_private *i915)
3422{
Chris Wilsoncad99462017-08-26 12:09:33 +01003423 if (wait_for(intel_engines_are_idle(i915), 50)) {
3424 DRM_ERROR("Failed to idle engines, declaring wedged!\n");
3425 i915_gem_set_wedged(i915);
3426 return -EIO;
Chris Wilson25112b62017-03-30 15:50:39 +01003427 }
3428
3429 return 0;
3430}
3431
Chris Wilson73cb9702016-10-28 13:58:46 +01003432int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
3433{
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003434 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003435
Chris Wilson863e9fd2017-05-30 13:13:32 +01003436 /* If the device is asleep, we have no requests outstanding */
3437 if (!READ_ONCE(i915->gt.awake))
3438 return 0;
3439
Chris Wilson9caa34a2016-11-11 14:58:08 +00003440 if (flags & I915_WAIT_LOCKED) {
3441 struct i915_gem_timeline *tl;
3442
3443 lockdep_assert_held(&i915->drm.struct_mutex);
3444
3445 list_for_each_entry(tl, &i915->gt.timelines, link) {
3446 ret = wait_for_timeline(tl, flags);
3447 if (ret)
3448 return ret;
3449 }
Chris Wilson72022a72017-03-30 15:50:38 +01003450
3451 i915_gem_retire_requests(i915);
3452 GEM_BUG_ON(i915->gt.active_requests);
Chris Wilson25112b62017-03-30 15:50:39 +01003453
3454 ret = wait_for_engines(i915);
Chris Wilson9caa34a2016-11-11 14:58:08 +00003455 } else {
3456 ret = wait_for_timeline(&i915->gt.global_timeline, flags);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003457 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003458
Chris Wilson25112b62017-03-30 15:50:39 +01003459 return ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003460}
3461
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003462static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
3463{
Chris Wilsone27ab732017-06-15 13:38:49 +01003464 /*
3465 * We manually flush the CPU domain so that we can override and
3466 * force the flush for the display, and perform it asyncrhonously.
3467 */
3468 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
3469 if (obj->cache_dirty)
3470 i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003471 obj->base.write_domain = 0;
3472}
3473
3474void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
3475{
3476 if (!READ_ONCE(obj->pin_display))
3477 return;
3478
3479 mutex_lock(&obj->base.dev->struct_mutex);
3480 __i915_gem_object_flush_for_display(obj);
3481 mutex_unlock(&obj->base.dev->struct_mutex);
3482}
3483
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003484/**
Chris Wilsone22d8e32017-04-12 12:01:11 +01003485 * Moves a single object to the WC read, and possibly write domain.
3486 * @obj: object to act on
3487 * @write: ask for write access or read only
3488 *
3489 * This function returns when the move is complete, including waiting on
3490 * flushes to occur.
3491 */
3492int
3493i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
3494{
3495 int ret;
3496
3497 lockdep_assert_held(&obj->base.dev->struct_mutex);
3498
3499 ret = i915_gem_object_wait(obj,
3500 I915_WAIT_INTERRUPTIBLE |
3501 I915_WAIT_LOCKED |
3502 (write ? I915_WAIT_ALL : 0),
3503 MAX_SCHEDULE_TIMEOUT,
3504 NULL);
3505 if (ret)
3506 return ret;
3507
3508 if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
3509 return 0;
3510
3511 /* Flush and acquire obj->pages so that we are coherent through
3512 * direct access in memory with previous cached writes through
3513 * shmemfs and that our cache domain tracking remains valid.
3514 * For example, if the obj->filp was moved to swap without us
3515 * being notified and releasing the pages, we would mistakenly
3516 * continue to assume that the obj remained out of the CPU cached
3517 * domain.
3518 */
3519 ret = i915_gem_object_pin_pages(obj);
3520 if (ret)
3521 return ret;
3522
3523 flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
3524
3525 /* Serialise direct access to this object with the barriers for
3526 * coherent writes from the GPU, by effectively invalidating the
3527 * WC domain upon first access.
3528 */
3529 if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
3530 mb();
3531
3532 /* It should now be out of any other write domains, and we can update
3533 * the domain values for our changes.
3534 */
3535 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
3536 obj->base.read_domains |= I915_GEM_DOMAIN_WC;
3537 if (write) {
3538 obj->base.read_domains = I915_GEM_DOMAIN_WC;
3539 obj->base.write_domain = I915_GEM_DOMAIN_WC;
3540 obj->mm.dirty = true;
3541 }
3542
3543 i915_gem_object_unpin_pages(obj);
3544 return 0;
3545}
3546
3547/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003548 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003549 * @obj: object to act on
3550 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003551 *
3552 * This function returns when the move is complete, including waiting on
3553 * flushes to occur.
3554 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003555int
Chris Wilson20217462010-11-23 15:26:33 +00003556i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003557{
Eric Anholte47c68e2008-11-14 13:35:19 -08003558 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003559
Chris Wilsone95433c2016-10-28 13:58:27 +01003560 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003561
Chris Wilsone95433c2016-10-28 13:58:27 +01003562 ret = i915_gem_object_wait(obj,
3563 I915_WAIT_INTERRUPTIBLE |
3564 I915_WAIT_LOCKED |
3565 (write ? I915_WAIT_ALL : 0),
3566 MAX_SCHEDULE_TIMEOUT,
3567 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003568 if (ret)
3569 return ret;
3570
Chris Wilsonc13d87e2016-07-20 09:21:15 +01003571 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3572 return 0;
3573
Chris Wilson43566de2015-01-02 16:29:29 +05303574 /* Flush and acquire obj->pages so that we are coherent through
3575 * direct access in memory with previous cached writes through
3576 * shmemfs and that our cache domain tracking remains valid.
3577 * For example, if the obj->filp was moved to swap without us
3578 * being notified and releasing the pages, we would mistakenly
3579 * continue to assume that the obj remained out of the CPU cached
3580 * domain.
3581 */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003582 ret = i915_gem_object_pin_pages(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303583 if (ret)
3584 return ret;
3585
Chris Wilsonef749212017-04-12 12:01:10 +01003586 flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003587
Chris Wilsond0a57782012-10-09 19:24:37 +01003588 /* Serialise direct access to this object with the barriers for
3589 * coherent writes from the GPU, by effectively invalidating the
3590 * GTT domain upon first access.
3591 */
3592 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3593 mb();
3594
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003595 /* It should now be out of any other write domains, and we can update
3596 * the domain values for our changes.
3597 */
Chris Wilson40e62d52016-10-28 13:58:41 +01003598 GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
Chris Wilson05394f32010-11-08 19:18:58 +00003599 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003600 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003601 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3602 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003603 obj->mm.dirty = true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003604 }
3605
Chris Wilsona4f5ea62016-10-28 13:58:35 +01003606 i915_gem_object_unpin_pages(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003607 return 0;
3608}
3609
Chris Wilsonef55f922015-10-09 14:11:27 +01003610/**
3611 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003612 * @obj: object to act on
3613 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003614 *
3615 * After this function returns, the object will be in the new cache-level
3616 * across all GTT and the contents of the backing storage will be coherent,
3617 * with respect to the new cache-level. In order to keep the backing storage
3618 * coherent for all users, we only allow a single cache level to be set
3619 * globally on the object and prevent it from being changed whilst the
3620 * hardware is reading from the object. That is if the object is currently
3621 * on the scanout it will be set to uncached (or equivalent display
3622 * cache coherency) and all non-MOCS GPU access will also be uncached so
3623 * that all direct access to the scanout remains coherent.
3624 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003625int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3626 enum i915_cache_level cache_level)
3627{
Chris Wilsonaa653a62016-08-04 07:52:27 +01003628 struct i915_vma *vma;
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003629 int ret;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003630
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003631 lockdep_assert_held(&obj->base.dev->struct_mutex);
3632
Chris Wilsone4ffd172011-04-04 09:44:39 +01003633 if (obj->cache_level == cache_level)
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003634 return 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003635
Chris Wilsonef55f922015-10-09 14:11:27 +01003636 /* Inspect the list of currently bound VMA and unbind any that would
3637 * be invalid given the new cache-level. This is principally to
3638 * catch the issue of the CS prefetch crossing page boundaries and
3639 * reading an invalid PTE on older architectures.
3640 */
Chris Wilsonaa653a62016-08-04 07:52:27 +01003641restart:
3642 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003643 if (!drm_mm_node_allocated(&vma->node))
3644 continue;
3645
Chris Wilson20dfbde2016-08-04 16:32:30 +01003646 if (i915_vma_is_pinned(vma)) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003647 DRM_DEBUG("can not change the cache level of pinned objects\n");
3648 return -EBUSY;
3649 }
3650
Chris Wilsonaa653a62016-08-04 07:52:27 +01003651 if (i915_gem_valid_gtt_space(vma, cache_level))
3652 continue;
3653
3654 ret = i915_vma_unbind(vma);
3655 if (ret)
3656 return ret;
3657
3658 /* As unbinding may affect other elements in the
3659 * obj->vma_list (due to side-effects from retiring
3660 * an active vma), play safe and restart the iterator.
3661 */
3662 goto restart;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003663 }
3664
Chris Wilsonef55f922015-10-09 14:11:27 +01003665 /* We can reuse the existing drm_mm nodes but need to change the
3666 * cache-level on the PTE. We could simply unbind them all and
3667 * rebind with the correct cache-level on next use. However since
3668 * we already have a valid slot, dma mapping, pages etc, we may as
3669 * rewrite the PTE in the belief that doing so tramples upon less
3670 * state and so involves less work.
3671 */
Chris Wilson15717de2016-08-04 07:52:26 +01003672 if (obj->bind_count) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003673 /* Before we change the PTE, the GPU must not be accessing it.
3674 * If we wait upon the object, we know that all the bound
3675 * VMA are no longer active.
3676 */
Chris Wilsone95433c2016-10-28 13:58:27 +01003677 ret = i915_gem_object_wait(obj,
3678 I915_WAIT_INTERRUPTIBLE |
3679 I915_WAIT_LOCKED |
3680 I915_WAIT_ALL,
3681 MAX_SCHEDULE_TIMEOUT,
3682 NULL);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003683 if (ret)
3684 return ret;
3685
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00003686 if (!HAS_LLC(to_i915(obj->base.dev)) &&
3687 cache_level != I915_CACHE_NONE) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003688 /* Access to snoopable pages through the GTT is
3689 * incoherent and on some machines causes a hard
3690 * lockup. Relinquish the CPU mmaping to force
3691 * userspace to refault in the pages and we can
3692 * then double check if the GTT mapping is still
3693 * valid for that pointer access.
3694 */
3695 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003696
Chris Wilsonef55f922015-10-09 14:11:27 +01003697 /* As we no longer need a fence for GTT access,
3698 * we can relinquish it now (and so prevent having
3699 * to steal a fence from someone else on the next
3700 * fence request). Note GPU activity would have
3701 * dropped the fence as all snoopable access is
3702 * supposed to be linear.
3703 */
Chris Wilson49ef5292016-08-18 17:17:00 +01003704 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3705 ret = i915_vma_put_fence(vma);
3706 if (ret)
3707 return ret;
3708 }
Chris Wilsonef55f922015-10-09 14:11:27 +01003709 } else {
3710 /* We either have incoherent backing store and
3711 * so no GTT access or the architecture is fully
3712 * coherent. In such cases, existing GTT mmaps
3713 * ignore the cache bit in the PTE and we can
3714 * rewrite it without confusing the GPU or having
3715 * to force userspace to fault back in its mmaps.
3716 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003717 }
3718
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003719 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003720 if (!drm_mm_node_allocated(&vma->node))
3721 continue;
3722
3723 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3724 if (ret)
3725 return ret;
3726 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003727 }
3728
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003729 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003730 vma->node.color = cache_level;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01003731 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01003732 obj->cache_dirty = true; /* Always invalidate stale cachelines */
Chris Wilson2c225692013-08-09 12:26:45 +01003733
Chris Wilsone4ffd172011-04-04 09:44:39 +01003734 return 0;
3735}
3736
Ben Widawsky199adf42012-09-21 17:01:20 -07003737int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3738 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003739{
Ben Widawsky199adf42012-09-21 17:01:20 -07003740 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003741 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003742 int err = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003743
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003744 rcu_read_lock();
3745 obj = i915_gem_object_lookup_rcu(file, args->handle);
3746 if (!obj) {
3747 err = -ENOENT;
3748 goto out;
3749 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003750
Chris Wilson651d7942013-08-08 14:41:10 +01003751 switch (obj->cache_level) {
3752 case I915_CACHE_LLC:
3753 case I915_CACHE_L3_LLC:
3754 args->caching = I915_CACHING_CACHED;
3755 break;
3756
Chris Wilson4257d3b2013-08-08 14:41:11 +01003757 case I915_CACHE_WT:
3758 args->caching = I915_CACHING_DISPLAY;
3759 break;
3760
Chris Wilson651d7942013-08-08 14:41:10 +01003761 default:
3762 args->caching = I915_CACHING_NONE;
3763 break;
3764 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01003765out:
3766 rcu_read_unlock();
3767 return err;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003768}
3769
Ben Widawsky199adf42012-09-21 17:01:20 -07003770int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3771 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003772{
Chris Wilson9c870d02016-10-24 13:42:15 +01003773 struct drm_i915_private *i915 = to_i915(dev);
Ben Widawsky199adf42012-09-21 17:01:20 -07003774 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003775 struct drm_i915_gem_object *obj;
3776 enum i915_cache_level level;
Chris Wilsond65415d2017-01-19 08:22:10 +00003777 int ret = 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003778
Ben Widawsky199adf42012-09-21 17:01:20 -07003779 switch (args->caching) {
3780 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003781 level = I915_CACHE_NONE;
3782 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003783 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003784 /*
3785 * Due to a HW issue on BXT A stepping, GPU stores via a
3786 * snooped mapping may leave stale data in a corresponding CPU
3787 * cacheline, whereas normally such cachelines would get
3788 * invalidated.
3789 */
Chris Wilson9c870d02016-10-24 13:42:15 +01003790 if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
Imre Deake5756c12015-08-14 18:43:30 +03003791 return -ENODEV;
3792
Chris Wilsone6994ae2012-07-10 10:27:08 +01003793 level = I915_CACHE_LLC;
3794 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003795 case I915_CACHING_DISPLAY:
Chris Wilson9c870d02016-10-24 13:42:15 +01003796 level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003797 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003798 default:
3799 return -EINVAL;
3800 }
3801
Chris Wilsond65415d2017-01-19 08:22:10 +00003802 obj = i915_gem_object_lookup(file, args->handle);
3803 if (!obj)
3804 return -ENOENT;
3805
3806 if (obj->cache_level == level)
3807 goto out;
3808
3809 ret = i915_gem_object_wait(obj,
3810 I915_WAIT_INTERRUPTIBLE,
3811 MAX_SCHEDULE_TIMEOUT,
3812 to_rps_client(file));
3813 if (ret)
3814 goto out;
3815
Ben Widawsky3bc29132012-09-26 16:15:20 -07003816 ret = i915_mutex_lock_interruptible(dev);
3817 if (ret)
Chris Wilsond65415d2017-01-19 08:22:10 +00003818 goto out;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003819
3820 ret = i915_gem_object_set_cache_level(obj, level);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003821 mutex_unlock(&dev->struct_mutex);
Chris Wilsond65415d2017-01-19 08:22:10 +00003822
3823out:
3824 i915_gem_object_put(obj);
Chris Wilsone6994ae2012-07-10 10:27:08 +01003825 return ret;
3826}
3827
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003828/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003829 * Prepare buffer for display plane (scanout, cursors, etc).
3830 * Can be called from an uninterruptible phase (modesetting) and allows
3831 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003832 */
Chris Wilson058d88c2016-08-15 10:49:06 +01003833struct i915_vma *
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003834i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3835 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003836 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003837{
Chris Wilson058d88c2016-08-15 10:49:06 +01003838 struct i915_vma *vma;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003839 int ret;
3840
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003841 lockdep_assert_held(&obj->base.dev->struct_mutex);
3842
Chris Wilsoncc98b412013-08-09 12:25:09 +01003843 /* Mark the pin_display early so that we account for the
3844 * display coherency whilst setting up the cache domains.
3845 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003846 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003847
Eric Anholta7ef0642011-03-29 16:59:54 -07003848 /* The display engine is not coherent with the LLC cache on gen6. As
3849 * a result, we make sure that the pinning that is about to occur is
3850 * done with uncached PTEs. This is lowest common denominator for all
3851 * chipsets.
3852 *
3853 * However for gen6+, we could do better by using the GFDT bit instead
3854 * of uncaching, which would allow us to flush all the LLC-cached data
3855 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3856 */
Chris Wilson651d7942013-08-08 14:41:10 +01003857 ret = i915_gem_object_set_cache_level(obj,
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003858 HAS_WT(to_i915(obj->base.dev)) ?
3859 I915_CACHE_WT : I915_CACHE_NONE);
Chris Wilson058d88c2016-08-15 10:49:06 +01003860 if (ret) {
3861 vma = ERR_PTR(ret);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003862 goto err_unpin_display;
Chris Wilson058d88c2016-08-15 10:49:06 +01003863 }
Eric Anholta7ef0642011-03-29 16:59:54 -07003864
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003865 /* As the user may map the buffer once pinned in the display plane
3866 * (e.g. libkms for the bootup splash), we have to ensure that we
Chris Wilson2efb8132016-08-18 17:17:06 +01003867 * always use map_and_fenceable for all scanout buffers. However,
3868 * it may simply be too big to fit into mappable, in which case
3869 * put it anyway and hope that userspace can cope (but always first
3870 * try to preserve the existing ABI).
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003871 */
Chris Wilson2efb8132016-08-18 17:17:06 +01003872 vma = ERR_PTR(-ENOSPC);
Chris Wilson47a8e3f2017-01-14 00:28:27 +00003873 if (!view || view->type == I915_GGTT_VIEW_NORMAL)
Chris Wilson2efb8132016-08-18 17:17:06 +01003874 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
3875 PIN_MAPPABLE | PIN_NONBLOCK);
Chris Wilson767a2222016-11-07 11:01:28 +00003876 if (IS_ERR(vma)) {
3877 struct drm_i915_private *i915 = to_i915(obj->base.dev);
3878 unsigned int flags;
3879
3880 /* Valleyview is definitely limited to scanning out the first
3881 * 512MiB. Lets presume this behaviour was inherited from the
3882 * g4x display engine and that all earlier gen are similarly
3883 * limited. Testing suggests that it is a little more
3884 * complicated than this. For example, Cherryview appears quite
3885 * happy to scanout from anywhere within its global aperture.
3886 */
3887 flags = 0;
3888 if (HAS_GMCH_DISPLAY(i915))
3889 flags = PIN_MAPPABLE;
3890 vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
3891 }
Chris Wilson058d88c2016-08-15 10:49:06 +01003892 if (IS_ERR(vma))
Chris Wilsoncc98b412013-08-09 12:25:09 +01003893 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003894
Chris Wilsond8923dc2016-08-18 17:17:07 +01003895 vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
3896
Chris Wilsona6a7cc42016-11-18 21:17:46 +00003897 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
Chris Wilson5a97bcc2017-02-22 11:40:46 +00003898 __i915_gem_object_flush_for_display(obj);
Chris Wilsond59b21e2017-02-22 11:40:49 +00003899 intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003900
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003901 /* It should now be out of any other write domains, and we can update
3902 * the domain values for our changes.
3903 */
Chris Wilson05394f32010-11-08 19:18:58 +00003904 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003905
Chris Wilson058d88c2016-08-15 10:49:06 +01003906 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003907
3908err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003909 obj->pin_display--;
Chris Wilson058d88c2016-08-15 10:49:06 +01003910 return vma;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003911}
3912
3913void
Chris Wilson058d88c2016-08-15 10:49:06 +01003914i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003915{
Chris Wilson49d73912016-11-29 09:50:08 +00003916 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003917
Chris Wilson058d88c2016-08-15 10:49:06 +01003918 if (WARN_ON(vma->obj->pin_display == 0))
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003919 return;
3920
Chris Wilsond8923dc2016-08-18 17:17:07 +01003921 if (--vma->obj->pin_display == 0)
Chris Wilsonf51455d2017-01-10 14:47:34 +00003922 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003923
Chris Wilson383d5822016-08-18 17:17:08 +01003924 /* Bump the LRU to try and avoid premature eviction whilst flipping */
Chris Wilsonbefedbb2017-01-19 19:26:55 +00003925 i915_gem_object_bump_inactive_ggtt(vma->obj);
Chris Wilson383d5822016-08-18 17:17:08 +01003926
Chris Wilson058d88c2016-08-15 10:49:06 +01003927 i915_vma_unpin(vma);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003928}
3929
Eric Anholte47c68e2008-11-14 13:35:19 -08003930/**
3931 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003932 * @obj: object to act on
3933 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08003934 *
3935 * This function returns when the move is complete, including waiting on
3936 * flushes to occur.
3937 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003938int
Chris Wilson919926a2010-11-12 13:42:53 +00003939i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003940{
Eric Anholte47c68e2008-11-14 13:35:19 -08003941 int ret;
3942
Chris Wilsone95433c2016-10-28 13:58:27 +01003943 lockdep_assert_held(&obj->base.dev->struct_mutex);
Chris Wilson4c7d62c2016-10-28 13:58:32 +01003944
Chris Wilsone95433c2016-10-28 13:58:27 +01003945 ret = i915_gem_object_wait(obj,
3946 I915_WAIT_INTERRUPTIBLE |
3947 I915_WAIT_LOCKED |
3948 (write ? I915_WAIT_ALL : 0),
3949 MAX_SCHEDULE_TIMEOUT,
3950 NULL);
Chris Wilson88241782011-01-07 17:09:48 +00003951 if (ret)
3952 return ret;
3953
Chris Wilsonef749212017-04-12 12:01:10 +01003954 flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003955
Eric Anholte47c68e2008-11-14 13:35:19 -08003956 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003957 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson57822dc2017-02-22 11:40:48 +00003958 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
Chris Wilson05394f32010-11-08 19:18:58 +00003959 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003960 }
3961
3962 /* It should now be out of any other write domains, and we can update
3963 * the domain values for our changes.
3964 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003965 GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
Eric Anholte47c68e2008-11-14 13:35:19 -08003966
3967 /* If we're writing through the CPU, then the GPU read domains will
3968 * need to be invalidated at next use.
3969 */
Chris Wilsone27ab732017-06-15 13:38:49 +01003970 if (write)
3971 __start_cpu_write(obj);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003972
3973 return 0;
3974}
3975
Eric Anholt673a3942008-07-30 12:06:12 -07003976/* Throttle our rendering by waiting until the ring has completed our requests
3977 * emitted over 20 msec ago.
3978 *
Eric Anholtb9624422009-06-03 07:27:35 +00003979 * Note that if we were to use the current jiffies each time around the loop,
3980 * we wouldn't escape the function with any frames outstanding if the time to
3981 * render a frame was over 20ms.
3982 *
Eric Anholt673a3942008-07-30 12:06:12 -07003983 * This should get us reasonable parallelism between CPU and GPU but also
3984 * relatively low latency when blocking on a particular request to finish.
3985 */
3986static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003987i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003988{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003989 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003990 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01003991 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00003992 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsone95433c2016-10-28 13:58:27 +01003993 long ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003994
Chris Wilsonf4457ae2016-04-13 17:35:08 +01003995 /* ABI: return -EIO if already wedged */
3996 if (i915_terminally_wedged(&dev_priv->gpu_error))
3997 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003998
Chris Wilson1c255952010-09-26 11:03:27 +01003999 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004000 list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
Eric Anholtb9624422009-06-03 07:27:35 +00004001 if (time_after_eq(request->emitted_jiffies, recent_enough))
4002 break;
4003
Chris Wilsonc8659ef2017-03-02 12:25:25 +00004004 if (target) {
4005 list_del(&target->client_link);
4006 target->file_priv = NULL;
4007 }
John Harrisonfcfa423c2015-05-29 17:44:12 +01004008
John Harrison54fb2412014-11-24 18:49:27 +00004009 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004010 }
John Harrisonff865882014-11-24 18:49:28 +00004011 if (target)
Chris Wilsone8a261e2016-07-20 13:31:49 +01004012 i915_gem_request_get(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004013 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004014
John Harrison54fb2412014-11-24 18:49:27 +00004015 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004016 return 0;
4017
Chris Wilsone95433c2016-10-28 13:58:27 +01004018 ret = i915_wait_request(target,
4019 I915_WAIT_INTERRUPTIBLE,
4020 MAX_SCHEDULE_TIMEOUT);
Chris Wilsone8a261e2016-07-20 13:31:49 +01004021 i915_gem_request_put(target);
John Harrisonff865882014-11-24 18:49:28 +00004022
Chris Wilsone95433c2016-10-28 13:58:27 +01004023 return ret < 0 ? ret : 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004024}
4025
Chris Wilson058d88c2016-08-15 10:49:06 +01004026struct i915_vma *
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004027i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4028 const struct i915_ggtt_view *view,
Chris Wilson91b2db62016-08-04 16:32:23 +01004029 u64 size,
Chris Wilson2ffffd02016-08-04 16:32:22 +01004030 u64 alignment,
4031 u64 flags)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004032{
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004033 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
4034 struct i915_address_space *vm = &dev_priv->ggtt.base;
Chris Wilson59bfa122016-08-04 16:32:31 +01004035 struct i915_vma *vma;
4036 int ret;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004037
Chris Wilson4c7d62c2016-10-28 13:58:32 +01004038 lockdep_assert_held(&obj->base.dev->struct_mutex);
4039
Chris Wilson43ae70d2017-10-09 09:44:01 +01004040 if (!view && flags & PIN_MAPPABLE) {
4041 /* If the required space is larger than the available
4042 * aperture, we will not able to find a slot for the
4043 * object and unbinding the object now will be in
4044 * vain. Worse, doing so may cause us to ping-pong
4045 * the object in and out of the Global GTT and
4046 * waste a lot of cycles under the mutex.
4047 */
4048 if (obj->base.size > dev_priv->ggtt.mappable_end)
4049 return ERR_PTR(-E2BIG);
4050
4051 /* If NONBLOCK is set the caller is optimistically
4052 * trying to cache the full object within the mappable
4053 * aperture, and *must* have a fallback in place for
4054 * situations where we cannot bind the object. We
4055 * can be a little more lax here and use the fallback
4056 * more often to avoid costly migrations of ourselves
4057 * and other objects within the aperture.
4058 *
4059 * Half-the-aperture is used as a simple heuristic.
4060 * More interesting would to do search for a free
4061 * block prior to making the commitment to unbind.
4062 * That caters for the self-harm case, and with a
4063 * little more heuristics (e.g. NOFAULT, NOEVICT)
4064 * we could try to minimise harm to others.
4065 */
4066 if (flags & PIN_NONBLOCK &&
4067 obj->base.size > dev_priv->ggtt.mappable_end / 2)
4068 return ERR_PTR(-ENOSPC);
4069 }
4070
Chris Wilson718659a2017-01-16 15:21:28 +00004071 vma = i915_vma_instance(obj, vm, view);
Chris Wilsone0216b72017-01-19 19:26:57 +00004072 if (unlikely(IS_ERR(vma)))
Chris Wilson058d88c2016-08-15 10:49:06 +01004073 return vma;
Chris Wilson59bfa122016-08-04 16:32:31 +01004074
4075 if (i915_vma_misplaced(vma, size, alignment, flags)) {
Chris Wilson43ae70d2017-10-09 09:44:01 +01004076 if (flags & PIN_NONBLOCK) {
4077 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
4078 return ERR_PTR(-ENOSPC);
Chris Wilson59bfa122016-08-04 16:32:31 +01004079
Chris Wilson43ae70d2017-10-09 09:44:01 +01004080 if (flags & PIN_MAPPABLE &&
Chris Wilson944397f2017-01-09 16:16:11 +00004081 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
Chris Wilsonad16d2e2016-10-13 09:55:04 +01004082 return ERR_PTR(-ENOSPC);
4083 }
4084
Chris Wilson59bfa122016-08-04 16:32:31 +01004085 WARN(i915_vma_is_pinned(vma),
4086 "bo is already pinned in ggtt with incorrect alignment:"
Chris Wilson05a20d02016-08-18 17:16:55 +01004087 " offset=%08x, req.alignment=%llx,"
4088 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
4089 i915_ggtt_offset(vma), alignment,
Chris Wilson59bfa122016-08-04 16:32:31 +01004090 !!(flags & PIN_MAPPABLE),
Chris Wilson05a20d02016-08-18 17:16:55 +01004091 i915_vma_is_map_and_fenceable(vma));
Chris Wilson59bfa122016-08-04 16:32:31 +01004092 ret = i915_vma_unbind(vma);
4093 if (ret)
Chris Wilson058d88c2016-08-15 10:49:06 +01004094 return ERR_PTR(ret);
Chris Wilson59bfa122016-08-04 16:32:31 +01004095 }
4096
Chris Wilson058d88c2016-08-15 10:49:06 +01004097 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
4098 if (ret)
4099 return ERR_PTR(ret);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004100
Chris Wilson058d88c2016-08-15 10:49:06 +01004101 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07004102}
4103
Chris Wilsonedf6b762016-08-09 09:23:33 +01004104static __always_inline unsigned int __busy_read_flag(unsigned int id)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004105{
4106 /* Note that we could alias engines in the execbuf API, but
4107 * that would be very unwise as it prevents userspace from
4108 * fine control over engine selection. Ahem.
4109 *
4110 * This should be something like EXEC_MAX_ENGINE instead of
4111 * I915_NUM_ENGINES.
4112 */
4113 BUILD_BUG_ON(I915_NUM_ENGINES > 16);
4114 return 0x10000 << id;
4115}
4116
4117static __always_inline unsigned int __busy_write_id(unsigned int id)
4118{
Chris Wilson70cb4722016-08-09 18:08:25 +01004119 /* The uABI guarantees an active writer is also amongst the read
4120 * engines. This would be true if we accessed the activity tracking
4121 * under the lock, but as we perform the lookup of the object and
4122 * its activity locklessly we can not guarantee that the last_write
4123 * being active implies that we have set the same engine flag from
4124 * last_read - hence we always set both read and write busy for
4125 * last_write.
4126 */
4127 return id | __busy_read_flag(id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004128}
4129
Chris Wilsonedf6b762016-08-09 09:23:33 +01004130static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004131__busy_set_if_active(const struct dma_fence *fence,
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004132 unsigned int (*flag)(unsigned int id))
4133{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004134 struct drm_i915_gem_request *rq;
Chris Wilson12555012016-08-16 09:50:40 +01004135
Chris Wilsond07f0e52016-10-28 13:58:44 +01004136 /* We have to check the current hw status of the fence as the uABI
4137 * guarantees forward progress. We could rely on the idle worker
4138 * to eventually flush us, but to minimise latency just ask the
4139 * hardware.
4140 *
4141 * Note we only report on the status of native fences.
4142 */
4143 if (!dma_fence_is_i915(fence))
Chris Wilson12555012016-08-16 09:50:40 +01004144 return 0;
4145
Chris Wilsond07f0e52016-10-28 13:58:44 +01004146 /* opencode to_request() in order to avoid const warnings */
4147 rq = container_of(fence, struct drm_i915_gem_request, fence);
4148 if (i915_gem_request_completed(rq))
4149 return 0;
4150
Chris Wilson1d39f282017-04-11 13:43:06 +01004151 return flag(rq->engine->uabi_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004152}
4153
Chris Wilsonedf6b762016-08-09 09:23:33 +01004154static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004155busy_check_reader(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004156{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004157 return __busy_set_if_active(fence, __busy_read_flag);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004158}
4159
Chris Wilsonedf6b762016-08-09 09:23:33 +01004160static __always_inline unsigned int
Chris Wilsond07f0e52016-10-28 13:58:44 +01004161busy_check_writer(const struct dma_fence *fence)
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004162{
Chris Wilsond07f0e52016-10-28 13:58:44 +01004163 if (!fence)
4164 return 0;
4165
4166 return __busy_set_if_active(fence, __busy_write_id);
Chris Wilson3fdc13c2016-08-05 10:14:18 +01004167}
4168
Eric Anholt673a3942008-07-30 12:06:12 -07004169int
Eric Anholt673a3942008-07-30 12:06:12 -07004170i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004171 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004172{
4173 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004174 struct drm_i915_gem_object *obj;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004175 struct reservation_object_list *list;
4176 unsigned int seq;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004177 int err;
Eric Anholt673a3942008-07-30 12:06:12 -07004178
Chris Wilsond07f0e52016-10-28 13:58:44 +01004179 err = -ENOENT;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004180 rcu_read_lock();
4181 obj = i915_gem_object_lookup_rcu(file, args->handle);
Chris Wilsond07f0e52016-10-28 13:58:44 +01004182 if (!obj)
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004183 goto out;
Chris Wilsond07f0e52016-10-28 13:58:44 +01004184
4185 /* A discrepancy here is that we do not report the status of
4186 * non-i915 fences, i.e. even though we may report the object as idle,
4187 * a call to set-domain may still stall waiting for foreign rendering.
4188 * This also means that wait-ioctl may report an object as busy,
4189 * where busy-ioctl considers it idle.
4190 *
4191 * We trade the ability to warn of foreign fences to report on which
4192 * i915 engines are active for the object.
4193 *
4194 * Alternatively, we can trade that extra information on read/write
4195 * activity with
4196 * args->busy =
4197 * !reservation_object_test_signaled_rcu(obj->resv, true);
4198 * to report the overall busyness. This is what the wait-ioctl does.
4199 *
4200 */
4201retry:
4202 seq = raw_read_seqcount(&obj->resv->seq);
4203
4204 /* Translate the exclusive fence to the READ *and* WRITE engine */
4205 args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
4206
4207 /* Translate shared fences to READ set of engines */
4208 list = rcu_dereference(obj->resv->fence);
4209 if (list) {
4210 unsigned int shared_count = list->shared_count, i;
4211
4212 for (i = 0; i < shared_count; ++i) {
4213 struct dma_fence *fence =
4214 rcu_dereference(list->shared[i]);
4215
4216 args->busy |= busy_check_reader(fence);
4217 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004218 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004219
Chris Wilsond07f0e52016-10-28 13:58:44 +01004220 if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
4221 goto retry;
Chris Wilson426960b2016-01-15 16:51:46 +00004222
Chris Wilsond07f0e52016-10-28 13:58:44 +01004223 err = 0;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004224out:
4225 rcu_read_unlock();
4226 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07004227}
4228
4229int
4230i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4231 struct drm_file *file_priv)
4232{
Akshay Joshi0206e352011-08-16 15:34:10 -04004233 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004234}
4235
Chris Wilson3ef94da2009-09-14 16:50:29 +01004236int
4237i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4238 struct drm_file *file_priv)
4239{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004240 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3ef94da2009-09-14 16:50:29 +01004241 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004242 struct drm_i915_gem_object *obj;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004243 int err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004244
4245 switch (args->madv) {
4246 case I915_MADV_DONTNEED:
4247 case I915_MADV_WILLNEED:
4248 break;
4249 default:
4250 return -EINVAL;
4251 }
4252
Chris Wilson03ac0642016-07-20 13:31:51 +01004253 obj = i915_gem_object_lookup(file_priv, args->handle);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004254 if (!obj)
4255 return -ENOENT;
4256
4257 err = mutex_lock_interruptible(&obj->mm.lock);
4258 if (err)
4259 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004260
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004261 if (obj->mm.pages &&
Chris Wilson3e510a82016-08-05 10:14:23 +01004262 i915_gem_object_is_tiled(obj) &&
Daniel Vetter656bfa32014-11-20 09:26:30 +01004263 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004264 if (obj->mm.madv == I915_MADV_WILLNEED) {
4265 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004266 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004267 obj->mm.quirked = false;
4268 }
4269 if (args->madv == I915_MADV_WILLNEED) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +00004270 GEM_BUG_ON(obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004271 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004272 obj->mm.quirked = true;
4273 }
Daniel Vetter656bfa32014-11-20 09:26:30 +01004274 }
4275
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004276 if (obj->mm.madv != __I915_MADV_PURGED)
4277 obj->mm.madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004278
Chris Wilson6c085a72012-08-20 11:40:46 +02004279 /* if the object is no longer attached, discard its backing storage */
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004280 if (obj->mm.madv == I915_MADV_DONTNEED && !obj->mm.pages)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004281 i915_gem_object_truncate(obj);
4282
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004283 args->retained = obj->mm.madv != __I915_MADV_PURGED;
Chris Wilson1233e2d2016-10-28 13:58:37 +01004284 mutex_unlock(&obj->mm.lock);
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004285
Chris Wilson1233e2d2016-10-28 13:58:37 +01004286out:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01004287 i915_gem_object_put(obj);
Chris Wilson1233e2d2016-10-28 13:58:37 +01004288 return err;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004289}
4290
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004291static void
4292frontbuffer_retire(struct i915_gem_active *active,
4293 struct drm_i915_gem_request *request)
4294{
4295 struct drm_i915_gem_object *obj =
4296 container_of(active, typeof(*obj), frontbuffer_write);
4297
Chris Wilsond59b21e2017-02-22 11:40:49 +00004298 intel_fb_obj_flush(obj, ORIGIN_CS);
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004299}
4300
Chris Wilson37e680a2012-06-07 15:38:42 +01004301void i915_gem_object_init(struct drm_i915_gem_object *obj,
4302 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004303{
Chris Wilson1233e2d2016-10-28 13:58:37 +01004304 mutex_init(&obj->mm.lock);
4305
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004306 INIT_LIST_HEAD(&obj->global_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004307 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilsond1b48c12017-08-16 09:52:08 +01004308 INIT_LIST_HEAD(&obj->lut_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004309 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004310
Chris Wilson37e680a2012-06-07 15:38:42 +01004311 obj->ops = ops;
4312
Chris Wilsond07f0e52016-10-28 13:58:44 +01004313 reservation_object_init(&obj->__builtin_resv);
4314 obj->resv = &obj->__builtin_resv;
4315
Chris Wilson50349242016-08-18 17:17:04 +01004316 obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
Chris Wilson5b8c8ae2016-11-16 19:07:04 +00004317 init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004318
4319 obj->mm.madv = I915_MADV_WILLNEED;
4320 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
4321 mutex_init(&obj->mm.get_page.lock);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004322
Dave Gordonf19ec8c2016-07-04 11:34:37 +01004323 i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004324}
4325
Chris Wilson37e680a2012-06-07 15:38:42 +01004326static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Tvrtko Ursulin3599a912016-11-01 14:44:10 +00004327 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
4328 I915_GEM_OBJECT_IS_SHRINKABLE,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004329
Chris Wilson37e680a2012-06-07 15:38:42 +01004330 .get_pages = i915_gem_object_get_pages_gtt,
4331 .put_pages = i915_gem_object_put_pages_gtt,
Chris Wilson7c55e2c2017-03-07 12:03:38 +00004332
4333 .pwrite = i915_gem_object_pwrite_gtt,
Chris Wilson37e680a2012-06-07 15:38:42 +01004334};
4335
Matthew Auld465c4032017-10-06 23:18:14 +01004336static int i915_gem_object_create_shmem(struct drm_device *dev,
4337 struct drm_gem_object *obj,
4338 size_t size)
4339{
4340 struct drm_i915_private *i915 = to_i915(dev);
4341 unsigned long flags = VM_NORESERVE;
4342 struct file *filp;
4343
4344 drm_gem_private_object_init(dev, obj, size);
4345
4346 if (i915->mm.gemfs)
4347 filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
4348 flags);
4349 else
4350 filp = shmem_file_setup("i915", size, flags);
4351
4352 if (IS_ERR(filp))
4353 return PTR_ERR(filp);
4354
4355 obj->filp = filp;
4356
4357 return 0;
4358}
4359
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004360struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00004361i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004362{
Daniel Vetterc397b902010-04-09 19:05:07 +00004363 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004364 struct address_space *mapping;
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004365 unsigned int cache_level;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004366 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004367 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004368
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004369 /* There is a prevalence of the assumption that we fit the object's
4370 * page count inside a 32bit _signed_ variable. Let's document this and
4371 * catch if we ever need to fix it. In the meantime, if you do spot
4372 * such a local variable, please consider fixing!
4373 */
Tvrtko Ursulin7a3ee5d2017-03-30 17:31:30 +01004374 if (size >> PAGE_SHIFT > INT_MAX)
Chris Wilsonb4bcbe22016-10-18 13:02:49 +01004375 return ERR_PTR(-E2BIG);
4376
4377 if (overflows_type(size, obj->base.size))
4378 return ERR_PTR(-E2BIG);
4379
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00004380 obj = i915_gem_object_alloc(dev_priv);
Daniel Vetterc397b902010-04-09 19:05:07 +00004381 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004382 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004383
Matthew Auld465c4032017-10-06 23:18:14 +01004384 ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004385 if (ret)
4386 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004387
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004388 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
Jani Nikulac0f86832016-12-07 12:13:04 +02004389 if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004390 /* 965gm cannot relocate objects above 4GiB. */
4391 mask &= ~__GFP_HIGHMEM;
4392 mask |= __GFP_DMA32;
4393 }
4394
Al Viro93c76a32015-12-04 23:45:44 -05004395 mapping = obj->base.filp->f_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004396 mapping_set_gfp_mask(mapping, mask);
Chris Wilson4846bf02017-06-09 12:03:46 +01004397 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
Hugh Dickins5949eac2011-06-27 16:18:18 -07004398
Chris Wilson37e680a2012-06-07 15:38:42 +01004399 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004400
Daniel Vetterc397b902010-04-09 19:05:07 +00004401 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4402 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4403
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004404 if (HAS_LLC(dev_priv))
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004405 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004406 * cache) for about a 10% performance improvement
4407 * compared to uncached. Graphics requests other than
4408 * display scanout are coherent with the CPU in
4409 * accessing this cache. This means in this mode we
4410 * don't need to clflush on the CPU side, and on the
4411 * GPU side we only need to flush internal caches to
4412 * get data visible to the CPU.
4413 *
4414 * However, we maintain the display planes as UC, and so
4415 * need to rebind when first used as such.
4416 */
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004417 cache_level = I915_CACHE_LLC;
4418 else
4419 cache_level = I915_CACHE_NONE;
Eric Anholta1871112011-03-29 16:59:55 -07004420
Chris Wilsonb8f55be2017-08-11 12:11:16 +01004421 i915_gem_object_set_cache_coherency(obj, cache_level);
Chris Wilsone27ab732017-06-15 13:38:49 +01004422
Daniel Vetterd861e332013-07-24 23:25:03 +02004423 trace_i915_gem_object_create(obj);
4424
Chris Wilson05394f32010-11-08 19:18:58 +00004425 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004426
4427fail:
4428 i915_gem_object_free(obj);
Chris Wilsonfe3db792016-04-25 13:32:13 +01004429 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004430}
4431
Chris Wilson340fbd82014-05-22 09:16:52 +01004432static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4433{
4434 /* If we are the last user of the backing storage (be it shmemfs
4435 * pages or stolen etc), we know that the pages are going to be
4436 * immediately released. In this case, we can then skip copying
4437 * back the contents from the GPU.
4438 */
4439
Chris Wilsona4f5ea62016-10-28 13:58:35 +01004440 if (obj->mm.madv != I915_MADV_WILLNEED)
Chris Wilson340fbd82014-05-22 09:16:52 +01004441 return false;
4442
4443 if (obj->base.filp == NULL)
4444 return true;
4445
4446 /* At first glance, this looks racy, but then again so would be
4447 * userspace racing mmap against close. However, the first external
4448 * reference to the filp can only be obtained through the
4449 * i915_gem_mmap_ioctl() which safeguards us against the user
4450 * acquiring such a reference whilst we are in the middle of
4451 * freeing the object.
4452 */
4453 return atomic_long_read(&obj->base.filp->f_count) == 1;
4454}
4455
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004456static void __i915_gem_free_objects(struct drm_i915_private *i915,
4457 struct llist_node *freed)
Chris Wilsonbe726152010-07-23 23:18:50 +01004458{
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004459 struct drm_i915_gem_object *obj, *on;
Chris Wilsonbe726152010-07-23 23:18:50 +01004460
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004461 mutex_lock(&i915->drm.struct_mutex);
4462 intel_runtime_pm_get(i915);
4463 llist_for_each_entry(obj, freed, freed) {
4464 struct i915_vma *vma, *vn;
Paulo Zanonif65c9162013-11-27 18:20:34 -02004465
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004466 trace_i915_gem_object_destroy(obj);
4467
4468 GEM_BUG_ON(i915_gem_object_is_active(obj));
4469 list_for_each_entry_safe(vma, vn,
4470 &obj->vma_list, obj_link) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004471 GEM_BUG_ON(i915_vma_is_active(vma));
4472 vma->flags &= ~I915_VMA_PIN_MASK;
4473 i915_vma_close(vma);
4474 }
Chris Wilsondb6c2b42016-11-01 11:54:00 +00004475 GEM_BUG_ON(!list_empty(&obj->vma_list));
4476 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004477
Joonas Lahtinen56cea322016-11-02 12:16:04 +02004478 list_del(&obj->global_link);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004479 }
4480 intel_runtime_pm_put(i915);
4481 mutex_unlock(&i915->drm.struct_mutex);
4482
Chris Wilsonf2be9d62017-04-07 11:25:52 +01004483 cond_resched();
4484
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004485 llist_for_each_entry_safe(obj, on, freed, freed) {
4486 GEM_BUG_ON(obj->bind_count);
Chris Wilsona65adaf2017-10-09 09:43:57 +01004487 GEM_BUG_ON(obj->userfault_count);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004488 GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
Chris Wilson67b48042017-08-22 12:05:16 +01004489 GEM_BUG_ON(!list_empty(&obj->lut_list));
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004490
4491 if (obj->ops->release)
4492 obj->ops->release(obj);
4493
4494 if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
4495 atomic_set(&obj->mm.pages_pin_count, 0);
Chris Wilson548625e2016-11-01 12:11:34 +00004496 __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004497 GEM_BUG_ON(obj->mm.pages);
4498
4499 if (obj->base.import_attach)
4500 drm_prime_gem_destroy(&obj->base, NULL);
4501
Chris Wilsond07f0e52016-10-28 13:58:44 +01004502 reservation_object_fini(&obj->__builtin_resv);
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004503 drm_gem_object_release(&obj->base);
4504 i915_gem_info_remove_obj(i915, obj->base.size);
4505
4506 kfree(obj->bit_17);
4507 i915_gem_object_free(obj);
4508 }
4509}
4510
4511static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
4512{
4513 struct llist_node *freed;
4514
4515 freed = llist_del_all(&i915->mm.free_list);
4516 if (unlikely(freed))
4517 __i915_gem_free_objects(i915, freed);
4518}
4519
4520static void __i915_gem_free_work(struct work_struct *work)
4521{
4522 struct drm_i915_private *i915 =
4523 container_of(work, struct drm_i915_private, mm.free_work);
4524 struct llist_node *freed;
Chris Wilson26e12f82011-03-20 11:20:19 +00004525
Chris Wilsonb1f788c2016-08-04 07:52:45 +01004526 /* All file-owned VMA should have been released by this point through
4527 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4528 * However, the object may also be bound into the global GTT (e.g.
4529 * older GPUs without per-process support, or for direct access through
4530 * the GTT either for the user or for scanout). Those VMA still need to
4531 * unbound now.
4532 */
Chris Wilson1488fc02012-04-24 15:47:31 +01004533
Chris Wilson5ad08be2017-04-07 11:25:51 +01004534 while ((freed = llist_del_all(&i915->mm.free_list))) {
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004535 __i915_gem_free_objects(i915, freed);
Chris Wilson5ad08be2017-04-07 11:25:51 +01004536 if (need_resched())
4537 break;
4538 }
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004539}
4540
4541static void __i915_gem_free_object_rcu(struct rcu_head *head)
4542{
4543 struct drm_i915_gem_object *obj =
4544 container_of(head, typeof(*obj), rcu);
4545 struct drm_i915_private *i915 = to_i915(obj->base.dev);
4546
4547 /* We can't simply use call_rcu() from i915_gem_free_object()
4548 * as we need to block whilst unbinding, and the call_rcu
4549 * task may be called from softirq context. So we take a
4550 * detour through a worker.
4551 */
4552 if (llist_add(&obj->freed, &i915->mm.free_list))
4553 schedule_work(&i915->mm.free_work);
4554}
4555
4556void i915_gem_free_object(struct drm_gem_object *gem_obj)
4557{
4558 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4559
Chris Wilsonbc0629a2016-11-01 10:03:17 +00004560 if (obj->mm.quirked)
4561 __i915_gem_object_unpin_pages(obj);
4562
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004563 if (discard_backing_storage(obj))
4564 obj->mm.madv = I915_MADV_DONTNEED;
Daniel Vettera071fa02014-06-18 23:28:09 +02004565
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01004566 /* Before we free the object, make sure any pure RCU-only
4567 * read-side critical sections are complete, e.g.
4568 * i915_gem_busy_ioctl(). For the corresponding synchronized
4569 * lookup see i915_gem_object_lookup_rcu().
4570 */
4571 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
Chris Wilsonbe726152010-07-23 23:18:50 +01004572}
4573
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004574void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
4575{
4576 lockdep_assert_held(&obj->base.dev->struct_mutex);
4577
Chris Wilsond1b48c12017-08-16 09:52:08 +01004578 if (!i915_gem_object_has_active_reference(obj) &&
4579 i915_gem_object_is_active(obj))
Chris Wilsonf8a7fde2016-10-28 13:58:29 +01004580 i915_gem_object_set_active_reference(obj);
4581 else
4582 i915_gem_object_put(obj);
4583}
4584
Chris Wilson3033aca2016-10-28 13:58:47 +01004585static void assert_kernel_context_is_current(struct drm_i915_private *dev_priv)
4586{
4587 struct intel_engine_cs *engine;
4588 enum intel_engine_id id;
4589
4590 for_each_engine(engine, dev_priv, id)
Chris Wilsonf131e352016-12-29 14:40:37 +00004591 GEM_BUG_ON(engine->last_retired_context &&
4592 !i915_gem_context_is_kernel(engine->last_retired_context));
Chris Wilson3033aca2016-10-28 13:58:47 +01004593}
4594
Chris Wilson24145512017-01-24 11:01:35 +00004595void i915_gem_sanitize(struct drm_i915_private *i915)
4596{
Chris Wilsonf36325f2017-08-26 12:09:34 +01004597 if (i915_terminally_wedged(&i915->gpu_error)) {
4598 mutex_lock(&i915->drm.struct_mutex);
4599 i915_gem_unset_wedged(i915);
4600 mutex_unlock(&i915->drm.struct_mutex);
4601 }
4602
Chris Wilson24145512017-01-24 11:01:35 +00004603 /*
4604 * If we inherit context state from the BIOS or earlier occupants
4605 * of the GPU, the GPU may be in an inconsistent state when we
4606 * try to take over. The only way to remove the earlier state
4607 * is by resetting. However, resetting on earlier gen is tricky as
4608 * it may impact the display and we are uncertain about the stability
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004609 * of the reset, so this could be applied to even earlier gen.
Chris Wilson24145512017-01-24 11:01:35 +00004610 */
Joonas Lahtinenea117b82017-04-28 10:53:38 +03004611 if (INTEL_GEN(i915) >= 5) {
Chris Wilson24145512017-01-24 11:01:35 +00004612 int reset = intel_gpu_reset(i915, ALL_ENGINES);
4613 WARN_ON(reset && reset != -ENODEV);
4614 }
4615}
4616
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004617int i915_gem_suspend(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004618{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004619 struct drm_device *dev = &dev_priv->drm;
Chris Wilsondcff85c2016-08-05 10:14:11 +01004620 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004621
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004622 intel_runtime_pm_get(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01004623 intel_suspend_gt_powersave(dev_priv);
4624
Chris Wilson45c5f202013-10-16 11:50:01 +01004625 mutex_lock(&dev->struct_mutex);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004626
4627 /* We have to flush all the executing contexts to main memory so
4628 * that they can saved in the hibernation image. To ensure the last
4629 * context image is coherent, we have to switch away from it. That
4630 * leaves the dev_priv->kernel_context still active when
4631 * we actually suspend, and its image in memory may not match the GPU
4632 * state. Fortunately, the kernel_context is disposable and we do
4633 * not rely on its state.
4634 */
4635 ret = i915_gem_switch_to_kernel_context(dev_priv);
4636 if (ret)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004637 goto err_unlock;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004638
Chris Wilson22dd3bb2016-09-09 14:11:50 +01004639 ret = i915_gem_wait_for_idle(dev_priv,
4640 I915_WAIT_INTERRUPTIBLE |
4641 I915_WAIT_LOCKED);
Chris Wilsoncad99462017-08-26 12:09:33 +01004642 if (ret && ret != -EIO)
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004643 goto err_unlock;
Chris Wilsonf7403342013-09-13 23:57:04 +01004644
Chris Wilson3033aca2016-10-28 13:58:47 +01004645 assert_kernel_context_is_current(dev_priv);
Chris Wilson829a0af2017-06-20 12:05:45 +01004646 i915_gem_contexts_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004647 mutex_unlock(&dev->struct_mutex);
4648
Sagar Arun Kamble63987bf2017-04-05 15:51:50 +05304649 intel_guc_suspend(dev_priv);
4650
Chris Wilson737b1502015-01-26 18:03:03 +02004651 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson67d97da2016-07-04 08:08:31 +01004652 cancel_delayed_work_sync(&dev_priv->gt.retire_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004653
4654 /* As the idle_work is rearming if it detects a race, play safe and
4655 * repeat the flush until it is definitely idle.
4656 */
Chris Wilson7c262402017-10-06 11:40:38 +01004657 drain_delayed_work(&dev_priv->gt.idle_work);
Chris Wilsonbdeb9782016-12-23 14:57:56 +00004658
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004659 /* Assert that we sucessfully flushed all the work and
4660 * reset the GPU back to its idle, low power state.
4661 */
Chris Wilson67d97da2016-07-04 08:08:31 +01004662 WARN_ON(dev_priv->gt.awake);
Chris Wilsonfc692bd2017-08-26 12:09:35 +01004663 if (WARN_ON(!intel_engines_are_idle(dev_priv)))
4664 i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004665
Imre Deak1c777c52016-10-12 17:46:37 +03004666 /*
4667 * Neither the BIOS, ourselves or any other kernel
4668 * expects the system to be in execlists mode on startup,
4669 * so we need to reset the GPU back to legacy mode. And the only
4670 * known way to disable logical contexts is through a GPU reset.
4671 *
4672 * So in order to leave the system in a known default configuration,
4673 * always reset the GPU upon unload and suspend. Afterwards we then
4674 * clean up the GEM state tracking, flushing off the requests and
4675 * leaving the system in a known idle state.
4676 *
4677 * Note that is of the upmost importance that the GPU is idle and
4678 * all stray writes are flushed *before* we dismantle the backing
4679 * storage for the pinned objects.
4680 *
4681 * However, since we are uncertain that resetting the GPU on older
4682 * machines is a good idea, we don't - just in case it leaves the
4683 * machine in an unusable condition.
4684 */
Chris Wilson24145512017-01-24 11:01:35 +00004685 i915_gem_sanitize(dev_priv);
Chris Wilsoncad99462017-08-26 12:09:33 +01004686
4687 intel_runtime_pm_put(dev_priv);
4688 return 0;
Imre Deak1c777c52016-10-12 17:46:37 +03004689
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004690err_unlock:
Chris Wilson45c5f202013-10-16 11:50:01 +01004691 mutex_unlock(&dev->struct_mutex);
Chris Wilsonc998e8a2017-03-02 08:30:29 +00004692 intel_runtime_pm_put(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004693 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004694}
4695
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004696void i915_gem_resume(struct drm_i915_private *dev_priv)
Chris Wilson5ab57c72016-07-15 14:56:20 +01004697{
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004698 struct drm_device *dev = &dev_priv->drm;
Chris Wilson5ab57c72016-07-15 14:56:20 +01004699
Imre Deak31ab49a2016-11-07 11:20:05 +02004700 WARN_ON(dev_priv->gt.awake);
4701
Chris Wilson5ab57c72016-07-15 14:56:20 +01004702 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin275a9912016-11-16 08:55:34 +00004703 i915_gem_restore_gtt_mappings(dev_priv);
Sagar Arun Kamble269e6ea2017-09-29 10:28:36 +05304704 i915_gem_restore_fences(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004705
4706 /* As we didn't flush the kernel context before suspend, we cannot
4707 * guarantee that the context image is complete. So let's just reset
4708 * it and start again.
4709 */
Chris Wilson821ed7d2016-09-09 14:11:53 +01004710 dev_priv->gt.resume(dev_priv);
Chris Wilson5ab57c72016-07-15 14:56:20 +01004711
4712 mutex_unlock(&dev->struct_mutex);
4713}
4714
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004715void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004716{
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004717 if (INTEL_GEN(dev_priv) < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004718 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4719 return;
4720
4721 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4722 DISP_TILE_SURFACE_SWIZZLING);
4723
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004724 if (IS_GEN5(dev_priv))
Daniel Vetter11782b02012-01-31 16:47:55 +01004725 return;
4726
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004727 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004728 if (IS_GEN6(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004729 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004730 else if (IS_GEN7(dev_priv))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004731 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004732 else if (IS_GEN8(dev_priv))
Ben Widawsky31a53362013-11-02 21:07:04 -07004733 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004734 else
4735 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004736}
Daniel Vettere21af882012-02-09 20:53:27 +01004737
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004738static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004739{
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004740 I915_WRITE(RING_CTL(base), 0);
4741 I915_WRITE(RING_HEAD(base), 0);
4742 I915_WRITE(RING_TAIL(base), 0);
4743 I915_WRITE(RING_START(base), 0);
4744}
4745
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004746static void init_unused_rings(struct drm_i915_private *dev_priv)
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004747{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004748 if (IS_I830(dev_priv)) {
4749 init_unused_ring(dev_priv, PRB1_BASE);
4750 init_unused_ring(dev_priv, SRB0_BASE);
4751 init_unused_ring(dev_priv, SRB1_BASE);
4752 init_unused_ring(dev_priv, SRB2_BASE);
4753 init_unused_ring(dev_priv, SRB3_BASE);
4754 } else if (IS_GEN2(dev_priv)) {
4755 init_unused_ring(dev_priv, SRB0_BASE);
4756 init_unused_ring(dev_priv, SRB1_BASE);
4757 } else if (IS_GEN3(dev_priv)) {
4758 init_unused_ring(dev_priv, PRB1_BASE);
4759 init_unused_ring(dev_priv, PRB2_BASE);
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004760 }
4761}
4762
Chris Wilson20a8a742017-02-08 14:30:31 +00004763static int __i915_gem_restart_engines(void *data)
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004764{
Chris Wilson20a8a742017-02-08 14:30:31 +00004765 struct drm_i915_private *i915 = data;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004766 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304767 enum intel_engine_id id;
Chris Wilson20a8a742017-02-08 14:30:31 +00004768 int err;
4769
4770 for_each_engine(engine, i915, id) {
4771 err = engine->init_hw(engine);
4772 if (err)
4773 return err;
4774 }
4775
4776 return 0;
4777}
4778
4779int i915_gem_init_hw(struct drm_i915_private *dev_priv)
4780{
Chris Wilsond200cda2016-04-28 09:56:44 +01004781 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004782
Chris Wilsonde867c22016-10-25 13:16:02 +01004783 dev_priv->gt.last_init_time = ktime_get();
4784
Chris Wilson5e4f5182015-02-13 14:35:59 +00004785 /* Double layer security blanket, see i915_gem_init() */
4786 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4787
Tvrtko Ursulin0031fb92016-11-04 14:42:44 +00004788 if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004789 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004790
Tvrtko Ursulin772c2a52016-10-13 11:03:01 +01004791 if (IS_HASWELL(dev_priv))
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004792 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004793 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004794
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01004795 if (HAS_PCH_NOP(dev_priv)) {
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004796 if (IS_IVYBRIDGE(dev_priv)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004797 u32 temp = I915_READ(GEN7_MSG_CTL);
4798 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4799 I915_WRITE(GEN7_MSG_CTL, temp);
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004800 } else if (INTEL_GEN(dev_priv) >= 7) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004801 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4802 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4803 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4804 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004805 }
4806
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004807 i915_gem_init_swizzling(dev_priv);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004808
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004809 /*
4810 * At least 830 can leave some of the unused rings
4811 * "active" (ie. head != tail) after resume which
4812 * will prevent c3 entry. Makes sure all unused rings
4813 * are totally idle.
4814 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01004815 init_unused_rings(dev_priv);
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004816
Dave Gordoned54c1a2016-01-19 19:02:54 +00004817 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004818
Tvrtko Ursulinc6be6072016-11-16 08:55:31 +00004819 ret = i915_ppgtt_init_hw(dev_priv);
John Harrison4ad2fd82015-06-18 13:11:20 +01004820 if (ret) {
4821 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4822 goto out;
4823 }
4824
4825 /* Need to do basic initialisation of all rings first: */
Chris Wilson20a8a742017-02-08 14:30:31 +00004826 ret = __i915_gem_restart_engines(dev_priv);
4827 if (ret)
4828 goto out;
Mika Kuoppala99433932013-01-22 14:12:17 +02004829
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004830 intel_mocs_init_l3cc_table(dev_priv);
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004831
Oscar Mateob8991402017-03-28 09:53:47 -07004832 /* We can't enable contexts until all firmware is loaded */
4833 ret = intel_uc_init_hw(dev_priv);
4834 if (ret)
4835 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004836
Chris Wilson5e4f5182015-02-13 14:35:59 +00004837out:
4838 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004839 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004840}
4841
Chris Wilson39df9192016-07-20 13:31:57 +01004842bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
4843{
4844 if (INTEL_INFO(dev_priv)->gen < 6)
4845 return false;
4846
4847 /* TODO: make semaphores and Execlists play nicely together */
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00004848 if (i915_modparams.enable_execlists)
Chris Wilson39df9192016-07-20 13:31:57 +01004849 return false;
4850
4851 if (value >= 0)
4852 return value;
4853
Chris Wilson39df9192016-07-20 13:31:57 +01004854 /* Enable semaphores on SNB when IO remapping is off */
Chris Wilson80debff2017-05-25 13:16:12 +01004855 if (IS_GEN6(dev_priv) && intel_vtd_active())
Chris Wilson39df9192016-07-20 13:31:57 +01004856 return false;
Chris Wilson39df9192016-07-20 13:31:57 +01004857
4858 return true;
4859}
4860
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004861int i915_gem_init(struct drm_i915_private *dev_priv)
Chris Wilson1070a422012-04-24 15:47:41 +01004862{
Chris Wilson1070a422012-04-24 15:47:41 +01004863 int ret;
4864
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004865 mutex_lock(&dev_priv->drm.struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004866
Matthew Auldda9fe3f32017-10-06 23:18:31 +01004867 /*
4868 * We need to fallback to 4K pages since gvt gtt handling doesn't
4869 * support huge page entries - we will need to check either hypervisor
4870 * mm can support huge guest page or just do emulation in gvt.
4871 */
4872 if (intel_vgpu_active(dev_priv))
4873 mkwrite_device_info(dev_priv)->page_sizes =
4874 I915_GTT_PAGE_SIZE_4K;
4875
Chris Wilson94312822017-05-03 10:39:18 +01004876 dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
Chris Wilson57822dc2017-02-22 11:40:48 +00004877
Michal Wajdeczko4f044a82017-09-19 19:38:44 +00004878 if (!i915_modparams.enable_execlists) {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004879 dev_priv->gt.resume = intel_legacy_submission_resume;
Chris Wilson7e37f882016-08-02 22:50:21 +01004880 dev_priv->gt.cleanup_engine = intel_engine_cleanup;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004881 } else {
Chris Wilson821ed7d2016-09-09 14:11:53 +01004882 dev_priv->gt.resume = intel_lr_context_resume;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004883 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004884 }
4885
Chris Wilson5e4f5182015-02-13 14:35:59 +00004886 /* This is just a security blanket to placate dragons.
4887 * On some systems, we very sporadically observe that the first TLBs
4888 * used by the CS may be stale, despite us poking the TLB reset. If
4889 * we hold the forcewake during initialisation these problems
4890 * just magically go away.
4891 */
4892 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4893
Chris Wilson8a2421b2017-06-16 15:05:22 +01004894 ret = i915_gem_init_userptr(dev_priv);
4895 if (ret)
4896 goto out_unlock;
Chris Wilsonf6b9d5c2016-08-04 07:52:23 +01004897
4898 ret = i915_gem_init_ggtt(dev_priv);
4899 if (ret)
4900 goto out_unlock;
Jesse Barnesd62b4892013-03-08 10:45:53 -08004901
Chris Wilson829a0af2017-06-20 12:05:45 +01004902 ret = i915_gem_contexts_init(dev_priv);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004903 if (ret)
4904 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004905
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004906 ret = intel_engines_init(dev_priv);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004907 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004908 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004909
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004910 ret = i915_gem_init_hw(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004911 if (ret == -EIO) {
Chris Wilson7e21d642016-07-27 09:07:29 +01004912 /* Allow engine initialisation to fail by marking the GPU as
Chris Wilson60990322014-04-09 09:19:42 +01004913 * wedged. But we only want to do this where the GPU is angry,
4914 * for all other failure, such as an allocation failure, bail.
4915 */
4916 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Chris Wilson821ed7d2016-09-09 14:11:53 +01004917 i915_gem_set_wedged(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004918 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004919 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004920
4921out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004922 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Tvrtko Ursulinbf9e8422016-12-01 14:16:38 +00004923 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004924
Chris Wilson60990322014-04-09 09:19:42 +01004925 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004926}
4927
Chris Wilson24145512017-01-24 11:01:35 +00004928void i915_gem_init_mmio(struct drm_i915_private *i915)
4929{
4930 i915_gem_sanitize(i915);
4931}
4932
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004933void
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004934i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004935{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004936 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05304937 enum intel_engine_id id;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004938
Akash Goel3b3f1652016-10-13 22:44:48 +05304939 for_each_engine(engine, dev_priv, id)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004940 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004941}
4942
Eric Anholt673a3942008-07-30 12:06:12 -07004943void
Imre Deak40ae4e12016-03-16 14:54:03 +02004944i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
4945{
Chris Wilson49ef5292016-08-18 17:17:00 +01004946 int i;
Imre Deak40ae4e12016-03-16 14:54:03 +02004947
4948 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
4949 !IS_CHERRYVIEW(dev_priv))
4950 dev_priv->num_fence_regs = 32;
Jani Nikula73f67aa2016-12-07 22:48:09 +02004951 else if (INTEL_INFO(dev_priv)->gen >= 4 ||
4952 IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4953 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004954 dev_priv->num_fence_regs = 16;
4955 else
4956 dev_priv->num_fence_regs = 8;
4957
Chris Wilsonc0336662016-05-06 15:40:21 +01004958 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02004959 dev_priv->num_fence_regs =
4960 I915_READ(vgtif_reg(avail_rs.fence_num));
4961
4962 /* Initialize fence registers to zero */
Chris Wilson49ef5292016-08-18 17:17:00 +01004963 for (i = 0; i < dev_priv->num_fence_regs; i++) {
4964 struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
4965
4966 fence->i915 = dev_priv;
4967 fence->id = i;
4968 list_add_tail(&fence->link, &dev_priv->mm.fence_list);
4969 }
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004970 i915_gem_restore_fences(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004971
Tvrtko Ursulin4362f4f2016-11-16 08:55:33 +00004972 i915_gem_detect_bit_6_swizzle(dev_priv);
Imre Deak40ae4e12016-03-16 14:54:03 +02004973}
4974
Chris Wilson73cb9702016-10-28 13:58:46 +01004975int
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00004976i915_gem_load_init(struct drm_i915_private *dev_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07004977{
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004978 int err = -ENOMEM;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004979
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004980 dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
4981 if (!dev_priv->objects)
Chris Wilson73cb9702016-10-28 13:58:46 +01004982 goto err_out;
Chris Wilson73cb9702016-10-28 13:58:46 +01004983
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004984 dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
4985 if (!dev_priv->vmas)
Chris Wilson73cb9702016-10-28 13:58:46 +01004986 goto err_objects;
Chris Wilson73cb9702016-10-28 13:58:46 +01004987
Chris Wilsond1b48c12017-08-16 09:52:08 +01004988 dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
4989 if (!dev_priv->luts)
4990 goto err_vmas;
4991
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004992 dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
4993 SLAB_HWCACHE_ALIGN |
4994 SLAB_RECLAIM_ACCOUNT |
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08004995 SLAB_TYPESAFE_BY_RCU);
Tvrtko Ursulina9335682016-11-02 15:14:59 +00004996 if (!dev_priv->requests)
Chris Wilsond1b48c12017-08-16 09:52:08 +01004997 goto err_luts;
Chris Wilson73cb9702016-10-28 13:58:46 +01004998
Chris Wilson52e54202016-11-14 20:41:02 +00004999 dev_priv->dependencies = KMEM_CACHE(i915_dependency,
5000 SLAB_HWCACHE_ALIGN |
5001 SLAB_RECLAIM_ACCOUNT);
5002 if (!dev_priv->dependencies)
5003 goto err_requests;
5004
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005005 dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
5006 if (!dev_priv->priorities)
5007 goto err_dependencies;
5008
Chris Wilson73cb9702016-10-28 13:58:46 +01005009 mutex_lock(&dev_priv->drm.struct_mutex);
5010 INIT_LIST_HEAD(&dev_priv->gt.timelines);
Chris Wilsonbb894852016-11-14 20:40:57 +00005011 err = i915_gem_timeline_init__global(dev_priv);
Chris Wilson73cb9702016-10-28 13:58:46 +01005012 mutex_unlock(&dev_priv->drm.struct_mutex);
5013 if (err)
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005014 goto err_priorities;
Eric Anholt673a3942008-07-30 12:06:12 -07005015
Chris Wilsonfbbd37b2016-10-28 13:58:42 +01005016 INIT_WORK(&dev_priv->mm.free_work, __i915_gem_free_work);
5017 init_llist_head(&dev_priv->mm.free_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02005018 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
5019 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07005020 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson275f0392016-10-24 13:42:14 +01005021 INIT_LIST_HEAD(&dev_priv->mm.userfault_list);
Chris Wilson67d97da2016-07-04 08:08:31 +01005022 INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
Eric Anholt673a3942008-07-30 12:06:12 -07005023 i915_gem_retire_work_handler);
Chris Wilson67d97da2016-07-04 08:08:31 +01005024 INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005025 i915_gem_idle_work_handler);
Chris Wilson1f15b762016-07-01 17:23:14 +01005026 init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005027 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005028
Joonas Lahtinen6f633402016-09-01 14:58:21 +03005029 atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
5030
Chris Wilsonb5add952016-08-04 16:32:36 +01005031 spin_lock_init(&dev_priv->fb_tracking.lock);
Chris Wilson73cb9702016-10-28 13:58:46 +01005032
Matthew Auld465c4032017-10-06 23:18:14 +01005033 err = i915_gemfs_init(dev_priv);
5034 if (err)
5035 DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
5036
Chris Wilson73cb9702016-10-28 13:58:46 +01005037 return 0;
5038
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005039err_priorities:
5040 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005041err_dependencies:
5042 kmem_cache_destroy(dev_priv->dependencies);
Chris Wilson73cb9702016-10-28 13:58:46 +01005043err_requests:
5044 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005045err_luts:
5046 kmem_cache_destroy(dev_priv->luts);
Chris Wilson73cb9702016-10-28 13:58:46 +01005047err_vmas:
5048 kmem_cache_destroy(dev_priv->vmas);
5049err_objects:
5050 kmem_cache_destroy(dev_priv->objects);
5051err_out:
5052 return err;
Eric Anholt673a3942008-07-30 12:06:12 -07005053}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005054
Tvrtko Ursulincb15d9f2016-12-01 14:16:39 +00005055void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
Imre Deakd64aa092016-01-19 15:26:29 +02005056{
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005057 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005058 WARN_ON(!llist_empty(&dev_priv->mm.free_list));
Chris Wilsonc4d4c1c2017-02-10 16:35:23 +00005059 WARN_ON(dev_priv->mm.object_count);
Chris Wilson7d5d59e2016-11-01 08:48:41 +00005060
Matthew Auldea84aa72016-11-17 21:04:11 +00005061 mutex_lock(&dev_priv->drm.struct_mutex);
5062 i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
5063 WARN_ON(!list_empty(&dev_priv->gt.timelines));
5064 mutex_unlock(&dev_priv->drm.struct_mutex);
5065
Chris Wilsonc5cf9a92017-05-17 13:10:04 +01005066 kmem_cache_destroy(dev_priv->priorities);
Chris Wilson52e54202016-11-14 20:41:02 +00005067 kmem_cache_destroy(dev_priv->dependencies);
Imre Deakd64aa092016-01-19 15:26:29 +02005068 kmem_cache_destroy(dev_priv->requests);
Chris Wilsond1b48c12017-08-16 09:52:08 +01005069 kmem_cache_destroy(dev_priv->luts);
Imre Deakd64aa092016-01-19 15:26:29 +02005070 kmem_cache_destroy(dev_priv->vmas);
5071 kmem_cache_destroy(dev_priv->objects);
Chris Wilson0eafec62016-08-04 16:32:41 +01005072
5073 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
5074 rcu_barrier();
Matthew Auld465c4032017-10-06 23:18:14 +01005075
5076 i915_gemfs_fini(dev_priv);
Imre Deakd64aa092016-01-19 15:26:29 +02005077}
5078
Chris Wilson6a800ea2016-09-21 14:51:07 +01005079int i915_gem_freeze(struct drm_i915_private *dev_priv)
5080{
Chris Wilsond0aa3012017-04-07 11:25:49 +01005081 /* Discard all purgeable objects, let userspace recover those as
5082 * required after resuming.
5083 */
Chris Wilson6a800ea2016-09-21 14:51:07 +01005084 i915_gem_shrink_all(dev_priv);
Chris Wilson6a800ea2016-09-21 14:51:07 +01005085
Chris Wilson6a800ea2016-09-21 14:51:07 +01005086 return 0;
5087}
5088
Chris Wilson461fb992016-05-14 07:26:33 +01005089int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5090{
5091 struct drm_i915_gem_object *obj;
Chris Wilson7aab2d52016-09-09 20:02:18 +01005092 struct list_head *phases[] = {
5093 &dev_priv->mm.unbound_list,
5094 &dev_priv->mm.bound_list,
5095 NULL
5096 }, **p;
Chris Wilson461fb992016-05-14 07:26:33 +01005097
5098 /* Called just before we write the hibernation image.
5099 *
5100 * We need to update the domain tracking to reflect that the CPU
5101 * will be accessing all the pages to create and restore from the
5102 * hibernation, and so upon restoration those pages will be in the
5103 * CPU domain.
5104 *
5105 * To make sure the hibernation image contains the latest state,
5106 * we update that state just before writing out the image.
Chris Wilson7aab2d52016-09-09 20:02:18 +01005107 *
5108 * To try and reduce the hibernation image, we manually shrink
Chris Wilsond0aa3012017-04-07 11:25:49 +01005109 * the objects as well, see i915_gem_freeze()
Chris Wilson461fb992016-05-14 07:26:33 +01005110 */
5111
Chris Wilson912d5722017-09-06 16:19:30 -07005112 i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
Chris Wilson17b93c42017-04-07 11:25:50 +01005113 i915_gem_drain_freed_objects(dev_priv);
Chris Wilson461fb992016-05-14 07:26:33 +01005114
Chris Wilsond0aa3012017-04-07 11:25:49 +01005115 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson7aab2d52016-09-09 20:02:18 +01005116 for (p = phases; *p; p++) {
Chris Wilsone27ab732017-06-15 13:38:49 +01005117 list_for_each_entry(obj, *p, global_link)
5118 __start_cpu_write(obj);
Chris Wilson461fb992016-05-14 07:26:33 +01005119 }
Chris Wilson6a800ea2016-09-21 14:51:07 +01005120 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson461fb992016-05-14 07:26:33 +01005121
5122 return 0;
5123}
5124
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005125void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005126{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005127 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilson15f7bbc2016-07-26 12:01:52 +01005128 struct drm_i915_gem_request *request;
Eric Anholtb9624422009-06-03 07:27:35 +00005129
5130 /* Clean up our request list when the client is going away, so that
5131 * later retire_requests won't dereference our soon-to-be-gone
5132 * file_priv.
5133 */
Chris Wilson1c255952010-09-26 11:03:27 +01005134 spin_lock(&file_priv->mm.lock);
Chris Wilsonc8659ef2017-03-02 12:25:25 +00005135 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005136 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01005137 spin_unlock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005138}
5139
Chris Wilson829a0af2017-06-20 12:05:45 +01005140int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005141{
5142 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005143 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005144
Chris Wilsonc4c29d72016-11-09 10:45:07 +00005145 DRM_DEBUG("\n");
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005146
5147 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5148 if (!file_priv)
5149 return -ENOMEM;
5150
5151 file->driver_priv = file_priv;
Chris Wilson829a0af2017-06-20 12:05:45 +01005152 file_priv->dev_priv = i915;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005153 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005154
5155 spin_lock_init(&file_priv->mm.lock);
5156 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005157
Chris Wilsonc80ff162016-07-27 09:07:27 +01005158 file_priv->bsd_engine = -1;
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005159
Chris Wilson829a0af2017-06-20 12:05:45 +01005160 ret = i915_gem_context_open(i915, file);
Ben Widawskye422b882013-12-06 14:10:58 -08005161 if (ret)
5162 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005163
Ben Widawskye422b882013-12-06 14:10:58 -08005164 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005165}
5166
Daniel Vetterb680c372014-09-19 18:27:27 +02005167/**
5168 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005169 * @old: current GEM buffer for the frontbuffer slots
5170 * @new: new GEM buffer for the frontbuffer slots
5171 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005172 *
5173 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5174 * from @old and setting them in @new. Both @old and @new can be NULL.
5175 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005176void i915_gem_track_fb(struct drm_i915_gem_object *old,
5177 struct drm_i915_gem_object *new,
5178 unsigned frontbuffer_bits)
5179{
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005180 /* Control of individual bits within the mask are guarded by
5181 * the owning plane->mutex, i.e. we can never see concurrent
5182 * manipulation of individual bits. But since the bitfield as a whole
5183 * is updated using RMW, we need to use atomics in order to update
5184 * the bits.
5185 */
5186 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
5187 sizeof(atomic_t) * BITS_PER_BYTE);
5188
Daniel Vettera071fa02014-06-18 23:28:09 +02005189 if (old) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005190 WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
5191 atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005192 }
5193
5194 if (new) {
Chris Wilsonfaf5bf02016-08-04 16:32:37 +01005195 WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
5196 atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
Daniel Vettera071fa02014-06-18 23:28:09 +02005197 }
5198}
5199
Dave Gordonea702992015-07-09 19:29:02 +01005200/* Allocate a new GEM object and fill it with the supplied data */
5201struct drm_i915_gem_object *
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005202i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
Dave Gordonea702992015-07-09 19:29:02 +01005203 const void *data, size_t size)
5204{
5205 struct drm_i915_gem_object *obj;
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005206 struct file *file;
5207 size_t offset;
5208 int err;
Dave Gordonea702992015-07-09 19:29:02 +01005209
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +00005210 obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005211 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005212 return obj;
5213
Chris Wilsonce8ff092017-03-17 19:46:47 +00005214 GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
Dave Gordonea702992015-07-09 19:29:02 +01005215
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005216 file = obj->base.filp;
5217 offset = 0;
5218 do {
5219 unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
5220 struct page *page;
5221 void *pgdata, *vaddr;
Dave Gordonea702992015-07-09 19:29:02 +01005222
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005223 err = pagecache_write_begin(file, file->f_mapping,
5224 offset, len, 0,
5225 &page, &pgdata);
5226 if (err < 0)
5227 goto fail;
Dave Gordonea702992015-07-09 19:29:02 +01005228
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005229 vaddr = kmap(page);
5230 memcpy(vaddr, data, len);
5231 kunmap(page);
5232
5233 err = pagecache_write_end(file, file->f_mapping,
5234 offset, len, len,
5235 page, pgdata);
5236 if (err < 0)
5237 goto fail;
5238
5239 size -= len;
5240 data += len;
5241 offset += len;
5242 } while (size);
Dave Gordonea702992015-07-09 19:29:02 +01005243
5244 return obj;
5245
5246fail:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01005247 i915_gem_object_put(obj);
Chris Wilsonbe062fa2017-03-17 19:46:48 +00005248 return ERR_PTR(err);
Dave Gordonea702992015-07-09 19:29:02 +01005249}
Chris Wilson96d77632016-10-28 13:58:33 +01005250
5251struct scatterlist *
5252i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
5253 unsigned int n,
5254 unsigned int *offset)
5255{
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005256 struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
Chris Wilson96d77632016-10-28 13:58:33 +01005257 struct scatterlist *sg;
5258 unsigned int idx, count;
5259
5260 might_sleep();
5261 GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005262 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
Chris Wilson96d77632016-10-28 13:58:33 +01005263
5264 /* As we iterate forward through the sg, we record each entry in a
5265 * radixtree for quick repeated (backwards) lookups. If we have seen
5266 * this index previously, we will have an entry for it.
5267 *
5268 * Initial lookup is O(N), but this is amortized to O(1) for
5269 * sequential page access (where each new request is consecutive
5270 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
5271 * i.e. O(1) with a large constant!
5272 */
5273 if (n < READ_ONCE(iter->sg_idx))
5274 goto lookup;
5275
5276 mutex_lock(&iter->lock);
5277
5278 /* We prefer to reuse the last sg so that repeated lookup of this
5279 * (or the subsequent) sg are fast - comparing against the last
5280 * sg is faster than going through the radixtree.
5281 */
5282
5283 sg = iter->sg_pos;
5284 idx = iter->sg_idx;
5285 count = __sg_page_count(sg);
5286
5287 while (idx + count <= n) {
5288 unsigned long exception, i;
5289 int ret;
5290
5291 /* If we cannot allocate and insert this entry, or the
5292 * individual pages from this range, cancel updating the
5293 * sg_idx so that on this lookup we are forced to linearly
5294 * scan onwards, but on future lookups we will try the
5295 * insertion again (in which case we need to be careful of
5296 * the error return reporting that we have already inserted
5297 * this index).
5298 */
5299 ret = radix_tree_insert(&iter->radix, idx, sg);
5300 if (ret && ret != -EEXIST)
5301 goto scan;
5302
5303 exception =
5304 RADIX_TREE_EXCEPTIONAL_ENTRY |
5305 idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
5306 for (i = 1; i < count; i++) {
5307 ret = radix_tree_insert(&iter->radix, idx + i,
5308 (void *)exception);
5309 if (ret && ret != -EEXIST)
5310 goto scan;
5311 }
5312
5313 idx += count;
5314 sg = ____sg_next(sg);
5315 count = __sg_page_count(sg);
5316 }
5317
5318scan:
5319 iter->sg_pos = sg;
5320 iter->sg_idx = idx;
5321
5322 mutex_unlock(&iter->lock);
5323
5324 if (unlikely(n < idx)) /* insertion completed by another thread */
5325 goto lookup;
5326
5327 /* In case we failed to insert the entry into the radixtree, we need
5328 * to look beyond the current sg.
5329 */
5330 while (idx + count <= n) {
5331 idx += count;
5332 sg = ____sg_next(sg);
5333 count = __sg_page_count(sg);
5334 }
5335
5336 *offset = n - idx;
5337 return sg;
5338
5339lookup:
5340 rcu_read_lock();
5341
5342 sg = radix_tree_lookup(&iter->radix, n);
5343 GEM_BUG_ON(!sg);
5344
5345 /* If this index is in the middle of multi-page sg entry,
5346 * the radixtree will contain an exceptional entry that points
5347 * to the start of that range. We will return the pointer to
5348 * the base page and the offset of this page within the
5349 * sg entry's range.
5350 */
5351 *offset = 0;
5352 if (unlikely(radix_tree_exception(sg))) {
5353 unsigned long base =
5354 (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
5355
5356 sg = radix_tree_lookup(&iter->radix, base);
5357 GEM_BUG_ON(!sg);
5358
5359 *offset = n - base;
5360 }
5361
5362 rcu_read_unlock();
5363
5364 return sg;
5365}
5366
5367struct page *
5368i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
5369{
5370 struct scatterlist *sg;
5371 unsigned int offset;
5372
5373 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
5374
5375 sg = i915_gem_object_get_sg(obj, n, &offset);
5376 return nth_page(sg_page(sg), offset);
5377}
5378
5379/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5380struct page *
5381i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
5382 unsigned int n)
5383{
5384 struct page *page;
5385
5386 page = i915_gem_object_get_page(obj, n);
Chris Wilsona4f5ea62016-10-28 13:58:35 +01005387 if (!obj->mm.dirty)
Chris Wilson96d77632016-10-28 13:58:33 +01005388 set_page_dirty(page);
5389
5390 return page;
5391}
5392
5393dma_addr_t
5394i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
5395 unsigned long n)
5396{
5397 struct scatterlist *sg;
5398 unsigned int offset;
5399
5400 sg = i915_gem_object_get_sg(obj, n, &offset);
5401 return sg_dma_address(sg) + (offset << PAGE_SHIFT);
5402}
Chris Wilson935a2f72017-02-13 17:15:13 +00005403
Chris Wilson8eeb7902017-07-26 19:16:01 +01005404int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
5405{
5406 struct sg_table *pages;
5407 int err;
5408
5409 if (align > obj->base.size)
5410 return -EINVAL;
5411
5412 if (obj->ops == &i915_gem_phys_ops)
5413 return 0;
5414
5415 if (obj->ops != &i915_gem_object_ops)
5416 return -EINVAL;
5417
5418 err = i915_gem_object_unbind(obj);
5419 if (err)
5420 return err;
5421
5422 mutex_lock(&obj->mm.lock);
5423
5424 if (obj->mm.madv != I915_MADV_WILLNEED) {
5425 err = -EFAULT;
5426 goto err_unlock;
5427 }
5428
5429 if (obj->mm.quirked) {
5430 err = -EFAULT;
5431 goto err_unlock;
5432 }
5433
5434 if (obj->mm.mapping) {
5435 err = -EBUSY;
5436 goto err_unlock;
5437 }
5438
5439 pages = obj->mm.pages;
5440 obj->ops = &i915_gem_phys_ops;
5441
Chris Wilson8fb6a5d2017-07-26 19:16:02 +01005442 err = ____i915_gem_object_get_pages(obj);
Chris Wilson8eeb7902017-07-26 19:16:01 +01005443 if (err)
5444 goto err_xfer;
5445
5446 /* Perma-pin (until release) the physical set of pages */
5447 __i915_gem_object_pin_pages(obj);
5448
5449 if (!IS_ERR_OR_NULL(pages))
5450 i915_gem_object_ops.put_pages(obj, pages);
5451 mutex_unlock(&obj->mm.lock);
5452 return 0;
5453
5454err_xfer:
5455 obj->ops = &i915_gem_object_ops;
5456 obj->mm.pages = pages;
5457err_unlock:
5458 mutex_unlock(&obj->mm.lock);
5459 return err;
5460}
5461
Chris Wilson935a2f72017-02-13 17:15:13 +00005462#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
5463#include "selftests/scatterlist.c"
Chris Wilson66d9cb52017-02-13 17:15:17 +00005464#include "selftests/mock_gem_device.c"
Chris Wilson44653982017-02-13 17:15:20 +00005465#include "selftests/huge_gem_object.c"
Matthew Auld40498662017-10-06 23:18:29 +01005466#include "selftests/huge_pages.c"
Chris Wilson8335fd62017-02-13 17:15:28 +00005467#include "selftests/i915_gem_object.c"
Chris Wilson17059452017-02-13 17:15:32 +00005468#include "selftests/i915_gem_coherency.c"
Chris Wilson935a2f72017-02-13 17:15:13 +00005469#endif