blob: eae8d7a89d09e9f69665035d88724b1776369c91 [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"
Yu Zhangeb822892015-02-10 19:05:49 +080032#include "i915_vgpu.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010033#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070034#include "intel_drv.h"
Peter Antoine0ccdacf2016-04-13 15:03:25 +010035#include "intel_mocs.h"
Hugh Dickins5949eac2011-06-27 16:18:18 -070036#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070038#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020040#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070041
Chris Wilson05394f32010-11-08 19:18:58 +000042static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
Daniel Vettere62b59e2015-01-21 14:53:48 +010043static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilsonc8725f32014-03-17 12:21:55 +000044static void
Chris Wilsonb4716182015-04-27 13:41:17 +010045i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
46static void
47i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
Chris Wilson61050802012-04-17 15:31:31 +010048
Chris Wilsonc76ce032013-08-08 14:41:03 +010049static bool cpu_cache_is_coherent(struct drm_device *dev,
50 enum i915_cache_level level)
51{
52 return HAS_LLC(dev) || level != I915_CACHE_NONE;
53}
54
Chris Wilson2c225692013-08-09 12:26:45 +010055static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
56{
57 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
58 return true;
59
60 return obj->pin_display;
61}
62
Chris Wilson73aa8082010-09-30 11:46:12 +010063/* some bookkeeping */
64static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
65 size_t size)
66{
Daniel Vetterc20e8352013-07-24 22:40:23 +020067 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010068 dev_priv->mm.object_count++;
69 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020070 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010071}
72
73static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
74 size_t size)
75{
Daniel Vetterc20e8352013-07-24 22:40:23 +020076 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010077 dev_priv->mm.object_count--;
78 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020079 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010080}
81
Chris Wilson21dd3732011-01-26 15:55:56 +000082static int
Daniel Vetter33196de2012-11-14 17:14:05 +010083i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010084{
Chris Wilson30dbf0c2010-09-25 10:19:17 +010085 int ret;
86
Chris Wilsond98c52c2016-04-13 17:35:05 +010087 if (!i915_reset_in_progress(error))
Chris Wilson30dbf0c2010-09-25 10:19:17 +010088 return 0;
89
Daniel Vetter0a6759c2012-07-04 22:18:41 +020090 /*
91 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
92 * userspace. If it takes that long something really bad is going on and
93 * we should simply try to bail out and fail as gracefully as possible.
94 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +010095 ret = wait_event_interruptible_timeout(error->reset_queue,
Chris Wilsond98c52c2016-04-13 17:35:05 +010096 !i915_reset_in_progress(error),
Daniel Vetter1f83fee2012-11-15 17:17:22 +010097 10*HZ);
Daniel Vetter0a6759c2012-07-04 22:18:41 +020098 if (ret == 0) {
99 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
100 return -EIO;
101 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100102 return ret;
Chris Wilsond98c52c2016-04-13 17:35:05 +0100103 } else {
104 return 0;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200105 }
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100106}
107
Chris Wilson54cf91d2010-11-25 18:00:26 +0000108int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100109{
Daniel Vetter33196de2012-11-14 17:14:05 +0100110 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100111 int ret;
112
Daniel Vetter33196de2012-11-14 17:14:05 +0100113 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100114 if (ret)
115 return ret;
116
117 ret = mutex_lock_interruptible(&dev->struct_mutex);
118 if (ret)
119 return ret;
120
Chris Wilson23bc5982010-09-29 16:10:57 +0100121 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100122 return 0;
123}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100124
Eric Anholt673a3942008-07-30 12:06:12 -0700125int
Eric Anholt5a125c32008-10-22 21:40:13 -0700126i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000127 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700128{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300129 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200130 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300131 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100132 struct i915_vma *vma;
Chris Wilson6299f992010-11-24 12:23:44 +0000133 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700134
Chris Wilson6299f992010-11-24 12:23:44 +0000135 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100136 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000137 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100138 if (vma->pin_count)
139 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000140 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100141 if (vma->pin_count)
142 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100143 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700144
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300145 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400146 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000147
Eric Anholt5a125c32008-10-22 21:40:13 -0700148 return 0;
149}
150
Chris Wilson6a2c4232014-11-04 04:51:40 -0800151static int
152i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100153{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800154 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
155 char *vaddr = obj->phys_handle->vaddr;
156 struct sg_table *st;
157 struct scatterlist *sg;
158 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100159
Chris Wilson6a2c4232014-11-04 04:51:40 -0800160 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
161 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100162
Chris Wilson6a2c4232014-11-04 04:51:40 -0800163 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
164 struct page *page;
165 char *src;
166
167 page = shmem_read_mapping_page(mapping, i);
168 if (IS_ERR(page))
169 return PTR_ERR(page);
170
171 src = kmap_atomic(page);
172 memcpy(vaddr, src, PAGE_SIZE);
173 drm_clflush_virt_range(vaddr, PAGE_SIZE);
174 kunmap_atomic(src);
175
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300176 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800177 vaddr += PAGE_SIZE;
178 }
179
Chris Wilsonc0336662016-05-06 15:40:21 +0100180 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson6a2c4232014-11-04 04:51:40 -0800181
182 st = kmalloc(sizeof(*st), GFP_KERNEL);
183 if (st == NULL)
184 return -ENOMEM;
185
186 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
187 kfree(st);
188 return -ENOMEM;
189 }
190
191 sg = st->sgl;
192 sg->offset = 0;
193 sg->length = obj->base.size;
194
195 sg_dma_address(sg) = obj->phys_handle->busaddr;
196 sg_dma_len(sg) = obj->base.size;
197
198 obj->pages = st;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800199 return 0;
200}
201
202static void
203i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
204{
205 int ret;
206
207 BUG_ON(obj->madv == __I915_MADV_PURGED);
208
209 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +0100210 if (WARN_ON(ret)) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800211 /* In the event of a disaster, abandon all caches and
212 * hope for the best.
213 */
Chris Wilson6a2c4232014-11-04 04:51:40 -0800214 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
215 }
216
217 if (obj->madv == I915_MADV_DONTNEED)
218 obj->dirty = 0;
219
220 if (obj->dirty) {
Chris Wilson00731152014-05-21 12:42:56 +0100221 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800222 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100223 int i;
224
225 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800226 struct page *page;
227 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100228
Chris Wilson6a2c4232014-11-04 04:51:40 -0800229 page = shmem_read_mapping_page(mapping, i);
230 if (IS_ERR(page))
231 continue;
232
233 dst = kmap_atomic(page);
234 drm_clflush_virt_range(vaddr, PAGE_SIZE);
235 memcpy(dst, vaddr, PAGE_SIZE);
236 kunmap_atomic(dst);
237
238 set_page_dirty(page);
239 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100240 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100242 vaddr += PAGE_SIZE;
243 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800244 obj->dirty = 0;
Chris Wilson00731152014-05-21 12:42:56 +0100245 }
246
Chris Wilson6a2c4232014-11-04 04:51:40 -0800247 sg_free_table(obj->pages);
248 kfree(obj->pages);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800249}
250
251static void
252i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
253{
254 drm_pci_free(obj->base.dev, obj->phys_handle);
255}
256
257static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
258 .get_pages = i915_gem_object_get_pages_phys,
259 .put_pages = i915_gem_object_put_pages_phys,
260 .release = i915_gem_object_release_phys,
261};
262
263static int
264drop_pages(struct drm_i915_gem_object *obj)
265{
266 struct i915_vma *vma, *next;
267 int ret;
268
269 drm_gem_object_reference(&obj->base);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000270 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800271 if (i915_vma_unbind(vma))
272 break;
273
274 ret = i915_gem_object_put_pages(obj);
275 drm_gem_object_unreference(&obj->base);
276
277 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100278}
279
280int
281i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
282 int align)
283{
284 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800285 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100286
287 if (obj->phys_handle) {
288 if ((unsigned long)obj->phys_handle->vaddr & (align -1))
289 return -EBUSY;
290
291 return 0;
292 }
293
294 if (obj->madv != I915_MADV_WILLNEED)
295 return -EFAULT;
296
297 if (obj->base.filp == NULL)
298 return -EINVAL;
299
Chris Wilson6a2c4232014-11-04 04:51:40 -0800300 ret = drop_pages(obj);
301 if (ret)
302 return ret;
303
Chris Wilson00731152014-05-21 12:42:56 +0100304 /* create a new object */
305 phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
306 if (!phys)
307 return -ENOMEM;
308
Chris Wilson00731152014-05-21 12:42:56 +0100309 obj->phys_handle = phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800310 obj->ops = &i915_gem_phys_ops;
311
312 return i915_gem_object_get_pages(obj);
Chris Wilson00731152014-05-21 12:42:56 +0100313}
314
315static int
316i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
317 struct drm_i915_gem_pwrite *args,
318 struct drm_file *file_priv)
319{
320 struct drm_device *dev = obj->base.dev;
321 void *vaddr = obj->phys_handle->vaddr + args->offset;
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300322 char __user *user_data = u64_to_user_ptr(args->data_ptr);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200323 int ret = 0;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800324
325 /* We manually control the domain here and pretend that it
326 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
327 */
328 ret = i915_gem_object_wait_rendering(obj, false);
329 if (ret)
330 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100331
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700332 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson00731152014-05-21 12:42:56 +0100333 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
334 unsigned long unwritten;
335
336 /* The physical object once assigned is fixed for the lifetime
337 * of the obj, so we can safely drop the lock and continue
338 * to access vaddr.
339 */
340 mutex_unlock(&dev->struct_mutex);
341 unwritten = copy_from_user(vaddr, user_data, args->size);
342 mutex_lock(&dev->struct_mutex);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200343 if (unwritten) {
344 ret = -EFAULT;
345 goto out;
346 }
Chris Wilson00731152014-05-21 12:42:56 +0100347 }
348
Chris Wilson6a2c4232014-11-04 04:51:40 -0800349 drm_clflush_virt_range(vaddr, args->size);
Chris Wilsonc0336662016-05-06 15:40:21 +0100350 i915_gem_chipset_flush(to_i915(dev));
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200351
352out:
Rodrigo Vivide152b62015-07-07 16:28:51 -0700353 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200354 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100355}
356
Chris Wilson42dcedd2012-11-15 11:32:30 +0000357void *i915_gem_object_alloc(struct drm_device *dev)
358{
359 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonefab6d82015-04-07 16:20:57 +0100360 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000361}
362
363void i915_gem_object_free(struct drm_i915_gem_object *obj)
364{
365 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsonefab6d82015-04-07 16:20:57 +0100366 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000367}
368
Dave Airlieff72145b2011-02-07 12:16:14 +1000369static int
370i915_gem_create(struct drm_file *file,
371 struct drm_device *dev,
372 uint64_t size,
373 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700374{
Chris Wilson05394f32010-11-08 19:18:58 +0000375 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300376 int ret;
377 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700378
Dave Airlieff72145b2011-02-07 12:16:14 +1000379 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200380 if (size == 0)
381 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700382
383 /* Allocate the new object */
Dave Gordond37cd8a2016-04-22 19:14:32 +0100384 obj = i915_gem_object_create(dev, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100385 if (IS_ERR(obj))
386 return PTR_ERR(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700387
Chris Wilson05394f32010-11-08 19:18:58 +0000388 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100389 /* drop reference from allocate - handle holds it now */
Daniel Vetterd861e332013-07-24 23:25:03 +0200390 drm_gem_object_unreference_unlocked(&obj->base);
391 if (ret)
392 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100393
Dave Airlieff72145b2011-02-07 12:16:14 +1000394 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700395 return 0;
396}
397
Dave Airlieff72145b2011-02-07 12:16:14 +1000398int
399i915_gem_dumb_create(struct drm_file *file,
400 struct drm_device *dev,
401 struct drm_mode_create_dumb *args)
402{
403 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300404 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000405 args->size = args->pitch * args->height;
406 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000407 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000408}
409
Dave Airlieff72145b2011-02-07 12:16:14 +1000410/**
411 * Creates a new mm object and returns a handle to it.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100412 * @dev: drm device pointer
413 * @data: ioctl data blob
414 * @file: drm file pointer
Dave Airlieff72145b2011-02-07 12:16:14 +1000415 */
416int
417i915_gem_create_ioctl(struct drm_device *dev, void *data,
418 struct drm_file *file)
419{
420 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200421
Dave Airlieff72145b2011-02-07 12:16:14 +1000422 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000423 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000424}
425
Daniel Vetter8c599672011-12-14 13:57:31 +0100426static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100427__copy_to_user_swizzled(char __user *cpu_vaddr,
428 const char *gpu_vaddr, int gpu_offset,
429 int length)
430{
431 int ret, cpu_offset = 0;
432
433 while (length > 0) {
434 int cacheline_end = ALIGN(gpu_offset + 1, 64);
435 int this_length = min(cacheline_end - gpu_offset, length);
436 int swizzled_gpu_offset = gpu_offset ^ 64;
437
438 ret = __copy_to_user(cpu_vaddr + cpu_offset,
439 gpu_vaddr + swizzled_gpu_offset,
440 this_length);
441 if (ret)
442 return ret + length;
443
444 cpu_offset += this_length;
445 gpu_offset += this_length;
446 length -= this_length;
447 }
448
449 return 0;
450}
451
452static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700453__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
454 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100455 int length)
456{
457 int ret, cpu_offset = 0;
458
459 while (length > 0) {
460 int cacheline_end = ALIGN(gpu_offset + 1, 64);
461 int this_length = min(cacheline_end - gpu_offset, length);
462 int swizzled_gpu_offset = gpu_offset ^ 64;
463
464 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
465 cpu_vaddr + cpu_offset,
466 this_length);
467 if (ret)
468 return ret + length;
469
470 cpu_offset += this_length;
471 gpu_offset += this_length;
472 length -= this_length;
473 }
474
475 return 0;
476}
477
Brad Volkin4c914c02014-02-18 10:15:45 -0800478/*
479 * Pins the specified object's pages and synchronizes the object with
480 * GPU accesses. Sets needs_clflush to non-zero if the caller should
481 * flush the object from the CPU cache.
482 */
483int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
484 int *needs_clflush)
485{
486 int ret;
487
488 *needs_clflush = 0;
489
Ben Widawsky1db6e2e2016-02-09 11:44:12 -0800490 if (WARN_ON((obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE) == 0))
Brad Volkin4c914c02014-02-18 10:15:45 -0800491 return -EINVAL;
492
493 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
494 /* If we're not in the cpu read domain, set ourself into the gtt
495 * read domain and manually flush cachelines (if required). This
496 * optimizes for the case when the gpu will dirty the data
497 * anyway again before the next pread happens. */
498 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
499 obj->cache_level);
500 ret = i915_gem_object_wait_rendering(obj, true);
501 if (ret)
502 return ret;
503 }
504
505 ret = i915_gem_object_get_pages(obj);
506 if (ret)
507 return ret;
508
509 i915_gem_object_pin_pages(obj);
510
511 return ret;
512}
513
Daniel Vetterd174bd62012-03-25 19:47:40 +0200514/* Per-page copy function for the shmem pread fastpath.
515 * Flushes invalid cachelines before reading the target if
516 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700517static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200518shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
519 char __user *user_data,
520 bool page_do_bit17_swizzling, bool needs_clflush)
521{
522 char *vaddr;
523 int ret;
524
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200525 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200526 return -EINVAL;
527
528 vaddr = kmap_atomic(page);
529 if (needs_clflush)
530 drm_clflush_virt_range(vaddr + shmem_page_offset,
531 page_length);
532 ret = __copy_to_user_inatomic(user_data,
533 vaddr + shmem_page_offset,
534 page_length);
535 kunmap_atomic(vaddr);
536
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100537 return ret ? -EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200538}
539
Daniel Vetter23c18c72012-03-25 19:47:42 +0200540static void
541shmem_clflush_swizzled_range(char *addr, unsigned long length,
542 bool swizzled)
543{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200544 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200545 unsigned long start = (unsigned long) addr;
546 unsigned long end = (unsigned long) addr + length;
547
548 /* For swizzling simply ensure that we always flush both
549 * channels. Lame, but simple and it works. Swizzled
550 * pwrite/pread is far from a hotpath - current userspace
551 * doesn't use it at all. */
552 start = round_down(start, 128);
553 end = round_up(end, 128);
554
555 drm_clflush_virt_range((void *)start, end - start);
556 } else {
557 drm_clflush_virt_range(addr, length);
558 }
559
560}
561
Daniel Vetterd174bd62012-03-25 19:47:40 +0200562/* Only difference to the fast-path function is that this can handle bit17
563 * and uses non-atomic copy and kmap functions. */
564static int
565shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
566 char __user *user_data,
567 bool page_do_bit17_swizzling, bool needs_clflush)
568{
569 char *vaddr;
570 int ret;
571
572 vaddr = kmap(page);
573 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200574 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
575 page_length,
576 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200577
578 if (page_do_bit17_swizzling)
579 ret = __copy_to_user_swizzled(user_data,
580 vaddr, shmem_page_offset,
581 page_length);
582 else
583 ret = __copy_to_user(user_data,
584 vaddr + shmem_page_offset,
585 page_length);
586 kunmap(page);
587
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100588 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200589}
590
Eric Anholteb014592009-03-10 11:44:52 -0700591static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200592i915_gem_shmem_pread(struct drm_device *dev,
593 struct drm_i915_gem_object *obj,
594 struct drm_i915_gem_pread *args,
595 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700596{
Daniel Vetter8461d222011-12-14 13:57:32 +0100597 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700598 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100599 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100600 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100601 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200602 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200603 int needs_clflush = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200604 struct sg_page_iter sg_iter;
Eric Anholteb014592009-03-10 11:44:52 -0700605
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300606 user_data = u64_to_user_ptr(args->data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700607 remain = args->size;
608
Daniel Vetter8461d222011-12-14 13:57:32 +0100609 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700610
Brad Volkin4c914c02014-02-18 10:15:45 -0800611 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100612 if (ret)
613 return ret;
614
Eric Anholteb014592009-03-10 11:44:52 -0700615 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100616
Imre Deak67d5a502013-02-18 19:28:02 +0200617 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
618 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200619 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +0100620
621 if (remain <= 0)
622 break;
623
Eric Anholteb014592009-03-10 11:44:52 -0700624 /* Operation in this page
625 *
Eric Anholteb014592009-03-10 11:44:52 -0700626 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700627 * page_length = bytes to copy for this page
628 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100629 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700630 page_length = remain;
631 if ((shmem_page_offset + page_length) > PAGE_SIZE)
632 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700633
Daniel Vetter8461d222011-12-14 13:57:32 +0100634 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
635 (page_to_phys(page) & (1 << 17)) != 0;
636
Daniel Vetterd174bd62012-03-25 19:47:40 +0200637 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
638 user_data, page_do_bit17_swizzling,
639 needs_clflush);
640 if (ret == 0)
641 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700642
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200643 mutex_unlock(&dev->struct_mutex);
644
Jani Nikulad330a952014-01-21 11:24:25 +0200645 if (likely(!i915.prefault_disable) && !prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200646 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200647 /* Userspace is tricking us, but we've already clobbered
648 * its pages with the prefault and promised to write the
649 * data up to the first fault. Hence ignore any errors
650 * and just continue. */
651 (void)ret;
652 prefaulted = 1;
653 }
654
Daniel Vetterd174bd62012-03-25 19:47:40 +0200655 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
656 user_data, page_do_bit17_swizzling,
657 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700658
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200659 mutex_lock(&dev->struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100660
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100661 if (ret)
Daniel Vetter8461d222011-12-14 13:57:32 +0100662 goto out;
Daniel Vetter8461d222011-12-14 13:57:32 +0100663
Chris Wilson17793c92014-03-07 08:30:36 +0000664next_page:
Eric Anholteb014592009-03-10 11:44:52 -0700665 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100666 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700667 offset += page_length;
668 }
669
Chris Wilson4f27b752010-10-14 15:26:45 +0100670out:
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100671 i915_gem_object_unpin_pages(obj);
672
Eric Anholteb014592009-03-10 11:44:52 -0700673 return ret;
674}
675
Eric Anholt673a3942008-07-30 12:06:12 -0700676/**
677 * Reads data from the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100678 * @dev: drm device pointer
679 * @data: ioctl data blob
680 * @file: drm file pointer
Eric Anholt673a3942008-07-30 12:06:12 -0700681 *
682 * On error, the contents of *data are undefined.
683 */
684int
685i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000686 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700687{
688 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000689 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100690 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700691
Chris Wilson51311d02010-11-17 09:10:42 +0000692 if (args->size == 0)
693 return 0;
694
695 if (!access_ok(VERIFY_WRITE,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300696 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000697 args->size))
698 return -EFAULT;
699
Chris Wilson4f27b752010-10-14 15:26:45 +0100700 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100701 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100702 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700703
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100704 obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000705 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100706 ret = -ENOENT;
707 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100708 }
Eric Anholt673a3942008-07-30 12:06:12 -0700709
Chris Wilson7dcd2492010-09-26 20:21:44 +0100710 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000711 if (args->offset > obj->base.size ||
712 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100713 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100714 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100715 }
716
Daniel Vetter1286ff72012-05-10 15:25:09 +0200717 /* prime objects have no backing filp to GEM pread/pwrite
718 * pages from.
719 */
720 if (!obj->base.filp) {
721 ret = -EINVAL;
722 goto out;
723 }
724
Chris Wilsondb53a302011-02-03 11:57:46 +0000725 trace_i915_gem_object_pread(obj, args->offset, args->size);
726
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200727 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700728
Chris Wilson35b62a82010-09-26 20:23:38 +0100729out:
Chris Wilson05394f32010-11-08 19:18:58 +0000730 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100731unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100732 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700733 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700734}
735
Keith Packard0839ccb2008-10-30 19:38:48 -0700736/* This is the fast write path which cannot handle
737 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700738 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700739
Keith Packard0839ccb2008-10-30 19:38:48 -0700740static inline int
741fast_user_write(struct io_mapping *mapping,
742 loff_t page_base, int page_offset,
743 char __user *user_data,
744 int length)
745{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700746 void __iomem *vaddr_atomic;
747 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700748 unsigned long unwritten;
749
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700750 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700751 /* We can use the cpu mem copy function because this is X86. */
752 vaddr = (void __force*)vaddr_atomic + page_offset;
753 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700754 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700755 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100756 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700757}
758
Eric Anholt3de09aa2009-03-09 09:42:23 -0700759/**
760 * This is the fast pwrite path, where we copy the data directly from the
761 * user into the GTT, uncached.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +0100762 * @dev: drm device pointer
763 * @obj: i915 gem object
764 * @args: pwrite arguments structure
765 * @file: drm file pointer
Eric Anholt3de09aa2009-03-09 09:42:23 -0700766 */
Eric Anholt673a3942008-07-30 12:06:12 -0700767static int
Chris Wilson05394f32010-11-08 19:18:58 +0000768i915_gem_gtt_pwrite_fast(struct drm_device *dev,
769 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700770 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000771 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700772{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300773 struct drm_i915_private *dev_priv = to_i915(dev);
774 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Eric Anholt673a3942008-07-30 12:06:12 -0700775 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700776 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700777 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200778 int page_offset, page_length, ret;
779
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100780 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200781 if (ret)
782 goto out;
783
784 ret = i915_gem_object_set_to_gtt_domain(obj, true);
785 if (ret)
786 goto out_unpin;
787
788 ret = i915_gem_object_put_fence(obj);
789 if (ret)
790 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700791
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300792 user_data = u64_to_user_ptr(args->data_ptr);
Eric Anholt673a3942008-07-30 12:06:12 -0700793 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700794
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700795 offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700796
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700797 intel_fb_obj_invalidate(obj, ORIGIN_GTT);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200798
Eric Anholt673a3942008-07-30 12:06:12 -0700799 while (remain > 0) {
800 /* Operation in this page
801 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700802 * page_base = page offset within aperture
803 * page_offset = offset within page
804 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700805 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100806 page_base = offset & PAGE_MASK;
807 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700808 page_length = remain;
809 if ((page_offset + remain) > PAGE_SIZE)
810 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700811
Keith Packard0839ccb2008-10-30 19:38:48 -0700812 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700813 * source page isn't available. Return the error and we'll
814 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700815 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300816 if (fast_user_write(ggtt->mappable, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200817 page_offset, user_data, page_length)) {
818 ret = -EFAULT;
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200819 goto out_flush;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200820 }
Eric Anholt673a3942008-07-30 12:06:12 -0700821
Keith Packard0839ccb2008-10-30 19:38:48 -0700822 remain -= page_length;
823 user_data += page_length;
824 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700825 }
Eric Anholt673a3942008-07-30 12:06:12 -0700826
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200827out_flush:
Rodrigo Vivide152b62015-07-07 16:28:51 -0700828 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200829out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800830 i915_gem_object_ggtt_unpin(obj);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200831out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700832 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700833}
834
Daniel Vetterd174bd62012-03-25 19:47:40 +0200835/* Per-page copy function for the shmem pwrite fastpath.
836 * Flushes invalid cachelines before writing to the target if
837 * needs_clflush_before is set and flushes out any written cachelines after
838 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700839static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200840shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
841 char __user *user_data,
842 bool page_do_bit17_swizzling,
843 bool needs_clflush_before,
844 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700845{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200846 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700847 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700848
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200849 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200850 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700851
Daniel Vetterd174bd62012-03-25 19:47:40 +0200852 vaddr = kmap_atomic(page);
853 if (needs_clflush_before)
854 drm_clflush_virt_range(vaddr + shmem_page_offset,
855 page_length);
Chris Wilsonc2831a92014-03-07 08:30:37 +0000856 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
857 user_data, page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200858 if (needs_clflush_after)
859 drm_clflush_virt_range(vaddr + shmem_page_offset,
860 page_length);
861 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700862
Chris Wilson755d2212012-09-04 21:02:55 +0100863 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700864}
865
Daniel Vetterd174bd62012-03-25 19:47:40 +0200866/* Only difference to the fast-path function is that this can handle bit17
867 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700868static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200869shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
870 char __user *user_data,
871 bool page_do_bit17_swizzling,
872 bool needs_clflush_before,
873 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700874{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200875 char *vaddr;
876 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700877
Daniel Vetterd174bd62012-03-25 19:47:40 +0200878 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200879 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200880 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
881 page_length,
882 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200883 if (page_do_bit17_swizzling)
884 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100885 user_data,
886 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200887 else
888 ret = __copy_from_user(vaddr + shmem_page_offset,
889 user_data,
890 page_length);
891 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200892 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
893 page_length,
894 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200895 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100896
Chris Wilson755d2212012-09-04 21:02:55 +0100897 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700898}
899
Eric Anholt40123c12009-03-09 13:42:30 -0700900static int
Daniel Vettere244a442012-03-25 19:47:28 +0200901i915_gem_shmem_pwrite(struct drm_device *dev,
902 struct drm_i915_gem_object *obj,
903 struct drm_i915_gem_pwrite *args,
904 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700905{
Eric Anholt40123c12009-03-09 13:42:30 -0700906 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100907 loff_t offset;
908 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100909 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100910 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200911 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200912 int needs_clflush_after = 0;
913 int needs_clflush_before = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200914 struct sg_page_iter sg_iter;
Eric Anholt40123c12009-03-09 13:42:30 -0700915
Gustavo Padovan3ed605b2016-04-26 12:32:27 -0300916 user_data = u64_to_user_ptr(args->data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700917 remain = args->size;
918
Daniel Vetter8c599672011-12-14 13:57:31 +0100919 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700920
Daniel Vetter58642882012-03-25 19:47:37 +0200921 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
922 /* If we're not in the cpu write domain, set ourself into the gtt
923 * write domain and manually flush cachelines (if required). This
924 * optimizes for the case when the gpu will use the data
925 * right away and we therefore have to clflush anyway. */
Chris Wilson2c225692013-08-09 12:26:45 +0100926 needs_clflush_after = cpu_write_needs_clflush(obj);
Ben Widawsky23f54482013-09-11 14:57:48 -0700927 ret = i915_gem_object_wait_rendering(obj, false);
928 if (ret)
929 return ret;
Daniel Vetter58642882012-03-25 19:47:37 +0200930 }
Chris Wilsonc76ce032013-08-08 14:41:03 +0100931 /* Same trick applies to invalidate partially written cachelines read
932 * before writing. */
933 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
934 needs_clflush_before =
935 !cpu_cache_is_coherent(dev, obj->cache_level);
Daniel Vetter58642882012-03-25 19:47:37 +0200936
Chris Wilson755d2212012-09-04 21:02:55 +0100937 ret = i915_gem_object_get_pages(obj);
938 if (ret)
939 return ret;
940
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700941 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200942
Chris Wilson755d2212012-09-04 21:02:55 +0100943 i915_gem_object_pin_pages(obj);
944
Eric Anholt40123c12009-03-09 13:42:30 -0700945 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000946 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700947
Imre Deak67d5a502013-02-18 19:28:02 +0200948 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
949 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200950 struct page *page = sg_page_iter_page(&sg_iter);
Daniel Vetter58642882012-03-25 19:47:37 +0200951 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100952
Chris Wilson9da3da62012-06-01 15:20:22 +0100953 if (remain <= 0)
954 break;
955
Eric Anholt40123c12009-03-09 13:42:30 -0700956 /* Operation in this page
957 *
Eric Anholt40123c12009-03-09 13:42:30 -0700958 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700959 * page_length = bytes to copy for this page
960 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100961 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700962
963 page_length = remain;
964 if ((shmem_page_offset + page_length) > PAGE_SIZE)
965 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700966
Daniel Vetter58642882012-03-25 19:47:37 +0200967 /* If we don't overwrite a cacheline completely we need to be
968 * careful to have up-to-date data by first clflushing. Don't
969 * overcomplicate things and flush the entire patch. */
970 partial_cacheline_write = needs_clflush_before &&
971 ((shmem_page_offset | page_length)
972 & (boot_cpu_data.x86_clflush_size - 1));
973
Daniel Vetter8c599672011-12-14 13:57:31 +0100974 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
975 (page_to_phys(page) & (1 << 17)) != 0;
976
Daniel Vetterd174bd62012-03-25 19:47:40 +0200977 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
978 user_data, page_do_bit17_swizzling,
979 partial_cacheline_write,
980 needs_clflush_after);
981 if (ret == 0)
982 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700983
Daniel Vettere244a442012-03-25 19:47:28 +0200984 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +0200985 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200986 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
987 user_data, page_do_bit17_swizzling,
988 partial_cacheline_write,
989 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700990
Daniel Vettere244a442012-03-25 19:47:28 +0200991 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +0100992
Chris Wilson755d2212012-09-04 21:02:55 +0100993 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +0100994 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +0100995
Chris Wilson17793c92014-03-07 08:30:36 +0000996next_page:
Eric Anholt40123c12009-03-09 13:42:30 -0700997 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100998 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700999 offset += page_length;
1000 }
1001
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001002out:
Chris Wilson755d2212012-09-04 21:02:55 +01001003 i915_gem_object_unpin_pages(obj);
1004
Daniel Vettere244a442012-03-25 19:47:28 +02001005 if (hit_slowpath) {
Daniel Vetter8dcf0152012-11-15 16:53:58 +01001006 /*
1007 * Fixup: Flush cpu caches in case we didn't flush the dirty
1008 * cachelines in-line while writing and the object moved
1009 * out of the cpu write domain while we've dropped the lock.
1010 */
1011 if (!needs_clflush_after &&
1012 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilson000433b2013-08-08 14:41:09 +01001013 if (i915_gem_clflush_object(obj, obj->pin_display))
Ville Syrjäläed75a552015-08-11 19:47:10 +03001014 needs_clflush_after = true;
Daniel Vettere244a442012-03-25 19:47:28 +02001015 }
Daniel Vetter8c599672011-12-14 13:57:31 +01001016 }
Eric Anholt40123c12009-03-09 13:42:30 -07001017
Daniel Vetter58642882012-03-25 19:47:37 +02001018 if (needs_clflush_after)
Chris Wilsonc0336662016-05-06 15:40:21 +01001019 i915_gem_chipset_flush(to_i915(dev));
Ville Syrjäläed75a552015-08-11 19:47:10 +03001020 else
1021 obj->cache_dirty = true;
Daniel Vetter58642882012-03-25 19:47:37 +02001022
Rodrigo Vivide152b62015-07-07 16:28:51 -07001023 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Eric Anholt40123c12009-03-09 13:42:30 -07001024 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001025}
1026
1027/**
1028 * Writes data to the object referenced by handle.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001029 * @dev: drm device
1030 * @data: ioctl data blob
1031 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001032 *
1033 * On error, the contents of the buffer that were to be modified are undefined.
1034 */
1035int
1036i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001037 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001038{
Imre Deak5d77d9c2014-11-12 16:40:35 +02001039 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001040 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001041 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001042 int ret;
1043
1044 if (args->size == 0)
1045 return 0;
1046
1047 if (!access_ok(VERIFY_READ,
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001048 u64_to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001049 args->size))
1050 return -EFAULT;
1051
Jani Nikulad330a952014-01-21 11:24:25 +02001052 if (likely(!i915.prefault_disable)) {
Gustavo Padovan3ed605b2016-04-26 12:32:27 -03001053 ret = fault_in_multipages_readable(u64_to_user_ptr(args->data_ptr),
Xiong Zhang0b74b502013-07-19 13:51:24 +08001054 args->size);
1055 if (ret)
1056 return -EFAULT;
1057 }
Eric Anholt673a3942008-07-30 12:06:12 -07001058
Imre Deak5d77d9c2014-11-12 16:40:35 +02001059 intel_runtime_pm_get(dev_priv);
1060
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001061 ret = i915_mutex_lock_interruptible(dev);
1062 if (ret)
Imre Deak5d77d9c2014-11-12 16:40:35 +02001063 goto put_rpm;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001064
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001065 obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001066 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001067 ret = -ENOENT;
1068 goto unlock;
1069 }
Eric Anholt673a3942008-07-30 12:06:12 -07001070
Chris Wilson7dcd2492010-09-26 20:21:44 +01001071 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001072 if (args->offset > obj->base.size ||
1073 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001074 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001075 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001076 }
1077
Daniel Vetter1286ff72012-05-10 15:25:09 +02001078 /* prime objects have no backing filp to GEM pread/pwrite
1079 * pages from.
1080 */
1081 if (!obj->base.filp) {
1082 ret = -EINVAL;
1083 goto out;
1084 }
1085
Chris Wilsondb53a302011-02-03 11:57:46 +00001086 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1087
Daniel Vetter935aaa62012-03-25 19:47:35 +02001088 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001089 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1090 * it would end up going through the fenced access, and we'll get
1091 * different detiling behavior between reading and writing.
1092 * pread/pwrite currently are reading and writing from the CPU
1093 * perspective, requiring manual detiling by the client.
1094 */
Chris Wilson2c225692013-08-09 12:26:45 +01001095 if (obj->tiling_mode == I915_TILING_NONE &&
1096 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
1097 cpu_write_needs_clflush(obj)) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001098 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001099 /* Note that the gtt paths might fail with non-page-backed user
1100 * pointers (e.g. gtt mappings when moving data between
1101 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -07001102 }
Eric Anholt673a3942008-07-30 12:06:12 -07001103
Chris Wilson6a2c4232014-11-04 04:51:40 -08001104 if (ret == -EFAULT || ret == -ENOSPC) {
1105 if (obj->phys_handle)
1106 ret = i915_gem_phys_pwrite(obj, args, file);
1107 else
1108 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
1109 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001110
Chris Wilson35b62a82010-09-26 20:23:38 +01001111out:
Chris Wilson05394f32010-11-08 19:18:58 +00001112 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001113unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001114 mutex_unlock(&dev->struct_mutex);
Imre Deak5d77d9c2014-11-12 16:40:35 +02001115put_rpm:
1116 intel_runtime_pm_put(dev_priv);
1117
Eric Anholt673a3942008-07-30 12:06:12 -07001118 return ret;
1119}
1120
Chris Wilsonf4457ae2016-04-13 17:35:08 +01001121static int
1122i915_gem_check_wedge(unsigned reset_counter, bool interruptible)
Chris Wilsonb3612372012-08-24 09:35:08 +01001123{
Chris Wilsonf4457ae2016-04-13 17:35:08 +01001124 if (__i915_terminally_wedged(reset_counter))
1125 return -EIO;
Chris Wilsond98c52c2016-04-13 17:35:05 +01001126
Chris Wilsonf4457ae2016-04-13 17:35:08 +01001127 if (__i915_reset_in_progress(reset_counter)) {
Chris Wilsonb3612372012-08-24 09:35:08 +01001128 /* Non-interruptible callers can't handle -EAGAIN, hence return
1129 * -EIO unconditionally for these. */
1130 if (!interruptible)
1131 return -EIO;
1132
Chris Wilsond98c52c2016-04-13 17:35:05 +01001133 return -EAGAIN;
Chris Wilsonb3612372012-08-24 09:35:08 +01001134 }
1135
1136 return 0;
1137}
1138
Chris Wilson094f9a52013-09-25 17:34:55 +01001139static void fake_irq(unsigned long data)
1140{
1141 wake_up_process((struct task_struct *)data);
1142}
1143
1144static bool missed_irq(struct drm_i915_private *dev_priv,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001145 struct intel_engine_cs *engine)
Chris Wilson094f9a52013-09-25 17:34:55 +01001146{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001147 return test_bit(engine->id, &dev_priv->gpu_error.missed_irq_rings);
Chris Wilson094f9a52013-09-25 17:34:55 +01001148}
1149
Chris Wilsonca5b7212015-12-11 11:32:58 +00001150static unsigned long local_clock_us(unsigned *cpu)
1151{
1152 unsigned long t;
1153
1154 /* Cheaply and approximately convert from nanoseconds to microseconds.
1155 * The result and subsequent calculations are also defined in the same
1156 * approximate microseconds units. The principal source of timing
1157 * error here is from the simple truncation.
1158 *
1159 * Note that local_clock() is only defined wrt to the current CPU;
1160 * the comparisons are no longer valid if we switch CPUs. Instead of
1161 * blocking preemption for the entire busywait, we can detect the CPU
1162 * switch and use that as indicator of system load and a reason to
1163 * stop busywaiting, see busywait_stop().
1164 */
1165 *cpu = get_cpu();
1166 t = local_clock() >> 10;
1167 put_cpu();
1168
1169 return t;
1170}
1171
1172static bool busywait_stop(unsigned long timeout, unsigned cpu)
1173{
1174 unsigned this_cpu;
1175
1176 if (time_after(local_clock_us(&this_cpu), timeout))
1177 return true;
1178
1179 return this_cpu != cpu;
1180}
1181
Chris Wilson91b0c352015-12-11 11:32:57 +00001182static int __i915_spin_request(struct drm_i915_gem_request *req, int state)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001183{
Chris Wilson2def4ad92015-04-07 16:20:41 +01001184 unsigned long timeout;
Chris Wilsonca5b7212015-12-11 11:32:58 +00001185 unsigned cpu;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001186
Chris Wilsonca5b7212015-12-11 11:32:58 +00001187 /* When waiting for high frequency requests, e.g. during synchronous
1188 * rendering split between the CPU and GPU, the finite amount of time
1189 * required to set up the irq and wait upon it limits the response
1190 * rate. By busywaiting on the request completion for a short while we
1191 * can service the high frequency waits as quick as possible. However,
1192 * if it is a slow request, we want to sleep as quickly as possible.
1193 * The tradeoff between waiting and sleeping is roughly the time it
1194 * takes to sleep on a request, on the order of a microsecond.
1195 */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001196
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001197 if (req->engine->irq_refcount)
Chris Wilson2def4ad92015-04-07 16:20:41 +01001198 return -EBUSY;
1199
Chris Wilson821485d2015-12-11 11:32:59 +00001200 /* Only spin if we know the GPU is processing this request */
1201 if (!i915_gem_request_started(req, true))
1202 return -EAGAIN;
1203
Chris Wilsonca5b7212015-12-11 11:32:58 +00001204 timeout = local_clock_us(&cpu) + 5;
Chris Wilson2def4ad92015-04-07 16:20:41 +01001205 while (!need_resched()) {
Daniel Vettereed29a52015-05-21 14:21:25 +02001206 if (i915_gem_request_completed(req, true))
Chris Wilson2def4ad92015-04-07 16:20:41 +01001207 return 0;
1208
Chris Wilson91b0c352015-12-11 11:32:57 +00001209 if (signal_pending_state(state, current))
1210 break;
1211
Chris Wilsonca5b7212015-12-11 11:32:58 +00001212 if (busywait_stop(timeout, cpu))
Chris Wilson2def4ad92015-04-07 16:20:41 +01001213 break;
1214
1215 cpu_relax_lowlatency();
1216 }
Chris Wilson821485d2015-12-11 11:32:59 +00001217
Daniel Vettereed29a52015-05-21 14:21:25 +02001218 if (i915_gem_request_completed(req, false))
Chris Wilson2def4ad92015-04-07 16:20:41 +01001219 return 0;
1220
1221 return -EAGAIN;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001222}
1223
Chris Wilsonb3612372012-08-24 09:35:08 +01001224/**
John Harrison9c654812014-11-24 18:49:35 +00001225 * __i915_wait_request - wait until execution of request has finished
1226 * @req: duh!
Chris Wilsonb3612372012-08-24 09:35:08 +01001227 * @interruptible: do an interruptible wait (normally yes)
1228 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001229 * @rps: RPS client
Chris Wilsonb3612372012-08-24 09:35:08 +01001230 *
Daniel Vetterf69061b2012-12-06 09:01:42 +01001231 * Note: It is of utmost importance that the passed in seqno and reset_counter
1232 * values have been read by the caller in an smp safe manner. Where read-side
1233 * locks are involved, it is sufficient to read the reset_counter before
1234 * unlocking the lock that protects the seqno. For lockless tricks, the
1235 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
1236 * inserted.
1237 *
John Harrison9c654812014-11-24 18:49:35 +00001238 * Returns 0 if the request was found within the alloted time. Else returns the
Chris Wilsonb3612372012-08-24 09:35:08 +01001239 * errno with remaining time filled in timeout argument.
1240 */
John Harrison9c654812014-11-24 18:49:35 +00001241int __i915_wait_request(struct drm_i915_gem_request *req,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001242 bool interruptible,
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00001243 s64 *timeout,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001244 struct intel_rps_client *rps)
Chris Wilsonb3612372012-08-24 09:35:08 +01001245{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001246 struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
Chris Wilsonc0336662016-05-06 15:40:21 +01001247 struct drm_i915_private *dev_priv = req->i915;
Mika Kuoppala168c3f22013-12-12 17:54:42 +02001248 const bool irq_test_in_progress =
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001249 ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_engine_flag(engine);
Chris Wilson91b0c352015-12-11 11:32:57 +00001250 int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
Chris Wilson094f9a52013-09-25 17:34:55 +01001251 DEFINE_WAIT(wait);
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001252 unsigned long timeout_expire;
Tvrtko Ursuline0313db2016-01-15 15:11:12 +00001253 s64 before = 0; /* Only to silence a compiler warning. */
Chris Wilsonb3612372012-08-24 09:35:08 +01001254 int ret;
1255
Jesse Barnes9df7575f2014-06-20 09:29:20 -07001256 WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
Paulo Zanonic67a4702013-08-19 13:18:09 -03001257
Chris Wilsonb4716182015-04-27 13:41:17 +01001258 if (list_empty(&req->list))
1259 return 0;
1260
John Harrison1b5a4332014-11-24 18:49:42 +00001261 if (i915_gem_request_completed(req, true))
Chris Wilsonb3612372012-08-24 09:35:08 +01001262 return 0;
1263
Chris Wilsonbb6d1982015-11-26 13:31:42 +00001264 timeout_expire = 0;
1265 if (timeout) {
1266 if (WARN_ON(*timeout < 0))
1267 return -EINVAL;
1268
1269 if (*timeout == 0)
1270 return -ETIME;
1271
1272 timeout_expire = jiffies + nsecs_to_jiffies_timeout(*timeout);
Tvrtko Ursuline0313db2016-01-15 15:11:12 +00001273
1274 /*
1275 * Record current time in case interrupted by signal, or wedged.
1276 */
1277 before = ktime_get_raw_ns();
Chris Wilsonbb6d1982015-11-26 13:31:42 +00001278 }
Chris Wilsonb3612372012-08-24 09:35:08 +01001279
Chris Wilson2e1b8732015-04-27 13:41:22 +01001280 if (INTEL_INFO(dev_priv)->gen >= 6)
Chris Wilsone61b9952015-04-27 13:41:24 +01001281 gen6_rps_boost(dev_priv, rps, req->emitted_jiffies);
Chris Wilsonb3612372012-08-24 09:35:08 +01001282
John Harrison74328ee2014-11-24 18:49:38 +00001283 trace_i915_gem_request_wait_begin(req);
Chris Wilson2def4ad92015-04-07 16:20:41 +01001284
1285 /* Optimistic spin for the next jiffie before touching IRQs */
Chris Wilson91b0c352015-12-11 11:32:57 +00001286 ret = __i915_spin_request(req, state);
Chris Wilson2def4ad92015-04-07 16:20:41 +01001287 if (ret == 0)
1288 goto out;
1289
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001290 if (!irq_test_in_progress && WARN_ON(!engine->irq_get(engine))) {
Chris Wilson2def4ad92015-04-07 16:20:41 +01001291 ret = -ENODEV;
1292 goto out;
1293 }
1294
Chris Wilson094f9a52013-09-25 17:34:55 +01001295 for (;;) {
1296 struct timer_list timer;
Chris Wilsonb3612372012-08-24 09:35:08 +01001297
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001298 prepare_to_wait(&engine->irq_queue, &wait, state);
Chris Wilsonb3612372012-08-24 09:35:08 +01001299
Daniel Vetterf69061b2012-12-06 09:01:42 +01001300 /* We need to check whether any gpu reset happened in between
Chris Wilsonf4457ae2016-04-13 17:35:08 +01001301 * the request being submitted and now. If a reset has occurred,
1302 * the request is effectively complete (we either are in the
1303 * process of or have discarded the rendering and completely
1304 * reset the GPU. The results of the request are lost and we
1305 * are free to continue on with the original operation.
1306 */
Chris Wilson299259a2016-04-13 17:35:06 +01001307 if (req->reset_counter != i915_reset_counter(&dev_priv->gpu_error)) {
Chris Wilsonf4457ae2016-04-13 17:35:08 +01001308 ret = 0;
Chris Wilson094f9a52013-09-25 17:34:55 +01001309 break;
1310 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01001311
John Harrison1b5a4332014-11-24 18:49:42 +00001312 if (i915_gem_request_completed(req, false)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001313 ret = 0;
1314 break;
1315 }
Chris Wilsonb3612372012-08-24 09:35:08 +01001316
Chris Wilson91b0c352015-12-11 11:32:57 +00001317 if (signal_pending_state(state, current)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001318 ret = -ERESTARTSYS;
1319 break;
1320 }
1321
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001322 if (timeout && time_after_eq(jiffies, timeout_expire)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001323 ret = -ETIME;
1324 break;
1325 }
1326
1327 timer.function = NULL;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001328 if (timeout || missed_irq(dev_priv, engine)) {
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001329 unsigned long expire;
1330
Chris Wilson094f9a52013-09-25 17:34:55 +01001331 setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001332 expire = missed_irq(dev_priv, engine) ? jiffies + 1 : timeout_expire;
Chris Wilson094f9a52013-09-25 17:34:55 +01001333 mod_timer(&timer, expire);
1334 }
1335
Chris Wilson5035c272013-10-04 09:58:46 +01001336 io_schedule();
Chris Wilson094f9a52013-09-25 17:34:55 +01001337
Chris Wilson094f9a52013-09-25 17:34:55 +01001338 if (timer.function) {
1339 del_singleshot_timer_sync(&timer);
1340 destroy_timer_on_stack(&timer);
1341 }
1342 }
Mika Kuoppala168c3f22013-12-12 17:54:42 +02001343 if (!irq_test_in_progress)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001344 engine->irq_put(engine);
Chris Wilson094f9a52013-09-25 17:34:55 +01001345
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001346 finish_wait(&engine->irq_queue, &wait);
Chris Wilsonb3612372012-08-24 09:35:08 +01001347
Chris Wilson2def4ad92015-04-07 16:20:41 +01001348out:
Chris Wilson2def4ad92015-04-07 16:20:41 +01001349 trace_i915_gem_request_wait_end(req);
1350
Chris Wilsonb3612372012-08-24 09:35:08 +01001351 if (timeout) {
Tvrtko Ursuline0313db2016-01-15 15:11:12 +00001352 s64 tres = *timeout - (ktime_get_raw_ns() - before);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00001353
1354 *timeout = tres < 0 ? 0 : tres;
Daniel Vetter9cca3062014-11-28 10:29:55 +01001355
1356 /*
1357 * Apparently ktime isn't accurate enough and occasionally has a
1358 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
1359 * things up to make the test happy. We allow up to 1 jiffy.
1360 *
1361 * This is a regrssion from the timespec->ktime conversion.
1362 */
1363 if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
1364 *timeout = 0;
Chris Wilsonb3612372012-08-24 09:35:08 +01001365 }
1366
Chris Wilson094f9a52013-09-25 17:34:55 +01001367 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001368}
1369
John Harrisonfcfa423c2015-05-29 17:44:12 +01001370int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
1371 struct drm_file *file)
1372{
John Harrisonfcfa423c2015-05-29 17:44:12 +01001373 struct drm_i915_file_private *file_priv;
1374
1375 WARN_ON(!req || !file || req->file_priv);
1376
1377 if (!req || !file)
1378 return -EINVAL;
1379
1380 if (req->file_priv)
1381 return -EINVAL;
1382
John Harrisonfcfa423c2015-05-29 17:44:12 +01001383 file_priv = file->driver_priv;
1384
1385 spin_lock(&file_priv->mm.lock);
1386 req->file_priv = file_priv;
1387 list_add_tail(&req->client_list, &file_priv->mm.request_list);
1388 spin_unlock(&file_priv->mm.lock);
1389
1390 req->pid = get_pid(task_pid(current));
1391
1392 return 0;
1393}
1394
Chris Wilsonb4716182015-04-27 13:41:17 +01001395static inline void
1396i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1397{
1398 struct drm_i915_file_private *file_priv = request->file_priv;
1399
1400 if (!file_priv)
1401 return;
1402
1403 spin_lock(&file_priv->mm.lock);
1404 list_del(&request->client_list);
1405 request->file_priv = NULL;
1406 spin_unlock(&file_priv->mm.lock);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001407
1408 put_pid(request->pid);
1409 request->pid = NULL;
Chris Wilsonb4716182015-04-27 13:41:17 +01001410}
1411
1412static void i915_gem_request_retire(struct drm_i915_gem_request *request)
1413{
1414 trace_i915_gem_request_retire(request);
1415
1416 /* We know the GPU must have read the request to have
1417 * sent us the seqno + interrupt, so use the position
1418 * of tail of the request to update the last known position
1419 * of the GPU head.
1420 *
1421 * Note this requires that we are always called in request
1422 * completion order.
1423 */
1424 request->ringbuf->last_retired_head = request->postfix;
1425
1426 list_del_init(&request->list);
1427 i915_gem_request_remove_from_client(request);
1428
Chris Wilsona16a4052016-04-28 09:56:56 +01001429 if (request->previous_context) {
Chris Wilson73db04c2016-04-28 09:56:55 +01001430 if (i915.enable_execlists)
Chris Wilsona16a4052016-04-28 09:56:56 +01001431 intel_lr_context_unpin(request->previous_context,
1432 request->engine);
Chris Wilson73db04c2016-04-28 09:56:55 +01001433 }
1434
Chris Wilsona16a4052016-04-28 09:56:56 +01001435 i915_gem_context_unreference(request->ctx);
Chris Wilsonb4716182015-04-27 13:41:17 +01001436 i915_gem_request_unreference(request);
1437}
1438
1439static void
1440__i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
1441{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001442 struct intel_engine_cs *engine = req->engine;
Chris Wilsonb4716182015-04-27 13:41:17 +01001443 struct drm_i915_gem_request *tmp;
1444
Chris Wilsonc0336662016-05-06 15:40:21 +01001445 lockdep_assert_held(&engine->i915->dev->struct_mutex);
Chris Wilsonb4716182015-04-27 13:41:17 +01001446
1447 if (list_empty(&req->list))
1448 return;
1449
1450 do {
1451 tmp = list_first_entry(&engine->request_list,
1452 typeof(*tmp), list);
1453
1454 i915_gem_request_retire(tmp);
1455 } while (tmp != req);
1456
1457 WARN_ON(i915_verify_lists(engine->dev));
1458}
1459
Chris Wilsonb3612372012-08-24 09:35:08 +01001460/**
Daniel Vettera4b3a572014-11-26 14:17:05 +01001461 * Waits for a request to be signaled, and cleans up the
Chris Wilsonb3612372012-08-24 09:35:08 +01001462 * request and object lists appropriately for that event.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001463 * @req: request to wait on
Chris Wilsonb3612372012-08-24 09:35:08 +01001464 */
1465int
Daniel Vettera4b3a572014-11-26 14:17:05 +01001466i915_wait_request(struct drm_i915_gem_request *req)
Chris Wilsonb3612372012-08-24 09:35:08 +01001467{
Tvrtko Ursulin791bee12016-04-19 16:46:09 +01001468 struct drm_i915_private *dev_priv = req->i915;
Daniel Vettera4b3a572014-11-26 14:17:05 +01001469 bool interruptible;
Chris Wilsonb3612372012-08-24 09:35:08 +01001470 int ret;
1471
Daniel Vettera4b3a572014-11-26 14:17:05 +01001472 interruptible = dev_priv->mm.interruptible;
1473
Tvrtko Ursulin791bee12016-04-19 16:46:09 +01001474 BUG_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
Chris Wilsonb3612372012-08-24 09:35:08 +01001475
Chris Wilson299259a2016-04-13 17:35:06 +01001476 ret = __i915_wait_request(req, interruptible, NULL, NULL);
Chris Wilsonb4716182015-04-27 13:41:17 +01001477 if (ret)
1478 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001479
Chris Wilsone075a322016-05-13 11:57:22 +01001480 /* If the GPU hung, we want to keep the requests to find the guilty. */
1481 if (req->reset_counter == i915_reset_counter(&dev_priv->gpu_error))
1482 __i915_gem_request_retire__upto(req);
1483
Chris Wilsond26e3af2013-06-29 22:05:26 +01001484 return 0;
1485}
1486
Chris Wilsonb3612372012-08-24 09:35:08 +01001487/**
1488 * Ensures that all rendering to the object has completed and the object is
1489 * safe to unbind from the GTT or access from the CPU.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001490 * @obj: i915 gem object
1491 * @readonly: waiting for read access or write
Chris Wilsonb3612372012-08-24 09:35:08 +01001492 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01001493int
Chris Wilsonb3612372012-08-24 09:35:08 +01001494i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1495 bool readonly)
1496{
Chris Wilsonb4716182015-04-27 13:41:17 +01001497 int ret, i;
Chris Wilsonb3612372012-08-24 09:35:08 +01001498
Chris Wilsonb4716182015-04-27 13:41:17 +01001499 if (!obj->active)
Chris Wilsonb3612372012-08-24 09:35:08 +01001500 return 0;
1501
Chris Wilsonb4716182015-04-27 13:41:17 +01001502 if (readonly) {
1503 if (obj->last_write_req != NULL) {
1504 ret = i915_wait_request(obj->last_write_req);
1505 if (ret)
1506 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001507
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001508 i = obj->last_write_req->engine->id;
Chris Wilsonb4716182015-04-27 13:41:17 +01001509 if (obj->last_read_req[i] == obj->last_write_req)
1510 i915_gem_object_retire__read(obj, i);
1511 else
1512 i915_gem_object_retire__write(obj);
1513 }
1514 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001515 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01001516 if (obj->last_read_req[i] == NULL)
1517 continue;
1518
1519 ret = i915_wait_request(obj->last_read_req[i]);
1520 if (ret)
1521 return ret;
1522
1523 i915_gem_object_retire__read(obj, i);
1524 }
Chris Wilsond501b1d2016-04-13 17:35:02 +01001525 GEM_BUG_ON(obj->active);
Chris Wilsonb4716182015-04-27 13:41:17 +01001526 }
1527
1528 return 0;
1529}
1530
1531static void
1532i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
1533 struct drm_i915_gem_request *req)
1534{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001535 int ring = req->engine->id;
Chris Wilsonb4716182015-04-27 13:41:17 +01001536
1537 if (obj->last_read_req[ring] == req)
1538 i915_gem_object_retire__read(obj, ring);
1539 else if (obj->last_write_req == req)
1540 i915_gem_object_retire__write(obj);
1541
Chris Wilsone075a322016-05-13 11:57:22 +01001542 if (req->reset_counter == i915_reset_counter(&req->i915->gpu_error))
1543 __i915_gem_request_retire__upto(req);
Chris Wilsonb3612372012-08-24 09:35:08 +01001544}
1545
Chris Wilson3236f572012-08-24 09:35:09 +01001546/* A nonblocking variant of the above wait. This is a highly dangerous routine
1547 * as the object state may change during this call.
1548 */
1549static __must_check int
1550i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001551 struct intel_rps_client *rps,
Chris Wilson3236f572012-08-24 09:35:09 +01001552 bool readonly)
1553{
1554 struct drm_device *dev = obj->base.dev;
1555 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001556 struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01001557 int ret, i, n = 0;
Chris Wilson3236f572012-08-24 09:35:09 +01001558
1559 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1560 BUG_ON(!dev_priv->mm.interruptible);
1561
Chris Wilsonb4716182015-04-27 13:41:17 +01001562 if (!obj->active)
Chris Wilson3236f572012-08-24 09:35:09 +01001563 return 0;
1564
Chris Wilsonb4716182015-04-27 13:41:17 +01001565 if (readonly) {
1566 struct drm_i915_gem_request *req;
1567
1568 req = obj->last_write_req;
1569 if (req == NULL)
1570 return 0;
1571
Chris Wilsonb4716182015-04-27 13:41:17 +01001572 requests[n++] = i915_gem_request_reference(req);
1573 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001574 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01001575 struct drm_i915_gem_request *req;
1576
1577 req = obj->last_read_req[i];
1578 if (req == NULL)
1579 continue;
1580
Chris Wilsonb4716182015-04-27 13:41:17 +01001581 requests[n++] = i915_gem_request_reference(req);
1582 }
1583 }
1584
1585 mutex_unlock(&dev->struct_mutex);
Chris Wilson299259a2016-04-13 17:35:06 +01001586 ret = 0;
Chris Wilsonb4716182015-04-27 13:41:17 +01001587 for (i = 0; ret == 0 && i < n; i++)
Chris Wilson299259a2016-04-13 17:35:06 +01001588 ret = __i915_wait_request(requests[i], true, NULL, rps);
Chris Wilsonb4716182015-04-27 13:41:17 +01001589 mutex_lock(&dev->struct_mutex);
1590
Chris Wilsonb4716182015-04-27 13:41:17 +01001591 for (i = 0; i < n; i++) {
1592 if (ret == 0)
1593 i915_gem_object_retire_request(obj, requests[i]);
1594 i915_gem_request_unreference(requests[i]);
1595 }
1596
1597 return ret;
Chris Wilson3236f572012-08-24 09:35:09 +01001598}
1599
Chris Wilson2e1b8732015-04-27 13:41:22 +01001600static struct intel_rps_client *to_rps_client(struct drm_file *file)
1601{
1602 struct drm_i915_file_private *fpriv = file->driver_priv;
1603 return &fpriv->rps;
1604}
1605
Eric Anholt673a3942008-07-30 12:06:12 -07001606/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001607 * Called when user space prepares to use an object with the CPU, either
1608 * through the mmap ioctl's mapping or a GTT mapping.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001609 * @dev: drm device
1610 * @data: ioctl data blob
1611 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001612 */
1613int
1614i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001615 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001616{
1617 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001618 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001619 uint32_t read_domains = args->read_domains;
1620 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001621 int ret;
1622
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001623 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001624 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001625 return -EINVAL;
1626
Chris Wilson21d509e2009-06-06 09:46:02 +01001627 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001628 return -EINVAL;
1629
1630 /* Having something in the write domain implies it's in the read
1631 * domain, and only that read domain. Enforce that in the request.
1632 */
1633 if (write_domain != 0 && read_domains != write_domain)
1634 return -EINVAL;
1635
Chris Wilson76c1dec2010-09-25 11:22:51 +01001636 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001637 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001638 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001639
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001640 obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001641 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001642 ret = -ENOENT;
1643 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001644 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001645
Chris Wilson3236f572012-08-24 09:35:09 +01001646 /* Try to flush the object off the GPU without holding the lock.
1647 * We will repeat the flush holding the lock in the normal manner
1648 * to catch cases where we are gazumped.
1649 */
Chris Wilson6e4930f2014-02-07 18:37:06 -02001650 ret = i915_gem_object_wait_rendering__nonblocking(obj,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001651 to_rps_client(file),
Chris Wilson6e4930f2014-02-07 18:37:06 -02001652 !write_domain);
Chris Wilson3236f572012-08-24 09:35:09 +01001653 if (ret)
1654 goto unref;
1655
Chris Wilson43566de2015-01-02 16:29:29 +05301656 if (read_domains & I915_GEM_DOMAIN_GTT)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001657 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301658 else
Eric Anholte47c68e2008-11-14 13:35:19 -08001659 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001660
Daniel Vetter031b6982015-06-26 19:35:16 +02001661 if (write_domain != 0)
1662 intel_fb_obj_invalidate(obj,
1663 write_domain == I915_GEM_DOMAIN_GTT ?
1664 ORIGIN_GTT : ORIGIN_CPU);
1665
Chris Wilson3236f572012-08-24 09:35:09 +01001666unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001667 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001668unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001669 mutex_unlock(&dev->struct_mutex);
1670 return ret;
1671}
1672
1673/**
1674 * Called when user space has done writes to this buffer
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001675 * @dev: drm device
1676 * @data: ioctl data blob
1677 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001678 */
1679int
1680i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001681 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001682{
1683 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001684 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001685 int ret = 0;
1686
Chris Wilson76c1dec2010-09-25 11:22:51 +01001687 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001688 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001689 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001690
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001691 obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001692 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001693 ret = -ENOENT;
1694 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001695 }
1696
Eric Anholt673a3942008-07-30 12:06:12 -07001697 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson2c225692013-08-09 12:26:45 +01001698 if (obj->pin_display)
Daniel Vettere62b59e2015-01-21 14:53:48 +01001699 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08001700
Chris Wilson05394f32010-11-08 19:18:58 +00001701 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001702unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001703 mutex_unlock(&dev->struct_mutex);
1704 return ret;
1705}
1706
1707/**
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01001708 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1709 * it is mapped to.
1710 * @dev: drm device
1711 * @data: ioctl data blob
1712 * @file: drm file
Eric Anholt673a3942008-07-30 12:06:12 -07001713 *
1714 * While the mapping holds a reference on the contents of the object, it doesn't
1715 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001716 *
1717 * IMPORTANT:
1718 *
1719 * DRM driver writers who look a this function as an example for how to do GEM
1720 * mmap support, please don't implement mmap support like here. The modern way
1721 * to implement DRM mmap support is with an mmap offset ioctl (like
1722 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1723 * That way debug tooling like valgrind will understand what's going on, hiding
1724 * the mmap call in a driver private ioctl will break that. The i915 driver only
1725 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001726 */
1727int
1728i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001729 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001730{
1731 struct drm_i915_gem_mmap *args = data;
1732 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001733 unsigned long addr;
1734
Akash Goel1816f922015-01-02 16:29:30 +05301735 if (args->flags & ~(I915_MMAP_WC))
1736 return -EINVAL;
1737
Borislav Petkov568a58e2016-03-29 17:42:01 +02001738 if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
Akash Goel1816f922015-01-02 16:29:30 +05301739 return -ENODEV;
1740
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001741 obj = drm_gem_object_lookup(file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001742 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001743 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001744
Daniel Vetter1286ff72012-05-10 15:25:09 +02001745 /* prime objects have no backing filp to GEM mmap
1746 * pages from.
1747 */
1748 if (!obj->filp) {
1749 drm_gem_object_unreference_unlocked(obj);
1750 return -EINVAL;
1751 }
1752
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001753 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001754 PROT_READ | PROT_WRITE, MAP_SHARED,
1755 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301756 if (args->flags & I915_MMAP_WC) {
1757 struct mm_struct *mm = current->mm;
1758 struct vm_area_struct *vma;
1759
Michal Hocko80a89a52016-05-23 16:26:11 -07001760 if (down_write_killable(&mm->mmap_sem)) {
1761 drm_gem_object_unreference_unlocked(obj);
1762 return -EINTR;
1763 }
Akash Goel1816f922015-01-02 16:29:30 +05301764 vma = find_vma(mm, addr);
1765 if (vma)
1766 vma->vm_page_prot =
1767 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1768 else
1769 addr = -ENOMEM;
1770 up_write(&mm->mmap_sem);
1771 }
Luca Barbieribc9025b2010-02-09 05:49:12 +00001772 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001773 if (IS_ERR((void *)addr))
1774 return addr;
1775
1776 args->addr_ptr = (uint64_t) addr;
1777
1778 return 0;
1779}
1780
Jesse Barnesde151cf2008-11-12 10:03:55 -08001781/**
1782 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001783 * @vma: VMA in question
1784 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001785 *
1786 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1787 * from userspace. The fault handler takes care of binding the object to
1788 * the GTT (if needed), allocating and programming a fence register (again,
1789 * only if needed based on whether the old reg is still valid or the object
1790 * is tiled) and inserting a new PTE into the faulting process.
1791 *
1792 * Note that the faulting process may involve evicting existing objects
1793 * from the GTT and/or fence registers to make room. So performance may
1794 * suffer if the GTT working set is large or there are few fence registers
1795 * left.
1796 */
1797int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1798{
Chris Wilson05394f32010-11-08 19:18:58 +00001799 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1800 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001801 struct drm_i915_private *dev_priv = to_i915(dev);
1802 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001803 struct i915_ggtt_view view = i915_ggtt_view_normal;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001804 pgoff_t page_offset;
1805 unsigned long pfn;
1806 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001807 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001808
Paulo Zanonif65c9162013-11-27 18:20:34 -02001809 intel_runtime_pm_get(dev_priv);
1810
Jesse Barnesde151cf2008-11-12 10:03:55 -08001811 /* We don't use vmf->pgoff since that has the fake offset */
1812 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1813 PAGE_SHIFT;
1814
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001815 ret = i915_mutex_lock_interruptible(dev);
1816 if (ret)
1817 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001818
Chris Wilsondb53a302011-02-03 11:57:46 +00001819 trace_i915_gem_object_fault(obj, page_offset, true, write);
1820
Chris Wilson6e4930f2014-02-07 18:37:06 -02001821 /* Try to flush the object off the GPU first without holding the lock.
1822 * Upon reacquiring the lock, we will perform our sanity checks and then
1823 * repeat the flush holding the lock in the normal manner to catch cases
1824 * where we are gazumped.
1825 */
1826 ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
1827 if (ret)
1828 goto unlock;
1829
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001830 /* Access to snoopable pages through the GTT is incoherent. */
1831 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001832 ret = -EFAULT;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001833 goto unlock;
1834 }
1835
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001836 /* Use a partial view if the object is bigger than the aperture. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001837 if (obj->base.size >= ggtt->mappable_end &&
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001838 obj->tiling_mode == I915_TILING_NONE) {
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001839 static const unsigned int chunk_size = 256; // 1 MiB
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001840
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001841 memset(&view, 0, sizeof(view));
1842 view.type = I915_GGTT_VIEW_PARTIAL;
1843 view.params.partial.offset = rounddown(page_offset, chunk_size);
1844 view.params.partial.size =
1845 min_t(unsigned int,
1846 chunk_size,
1847 (vma->vm_end - vma->vm_start)/PAGE_SIZE -
1848 view.params.partial.offset);
1849 }
1850
1851 /* Now pin it into the GTT if needed */
1852 ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001853 if (ret)
1854 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001855
Chris Wilsonc9839302012-11-20 10:45:17 +00001856 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1857 if (ret)
1858 goto unpin;
1859
1860 ret = i915_gem_object_get_fence(obj);
1861 if (ret)
1862 goto unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001863
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001864 /* Finally, remap it using the new GTT offset */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001865 pfn = ggtt->mappable_base +
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001866 i915_gem_obj_ggtt_offset_view(obj, &view);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001867 pfn >>= PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001868
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001869 if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
1870 /* Overriding existing pages in partial view does not cause
1871 * us any trouble as TLBs are still valid because the fault
1872 * is due to userspace losing part of the mapping or never
1873 * having accessed it before (at this partials' range).
1874 */
1875 unsigned long base = vma->vm_start +
1876 (view.params.partial.offset << PAGE_SHIFT);
1877 unsigned int i;
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001878
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001879 for (i = 0; i < view.params.partial.size; i++) {
1880 ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001881 if (ret)
1882 break;
1883 }
1884
1885 obj->fault_mappable = true;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001886 } else {
1887 if (!obj->fault_mappable) {
1888 unsigned long size = min_t(unsigned long,
1889 vma->vm_end - vma->vm_start,
1890 obj->base.size);
1891 int i;
1892
1893 for (i = 0; i < size >> PAGE_SHIFT; i++) {
1894 ret = vm_insert_pfn(vma,
1895 (unsigned long)vma->vm_start + i * PAGE_SIZE,
1896 pfn + i);
1897 if (ret)
1898 break;
1899 }
1900
1901 obj->fault_mappable = true;
1902 } else
1903 ret = vm_insert_pfn(vma,
1904 (unsigned long)vmf->virtual_address,
1905 pfn + page_offset);
1906 }
Chris Wilsonc9839302012-11-20 10:45:17 +00001907unpin:
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001908 i915_gem_object_ggtt_unpin_view(obj, &view);
Chris Wilsonc7150892009-09-23 00:43:56 +01001909unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001910 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001911out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001912 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001913 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001914 /*
1915 * We eat errors when the gpu is terminally wedged to avoid
1916 * userspace unduly crashing (gl has no provisions for mmaps to
1917 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1918 * and so needs to be reported.
1919 */
1920 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001921 ret = VM_FAULT_SIGBUS;
1922 break;
1923 }
Chris Wilson045e7692010-11-07 09:18:22 +00001924 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001925 /*
1926 * EAGAIN means the gpu is hung and we'll wait for the error
1927 * handler to reset everything when re-faulting in
1928 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001929 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001930 case 0:
1931 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001932 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001933 case -EBUSY:
1934 /*
1935 * EBUSY is ok: this just means that another thread
1936 * already did the job.
1937 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001938 ret = VM_FAULT_NOPAGE;
1939 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001940 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001941 ret = VM_FAULT_OOM;
1942 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001943 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001944 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001945 ret = VM_FAULT_SIGBUS;
1946 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001947 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001948 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001949 ret = VM_FAULT_SIGBUS;
1950 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001951 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001952
1953 intel_runtime_pm_put(dev_priv);
1954 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001955}
1956
1957/**
Chris Wilson901782b2009-07-10 08:18:50 +01001958 * i915_gem_release_mmap - remove physical page mappings
1959 * @obj: obj in question
1960 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001961 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001962 * relinquish ownership of the pages back to the system.
1963 *
1964 * It is vital that we remove the page mapping if we have mapped a tiled
1965 * object through the GTT and then lose the fence register due to
1966 * resource pressure. Similarly if the object has been moved out of the
1967 * aperture, than pages mapped into userspace must be revoked. Removing the
1968 * mapping will then trigger a page fault on the next user access, allowing
1969 * fixup by i915_gem_fault().
1970 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001971void
Chris Wilson05394f32010-11-08 19:18:58 +00001972i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001973{
Chris Wilson349f2cc2016-04-13 17:35:12 +01001974 /* Serialisation between user GTT access and our code depends upon
1975 * revoking the CPU's PTE whilst the mutex is held. The next user
1976 * pagefault then has to wait until we release the mutex.
1977 */
1978 lockdep_assert_held(&obj->base.dev->struct_mutex);
1979
Chris Wilson6299f992010-11-24 12:23:44 +00001980 if (!obj->fault_mappable)
1981 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001982
David Herrmann6796cb12014-01-03 14:24:19 +01001983 drm_vma_node_unmap(&obj->base.vma_node,
1984 obj->base.dev->anon_inode->i_mapping);
Chris Wilson349f2cc2016-04-13 17:35:12 +01001985
1986 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1987 * memory transactions from userspace before we return. The TLB
1988 * flushing implied above by changing the PTE above *should* be
1989 * sufficient, an extra barrier here just provides us with a bit
1990 * of paranoid documentation about our requirement to serialise
1991 * memory writes before touching registers / GSM.
1992 */
1993 wmb();
1994
Chris Wilson6299f992010-11-24 12:23:44 +00001995 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001996}
1997
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001998void
1999i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
2000{
2001 struct drm_i915_gem_object *obj;
2002
2003 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
2004 i915_gem_release_mmap(obj);
2005}
2006
Imre Deak0fa87792013-01-07 21:47:35 +02002007uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07002008i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00002009{
Chris Wilsone28f8712011-07-18 13:11:49 -07002010 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002011
2012 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07002013 tiling_mode == I915_TILING_NONE)
2014 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002015
2016 /* Previous chips need a power-of-two fence region when tiling */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01002017 if (IS_GEN3(dev))
Chris Wilsone28f8712011-07-18 13:11:49 -07002018 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002019 else
Chris Wilsone28f8712011-07-18 13:11:49 -07002020 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002021
Chris Wilsone28f8712011-07-18 13:11:49 -07002022 while (gtt_size < size)
2023 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002024
Chris Wilsone28f8712011-07-18 13:11:49 -07002025 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002026}
2027
Jesse Barnesde151cf2008-11-12 10:03:55 -08002028/**
2029 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002030 * @dev: drm device
2031 * @size: object size
2032 * @tiling_mode: tiling mode
2033 * @fenced: is fenced alignemned required or not
Jesse Barnesde151cf2008-11-12 10:03:55 -08002034 *
2035 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01002036 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002037 */
Imre Deakd8651102013-01-07 21:47:33 +02002038uint32_t
2039i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
2040 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002041{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002042 /*
2043 * Minimum alignment is 4k (GTT page size), but might be greater
2044 * if a fence register is needed for the object.
2045 */
Imre Deakd8651102013-01-07 21:47:33 +02002046 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07002047 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002048 return 4096;
2049
2050 /*
2051 * Previous chips need to be aligned to the size of the smallest
2052 * fence register that can contain the object.
2053 */
Chris Wilsone28f8712011-07-18 13:11:49 -07002054 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002055}
2056
Chris Wilsond8cb5082012-08-11 15:41:03 +01002057static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2058{
2059 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2060 int ret;
2061
Daniel Vetterda494d72012-12-20 15:11:16 +01002062 dev_priv->mm.shrinker_no_lock_stealing = true;
2063
Chris Wilsond8cb5082012-08-11 15:41:03 +01002064 ret = drm_gem_create_mmap_offset(&obj->base);
2065 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01002066 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002067
2068 /* Badly fragmented mmap space? The only way we can recover
2069 * space is by destroying unwanted objects. We can't randomly release
2070 * mmap_offsets as userspace expects them to be persistent for the
2071 * lifetime of the objects. The closest we can is to release the
2072 * offsets on purgeable objects by truncating it and marking it purged,
2073 * which prevents userspace from ever using that object again.
2074 */
Chris Wilson21ab4e72014-09-09 11:16:08 +01002075 i915_gem_shrink(dev_priv,
2076 obj->base.size >> PAGE_SHIFT,
2077 I915_SHRINK_BOUND |
2078 I915_SHRINK_UNBOUND |
2079 I915_SHRINK_PURGEABLE);
Chris Wilsond8cb5082012-08-11 15:41:03 +01002080 ret = drm_gem_create_mmap_offset(&obj->base);
2081 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01002082 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002083
2084 i915_gem_shrink_all(dev_priv);
Daniel Vetterda494d72012-12-20 15:11:16 +01002085 ret = drm_gem_create_mmap_offset(&obj->base);
2086out:
2087 dev_priv->mm.shrinker_no_lock_stealing = false;
2088
2089 return ret;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002090}
2091
2092static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2093{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002094 drm_gem_free_mmap_offset(&obj->base);
2095}
2096
Dave Airlieda6b51d2014-12-24 13:11:17 +10002097int
Dave Airlieff72145b2011-02-07 12:16:14 +10002098i915_gem_mmap_gtt(struct drm_file *file,
2099 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002100 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002101 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002102{
Chris Wilson05394f32010-11-08 19:18:58 +00002103 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002104 int ret;
2105
Chris Wilson76c1dec2010-09-25 11:22:51 +01002106 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002107 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01002108 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002109
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01002110 obj = to_intel_bo(drm_gem_object_lookup(file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00002111 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002112 ret = -ENOENT;
2113 goto unlock;
2114 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002115
Chris Wilson05394f32010-11-08 19:18:58 +00002116 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002117 DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002118 ret = -EFAULT;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002119 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01002120 }
2121
Chris Wilsond8cb5082012-08-11 15:41:03 +01002122 ret = i915_gem_object_create_mmap_offset(obj);
2123 if (ret)
2124 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002125
David Herrmann0de23972013-07-24 21:07:52 +02002126 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002127
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002128out:
Chris Wilson05394f32010-11-08 19:18:58 +00002129 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002130unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002131 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002132 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002133}
2134
Dave Airlieff72145b2011-02-07 12:16:14 +10002135/**
2136 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2137 * @dev: DRM device
2138 * @data: GTT mapping ioctl data
2139 * @file: GEM object info
2140 *
2141 * Simply returns the fake offset to userspace so it can mmap it.
2142 * The mmap call will end up in drm_gem_mmap(), which will set things
2143 * up so we can get faults in the handler above.
2144 *
2145 * The fault handler will take care of binding the object into the GTT
2146 * (since it may have been evicted to make room for something), allocating
2147 * a fence register, and mapping the appropriate aperture address into
2148 * userspace.
2149 */
2150int
2151i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2152 struct drm_file *file)
2153{
2154 struct drm_i915_gem_mmap_gtt *args = data;
2155
Dave Airlieda6b51d2014-12-24 13:11:17 +10002156 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002157}
2158
Daniel Vetter225067e2012-08-20 10:23:20 +02002159/* Immediately discard the backing storage */
2160static void
2161i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002162{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002163 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002164
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002165 if (obj->base.filp == NULL)
2166 return;
2167
Daniel Vetter225067e2012-08-20 10:23:20 +02002168 /* Our goal here is to return as much of the memory as
2169 * is possible back to the system as we are called from OOM.
2170 * To do this we must instruct the shmfs to drop all of its
2171 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002172 */
Chris Wilson55372522014-03-25 13:23:06 +00002173 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Daniel Vetter225067e2012-08-20 10:23:20 +02002174 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01002175}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002176
Chris Wilson55372522014-03-25 13:23:06 +00002177/* Try to discard unwanted pages */
2178static void
2179i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002180{
Chris Wilson55372522014-03-25 13:23:06 +00002181 struct address_space *mapping;
2182
2183 switch (obj->madv) {
2184 case I915_MADV_DONTNEED:
2185 i915_gem_object_truncate(obj);
2186 case __I915_MADV_PURGED:
2187 return;
2188 }
2189
2190 if (obj->base.filp == NULL)
2191 return;
2192
2193 mapping = file_inode(obj->base.filp)->i_mapping,
2194 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002195}
2196
Chris Wilson5cdf5882010-09-27 15:51:07 +01002197static void
Chris Wilson05394f32010-11-08 19:18:58 +00002198i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002199{
Dave Gordon85d12252016-05-20 11:54:06 +01002200 struct sgt_iter sgt_iter;
2201 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002202 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002203
Chris Wilson05394f32010-11-08 19:18:58 +00002204 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07002205
Chris Wilson6c085a72012-08-20 11:40:46 +02002206 ret = i915_gem_object_set_to_cpu_domain(obj, true);
Chris Wilsonf4457ae2016-04-13 17:35:08 +01002207 if (WARN_ON(ret)) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002208 /* In the event of a disaster, abandon all caches and
2209 * hope for the best.
2210 */
Chris Wilson2c225692013-08-09 12:26:45 +01002211 i915_gem_clflush_object(obj, true);
Chris Wilson6c085a72012-08-20 11:40:46 +02002212 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2213 }
2214
Imre Deake2273302015-07-09 12:59:05 +03002215 i915_gem_gtt_finish_object(obj);
2216
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002217 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07002218 i915_gem_object_save_bit_17_swizzle(obj);
2219
Chris Wilson05394f32010-11-08 19:18:58 +00002220 if (obj->madv == I915_MADV_DONTNEED)
2221 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01002222
Dave Gordon85d12252016-05-20 11:54:06 +01002223 for_each_sgt_page(page, sgt_iter, obj->pages) {
Chris Wilson05394f32010-11-08 19:18:58 +00002224 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002225 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002226
Chris Wilson05394f32010-11-08 19:18:58 +00002227 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002228 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002229
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002230 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002231 }
Chris Wilson05394f32010-11-08 19:18:58 +00002232 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002233
Chris Wilson9da3da62012-06-01 15:20:22 +01002234 sg_free_table(obj->pages);
2235 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002236}
2237
Chris Wilsondd624af2013-01-15 12:39:35 +00002238int
Chris Wilson37e680a2012-06-07 15:38:42 +01002239i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
2240{
2241 const struct drm_i915_gem_object_ops *ops = obj->ops;
2242
Chris Wilson2f745ad2012-09-04 21:02:58 +01002243 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01002244 return 0;
2245
Chris Wilsona5570172012-09-04 21:02:54 +01002246 if (obj->pages_pin_count)
2247 return -EBUSY;
2248
Ben Widawsky98438772013-07-31 17:00:12 -07002249 BUG_ON(i915_gem_obj_bound_any(obj));
Ben Widawsky3e123022013-07-31 17:00:04 -07002250
Chris Wilsona2165e32012-12-03 11:49:00 +00002251 /* ->put_pages might need to allocate memory for the bit17 swizzle
2252 * array, hence protect them from being reaped by removing them from gtt
2253 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002254 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00002255
Chris Wilson0a798eb2016-04-08 12:11:11 +01002256 if (obj->mapping) {
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002257 if (is_vmalloc_addr(obj->mapping))
2258 vunmap(obj->mapping);
2259 else
2260 kunmap(kmap_to_page(obj->mapping));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002261 obj->mapping = NULL;
2262 }
2263
Chris Wilson37e680a2012-06-07 15:38:42 +01002264 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002265 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002266
Chris Wilson55372522014-03-25 13:23:06 +00002267 i915_gem_object_invalidate(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02002268
2269 return 0;
2270}
2271
Chris Wilson37e680a2012-06-07 15:38:42 +01002272static int
Chris Wilson6c085a72012-08-20 11:40:46 +02002273i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002274{
Chris Wilson6c085a72012-08-20 11:40:46 +02002275 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002276 int page_count, i;
2277 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002278 struct sg_table *st;
2279 struct scatterlist *sg;
Dave Gordon85d12252016-05-20 11:54:06 +01002280 struct sgt_iter sgt_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002281 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002282 unsigned long last_pfn = 0; /* suppress gcc warning */
Imre Deake2273302015-07-09 12:59:05 +03002283 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002284 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002285
Chris Wilson6c085a72012-08-20 11:40:46 +02002286 /* Assert that the object is not currently in any GPU domain. As it
2287 * wasn't in the GTT, there shouldn't be any way it could have been in
2288 * a GPU cache
2289 */
2290 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2291 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2292
Chris Wilson9da3da62012-06-01 15:20:22 +01002293 st = kmalloc(sizeof(*st), GFP_KERNEL);
2294 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002295 return -ENOMEM;
2296
Chris Wilson9da3da62012-06-01 15:20:22 +01002297 page_count = obj->base.size / PAGE_SIZE;
2298 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002299 kfree(st);
2300 return -ENOMEM;
2301 }
2302
2303 /* Get the list of pages out of our struct file. They'll be pinned
2304 * at this point until we release them.
2305 *
2306 * Fail silently without starting the shrinker
2307 */
Al Viro496ad9a2013-01-23 17:07:38 -05002308 mapping = file_inode(obj->base.filp)->i_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002309 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002310 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002311 sg = st->sgl;
2312 st->nents = 0;
2313 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002314 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2315 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002316 i915_gem_shrink(dev_priv,
2317 page_count,
2318 I915_SHRINK_BOUND |
2319 I915_SHRINK_UNBOUND |
2320 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002321 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2322 }
2323 if (IS_ERR(page)) {
2324 /* We've tried hard to allocate the memory by reaping
2325 * our own buffer, now let the real VM do its job and
2326 * go down in flames if truly OOM.
2327 */
Chris Wilson6c085a72012-08-20 11:40:46 +02002328 i915_gem_shrink_all(dev_priv);
David Herrmannf461d1b2014-05-25 14:34:10 +02002329 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002330 if (IS_ERR(page)) {
2331 ret = PTR_ERR(page);
Chris Wilson6c085a72012-08-20 11:40:46 +02002332 goto err_pages;
Imre Deake2273302015-07-09 12:59:05 +03002333 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002334 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002335#ifdef CONFIG_SWIOTLB
2336 if (swiotlb_nr_tbl()) {
2337 st->nents++;
2338 sg_set_page(sg, page, PAGE_SIZE, 0);
2339 sg = sg_next(sg);
2340 continue;
2341 }
2342#endif
Imre Deak90797e62013-02-18 19:28:03 +02002343 if (!i || page_to_pfn(page) != last_pfn + 1) {
2344 if (i)
2345 sg = sg_next(sg);
2346 st->nents++;
2347 sg_set_page(sg, page, PAGE_SIZE, 0);
2348 } else {
2349 sg->length += PAGE_SIZE;
2350 }
2351 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002352
2353 /* Check that the i965g/gm workaround works. */
2354 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002355 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002356#ifdef CONFIG_SWIOTLB
2357 if (!swiotlb_nr_tbl())
2358#endif
2359 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002360 obj->pages = st;
2361
Imre Deake2273302015-07-09 12:59:05 +03002362 ret = i915_gem_gtt_prepare_object(obj);
2363 if (ret)
2364 goto err_pages;
2365
Eric Anholt673a3942008-07-30 12:06:12 -07002366 if (i915_gem_object_needs_bit17_swizzle(obj))
2367 i915_gem_object_do_bit_17_swizzle(obj);
2368
Daniel Vetter656bfa32014-11-20 09:26:30 +01002369 if (obj->tiling_mode != I915_TILING_NONE &&
2370 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
2371 i915_gem_object_pin_pages(obj);
2372
Eric Anholt673a3942008-07-30 12:06:12 -07002373 return 0;
2374
2375err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02002376 sg_mark_end(sg);
Dave Gordon85d12252016-05-20 11:54:06 +01002377 for_each_sgt_page(page, sgt_iter, st)
2378 put_page(page);
Chris Wilson9da3da62012-06-01 15:20:22 +01002379 sg_free_table(st);
2380 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002381
2382 /* shmemfs first checks if there is enough memory to allocate the page
2383 * and reports ENOSPC should there be insufficient, along with the usual
2384 * ENOMEM for a genuine allocation failure.
2385 *
2386 * We use ENOSPC in our driver to mean that we have run out of aperture
2387 * space and so want to translate the error from shmemfs back to our
2388 * usual understanding of ENOMEM.
2389 */
Imre Deake2273302015-07-09 12:59:05 +03002390 if (ret == -ENOSPC)
2391 ret = -ENOMEM;
2392
2393 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002394}
2395
Chris Wilson37e680a2012-06-07 15:38:42 +01002396/* Ensure that the associated pages are gathered from the backing storage
2397 * and pinned into our object. i915_gem_object_get_pages() may be called
2398 * multiple times before they are released by a single call to
2399 * i915_gem_object_put_pages() - once the pages are no longer referenced
2400 * either as a result of memory pressure (reaping pages under the shrinker)
2401 * or as the object is itself released.
2402 */
2403int
2404i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2405{
2406 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2407 const struct drm_i915_gem_object_ops *ops = obj->ops;
2408 int ret;
2409
Chris Wilson2f745ad2012-09-04 21:02:58 +01002410 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01002411 return 0;
2412
Chris Wilson43e28f02013-01-08 10:53:09 +00002413 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002414 DRM_DEBUG("Attempting to obtain a purgeable object\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002415 return -EFAULT;
Chris Wilson43e28f02013-01-08 10:53:09 +00002416 }
2417
Chris Wilsona5570172012-09-04 21:02:54 +01002418 BUG_ON(obj->pages_pin_count);
2419
Chris Wilson37e680a2012-06-07 15:38:42 +01002420 ret = ops->get_pages(obj);
2421 if (ret)
2422 return ret;
2423
Ben Widawsky35c20a62013-05-31 11:28:48 -07002424 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilsonee286372015-04-07 16:20:25 +01002425
2426 obj->get_page.sg = obj->pages->sgl;
2427 obj->get_page.last = 0;
2428
Chris Wilson37e680a2012-06-07 15:38:42 +01002429 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002430}
2431
Dave Gordondd6034c2016-05-20 11:54:04 +01002432/* The 'mapping' part of i915_gem_object_pin_map() below */
2433static void *i915_gem_object_map(const struct drm_i915_gem_object *obj)
2434{
2435 unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
2436 struct sg_table *sgt = obj->pages;
Dave Gordon85d12252016-05-20 11:54:06 +01002437 struct sgt_iter sgt_iter;
2438 struct page *page;
Dave Gordonb338fa42016-05-20 11:54:05 +01002439 struct page *stack_pages[32];
2440 struct page **pages = stack_pages;
Dave Gordondd6034c2016-05-20 11:54:04 +01002441 unsigned long i = 0;
2442 void *addr;
2443
2444 /* A single page can always be kmapped */
2445 if (n_pages == 1)
2446 return kmap(sg_page(sgt->sgl));
2447
Dave Gordonb338fa42016-05-20 11:54:05 +01002448 if (n_pages > ARRAY_SIZE(stack_pages)) {
2449 /* Too big for stack -- allocate temporary array instead */
2450 pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
2451 if (!pages)
2452 return NULL;
2453 }
Dave Gordondd6034c2016-05-20 11:54:04 +01002454
Dave Gordon85d12252016-05-20 11:54:06 +01002455 for_each_sgt_page(page, sgt_iter, sgt)
2456 pages[i++] = page;
Dave Gordondd6034c2016-05-20 11:54:04 +01002457
2458 /* Check that we have the expected number of pages */
2459 GEM_BUG_ON(i != n_pages);
2460
2461 addr = vmap(pages, n_pages, 0, PAGE_KERNEL);
2462
Dave Gordonb338fa42016-05-20 11:54:05 +01002463 if (pages != stack_pages)
2464 drm_free_large(pages);
Dave Gordondd6034c2016-05-20 11:54:04 +01002465
2466 return addr;
2467}
2468
2469/* get, pin, and map the pages of the object into kernel space */
Chris Wilson0a798eb2016-04-08 12:11:11 +01002470void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
2471{
2472 int ret;
2473
2474 lockdep_assert_held(&obj->base.dev->struct_mutex);
2475
2476 ret = i915_gem_object_get_pages(obj);
2477 if (ret)
2478 return ERR_PTR(ret);
2479
2480 i915_gem_object_pin_pages(obj);
2481
Dave Gordondd6034c2016-05-20 11:54:04 +01002482 if (!obj->mapping) {
2483 obj->mapping = i915_gem_object_map(obj);
2484 if (!obj->mapping) {
Chris Wilson0a798eb2016-04-08 12:11:11 +01002485 i915_gem_object_unpin_pages(obj);
2486 return ERR_PTR(-ENOMEM);
2487 }
2488 }
2489
2490 return obj->mapping;
2491}
2492
Ben Widawskye2d05a82013-09-24 09:57:58 -07002493void i915_vma_move_to_active(struct i915_vma *vma,
John Harrisonb2af0372015-05-29 17:43:50 +01002494 struct drm_i915_gem_request *req)
Ben Widawskye2d05a82013-09-24 09:57:58 -07002495{
Chris Wilsonb4716182015-04-27 13:41:17 +01002496 struct drm_i915_gem_object *obj = vma->obj;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002497 struct intel_engine_cs *engine;
John Harrisonb2af0372015-05-29 17:43:50 +01002498
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002499 engine = i915_gem_request_get_engine(req);
Chris Wilsonb4716182015-04-27 13:41:17 +01002500
2501 /* Add a reference if we're newly entering the active list. */
2502 if (obj->active == 0)
2503 drm_gem_object_reference(&obj->base);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002504 obj->active |= intel_engine_flag(engine);
Chris Wilsonb4716182015-04-27 13:41:17 +01002505
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002506 list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002507 i915_gem_request_assign(&obj->last_read_req[engine->id], req);
Chris Wilsonb4716182015-04-27 13:41:17 +01002508
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002509 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Ben Widawskye2d05a82013-09-24 09:57:58 -07002510}
2511
Chris Wilsoncaea7472010-11-12 13:53:37 +00002512static void
Chris Wilsonb4716182015-04-27 13:41:17 +01002513i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
2514{
Chris Wilsond501b1d2016-04-13 17:35:02 +01002515 GEM_BUG_ON(obj->last_write_req == NULL);
2516 GEM_BUG_ON(!(obj->active & intel_engine_flag(obj->last_write_req->engine)));
Chris Wilsonb4716182015-04-27 13:41:17 +01002517
2518 i915_gem_request_assign(&obj->last_write_req, NULL);
Rodrigo Vivide152b62015-07-07 16:28:51 -07002519 intel_fb_obj_flush(obj, true, ORIGIN_CS);
Chris Wilsonb4716182015-04-27 13:41:17 +01002520}
2521
2522static void
2523i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
Chris Wilsoncaea7472010-11-12 13:53:37 +00002524{
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002525 struct i915_vma *vma;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002526
Chris Wilsond501b1d2016-04-13 17:35:02 +01002527 GEM_BUG_ON(obj->last_read_req[ring] == NULL);
2528 GEM_BUG_ON(!(obj->active & (1 << ring)));
Chris Wilsonb4716182015-04-27 13:41:17 +01002529
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002530 list_del_init(&obj->engine_list[ring]);
Chris Wilsonb4716182015-04-27 13:41:17 +01002531 i915_gem_request_assign(&obj->last_read_req[ring], NULL);
2532
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002533 if (obj->last_write_req && obj->last_write_req->engine->id == ring)
Chris Wilsonb4716182015-04-27 13:41:17 +01002534 i915_gem_object_retire__write(obj);
2535
2536 obj->active &= ~(1 << ring);
2537 if (obj->active)
2538 return;
Chris Wilson65ce3022012-07-20 12:41:02 +01002539
Chris Wilson6c246952015-07-27 10:26:26 +01002540 /* Bump our place on the bound list to keep it roughly in LRU order
2541 * so that we don't steal from recently used but inactive objects
2542 * (unless we are forced to ofc!)
2543 */
2544 list_move_tail(&obj->global_list,
2545 &to_i915(obj->base.dev)->mm.bound_list);
2546
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002547 list_for_each_entry(vma, &obj->vma_list, obj_link) {
2548 if (!list_empty(&vma->vm_link))
2549 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002550 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00002551
John Harrison97b2a6a2014-11-24 18:49:26 +00002552 i915_gem_request_assign(&obj->last_fenced_req, NULL);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002553 drm_gem_object_unreference(&obj->base);
Chris Wilsonc8725f32014-03-17 12:21:55 +00002554}
2555
Chris Wilson9d7730912012-11-27 16:22:52 +00002556static int
Chris Wilsonc0336662016-05-06 15:40:21 +01002557i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002558{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002559 struct intel_engine_cs *engine;
Chris Wilson29dcb572016-04-07 07:29:13 +01002560 int ret;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002561
Chris Wilson107f27a52012-12-10 13:56:17 +02002562 /* Carefully retire all requests without writing to the rings */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002563 for_each_engine(engine, dev_priv) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002564 ret = intel_engine_idle(engine);
Chris Wilson107f27a52012-12-10 13:56:17 +02002565 if (ret)
2566 return ret;
Chris Wilson9d7730912012-11-27 16:22:52 +00002567 }
Chris Wilsonc0336662016-05-06 15:40:21 +01002568 i915_gem_retire_requests(dev_priv);
Chris Wilson107f27a52012-12-10 13:56:17 +02002569
2570 /* Finally reset hw state */
Chris Wilson29dcb572016-04-07 07:29:13 +01002571 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002572 intel_ring_init_seqno(engine, seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002573
Chris Wilson9d7730912012-11-27 16:22:52 +00002574 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002575}
2576
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002577int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
2578{
2579 struct drm_i915_private *dev_priv = dev->dev_private;
2580 int ret;
2581
2582 if (seqno == 0)
2583 return -EINVAL;
2584
2585 /* HWS page needs to be set less than what we
2586 * will inject to ring
2587 */
Chris Wilsonc0336662016-05-06 15:40:21 +01002588 ret = i915_gem_init_seqno(dev_priv, seqno - 1);
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002589 if (ret)
2590 return ret;
2591
2592 /* Carefully set the last_seqno value so that wrap
2593 * detection still works
2594 */
2595 dev_priv->next_seqno = seqno;
2596 dev_priv->last_seqno = seqno - 1;
2597 if (dev_priv->last_seqno == 0)
2598 dev_priv->last_seqno--;
2599
2600 return 0;
2601}
2602
Chris Wilson9d7730912012-11-27 16:22:52 +00002603int
Chris Wilsonc0336662016-05-06 15:40:21 +01002604i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002605{
Chris Wilson9d7730912012-11-27 16:22:52 +00002606 /* reserve 0 for non-seqno */
2607 if (dev_priv->next_seqno == 0) {
Chris Wilsonc0336662016-05-06 15:40:21 +01002608 int ret = i915_gem_init_seqno(dev_priv, 0);
Chris Wilson9d7730912012-11-27 16:22:52 +00002609 if (ret)
2610 return ret;
2611
2612 dev_priv->next_seqno = 1;
2613 }
2614
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02002615 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
Chris Wilson9d7730912012-11-27 16:22:52 +00002616 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002617}
2618
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002619/*
2620 * NB: This function is not allowed to fail. Doing so would mean the the
2621 * request is not being tracked for completion but the work itself is
2622 * going to happen on the hardware. This would be a Bad Thing(tm).
2623 */
John Harrison75289872015-05-29 17:43:49 +01002624void __i915_add_request(struct drm_i915_gem_request *request,
John Harrison5b4a60c2015-05-29 17:43:34 +01002625 struct drm_i915_gem_object *obj,
2626 bool flush_caches)
Eric Anholt673a3942008-07-30 12:06:12 -07002627{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002628 struct intel_engine_cs *engine;
John Harrison75289872015-05-29 17:43:49 +01002629 struct drm_i915_private *dev_priv;
Oscar Mateo48e29f52014-07-24 17:04:29 +01002630 struct intel_ringbuffer *ringbuf;
Nick Hoath6d3d8272015-01-15 13:10:39 +00002631 u32 request_start;
Chris Wilson0251a962016-04-28 09:56:47 +01002632 u32 reserved_tail;
Chris Wilson3cce4692010-10-27 16:11:02 +01002633 int ret;
2634
Oscar Mateo48e29f52014-07-24 17:04:29 +01002635 if (WARN_ON(request == NULL))
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002636 return;
Oscar Mateo48e29f52014-07-24 17:04:29 +01002637
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002638 engine = request->engine;
Tvrtko Ursulin39dabec2016-03-17 13:04:10 +00002639 dev_priv = request->i915;
John Harrison75289872015-05-29 17:43:49 +01002640 ringbuf = request->ringbuf;
2641
John Harrison29b1b412015-06-18 13:10:09 +01002642 /*
2643 * To ensure that this call will not fail, space for its emissions
2644 * should already have been reserved in the ring buffer. Let the ring
2645 * know that it is time to use that space up.
2646 */
Oscar Mateo48e29f52014-07-24 17:04:29 +01002647 request_start = intel_ring_get_tail(ringbuf);
Chris Wilson0251a962016-04-28 09:56:47 +01002648 reserved_tail = request->reserved_space;
2649 request->reserved_space = 0;
2650
Daniel Vettercc889e02012-06-13 20:45:19 +02002651 /*
2652 * Emit any outstanding flushes - execbuf can fail to emit the flush
2653 * after having emitted the batchbuffer command. Hence we need to fix
2654 * things up similar to emitting the lazy request. The difference here
2655 * is that the flush _must_ happen before the next request, no matter
2656 * what.
2657 */
John Harrison5b4a60c2015-05-29 17:43:34 +01002658 if (flush_caches) {
2659 if (i915.enable_execlists)
John Harrison4866d722015-05-29 17:43:55 +01002660 ret = logical_ring_flush_all_caches(request);
John Harrison5b4a60c2015-05-29 17:43:34 +01002661 else
John Harrison4866d722015-05-29 17:43:55 +01002662 ret = intel_ring_flush_all_caches(request);
John Harrison5b4a60c2015-05-29 17:43:34 +01002663 /* Not allowed to fail! */
2664 WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
2665 }
Daniel Vettercc889e02012-06-13 20:45:19 +02002666
Chris Wilson7c90b7d2016-04-07 07:29:17 +01002667 trace_i915_gem_request_add(request);
2668
2669 request->head = request_start;
2670
2671 /* Whilst this request exists, batch_obj will be on the
2672 * active_list, and so will hold the active reference. Only when this
2673 * request is retired will the the batch_obj be moved onto the
2674 * inactive_list and lose its active reference. Hence we do not need
2675 * to explicitly hold another reference here.
2676 */
2677 request->batch_obj = obj;
2678
2679 /* Seal the request and mark it as pending execution. Note that
2680 * we may inspect this state, without holding any locks, during
2681 * hangcheck. Hence we apply the barrier to ensure that we do not
2682 * see a more recent value in the hws than we are tracking.
2683 */
2684 request->emitted_jiffies = jiffies;
2685 request->previous_seqno = engine->last_submitted_seqno;
2686 smp_store_mb(engine->last_submitted_seqno, request->seqno);
2687 list_add_tail(&request->list, &engine->request_list);
2688
Chris Wilsona71d8d92012-02-15 11:25:36 +00002689 /* Record the position of the start of the request so that
2690 * should we detect the updated seqno part-way through the
2691 * GPU processing the request, we never over-estimate the
2692 * position of the head.
2693 */
Nick Hoath6d3d8272015-01-15 13:10:39 +00002694 request->postfix = intel_ring_get_tail(ringbuf);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002695
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002696 if (i915.enable_execlists)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002697 ret = engine->emit_request(request);
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002698 else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002699 ret = engine->add_request(request);
Michel Thierry53292cd2015-04-15 18:11:33 +01002700
2701 request->tail = intel_ring_get_tail(ringbuf);
Oscar Mateo48e29f52014-07-24 17:04:29 +01002702 }
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002703 /* Not allowed to fail! */
2704 WARN(ret, "emit|add_request failed: %d!\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002705
Chris Wilsonc0336662016-05-06 15:40:21 +01002706 i915_queue_hangcheck(engine->i915);
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03002707
Daniel Vetter87255482014-11-19 20:36:48 +01002708 queue_delayed_work(dev_priv->wq,
2709 &dev_priv->mm.retire_work,
2710 round_jiffies_up_relative(HZ));
Tvrtko Ursulin7d993732016-04-28 12:57:00 +01002711 intel_mark_busy(dev_priv);
Daniel Vettercc889e02012-06-13 20:45:19 +02002712
John Harrison29b1b412015-06-18 13:10:09 +01002713 /* Sanity check that the reserved size was large enough. */
Chris Wilson0251a962016-04-28 09:56:47 +01002714 ret = intel_ring_get_tail(ringbuf) - request_start;
2715 if (ret < 0)
2716 ret += ringbuf->size;
2717 WARN_ONCE(ret > reserved_tail,
2718 "Not enough space reserved (%d bytes) "
2719 "for adding the request (%d bytes)\n",
2720 reserved_tail, ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002721}
2722
Mika Kuoppala939fd762014-01-30 19:04:44 +02002723static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
Chris Wilsone2efd132016-05-24 14:53:34 +01002724 const struct i915_gem_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002725{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002726 unsigned long elapsed;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002727
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002728 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
2729
2730 if (ctx->hang_stats.banned)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002731 return true;
2732
Chris Wilson676fa572014-12-24 08:13:39 -08002733 if (ctx->hang_stats.ban_period_seconds &&
2734 elapsed <= ctx->hang_stats.ban_period_seconds) {
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002735 if (!i915_gem_context_is_default(ctx)) {
Mika Kuoppala3fac8972014-01-30 16:05:48 +02002736 DRM_DEBUG("context hanging too fast, banning!\n");
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002737 return true;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +02002738 } else if (i915_stop_ring_allow_ban(dev_priv)) {
2739 if (i915_stop_ring_allow_warn(dev_priv))
2740 DRM_ERROR("gpu hanging too fast, banning!\n");
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002741 return true;
Mika Kuoppala3fac8972014-01-30 16:05:48 +02002742 }
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002743 }
2744
2745 return false;
2746}
2747
Mika Kuoppala939fd762014-01-30 19:04:44 +02002748static void i915_set_reset_status(struct drm_i915_private *dev_priv,
Chris Wilsone2efd132016-05-24 14:53:34 +01002749 struct i915_gem_context *ctx,
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002750 const bool guilty)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002751{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002752 struct i915_ctx_hang_stats *hs;
2753
2754 if (WARN_ON(!ctx))
2755 return;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002756
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002757 hs = &ctx->hang_stats;
2758
2759 if (guilty) {
Mika Kuoppala939fd762014-01-30 19:04:44 +02002760 hs->banned = i915_context_is_banned(dev_priv, ctx);
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002761 hs->batch_active++;
2762 hs->guilty_ts = get_seconds();
2763 } else {
2764 hs->batch_pending++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002765 }
2766}
2767
John Harrisonabfe2622014-11-24 18:49:24 +00002768void i915_gem_request_free(struct kref *req_ref)
2769{
2770 struct drm_i915_gem_request *req = container_of(req_ref,
2771 typeof(*req), ref);
Chris Wilsonefab6d82015-04-07 16:20:57 +01002772 kmem_cache_free(req->i915->requests, req);
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002773}
2774
Dave Gordon26827082016-01-19 19:02:53 +00002775static inline int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002776__i915_gem_request_alloc(struct intel_engine_cs *engine,
Chris Wilsone2efd132016-05-24 14:53:34 +01002777 struct i915_gem_context *ctx,
Dave Gordon26827082016-01-19 19:02:53 +00002778 struct drm_i915_gem_request **req_out)
John Harrison6689cb22015-03-19 12:30:08 +00002779{
Chris Wilsonc0336662016-05-06 15:40:21 +01002780 struct drm_i915_private *dev_priv = engine->i915;
Chris Wilson299259a2016-04-13 17:35:06 +01002781 unsigned reset_counter = i915_reset_counter(&dev_priv->gpu_error);
Daniel Vettereed29a52015-05-21 14:21:25 +02002782 struct drm_i915_gem_request *req;
John Harrison6689cb22015-03-19 12:30:08 +00002783 int ret;
John Harrison6689cb22015-03-19 12:30:08 +00002784
John Harrison217e46b2015-05-29 17:43:29 +01002785 if (!req_out)
2786 return -EINVAL;
2787
John Harrisonbccca492015-05-29 17:44:11 +01002788 *req_out = NULL;
John Harrison6689cb22015-03-19 12:30:08 +00002789
Chris Wilsonf4457ae2016-04-13 17:35:08 +01002790 /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
2791 * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
2792 * and restart.
2793 */
2794 ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible);
Chris Wilson299259a2016-04-13 17:35:06 +01002795 if (ret)
2796 return ret;
2797
Daniel Vettereed29a52015-05-21 14:21:25 +02002798 req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
2799 if (req == NULL)
John Harrison6689cb22015-03-19 12:30:08 +00002800 return -ENOMEM;
2801
Chris Wilsonc0336662016-05-06 15:40:21 +01002802 ret = i915_gem_get_seqno(engine->i915, &req->seqno);
Chris Wilson9a0c1e22015-05-21 21:01:45 +01002803 if (ret)
2804 goto err;
John Harrison6689cb22015-03-19 12:30:08 +00002805
John Harrison40e895c2015-05-29 17:43:26 +01002806 kref_init(&req->ref);
2807 req->i915 = dev_priv;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002808 req->engine = engine;
Chris Wilson299259a2016-04-13 17:35:06 +01002809 req->reset_counter = reset_counter;
John Harrison40e895c2015-05-29 17:43:26 +01002810 req->ctx = ctx;
2811 i915_gem_context_reference(req->ctx);
John Harrison6689cb22015-03-19 12:30:08 +00002812
John Harrison29b1b412015-06-18 13:10:09 +01002813 /*
2814 * Reserve space in the ring buffer for all the commands required to
2815 * eventually emit this request. This is to guarantee that the
2816 * i915_add_request() call can't fail. Note that the reserve may need
2817 * to be redone if the request is not actually submitted straight
2818 * away, e.g. because a GPU scheduler has deferred it.
John Harrison29b1b412015-06-18 13:10:09 +01002819 */
Chris Wilson0251a962016-04-28 09:56:47 +01002820 req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
Chris Wilsonbfa01202016-04-28 09:56:48 +01002821
2822 if (i915.enable_execlists)
2823 ret = intel_logical_ring_alloc_request_extras(req);
2824 else
2825 ret = intel_ring_alloc_request_extras(req);
2826 if (ret)
2827 goto err_ctx;
John Harrison29b1b412015-06-18 13:10:09 +01002828
John Harrisonbccca492015-05-29 17:44:11 +01002829 *req_out = req;
John Harrison6689cb22015-03-19 12:30:08 +00002830 return 0;
Chris Wilson9a0c1e22015-05-21 21:01:45 +01002831
Chris Wilsonbfa01202016-04-28 09:56:48 +01002832err_ctx:
2833 i915_gem_context_unreference(ctx);
Chris Wilson9a0c1e22015-05-21 21:01:45 +01002834err:
2835 kmem_cache_free(dev_priv->requests, req);
2836 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002837}
2838
Dave Gordon26827082016-01-19 19:02:53 +00002839/**
2840 * i915_gem_request_alloc - allocate a request structure
2841 *
2842 * @engine: engine that we wish to issue the request on.
2843 * @ctx: context that the request will be associated with.
2844 * This can be NULL if the request is not directly related to
2845 * any specific user context, in which case this function will
2846 * choose an appropriate context to use.
2847 *
2848 * Returns a pointer to the allocated request if successful,
2849 * or an error code if not.
2850 */
2851struct drm_i915_gem_request *
2852i915_gem_request_alloc(struct intel_engine_cs *engine,
Chris Wilsone2efd132016-05-24 14:53:34 +01002853 struct i915_gem_context *ctx)
Dave Gordon26827082016-01-19 19:02:53 +00002854{
2855 struct drm_i915_gem_request *req;
2856 int err;
2857
2858 if (ctx == NULL)
Chris Wilsonc0336662016-05-06 15:40:21 +01002859 ctx = engine->i915->kernel_context;
Dave Gordon26827082016-01-19 19:02:53 +00002860 err = __i915_gem_request_alloc(engine, ctx, &req);
2861 return err ? ERR_PTR(err) : req;
2862}
2863
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002864struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002865i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002866{
Chris Wilson4db080f2013-12-04 11:37:09 +00002867 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002868
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002869 list_for_each_entry(request, &engine->request_list, list) {
John Harrison1b5a4332014-11-24 18:49:42 +00002870 if (i915_gem_request_completed(request, false))
Chris Wilson4db080f2013-12-04 11:37:09 +00002871 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002872
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002873 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002874 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002875
2876 return NULL;
2877}
2878
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002879static void i915_gem_reset_engine_status(struct drm_i915_private *dev_priv,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002880 struct intel_engine_cs *engine)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002881{
2882 struct drm_i915_gem_request *request;
2883 bool ring_hung;
2884
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002885 request = i915_gem_find_active_request(engine);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002886
2887 if (request == NULL)
2888 return;
2889
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002890 ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002891
Mika Kuoppala939fd762014-01-30 19:04:44 +02002892 i915_set_reset_status(dev_priv, request->ctx, ring_hung);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002893
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002894 list_for_each_entry_continue(request, &engine->request_list, list)
Mika Kuoppala939fd762014-01-30 19:04:44 +02002895 i915_set_reset_status(dev_priv, request->ctx, false);
Chris Wilson4db080f2013-12-04 11:37:09 +00002896}
2897
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002898static void i915_gem_reset_engine_cleanup(struct drm_i915_private *dev_priv,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002899 struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002900{
Chris Wilson608c1a52015-09-03 13:01:40 +01002901 struct intel_ringbuffer *buffer;
2902
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002903 while (!list_empty(&engine->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002904 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002905
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002906 obj = list_first_entry(&engine->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00002907 struct drm_i915_gem_object,
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002908 engine_list[engine->id]);
Eric Anholt673a3942008-07-30 12:06:12 -07002909
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002910 i915_gem_object_retire__read(obj, engine->id);
Eric Anholt673a3942008-07-30 12:06:12 -07002911 }
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002912
2913 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002914 * Clear the execlists queue up before freeing the requests, as those
2915 * are the ones that keep the context and ringbuffer backing objects
2916 * pinned in place.
2917 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002918
Tomas Elf7de16912015-10-19 16:32:32 +01002919 if (i915.enable_execlists) {
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002920 /* Ensure irq handler finishes or is cancelled. */
2921 tasklet_kill(&engine->irq_tasklet);
Mika Kuoppala1197b4f2015-01-13 11:32:24 +02002922
Tvrtko Ursuline39d42f2016-04-28 09:56:58 +01002923 intel_execlists_cancel_requests(engine);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002924 }
2925
2926 /*
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002927 * We must free the requests after all the corresponding objects have
2928 * been moved off active lists. Which is the same order as the normal
2929 * retire_requests function does. This is important if object hold
2930 * implicit references on things like e.g. ppgtt address spaces through
2931 * the request.
2932 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002933 while (!list_empty(&engine->request_list)) {
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002934 struct drm_i915_gem_request *request;
2935
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002936 request = list_first_entry(&engine->request_list,
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002937 struct drm_i915_gem_request,
2938 list);
2939
Chris Wilsonb4716182015-04-27 13:41:17 +01002940 i915_gem_request_retire(request);
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002941 }
Chris Wilson608c1a52015-09-03 13:01:40 +01002942
2943 /* Having flushed all requests from all queues, we know that all
2944 * ringbuffers must now be empty. However, since we do not reclaim
2945 * all space when retiring the request (to prevent HEADs colliding
2946 * with rapid ringbuffer wraparound) the amount of available space
2947 * upon reset is less than when we start. Do one more pass over
2948 * all the ringbuffers to reset last_retired_head.
2949 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002950 list_for_each_entry(buffer, &engine->buffers, link) {
Chris Wilson608c1a52015-09-03 13:01:40 +01002951 buffer->last_retired_head = buffer->tail;
2952 intel_ring_update_space(buffer);
2953 }
Chris Wilson2ed53a92016-04-07 07:29:11 +01002954
2955 intel_ring_init_seqno(engine, engine->last_submitted_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002956}
2957
Chris Wilson069efc12010-09-30 16:53:18 +01002958void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002959{
Chris Wilsondfaae392010-09-22 10:31:52 +01002960 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002961 struct intel_engine_cs *engine;
Eric Anholt673a3942008-07-30 12:06:12 -07002962
Chris Wilson4db080f2013-12-04 11:37:09 +00002963 /*
2964 * Before we free the objects from the requests, we need to inspect
2965 * them for finding the guilty party. As the requests only borrow
2966 * their reference to the objects, the inspection must be done first.
2967 */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002968 for_each_engine(engine, dev_priv)
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002969 i915_gem_reset_engine_status(dev_priv, engine);
Chris Wilson4db080f2013-12-04 11:37:09 +00002970
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002971 for_each_engine(engine, dev_priv)
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002972 i915_gem_reset_engine_cleanup(dev_priv, engine);
Chris Wilsondfaae392010-09-22 10:31:52 +01002973
Ben Widawskyacce9ff2013-12-06 14:11:03 -08002974 i915_gem_context_reset(dev);
2975
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002976 i915_gem_restore_fences(dev);
Chris Wilsonb4716182015-04-27 13:41:17 +01002977
2978 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002979}
2980
2981/**
2982 * This function clears the request list as sequence numbers are passed.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01002983 * @engine: engine to retire requests on
Eric Anholt673a3942008-07-30 12:06:12 -07002984 */
Chris Wilson1cf0ba12014-05-05 09:07:33 +01002985void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002986i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
Eric Anholt673a3942008-07-30 12:06:12 -07002987{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002988 WARN_ON(i915_verify_lists(engine->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002989
Chris Wilson832a3aa2015-03-18 18:19:22 +00002990 /* Retire requests first as we use it above for the early return.
2991 * If we retire requests last, we may use a later seqno and so clear
2992 * the requests lists without clearing the active list, leading to
2993 * confusion.
Chris Wilsone9103032014-01-07 11:45:14 +00002994 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002995 while (!list_empty(&engine->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002996 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002997
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002998 request = list_first_entry(&engine->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002999 struct drm_i915_gem_request,
3000 list);
Eric Anholt673a3942008-07-30 12:06:12 -07003001
John Harrison1b5a4332014-11-24 18:49:42 +00003002 if (!i915_gem_request_completed(request, true))
Eric Anholt673a3942008-07-30 12:06:12 -07003003 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01003004
Chris Wilsonb4716182015-04-27 13:41:17 +01003005 i915_gem_request_retire(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01003006 }
3007
Chris Wilson832a3aa2015-03-18 18:19:22 +00003008 /* Move any buffers on the active list that are no longer referenced
3009 * by the ringbuffer to the flushing/inactive lists as appropriate,
3010 * before we free the context associated with the requests.
3011 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003012 while (!list_empty(&engine->active_list)) {
Chris Wilson832a3aa2015-03-18 18:19:22 +00003013 struct drm_i915_gem_object *obj;
3014
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003015 obj = list_first_entry(&engine->active_list,
3016 struct drm_i915_gem_object,
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00003017 engine_list[engine->id]);
Chris Wilson832a3aa2015-03-18 18:19:22 +00003018
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003019 if (!list_empty(&obj->last_read_req[engine->id]->list))
Chris Wilson832a3aa2015-03-18 18:19:22 +00003020 break;
3021
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003022 i915_gem_object_retire__read(obj, engine->id);
Chris Wilson832a3aa2015-03-18 18:19:22 +00003023 }
3024
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003025 if (unlikely(engine->trace_irq_req &&
3026 i915_gem_request_completed(engine->trace_irq_req, true))) {
3027 engine->irq_put(engine);
3028 i915_gem_request_assign(&engine->trace_irq_req, NULL);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01003029 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003030
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003031 WARN_ON(i915_verify_lists(engine->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003032}
3033
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003034bool
Chris Wilsonc0336662016-05-06 15:40:21 +01003035i915_gem_retire_requests(struct drm_i915_private *dev_priv)
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01003036{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003037 struct intel_engine_cs *engine;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003038 bool idle = true;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01003039
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003040 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003041 i915_gem_retire_requests_ring(engine);
3042 idle &= list_empty(&engine->request_list);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00003043 if (i915.enable_execlists) {
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01003044 spin_lock_bh(&engine->execlist_lock);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003045 idle &= list_empty(&engine->execlist_queue);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01003046 spin_unlock_bh(&engine->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00003047 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003048 }
3049
3050 if (idle)
3051 mod_delayed_work(dev_priv->wq,
3052 &dev_priv->mm.idle_work,
3053 msecs_to_jiffies(100));
3054
3055 return idle;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01003056}
3057
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003058static void
Eric Anholt673a3942008-07-30 12:06:12 -07003059i915_gem_retire_work_handler(struct work_struct *work)
3060{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003061 struct drm_i915_private *dev_priv =
3062 container_of(work, typeof(*dev_priv), mm.retire_work.work);
3063 struct drm_device *dev = dev_priv->dev;
Chris Wilson0a587052011-01-09 21:05:44 +00003064 bool idle;
Eric Anholt673a3942008-07-30 12:06:12 -07003065
Chris Wilson891b48c2010-09-29 12:26:37 +01003066 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003067 idle = false;
3068 if (mutex_trylock(&dev->struct_mutex)) {
Chris Wilsonc0336662016-05-06 15:40:21 +01003069 idle = i915_gem_retire_requests(dev_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003070 mutex_unlock(&dev->struct_mutex);
3071 }
3072 if (!idle)
Chris Wilsonbcb45082012-10-05 17:02:57 +01003073 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
3074 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003075}
Chris Wilson891b48c2010-09-29 12:26:37 +01003076
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003077static void
3078i915_gem_idle_work_handler(struct work_struct *work)
3079{
3080 struct drm_i915_private *dev_priv =
3081 container_of(work, typeof(*dev_priv), mm.idle_work.work);
Chris Wilson35c94182015-04-07 16:20:37 +01003082 struct drm_device *dev = dev_priv->dev;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003083 struct intel_engine_cs *engine;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003084
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003085 for_each_engine(engine, dev_priv)
3086 if (!list_empty(&engine->request_list))
Chris Wilson423795c2015-04-07 16:21:08 +01003087 return;
Zou Nan hai852835f2010-05-21 09:08:56 +08003088
Daniel Vetter30ecad72015-12-09 09:29:36 +01003089 /* we probably should sync with hangcheck here, using cancel_work_sync.
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003090 * Also locking seems to be fubar here, engine->request_list is protected
Daniel Vetter30ecad72015-12-09 09:29:36 +01003091 * by dev->struct_mutex. */
3092
Tvrtko Ursulin7d993732016-04-28 12:57:00 +01003093 intel_mark_idle(dev_priv);
Chris Wilson35c94182015-04-07 16:20:37 +01003094
3095 if (mutex_trylock(&dev->struct_mutex)) {
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003096 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003097 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson35c94182015-04-07 16:20:37 +01003098
3099 mutex_unlock(&dev->struct_mutex);
3100 }
Eric Anholt673a3942008-07-30 12:06:12 -07003101}
3102
Ben Widawsky5816d642012-04-11 11:18:19 -07003103/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003104 * Ensures that an object will eventually get non-busy by flushing any required
3105 * write domains, emitting any outstanding lazy request and retiring and
3106 * completed requests.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003107 * @obj: object to flush
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003108 */
3109static int
3110i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
3111{
John Harrisona5ac0f92015-05-29 17:44:15 +01003112 int i;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003113
Chris Wilsonb4716182015-04-27 13:41:17 +01003114 if (!obj->active)
3115 return 0;
John Harrison41c52412014-11-24 18:49:43 +00003116
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003117 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01003118 struct drm_i915_gem_request *req;
3119
3120 req = obj->last_read_req[i];
3121 if (req == NULL)
3122 continue;
3123
Chris Wilsone6db7462016-05-13 11:57:21 +01003124 if (i915_gem_request_completed(req, true))
Chris Wilsonb4716182015-04-27 13:41:17 +01003125 i915_gem_object_retire__read(obj, i);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003126 }
3127
3128 return 0;
3129}
3130
3131/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003132 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003133 * @dev: drm device pointer
3134 * @data: ioctl data blob
3135 * @file: drm file pointer
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003136 *
3137 * Returns 0 if successful, else an error is returned with the remaining time in
3138 * the timeout parameter.
3139 * -ETIME: object is still busy after timeout
3140 * -ERESTARTSYS: signal interrupted the wait
3141 * -ENONENT: object doesn't exist
3142 * Also possible, but rare:
3143 * -EAGAIN: GPU wedged
3144 * -ENOMEM: damn
3145 * -ENODEV: Internal IRQ fail
3146 * -E?: The add request failed
3147 *
3148 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3149 * non-zero timeout parameter the wait ioctl will wait for the given number of
3150 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3151 * without holding struct_mutex the object may become re-busied before this
3152 * function completes. A similar but shorter * race condition exists in the busy
3153 * ioctl
3154 */
3155int
3156i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3157{
3158 struct drm_i915_gem_wait *args = data;
3159 struct drm_i915_gem_object *obj;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003160 struct drm_i915_gem_request *req[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01003161 int i, n = 0;
3162 int ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003163
Daniel Vetter11b5d512014-09-29 15:31:26 +02003164 if (args->flags != 0)
3165 return -EINVAL;
3166
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003167 ret = i915_mutex_lock_interruptible(dev);
3168 if (ret)
3169 return ret;
3170
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01003171 obj = to_intel_bo(drm_gem_object_lookup(file, args->bo_handle));
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003172 if (&obj->base == NULL) {
3173 mutex_unlock(&dev->struct_mutex);
3174 return -ENOENT;
3175 }
3176
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003177 /* Need to make sure the object gets inactive eventually. */
3178 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003179 if (ret)
3180 goto out;
3181
Chris Wilsonb4716182015-04-27 13:41:17 +01003182 if (!obj->active)
John Harrison97b2a6a2014-11-24 18:49:26 +00003183 goto out;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003184
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003185 /* Do this after OLR check to make sure we make forward progress polling
Chris Wilson762e4582015-03-04 18:09:26 +00003186 * on this IOCTL with a timeout == 0 (like busy ioctl)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003187 */
Chris Wilson762e4582015-03-04 18:09:26 +00003188 if (args->timeout_ns == 0) {
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003189 ret = -ETIME;
3190 goto out;
3191 }
3192
3193 drm_gem_object_unreference(&obj->base);
Chris Wilsonb4716182015-04-27 13:41:17 +01003194
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003195 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01003196 if (obj->last_read_req[i] == NULL)
3197 continue;
3198
3199 req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
3200 }
3201
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003202 mutex_unlock(&dev->struct_mutex);
3203
Chris Wilsonb4716182015-04-27 13:41:17 +01003204 for (i = 0; i < n; i++) {
3205 if (ret == 0)
Chris Wilson299259a2016-04-13 17:35:06 +01003206 ret = __i915_wait_request(req[i], true,
Chris Wilsonb4716182015-04-27 13:41:17 +01003207 args->timeout_ns > 0 ? &args->timeout_ns : NULL,
Chris Wilsonb6aa0872015-12-02 09:13:46 +00003208 to_rps_client(file));
Chris Wilson73db04c2016-04-28 09:56:55 +01003209 i915_gem_request_unreference(req[i]);
Chris Wilsonb4716182015-04-27 13:41:17 +01003210 }
John Harrisonff865882014-11-24 18:49:28 +00003211 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003212
3213out:
3214 drm_gem_object_unreference(&obj->base);
3215 mutex_unlock(&dev->struct_mutex);
3216 return ret;
3217}
3218
Chris Wilsonb4716182015-04-27 13:41:17 +01003219static int
3220__i915_gem_object_sync(struct drm_i915_gem_object *obj,
3221 struct intel_engine_cs *to,
John Harrison91af1272015-06-18 13:14:56 +01003222 struct drm_i915_gem_request *from_req,
3223 struct drm_i915_gem_request **to_req)
Chris Wilsonb4716182015-04-27 13:41:17 +01003224{
3225 struct intel_engine_cs *from;
3226 int ret;
3227
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003228 from = i915_gem_request_get_engine(from_req);
Chris Wilsonb4716182015-04-27 13:41:17 +01003229 if (to == from)
3230 return 0;
3231
John Harrison91af1272015-06-18 13:14:56 +01003232 if (i915_gem_request_completed(from_req, true))
Chris Wilsonb4716182015-04-27 13:41:17 +01003233 return 0;
3234
Chris Wilsonc0336662016-05-06 15:40:21 +01003235 if (!i915_semaphore_is_enabled(to_i915(obj->base.dev))) {
Chris Wilsona6f766f2015-04-27 13:41:20 +01003236 struct drm_i915_private *i915 = to_i915(obj->base.dev);
John Harrison91af1272015-06-18 13:14:56 +01003237 ret = __i915_wait_request(from_req,
Chris Wilsona6f766f2015-04-27 13:41:20 +01003238 i915->mm.interruptible,
3239 NULL,
3240 &i915->rps.semaphores);
Chris Wilsonb4716182015-04-27 13:41:17 +01003241 if (ret)
3242 return ret;
3243
John Harrison91af1272015-06-18 13:14:56 +01003244 i915_gem_object_retire_request(obj, from_req);
Chris Wilsonb4716182015-04-27 13:41:17 +01003245 } else {
3246 int idx = intel_ring_sync_index(from, to);
John Harrison91af1272015-06-18 13:14:56 +01003247 u32 seqno = i915_gem_request_get_seqno(from_req);
3248
3249 WARN_ON(!to_req);
Chris Wilsonb4716182015-04-27 13:41:17 +01003250
3251 if (seqno <= from->semaphore.sync_seqno[idx])
3252 return 0;
3253
John Harrison91af1272015-06-18 13:14:56 +01003254 if (*to_req == NULL) {
Dave Gordon26827082016-01-19 19:02:53 +00003255 struct drm_i915_gem_request *req;
3256
3257 req = i915_gem_request_alloc(to, NULL);
3258 if (IS_ERR(req))
3259 return PTR_ERR(req);
3260
3261 *to_req = req;
John Harrison91af1272015-06-18 13:14:56 +01003262 }
3263
John Harrison599d9242015-05-29 17:44:04 +01003264 trace_i915_gem_ring_sync_to(*to_req, from, from_req);
3265 ret = to->semaphore.sync_to(*to_req, from, seqno);
Chris Wilsonb4716182015-04-27 13:41:17 +01003266 if (ret)
3267 return ret;
3268
3269 /* We use last_read_req because sync_to()
3270 * might have just caused seqno wrap under
3271 * the radar.
3272 */
3273 from->semaphore.sync_seqno[idx] =
3274 i915_gem_request_get_seqno(obj->last_read_req[from->id]);
3275 }
3276
3277 return 0;
3278}
3279
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003280/**
Ben Widawsky5816d642012-04-11 11:18:19 -07003281 * i915_gem_object_sync - sync an object to a ring.
3282 *
3283 * @obj: object which may be in use on another ring.
3284 * @to: ring we wish to use the object on. May be NULL.
John Harrison91af1272015-06-18 13:14:56 +01003285 * @to_req: request we wish to use the object for. See below.
3286 * This will be allocated and returned if a request is
3287 * required but not passed in.
Ben Widawsky5816d642012-04-11 11:18:19 -07003288 *
3289 * This code is meant to abstract object synchronization with the GPU.
3290 * Calling with NULL implies synchronizing the object with the CPU
Chris Wilsonb4716182015-04-27 13:41:17 +01003291 * rather than a particular GPU ring. Conceptually we serialise writes
John Harrison91af1272015-06-18 13:14:56 +01003292 * between engines inside the GPU. We only allow one engine to write
Chris Wilsonb4716182015-04-27 13:41:17 +01003293 * into a buffer at any time, but multiple readers. To ensure each has
3294 * a coherent view of memory, we must:
3295 *
3296 * - If there is an outstanding write request to the object, the new
3297 * request must wait for it to complete (either CPU or in hw, requests
3298 * on the same ring will be naturally ordered).
3299 *
3300 * - If we are a write request (pending_write_domain is set), the new
3301 * request must wait for outstanding read requests to complete.
Ben Widawsky5816d642012-04-11 11:18:19 -07003302 *
John Harrison91af1272015-06-18 13:14:56 +01003303 * For CPU synchronisation (NULL to) no request is required. For syncing with
3304 * rings to_req must be non-NULL. However, a request does not have to be
3305 * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
3306 * request will be allocated automatically and returned through *to_req. Note
3307 * that it is not guaranteed that commands will be emitted (because the system
3308 * might already be idle). Hence there is no need to create a request that
3309 * might never have any work submitted. Note further that if a request is
3310 * returned in *to_req, it is the responsibility of the caller to submit
3311 * that request (after potentially adding more work to it).
3312 *
Ben Widawsky5816d642012-04-11 11:18:19 -07003313 * Returns 0 if successful, else propagates up the lower layer error.
3314 */
Ben Widawsky2911a352012-04-05 14:47:36 -07003315int
3316i915_gem_object_sync(struct drm_i915_gem_object *obj,
John Harrison91af1272015-06-18 13:14:56 +01003317 struct intel_engine_cs *to,
3318 struct drm_i915_gem_request **to_req)
Ben Widawsky2911a352012-04-05 14:47:36 -07003319{
Chris Wilsonb4716182015-04-27 13:41:17 +01003320 const bool readonly = obj->base.pending_write_domain == 0;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003321 struct drm_i915_gem_request *req[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01003322 int ret, i, n;
Ben Widawsky2911a352012-04-05 14:47:36 -07003323
Chris Wilsonb4716182015-04-27 13:41:17 +01003324 if (!obj->active)
Ben Widawsky2911a352012-04-05 14:47:36 -07003325 return 0;
3326
Chris Wilsonb4716182015-04-27 13:41:17 +01003327 if (to == NULL)
3328 return i915_gem_object_wait_rendering(obj, readonly);
Ben Widawsky2911a352012-04-05 14:47:36 -07003329
Chris Wilsonb4716182015-04-27 13:41:17 +01003330 n = 0;
3331 if (readonly) {
3332 if (obj->last_write_req)
3333 req[n++] = obj->last_write_req;
3334 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003335 for (i = 0; i < I915_NUM_ENGINES; i++)
Chris Wilsonb4716182015-04-27 13:41:17 +01003336 if (obj->last_read_req[i])
3337 req[n++] = obj->last_read_req[i];
3338 }
3339 for (i = 0; i < n; i++) {
John Harrison91af1272015-06-18 13:14:56 +01003340 ret = __i915_gem_object_sync(obj, to, req[i], to_req);
Chris Wilsonb4716182015-04-27 13:41:17 +01003341 if (ret)
3342 return ret;
3343 }
Ben Widawsky2911a352012-04-05 14:47:36 -07003344
Chris Wilsonb4716182015-04-27 13:41:17 +01003345 return 0;
Ben Widawsky2911a352012-04-05 14:47:36 -07003346}
3347
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003348static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
3349{
3350 u32 old_write_domain, old_read_domains;
3351
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003352 /* Force a pagefault for domain tracking on next user access */
3353 i915_gem_release_mmap(obj);
3354
Keith Packardb97c3d92011-06-24 21:02:59 -07003355 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3356 return;
3357
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003358 old_read_domains = obj->base.read_domains;
3359 old_write_domain = obj->base.write_domain;
3360
3361 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
3362 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
3363
3364 trace_i915_gem_object_change_domain(obj,
3365 old_read_domains,
3366 old_write_domain);
3367}
3368
Chris Wilson8ef85612016-04-28 09:56:39 +01003369static void __i915_vma_iounmap(struct i915_vma *vma)
3370{
3371 GEM_BUG_ON(vma->pin_count);
3372
3373 if (vma->iomap == NULL)
3374 return;
3375
3376 io_mapping_unmap(vma->iomap);
3377 vma->iomap = NULL;
3378}
3379
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003380static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
Eric Anholt673a3942008-07-30 12:06:12 -07003381{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003382 struct drm_i915_gem_object *obj = vma->obj;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003383 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson43e28f02013-01-08 10:53:09 +00003384 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003385
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003386 if (list_empty(&vma->obj_link))
Eric Anholt673a3942008-07-30 12:06:12 -07003387 return 0;
3388
Daniel Vetter0ff501c2013-08-29 19:50:31 +02003389 if (!drm_mm_node_allocated(&vma->node)) {
3390 i915_gem_vma_destroy(vma);
Daniel Vetter0ff501c2013-08-29 19:50:31 +02003391 return 0;
3392 }
Ben Widawsky433544b2013-08-13 18:09:06 -07003393
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003394 if (vma->pin_count)
Chris Wilson31d8d652012-05-24 19:11:20 +01003395 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07003396
Chris Wilsonc4670ad2012-08-20 10:23:27 +01003397 BUG_ON(obj->pages == NULL);
3398
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003399 if (wait) {
3400 ret = i915_gem_object_wait_rendering(obj, false);
3401 if (ret)
3402 return ret;
3403 }
Chris Wilsona8198ee2011-04-13 22:04:09 +01003404
Chris Wilson596c5922016-02-26 11:03:20 +00003405 if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003406 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01003407
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003408 /* release the fence reg _after_ flushing */
3409 ret = i915_gem_object_put_fence(obj);
3410 if (ret)
3411 return ret;
Chris Wilson8ef85612016-04-28 09:56:39 +01003412
3413 __i915_vma_iounmap(vma);
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003414 }
Daniel Vetter96b47b62009-12-15 17:50:00 +01003415
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003416 trace_i915_vma_unbind(vma);
Chris Wilsondb53a302011-02-03 11:57:46 +00003417
Daniel Vetter777dc5b2015-04-14 17:35:12 +02003418 vma->vm->unbind_vma(vma);
Mika Kuoppala5e562f12015-04-30 11:02:31 +03003419 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003420
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003421 list_del_init(&vma->vm_link);
Chris Wilson596c5922016-02-26 11:03:20 +00003422 if (vma->is_ggtt) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003423 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
3424 obj->map_and_fenceable = false;
3425 } else if (vma->ggtt_view.pages) {
3426 sg_free_table(vma->ggtt_view.pages);
3427 kfree(vma->ggtt_view.pages);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003428 }
Chris Wilson016a65a2015-06-11 08:06:08 +01003429 vma->ggtt_view.pages = NULL;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003430 }
Eric Anholt673a3942008-07-30 12:06:12 -07003431
Ben Widawsky2f633152013-07-17 12:19:03 -07003432 drm_mm_remove_node(&vma->node);
3433 i915_gem_vma_destroy(vma);
3434
3435 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02003436 * no more VMAs exist. */
Imre Deake2273302015-07-09 12:59:05 +03003437 if (list_empty(&obj->vma_list))
Ben Widawsky2f633152013-07-17 12:19:03 -07003438 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003439
Chris Wilson70903c32013-12-04 09:59:09 +00003440 /* And finally now the object is completely decoupled from this vma,
3441 * we can drop its hold on the backing storage and allow it to be
3442 * reaped by the shrinker.
3443 */
3444 i915_gem_object_unpin_pages(obj);
3445
Chris Wilson88241782011-01-07 17:09:48 +00003446 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00003447}
3448
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003449int i915_vma_unbind(struct i915_vma *vma)
3450{
3451 return __i915_vma_unbind(vma, true);
3452}
3453
3454int __i915_vma_unbind_no_wait(struct i915_vma *vma)
3455{
3456 return __i915_vma_unbind(vma, false);
3457}
3458
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003459int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003460{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003461 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003462 struct intel_engine_cs *engine;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003463 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003464
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003465 /* Flush everything onto the inactive list. */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003466 for_each_engine(engine, dev_priv) {
Thomas Danielecdb5fd2014-08-20 16:29:24 +01003467 if (!i915.enable_execlists) {
John Harrison73cfa862015-05-29 17:43:35 +01003468 struct drm_i915_gem_request *req;
3469
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003470 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +00003471 if (IS_ERR(req))
3472 return PTR_ERR(req);
John Harrison73cfa862015-05-29 17:43:35 +01003473
John Harrisonba01cc92015-05-29 17:43:41 +01003474 ret = i915_switch_context(req);
John Harrison75289872015-05-29 17:43:49 +01003475 i915_add_request_no_flush(req);
Chris Wilsonaa9b7812016-04-13 17:35:15 +01003476 if (ret)
3477 return ret;
Thomas Danielecdb5fd2014-08-20 16:29:24 +01003478 }
Ben Widawskyb6c74882012-08-14 14:35:14 -07003479
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003480 ret = intel_engine_idle(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003481 if (ret)
3482 return ret;
3483 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003484
Chris Wilsonb4716182015-04-27 13:41:17 +01003485 WARN_ON(i915_verify_lists(dev));
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003486 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003487}
3488
Chris Wilson4144f9b2014-09-11 08:43:48 +01003489static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003490 unsigned long cache_level)
3491{
Chris Wilson4144f9b2014-09-11 08:43:48 +01003492 struct drm_mm_node *gtt_space = &vma->node;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003493 struct drm_mm_node *other;
3494
Chris Wilson4144f9b2014-09-11 08:43:48 +01003495 /*
3496 * On some machines we have to be careful when putting differing types
3497 * of snoopable memory together to avoid the prefetcher crossing memory
3498 * domains and dying. During vm initialisation, we decide whether or not
3499 * these constraints apply and set the drm_mm.color_adjust
3500 * appropriately.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003501 */
Chris Wilson4144f9b2014-09-11 08:43:48 +01003502 if (vma->vm->mm.color_adjust == NULL)
Chris Wilson42d6ab42012-07-26 11:49:32 +01003503 return true;
3504
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003505 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003506 return true;
3507
3508 if (list_empty(&gtt_space->node_list))
3509 return true;
3510
3511 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3512 if (other->allocated && !other->hole_follows && other->color != cache_level)
3513 return false;
3514
3515 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3516 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3517 return false;
3518
3519 return true;
3520}
3521
Jesse Barnesde151cf2008-11-12 10:03:55 -08003522/**
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003523 * Finds free space in the GTT aperture and binds the object or a view of it
3524 * there.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003525 * @obj: object to bind
3526 * @vm: address space to bind into
3527 * @ggtt_view: global gtt view if applicable
3528 * @alignment: requested alignment
3529 * @flags: mask of PIN_* flags to use
Eric Anholt673a3942008-07-30 12:06:12 -07003530 */
Daniel Vetter262de142014-02-14 14:01:20 +01003531static struct i915_vma *
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003532i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3533 struct i915_address_space *vm,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003534 const struct i915_ggtt_view *ggtt_view,
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003535 unsigned alignment,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003536 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003537{
Chris Wilson05394f32010-11-08 19:18:58 +00003538 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003539 struct drm_i915_private *dev_priv = to_i915(dev);
3540 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierry65bd3422015-07-29 17:23:58 +01003541 u32 fence_alignment, unfenced_alignment;
Michel Thierry101b5062015-10-01 13:33:57 +01003542 u32 search_flag, alloc_flag;
3543 u64 start, end;
Michel Thierry65bd3422015-07-29 17:23:58 +01003544 u64 size, fence_size;
Ben Widawsky2f633152013-07-17 12:19:03 -07003545 struct i915_vma *vma;
Chris Wilson07f73f62009-09-14 16:50:30 +01003546 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003547
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003548 if (i915_is_ggtt(vm)) {
3549 u32 view_size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003550
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003551 if (WARN_ON(!ggtt_view))
3552 return ERR_PTR(-EINVAL);
3553
3554 view_size = i915_ggtt_view_size(obj, ggtt_view);
3555
3556 fence_size = i915_gem_get_gtt_size(dev,
3557 view_size,
3558 obj->tiling_mode);
3559 fence_alignment = i915_gem_get_gtt_alignment(dev,
3560 view_size,
3561 obj->tiling_mode,
3562 true);
3563 unfenced_alignment = i915_gem_get_gtt_alignment(dev,
3564 view_size,
3565 obj->tiling_mode,
3566 false);
3567 size = flags & PIN_MAPPABLE ? fence_size : view_size;
3568 } else {
3569 fence_size = i915_gem_get_gtt_size(dev,
3570 obj->base.size,
3571 obj->tiling_mode);
3572 fence_alignment = i915_gem_get_gtt_alignment(dev,
3573 obj->base.size,
3574 obj->tiling_mode,
3575 true);
3576 unfenced_alignment =
3577 i915_gem_get_gtt_alignment(dev,
3578 obj->base.size,
3579 obj->tiling_mode,
3580 false);
3581 size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
3582 }
Chris Wilsona00b10c2010-09-24 21:15:47 +01003583
Michel Thierry101b5062015-10-01 13:33:57 +01003584 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
3585 end = vm->total;
3586 if (flags & PIN_MAPPABLE)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003587 end = min_t(u64, end, ggtt->mappable_end);
Michel Thierry101b5062015-10-01 13:33:57 +01003588 if (flags & PIN_ZONE_4G)
Michel Thierry48ea1e32016-01-11 11:39:27 +00003589 end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
Michel Thierry101b5062015-10-01 13:33:57 +01003590
Eric Anholt673a3942008-07-30 12:06:12 -07003591 if (alignment == 0)
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003592 alignment = flags & PIN_MAPPABLE ? fence_alignment :
Daniel Vetter5e783302010-11-14 22:32:36 +01003593 unfenced_alignment;
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003594 if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003595 DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
3596 ggtt_view ? ggtt_view->type : 0,
3597 alignment);
Daniel Vetter262de142014-02-14 14:01:20 +01003598 return ERR_PTR(-EINVAL);
Eric Anholt673a3942008-07-30 12:06:12 -07003599 }
3600
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003601 /* If binding the object/GGTT view requires more space than the entire
3602 * aperture has, reject it early before evicting everything in a vain
3603 * attempt to find space.
Chris Wilson654fc602010-05-27 13:18:21 +01003604 */
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003605 if (size > end) {
Michel Thierry65bd3422015-07-29 17:23:58 +01003606 DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003607 ggtt_view ? ggtt_view->type : 0,
3608 size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003609 flags & PIN_MAPPABLE ? "mappable" : "total",
Chris Wilsond23db882014-05-23 08:48:08 +02003610 end);
Daniel Vetter262de142014-02-14 14:01:20 +01003611 return ERR_PTR(-E2BIG);
Chris Wilson654fc602010-05-27 13:18:21 +01003612 }
3613
Chris Wilson37e680a2012-06-07 15:38:42 +01003614 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003615 if (ret)
Daniel Vetter262de142014-02-14 14:01:20 +01003616 return ERR_PTR(ret);
Chris Wilson6c085a72012-08-20 11:40:46 +02003617
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003618 i915_gem_object_pin_pages(obj);
3619
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003620 vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
3621 i915_gem_obj_lookup_or_create_vma(obj, vm);
3622
Daniel Vetter262de142014-02-14 14:01:20 +01003623 if (IS_ERR(vma))
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003624 goto err_unpin;
Ben Widawsky2f633152013-07-17 12:19:03 -07003625
Chris Wilson506a8e82015-12-08 11:55:07 +00003626 if (flags & PIN_OFFSET_FIXED) {
3627 uint64_t offset = flags & PIN_OFFSET_MASK;
3628
3629 if (offset & (alignment - 1) || offset + size > end) {
3630 ret = -EINVAL;
3631 goto err_free_vma;
3632 }
3633 vma->node.start = offset;
3634 vma->node.size = size;
3635 vma->node.color = obj->cache_level;
3636 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3637 if (ret) {
3638 ret = i915_gem_evict_for_vma(vma);
3639 if (ret == 0)
3640 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3641 }
3642 if (ret)
3643 goto err_free_vma;
Michel Thierry101b5062015-10-01 13:33:57 +01003644 } else {
Chris Wilson506a8e82015-12-08 11:55:07 +00003645 if (flags & PIN_HIGH) {
3646 search_flag = DRM_MM_SEARCH_BELOW;
3647 alloc_flag = DRM_MM_CREATE_TOP;
3648 } else {
3649 search_flag = DRM_MM_SEARCH_DEFAULT;
3650 alloc_flag = DRM_MM_CREATE_DEFAULT;
3651 }
Michel Thierry101b5062015-10-01 13:33:57 +01003652
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003653search_free:
Chris Wilson506a8e82015-12-08 11:55:07 +00003654 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
3655 size, alignment,
3656 obj->cache_level,
3657 start, end,
3658 search_flag,
3659 alloc_flag);
3660 if (ret) {
3661 ret = i915_gem_evict_something(dev, vm, size, alignment,
3662 obj->cache_level,
3663 start, end,
3664 flags);
3665 if (ret == 0)
3666 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003667
Chris Wilson506a8e82015-12-08 11:55:07 +00003668 goto err_free_vma;
3669 }
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003670 }
Chris Wilson4144f9b2014-09-11 08:43:48 +01003671 if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
Ben Widawsky2f633152013-07-17 12:19:03 -07003672 ret = -EINVAL;
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003673 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003674 }
3675
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003676 trace_i915_vma_bind(vma, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07003677 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003678 if (ret)
Imre Deake2273302015-07-09 12:59:05 +03003679 goto err_remove_node;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003680
Ben Widawsky35c20a62013-05-31 11:28:48 -07003681 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003682 list_add_tail(&vma->vm_link, &vm->inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003683
Daniel Vetter262de142014-02-14 14:01:20 +01003684 return vma;
Ben Widawsky2f633152013-07-17 12:19:03 -07003685
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003686err_remove_node:
Dan Carpenter6286ef92013-07-19 08:46:27 +03003687 drm_mm_remove_node(&vma->node);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003688err_free_vma:
Ben Widawsky2f633152013-07-17 12:19:03 -07003689 i915_gem_vma_destroy(vma);
Daniel Vetter262de142014-02-14 14:01:20 +01003690 vma = ERR_PTR(ret);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003691err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003692 i915_gem_object_unpin_pages(obj);
Daniel Vetter262de142014-02-14 14:01:20 +01003693 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003694}
3695
Chris Wilson000433b2013-08-08 14:41:09 +01003696bool
Chris Wilson2c225692013-08-09 12:26:45 +01003697i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3698 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003699{
Eric Anholt673a3942008-07-30 12:06:12 -07003700 /* If we don't have a page list set up, then we're not pinned
3701 * to GPU, and we can ignore the cache flush because it'll happen
3702 * again at bind time.
3703 */
Chris Wilson05394f32010-11-08 19:18:58 +00003704 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003705 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003706
Imre Deak769ce462013-02-13 21:56:05 +02003707 /*
3708 * Stolen memory is always coherent with the GPU as it is explicitly
3709 * marked as wc by the system, or the system is cache-coherent.
3710 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003711 if (obj->stolen || obj->phys_handle)
Chris Wilson000433b2013-08-08 14:41:09 +01003712 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003713
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003714 /* If the GPU is snooping the contents of the CPU cache,
3715 * we do not need to manually clear the CPU cache lines. However,
3716 * the caches are only snooped when the render cache is
3717 * flushed/invalidated. As we always have to emit invalidations
3718 * and flushes when moving into and out of the RENDER domain, correct
3719 * snooping behaviour occurs naturally as the result of our domain
3720 * tracking.
3721 */
Chris Wilson0f719792015-01-13 13:32:52 +00003722 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3723 obj->cache_dirty = true;
Chris Wilson000433b2013-08-08 14:41:09 +01003724 return false;
Chris Wilson0f719792015-01-13 13:32:52 +00003725 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003726
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003727 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003728 drm_clflush_sg(obj->pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003729 obj->cache_dirty = false;
Chris Wilson000433b2013-08-08 14:41:09 +01003730
3731 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003732}
3733
3734/** Flushes the GTT write domain for the object if it's dirty. */
3735static void
Chris Wilson05394f32010-11-08 19:18:58 +00003736i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003737{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003738 uint32_t old_write_domain;
3739
Chris Wilson05394f32010-11-08 19:18:58 +00003740 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003741 return;
3742
Chris Wilson63256ec2011-01-04 18:42:07 +00003743 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003744 * to it immediately go to main memory as far as we know, so there's
3745 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003746 *
3747 * However, we do have to enforce the order so that all writes through
3748 * the GTT land before any writes to the device, such as updates to
3749 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003750 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003751 wmb();
3752
Chris Wilson05394f32010-11-08 19:18:58 +00003753 old_write_domain = obj->base.write_domain;
3754 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003755
Rodrigo Vivide152b62015-07-07 16:28:51 -07003756 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003757
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003758 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003759 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003760 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003761}
3762
3763/** Flushes the CPU write domain for the object if it's dirty. */
3764static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003765i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003766{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003767 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003768
Chris Wilson05394f32010-11-08 19:18:58 +00003769 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003770 return;
3771
Daniel Vettere62b59e2015-01-21 14:53:48 +01003772 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilsonc0336662016-05-06 15:40:21 +01003773 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilson000433b2013-08-08 14:41:09 +01003774
Chris Wilson05394f32010-11-08 19:18:58 +00003775 old_write_domain = obj->base.write_domain;
3776 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003777
Rodrigo Vivide152b62015-07-07 16:28:51 -07003778 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003779
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003780 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003781 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003782 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003783}
3784
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003785/**
3786 * Moves a single object to the GTT read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003787 * @obj: object to act on
3788 * @write: ask for write access or read only
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003789 *
3790 * This function returns when the move is complete, including waiting on
3791 * flushes to occur.
3792 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003793int
Chris Wilson20217462010-11-23 15:26:33 +00003794i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003795{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003796 struct drm_device *dev = obj->base.dev;
3797 struct drm_i915_private *dev_priv = to_i915(dev);
3798 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003799 uint32_t old_write_domain, old_read_domains;
Chris Wilson43566de2015-01-02 16:29:29 +05303800 struct i915_vma *vma;
Eric Anholte47c68e2008-11-14 13:35:19 -08003801 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003802
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003803 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3804 return 0;
3805
Chris Wilson0201f1e2012-07-20 12:41:01 +01003806 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003807 if (ret)
3808 return ret;
3809
Chris Wilson43566de2015-01-02 16:29:29 +05303810 /* Flush and acquire obj->pages so that we are coherent through
3811 * direct access in memory with previous cached writes through
3812 * shmemfs and that our cache domain tracking remains valid.
3813 * For example, if the obj->filp was moved to swap without us
3814 * being notified and releasing the pages, we would mistakenly
3815 * continue to assume that the obj remained out of the CPU cached
3816 * domain.
3817 */
3818 ret = i915_gem_object_get_pages(obj);
3819 if (ret)
3820 return ret;
3821
Daniel Vettere62b59e2015-01-21 14:53:48 +01003822 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003823
Chris Wilsond0a57782012-10-09 19:24:37 +01003824 /* Serialise direct access to this object with the barriers for
3825 * coherent writes from the GPU, by effectively invalidating the
3826 * GTT domain upon first access.
3827 */
3828 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3829 mb();
3830
Chris Wilson05394f32010-11-08 19:18:58 +00003831 old_write_domain = obj->base.write_domain;
3832 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003833
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003834 /* It should now be out of any other write domains, and we can update
3835 * the domain values for our changes.
3836 */
Chris Wilson05394f32010-11-08 19:18:58 +00003837 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3838 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003839 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003840 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3841 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3842 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003843 }
3844
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003845 trace_i915_gem_object_change_domain(obj,
3846 old_read_domains,
3847 old_write_domain);
3848
Chris Wilson8325a092012-04-24 15:52:35 +01003849 /* And bump the LRU for this access */
Chris Wilson43566de2015-01-02 16:29:29 +05303850 vma = i915_gem_obj_to_ggtt(obj);
3851 if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003852 list_move_tail(&vma->vm_link,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003853 &ggtt->base.inactive_list);
Chris Wilson8325a092012-04-24 15:52:35 +01003854
Eric Anholte47c68e2008-11-14 13:35:19 -08003855 return 0;
3856}
3857
Chris Wilsonef55f922015-10-09 14:11:27 +01003858/**
3859 * Changes the cache-level of an object across all VMA.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01003860 * @obj: object to act on
3861 * @cache_level: new cache level to set for the object
Chris Wilsonef55f922015-10-09 14:11:27 +01003862 *
3863 * After this function returns, the object will be in the new cache-level
3864 * across all GTT and the contents of the backing storage will be coherent,
3865 * with respect to the new cache-level. In order to keep the backing storage
3866 * coherent for all users, we only allow a single cache level to be set
3867 * globally on the object and prevent it from being changed whilst the
3868 * hardware is reading from the object. That is if the object is currently
3869 * on the scanout it will be set to uncached (or equivalent display
3870 * cache coherency) and all non-MOCS GPU access will also be uncached so
3871 * that all direct access to the scanout remains coherent.
3872 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003873int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3874 enum i915_cache_level cache_level)
3875{
Daniel Vetter7bddb012012-02-09 17:15:47 +01003876 struct drm_device *dev = obj->base.dev;
Chris Wilsondf6f7832014-03-21 07:40:56 +00003877 struct i915_vma *vma, *next;
Chris Wilsonef55f922015-10-09 14:11:27 +01003878 bool bound = false;
Ville Syrjäläed75a552015-08-11 19:47:10 +03003879 int ret = 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003880
3881 if (obj->cache_level == cache_level)
Ville Syrjäläed75a552015-08-11 19:47:10 +03003882 goto out;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003883
Chris Wilsonef55f922015-10-09 14:11:27 +01003884 /* Inspect the list of currently bound VMA and unbind any that would
3885 * be invalid given the new cache-level. This is principally to
3886 * catch the issue of the CS prefetch crossing page boundaries and
3887 * reading an invalid PTE on older architectures.
3888 */
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003889 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003890 if (!drm_mm_node_allocated(&vma->node))
3891 continue;
3892
3893 if (vma->pin_count) {
3894 DRM_DEBUG("can not change the cache level of pinned objects\n");
3895 return -EBUSY;
3896 }
3897
Chris Wilson4144f9b2014-09-11 08:43:48 +01003898 if (!i915_gem_valid_gtt_space(vma, cache_level)) {
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003899 ret = i915_vma_unbind(vma);
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003900 if (ret)
3901 return ret;
Chris Wilsonef55f922015-10-09 14:11:27 +01003902 } else
3903 bound = true;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003904 }
3905
Chris Wilsonef55f922015-10-09 14:11:27 +01003906 /* We can reuse the existing drm_mm nodes but need to change the
3907 * cache-level on the PTE. We could simply unbind them all and
3908 * rebind with the correct cache-level on next use. However since
3909 * we already have a valid slot, dma mapping, pages etc, we may as
3910 * rewrite the PTE in the belief that doing so tramples upon less
3911 * state and so involves less work.
3912 */
3913 if (bound) {
3914 /* Before we change the PTE, the GPU must not be accessing it.
3915 * If we wait upon the object, we know that all the bound
3916 * VMA are no longer active.
3917 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01003918 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003919 if (ret)
3920 return ret;
3921
Chris Wilsonef55f922015-10-09 14:11:27 +01003922 if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
3923 /* Access to snoopable pages through the GTT is
3924 * incoherent and on some machines causes a hard
3925 * lockup. Relinquish the CPU mmaping to force
3926 * userspace to refault in the pages and we can
3927 * then double check if the GTT mapping is still
3928 * valid for that pointer access.
3929 */
3930 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003931
Chris Wilsonef55f922015-10-09 14:11:27 +01003932 /* As we no longer need a fence for GTT access,
3933 * we can relinquish it now (and so prevent having
3934 * to steal a fence from someone else on the next
3935 * fence request). Note GPU activity would have
3936 * dropped the fence as all snoopable access is
3937 * supposed to be linear.
3938 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003939 ret = i915_gem_object_put_fence(obj);
3940 if (ret)
3941 return ret;
Chris Wilsonef55f922015-10-09 14:11:27 +01003942 } else {
3943 /* We either have incoherent backing store and
3944 * so no GTT access or the architecture is fully
3945 * coherent. In such cases, existing GTT mmaps
3946 * ignore the cache bit in the PTE and we can
3947 * rewrite it without confusing the GPU or having
3948 * to force userspace to fault back in its mmaps.
3949 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003950 }
3951
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003952 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003953 if (!drm_mm_node_allocated(&vma->node))
3954 continue;
3955
3956 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3957 if (ret)
3958 return ret;
3959 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003960 }
3961
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003962 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003963 vma->node.color = cache_level;
3964 obj->cache_level = cache_level;
3965
Ville Syrjäläed75a552015-08-11 19:47:10 +03003966out:
Chris Wilsonef55f922015-10-09 14:11:27 +01003967 /* Flush the dirty CPU caches to the backing storage so that the
3968 * object is now coherent at its new cache level (with respect
3969 * to the access domain).
3970 */
Chris Wilson0f719792015-01-13 13:32:52 +00003971 if (obj->cache_dirty &&
3972 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
3973 cpu_write_needs_clflush(obj)) {
3974 if (i915_gem_clflush_object(obj, true))
Chris Wilsonc0336662016-05-06 15:40:21 +01003975 i915_gem_chipset_flush(to_i915(obj->base.dev));
Chris Wilsone4ffd172011-04-04 09:44:39 +01003976 }
3977
Chris Wilsone4ffd172011-04-04 09:44:39 +01003978 return 0;
3979}
3980
Ben Widawsky199adf42012-09-21 17:01:20 -07003981int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3982 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003983{
Ben Widawsky199adf42012-09-21 17:01:20 -07003984 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003985 struct drm_i915_gem_object *obj;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003986
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01003987 obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
Chris Wilson432be692015-05-07 12:14:55 +01003988 if (&obj->base == NULL)
3989 return -ENOENT;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003990
Chris Wilson651d7942013-08-08 14:41:10 +01003991 switch (obj->cache_level) {
3992 case I915_CACHE_LLC:
3993 case I915_CACHE_L3_LLC:
3994 args->caching = I915_CACHING_CACHED;
3995 break;
3996
Chris Wilson4257d3b2013-08-08 14:41:11 +01003997 case I915_CACHE_WT:
3998 args->caching = I915_CACHING_DISPLAY;
3999 break;
4000
Chris Wilson651d7942013-08-08 14:41:10 +01004001 default:
4002 args->caching = I915_CACHING_NONE;
4003 break;
4004 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01004005
Chris Wilson432be692015-05-07 12:14:55 +01004006 drm_gem_object_unreference_unlocked(&obj->base);
4007 return 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004008}
4009
Ben Widawsky199adf42012-09-21 17:01:20 -07004010int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
4011 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01004012{
Imre Deakfd0fe6a2015-11-04 21:25:32 +02004013 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky199adf42012-09-21 17:01:20 -07004014 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004015 struct drm_i915_gem_object *obj;
4016 enum i915_cache_level level;
4017 int ret;
4018
Ben Widawsky199adf42012-09-21 17:01:20 -07004019 switch (args->caching) {
4020 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01004021 level = I915_CACHE_NONE;
4022 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07004023 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03004024 /*
4025 * Due to a HW issue on BXT A stepping, GPU stores via a
4026 * snooped mapping may leave stale data in a corresponding CPU
4027 * cacheline, whereas normally such cachelines would get
4028 * invalidated.
4029 */
Tvrtko Ursulinca377802016-03-02 12:10:31 +00004030 if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
Imre Deake5756c12015-08-14 18:43:30 +03004031 return -ENODEV;
4032
Chris Wilsone6994ae2012-07-10 10:27:08 +01004033 level = I915_CACHE_LLC;
4034 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004035 case I915_CACHING_DISPLAY:
4036 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
4037 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004038 default:
4039 return -EINVAL;
4040 }
4041
Imre Deakfd0fe6a2015-11-04 21:25:32 +02004042 intel_runtime_pm_get(dev_priv);
4043
Ben Widawsky3bc29132012-09-26 16:15:20 -07004044 ret = i915_mutex_lock_interruptible(dev);
4045 if (ret)
Imre Deakfd0fe6a2015-11-04 21:25:32 +02004046 goto rpm_put;
Ben Widawsky3bc29132012-09-26 16:15:20 -07004047
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01004048 obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
Chris Wilsone6994ae2012-07-10 10:27:08 +01004049 if (&obj->base == NULL) {
4050 ret = -ENOENT;
4051 goto unlock;
4052 }
4053
4054 ret = i915_gem_object_set_cache_level(obj, level);
4055
4056 drm_gem_object_unreference(&obj->base);
4057unlock:
4058 mutex_unlock(&dev->struct_mutex);
Imre Deakfd0fe6a2015-11-04 21:25:32 +02004059rpm_put:
4060 intel_runtime_pm_put(dev_priv);
4061
Chris Wilsone6994ae2012-07-10 10:27:08 +01004062 return ret;
4063}
4064
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004065/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004066 * Prepare buffer for display plane (scanout, cursors, etc).
4067 * Can be called from an uninterruptible phase (modesetting) and allows
4068 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004069 */
4070int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004071i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4072 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004073 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004074{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004075 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004076 int ret;
4077
Chris Wilsoncc98b412013-08-09 12:25:09 +01004078 /* Mark the pin_display early so that we account for the
4079 * display coherency whilst setting up the cache domains.
4080 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004081 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004082
Eric Anholta7ef0642011-03-29 16:59:54 -07004083 /* The display engine is not coherent with the LLC cache on gen6. As
4084 * a result, we make sure that the pinning that is about to occur is
4085 * done with uncached PTEs. This is lowest common denominator for all
4086 * chipsets.
4087 *
4088 * However for gen6+, we could do better by using the GFDT bit instead
4089 * of uncaching, which would allow us to flush all the LLC-cached data
4090 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4091 */
Chris Wilson651d7942013-08-08 14:41:10 +01004092 ret = i915_gem_object_set_cache_level(obj,
4093 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
Eric Anholta7ef0642011-03-29 16:59:54 -07004094 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004095 goto err_unpin_display;
Eric Anholta7ef0642011-03-29 16:59:54 -07004096
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004097 /* As the user may map the buffer once pinned in the display plane
4098 * (e.g. libkms for the bootup splash), we have to ensure that we
4099 * always use map_and_fenceable for all scanout buffers.
4100 */
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00004101 ret = i915_gem_object_ggtt_pin(obj, view, alignment,
4102 view->type == I915_GGTT_VIEW_NORMAL ?
4103 PIN_MAPPABLE : 0);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004104 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004105 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004106
Daniel Vettere62b59e2015-01-21 14:53:48 +01004107 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004108
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004109 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00004110 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004111
4112 /* It should now be out of any other write domains, and we can update
4113 * the domain values for our changes.
4114 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01004115 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00004116 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004117
4118 trace_i915_gem_object_change_domain(obj,
4119 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004120 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004121
4122 return 0;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004123
4124err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004125 obj->pin_display--;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004126 return ret;
4127}
4128
4129void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004130i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
4131 const struct i915_ggtt_view *view)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004132{
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004133 if (WARN_ON(obj->pin_display == 0))
4134 return;
4135
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004136 i915_gem_object_ggtt_unpin_view(obj, view);
4137
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004138 obj->pin_display--;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004139}
4140
Eric Anholte47c68e2008-11-14 13:35:19 -08004141/**
4142 * Moves a single object to the CPU read, and possibly write domain.
Tvrtko Ursulin14bb2c12016-06-03 14:02:17 +01004143 * @obj: object to act on
4144 * @write: requesting write or read-only access
Eric Anholte47c68e2008-11-14 13:35:19 -08004145 *
4146 * This function returns when the move is complete, including waiting on
4147 * flushes to occur.
4148 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004149int
Chris Wilson919926a2010-11-12 13:42:53 +00004150i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004151{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004152 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08004153 int ret;
4154
Chris Wilson8d7e3de2011-02-07 15:23:02 +00004155 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
4156 return 0;
4157
Chris Wilson0201f1e2012-07-20 12:41:01 +01004158 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00004159 if (ret)
4160 return ret;
4161
Eric Anholte47c68e2008-11-14 13:35:19 -08004162 i915_gem_object_flush_gtt_write_domain(obj);
4163
Chris Wilson05394f32010-11-08 19:18:58 +00004164 old_write_domain = obj->base.write_domain;
4165 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004166
Eric Anholte47c68e2008-11-14 13:35:19 -08004167 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00004168 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01004169 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08004170
Chris Wilson05394f32010-11-08 19:18:58 +00004171 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004172 }
4173
4174 /* It should now be out of any other write domains, and we can update
4175 * the domain values for our changes.
4176 */
Chris Wilson05394f32010-11-08 19:18:58 +00004177 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08004178
4179 /* If we're writing through the CPU, then the GPU read domains will
4180 * need to be invalidated at next use.
4181 */
4182 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00004183 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4184 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004185 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004186
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004187 trace_i915_gem_object_change_domain(obj,
4188 old_read_domains,
4189 old_write_domain);
4190
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004191 return 0;
4192}
4193
Eric Anholt673a3942008-07-30 12:06:12 -07004194/* Throttle our rendering by waiting until the ring has completed our requests
4195 * emitted over 20 msec ago.
4196 *
Eric Anholtb9624422009-06-03 07:27:35 +00004197 * Note that if we were to use the current jiffies each time around the loop,
4198 * we wouldn't escape the function with any frames outstanding if the time to
4199 * render a frame was over 20ms.
4200 *
Eric Anholt673a3942008-07-30 12:06:12 -07004201 * This should get us reasonable parallelism between CPU and GPU but also
4202 * relatively low latency when blocking on a particular request to finish.
4203 */
4204static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004205i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004206{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004207 struct drm_i915_private *dev_priv = dev->dev_private;
4208 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004209 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00004210 struct drm_i915_gem_request *request, *target = NULL;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004211 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004212
Daniel Vetter308887a2012-11-14 17:14:06 +01004213 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
4214 if (ret)
4215 return ret;
4216
Chris Wilsonf4457ae2016-04-13 17:35:08 +01004217 /* ABI: return -EIO if already wedged */
4218 if (i915_terminally_wedged(&dev_priv->gpu_error))
4219 return -EIO;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004220
Chris Wilson1c255952010-09-26 11:03:27 +01004221 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004222 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00004223 if (time_after_eq(request->emitted_jiffies, recent_enough))
4224 break;
4225
John Harrisonfcfa423c2015-05-29 17:44:12 +01004226 /*
4227 * Note that the request might not have been submitted yet.
4228 * In which case emitted_jiffies will be zero.
4229 */
4230 if (!request->emitted_jiffies)
4231 continue;
4232
John Harrison54fb2412014-11-24 18:49:27 +00004233 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004234 }
John Harrisonff865882014-11-24 18:49:28 +00004235 if (target)
4236 i915_gem_request_reference(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004237 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004238
John Harrison54fb2412014-11-24 18:49:27 +00004239 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004240 return 0;
4241
Chris Wilson299259a2016-04-13 17:35:06 +01004242 ret = __i915_wait_request(target, true, NULL, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004243 if (ret == 0)
4244 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00004245
Chris Wilson73db04c2016-04-28 09:56:55 +01004246 i915_gem_request_unreference(target);
John Harrisonff865882014-11-24 18:49:28 +00004247
Eric Anholt673a3942008-07-30 12:06:12 -07004248 return ret;
4249}
4250
Chris Wilsond23db882014-05-23 08:48:08 +02004251static bool
4252i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
4253{
4254 struct drm_i915_gem_object *obj = vma->obj;
4255
4256 if (alignment &&
4257 vma->node.start & (alignment - 1))
4258 return true;
4259
4260 if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
4261 return true;
4262
4263 if (flags & PIN_OFFSET_BIAS &&
4264 vma->node.start < (flags & PIN_OFFSET_MASK))
4265 return true;
4266
Chris Wilson506a8e82015-12-08 11:55:07 +00004267 if (flags & PIN_OFFSET_FIXED &&
4268 vma->node.start != (flags & PIN_OFFSET_MASK))
4269 return true;
4270
Chris Wilsond23db882014-05-23 08:48:08 +02004271 return false;
4272}
4273
Chris Wilsond0710ab2015-11-20 14:16:39 +00004274void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
4275{
4276 struct drm_i915_gem_object *obj = vma->obj;
4277 bool mappable, fenceable;
4278 u32 fence_size, fence_alignment;
4279
4280 fence_size = i915_gem_get_gtt_size(obj->base.dev,
4281 obj->base.size,
4282 obj->tiling_mode);
4283 fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
4284 obj->base.size,
4285 obj->tiling_mode,
4286 true);
4287
4288 fenceable = (vma->node.size == fence_size &&
4289 (vma->node.start & (fence_alignment - 1)) == 0);
4290
4291 mappable = (vma->node.start + fence_size <=
Joonas Lahtinen62106b42016-03-18 10:42:57 +02004292 to_i915(obj->base.dev)->ggtt.mappable_end);
Chris Wilsond0710ab2015-11-20 14:16:39 +00004293
4294 obj->map_and_fenceable = mappable && fenceable;
4295}
4296
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004297static int
4298i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
4299 struct i915_address_space *vm,
4300 const struct i915_ggtt_view *ggtt_view,
4301 uint32_t alignment,
4302 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07004303{
Ben Widawsky6e7186a2014-05-06 22:21:36 -07004304 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004305 struct i915_vma *vma;
Chris Wilsonef79e172014-10-31 13:53:52 +00004306 unsigned bound;
Eric Anholt673a3942008-07-30 12:06:12 -07004307 int ret;
4308
Ben Widawsky6e7186a2014-05-06 22:21:36 -07004309 if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
4310 return -ENODEV;
4311
Daniel Vetterbf3d1492014-02-14 14:01:12 +01004312 if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
Daniel Vetter1ec9e262014-02-14 14:01:11 +01004313 return -EINVAL;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004314
Chris Wilsonc826c442014-10-31 13:53:53 +00004315 if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
4316 return -EINVAL;
4317
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004318 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
4319 return -EINVAL;
4320
4321 vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
4322 i915_gem_obj_to_vma(obj, vm);
4323
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004324 if (vma) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004325 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
4326 return -EBUSY;
4327
Chris Wilsond23db882014-05-23 08:48:08 +02004328 if (i915_vma_misplaced(vma, alignment, flags)) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004329 WARN(vma->pin_count,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004330 "bo is already pinned in %s with incorrect alignment:"
Michel Thierry088e0df2015-08-07 17:40:17 +01004331 " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004332 " obj->map_and_fenceable=%d\n",
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004333 ggtt_view ? "ggtt" : "ppgtt",
Michel Thierry088e0df2015-08-07 17:40:17 +01004334 upper_32_bits(vma->node.start),
4335 lower_32_bits(vma->node.start),
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004336 alignment,
Chris Wilsond23db882014-05-23 08:48:08 +02004337 !!(flags & PIN_MAPPABLE),
Chris Wilson05394f32010-11-08 19:18:58 +00004338 obj->map_and_fenceable);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004339 ret = i915_vma_unbind(vma);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004340 if (ret)
4341 return ret;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004342
4343 vma = NULL;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004344 }
4345 }
4346
Chris Wilsonef79e172014-10-31 13:53:52 +00004347 bound = vma ? vma->bound : 0;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004348 if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004349 vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
4350 flags);
Daniel Vetter262de142014-02-14 14:01:20 +01004351 if (IS_ERR(vma))
4352 return PTR_ERR(vma);
Daniel Vetter08755462015-04-20 09:04:05 -07004353 } else {
4354 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004355 if (ret)
4356 return ret;
4357 }
Daniel Vetter74898d72012-02-15 23:50:22 +01004358
Joonas Lahtinen91e67112015-05-06 14:33:58 +03004359 if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
4360 (bound ^ vma->bound) & GLOBAL_BIND) {
Chris Wilsond0710ab2015-11-20 14:16:39 +00004361 __i915_vma_set_map_and_fenceable(vma);
Joonas Lahtinen91e67112015-05-06 14:33:58 +03004362 WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
4363 }
Chris Wilsonef79e172014-10-31 13:53:52 +00004364
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004365 vma->pin_count++;
Eric Anholt673a3942008-07-30 12:06:12 -07004366 return 0;
4367}
4368
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004369int
4370i915_gem_object_pin(struct drm_i915_gem_object *obj,
4371 struct i915_address_space *vm,
4372 uint32_t alignment,
4373 uint64_t flags)
4374{
4375 return i915_gem_object_do_pin(obj, vm,
4376 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
4377 alignment, flags);
4378}
4379
4380int
4381i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4382 const struct i915_ggtt_view *view,
4383 uint32_t alignment,
4384 uint64_t flags)
4385{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004386 struct drm_device *dev = obj->base.dev;
4387 struct drm_i915_private *dev_priv = to_i915(dev);
4388 struct i915_ggtt *ggtt = &dev_priv->ggtt;
4389
Matthew Auldade7daa2016-03-24 15:54:20 +00004390 BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004391
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004392 return i915_gem_object_do_pin(obj, &ggtt->base, view,
Tvrtko Ursulin6fafab72015-03-17 15:36:51 +00004393 alignment, flags | PIN_GLOBAL);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004394}
4395
Eric Anholt673a3942008-07-30 12:06:12 -07004396void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004397i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
4398 const struct i915_ggtt_view *view)
Eric Anholt673a3942008-07-30 12:06:12 -07004399{
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004400 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
Eric Anholt673a3942008-07-30 12:06:12 -07004401
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004402 WARN_ON(vma->pin_count == 0);
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004403 WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004404
Chris Wilson30154652015-04-07 17:28:24 +01004405 --vma->pin_count;
Eric Anholt673a3942008-07-30 12:06:12 -07004406}
4407
4408int
Eric Anholt673a3942008-07-30 12:06:12 -07004409i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004410 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004411{
4412 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004413 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004414 int ret;
4415
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004416 ret = i915_mutex_lock_interruptible(dev);
4417 if (ret)
4418 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004419
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01004420 obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004421 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004422 ret = -ENOENT;
4423 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004424 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004425
Chris Wilson0be555b2010-08-04 15:36:30 +01004426 /* Count all active objects as busy, even if they are currently not used
4427 * by the gpu. Users of this interface expect objects to eventually
4428 * become non-busy without any further actions, therefore emit any
4429 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004430 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004431 ret = i915_gem_object_flush_active(obj);
Chris Wilsonb4716182015-04-27 13:41:17 +01004432 if (ret)
4433 goto unref;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004434
Chris Wilson426960b2016-01-15 16:51:46 +00004435 args->busy = 0;
4436 if (obj->active) {
4437 int i;
4438
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004439 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilson426960b2016-01-15 16:51:46 +00004440 struct drm_i915_gem_request *req;
4441
4442 req = obj->last_read_req[i];
4443 if (req)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00004444 args->busy |= 1 << (16 + req->engine->exec_id);
Chris Wilson426960b2016-01-15 16:51:46 +00004445 }
4446 if (obj->last_write_req)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00004447 args->busy |= obj->last_write_req->engine->exec_id;
Chris Wilson426960b2016-01-15 16:51:46 +00004448 }
Eric Anholt673a3942008-07-30 12:06:12 -07004449
Chris Wilsonb4716182015-04-27 13:41:17 +01004450unref:
Chris Wilson05394f32010-11-08 19:18:58 +00004451 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004452unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004453 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004454 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004455}
4456
4457int
4458i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4459 struct drm_file *file_priv)
4460{
Akshay Joshi0206e352011-08-16 15:34:10 -04004461 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004462}
4463
Chris Wilson3ef94da2009-09-14 16:50:29 +01004464int
4465i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4466 struct drm_file *file_priv)
4467{
Daniel Vetter656bfa32014-11-20 09:26:30 +01004468 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004469 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004470 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004471 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004472
4473 switch (args->madv) {
4474 case I915_MADV_DONTNEED:
4475 case I915_MADV_WILLNEED:
4476 break;
4477 default:
4478 return -EINVAL;
4479 }
4480
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004481 ret = i915_mutex_lock_interruptible(dev);
4482 if (ret)
4483 return ret;
4484
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01004485 obj = to_intel_bo(drm_gem_object_lookup(file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004486 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004487 ret = -ENOENT;
4488 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004489 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004490
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004491 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004492 ret = -EINVAL;
4493 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004494 }
4495
Daniel Vetter656bfa32014-11-20 09:26:30 +01004496 if (obj->pages &&
4497 obj->tiling_mode != I915_TILING_NONE &&
4498 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4499 if (obj->madv == I915_MADV_WILLNEED)
4500 i915_gem_object_unpin_pages(obj);
4501 if (args->madv == I915_MADV_WILLNEED)
4502 i915_gem_object_pin_pages(obj);
4503 }
4504
Chris Wilson05394f32010-11-08 19:18:58 +00004505 if (obj->madv != __I915_MADV_PURGED)
4506 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004507
Chris Wilson6c085a72012-08-20 11:40:46 +02004508 /* if the object is no longer attached, discard its backing storage */
Daniel Vetterbe6a0372015-03-18 10:46:04 +01004509 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004510 i915_gem_object_truncate(obj);
4511
Chris Wilson05394f32010-11-08 19:18:58 +00004512 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004513
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004514out:
Chris Wilson05394f32010-11-08 19:18:58 +00004515 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004516unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004517 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004518 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004519}
4520
Chris Wilson37e680a2012-06-07 15:38:42 +01004521void i915_gem_object_init(struct drm_i915_gem_object *obj,
4522 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004523{
Chris Wilsonb4716182015-04-27 13:41:17 +01004524 int i;
4525
Ben Widawsky35c20a62013-05-31 11:28:48 -07004526 INIT_LIST_HEAD(&obj->global_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004527 for (i = 0; i < I915_NUM_ENGINES; i++)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004528 INIT_LIST_HEAD(&obj->engine_list[i]);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004529 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004530 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004531 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004532
Chris Wilson37e680a2012-06-07 15:38:42 +01004533 obj->ops = ops;
4534
Chris Wilson0327d6b2012-08-11 15:41:06 +01004535 obj->fence_reg = I915_FENCE_REG_NONE;
4536 obj->madv = I915_MADV_WILLNEED;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004537
4538 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
4539}
4540
Chris Wilson37e680a2012-06-07 15:38:42 +01004541static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +00004542 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004543 .get_pages = i915_gem_object_get_pages_gtt,
4544 .put_pages = i915_gem_object_put_pages_gtt,
4545};
4546
Dave Gordond37cd8a2016-04-22 19:14:32 +01004547struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004548 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004549{
Daniel Vetterc397b902010-04-09 19:05:07 +00004550 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004551 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004552 gfp_t mask;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004553 int ret;
Daniel Vetterc397b902010-04-09 19:05:07 +00004554
Chris Wilson42dcedd2012-11-15 11:32:30 +00004555 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004556 if (obj == NULL)
Chris Wilsonfe3db792016-04-25 13:32:13 +01004557 return ERR_PTR(-ENOMEM);
Daniel Vetterc397b902010-04-09 19:05:07 +00004558
Chris Wilsonfe3db792016-04-25 13:32:13 +01004559 ret = drm_gem_object_init(dev, &obj->base, size);
4560 if (ret)
4561 goto fail;
Daniel Vetterc397b902010-04-09 19:05:07 +00004562
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004563 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4564 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4565 /* 965gm cannot relocate objects above 4GiB. */
4566 mask &= ~__GFP_HIGHMEM;
4567 mask |= __GFP_DMA32;
4568 }
4569
Al Viro496ad9a2013-01-23 17:07:38 -05004570 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004571 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004572
Chris Wilson37e680a2012-06-07 15:38:42 +01004573 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004574
Daniel Vetterc397b902010-04-09 19:05:07 +00004575 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4576 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4577
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004578 if (HAS_LLC(dev)) {
4579 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004580 * cache) for about a 10% performance improvement
4581 * compared to uncached. Graphics requests other than
4582 * display scanout are coherent with the CPU in
4583 * accessing this cache. This means in this mode we
4584 * don't need to clflush on the CPU side, and on the
4585 * GPU side we only need to flush internal caches to
4586 * get data visible to the CPU.
4587 *
4588 * However, we maintain the display planes as UC, and so
4589 * need to rebind when first used as such.
4590 */
4591 obj->cache_level = I915_CACHE_LLC;
4592 } else
4593 obj->cache_level = I915_CACHE_NONE;
4594
Daniel Vetterd861e332013-07-24 23:25:03 +02004595 trace_i915_gem_object_create(obj);
4596
Chris Wilson05394f32010-11-08 19:18:58 +00004597 return obj;
Chris Wilsonfe3db792016-04-25 13:32:13 +01004598
4599fail:
4600 i915_gem_object_free(obj);
4601
4602 return ERR_PTR(ret);
Daniel Vetterac52bc52010-04-09 19:05:06 +00004603}
4604
Chris Wilson340fbd82014-05-22 09:16:52 +01004605static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4606{
4607 /* If we are the last user of the backing storage (be it shmemfs
4608 * pages or stolen etc), we know that the pages are going to be
4609 * immediately released. In this case, we can then skip copying
4610 * back the contents from the GPU.
4611 */
4612
4613 if (obj->madv != I915_MADV_WILLNEED)
4614 return false;
4615
4616 if (obj->base.filp == NULL)
4617 return true;
4618
4619 /* At first glance, this looks racy, but then again so would be
4620 * userspace racing mmap against close. However, the first external
4621 * reference to the filp can only be obtained through the
4622 * i915_gem_mmap_ioctl() which safeguards us against the user
4623 * acquiring such a reference whilst we are in the middle of
4624 * freeing the object.
4625 */
4626 return atomic_long_read(&obj->base.filp->f_count) == 1;
4627}
4628
Chris Wilson1488fc02012-04-24 15:47:31 +01004629void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004630{
Chris Wilson1488fc02012-04-24 15:47:31 +01004631 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004632 struct drm_device *dev = obj->base.dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004633 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004634 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004635
Paulo Zanonif65c9162013-11-27 18:20:34 -02004636 intel_runtime_pm_get(dev_priv);
4637
Chris Wilson26e12f892011-03-20 11:20:19 +00004638 trace_i915_gem_object_destroy(obj);
4639
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004640 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004641 int ret;
4642
4643 vma->pin_count = 0;
4644 ret = i915_vma_unbind(vma);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004645 if (WARN_ON(ret == -ERESTARTSYS)) {
4646 bool was_interruptible;
Chris Wilson1488fc02012-04-24 15:47:31 +01004647
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004648 was_interruptible = dev_priv->mm.interruptible;
4649 dev_priv->mm.interruptible = false;
Chris Wilson1488fc02012-04-24 15:47:31 +01004650
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004651 WARN_ON(i915_vma_unbind(vma));
Chris Wilson1488fc02012-04-24 15:47:31 +01004652
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004653 dev_priv->mm.interruptible = was_interruptible;
4654 }
Chris Wilson1488fc02012-04-24 15:47:31 +01004655 }
4656
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004657 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4658 * before progressing. */
4659 if (obj->stolen)
4660 i915_gem_object_unpin_pages(obj);
4661
Daniel Vettera071fa02014-06-18 23:28:09 +02004662 WARN_ON(obj->frontbuffer_bits);
4663
Daniel Vetter656bfa32014-11-20 09:26:30 +01004664 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4665 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
4666 obj->tiling_mode != I915_TILING_NONE)
4667 i915_gem_object_unpin_pages(obj);
4668
Ben Widawsky401c29f2013-05-31 11:28:47 -07004669 if (WARN_ON(obj->pages_pin_count))
4670 obj->pages_pin_count = 0;
Chris Wilson340fbd82014-05-22 09:16:52 +01004671 if (discard_backing_storage(obj))
Chris Wilson55372522014-03-25 13:23:06 +00004672 obj->madv = I915_MADV_DONTNEED;
Chris Wilson37e680a2012-06-07 15:38:42 +01004673 i915_gem_object_put_pages(obj);
Chris Wilsond8cb5082012-08-11 15:41:03 +01004674 i915_gem_object_free_mmap_offset(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004675
Chris Wilson9da3da62012-06-01 15:20:22 +01004676 BUG_ON(obj->pages);
4677
Chris Wilson2f745ad2012-09-04 21:02:58 +01004678 if (obj->base.import_attach)
4679 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004680
Chris Wilson5cc9ed42014-05-16 14:22:37 +01004681 if (obj->ops->release)
4682 obj->ops->release(obj);
4683
Chris Wilson05394f32010-11-08 19:18:58 +00004684 drm_gem_object_release(&obj->base);
4685 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004686
Chris Wilson05394f32010-11-08 19:18:58 +00004687 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004688 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004689
4690 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004691}
4692
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004693struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
4694 struct i915_address_space *vm)
Ben Widawsky2f633152013-07-17 12:19:03 -07004695{
Daniel Vettere656a6c2013-08-14 14:14:04 +02004696 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004697 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Tvrtko Ursulin1b683722015-11-12 11:59:55 +00004698 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
4699 vma->vm == vm)
Daniel Vettere656a6c2013-08-14 14:14:04 +02004700 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004701 }
4702 return NULL;
4703}
Daniel Vettere656a6c2013-08-14 14:14:04 +02004704
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004705struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
4706 const struct i915_ggtt_view *view)
4707{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004708 struct i915_vma *vma;
4709
Tvrtko Ursulin598b9ec2016-04-21 13:04:44 +01004710 GEM_BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004711
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004712 list_for_each_entry(vma, &obj->vma_list, obj_link)
Tvrtko Ursulin598b9ec2016-04-21 13:04:44 +01004713 if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004714 return vma;
Daniel Vettere656a6c2013-08-14 14:14:04 +02004715 return NULL;
4716}
4717
Ben Widawsky2f633152013-07-17 12:19:03 -07004718void i915_gem_vma_destroy(struct i915_vma *vma)
4719{
4720 WARN_ON(vma->node.allocated);
Chris Wilsonaaa05662013-08-20 12:56:40 +01004721
4722 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4723 if (!list_empty(&vma->exec_list))
4724 return;
4725
Chris Wilson596c5922016-02-26 11:03:20 +00004726 if (!vma->is_ggtt)
4727 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004728
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004729 list_del(&vma->obj_link);
Daniel Vetterb93dab62013-08-26 11:23:47 +02004730
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004731 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
Ben Widawsky2f633152013-07-17 12:19:03 -07004732}
4733
Chris Wilsone3efda42014-04-09 09:19:41 +01004734static void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004735i915_gem_stop_engines(struct drm_device *dev)
Chris Wilsone3efda42014-04-09 09:19:41 +01004736{
4737 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004738 struct intel_engine_cs *engine;
Chris Wilsone3efda42014-04-09 09:19:41 +01004739
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004740 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004741 dev_priv->gt.stop_engine(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01004742}
4743
Jesse Barnes5669fca2009-02-17 15:13:31 -08004744int
Chris Wilson45c5f202013-10-16 11:50:01 +01004745i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004746{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004747 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson45c5f202013-10-16 11:50:01 +01004748 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004749
Chris Wilson45c5f202013-10-16 11:50:01 +01004750 mutex_lock(&dev->struct_mutex);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004751 ret = i915_gpu_idle(dev);
Chris Wilsonf7403342013-09-13 23:57:04 +01004752 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004753 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004754
Chris Wilsonc0336662016-05-06 15:40:21 +01004755 i915_gem_retire_requests(dev_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004756
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004757 i915_gem_stop_engines(dev);
Chris Wilsonb2e862d2016-04-28 09:56:41 +01004758 i915_gem_context_lost(dev_priv);
Chris Wilson45c5f202013-10-16 11:50:01 +01004759 mutex_unlock(&dev->struct_mutex);
4760
Chris Wilson737b1502015-01-26 18:03:03 +02004761 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004762 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
Deepak S274fa1c2014-08-05 07:51:20 -07004763 flush_delayed_work(&dev_priv->mm.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004764
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004765 /* Assert that we sucessfully flushed all the work and
4766 * reset the GPU back to its idle, low power state.
4767 */
4768 WARN_ON(dev_priv->mm.busy);
4769
Eric Anholt673a3942008-07-30 12:06:12 -07004770 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004771
4772err:
4773 mutex_unlock(&dev->struct_mutex);
4774 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004775}
4776
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004777void i915_gem_init_swizzling(struct drm_device *dev)
4778{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004779 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004780
Daniel Vetter11782b02012-01-31 16:47:55 +01004781 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004782 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4783 return;
4784
4785 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4786 DISP_TILE_SURFACE_SWIZZLING);
4787
Daniel Vetter11782b02012-01-31 16:47:55 +01004788 if (IS_GEN5(dev))
4789 return;
4790
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004791 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4792 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004793 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004794 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004795 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky31a53362013-11-02 21:07:04 -07004796 else if (IS_GEN8(dev))
4797 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004798 else
4799 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004800}
Daniel Vettere21af882012-02-09 20:53:27 +01004801
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004802static void init_unused_ring(struct drm_device *dev, u32 base)
4803{
4804 struct drm_i915_private *dev_priv = dev->dev_private;
4805
4806 I915_WRITE(RING_CTL(base), 0);
4807 I915_WRITE(RING_HEAD(base), 0);
4808 I915_WRITE(RING_TAIL(base), 0);
4809 I915_WRITE(RING_START(base), 0);
4810}
4811
4812static void init_unused_rings(struct drm_device *dev)
4813{
4814 if (IS_I830(dev)) {
4815 init_unused_ring(dev, PRB1_BASE);
4816 init_unused_ring(dev, SRB0_BASE);
4817 init_unused_ring(dev, SRB1_BASE);
4818 init_unused_ring(dev, SRB2_BASE);
4819 init_unused_ring(dev, SRB3_BASE);
4820 } else if (IS_GEN2(dev)) {
4821 init_unused_ring(dev, SRB0_BASE);
4822 init_unused_ring(dev, SRB1_BASE);
4823 } else if (IS_GEN3(dev)) {
4824 init_unused_ring(dev, PRB1_BASE);
4825 init_unused_ring(dev, PRB2_BASE);
4826 }
4827}
4828
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004829int i915_gem_init_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004830{
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004831 struct drm_i915_private *dev_priv = dev->dev_private;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004832 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004833
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004834 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004835 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004836 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004837
4838 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004839 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004840 if (ret)
4841 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004842 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004843
Jani Nikulad39398f2015-10-07 11:17:44 +03004844 if (HAS_BLT(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01004845 ret = intel_init_blt_ring_buffer(dev);
4846 if (ret)
4847 goto cleanup_bsd_ring;
4848 }
4849
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004850 if (HAS_VEBOX(dev)) {
4851 ret = intel_init_vebox_ring_buffer(dev);
4852 if (ret)
4853 goto cleanup_blt_ring;
4854 }
4855
Zhao Yakui845f74a2014-04-17 10:37:37 +08004856 if (HAS_BSD2(dev)) {
4857 ret = intel_init_bsd2_ring_buffer(dev);
4858 if (ret)
4859 goto cleanup_vebox_ring;
4860 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004861
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004862 return 0;
4863
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004864cleanup_vebox_ring:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004865 intel_cleanup_engine(&dev_priv->engine[VECS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004866cleanup_blt_ring:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004867 intel_cleanup_engine(&dev_priv->engine[BCS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004868cleanup_bsd_ring:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004869 intel_cleanup_engine(&dev_priv->engine[VCS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004870cleanup_render_ring:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004871 intel_cleanup_engine(&dev_priv->engine[RCS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004872
4873 return ret;
4874}
4875
4876int
4877i915_gem_init_hw(struct drm_device *dev)
4878{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004879 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004880 struct intel_engine_cs *engine;
Chris Wilsond200cda2016-04-28 09:56:44 +01004881 int ret;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004882
Chris Wilson5e4f5182015-02-13 14:35:59 +00004883 /* Double layer security blanket, see i915_gem_init() */
4884 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4885
Mika Kuoppala3accaf72016-04-13 17:26:43 +03004886 if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004887 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004888
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004889 if (IS_HASWELL(dev))
4890 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
4891 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004892
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004893 if (HAS_PCH_NOP(dev)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004894 if (IS_IVYBRIDGE(dev)) {
4895 u32 temp = I915_READ(GEN7_MSG_CTL);
4896 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4897 I915_WRITE(GEN7_MSG_CTL, temp);
4898 } else if (INTEL_INFO(dev)->gen >= 7) {
4899 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4900 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4901 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4902 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004903 }
4904
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004905 i915_gem_init_swizzling(dev);
4906
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004907 /*
4908 * At least 830 can leave some of the unused rings
4909 * "active" (ie. head != tail) after resume which
4910 * will prevent c3 entry. Makes sure all unused rings
4911 * are totally idle.
4912 */
4913 init_unused_rings(dev);
4914
Dave Gordoned54c1a2016-01-19 19:02:54 +00004915 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004916
John Harrison4ad2fd82015-06-18 13:11:20 +01004917 ret = i915_ppgtt_init_hw(dev);
4918 if (ret) {
4919 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4920 goto out;
4921 }
4922
4923 /* Need to do basic initialisation of all rings first: */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004924 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004925 ret = engine->init_hw(engine);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004926 if (ret)
Chris Wilson5e4f5182015-02-13 14:35:59 +00004927 goto out;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004928 }
Mika Kuoppala99433932013-01-22 14:12:17 +02004929
Peter Antoine0ccdacf2016-04-13 15:03:25 +01004930 intel_mocs_init_l3cc_table(dev);
4931
Alex Dai33a732f2015-08-12 15:43:36 +01004932 /* We can't enable contexts until all firmware is loaded */
Dave Gordone556f7c2016-06-07 09:14:49 +01004933 ret = intel_guc_setup(dev);
4934 if (ret)
4935 goto out;
Alex Dai33a732f2015-08-12 15:43:36 +01004936
Nick Hoathe84fe802015-09-11 12:53:46 +01004937 /*
4938 * Increment the next seqno by 0x100 so we have a visible break
4939 * on re-initialisation
4940 */
4941 ret = i915_gem_set_seqno(dev, dev_priv->next_seqno+0x100);
Daniel Vetter82460d92014-08-06 20:19:53 +02004942
Chris Wilson5e4f5182015-02-13 14:35:59 +00004943out:
4944 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004945 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004946}
4947
Chris Wilson1070a422012-04-24 15:47:41 +01004948int i915_gem_init(struct drm_device *dev)
4949{
4950 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1070a422012-04-24 15:47:41 +01004951 int ret;
4952
Chris Wilson1070a422012-04-24 15:47:41 +01004953 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004954
Oscar Mateoa83014d2014-07-24 17:04:21 +01004955 if (!i915.enable_execlists) {
John Harrisonf3dc74c2015-03-19 12:30:06 +00004956 dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004957 dev_priv->gt.init_engines = i915_gem_init_engines;
4958 dev_priv->gt.cleanup_engine = intel_cleanup_engine;
4959 dev_priv->gt.stop_engine = intel_stop_engine;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004960 } else {
John Harrisonf3dc74c2015-03-19 12:30:06 +00004961 dev_priv->gt.execbuf_submit = intel_execlists_submission;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004962 dev_priv->gt.init_engines = intel_logical_rings_init;
4963 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
4964 dev_priv->gt.stop_engine = intel_logical_ring_stop;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004965 }
4966
Chris Wilson5e4f5182015-02-13 14:35:59 +00004967 /* This is just a security blanket to placate dragons.
4968 * On some systems, we very sporadically observe that the first TLBs
4969 * used by the CS may be stale, despite us poking the TLB reset. If
4970 * we hold the forcewake during initialisation these problems
4971 * just magically go away.
4972 */
4973 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4974
Chris Wilson72778cb2016-05-19 16:17:16 +01004975 i915_gem_init_userptr(dev_priv);
Joonas Lahtinend85489d2016-03-24 16:47:46 +02004976 i915_gem_init_ggtt(dev);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004977
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004978 ret = i915_gem_context_init(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004979 if (ret)
4980 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004981
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004982 ret = dev_priv->gt.init_engines(dev);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004983 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004984 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004985
4986 ret = i915_gem_init_hw(dev);
Chris Wilson60990322014-04-09 09:19:42 +01004987 if (ret == -EIO) {
4988 /* Allow ring initialisation to fail by marking the GPU as
4989 * wedged. But we only want to do this where the GPU is angry,
4990 * for all other failure, such as an allocation failure, bail.
4991 */
4992 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Peter Zijlstra805de8f42015-04-24 01:12:32 +02004993 atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
Chris Wilson60990322014-04-09 09:19:42 +01004994 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004995 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004996
4997out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004998 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson60990322014-04-09 09:19:42 +01004999 mutex_unlock(&dev->struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005000
Chris Wilson60990322014-04-09 09:19:42 +01005001 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005002}
5003
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005004void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005005i915_gem_cleanup_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005006{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03005007 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005008 struct intel_engine_cs *engine;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005009
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005010 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005011 dev_priv->gt.cleanup_engine(engine);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005012}
5013
Chris Wilson64193402010-10-24 12:38:05 +01005014static void
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00005015init_engine_lists(struct intel_engine_cs *engine)
Chris Wilson64193402010-10-24 12:38:05 +01005016{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00005017 INIT_LIST_HEAD(&engine->active_list);
5018 INIT_LIST_HEAD(&engine->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01005019}
5020
Eric Anholt673a3942008-07-30 12:06:12 -07005021void
Imre Deak40ae4e12016-03-16 14:54:03 +02005022i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5023{
5024 struct drm_device *dev = dev_priv->dev;
5025
5026 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
5027 !IS_CHERRYVIEW(dev_priv))
5028 dev_priv->num_fence_regs = 32;
5029 else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
5030 IS_I945GM(dev_priv) || IS_G33(dev_priv))
5031 dev_priv->num_fence_regs = 16;
5032 else
5033 dev_priv->num_fence_regs = 8;
5034
Chris Wilsonc0336662016-05-06 15:40:21 +01005035 if (intel_vgpu_active(dev_priv))
Imre Deak40ae4e12016-03-16 14:54:03 +02005036 dev_priv->num_fence_regs =
5037 I915_READ(vgtif_reg(avail_rs.fence_num));
5038
5039 /* Initialize fence registers to zero */
5040 i915_gem_restore_fences(dev);
5041
5042 i915_gem_detect_bit_6_swizzle(dev);
5043}
5044
5045void
Imre Deakd64aa092016-01-19 15:26:29 +02005046i915_gem_load_init(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07005047{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03005048 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005049 int i;
5050
Chris Wilsonefab6d82015-04-07 16:20:57 +01005051 dev_priv->objects =
Chris Wilson42dcedd2012-11-15 11:32:30 +00005052 kmem_cache_create("i915_gem_object",
5053 sizeof(struct drm_i915_gem_object), 0,
5054 SLAB_HWCACHE_ALIGN,
5055 NULL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01005056 dev_priv->vmas =
5057 kmem_cache_create("i915_gem_vma",
5058 sizeof(struct i915_vma), 0,
5059 SLAB_HWCACHE_ALIGN,
5060 NULL);
Chris Wilsonefab6d82015-04-07 16:20:57 +01005061 dev_priv->requests =
5062 kmem_cache_create("i915_gem_request",
5063 sizeof(struct drm_i915_gem_request), 0,
5064 SLAB_HWCACHE_ALIGN,
5065 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07005066
Ben Widawskyfc8c0672013-07-31 16:59:54 -07005067 INIT_LIST_HEAD(&dev_priv->vm_list);
Ben Widawskya33afea2013-09-17 21:12:45 -07005068 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02005069 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
5070 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07005071 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00005072 for (i = 0; i < I915_NUM_ENGINES; i++)
5073 init_engine_lists(&dev_priv->engine[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02005074 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02005075 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07005076 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
5077 i915_gem_retire_work_handler);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005078 INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
5079 i915_gem_idle_work_handler);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005080 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005081
Chris Wilson72bfa192010-12-19 11:42:05 +00005082 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
5083
Nick Hoathe84fe802015-09-11 12:53:46 +01005084 /*
5085 * Set initial sequence number for requests.
5086 * Using this number allows the wraparound to happen early,
5087 * catching any obvious problems.
5088 */
5089 dev_priv->next_seqno = ((u32)~0 - 0x1100);
5090 dev_priv->last_seqno = ((u32)~0 - 0x1101);
5091
Chris Wilson19b2dbd2013-06-12 10:15:12 +01005092 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt10ed13e2011-05-06 13:53:49 -07005093
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005094 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01005095
Chris Wilsonce453d82011-02-21 14:43:56 +00005096 dev_priv->mm.interruptible = true;
5097
Daniel Vetterf99d7062014-06-19 16:01:59 +02005098 mutex_init(&dev_priv->fb_tracking.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07005099}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005100
Imre Deakd64aa092016-01-19 15:26:29 +02005101void i915_gem_load_cleanup(struct drm_device *dev)
5102{
5103 struct drm_i915_private *dev_priv = to_i915(dev);
5104
5105 kmem_cache_destroy(dev_priv->requests);
5106 kmem_cache_destroy(dev_priv->vmas);
5107 kmem_cache_destroy(dev_priv->objects);
5108}
5109
Chris Wilson461fb992016-05-14 07:26:33 +01005110int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
5111{
5112 struct drm_i915_gem_object *obj;
5113
5114 /* Called just before we write the hibernation image.
5115 *
5116 * We need to update the domain tracking to reflect that the CPU
5117 * will be accessing all the pages to create and restore from the
5118 * hibernation, and so upon restoration those pages will be in the
5119 * CPU domain.
5120 *
5121 * To make sure the hibernation image contains the latest state,
5122 * we update that state just before writing out the image.
5123 */
5124
5125 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
5126 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
5127 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
5128 }
5129
5130 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
5131 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
5132 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
5133 }
5134
5135 return 0;
5136}
5137
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005138void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005139{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005140 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005141
5142 /* Clean up our request list when the client is going away, so that
5143 * later retire_requests won't dereference our soon-to-be-gone
5144 * file_priv.
5145 */
Chris Wilson1c255952010-09-26 11:03:27 +01005146 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005147 while (!list_empty(&file_priv->mm.request_list)) {
5148 struct drm_i915_gem_request *request;
5149
5150 request = list_first_entry(&file_priv->mm.request_list,
5151 struct drm_i915_gem_request,
5152 client_list);
5153 list_del(&request->client_list);
5154 request->file_priv = NULL;
5155 }
Chris Wilson1c255952010-09-26 11:03:27 +01005156 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01005157
Chris Wilson2e1b8732015-04-27 13:41:22 +01005158 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01005159 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01005160 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005161 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005162 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005163}
5164
5165int i915_gem_open(struct drm_device *dev, struct drm_file *file)
5166{
5167 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005168 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005169
5170 DRM_DEBUG_DRIVER("\n");
5171
5172 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5173 if (!file_priv)
5174 return -ENOMEM;
5175
5176 file->driver_priv = file_priv;
5177 file_priv->dev_priv = dev->dev_private;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005178 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01005179 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005180
5181 spin_lock_init(&file_priv->mm.lock);
5182 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005183
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005184 file_priv->bsd_ring = -1;
5185
Ben Widawskye422b882013-12-06 14:10:58 -08005186 ret = i915_gem_context_open(dev, file);
5187 if (ret)
5188 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005189
Ben Widawskye422b882013-12-06 14:10:58 -08005190 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005191}
5192
Daniel Vetterb680c372014-09-19 18:27:27 +02005193/**
5194 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005195 * @old: current GEM buffer for the frontbuffer slots
5196 * @new: new GEM buffer for the frontbuffer slots
5197 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005198 *
5199 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5200 * from @old and setting them in @new. Both @old and @new can be NULL.
5201 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005202void i915_gem_track_fb(struct drm_i915_gem_object *old,
5203 struct drm_i915_gem_object *new,
5204 unsigned frontbuffer_bits)
5205{
5206 if (old) {
5207 WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
5208 WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
5209 old->frontbuffer_bits &= ~frontbuffer_bits;
5210 }
5211
5212 if (new) {
5213 WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
5214 WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
5215 new->frontbuffer_bits |= frontbuffer_bits;
5216 }
5217}
5218
Ben Widawskya70a3142013-07-31 16:59:56 -07005219/* All the new VM stuff */
Michel Thierry088e0df2015-08-07 17:40:17 +01005220u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
5221 struct i915_address_space *vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07005222{
5223 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
5224 struct i915_vma *vma;
5225
Daniel Vetter896ab1a2014-08-06 15:04:51 +02005226 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
Ben Widawskya70a3142013-07-31 16:59:56 -07005227
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005228 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00005229 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005230 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5231 continue;
5232 if (vma->vm == vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07005233 return vma->node.start;
Ben Widawskya70a3142013-07-31 16:59:56 -07005234 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005235
Daniel Vetterf25748ea2014-06-17 22:34:38 +02005236 WARN(1, "%s vma for this object not found.\n",
5237 i915_is_ggtt(vm) ? "global" : "ppgtt");
Ben Widawskya70a3142013-07-31 16:59:56 -07005238 return -1;
5239}
5240
Michel Thierry088e0df2015-08-07 17:40:17 +01005241u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
5242 const struct i915_ggtt_view *view)
Ben Widawskya70a3142013-07-31 16:59:56 -07005243{
5244 struct i915_vma *vma;
5245
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005246 list_for_each_entry(vma, &o->vma_list, obj_link)
Tvrtko Ursulin8aac2222016-04-21 13:04:45 +01005247 if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005248 return vma->node.start;
5249
Tvrtko Ursulin5678ad72015-03-17 14:45:29 +00005250 WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005251 return -1;
5252}
5253
5254bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
5255 struct i915_address_space *vm)
5256{
5257 struct i915_vma *vma;
5258
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005259 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00005260 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005261 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5262 continue;
5263 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
5264 return true;
5265 }
5266
5267 return false;
5268}
5269
5270bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005271 const struct i915_ggtt_view *view)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005272{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005273 struct i915_vma *vma;
5274
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005275 list_for_each_entry(vma, &o->vma_list, obj_link)
Tvrtko Ursulinff5ec222016-04-21 13:04:46 +01005276 if (vma->is_ggtt &&
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005277 i915_ggtt_view_equal(&vma->ggtt_view, view) &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00005278 drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07005279 return true;
5280
5281 return false;
5282}
5283
5284bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
5285{
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01005286 struct i915_vma *vma;
Ben Widawskya70a3142013-07-31 16:59:56 -07005287
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005288 list_for_each_entry(vma, &o->vma_list, obj_link)
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01005289 if (drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07005290 return true;
5291
5292 return false;
5293}
5294
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01005295unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
Ben Widawskya70a3142013-07-31 16:59:56 -07005296{
Ben Widawskya70a3142013-07-31 16:59:56 -07005297 struct i915_vma *vma;
5298
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01005299 GEM_BUG_ON(list_empty(&o->vma_list));
Ben Widawskya70a3142013-07-31 16:59:56 -07005300
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005301 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00005302 if (vma->is_ggtt &&
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01005303 vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
Ben Widawskya70a3142013-07-31 16:59:56 -07005304 return vma->node.size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005305 }
Tvrtko Ursulin8da32722016-04-21 13:04:43 +01005306
Ben Widawskya70a3142013-07-31 16:59:56 -07005307 return 0;
5308}
5309
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005310bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07005311{
5312 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005313 list_for_each_entry(vma, &obj->vma_list, obj_link)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005314 if (vma->pin_count > 0)
5315 return true;
Joonas Lahtinena6631ae2015-05-06 14:34:58 +03005316
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005317 return false;
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07005318}
Dave Gordonea702992015-07-09 19:29:02 +01005319
Dave Gordon033908a2015-12-10 18:51:23 +00005320/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5321struct page *
5322i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
5323{
5324 struct page *page;
5325
5326 /* Only default objects have per-page dirty tracking */
Chris Wilsonde472662016-01-22 18:32:31 +00005327 if (WARN_ON((obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE) == 0))
Dave Gordon033908a2015-12-10 18:51:23 +00005328 return NULL;
5329
5330 page = i915_gem_object_get_page(obj, n);
5331 set_page_dirty(page);
5332 return page;
5333}
5334
Dave Gordonea702992015-07-09 19:29:02 +01005335/* Allocate a new GEM object and fill it with the supplied data */
5336struct drm_i915_gem_object *
5337i915_gem_object_create_from_data(struct drm_device *dev,
5338 const void *data, size_t size)
5339{
5340 struct drm_i915_gem_object *obj;
5341 struct sg_table *sg;
5342 size_t bytes;
5343 int ret;
5344
Dave Gordond37cd8a2016-04-22 19:14:32 +01005345 obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
Chris Wilsonfe3db792016-04-25 13:32:13 +01005346 if (IS_ERR(obj))
Dave Gordonea702992015-07-09 19:29:02 +01005347 return obj;
5348
5349 ret = i915_gem_object_set_to_cpu_domain(obj, true);
5350 if (ret)
5351 goto fail;
5352
5353 ret = i915_gem_object_get_pages(obj);
5354 if (ret)
5355 goto fail;
5356
5357 i915_gem_object_pin_pages(obj);
5358 sg = obj->pages;
5359 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Dave Gordon9e7d18c2015-12-10 18:51:24 +00005360 obj->dirty = 1; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01005361 i915_gem_object_unpin_pages(obj);
5362
5363 if (WARN_ON(bytes != size)) {
5364 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
5365 ret = -EFAULT;
5366 goto fail;
5367 }
5368
5369 return obj;
5370
5371fail:
5372 drm_gem_object_unreference(&obj->base);
5373 return ERR_PTR(ret);
5374}