blob: 6dc2585aefea856a39824e94fd0070ae0dab4502 [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"
Hugh Dickins5949eac2011-06-27 16:18:18 -070035#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070037#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020039#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070040
Chris Wilson05394f32010-11-08 19:18:58 +000041static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
Daniel Vettere62b59e2015-01-21 14:53:48 +010042static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilsonc8725f32014-03-17 12:21:55 +000043static void
Chris Wilsonb4716182015-04-27 13:41:17 +010044i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
45static void
46i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
Chris Wilson61050802012-04-17 15:31:31 +010047
Chris Wilsonc76ce032013-08-08 14:41:03 +010048static bool cpu_cache_is_coherent(struct drm_device *dev,
49 enum i915_cache_level level)
50{
51 return HAS_LLC(dev) || level != I915_CACHE_NONE;
52}
53
Chris Wilson2c225692013-08-09 12:26:45 +010054static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
55{
56 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
57 return true;
58
59 return obj->pin_display;
60}
61
Chris Wilson73aa8082010-09-30 11:46:12 +010062/* some bookkeeping */
63static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
64 size_t size)
65{
Daniel Vetterc20e8352013-07-24 22:40:23 +020066 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010067 dev_priv->mm.object_count++;
68 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020069 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010070}
71
72static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
73 size_t size)
74{
Daniel Vetterc20e8352013-07-24 22:40:23 +020075 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010076 dev_priv->mm.object_count--;
77 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020078 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010079}
80
Chris Wilson21dd3732011-01-26 15:55:56 +000081static int
Daniel Vetter33196de2012-11-14 17:14:05 +010082i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010083{
Chris Wilson30dbf0c2010-09-25 10:19:17 +010084 int ret;
85
Daniel Vetter7abb6902013-05-24 21:29:32 +020086#define EXIT_COND (!i915_reset_in_progress(error) || \
87 i915_terminally_wedged(error))
Daniel Vetter1f83fee2012-11-15 17:17:22 +010088 if (EXIT_COND)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010089 return 0;
90
Daniel Vetter0a6759c2012-07-04 22:18:41 +020091 /*
92 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
93 * userspace. If it takes that long something really bad is going on and
94 * we should simply try to bail out and fail as gracefully as possible.
95 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +010096 ret = wait_event_interruptible_timeout(error->reset_queue,
97 EXIT_COND,
98 10*HZ);
Daniel Vetter0a6759c2012-07-04 22:18:41 +020099 if (ret == 0) {
100 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
101 return -EIO;
102 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100103 return ret;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200104 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100105#undef EXIT_COND
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100106
Chris Wilson21dd3732011-01-26 15:55:56 +0000107 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100108}
109
Chris Wilson54cf91d2010-11-25 18:00:26 +0000110int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100111{
Daniel Vetter33196de2012-11-14 17:14:05 +0100112 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100113 int ret;
114
Daniel Vetter33196de2012-11-14 17:14:05 +0100115 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100116 if (ret)
117 return ret;
118
119 ret = mutex_lock_interruptible(&dev->struct_mutex);
120 if (ret)
121 return ret;
122
Chris Wilson23bc5982010-09-29 16:10:57 +0100123 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100124 return 0;
125}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100126
Eric Anholt673a3942008-07-30 12:06:12 -0700127int
Eric Anholt5a125c32008-10-22 21:40:13 -0700128i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000129 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700130{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300131 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +0200132 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300133 struct drm_i915_gem_get_aperture *args = data;
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100134 struct i915_vma *vma;
Chris Wilson6299f992010-11-24 12:23:44 +0000135 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700136
Chris Wilson6299f992010-11-24 12:23:44 +0000137 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100138 mutex_lock(&dev->struct_mutex);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000139 list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100140 if (vma->pin_count)
141 pinned += vma->node.size;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000142 list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
Tvrtko Ursulinca1543b2015-07-01 11:51:10 +0100143 if (vma->pin_count)
144 pinned += vma->node.size;
Chris Wilson73aa8082010-09-30 11:46:12 +0100145 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700146
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300147 args->aper_size = ggtt->base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400148 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000149
Eric Anholt5a125c32008-10-22 21:40:13 -0700150 return 0;
151}
152
Chris Wilson6a2c4232014-11-04 04:51:40 -0800153static int
154i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100155{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800156 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
157 char *vaddr = obj->phys_handle->vaddr;
158 struct sg_table *st;
159 struct scatterlist *sg;
160 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100161
Chris Wilson6a2c4232014-11-04 04:51:40 -0800162 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
163 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100164
Chris Wilson6a2c4232014-11-04 04:51:40 -0800165 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
166 struct page *page;
167 char *src;
168
169 page = shmem_read_mapping_page(mapping, i);
170 if (IS_ERR(page))
171 return PTR_ERR(page);
172
173 src = kmap_atomic(page);
174 memcpy(vaddr, src, PAGE_SIZE);
175 drm_clflush_virt_range(vaddr, PAGE_SIZE);
176 kunmap_atomic(src);
177
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300178 put_page(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800179 vaddr += PAGE_SIZE;
180 }
181
182 i915_gem_chipset_flush(obj->base.dev);
183
184 st = kmalloc(sizeof(*st), GFP_KERNEL);
185 if (st == NULL)
186 return -ENOMEM;
187
188 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
189 kfree(st);
190 return -ENOMEM;
191 }
192
193 sg = st->sgl;
194 sg->offset = 0;
195 sg->length = obj->base.size;
196
197 sg_dma_address(sg) = obj->phys_handle->busaddr;
198 sg_dma_len(sg) = obj->base.size;
199
200 obj->pages = st;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800201 return 0;
202}
203
204static void
205i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
206{
207 int ret;
208
209 BUG_ON(obj->madv == __I915_MADV_PURGED);
210
211 ret = i915_gem_object_set_to_cpu_domain(obj, true);
212 if (ret) {
213 /* In the event of a disaster, abandon all caches and
214 * hope for the best.
215 */
216 WARN_ON(ret != -EIO);
217 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
218 }
219
220 if (obj->madv == I915_MADV_DONTNEED)
221 obj->dirty = 0;
222
223 if (obj->dirty) {
Chris Wilson00731152014-05-21 12:42:56 +0100224 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800225 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100226 int i;
227
228 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800229 struct page *page;
230 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100231
Chris Wilson6a2c4232014-11-04 04:51:40 -0800232 page = shmem_read_mapping_page(mapping, i);
233 if (IS_ERR(page))
234 continue;
235
236 dst = kmap_atomic(page);
237 drm_clflush_virt_range(vaddr, PAGE_SIZE);
238 memcpy(dst, vaddr, PAGE_SIZE);
239 kunmap_atomic(dst);
240
241 set_page_dirty(page);
242 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100243 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300244 put_page(page);
Chris Wilson00731152014-05-21 12:42:56 +0100245 vaddr += PAGE_SIZE;
246 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800247 obj->dirty = 0;
Chris Wilson00731152014-05-21 12:42:56 +0100248 }
249
Chris Wilson6a2c4232014-11-04 04:51:40 -0800250 sg_free_table(obj->pages);
251 kfree(obj->pages);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800252}
253
254static void
255i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
256{
257 drm_pci_free(obj->base.dev, obj->phys_handle);
258}
259
260static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
261 .get_pages = i915_gem_object_get_pages_phys,
262 .put_pages = i915_gem_object_put_pages_phys,
263 .release = i915_gem_object_release_phys,
264};
265
266static int
267drop_pages(struct drm_i915_gem_object *obj)
268{
269 struct i915_vma *vma, *next;
270 int ret;
271
272 drm_gem_object_reference(&obj->base);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +0000273 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link)
Chris Wilson6a2c4232014-11-04 04:51:40 -0800274 if (i915_vma_unbind(vma))
275 break;
276
277 ret = i915_gem_object_put_pages(obj);
278 drm_gem_object_unreference(&obj->base);
279
280 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100281}
282
283int
284i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
285 int align)
286{
287 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800288 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100289
290 if (obj->phys_handle) {
291 if ((unsigned long)obj->phys_handle->vaddr & (align -1))
292 return -EBUSY;
293
294 return 0;
295 }
296
297 if (obj->madv != I915_MADV_WILLNEED)
298 return -EFAULT;
299
300 if (obj->base.filp == NULL)
301 return -EINVAL;
302
Chris Wilson6a2c4232014-11-04 04:51:40 -0800303 ret = drop_pages(obj);
304 if (ret)
305 return ret;
306
Chris Wilson00731152014-05-21 12:42:56 +0100307 /* create a new object */
308 phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
309 if (!phys)
310 return -ENOMEM;
311
Chris Wilson00731152014-05-21 12:42:56 +0100312 obj->phys_handle = phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800313 obj->ops = &i915_gem_phys_ops;
314
315 return i915_gem_object_get_pages(obj);
Chris Wilson00731152014-05-21 12:42:56 +0100316}
317
318static int
319i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
320 struct drm_i915_gem_pwrite *args,
321 struct drm_file *file_priv)
322{
323 struct drm_device *dev = obj->base.dev;
324 void *vaddr = obj->phys_handle->vaddr + args->offset;
325 char __user *user_data = to_user_ptr(args->data_ptr);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200326 int ret = 0;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800327
328 /* We manually control the domain here and pretend that it
329 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
330 */
331 ret = i915_gem_object_wait_rendering(obj, false);
332 if (ret)
333 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100334
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700335 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Chris Wilson00731152014-05-21 12:42:56 +0100336 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
337 unsigned long unwritten;
338
339 /* The physical object once assigned is fixed for the lifetime
340 * of the obj, so we can safely drop the lock and continue
341 * to access vaddr.
342 */
343 mutex_unlock(&dev->struct_mutex);
344 unwritten = copy_from_user(vaddr, user_data, args->size);
345 mutex_lock(&dev->struct_mutex);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200346 if (unwritten) {
347 ret = -EFAULT;
348 goto out;
349 }
Chris Wilson00731152014-05-21 12:42:56 +0100350 }
351
Chris Wilson6a2c4232014-11-04 04:51:40 -0800352 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson00731152014-05-21 12:42:56 +0100353 i915_gem_chipset_flush(dev);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200354
355out:
Rodrigo Vivide152b62015-07-07 16:28:51 -0700356 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200357 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100358}
359
Chris Wilson42dcedd2012-11-15 11:32:30 +0000360void *i915_gem_object_alloc(struct drm_device *dev)
361{
362 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonefab6d82015-04-07 16:20:57 +0100363 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000364}
365
366void i915_gem_object_free(struct drm_i915_gem_object *obj)
367{
368 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsonefab6d82015-04-07 16:20:57 +0100369 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000370}
371
Dave Airlieff72145b2011-02-07 12:16:14 +1000372static int
373i915_gem_create(struct drm_file *file,
374 struct drm_device *dev,
375 uint64_t size,
376 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700377{
Chris Wilson05394f32010-11-08 19:18:58 +0000378 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300379 int ret;
380 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700381
Dave Airlieff72145b2011-02-07 12:16:14 +1000382 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200383 if (size == 0)
384 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700385
386 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000387 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700388 if (obj == NULL)
389 return -ENOMEM;
390
Chris Wilson05394f32010-11-08 19:18:58 +0000391 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100392 /* drop reference from allocate - handle holds it now */
Daniel Vetterd861e332013-07-24 23:25:03 +0200393 drm_gem_object_unreference_unlocked(&obj->base);
394 if (ret)
395 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100396
Dave Airlieff72145b2011-02-07 12:16:14 +1000397 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700398 return 0;
399}
400
Dave Airlieff72145b2011-02-07 12:16:14 +1000401int
402i915_gem_dumb_create(struct drm_file *file,
403 struct drm_device *dev,
404 struct drm_mode_create_dumb *args)
405{
406 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300407 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000408 args->size = args->pitch * args->height;
409 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000410 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000411}
412
Dave Airlieff72145b2011-02-07 12:16:14 +1000413/**
414 * Creates a new mm object and returns a handle to it.
415 */
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
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200606 user_data = 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.
678 *
679 * On error, the contents of *data are undefined.
680 */
681int
682i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000683 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700684{
685 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000686 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100687 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700688
Chris Wilson51311d02010-11-17 09:10:42 +0000689 if (args->size == 0)
690 return 0;
691
692 if (!access_ok(VERIFY_WRITE,
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200693 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000694 args->size))
695 return -EFAULT;
696
Chris Wilson4f27b752010-10-14 15:26:45 +0100697 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100698 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100699 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700700
Chris Wilson05394f32010-11-08 19:18:58 +0000701 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000702 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100703 ret = -ENOENT;
704 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100705 }
Eric Anholt673a3942008-07-30 12:06:12 -0700706
Chris Wilson7dcd2492010-09-26 20:21:44 +0100707 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000708 if (args->offset > obj->base.size ||
709 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100710 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100711 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100712 }
713
Daniel Vetter1286ff72012-05-10 15:25:09 +0200714 /* prime objects have no backing filp to GEM pread/pwrite
715 * pages from.
716 */
717 if (!obj->base.filp) {
718 ret = -EINVAL;
719 goto out;
720 }
721
Chris Wilsondb53a302011-02-03 11:57:46 +0000722 trace_i915_gem_object_pread(obj, args->offset, args->size);
723
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200724 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700725
Chris Wilson35b62a82010-09-26 20:23:38 +0100726out:
Chris Wilson05394f32010-11-08 19:18:58 +0000727 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100728unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100729 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700730 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700731}
732
Keith Packard0839ccb2008-10-30 19:38:48 -0700733/* This is the fast write path which cannot handle
734 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700735 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700736
Keith Packard0839ccb2008-10-30 19:38:48 -0700737static inline int
738fast_user_write(struct io_mapping *mapping,
739 loff_t page_base, int page_offset,
740 char __user *user_data,
741 int length)
742{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700743 void __iomem *vaddr_atomic;
744 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700745 unsigned long unwritten;
746
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700747 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700748 /* We can use the cpu mem copy function because this is X86. */
749 vaddr = (void __force*)vaddr_atomic + page_offset;
750 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700751 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700752 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100753 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700754}
755
Eric Anholt3de09aa2009-03-09 09:42:23 -0700756/**
757 * This is the fast pwrite path, where we copy the data directly from the
758 * user into the GTT, uncached.
759 */
Eric Anholt673a3942008-07-30 12:06:12 -0700760static int
Chris Wilson05394f32010-11-08 19:18:58 +0000761i915_gem_gtt_pwrite_fast(struct drm_device *dev,
762 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700763 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000764 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700765{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300766 struct drm_i915_private *dev_priv = to_i915(dev);
767 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Eric Anholt673a3942008-07-30 12:06:12 -0700768 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700769 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700770 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200771 int page_offset, page_length, ret;
772
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100773 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200774 if (ret)
775 goto out;
776
777 ret = i915_gem_object_set_to_gtt_domain(obj, true);
778 if (ret)
779 goto out_unpin;
780
781 ret = i915_gem_object_put_fence(obj);
782 if (ret)
783 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700784
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200785 user_data = to_user_ptr(args->data_ptr);
Eric Anholt673a3942008-07-30 12:06:12 -0700786 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700787
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700788 offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700789
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700790 intel_fb_obj_invalidate(obj, ORIGIN_GTT);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200791
Eric Anholt673a3942008-07-30 12:06:12 -0700792 while (remain > 0) {
793 /* Operation in this page
794 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700795 * page_base = page offset within aperture
796 * page_offset = offset within page
797 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700798 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100799 page_base = offset & PAGE_MASK;
800 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700801 page_length = remain;
802 if ((page_offset + remain) > PAGE_SIZE)
803 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700804
Keith Packard0839ccb2008-10-30 19:38:48 -0700805 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700806 * source page isn't available. Return the error and we'll
807 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700808 */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300809 if (fast_user_write(ggtt->mappable, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200810 page_offset, user_data, page_length)) {
811 ret = -EFAULT;
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200812 goto out_flush;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200813 }
Eric Anholt673a3942008-07-30 12:06:12 -0700814
Keith Packard0839ccb2008-10-30 19:38:48 -0700815 remain -= page_length;
816 user_data += page_length;
817 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700818 }
Eric Anholt673a3942008-07-30 12:06:12 -0700819
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200820out_flush:
Rodrigo Vivide152b62015-07-07 16:28:51 -0700821 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200822out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800823 i915_gem_object_ggtt_unpin(obj);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200824out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700825 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700826}
827
Daniel Vetterd174bd62012-03-25 19:47:40 +0200828/* Per-page copy function for the shmem pwrite fastpath.
829 * Flushes invalid cachelines before writing to the target if
830 * needs_clflush_before is set and flushes out any written cachelines after
831 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700832static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200833shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
834 char __user *user_data,
835 bool page_do_bit17_swizzling,
836 bool needs_clflush_before,
837 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700838{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200839 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700840 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700841
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200842 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200843 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700844
Daniel Vetterd174bd62012-03-25 19:47:40 +0200845 vaddr = kmap_atomic(page);
846 if (needs_clflush_before)
847 drm_clflush_virt_range(vaddr + shmem_page_offset,
848 page_length);
Chris Wilsonc2831a92014-03-07 08:30:37 +0000849 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
850 user_data, page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200851 if (needs_clflush_after)
852 drm_clflush_virt_range(vaddr + shmem_page_offset,
853 page_length);
854 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700855
Chris Wilson755d2212012-09-04 21:02:55 +0100856 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700857}
858
Daniel Vetterd174bd62012-03-25 19:47:40 +0200859/* Only difference to the fast-path function is that this can handle bit17
860 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700861static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200862shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
863 char __user *user_data,
864 bool page_do_bit17_swizzling,
865 bool needs_clflush_before,
866 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700867{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200868 char *vaddr;
869 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700870
Daniel Vetterd174bd62012-03-25 19:47:40 +0200871 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200872 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200873 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
874 page_length,
875 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200876 if (page_do_bit17_swizzling)
877 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100878 user_data,
879 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200880 else
881 ret = __copy_from_user(vaddr + shmem_page_offset,
882 user_data,
883 page_length);
884 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200885 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
886 page_length,
887 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200888 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100889
Chris Wilson755d2212012-09-04 21:02:55 +0100890 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700891}
892
Eric Anholt40123c12009-03-09 13:42:30 -0700893static int
Daniel Vettere244a442012-03-25 19:47:28 +0200894i915_gem_shmem_pwrite(struct drm_device *dev,
895 struct drm_i915_gem_object *obj,
896 struct drm_i915_gem_pwrite *args,
897 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700898{
Eric Anholt40123c12009-03-09 13:42:30 -0700899 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100900 loff_t offset;
901 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100902 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100903 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200904 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200905 int needs_clflush_after = 0;
906 int needs_clflush_before = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200907 struct sg_page_iter sg_iter;
Eric Anholt40123c12009-03-09 13:42:30 -0700908
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200909 user_data = to_user_ptr(args->data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700910 remain = args->size;
911
Daniel Vetter8c599672011-12-14 13:57:31 +0100912 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700913
Daniel Vetter58642882012-03-25 19:47:37 +0200914 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
915 /* If we're not in the cpu write domain, set ourself into the gtt
916 * write domain and manually flush cachelines (if required). This
917 * optimizes for the case when the gpu will use the data
918 * right away and we therefore have to clflush anyway. */
Chris Wilson2c225692013-08-09 12:26:45 +0100919 needs_clflush_after = cpu_write_needs_clflush(obj);
Ben Widawsky23f54482013-09-11 14:57:48 -0700920 ret = i915_gem_object_wait_rendering(obj, false);
921 if (ret)
922 return ret;
Daniel Vetter58642882012-03-25 19:47:37 +0200923 }
Chris Wilsonc76ce032013-08-08 14:41:03 +0100924 /* Same trick applies to invalidate partially written cachelines read
925 * before writing. */
926 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
927 needs_clflush_before =
928 !cpu_cache_is_coherent(dev, obj->cache_level);
Daniel Vetter58642882012-03-25 19:47:37 +0200929
Chris Wilson755d2212012-09-04 21:02:55 +0100930 ret = i915_gem_object_get_pages(obj);
931 if (ret)
932 return ret;
933
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -0700934 intel_fb_obj_invalidate(obj, ORIGIN_CPU);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200935
Chris Wilson755d2212012-09-04 21:02:55 +0100936 i915_gem_object_pin_pages(obj);
937
Eric Anholt40123c12009-03-09 13:42:30 -0700938 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000939 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700940
Imre Deak67d5a502013-02-18 19:28:02 +0200941 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
942 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200943 struct page *page = sg_page_iter_page(&sg_iter);
Daniel Vetter58642882012-03-25 19:47:37 +0200944 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100945
Chris Wilson9da3da62012-06-01 15:20:22 +0100946 if (remain <= 0)
947 break;
948
Eric Anholt40123c12009-03-09 13:42:30 -0700949 /* Operation in this page
950 *
Eric Anholt40123c12009-03-09 13:42:30 -0700951 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700952 * page_length = bytes to copy for this page
953 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100954 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700955
956 page_length = remain;
957 if ((shmem_page_offset + page_length) > PAGE_SIZE)
958 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700959
Daniel Vetter58642882012-03-25 19:47:37 +0200960 /* If we don't overwrite a cacheline completely we need to be
961 * careful to have up-to-date data by first clflushing. Don't
962 * overcomplicate things and flush the entire patch. */
963 partial_cacheline_write = needs_clflush_before &&
964 ((shmem_page_offset | page_length)
965 & (boot_cpu_data.x86_clflush_size - 1));
966
Daniel Vetter8c599672011-12-14 13:57:31 +0100967 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
968 (page_to_phys(page) & (1 << 17)) != 0;
969
Daniel Vetterd174bd62012-03-25 19:47:40 +0200970 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
971 user_data, page_do_bit17_swizzling,
972 partial_cacheline_write,
973 needs_clflush_after);
974 if (ret == 0)
975 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700976
Daniel Vettere244a442012-03-25 19:47:28 +0200977 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +0200978 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200979 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
980 user_data, page_do_bit17_swizzling,
981 partial_cacheline_write,
982 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700983
Daniel Vettere244a442012-03-25 19:47:28 +0200984 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +0100985
Chris Wilson755d2212012-09-04 21:02:55 +0100986 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +0100987 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +0100988
Chris Wilson17793c92014-03-07 08:30:36 +0000989next_page:
Eric Anholt40123c12009-03-09 13:42:30 -0700990 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100991 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700992 offset += page_length;
993 }
994
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100995out:
Chris Wilson755d2212012-09-04 21:02:55 +0100996 i915_gem_object_unpin_pages(obj);
997
Daniel Vettere244a442012-03-25 19:47:28 +0200998 if (hit_slowpath) {
Daniel Vetter8dcf0152012-11-15 16:53:58 +0100999 /*
1000 * Fixup: Flush cpu caches in case we didn't flush the dirty
1001 * cachelines in-line while writing and the object moved
1002 * out of the cpu write domain while we've dropped the lock.
1003 */
1004 if (!needs_clflush_after &&
1005 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilson000433b2013-08-08 14:41:09 +01001006 if (i915_gem_clflush_object(obj, obj->pin_display))
Ville Syrjäläed75a552015-08-11 19:47:10 +03001007 needs_clflush_after = true;
Daniel Vettere244a442012-03-25 19:47:28 +02001008 }
Daniel Vetter8c599672011-12-14 13:57:31 +01001009 }
Eric Anholt40123c12009-03-09 13:42:30 -07001010
Daniel Vetter58642882012-03-25 19:47:37 +02001011 if (needs_clflush_after)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001012 i915_gem_chipset_flush(dev);
Ville Syrjäläed75a552015-08-11 19:47:10 +03001013 else
1014 obj->cache_dirty = true;
Daniel Vetter58642882012-03-25 19:47:37 +02001015
Rodrigo Vivide152b62015-07-07 16:28:51 -07001016 intel_fb_obj_flush(obj, false, ORIGIN_CPU);
Eric Anholt40123c12009-03-09 13:42:30 -07001017 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001018}
1019
1020/**
1021 * Writes data to the object referenced by handle.
1022 *
1023 * On error, the contents of the buffer that were to be modified are undefined.
1024 */
1025int
1026i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001027 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001028{
Imre Deak5d77d9c2014-11-12 16:40:35 +02001029 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001030 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001031 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001032 int ret;
1033
1034 if (args->size == 0)
1035 return 0;
1036
1037 if (!access_ok(VERIFY_READ,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001038 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001039 args->size))
1040 return -EFAULT;
1041
Jani Nikulad330a952014-01-21 11:24:25 +02001042 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001043 ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
1044 args->size);
1045 if (ret)
1046 return -EFAULT;
1047 }
Eric Anholt673a3942008-07-30 12:06:12 -07001048
Imre Deak5d77d9c2014-11-12 16:40:35 +02001049 intel_runtime_pm_get(dev_priv);
1050
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001051 ret = i915_mutex_lock_interruptible(dev);
1052 if (ret)
Imre Deak5d77d9c2014-11-12 16:40:35 +02001053 goto put_rpm;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001054
Chris Wilson05394f32010-11-08 19:18:58 +00001055 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001056 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001057 ret = -ENOENT;
1058 goto unlock;
1059 }
Eric Anholt673a3942008-07-30 12:06:12 -07001060
Chris Wilson7dcd2492010-09-26 20:21:44 +01001061 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001062 if (args->offset > obj->base.size ||
1063 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001064 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001065 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001066 }
1067
Daniel Vetter1286ff72012-05-10 15:25:09 +02001068 /* prime objects have no backing filp to GEM pread/pwrite
1069 * pages from.
1070 */
1071 if (!obj->base.filp) {
1072 ret = -EINVAL;
1073 goto out;
1074 }
1075
Chris Wilsondb53a302011-02-03 11:57:46 +00001076 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1077
Daniel Vetter935aaa62012-03-25 19:47:35 +02001078 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001079 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1080 * it would end up going through the fenced access, and we'll get
1081 * different detiling behavior between reading and writing.
1082 * pread/pwrite currently are reading and writing from the CPU
1083 * perspective, requiring manual detiling by the client.
1084 */
Chris Wilson2c225692013-08-09 12:26:45 +01001085 if (obj->tiling_mode == I915_TILING_NONE &&
1086 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
1087 cpu_write_needs_clflush(obj)) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001088 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001089 /* Note that the gtt paths might fail with non-page-backed user
1090 * pointers (e.g. gtt mappings when moving data between
1091 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -07001092 }
Eric Anholt673a3942008-07-30 12:06:12 -07001093
Chris Wilson6a2c4232014-11-04 04:51:40 -08001094 if (ret == -EFAULT || ret == -ENOSPC) {
1095 if (obj->phys_handle)
1096 ret = i915_gem_phys_pwrite(obj, args, file);
1097 else
1098 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
1099 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001100
Chris Wilson35b62a82010-09-26 20:23:38 +01001101out:
Chris Wilson05394f32010-11-08 19:18:58 +00001102 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001103unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001104 mutex_unlock(&dev->struct_mutex);
Imre Deak5d77d9c2014-11-12 16:40:35 +02001105put_rpm:
1106 intel_runtime_pm_put(dev_priv);
1107
Eric Anholt673a3942008-07-30 12:06:12 -07001108 return ret;
1109}
1110
Chris Wilsonb3612372012-08-24 09:35:08 +01001111int
Daniel Vetter33196de2012-11-14 17:14:05 +01001112i915_gem_check_wedge(struct i915_gpu_error *error,
Chris Wilsonb3612372012-08-24 09:35:08 +01001113 bool interruptible)
1114{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001115 if (i915_reset_in_progress(error)) {
Chris Wilsonb3612372012-08-24 09:35:08 +01001116 /* Non-interruptible callers can't handle -EAGAIN, hence return
1117 * -EIO unconditionally for these. */
1118 if (!interruptible)
1119 return -EIO;
1120
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001121 /* Recovery complete, but the reset failed ... */
1122 if (i915_terminally_wedged(error))
Chris Wilsonb3612372012-08-24 09:35:08 +01001123 return -EIO;
1124
McAulay, Alistair6689c162014-08-15 18:51:35 +01001125 /*
1126 * Check if GPU Reset is in progress - we need intel_ring_begin
1127 * to work properly to reinit the hw state while the gpu is
1128 * still marked as reset-in-progress. Handle this with a flag.
1129 */
1130 if (!error->reload_in_reset)
1131 return -EAGAIN;
Chris Wilsonb3612372012-08-24 09:35:08 +01001132 }
1133
1134 return 0;
1135}
1136
Chris Wilson094f9a52013-09-25 17:34:55 +01001137static void fake_irq(unsigned long data)
1138{
1139 wake_up_process((struct task_struct *)data);
1140}
1141
1142static bool missed_irq(struct drm_i915_private *dev_priv,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001143 struct intel_engine_cs *engine)
Chris Wilson094f9a52013-09-25 17:34:55 +01001144{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00001145 return test_bit(engine->id, &dev_priv->gpu_error.missed_irq_rings);
Chris Wilson094f9a52013-09-25 17:34:55 +01001146}
1147
Chris Wilsonca5b7212015-12-11 11:32:58 +00001148static unsigned long local_clock_us(unsigned *cpu)
1149{
1150 unsigned long t;
1151
1152 /* Cheaply and approximately convert from nanoseconds to microseconds.
1153 * The result and subsequent calculations are also defined in the same
1154 * approximate microseconds units. The principal source of timing
1155 * error here is from the simple truncation.
1156 *
1157 * Note that local_clock() is only defined wrt to the current CPU;
1158 * the comparisons are no longer valid if we switch CPUs. Instead of
1159 * blocking preemption for the entire busywait, we can detect the CPU
1160 * switch and use that as indicator of system load and a reason to
1161 * stop busywaiting, see busywait_stop().
1162 */
1163 *cpu = get_cpu();
1164 t = local_clock() >> 10;
1165 put_cpu();
1166
1167 return t;
1168}
1169
1170static bool busywait_stop(unsigned long timeout, unsigned cpu)
1171{
1172 unsigned this_cpu;
1173
1174 if (time_after(local_clock_us(&this_cpu), timeout))
1175 return true;
1176
1177 return this_cpu != cpu;
1178}
1179
Chris Wilson91b0c352015-12-11 11:32:57 +00001180static int __i915_spin_request(struct drm_i915_gem_request *req, int state)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001181{
Chris Wilson2def4ad92015-04-07 16:20:41 +01001182 unsigned long timeout;
Chris Wilsonca5b7212015-12-11 11:32:58 +00001183 unsigned cpu;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001184
Chris Wilsonca5b7212015-12-11 11:32:58 +00001185 /* When waiting for high frequency requests, e.g. during synchronous
1186 * rendering split between the CPU and GPU, the finite amount of time
1187 * required to set up the irq and wait upon it limits the response
1188 * rate. By busywaiting on the request completion for a short while we
1189 * can service the high frequency waits as quick as possible. However,
1190 * if it is a slow request, we want to sleep as quickly as possible.
1191 * The tradeoff between waiting and sleeping is roughly the time it
1192 * takes to sleep on a request, on the order of a microsecond.
1193 */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001194
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001195 if (req->engine->irq_refcount)
Chris Wilson2def4ad92015-04-07 16:20:41 +01001196 return -EBUSY;
1197
Chris Wilson821485d2015-12-11 11:32:59 +00001198 /* Only spin if we know the GPU is processing this request */
1199 if (!i915_gem_request_started(req, true))
1200 return -EAGAIN;
1201
Chris Wilsonca5b7212015-12-11 11:32:58 +00001202 timeout = local_clock_us(&cpu) + 5;
Chris Wilson2def4ad92015-04-07 16:20:41 +01001203 while (!need_resched()) {
Daniel Vettereed29a52015-05-21 14:21:25 +02001204 if (i915_gem_request_completed(req, true))
Chris Wilson2def4ad92015-04-07 16:20:41 +01001205 return 0;
1206
Chris Wilson91b0c352015-12-11 11:32:57 +00001207 if (signal_pending_state(state, current))
1208 break;
1209
Chris Wilsonca5b7212015-12-11 11:32:58 +00001210 if (busywait_stop(timeout, cpu))
Chris Wilson2def4ad92015-04-07 16:20:41 +01001211 break;
1212
1213 cpu_relax_lowlatency();
1214 }
Chris Wilson821485d2015-12-11 11:32:59 +00001215
Daniel Vettereed29a52015-05-21 14:21:25 +02001216 if (i915_gem_request_completed(req, false))
Chris Wilson2def4ad92015-04-07 16:20:41 +01001217 return 0;
1218
1219 return -EAGAIN;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001220}
1221
Chris Wilsonb3612372012-08-24 09:35:08 +01001222/**
John Harrison9c654812014-11-24 18:49:35 +00001223 * __i915_wait_request - wait until execution of request has finished
1224 * @req: duh!
1225 * @reset_counter: reset sequence associated with the given request
Chris Wilsonb3612372012-08-24 09:35:08 +01001226 * @interruptible: do an interruptible wait (normally yes)
1227 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1228 *
Daniel Vetterf69061b2012-12-06 09:01:42 +01001229 * Note: It is of utmost importance that the passed in seqno and reset_counter
1230 * values have been read by the caller in an smp safe manner. Where read-side
1231 * locks are involved, it is sufficient to read the reset_counter before
1232 * unlocking the lock that protects the seqno. For lockless tricks, the
1233 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
1234 * inserted.
1235 *
John Harrison9c654812014-11-24 18:49:35 +00001236 * Returns 0 if the request was found within the alloted time. Else returns the
Chris Wilsonb3612372012-08-24 09:35:08 +01001237 * errno with remaining time filled in timeout argument.
1238 */
John Harrison9c654812014-11-24 18:49:35 +00001239int __i915_wait_request(struct drm_i915_gem_request *req,
Daniel Vetterf69061b2012-12-06 09:01:42 +01001240 unsigned reset_counter,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001241 bool interruptible,
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00001242 s64 *timeout,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001243 struct intel_rps_client *rps)
Chris Wilsonb3612372012-08-24 09:35:08 +01001244{
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001245 struct intel_engine_cs *engine = i915_gem_request_get_engine(req);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001246 struct drm_device *dev = engine->dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03001247 struct drm_i915_private *dev_priv = dev->dev_private;
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
1301 * the caller grabbing the seqno and now ... */
Chris Wilson094f9a52013-09-25 17:34:55 +01001302 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
1303 /* ... but upgrade the -EAGAIN to an -EIO if the gpu
1304 * is truely gone. */
1305 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1306 if (ret == 0)
1307 ret = -EAGAIN;
1308 break;
1309 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01001310
John Harrison1b5a4332014-11-24 18:49:42 +00001311 if (i915_gem_request_completed(req, false)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001312 ret = 0;
1313 break;
1314 }
Chris Wilsonb3612372012-08-24 09:35:08 +01001315
Chris Wilson91b0c352015-12-11 11:32:57 +00001316 if (signal_pending_state(state, current)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001317 ret = -ERESTARTSYS;
1318 break;
1319 }
1320
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001321 if (timeout && time_after_eq(jiffies, timeout_expire)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001322 ret = -ETIME;
1323 break;
1324 }
1325
1326 timer.function = NULL;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001327 if (timeout || missed_irq(dev_priv, engine)) {
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001328 unsigned long expire;
1329
Chris Wilson094f9a52013-09-25 17:34:55 +01001330 setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001331 expire = missed_irq(dev_priv, engine) ? jiffies + 1 : timeout_expire;
Chris Wilson094f9a52013-09-25 17:34:55 +01001332 mod_timer(&timer, expire);
1333 }
1334
Chris Wilson5035c272013-10-04 09:58:46 +01001335 io_schedule();
Chris Wilson094f9a52013-09-25 17:34:55 +01001336
Chris Wilson094f9a52013-09-25 17:34:55 +01001337 if (timer.function) {
1338 del_singleshot_timer_sync(&timer);
1339 destroy_timer_on_stack(&timer);
1340 }
1341 }
Mika Kuoppala168c3f22013-12-12 17:54:42 +02001342 if (!irq_test_in_progress)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001343 engine->irq_put(engine);
Chris Wilson094f9a52013-09-25 17:34:55 +01001344
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00001345 finish_wait(&engine->irq_queue, &wait);
Chris Wilsonb3612372012-08-24 09:35:08 +01001346
Chris Wilson2def4ad92015-04-07 16:20:41 +01001347out:
Chris Wilson2def4ad92015-04-07 16:20:41 +01001348 trace_i915_gem_request_wait_end(req);
1349
Chris Wilsonb3612372012-08-24 09:35:08 +01001350 if (timeout) {
Tvrtko Ursuline0313db2016-01-15 15:11:12 +00001351 s64 tres = *timeout - (ktime_get_raw_ns() - before);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00001352
1353 *timeout = tres < 0 ? 0 : tres;
Daniel Vetter9cca3062014-11-28 10:29:55 +01001354
1355 /*
1356 * Apparently ktime isn't accurate enough and occasionally has a
1357 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
1358 * things up to make the test happy. We allow up to 1 jiffy.
1359 *
1360 * This is a regrssion from the timespec->ktime conversion.
1361 */
1362 if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
1363 *timeout = 0;
Chris Wilsonb3612372012-08-24 09:35:08 +01001364 }
1365
Chris Wilson094f9a52013-09-25 17:34:55 +01001366 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001367}
1368
John Harrisonfcfa423c2015-05-29 17:44:12 +01001369int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
1370 struct drm_file *file)
1371{
John Harrisonfcfa423c2015-05-29 17:44:12 +01001372 struct drm_i915_file_private *file_priv;
1373
1374 WARN_ON(!req || !file || req->file_priv);
1375
1376 if (!req || !file)
1377 return -EINVAL;
1378
1379 if (req->file_priv)
1380 return -EINVAL;
1381
John Harrisonfcfa423c2015-05-29 17:44:12 +01001382 file_priv = file->driver_priv;
1383
1384 spin_lock(&file_priv->mm.lock);
1385 req->file_priv = file_priv;
1386 list_add_tail(&req->client_list, &file_priv->mm.request_list);
1387 spin_unlock(&file_priv->mm.lock);
1388
1389 req->pid = get_pid(task_pid(current));
1390
1391 return 0;
1392}
1393
Chris Wilsonb4716182015-04-27 13:41:17 +01001394static inline void
1395i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1396{
1397 struct drm_i915_file_private *file_priv = request->file_priv;
1398
1399 if (!file_priv)
1400 return;
1401
1402 spin_lock(&file_priv->mm.lock);
1403 list_del(&request->client_list);
1404 request->file_priv = NULL;
1405 spin_unlock(&file_priv->mm.lock);
John Harrisonfcfa423c2015-05-29 17:44:12 +01001406
1407 put_pid(request->pid);
1408 request->pid = NULL;
Chris Wilsonb4716182015-04-27 13:41:17 +01001409}
1410
1411static void i915_gem_request_retire(struct drm_i915_gem_request *request)
1412{
1413 trace_i915_gem_request_retire(request);
1414
1415 /* We know the GPU must have read the request to have
1416 * sent us the seqno + interrupt, so use the position
1417 * of tail of the request to update the last known position
1418 * of the GPU head.
1419 *
1420 * Note this requires that we are always called in request
1421 * completion order.
1422 */
1423 request->ringbuf->last_retired_head = request->postfix;
1424
1425 list_del_init(&request->list);
1426 i915_gem_request_remove_from_client(request);
1427
Chris Wilsonb4716182015-04-27 13:41:17 +01001428 i915_gem_request_unreference(request);
1429}
1430
1431static void
1432__i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
1433{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001434 struct intel_engine_cs *engine = req->engine;
Chris Wilsonb4716182015-04-27 13:41:17 +01001435 struct drm_i915_gem_request *tmp;
1436
1437 lockdep_assert_held(&engine->dev->struct_mutex);
1438
1439 if (list_empty(&req->list))
1440 return;
1441
1442 do {
1443 tmp = list_first_entry(&engine->request_list,
1444 typeof(*tmp), list);
1445
1446 i915_gem_request_retire(tmp);
1447 } while (tmp != req);
1448
1449 WARN_ON(i915_verify_lists(engine->dev));
1450}
1451
Chris Wilsonb3612372012-08-24 09:35:08 +01001452/**
Daniel Vettera4b3a572014-11-26 14:17:05 +01001453 * Waits for a request to be signaled, and cleans up the
Chris Wilsonb3612372012-08-24 09:35:08 +01001454 * request and object lists appropriately for that event.
1455 */
1456int
Daniel Vettera4b3a572014-11-26 14:17:05 +01001457i915_wait_request(struct drm_i915_gem_request *req)
Chris Wilsonb3612372012-08-24 09:35:08 +01001458{
Daniel Vettera4b3a572014-11-26 14:17:05 +01001459 struct drm_device *dev;
1460 struct drm_i915_private *dev_priv;
1461 bool interruptible;
Chris Wilsonb3612372012-08-24 09:35:08 +01001462 int ret;
1463
Daniel Vettera4b3a572014-11-26 14:17:05 +01001464 BUG_ON(req == NULL);
1465
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001466 dev = req->engine->dev;
Daniel Vettera4b3a572014-11-26 14:17:05 +01001467 dev_priv = dev->dev_private;
1468 interruptible = dev_priv->mm.interruptible;
1469
Chris Wilsonb3612372012-08-24 09:35:08 +01001470 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
Chris Wilsonb3612372012-08-24 09:35:08 +01001471
Daniel Vetter33196de2012-11-14 17:14:05 +01001472 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
Chris Wilsonb3612372012-08-24 09:35:08 +01001473 if (ret)
1474 return ret;
1475
Chris Wilsonb4716182015-04-27 13:41:17 +01001476 ret = __i915_wait_request(req,
1477 atomic_read(&dev_priv->gpu_error.reset_counter),
John Harrison9c654812014-11-24 18:49:35 +00001478 interruptible, NULL, NULL);
Chris Wilsonb4716182015-04-27 13:41:17 +01001479 if (ret)
1480 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001481
Chris Wilsonb4716182015-04-27 13:41:17 +01001482 __i915_gem_request_retire__upto(req);
Chris Wilsond26e3af2013-06-29 22:05:26 +01001483 return 0;
1484}
1485
Chris Wilsonb3612372012-08-24 09:35:08 +01001486/**
1487 * Ensures that all rendering to the object has completed and the object is
1488 * safe to unbind from the GTT or access from the CPU.
1489 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01001490int
Chris Wilsonb3612372012-08-24 09:35:08 +01001491i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1492 bool readonly)
1493{
Chris Wilsonb4716182015-04-27 13:41:17 +01001494 int ret, i;
Chris Wilsonb3612372012-08-24 09:35:08 +01001495
Chris Wilsonb4716182015-04-27 13:41:17 +01001496 if (!obj->active)
Chris Wilsonb3612372012-08-24 09:35:08 +01001497 return 0;
1498
Chris Wilsonb4716182015-04-27 13:41:17 +01001499 if (readonly) {
1500 if (obj->last_write_req != NULL) {
1501 ret = i915_wait_request(obj->last_write_req);
1502 if (ret)
1503 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001504
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001505 i = obj->last_write_req->engine->id;
Chris Wilsonb4716182015-04-27 13:41:17 +01001506 if (obj->last_read_req[i] == obj->last_write_req)
1507 i915_gem_object_retire__read(obj, i);
1508 else
1509 i915_gem_object_retire__write(obj);
1510 }
1511 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001512 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01001513 if (obj->last_read_req[i] == NULL)
1514 continue;
1515
1516 ret = i915_wait_request(obj->last_read_req[i]);
1517 if (ret)
1518 return ret;
1519
1520 i915_gem_object_retire__read(obj, i);
1521 }
Chris Wilsond501b1d2016-04-13 17:35:02 +01001522 GEM_BUG_ON(obj->active);
Chris Wilsonb4716182015-04-27 13:41:17 +01001523 }
1524
1525 return 0;
1526}
1527
1528static void
1529i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
1530 struct drm_i915_gem_request *req)
1531{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00001532 int ring = req->engine->id;
Chris Wilsonb4716182015-04-27 13:41:17 +01001533
1534 if (obj->last_read_req[ring] == req)
1535 i915_gem_object_retire__read(obj, ring);
1536 else if (obj->last_write_req == req)
1537 i915_gem_object_retire__write(obj);
1538
1539 __i915_gem_request_retire__upto(req);
Chris Wilsonb3612372012-08-24 09:35:08 +01001540}
1541
Chris Wilson3236f572012-08-24 09:35:09 +01001542/* A nonblocking variant of the above wait. This is a highly dangerous routine
1543 * as the object state may change during this call.
1544 */
1545static __must_check int
1546i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001547 struct intel_rps_client *rps,
Chris Wilson3236f572012-08-24 09:35:09 +01001548 bool readonly)
1549{
1550 struct drm_device *dev = obj->base.dev;
1551 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001552 struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
Daniel Vetterf69061b2012-12-06 09:01:42 +01001553 unsigned reset_counter;
Chris Wilsonb4716182015-04-27 13:41:17 +01001554 int ret, i, n = 0;
Chris Wilson3236f572012-08-24 09:35:09 +01001555
1556 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1557 BUG_ON(!dev_priv->mm.interruptible);
1558
Chris Wilsonb4716182015-04-27 13:41:17 +01001559 if (!obj->active)
Chris Wilson3236f572012-08-24 09:35:09 +01001560 return 0;
1561
Daniel Vetter33196de2012-11-14 17:14:05 +01001562 ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
Chris Wilson3236f572012-08-24 09:35:09 +01001563 if (ret)
1564 return ret;
1565
Daniel Vetterf69061b2012-12-06 09:01:42 +01001566 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilson3236f572012-08-24 09:35:09 +01001567
Chris Wilsonb4716182015-04-27 13:41:17 +01001568 if (readonly) {
1569 struct drm_i915_gem_request *req;
1570
1571 req = obj->last_write_req;
1572 if (req == NULL)
1573 return 0;
1574
Chris Wilsonb4716182015-04-27 13:41:17 +01001575 requests[n++] = i915_gem_request_reference(req);
1576 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00001577 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01001578 struct drm_i915_gem_request *req;
1579
1580 req = obj->last_read_req[i];
1581 if (req == NULL)
1582 continue;
1583
Chris Wilsonb4716182015-04-27 13:41:17 +01001584 requests[n++] = i915_gem_request_reference(req);
1585 }
1586 }
1587
1588 mutex_unlock(&dev->struct_mutex);
1589 for (i = 0; ret == 0 && i < n; i++)
1590 ret = __i915_wait_request(requests[i], reset_counter, true,
Chris Wilson2e1b8732015-04-27 13:41:22 +01001591 NULL, rps);
Chris Wilsonb4716182015-04-27 13:41:17 +01001592 mutex_lock(&dev->struct_mutex);
1593
Chris Wilsonb4716182015-04-27 13:41:17 +01001594 for (i = 0; i < n; i++) {
1595 if (ret == 0)
1596 i915_gem_object_retire_request(obj, requests[i]);
1597 i915_gem_request_unreference(requests[i]);
1598 }
1599
1600 return ret;
Chris Wilson3236f572012-08-24 09:35:09 +01001601}
1602
Chris Wilson2e1b8732015-04-27 13:41:22 +01001603static struct intel_rps_client *to_rps_client(struct drm_file *file)
1604{
1605 struct drm_i915_file_private *fpriv = file->driver_priv;
1606 return &fpriv->rps;
1607}
1608
Eric Anholt673a3942008-07-30 12:06:12 -07001609/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001610 * Called when user space prepares to use an object with the CPU, either
1611 * through the mmap ioctl's mapping or a GTT mapping.
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 Wilson05394f32010-11-08 19:18:58 +00001640 obj = to_intel_bo(drm_gem_object_lookup(dev, 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
1675 */
1676int
1677i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001678 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001679{
1680 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001681 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001682 int ret = 0;
1683
Chris Wilson76c1dec2010-09-25 11:22:51 +01001684 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001685 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001686 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001687
Chris Wilson05394f32010-11-08 19:18:58 +00001688 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001689 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001690 ret = -ENOENT;
1691 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001692 }
1693
Eric Anholt673a3942008-07-30 12:06:12 -07001694 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson2c225692013-08-09 12:26:45 +01001695 if (obj->pin_display)
Daniel Vettere62b59e2015-01-21 14:53:48 +01001696 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08001697
Chris Wilson05394f32010-11-08 19:18:58 +00001698 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001699unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001700 mutex_unlock(&dev->struct_mutex);
1701 return ret;
1702}
1703
1704/**
1705 * Maps the contents of an object, returning the address it is mapped
1706 * into.
1707 *
1708 * While the mapping holds a reference on the contents of the object, it doesn't
1709 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001710 *
1711 * IMPORTANT:
1712 *
1713 * DRM driver writers who look a this function as an example for how to do GEM
1714 * mmap support, please don't implement mmap support like here. The modern way
1715 * to implement DRM mmap support is with an mmap offset ioctl (like
1716 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1717 * That way debug tooling like valgrind will understand what's going on, hiding
1718 * the mmap call in a driver private ioctl will break that. The i915 driver only
1719 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001720 */
1721int
1722i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001723 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001724{
1725 struct drm_i915_gem_mmap *args = data;
1726 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001727 unsigned long addr;
1728
Akash Goel1816f922015-01-02 16:29:30 +05301729 if (args->flags & ~(I915_MMAP_WC))
1730 return -EINVAL;
1731
1732 if (args->flags & I915_MMAP_WC && !cpu_has_pat)
1733 return -ENODEV;
1734
Chris Wilson05394f32010-11-08 19:18:58 +00001735 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001736 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001737 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001738
Daniel Vetter1286ff72012-05-10 15:25:09 +02001739 /* prime objects have no backing filp to GEM mmap
1740 * pages from.
1741 */
1742 if (!obj->filp) {
1743 drm_gem_object_unreference_unlocked(obj);
1744 return -EINVAL;
1745 }
1746
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001747 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001748 PROT_READ | PROT_WRITE, MAP_SHARED,
1749 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301750 if (args->flags & I915_MMAP_WC) {
1751 struct mm_struct *mm = current->mm;
1752 struct vm_area_struct *vma;
1753
1754 down_write(&mm->mmap_sem);
1755 vma = find_vma(mm, addr);
1756 if (vma)
1757 vma->vm_page_prot =
1758 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1759 else
1760 addr = -ENOMEM;
1761 up_write(&mm->mmap_sem);
1762 }
Luca Barbieribc9025b2010-02-09 05:49:12 +00001763 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001764 if (IS_ERR((void *)addr))
1765 return addr;
1766
1767 args->addr_ptr = (uint64_t) addr;
1768
1769 return 0;
1770}
1771
Jesse Barnesde151cf2008-11-12 10:03:55 -08001772/**
1773 * i915_gem_fault - fault a page into the GTT
Geliang Tangd9072a32015-09-15 05:58:44 -07001774 * @vma: VMA in question
1775 * @vmf: fault info
Jesse Barnesde151cf2008-11-12 10:03:55 -08001776 *
1777 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1778 * from userspace. The fault handler takes care of binding the object to
1779 * the GTT (if needed), allocating and programming a fence register (again,
1780 * only if needed based on whether the old reg is still valid or the object
1781 * is tiled) and inserting a new PTE into the faulting process.
1782 *
1783 * Note that the faulting process may involve evicting existing objects
1784 * from the GTT and/or fence registers to make room. So performance may
1785 * suffer if the GTT working set is large or there are few fence registers
1786 * left.
1787 */
1788int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1789{
Chris Wilson05394f32010-11-08 19:18:58 +00001790 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1791 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001792 struct drm_i915_private *dev_priv = to_i915(dev);
1793 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001794 struct i915_ggtt_view view = i915_ggtt_view_normal;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001795 pgoff_t page_offset;
1796 unsigned long pfn;
1797 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001798 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001799
Paulo Zanonif65c9162013-11-27 18:20:34 -02001800 intel_runtime_pm_get(dev_priv);
1801
Jesse Barnesde151cf2008-11-12 10:03:55 -08001802 /* We don't use vmf->pgoff since that has the fake offset */
1803 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1804 PAGE_SHIFT;
1805
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001806 ret = i915_mutex_lock_interruptible(dev);
1807 if (ret)
1808 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001809
Chris Wilsondb53a302011-02-03 11:57:46 +00001810 trace_i915_gem_object_fault(obj, page_offset, true, write);
1811
Chris Wilson6e4930f2014-02-07 18:37:06 -02001812 /* Try to flush the object off the GPU first without holding the lock.
1813 * Upon reacquiring the lock, we will perform our sanity checks and then
1814 * repeat the flush holding the lock in the normal manner to catch cases
1815 * where we are gazumped.
1816 */
1817 ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
1818 if (ret)
1819 goto unlock;
1820
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001821 /* Access to snoopable pages through the GTT is incoherent. */
1822 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001823 ret = -EFAULT;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001824 goto unlock;
1825 }
1826
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001827 /* Use a partial view if the object is bigger than the aperture. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001828 if (obj->base.size >= ggtt->mappable_end &&
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001829 obj->tiling_mode == I915_TILING_NONE) {
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001830 static const unsigned int chunk_size = 256; // 1 MiB
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001831
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001832 memset(&view, 0, sizeof(view));
1833 view.type = I915_GGTT_VIEW_PARTIAL;
1834 view.params.partial.offset = rounddown(page_offset, chunk_size);
1835 view.params.partial.size =
1836 min_t(unsigned int,
1837 chunk_size,
1838 (vma->vm_end - vma->vm_start)/PAGE_SIZE -
1839 view.params.partial.offset);
1840 }
1841
1842 /* Now pin it into the GTT if needed */
1843 ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001844 if (ret)
1845 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001846
Chris Wilsonc9839302012-11-20 10:45:17 +00001847 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1848 if (ret)
1849 goto unpin;
1850
1851 ret = i915_gem_object_get_fence(obj);
1852 if (ret)
1853 goto unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001854
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001855 /* Finally, remap it using the new GTT offset */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001856 pfn = ggtt->mappable_base +
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001857 i915_gem_obj_ggtt_offset_view(obj, &view);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001858 pfn >>= PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001859
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001860 if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
1861 /* Overriding existing pages in partial view does not cause
1862 * us any trouble as TLBs are still valid because the fault
1863 * is due to userspace losing part of the mapping or never
1864 * having accessed it before (at this partials' range).
1865 */
1866 unsigned long base = vma->vm_start +
1867 (view.params.partial.offset << PAGE_SHIFT);
1868 unsigned int i;
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001869
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001870 for (i = 0; i < view.params.partial.size; i++) {
1871 ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001872 if (ret)
1873 break;
1874 }
1875
1876 obj->fault_mappable = true;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001877 } else {
1878 if (!obj->fault_mappable) {
1879 unsigned long size = min_t(unsigned long,
1880 vma->vm_end - vma->vm_start,
1881 obj->base.size);
1882 int i;
1883
1884 for (i = 0; i < size >> PAGE_SHIFT; i++) {
1885 ret = vm_insert_pfn(vma,
1886 (unsigned long)vma->vm_start + i * PAGE_SIZE,
1887 pfn + i);
1888 if (ret)
1889 break;
1890 }
1891
1892 obj->fault_mappable = true;
1893 } else
1894 ret = vm_insert_pfn(vma,
1895 (unsigned long)vmf->virtual_address,
1896 pfn + page_offset);
1897 }
Chris Wilsonc9839302012-11-20 10:45:17 +00001898unpin:
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001899 i915_gem_object_ggtt_unpin_view(obj, &view);
Chris Wilsonc7150892009-09-23 00:43:56 +01001900unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001901 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001902out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001903 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001904 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001905 /*
1906 * We eat errors when the gpu is terminally wedged to avoid
1907 * userspace unduly crashing (gl has no provisions for mmaps to
1908 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1909 * and so needs to be reported.
1910 */
1911 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001912 ret = VM_FAULT_SIGBUS;
1913 break;
1914 }
Chris Wilson045e7692010-11-07 09:18:22 +00001915 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001916 /*
1917 * EAGAIN means the gpu is hung and we'll wait for the error
1918 * handler to reset everything when re-faulting in
1919 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001920 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001921 case 0:
1922 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001923 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001924 case -EBUSY:
1925 /*
1926 * EBUSY is ok: this just means that another thread
1927 * already did the job.
1928 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001929 ret = VM_FAULT_NOPAGE;
1930 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001931 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001932 ret = VM_FAULT_OOM;
1933 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001934 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001935 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001936 ret = VM_FAULT_SIGBUS;
1937 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001938 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001939 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001940 ret = VM_FAULT_SIGBUS;
1941 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001942 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001943
1944 intel_runtime_pm_put(dev_priv);
1945 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001946}
1947
1948/**
Chris Wilson901782b2009-07-10 08:18:50 +01001949 * i915_gem_release_mmap - remove physical page mappings
1950 * @obj: obj in question
1951 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001952 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001953 * relinquish ownership of the pages back to the system.
1954 *
1955 * It is vital that we remove the page mapping if we have mapped a tiled
1956 * object through the GTT and then lose the fence register due to
1957 * resource pressure. Similarly if the object has been moved out of the
1958 * aperture, than pages mapped into userspace must be revoked. Removing the
1959 * mapping will then trigger a page fault on the next user access, allowing
1960 * fixup by i915_gem_fault().
1961 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001962void
Chris Wilson05394f32010-11-08 19:18:58 +00001963i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001964{
Chris Wilson6299f992010-11-24 12:23:44 +00001965 if (!obj->fault_mappable)
1966 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001967
David Herrmann6796cb12014-01-03 14:24:19 +01001968 drm_vma_node_unmap(&obj->base.vma_node,
1969 obj->base.dev->anon_inode->i_mapping);
Chris Wilson6299f992010-11-24 12:23:44 +00001970 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001971}
1972
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001973void
1974i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
1975{
1976 struct drm_i915_gem_object *obj;
1977
1978 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
1979 i915_gem_release_mmap(obj);
1980}
1981
Imre Deak0fa87792013-01-07 21:47:35 +02001982uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001983i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001984{
Chris Wilsone28f8712011-07-18 13:11:49 -07001985 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001986
1987 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001988 tiling_mode == I915_TILING_NONE)
1989 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001990
1991 /* Previous chips need a power-of-two fence region when tiling */
1992 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001993 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001994 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001995 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001996
Chris Wilsone28f8712011-07-18 13:11:49 -07001997 while (gtt_size < size)
1998 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001999
Chris Wilsone28f8712011-07-18 13:11:49 -07002000 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00002001}
2002
Jesse Barnesde151cf2008-11-12 10:03:55 -08002003/**
2004 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
2005 * @obj: object to check
2006 *
2007 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01002008 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002009 */
Imre Deakd8651102013-01-07 21:47:33 +02002010uint32_t
2011i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
2012 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002013{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002014 /*
2015 * Minimum alignment is 4k (GTT page size), but might be greater
2016 * if a fence register is needed for the object.
2017 */
Imre Deakd8651102013-01-07 21:47:33 +02002018 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07002019 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002020 return 4096;
2021
2022 /*
2023 * Previous chips need to be aligned to the size of the smallest
2024 * fence register that can contain the object.
2025 */
Chris Wilsone28f8712011-07-18 13:11:49 -07002026 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01002027}
2028
Chris Wilsond8cb5082012-08-11 15:41:03 +01002029static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
2030{
2031 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2032 int ret;
2033
David Herrmann0de23972013-07-24 21:07:52 +02002034 if (drm_vma_node_has_offset(&obj->base.vma_node))
Chris Wilsond8cb5082012-08-11 15:41:03 +01002035 return 0;
2036
Daniel Vetterda494d72012-12-20 15:11:16 +01002037 dev_priv->mm.shrinker_no_lock_stealing = true;
2038
Chris Wilsond8cb5082012-08-11 15:41:03 +01002039 ret = drm_gem_create_mmap_offset(&obj->base);
2040 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01002041 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002042
2043 /* Badly fragmented mmap space? The only way we can recover
2044 * space is by destroying unwanted objects. We can't randomly release
2045 * mmap_offsets as userspace expects them to be persistent for the
2046 * lifetime of the objects. The closest we can is to release the
2047 * offsets on purgeable objects by truncating it and marking it purged,
2048 * which prevents userspace from ever using that object again.
2049 */
Chris Wilson21ab4e72014-09-09 11:16:08 +01002050 i915_gem_shrink(dev_priv,
2051 obj->base.size >> PAGE_SHIFT,
2052 I915_SHRINK_BOUND |
2053 I915_SHRINK_UNBOUND |
2054 I915_SHRINK_PURGEABLE);
Chris Wilsond8cb5082012-08-11 15:41:03 +01002055 ret = drm_gem_create_mmap_offset(&obj->base);
2056 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01002057 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002058
2059 i915_gem_shrink_all(dev_priv);
Daniel Vetterda494d72012-12-20 15:11:16 +01002060 ret = drm_gem_create_mmap_offset(&obj->base);
2061out:
2062 dev_priv->mm.shrinker_no_lock_stealing = false;
2063
2064 return ret;
Chris Wilsond8cb5082012-08-11 15:41:03 +01002065}
2066
2067static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
2068{
Chris Wilsond8cb5082012-08-11 15:41:03 +01002069 drm_gem_free_mmap_offset(&obj->base);
2070}
2071
Dave Airlieda6b51d2014-12-24 13:11:17 +10002072int
Dave Airlieff72145b2011-02-07 12:16:14 +10002073i915_gem_mmap_gtt(struct drm_file *file,
2074 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10002075 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10002076 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002077{
Chris Wilson05394f32010-11-08 19:18:58 +00002078 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002079 int ret;
2080
Chris Wilson76c1dec2010-09-25 11:22:51 +01002081 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002082 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01002083 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002084
Dave Airlieff72145b2011-02-07 12:16:14 +10002085 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00002086 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002087 ret = -ENOENT;
2088 goto unlock;
2089 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002090
Chris Wilson05394f32010-11-08 19:18:58 +00002091 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002092 DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002093 ret = -EFAULT;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002094 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01002095 }
2096
Chris Wilsond8cb5082012-08-11 15:41:03 +01002097 ret = i915_gem_object_create_mmap_offset(obj);
2098 if (ret)
2099 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002100
David Herrmann0de23972013-07-24 21:07:52 +02002101 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002102
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002103out:
Chris Wilson05394f32010-11-08 19:18:58 +00002104 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002105unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08002106 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01002107 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002108}
2109
Dave Airlieff72145b2011-02-07 12:16:14 +10002110/**
2111 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2112 * @dev: DRM device
2113 * @data: GTT mapping ioctl data
2114 * @file: GEM object info
2115 *
2116 * Simply returns the fake offset to userspace so it can mmap it.
2117 * The mmap call will end up in drm_gem_mmap(), which will set things
2118 * up so we can get faults in the handler above.
2119 *
2120 * The fault handler will take care of binding the object into the GTT
2121 * (since it may have been evicted to make room for something), allocating
2122 * a fence register, and mapping the appropriate aperture address into
2123 * userspace.
2124 */
2125int
2126i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
2127 struct drm_file *file)
2128{
2129 struct drm_i915_gem_mmap_gtt *args = data;
2130
Dave Airlieda6b51d2014-12-24 13:11:17 +10002131 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10002132}
2133
Daniel Vetter225067e2012-08-20 10:23:20 +02002134/* Immediately discard the backing storage */
2135static void
2136i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01002137{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002138 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02002139
Chris Wilson4d6294bf2012-08-11 15:41:05 +01002140 if (obj->base.filp == NULL)
2141 return;
2142
Daniel Vetter225067e2012-08-20 10:23:20 +02002143 /* Our goal here is to return as much of the memory as
2144 * is possible back to the system as we are called from OOM.
2145 * To do this we must instruct the shmfs to drop all of its
2146 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01002147 */
Chris Wilson55372522014-03-25 13:23:06 +00002148 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Daniel Vetter225067e2012-08-20 10:23:20 +02002149 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01002150}
Chris Wilsone5281cc2010-10-28 13:45:36 +01002151
Chris Wilson55372522014-03-25 13:23:06 +00002152/* Try to discard unwanted pages */
2153static void
2154i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02002155{
Chris Wilson55372522014-03-25 13:23:06 +00002156 struct address_space *mapping;
2157
2158 switch (obj->madv) {
2159 case I915_MADV_DONTNEED:
2160 i915_gem_object_truncate(obj);
2161 case __I915_MADV_PURGED:
2162 return;
2163 }
2164
2165 if (obj->base.filp == NULL)
2166 return;
2167
2168 mapping = file_inode(obj->base.filp)->i_mapping,
2169 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002170}
2171
Chris Wilson5cdf5882010-09-27 15:51:07 +01002172static void
Chris Wilson05394f32010-11-08 19:18:58 +00002173i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002174{
Imre Deak90797e62013-02-18 19:28:03 +02002175 struct sg_page_iter sg_iter;
2176 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002177
Chris Wilson05394f32010-11-08 19:18:58 +00002178 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07002179
Chris Wilson6c085a72012-08-20 11:40:46 +02002180 ret = i915_gem_object_set_to_cpu_domain(obj, true);
2181 if (ret) {
2182 /* In the event of a disaster, abandon all caches and
2183 * hope for the best.
2184 */
2185 WARN_ON(ret != -EIO);
Chris Wilson2c225692013-08-09 12:26:45 +01002186 i915_gem_clflush_object(obj, true);
Chris Wilson6c085a72012-08-20 11:40:46 +02002187 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2188 }
2189
Imre Deake2273302015-07-09 12:59:05 +03002190 i915_gem_gtt_finish_object(obj);
2191
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002192 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07002193 i915_gem_object_save_bit_17_swizzle(obj);
2194
Chris Wilson05394f32010-11-08 19:18:58 +00002195 if (obj->madv == I915_MADV_DONTNEED)
2196 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01002197
Imre Deak90797e62013-02-18 19:28:03 +02002198 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02002199 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +01002200
Chris Wilson05394f32010-11-08 19:18:58 +00002201 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002202 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002203
Chris Wilson05394f32010-11-08 19:18:58 +00002204 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002205 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002206
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002207 put_page(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002208 }
Chris Wilson05394f32010-11-08 19:18:58 +00002209 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002210
Chris Wilson9da3da62012-06-01 15:20:22 +01002211 sg_free_table(obj->pages);
2212 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002213}
2214
Chris Wilsondd624af2013-01-15 12:39:35 +00002215int
Chris Wilson37e680a2012-06-07 15:38:42 +01002216i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
2217{
2218 const struct drm_i915_gem_object_ops *ops = obj->ops;
2219
Chris Wilson2f745ad2012-09-04 21:02:58 +01002220 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01002221 return 0;
2222
Chris Wilsona5570172012-09-04 21:02:54 +01002223 if (obj->pages_pin_count)
2224 return -EBUSY;
2225
Ben Widawsky98438772013-07-31 17:00:12 -07002226 BUG_ON(i915_gem_obj_bound_any(obj));
Ben Widawsky3e123022013-07-31 17:00:04 -07002227
Chris Wilsona2165e32012-12-03 11:49:00 +00002228 /* ->put_pages might need to allocate memory for the bit17 swizzle
2229 * array, hence protect them from being reaped by removing them from gtt
2230 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002231 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00002232
Chris Wilson0a798eb2016-04-08 12:11:11 +01002233 if (obj->mapping) {
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002234 if (is_vmalloc_addr(obj->mapping))
2235 vunmap(obj->mapping);
2236 else
2237 kunmap(kmap_to_page(obj->mapping));
Chris Wilson0a798eb2016-04-08 12:11:11 +01002238 obj->mapping = NULL;
2239 }
2240
Chris Wilson37e680a2012-06-07 15:38:42 +01002241 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002242 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002243
Chris Wilson55372522014-03-25 13:23:06 +00002244 i915_gem_object_invalidate(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02002245
2246 return 0;
2247}
2248
Chris Wilson37e680a2012-06-07 15:38:42 +01002249static int
Chris Wilson6c085a72012-08-20 11:40:46 +02002250i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002251{
Chris Wilson6c085a72012-08-20 11:40:46 +02002252 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002253 int page_count, i;
2254 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002255 struct sg_table *st;
2256 struct scatterlist *sg;
Imre Deak90797e62013-02-18 19:28:03 +02002257 struct sg_page_iter sg_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002258 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002259 unsigned long last_pfn = 0; /* suppress gcc warning */
Imre Deake2273302015-07-09 12:59:05 +03002260 int ret;
Chris Wilson6c085a72012-08-20 11:40:46 +02002261 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002262
Chris Wilson6c085a72012-08-20 11:40:46 +02002263 /* Assert that the object is not currently in any GPU domain. As it
2264 * wasn't in the GTT, there shouldn't be any way it could have been in
2265 * a GPU cache
2266 */
2267 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2268 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2269
Chris Wilson9da3da62012-06-01 15:20:22 +01002270 st = kmalloc(sizeof(*st), GFP_KERNEL);
2271 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002272 return -ENOMEM;
2273
Chris Wilson9da3da62012-06-01 15:20:22 +01002274 page_count = obj->base.size / PAGE_SIZE;
2275 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002276 kfree(st);
2277 return -ENOMEM;
2278 }
2279
2280 /* Get the list of pages out of our struct file. They'll be pinned
2281 * at this point until we release them.
2282 *
2283 * Fail silently without starting the shrinker
2284 */
Al Viro496ad9a2013-01-23 17:07:38 -05002285 mapping = file_inode(obj->base.filp)->i_mapping;
Michal Hockoc62d2552015-11-06 16:28:49 -08002286 gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
Mel Gormand0164ad2015-11-06 16:28:21 -08002287 gfp |= __GFP_NORETRY | __GFP_NOWARN;
Imre Deak90797e62013-02-18 19:28:03 +02002288 sg = st->sgl;
2289 st->nents = 0;
2290 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002291 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2292 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002293 i915_gem_shrink(dev_priv,
2294 page_count,
2295 I915_SHRINK_BOUND |
2296 I915_SHRINK_UNBOUND |
2297 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002298 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2299 }
2300 if (IS_ERR(page)) {
2301 /* We've tried hard to allocate the memory by reaping
2302 * our own buffer, now let the real VM do its job and
2303 * go down in flames if truly OOM.
2304 */
Chris Wilson6c085a72012-08-20 11:40:46 +02002305 i915_gem_shrink_all(dev_priv);
David Herrmannf461d1b2014-05-25 14:34:10 +02002306 page = shmem_read_mapping_page(mapping, i);
Imre Deake2273302015-07-09 12:59:05 +03002307 if (IS_ERR(page)) {
2308 ret = PTR_ERR(page);
Chris Wilson6c085a72012-08-20 11:40:46 +02002309 goto err_pages;
Imre Deake2273302015-07-09 12:59:05 +03002310 }
Chris Wilson6c085a72012-08-20 11:40:46 +02002311 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002312#ifdef CONFIG_SWIOTLB
2313 if (swiotlb_nr_tbl()) {
2314 st->nents++;
2315 sg_set_page(sg, page, PAGE_SIZE, 0);
2316 sg = sg_next(sg);
2317 continue;
2318 }
2319#endif
Imre Deak90797e62013-02-18 19:28:03 +02002320 if (!i || page_to_pfn(page) != last_pfn + 1) {
2321 if (i)
2322 sg = sg_next(sg);
2323 st->nents++;
2324 sg_set_page(sg, page, PAGE_SIZE, 0);
2325 } else {
2326 sg->length += PAGE_SIZE;
2327 }
2328 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002329
2330 /* Check that the i965g/gm workaround works. */
2331 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002332 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002333#ifdef CONFIG_SWIOTLB
2334 if (!swiotlb_nr_tbl())
2335#endif
2336 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002337 obj->pages = st;
2338
Imre Deake2273302015-07-09 12:59:05 +03002339 ret = i915_gem_gtt_prepare_object(obj);
2340 if (ret)
2341 goto err_pages;
2342
Eric Anholt673a3942008-07-30 12:06:12 -07002343 if (i915_gem_object_needs_bit17_swizzle(obj))
2344 i915_gem_object_do_bit_17_swizzle(obj);
2345
Daniel Vetter656bfa32014-11-20 09:26:30 +01002346 if (obj->tiling_mode != I915_TILING_NONE &&
2347 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
2348 i915_gem_object_pin_pages(obj);
2349
Eric Anholt673a3942008-07-30 12:06:12 -07002350 return 0;
2351
2352err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02002353 sg_mark_end(sg);
2354 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002355 put_page(sg_page_iter_page(&sg_iter));
Chris Wilson9da3da62012-06-01 15:20:22 +01002356 sg_free_table(st);
2357 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002358
2359 /* shmemfs first checks if there is enough memory to allocate the page
2360 * and reports ENOSPC should there be insufficient, along with the usual
2361 * ENOMEM for a genuine allocation failure.
2362 *
2363 * We use ENOSPC in our driver to mean that we have run out of aperture
2364 * space and so want to translate the error from shmemfs back to our
2365 * usual understanding of ENOMEM.
2366 */
Imre Deake2273302015-07-09 12:59:05 +03002367 if (ret == -ENOSPC)
2368 ret = -ENOMEM;
2369
2370 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002371}
2372
Chris Wilson37e680a2012-06-07 15:38:42 +01002373/* Ensure that the associated pages are gathered from the backing storage
2374 * and pinned into our object. i915_gem_object_get_pages() may be called
2375 * multiple times before they are released by a single call to
2376 * i915_gem_object_put_pages() - once the pages are no longer referenced
2377 * either as a result of memory pressure (reaping pages under the shrinker)
2378 * or as the object is itself released.
2379 */
2380int
2381i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2382{
2383 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2384 const struct drm_i915_gem_object_ops *ops = obj->ops;
2385 int ret;
2386
Chris Wilson2f745ad2012-09-04 21:02:58 +01002387 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01002388 return 0;
2389
Chris Wilson43e28f02013-01-08 10:53:09 +00002390 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002391 DRM_DEBUG("Attempting to obtain a purgeable object\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002392 return -EFAULT;
Chris Wilson43e28f02013-01-08 10:53:09 +00002393 }
2394
Chris Wilsona5570172012-09-04 21:02:54 +01002395 BUG_ON(obj->pages_pin_count);
2396
Chris Wilson37e680a2012-06-07 15:38:42 +01002397 ret = ops->get_pages(obj);
2398 if (ret)
2399 return ret;
2400
Ben Widawsky35c20a62013-05-31 11:28:48 -07002401 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilsonee286372015-04-07 16:20:25 +01002402
2403 obj->get_page.sg = obj->pages->sgl;
2404 obj->get_page.last = 0;
2405
Chris Wilson37e680a2012-06-07 15:38:42 +01002406 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002407}
2408
Chris Wilson0a798eb2016-04-08 12:11:11 +01002409void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
2410{
2411 int ret;
2412
2413 lockdep_assert_held(&obj->base.dev->struct_mutex);
2414
2415 ret = i915_gem_object_get_pages(obj);
2416 if (ret)
2417 return ERR_PTR(ret);
2418
2419 i915_gem_object_pin_pages(obj);
2420
2421 if (obj->mapping == NULL) {
Chris Wilson0a798eb2016-04-08 12:11:11 +01002422 struct page **pages;
Chris Wilson0a798eb2016-04-08 12:11:11 +01002423
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002424 pages = NULL;
2425 if (obj->base.size == PAGE_SIZE)
2426 obj->mapping = kmap(sg_page(obj->pages->sgl));
2427 else
2428 pages = drm_malloc_gfp(obj->base.size >> PAGE_SHIFT,
2429 sizeof(*pages),
2430 GFP_TEMPORARY);
Chris Wilson0a798eb2016-04-08 12:11:11 +01002431 if (pages != NULL) {
Chris Wilsonfb8621d2016-04-08 12:11:14 +01002432 struct sg_page_iter sg_iter;
2433 int n;
2434
Chris Wilson0a798eb2016-04-08 12:11:11 +01002435 n = 0;
2436 for_each_sg_page(obj->pages->sgl, &sg_iter,
2437 obj->pages->nents, 0)
2438 pages[n++] = sg_page_iter_page(&sg_iter);
2439
2440 obj->mapping = vmap(pages, n, 0, PAGE_KERNEL);
2441 drm_free_large(pages);
2442 }
2443 if (obj->mapping == NULL) {
2444 i915_gem_object_unpin_pages(obj);
2445 return ERR_PTR(-ENOMEM);
2446 }
2447 }
2448
2449 return obj->mapping;
2450}
2451
Ben Widawskye2d05a82013-09-24 09:57:58 -07002452void i915_vma_move_to_active(struct i915_vma *vma,
John Harrisonb2af0372015-05-29 17:43:50 +01002453 struct drm_i915_gem_request *req)
Ben Widawskye2d05a82013-09-24 09:57:58 -07002454{
Chris Wilsonb4716182015-04-27 13:41:17 +01002455 struct drm_i915_gem_object *obj = vma->obj;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002456 struct intel_engine_cs *engine;
John Harrisonb2af0372015-05-29 17:43:50 +01002457
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002458 engine = i915_gem_request_get_engine(req);
Chris Wilsonb4716182015-04-27 13:41:17 +01002459
2460 /* Add a reference if we're newly entering the active list. */
2461 if (obj->active == 0)
2462 drm_gem_object_reference(&obj->base);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002463 obj->active |= intel_engine_flag(engine);
Chris Wilsonb4716182015-04-27 13:41:17 +01002464
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002465 list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002466 i915_gem_request_assign(&obj->last_read_req[engine->id], req);
Chris Wilsonb4716182015-04-27 13:41:17 +01002467
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002468 list_move_tail(&vma->vm_link, &vma->vm->active_list);
Ben Widawskye2d05a82013-09-24 09:57:58 -07002469}
2470
Chris Wilsoncaea7472010-11-12 13:53:37 +00002471static void
Chris Wilsonb4716182015-04-27 13:41:17 +01002472i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
2473{
Chris Wilsond501b1d2016-04-13 17:35:02 +01002474 GEM_BUG_ON(obj->last_write_req == NULL);
2475 GEM_BUG_ON(!(obj->active & intel_engine_flag(obj->last_write_req->engine)));
Chris Wilsonb4716182015-04-27 13:41:17 +01002476
2477 i915_gem_request_assign(&obj->last_write_req, NULL);
Rodrigo Vivide152b62015-07-07 16:28:51 -07002478 intel_fb_obj_flush(obj, true, ORIGIN_CS);
Chris Wilsonb4716182015-04-27 13:41:17 +01002479}
2480
2481static void
2482i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
Chris Wilsoncaea7472010-11-12 13:53:37 +00002483{
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002484 struct i915_vma *vma;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002485
Chris Wilsond501b1d2016-04-13 17:35:02 +01002486 GEM_BUG_ON(obj->last_read_req[ring] == NULL);
2487 GEM_BUG_ON(!(obj->active & (1 << ring)));
Chris Wilsonb4716182015-04-27 13:41:17 +01002488
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002489 list_del_init(&obj->engine_list[ring]);
Chris Wilsonb4716182015-04-27 13:41:17 +01002490 i915_gem_request_assign(&obj->last_read_req[ring], NULL);
2491
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002492 if (obj->last_write_req && obj->last_write_req->engine->id == ring)
Chris Wilsonb4716182015-04-27 13:41:17 +01002493 i915_gem_object_retire__write(obj);
2494
2495 obj->active &= ~(1 << ring);
2496 if (obj->active)
2497 return;
Chris Wilson65ce3022012-07-20 12:41:02 +01002498
Chris Wilson6c246952015-07-27 10:26:26 +01002499 /* Bump our place on the bound list to keep it roughly in LRU order
2500 * so that we don't steal from recently used but inactive objects
2501 * (unless we are forced to ofc!)
2502 */
2503 list_move_tail(&obj->global_list,
2504 &to_i915(obj->base.dev)->mm.bound_list);
2505
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00002506 list_for_each_entry(vma, &obj->vma_list, obj_link) {
2507 if (!list_empty(&vma->vm_link))
2508 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002509 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00002510
John Harrison97b2a6a2014-11-24 18:49:26 +00002511 i915_gem_request_assign(&obj->last_fenced_req, NULL);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002512 drm_gem_object_unreference(&obj->base);
Chris Wilsonc8725f32014-03-17 12:21:55 +00002513}
2514
Chris Wilson9d7730912012-11-27 16:22:52 +00002515static int
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002516i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002517{
Chris Wilson9d7730912012-11-27 16:22:52 +00002518 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002519 struct intel_engine_cs *engine;
Chris Wilson29dcb572016-04-07 07:29:13 +01002520 int ret;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002521
Chris Wilson107f27a52012-12-10 13:56:17 +02002522 /* Carefully retire all requests without writing to the rings */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002523 for_each_engine(engine, dev_priv) {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002524 ret = intel_engine_idle(engine);
Chris Wilson107f27a52012-12-10 13:56:17 +02002525 if (ret)
2526 return ret;
Chris Wilson9d7730912012-11-27 16:22:52 +00002527 }
Chris Wilson9d7730912012-11-27 16:22:52 +00002528 i915_gem_retire_requests(dev);
Chris Wilson107f27a52012-12-10 13:56:17 +02002529
2530 /* Finally reset hw state */
Chris Wilson29dcb572016-04-07 07:29:13 +01002531 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002532 intel_ring_init_seqno(engine, seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002533
Chris Wilson9d7730912012-11-27 16:22:52 +00002534 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002535}
2536
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002537int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
2538{
2539 struct drm_i915_private *dev_priv = dev->dev_private;
2540 int ret;
2541
2542 if (seqno == 0)
2543 return -EINVAL;
2544
2545 /* HWS page needs to be set less than what we
2546 * will inject to ring
2547 */
2548 ret = i915_gem_init_seqno(dev, seqno - 1);
2549 if (ret)
2550 return ret;
2551
2552 /* Carefully set the last_seqno value so that wrap
2553 * detection still works
2554 */
2555 dev_priv->next_seqno = seqno;
2556 dev_priv->last_seqno = seqno - 1;
2557 if (dev_priv->last_seqno == 0)
2558 dev_priv->last_seqno--;
2559
2560 return 0;
2561}
2562
Chris Wilson9d7730912012-11-27 16:22:52 +00002563int
2564i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002565{
Chris Wilson9d7730912012-11-27 16:22:52 +00002566 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002567
Chris Wilson9d7730912012-11-27 16:22:52 +00002568 /* reserve 0 for non-seqno */
2569 if (dev_priv->next_seqno == 0) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002570 int ret = i915_gem_init_seqno(dev, 0);
Chris Wilson9d7730912012-11-27 16:22:52 +00002571 if (ret)
2572 return ret;
2573
2574 dev_priv->next_seqno = 1;
2575 }
2576
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02002577 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
Chris Wilson9d7730912012-11-27 16:22:52 +00002578 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002579}
2580
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002581/*
2582 * NB: This function is not allowed to fail. Doing so would mean the the
2583 * request is not being tracked for completion but the work itself is
2584 * going to happen on the hardware. This would be a Bad Thing(tm).
2585 */
John Harrison75289872015-05-29 17:43:49 +01002586void __i915_add_request(struct drm_i915_gem_request *request,
John Harrison5b4a60c2015-05-29 17:43:34 +01002587 struct drm_i915_gem_object *obj,
2588 bool flush_caches)
Eric Anholt673a3942008-07-30 12:06:12 -07002589{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002590 struct intel_engine_cs *engine;
John Harrison75289872015-05-29 17:43:49 +01002591 struct drm_i915_private *dev_priv;
Oscar Mateo48e29f52014-07-24 17:04:29 +01002592 struct intel_ringbuffer *ringbuf;
Nick Hoath6d3d8272015-01-15 13:10:39 +00002593 u32 request_start;
Chris Wilson3cce4692010-10-27 16:11:02 +01002594 int ret;
2595
Oscar Mateo48e29f52014-07-24 17:04:29 +01002596 if (WARN_ON(request == NULL))
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002597 return;
Oscar Mateo48e29f52014-07-24 17:04:29 +01002598
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002599 engine = request->engine;
Tvrtko Ursulin39dabec2016-03-17 13:04:10 +00002600 dev_priv = request->i915;
John Harrison75289872015-05-29 17:43:49 +01002601 ringbuf = request->ringbuf;
2602
John Harrison29b1b412015-06-18 13:10:09 +01002603 /*
2604 * To ensure that this call will not fail, space for its emissions
2605 * should already have been reserved in the ring buffer. Let the ring
2606 * know that it is time to use that space up.
2607 */
2608 intel_ring_reserved_space_use(ringbuf);
2609
Oscar Mateo48e29f52014-07-24 17:04:29 +01002610 request_start = intel_ring_get_tail(ringbuf);
Daniel Vettercc889e02012-06-13 20:45:19 +02002611 /*
2612 * Emit any outstanding flushes - execbuf can fail to emit the flush
2613 * after having emitted the batchbuffer command. Hence we need to fix
2614 * things up similar to emitting the lazy request. The difference here
2615 * is that the flush _must_ happen before the next request, no matter
2616 * what.
2617 */
John Harrison5b4a60c2015-05-29 17:43:34 +01002618 if (flush_caches) {
2619 if (i915.enable_execlists)
John Harrison4866d722015-05-29 17:43:55 +01002620 ret = logical_ring_flush_all_caches(request);
John Harrison5b4a60c2015-05-29 17:43:34 +01002621 else
John Harrison4866d722015-05-29 17:43:55 +01002622 ret = intel_ring_flush_all_caches(request);
John Harrison5b4a60c2015-05-29 17:43:34 +01002623 /* Not allowed to fail! */
2624 WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
2625 }
Daniel Vettercc889e02012-06-13 20:45:19 +02002626
Chris Wilson7c90b7d2016-04-07 07:29:17 +01002627 trace_i915_gem_request_add(request);
2628
2629 request->head = request_start;
2630
2631 /* Whilst this request exists, batch_obj will be on the
2632 * active_list, and so will hold the active reference. Only when this
2633 * request is retired will the the batch_obj be moved onto the
2634 * inactive_list and lose its active reference. Hence we do not need
2635 * to explicitly hold another reference here.
2636 */
2637 request->batch_obj = obj;
2638
2639 /* Seal the request and mark it as pending execution. Note that
2640 * we may inspect this state, without holding any locks, during
2641 * hangcheck. Hence we apply the barrier to ensure that we do not
2642 * see a more recent value in the hws than we are tracking.
2643 */
2644 request->emitted_jiffies = jiffies;
2645 request->previous_seqno = engine->last_submitted_seqno;
2646 smp_store_mb(engine->last_submitted_seqno, request->seqno);
2647 list_add_tail(&request->list, &engine->request_list);
2648
Chris Wilsona71d8d92012-02-15 11:25:36 +00002649 /* Record the position of the start of the request so that
2650 * should we detect the updated seqno part-way through the
2651 * GPU processing the request, we never over-estimate the
2652 * position of the head.
2653 */
Nick Hoath6d3d8272015-01-15 13:10:39 +00002654 request->postfix = intel_ring_get_tail(ringbuf);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002655
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002656 if (i915.enable_execlists)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002657 ret = engine->emit_request(request);
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002658 else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002659 ret = engine->add_request(request);
Michel Thierry53292cd2015-04-15 18:11:33 +01002660
2661 request->tail = intel_ring_get_tail(ringbuf);
Oscar Mateo48e29f52014-07-24 17:04:29 +01002662 }
John Harrisonbf7dc5b2015-05-29 17:43:24 +01002663 /* Not allowed to fail! */
2664 WARN(ret, "emit|add_request failed: %d!\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07002665
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002666 i915_queue_hangcheck(engine->dev);
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03002667
Daniel Vetter87255482014-11-19 20:36:48 +01002668 queue_delayed_work(dev_priv->wq,
2669 &dev_priv->mm.retire_work,
2670 round_jiffies_up_relative(HZ));
2671 intel_mark_busy(dev_priv->dev);
Daniel Vettercc889e02012-06-13 20:45:19 +02002672
John Harrison29b1b412015-06-18 13:10:09 +01002673 /* Sanity check that the reserved size was large enough. */
2674 intel_ring_reserved_space_end(ringbuf);
Eric Anholt673a3942008-07-30 12:06:12 -07002675}
2676
Mika Kuoppala939fd762014-01-30 19:04:44 +02002677static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
Oscar Mateo273497e2014-05-22 14:13:37 +01002678 const struct intel_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002679{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002680 unsigned long elapsed;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002681
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002682 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
2683
2684 if (ctx->hang_stats.banned)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002685 return true;
2686
Chris Wilson676fa572014-12-24 08:13:39 -08002687 if (ctx->hang_stats.ban_period_seconds &&
2688 elapsed <= ctx->hang_stats.ban_period_seconds) {
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002689 if (!i915_gem_context_is_default(ctx)) {
Mika Kuoppala3fac8972014-01-30 16:05:48 +02002690 DRM_DEBUG("context hanging too fast, banning!\n");
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002691 return true;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +02002692 } else if (i915_stop_ring_allow_ban(dev_priv)) {
2693 if (i915_stop_ring_allow_warn(dev_priv))
2694 DRM_ERROR("gpu hanging too fast, banning!\n");
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002695 return true;
Mika Kuoppala3fac8972014-01-30 16:05:48 +02002696 }
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002697 }
2698
2699 return false;
2700}
2701
Mika Kuoppala939fd762014-01-30 19:04:44 +02002702static void i915_set_reset_status(struct drm_i915_private *dev_priv,
Oscar Mateo273497e2014-05-22 14:13:37 +01002703 struct intel_context *ctx,
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002704 const bool guilty)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002705{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002706 struct i915_ctx_hang_stats *hs;
2707
2708 if (WARN_ON(!ctx))
2709 return;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002710
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002711 hs = &ctx->hang_stats;
2712
2713 if (guilty) {
Mika Kuoppala939fd762014-01-30 19:04:44 +02002714 hs->banned = i915_context_is_banned(dev_priv, ctx);
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002715 hs->batch_active++;
2716 hs->guilty_ts = get_seconds();
2717 } else {
2718 hs->batch_pending++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002719 }
2720}
2721
John Harrisonabfe2622014-11-24 18:49:24 +00002722void i915_gem_request_free(struct kref *req_ref)
2723{
2724 struct drm_i915_gem_request *req = container_of(req_ref,
2725 typeof(*req), ref);
2726 struct intel_context *ctx = req->ctx;
2727
John Harrisonfcfa423c2015-05-29 17:44:12 +01002728 if (req->file_priv)
2729 i915_gem_request_remove_from_client(req);
2730
Thomas Daniel0794aed2014-11-25 10:39:25 +00002731 if (ctx) {
Dave Gordone28e4042016-01-19 19:02:55 +00002732 if (i915.enable_execlists && ctx != req->i915->kernel_context)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002733 intel_lr_context_unpin(ctx, req->engine);
John Harrisonabfe2622014-11-24 18:49:24 +00002734
Oscar Mateodcb4c122014-11-13 10:28:10 +00002735 i915_gem_context_unreference(ctx);
2736 }
John Harrisonabfe2622014-11-24 18:49:24 +00002737
Chris Wilsonefab6d82015-04-07 16:20:57 +01002738 kmem_cache_free(req->i915->requests, req);
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002739}
2740
Dave Gordon26827082016-01-19 19:02:53 +00002741static inline int
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002742__i915_gem_request_alloc(struct intel_engine_cs *engine,
Dave Gordon26827082016-01-19 19:02:53 +00002743 struct intel_context *ctx,
2744 struct drm_i915_gem_request **req_out)
John Harrison6689cb22015-03-19 12:30:08 +00002745{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002746 struct drm_i915_private *dev_priv = to_i915(engine->dev);
Daniel Vettereed29a52015-05-21 14:21:25 +02002747 struct drm_i915_gem_request *req;
John Harrison6689cb22015-03-19 12:30:08 +00002748 int ret;
John Harrison6689cb22015-03-19 12:30:08 +00002749
John Harrison217e46b2015-05-29 17:43:29 +01002750 if (!req_out)
2751 return -EINVAL;
2752
John Harrisonbccca492015-05-29 17:44:11 +01002753 *req_out = NULL;
John Harrison6689cb22015-03-19 12:30:08 +00002754
Daniel Vettereed29a52015-05-21 14:21:25 +02002755 req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
2756 if (req == NULL)
John Harrison6689cb22015-03-19 12:30:08 +00002757 return -ENOMEM;
2758
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002759 ret = i915_gem_get_seqno(engine->dev, &req->seqno);
Chris Wilson9a0c1e22015-05-21 21:01:45 +01002760 if (ret)
2761 goto err;
John Harrison6689cb22015-03-19 12:30:08 +00002762
John Harrison40e895c2015-05-29 17:43:26 +01002763 kref_init(&req->ref);
2764 req->i915 = dev_priv;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00002765 req->engine = engine;
John Harrison40e895c2015-05-29 17:43:26 +01002766 req->ctx = ctx;
2767 i915_gem_context_reference(req->ctx);
John Harrison6689cb22015-03-19 12:30:08 +00002768
2769 if (i915.enable_execlists)
John Harrison40e895c2015-05-29 17:43:26 +01002770 ret = intel_logical_ring_alloc_request_extras(req);
John Harrison6689cb22015-03-19 12:30:08 +00002771 else
Daniel Vettereed29a52015-05-21 14:21:25 +02002772 ret = intel_ring_alloc_request_extras(req);
John Harrison40e895c2015-05-29 17:43:26 +01002773 if (ret) {
2774 i915_gem_context_unreference(req->ctx);
Chris Wilson9a0c1e22015-05-21 21:01:45 +01002775 goto err;
John Harrison40e895c2015-05-29 17:43:26 +01002776 }
John Harrison6689cb22015-03-19 12:30:08 +00002777
John Harrison29b1b412015-06-18 13:10:09 +01002778 /*
2779 * Reserve space in the ring buffer for all the commands required to
2780 * eventually emit this request. This is to guarantee that the
2781 * i915_add_request() call can't fail. Note that the reserve may need
2782 * to be redone if the request is not actually submitted straight
2783 * away, e.g. because a GPU scheduler has deferred it.
John Harrison29b1b412015-06-18 13:10:09 +01002784 */
John Harrisonccd98fe2015-05-29 17:44:09 +01002785 if (i915.enable_execlists)
2786 ret = intel_logical_ring_reserve_space(req);
2787 else
2788 ret = intel_ring_reserve_space(req);
2789 if (ret) {
2790 /*
2791 * At this point, the request is fully allocated even if not
2792 * fully prepared. Thus it can be cleaned up using the proper
2793 * free code.
2794 */
2795 i915_gem_request_cancel(req);
2796 return ret;
2797 }
John Harrison29b1b412015-06-18 13:10:09 +01002798
John Harrisonbccca492015-05-29 17:44:11 +01002799 *req_out = req;
John Harrison6689cb22015-03-19 12:30:08 +00002800 return 0;
Chris Wilson9a0c1e22015-05-21 21:01:45 +01002801
2802err:
2803 kmem_cache_free(dev_priv->requests, req);
2804 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002805}
2806
Dave Gordon26827082016-01-19 19:02:53 +00002807/**
2808 * i915_gem_request_alloc - allocate a request structure
2809 *
2810 * @engine: engine that we wish to issue the request on.
2811 * @ctx: context that the request will be associated with.
2812 * This can be NULL if the request is not directly related to
2813 * any specific user context, in which case this function will
2814 * choose an appropriate context to use.
2815 *
2816 * Returns a pointer to the allocated request if successful,
2817 * or an error code if not.
2818 */
2819struct drm_i915_gem_request *
2820i915_gem_request_alloc(struct intel_engine_cs *engine,
2821 struct intel_context *ctx)
2822{
2823 struct drm_i915_gem_request *req;
2824 int err;
2825
2826 if (ctx == NULL)
Dave Gordoned54c1a2016-01-19 19:02:54 +00002827 ctx = to_i915(engine->dev)->kernel_context;
Dave Gordon26827082016-01-19 19:02:53 +00002828 err = __i915_gem_request_alloc(engine, ctx, &req);
2829 return err ? ERR_PTR(err) : req;
2830}
2831
John Harrison29b1b412015-06-18 13:10:09 +01002832void i915_gem_request_cancel(struct drm_i915_gem_request *req)
2833{
2834 intel_ring_reserved_space_cancel(req->ringbuf);
2835
2836 i915_gem_request_unreference(req);
2837}
2838
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002839struct drm_i915_gem_request *
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002840i915_gem_find_active_request(struct intel_engine_cs *engine)
Chris Wilson9375e442010-09-19 12:21:28 +01002841{
Chris Wilson4db080f2013-12-04 11:37:09 +00002842 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002843
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002844 list_for_each_entry(request, &engine->request_list, list) {
John Harrison1b5a4332014-11-24 18:49:42 +00002845 if (i915_gem_request_completed(request, false))
Chris Wilson4db080f2013-12-04 11:37:09 +00002846 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002847
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002848 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002849 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002850
2851 return NULL;
2852}
2853
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002854static void i915_gem_reset_engine_status(struct drm_i915_private *dev_priv,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002855 struct intel_engine_cs *engine)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002856{
2857 struct drm_i915_gem_request *request;
2858 bool ring_hung;
2859
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002860 request = i915_gem_find_active_request(engine);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002861
2862 if (request == NULL)
2863 return;
2864
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002865 ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002866
Mika Kuoppala939fd762014-01-30 19:04:44 +02002867 i915_set_reset_status(dev_priv, request->ctx, ring_hung);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002868
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002869 list_for_each_entry_continue(request, &engine->request_list, list)
Mika Kuoppala939fd762014-01-30 19:04:44 +02002870 i915_set_reset_status(dev_priv, request->ctx, false);
Chris Wilson4db080f2013-12-04 11:37:09 +00002871}
2872
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002873static void i915_gem_reset_engine_cleanup(struct drm_i915_private *dev_priv,
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002874 struct intel_engine_cs *engine)
Chris Wilson4db080f2013-12-04 11:37:09 +00002875{
Chris Wilson608c1a52015-09-03 13:01:40 +01002876 struct intel_ringbuffer *buffer;
2877
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002878 while (!list_empty(&engine->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002879 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002880
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002881 obj = list_first_entry(&engine->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00002882 struct drm_i915_gem_object,
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002883 engine_list[engine->id]);
Eric Anholt673a3942008-07-30 12:06:12 -07002884
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002885 i915_gem_object_retire__read(obj, engine->id);
Eric Anholt673a3942008-07-30 12:06:12 -07002886 }
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002887
2888 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002889 * Clear the execlists queue up before freeing the requests, as those
2890 * are the ones that keep the context and ringbuffer backing objects
2891 * pinned in place.
2892 */
Oscar Mateodcb4c122014-11-13 10:28:10 +00002893
Tomas Elf7de16912015-10-19 16:32:32 +01002894 if (i915.enable_execlists) {
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002895 /* Ensure irq handler finishes or is cancelled. */
2896 tasklet_kill(&engine->irq_tasklet);
Mika Kuoppala1197b4f2015-01-13 11:32:24 +02002897
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002898 spin_lock_bh(&engine->execlist_lock);
Tomas Elfc5baa562015-10-23 18:02:37 +01002899 /* list_splice_tail_init checks for empty lists */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002900 list_splice_tail_init(&engine->execlist_queue,
2901 &engine->execlist_retired_req_list);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01002902 spin_unlock_bh(&engine->execlist_lock);
Mika Kuoppala1197b4f2015-01-13 11:32:24 +02002903
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002904 intel_execlists_retire_requests(engine);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002905 }
2906
2907 /*
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002908 * We must free the requests after all the corresponding objects have
2909 * been moved off active lists. Which is the same order as the normal
2910 * retire_requests function does. This is important if object hold
2911 * implicit references on things like e.g. ppgtt address spaces through
2912 * the request.
2913 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002914 while (!list_empty(&engine->request_list)) {
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002915 struct drm_i915_gem_request *request;
2916
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002917 request = list_first_entry(&engine->request_list,
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002918 struct drm_i915_gem_request,
2919 list);
2920
Chris Wilsonb4716182015-04-27 13:41:17 +01002921 i915_gem_request_retire(request);
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002922 }
Chris Wilson608c1a52015-09-03 13:01:40 +01002923
2924 /* Having flushed all requests from all queues, we know that all
2925 * ringbuffers must now be empty. However, since we do not reclaim
2926 * all space when retiring the request (to prevent HEADs colliding
2927 * with rapid ringbuffer wraparound) the amount of available space
2928 * upon reset is less than when we start. Do one more pass over
2929 * all the ringbuffers to reset last_retired_head.
2930 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002931 list_for_each_entry(buffer, &engine->buffers, link) {
Chris Wilson608c1a52015-09-03 13:01:40 +01002932 buffer->last_retired_head = buffer->tail;
2933 intel_ring_update_space(buffer);
2934 }
Chris Wilson2ed53a92016-04-07 07:29:11 +01002935
2936 intel_ring_init_seqno(engine, engine->last_submitted_seqno);
Eric Anholt673a3942008-07-30 12:06:12 -07002937}
2938
Chris Wilson069efc12010-09-30 16:53:18 +01002939void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002940{
Chris Wilsondfaae392010-09-22 10:31:52 +01002941 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00002942 struct intel_engine_cs *engine;
Eric Anholt673a3942008-07-30 12:06:12 -07002943
Chris Wilson4db080f2013-12-04 11:37:09 +00002944 /*
2945 * Before we free the objects from the requests, we need to inspect
2946 * them for finding the guilty party. As the requests only borrow
2947 * their reference to the objects, the inspection must be done first.
2948 */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002949 for_each_engine(engine, dev_priv)
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002950 i915_gem_reset_engine_status(dev_priv, engine);
Chris Wilson4db080f2013-12-04 11:37:09 +00002951
Dave Gordonb4ac5af2016-03-24 11:20:38 +00002952 for_each_engine(engine, dev_priv)
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00002953 i915_gem_reset_engine_cleanup(dev_priv, engine);
Chris Wilsondfaae392010-09-22 10:31:52 +01002954
Ben Widawskyacce9ff2013-12-06 14:11:03 -08002955 i915_gem_context_reset(dev);
2956
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002957 i915_gem_restore_fences(dev);
Chris Wilsonb4716182015-04-27 13:41:17 +01002958
2959 WARN_ON(i915_verify_lists(dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002960}
2961
2962/**
2963 * This function clears the request list as sequence numbers are passed.
2964 */
Chris Wilson1cf0ba12014-05-05 09:07:33 +01002965void
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002966i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
Eric Anholt673a3942008-07-30 12:06:12 -07002967{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002968 WARN_ON(i915_verify_lists(engine->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002969
Chris Wilson832a3aa2015-03-18 18:19:22 +00002970 /* Retire requests first as we use it above for the early return.
2971 * If we retire requests last, we may use a later seqno and so clear
2972 * the requests lists without clearing the active list, leading to
2973 * confusion.
Chris Wilsone9103032014-01-07 11:45:14 +00002974 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002975 while (!list_empty(&engine->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002976 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002977
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002978 request = list_first_entry(&engine->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002979 struct drm_i915_gem_request,
2980 list);
Eric Anholt673a3942008-07-30 12:06:12 -07002981
John Harrison1b5a4332014-11-24 18:49:42 +00002982 if (!i915_gem_request_completed(request, true))
Eric Anholt673a3942008-07-30 12:06:12 -07002983 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002984
Chris Wilsonb4716182015-04-27 13:41:17 +01002985 i915_gem_request_retire(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002986 }
2987
Chris Wilson832a3aa2015-03-18 18:19:22 +00002988 /* Move any buffers on the active list that are no longer referenced
2989 * by the ringbuffer to the flushing/inactive lists as appropriate,
2990 * before we free the context associated with the requests.
2991 */
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002992 while (!list_empty(&engine->active_list)) {
Chris Wilson832a3aa2015-03-18 18:19:22 +00002993 struct drm_i915_gem_object *obj;
2994
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002995 obj = list_first_entry(&engine->active_list,
2996 struct drm_i915_gem_object,
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00002997 engine_list[engine->id]);
Chris Wilson832a3aa2015-03-18 18:19:22 +00002998
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00002999 if (!list_empty(&obj->last_read_req[engine->id]->list))
Chris Wilson832a3aa2015-03-18 18:19:22 +00003000 break;
3001
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003002 i915_gem_object_retire__read(obj, engine->id);
Chris Wilson832a3aa2015-03-18 18:19:22 +00003003 }
3004
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003005 if (unlikely(engine->trace_irq_req &&
3006 i915_gem_request_completed(engine->trace_irq_req, true))) {
3007 engine->irq_put(engine);
3008 i915_gem_request_assign(&engine->trace_irq_req, NULL);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01003009 }
Chris Wilson23bc5982010-09-29 16:10:57 +01003010
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00003011 WARN_ON(i915_verify_lists(engine->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07003012}
3013
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003014bool
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01003015i915_gem_retire_requests(struct drm_device *dev)
3016{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003017 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003018 struct intel_engine_cs *engine;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003019 bool idle = true;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01003020
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003021 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003022 i915_gem_retire_requests_ring(engine);
3023 idle &= list_empty(&engine->request_list);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00003024 if (i915.enable_execlists) {
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01003025 spin_lock_bh(&engine->execlist_lock);
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003026 idle &= list_empty(&engine->execlist_queue);
Tvrtko Ursulin27af5ee2016-04-04 12:11:56 +01003027 spin_unlock_bh(&engine->execlist_lock);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00003028
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003029 intel_execlists_retire_requests(engine);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00003030 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003031 }
3032
3033 if (idle)
3034 mod_delayed_work(dev_priv->wq,
3035 &dev_priv->mm.idle_work,
3036 msecs_to_jiffies(100));
3037
3038 return idle;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01003039}
3040
Daniel Vetter75ef9da2010-08-21 00:25:16 +02003041static void
Eric Anholt673a3942008-07-30 12:06:12 -07003042i915_gem_retire_work_handler(struct work_struct *work)
3043{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003044 struct drm_i915_private *dev_priv =
3045 container_of(work, typeof(*dev_priv), mm.retire_work.work);
3046 struct drm_device *dev = dev_priv->dev;
Chris Wilson0a587052011-01-09 21:05:44 +00003047 bool idle;
Eric Anholt673a3942008-07-30 12:06:12 -07003048
Chris Wilson891b48c2010-09-29 12:26:37 +01003049 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003050 idle = false;
3051 if (mutex_trylock(&dev->struct_mutex)) {
3052 idle = i915_gem_retire_requests(dev);
3053 mutex_unlock(&dev->struct_mutex);
3054 }
3055 if (!idle)
Chris Wilsonbcb45082012-10-05 17:02:57 +01003056 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
3057 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003058}
Chris Wilson891b48c2010-09-29 12:26:37 +01003059
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003060static void
3061i915_gem_idle_work_handler(struct work_struct *work)
3062{
3063 struct drm_i915_private *dev_priv =
3064 container_of(work, typeof(*dev_priv), mm.idle_work.work);
Chris Wilson35c94182015-04-07 16:20:37 +01003065 struct drm_device *dev = dev_priv->dev;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003066 struct intel_engine_cs *engine;
Zou Nan haid1b851f2010-05-21 09:08:57 +08003067
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003068 for_each_engine(engine, dev_priv)
3069 if (!list_empty(&engine->request_list))
Chris Wilson423795c2015-04-07 16:21:08 +01003070 return;
Zou Nan hai852835f2010-05-21 09:08:56 +08003071
Daniel Vetter30ecad72015-12-09 09:29:36 +01003072 /* we probably should sync with hangcheck here, using cancel_work_sync.
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003073 * Also locking seems to be fubar here, engine->request_list is protected
Daniel Vetter30ecad72015-12-09 09:29:36 +01003074 * by dev->struct_mutex. */
3075
Chris Wilson35c94182015-04-07 16:20:37 +01003076 intel_mark_idle(dev);
3077
3078 if (mutex_trylock(&dev->struct_mutex)) {
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003079 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003080 i915_gem_batch_pool_fini(&engine->batch_pool);
Chris Wilson35c94182015-04-07 16:20:37 +01003081
3082 mutex_unlock(&dev->struct_mutex);
3083 }
Eric Anholt673a3942008-07-30 12:06:12 -07003084}
3085
Ben Widawsky5816d642012-04-11 11:18:19 -07003086/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003087 * Ensures that an object will eventually get non-busy by flushing any required
3088 * write domains, emitting any outstanding lazy request and retiring and
3089 * completed requests.
3090 */
3091static int
3092i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
3093{
John Harrisona5ac0f92015-05-29 17:44:15 +01003094 int i;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003095
Chris Wilsonb4716182015-04-27 13:41:17 +01003096 if (!obj->active)
3097 return 0;
John Harrison41c52412014-11-24 18:49:43 +00003098
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003099 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01003100 struct drm_i915_gem_request *req;
3101
3102 req = obj->last_read_req[i];
3103 if (req == NULL)
3104 continue;
3105
3106 if (list_empty(&req->list))
3107 goto retire;
3108
Chris Wilsonb4716182015-04-27 13:41:17 +01003109 if (i915_gem_request_completed(req, true)) {
3110 __i915_gem_request_retire__upto(req);
3111retire:
3112 i915_gem_object_retire__read(obj, i);
3113 }
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003114 }
3115
3116 return 0;
3117}
3118
3119/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003120 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
3121 * @DRM_IOCTL_ARGS: standard ioctl arguments
3122 *
3123 * Returns 0 if successful, else an error is returned with the remaining time in
3124 * the timeout parameter.
3125 * -ETIME: object is still busy after timeout
3126 * -ERESTARTSYS: signal interrupted the wait
3127 * -ENONENT: object doesn't exist
3128 * Also possible, but rare:
3129 * -EAGAIN: GPU wedged
3130 * -ENOMEM: damn
3131 * -ENODEV: Internal IRQ fail
3132 * -E?: The add request failed
3133 *
3134 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
3135 * non-zero timeout parameter the wait ioctl will wait for the given number of
3136 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3137 * without holding struct_mutex the object may become re-busied before this
3138 * function completes. A similar but shorter * race condition exists in the busy
3139 * ioctl
3140 */
3141int
3142i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
3143{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003144 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003145 struct drm_i915_gem_wait *args = data;
3146 struct drm_i915_gem_object *obj;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003147 struct drm_i915_gem_request *req[I915_NUM_ENGINES];
Daniel Vetterf69061b2012-12-06 09:01:42 +01003148 unsigned reset_counter;
Chris Wilsonb4716182015-04-27 13:41:17 +01003149 int i, n = 0;
3150 int ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003151
Daniel Vetter11b5d512014-09-29 15:31:26 +02003152 if (args->flags != 0)
3153 return -EINVAL;
3154
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003155 ret = i915_mutex_lock_interruptible(dev);
3156 if (ret)
3157 return ret;
3158
3159 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
3160 if (&obj->base == NULL) {
3161 mutex_unlock(&dev->struct_mutex);
3162 return -ENOENT;
3163 }
3164
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003165 /* Need to make sure the object gets inactive eventually. */
3166 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003167 if (ret)
3168 goto out;
3169
Chris Wilsonb4716182015-04-27 13:41:17 +01003170 if (!obj->active)
John Harrison97b2a6a2014-11-24 18:49:26 +00003171 goto out;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003172
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003173 /* Do this after OLR check to make sure we make forward progress polling
Chris Wilson762e4582015-03-04 18:09:26 +00003174 * on this IOCTL with a timeout == 0 (like busy ioctl)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003175 */
Chris Wilson762e4582015-03-04 18:09:26 +00003176 if (args->timeout_ns == 0) {
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003177 ret = -ETIME;
3178 goto out;
3179 }
3180
3181 drm_gem_object_unreference(&obj->base);
Daniel Vetterf69061b2012-12-06 09:01:42 +01003182 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsonb4716182015-04-27 13:41:17 +01003183
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003184 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilsonb4716182015-04-27 13:41:17 +01003185 if (obj->last_read_req[i] == NULL)
3186 continue;
3187
3188 req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
3189 }
3190
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003191 mutex_unlock(&dev->struct_mutex);
3192
Chris Wilsonb4716182015-04-27 13:41:17 +01003193 for (i = 0; i < n; i++) {
3194 if (ret == 0)
3195 ret = __i915_wait_request(req[i], reset_counter, true,
3196 args->timeout_ns > 0 ? &args->timeout_ns : NULL,
Chris Wilsonb6aa0872015-12-02 09:13:46 +00003197 to_rps_client(file));
Chris Wilsonb4716182015-04-27 13:41:17 +01003198 i915_gem_request_unreference__unlocked(req[i]);
3199 }
John Harrisonff865882014-11-24 18:49:28 +00003200 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003201
3202out:
3203 drm_gem_object_unreference(&obj->base);
3204 mutex_unlock(&dev->struct_mutex);
3205 return ret;
3206}
3207
Chris Wilsonb4716182015-04-27 13:41:17 +01003208static int
3209__i915_gem_object_sync(struct drm_i915_gem_object *obj,
3210 struct intel_engine_cs *to,
John Harrison91af1272015-06-18 13:14:56 +01003211 struct drm_i915_gem_request *from_req,
3212 struct drm_i915_gem_request **to_req)
Chris Wilsonb4716182015-04-27 13:41:17 +01003213{
3214 struct intel_engine_cs *from;
3215 int ret;
3216
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003217 from = i915_gem_request_get_engine(from_req);
Chris Wilsonb4716182015-04-27 13:41:17 +01003218 if (to == from)
3219 return 0;
3220
John Harrison91af1272015-06-18 13:14:56 +01003221 if (i915_gem_request_completed(from_req, true))
Chris Wilsonb4716182015-04-27 13:41:17 +01003222 return 0;
3223
Chris Wilsonb4716182015-04-27 13:41:17 +01003224 if (!i915_semaphore_is_enabled(obj->base.dev)) {
Chris Wilsona6f766f2015-04-27 13:41:20 +01003225 struct drm_i915_private *i915 = to_i915(obj->base.dev);
John Harrison91af1272015-06-18 13:14:56 +01003226 ret = __i915_wait_request(from_req,
Chris Wilsona6f766f2015-04-27 13:41:20 +01003227 atomic_read(&i915->gpu_error.reset_counter),
3228 i915->mm.interruptible,
3229 NULL,
3230 &i915->rps.semaphores);
Chris Wilsonb4716182015-04-27 13:41:17 +01003231 if (ret)
3232 return ret;
3233
John Harrison91af1272015-06-18 13:14:56 +01003234 i915_gem_object_retire_request(obj, from_req);
Chris Wilsonb4716182015-04-27 13:41:17 +01003235 } else {
3236 int idx = intel_ring_sync_index(from, to);
John Harrison91af1272015-06-18 13:14:56 +01003237 u32 seqno = i915_gem_request_get_seqno(from_req);
3238
3239 WARN_ON(!to_req);
Chris Wilsonb4716182015-04-27 13:41:17 +01003240
3241 if (seqno <= from->semaphore.sync_seqno[idx])
3242 return 0;
3243
John Harrison91af1272015-06-18 13:14:56 +01003244 if (*to_req == NULL) {
Dave Gordon26827082016-01-19 19:02:53 +00003245 struct drm_i915_gem_request *req;
3246
3247 req = i915_gem_request_alloc(to, NULL);
3248 if (IS_ERR(req))
3249 return PTR_ERR(req);
3250
3251 *to_req = req;
John Harrison91af1272015-06-18 13:14:56 +01003252 }
3253
John Harrison599d9242015-05-29 17:44:04 +01003254 trace_i915_gem_ring_sync_to(*to_req, from, from_req);
3255 ret = to->semaphore.sync_to(*to_req, from, seqno);
Chris Wilsonb4716182015-04-27 13:41:17 +01003256 if (ret)
3257 return ret;
3258
3259 /* We use last_read_req because sync_to()
3260 * might have just caused seqno wrap under
3261 * the radar.
3262 */
3263 from->semaphore.sync_seqno[idx] =
3264 i915_gem_request_get_seqno(obj->last_read_req[from->id]);
3265 }
3266
3267 return 0;
3268}
3269
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07003270/**
Ben Widawsky5816d642012-04-11 11:18:19 -07003271 * i915_gem_object_sync - sync an object to a ring.
3272 *
3273 * @obj: object which may be in use on another ring.
3274 * @to: ring we wish to use the object on. May be NULL.
John Harrison91af1272015-06-18 13:14:56 +01003275 * @to_req: request we wish to use the object for. See below.
3276 * This will be allocated and returned if a request is
3277 * required but not passed in.
Ben Widawsky5816d642012-04-11 11:18:19 -07003278 *
3279 * This code is meant to abstract object synchronization with the GPU.
3280 * Calling with NULL implies synchronizing the object with the CPU
Chris Wilsonb4716182015-04-27 13:41:17 +01003281 * rather than a particular GPU ring. Conceptually we serialise writes
John Harrison91af1272015-06-18 13:14:56 +01003282 * between engines inside the GPU. We only allow one engine to write
Chris Wilsonb4716182015-04-27 13:41:17 +01003283 * into a buffer at any time, but multiple readers. To ensure each has
3284 * a coherent view of memory, we must:
3285 *
3286 * - If there is an outstanding write request to the object, the new
3287 * request must wait for it to complete (either CPU or in hw, requests
3288 * on the same ring will be naturally ordered).
3289 *
3290 * - If we are a write request (pending_write_domain is set), the new
3291 * request must wait for outstanding read requests to complete.
Ben Widawsky5816d642012-04-11 11:18:19 -07003292 *
John Harrison91af1272015-06-18 13:14:56 +01003293 * For CPU synchronisation (NULL to) no request is required. For syncing with
3294 * rings to_req must be non-NULL. However, a request does not have to be
3295 * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
3296 * request will be allocated automatically and returned through *to_req. Note
3297 * that it is not guaranteed that commands will be emitted (because the system
3298 * might already be idle). Hence there is no need to create a request that
3299 * might never have any work submitted. Note further that if a request is
3300 * returned in *to_req, it is the responsibility of the caller to submit
3301 * that request (after potentially adding more work to it).
3302 *
Ben Widawsky5816d642012-04-11 11:18:19 -07003303 * Returns 0 if successful, else propagates up the lower layer error.
3304 */
Ben Widawsky2911a352012-04-05 14:47:36 -07003305int
3306i915_gem_object_sync(struct drm_i915_gem_object *obj,
John Harrison91af1272015-06-18 13:14:56 +01003307 struct intel_engine_cs *to,
3308 struct drm_i915_gem_request **to_req)
Ben Widawsky2911a352012-04-05 14:47:36 -07003309{
Chris Wilsonb4716182015-04-27 13:41:17 +01003310 const bool readonly = obj->base.pending_write_domain == 0;
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003311 struct drm_i915_gem_request *req[I915_NUM_ENGINES];
Chris Wilsonb4716182015-04-27 13:41:17 +01003312 int ret, i, n;
Ben Widawsky2911a352012-04-05 14:47:36 -07003313
Chris Wilsonb4716182015-04-27 13:41:17 +01003314 if (!obj->active)
Ben Widawsky2911a352012-04-05 14:47:36 -07003315 return 0;
3316
Chris Wilsonb4716182015-04-27 13:41:17 +01003317 if (to == NULL)
3318 return i915_gem_object_wait_rendering(obj, readonly);
Ben Widawsky2911a352012-04-05 14:47:36 -07003319
Chris Wilsonb4716182015-04-27 13:41:17 +01003320 n = 0;
3321 if (readonly) {
3322 if (obj->last_write_req)
3323 req[n++] = obj->last_write_req;
3324 } else {
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003325 for (i = 0; i < I915_NUM_ENGINES; i++)
Chris Wilsonb4716182015-04-27 13:41:17 +01003326 if (obj->last_read_req[i])
3327 req[n++] = obj->last_read_req[i];
3328 }
3329 for (i = 0; i < n; i++) {
John Harrison91af1272015-06-18 13:14:56 +01003330 ret = __i915_gem_object_sync(obj, to, req[i], to_req);
Chris Wilsonb4716182015-04-27 13:41:17 +01003331 if (ret)
3332 return ret;
3333 }
Ben Widawsky2911a352012-04-05 14:47:36 -07003334
Chris Wilsonb4716182015-04-27 13:41:17 +01003335 return 0;
Ben Widawsky2911a352012-04-05 14:47:36 -07003336}
3337
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003338static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
3339{
3340 u32 old_write_domain, old_read_domains;
3341
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003342 /* Force a pagefault for domain tracking on next user access */
3343 i915_gem_release_mmap(obj);
3344
Keith Packardb97c3d92011-06-24 21:02:59 -07003345 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3346 return;
3347
Chris Wilson97c809fd2012-10-09 19:24:38 +01003348 /* Wait for any direct GTT access to complete */
3349 mb();
3350
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003351 old_read_domains = obj->base.read_domains;
3352 old_write_domain = obj->base.write_domain;
3353
3354 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
3355 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
3356
3357 trace_i915_gem_object_change_domain(obj,
3358 old_read_domains,
3359 old_write_domain);
3360}
3361
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003362static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
Eric Anholt673a3942008-07-30 12:06:12 -07003363{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003364 struct drm_i915_gem_object *obj = vma->obj;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003365 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson43e28f02013-01-08 10:53:09 +00003366 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003367
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003368 if (list_empty(&vma->obj_link))
Eric Anholt673a3942008-07-30 12:06:12 -07003369 return 0;
3370
Daniel Vetter0ff501c2013-08-29 19:50:31 +02003371 if (!drm_mm_node_allocated(&vma->node)) {
3372 i915_gem_vma_destroy(vma);
Daniel Vetter0ff501c2013-08-29 19:50:31 +02003373 return 0;
3374 }
Ben Widawsky433544b2013-08-13 18:09:06 -07003375
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003376 if (vma->pin_count)
Chris Wilson31d8d652012-05-24 19:11:20 +01003377 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07003378
Chris Wilsonc4670ad2012-08-20 10:23:27 +01003379 BUG_ON(obj->pages == NULL);
3380
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003381 if (wait) {
3382 ret = i915_gem_object_wait_rendering(obj, false);
3383 if (ret)
3384 return ret;
3385 }
Chris Wilsona8198ee2011-04-13 22:04:09 +01003386
Chris Wilson596c5922016-02-26 11:03:20 +00003387 if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003388 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01003389
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003390 /* release the fence reg _after_ flushing */
3391 ret = i915_gem_object_put_fence(obj);
3392 if (ret)
3393 return ret;
3394 }
Daniel Vetter96b47b62009-12-15 17:50:00 +01003395
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003396 trace_i915_vma_unbind(vma);
Chris Wilsondb53a302011-02-03 11:57:46 +00003397
Daniel Vetter777dc5b2015-04-14 17:35:12 +02003398 vma->vm->unbind_vma(vma);
Mika Kuoppala5e562f12015-04-30 11:02:31 +03003399 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003400
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003401 list_del_init(&vma->vm_link);
Chris Wilson596c5922016-02-26 11:03:20 +00003402 if (vma->is_ggtt) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003403 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
3404 obj->map_and_fenceable = false;
3405 } else if (vma->ggtt_view.pages) {
3406 sg_free_table(vma->ggtt_view.pages);
3407 kfree(vma->ggtt_view.pages);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003408 }
Chris Wilson016a65a2015-06-11 08:06:08 +01003409 vma->ggtt_view.pages = NULL;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003410 }
Eric Anholt673a3942008-07-30 12:06:12 -07003411
Ben Widawsky2f633152013-07-17 12:19:03 -07003412 drm_mm_remove_node(&vma->node);
3413 i915_gem_vma_destroy(vma);
3414
3415 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02003416 * no more VMAs exist. */
Imre Deake2273302015-07-09 12:59:05 +03003417 if (list_empty(&obj->vma_list))
Ben Widawsky2f633152013-07-17 12:19:03 -07003418 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07003419
Chris Wilson70903c32013-12-04 09:59:09 +00003420 /* And finally now the object is completely decoupled from this vma,
3421 * we can drop its hold on the backing storage and allow it to be
3422 * reaped by the shrinker.
3423 */
3424 i915_gem_object_unpin_pages(obj);
3425
Chris Wilson88241782011-01-07 17:09:48 +00003426 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00003427}
3428
Tvrtko Ursuline9f24d52015-10-05 13:26:36 +01003429int i915_vma_unbind(struct i915_vma *vma)
3430{
3431 return __i915_vma_unbind(vma, true);
3432}
3433
3434int __i915_vma_unbind_no_wait(struct i915_vma *vma)
3435{
3436 return __i915_vma_unbind(vma, false);
3437}
3438
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003439int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003440{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003441 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003442 struct intel_engine_cs *engine;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003443 int ret;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003444
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003445 /* Flush everything onto the inactive list. */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00003446 for_each_engine(engine, dev_priv) {
Thomas Danielecdb5fd2014-08-20 16:29:24 +01003447 if (!i915.enable_execlists) {
John Harrison73cfa862015-05-29 17:43:35 +01003448 struct drm_i915_gem_request *req;
3449
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00003450 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +00003451 if (IS_ERR(req))
3452 return PTR_ERR(req);
John Harrison73cfa862015-05-29 17:43:35 +01003453
John Harrisonba01cc92015-05-29 17:43:41 +01003454 ret = i915_switch_context(req);
John Harrison73cfa862015-05-29 17:43:35 +01003455 if (ret) {
3456 i915_gem_request_cancel(req);
3457 return ret;
3458 }
3459
John Harrison75289872015-05-29 17:43:49 +01003460 i915_add_request_no_flush(req);
Thomas Danielecdb5fd2014-08-20 16:29:24 +01003461 }
Ben Widawskyb6c74882012-08-14 14:35:14 -07003462
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00003463 ret = intel_engine_idle(engine);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003464 if (ret)
3465 return ret;
3466 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003467
Chris Wilsonb4716182015-04-27 13:41:17 +01003468 WARN_ON(i915_verify_lists(dev));
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003469 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003470}
3471
Chris Wilson4144f9b2014-09-11 08:43:48 +01003472static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003473 unsigned long cache_level)
3474{
Chris Wilson4144f9b2014-09-11 08:43:48 +01003475 struct drm_mm_node *gtt_space = &vma->node;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003476 struct drm_mm_node *other;
3477
Chris Wilson4144f9b2014-09-11 08:43:48 +01003478 /*
3479 * On some machines we have to be careful when putting differing types
3480 * of snoopable memory together to avoid the prefetcher crossing memory
3481 * domains and dying. During vm initialisation, we decide whether or not
3482 * these constraints apply and set the drm_mm.color_adjust
3483 * appropriately.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003484 */
Chris Wilson4144f9b2014-09-11 08:43:48 +01003485 if (vma->vm->mm.color_adjust == NULL)
Chris Wilson42d6ab42012-07-26 11:49:32 +01003486 return true;
3487
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003488 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003489 return true;
3490
3491 if (list_empty(&gtt_space->node_list))
3492 return true;
3493
3494 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3495 if (other->allocated && !other->hole_follows && other->color != cache_level)
3496 return false;
3497
3498 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3499 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3500 return false;
3501
3502 return true;
3503}
3504
Jesse Barnesde151cf2008-11-12 10:03:55 -08003505/**
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003506 * Finds free space in the GTT aperture and binds the object or a view of it
3507 * there.
Eric Anholt673a3942008-07-30 12:06:12 -07003508 */
Daniel Vetter262de142014-02-14 14:01:20 +01003509static struct i915_vma *
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003510i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3511 struct i915_address_space *vm,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003512 const struct i915_ggtt_view *ggtt_view,
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003513 unsigned alignment,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003514 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003515{
Chris Wilson05394f32010-11-08 19:18:58 +00003516 struct drm_device *dev = obj->base.dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003517 struct drm_i915_private *dev_priv = to_i915(dev);
3518 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Michel Thierry65bd3422015-07-29 17:23:58 +01003519 u32 fence_alignment, unfenced_alignment;
Michel Thierry101b5062015-10-01 13:33:57 +01003520 u32 search_flag, alloc_flag;
3521 u64 start, end;
Michel Thierry65bd3422015-07-29 17:23:58 +01003522 u64 size, fence_size;
Ben Widawsky2f633152013-07-17 12:19:03 -07003523 struct i915_vma *vma;
Chris Wilson07f73f62009-09-14 16:50:30 +01003524 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003525
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003526 if (i915_is_ggtt(vm)) {
3527 u32 view_size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003528
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003529 if (WARN_ON(!ggtt_view))
3530 return ERR_PTR(-EINVAL);
3531
3532 view_size = i915_ggtt_view_size(obj, ggtt_view);
3533
3534 fence_size = i915_gem_get_gtt_size(dev,
3535 view_size,
3536 obj->tiling_mode);
3537 fence_alignment = i915_gem_get_gtt_alignment(dev,
3538 view_size,
3539 obj->tiling_mode,
3540 true);
3541 unfenced_alignment = i915_gem_get_gtt_alignment(dev,
3542 view_size,
3543 obj->tiling_mode,
3544 false);
3545 size = flags & PIN_MAPPABLE ? fence_size : view_size;
3546 } else {
3547 fence_size = i915_gem_get_gtt_size(dev,
3548 obj->base.size,
3549 obj->tiling_mode);
3550 fence_alignment = i915_gem_get_gtt_alignment(dev,
3551 obj->base.size,
3552 obj->tiling_mode,
3553 true);
3554 unfenced_alignment =
3555 i915_gem_get_gtt_alignment(dev,
3556 obj->base.size,
3557 obj->tiling_mode,
3558 false);
3559 size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
3560 }
Chris Wilsona00b10c2010-09-24 21:15:47 +01003561
Michel Thierry101b5062015-10-01 13:33:57 +01003562 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
3563 end = vm->total;
3564 if (flags & PIN_MAPPABLE)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003565 end = min_t(u64, end, ggtt->mappable_end);
Michel Thierry101b5062015-10-01 13:33:57 +01003566 if (flags & PIN_ZONE_4G)
Michel Thierry48ea1e32016-01-11 11:39:27 +00003567 end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
Michel Thierry101b5062015-10-01 13:33:57 +01003568
Eric Anholt673a3942008-07-30 12:06:12 -07003569 if (alignment == 0)
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003570 alignment = flags & PIN_MAPPABLE ? fence_alignment :
Daniel Vetter5e783302010-11-14 22:32:36 +01003571 unfenced_alignment;
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003572 if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003573 DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
3574 ggtt_view ? ggtt_view->type : 0,
3575 alignment);
Daniel Vetter262de142014-02-14 14:01:20 +01003576 return ERR_PTR(-EINVAL);
Eric Anholt673a3942008-07-30 12:06:12 -07003577 }
3578
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003579 /* If binding the object/GGTT view requires more space than the entire
3580 * aperture has, reject it early before evicting everything in a vain
3581 * attempt to find space.
Chris Wilson654fc602010-05-27 13:18:21 +01003582 */
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003583 if (size > end) {
Michel Thierry65bd3422015-07-29 17:23:58 +01003584 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 +03003585 ggtt_view ? ggtt_view->type : 0,
3586 size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003587 flags & PIN_MAPPABLE ? "mappable" : "total",
Chris Wilsond23db882014-05-23 08:48:08 +02003588 end);
Daniel Vetter262de142014-02-14 14:01:20 +01003589 return ERR_PTR(-E2BIG);
Chris Wilson654fc602010-05-27 13:18:21 +01003590 }
3591
Chris Wilson37e680a2012-06-07 15:38:42 +01003592 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003593 if (ret)
Daniel Vetter262de142014-02-14 14:01:20 +01003594 return ERR_PTR(ret);
Chris Wilson6c085a72012-08-20 11:40:46 +02003595
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003596 i915_gem_object_pin_pages(obj);
3597
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003598 vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
3599 i915_gem_obj_lookup_or_create_vma(obj, vm);
3600
Daniel Vetter262de142014-02-14 14:01:20 +01003601 if (IS_ERR(vma))
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003602 goto err_unpin;
Ben Widawsky2f633152013-07-17 12:19:03 -07003603
Chris Wilson506a8e82015-12-08 11:55:07 +00003604 if (flags & PIN_OFFSET_FIXED) {
3605 uint64_t offset = flags & PIN_OFFSET_MASK;
3606
3607 if (offset & (alignment - 1) || offset + size > end) {
3608 ret = -EINVAL;
3609 goto err_free_vma;
3610 }
3611 vma->node.start = offset;
3612 vma->node.size = size;
3613 vma->node.color = obj->cache_level;
3614 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3615 if (ret) {
3616 ret = i915_gem_evict_for_vma(vma);
3617 if (ret == 0)
3618 ret = drm_mm_reserve_node(&vm->mm, &vma->node);
3619 }
3620 if (ret)
3621 goto err_free_vma;
Michel Thierry101b5062015-10-01 13:33:57 +01003622 } else {
Chris Wilson506a8e82015-12-08 11:55:07 +00003623 if (flags & PIN_HIGH) {
3624 search_flag = DRM_MM_SEARCH_BELOW;
3625 alloc_flag = DRM_MM_CREATE_TOP;
3626 } else {
3627 search_flag = DRM_MM_SEARCH_DEFAULT;
3628 alloc_flag = DRM_MM_CREATE_DEFAULT;
3629 }
Michel Thierry101b5062015-10-01 13:33:57 +01003630
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003631search_free:
Chris Wilson506a8e82015-12-08 11:55:07 +00003632 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
3633 size, alignment,
3634 obj->cache_level,
3635 start, end,
3636 search_flag,
3637 alloc_flag);
3638 if (ret) {
3639 ret = i915_gem_evict_something(dev, vm, size, alignment,
3640 obj->cache_level,
3641 start, end,
3642 flags);
3643 if (ret == 0)
3644 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003645
Chris Wilson506a8e82015-12-08 11:55:07 +00003646 goto err_free_vma;
3647 }
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003648 }
Chris Wilson4144f9b2014-09-11 08:43:48 +01003649 if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
Ben Widawsky2f633152013-07-17 12:19:03 -07003650 ret = -EINVAL;
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003651 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003652 }
3653
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003654 trace_i915_vma_bind(vma, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07003655 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003656 if (ret)
Imre Deake2273302015-07-09 12:59:05 +03003657 goto err_remove_node;
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003658
Ben Widawsky35c20a62013-05-31 11:28:48 -07003659 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003660 list_add_tail(&vma->vm_link, &vm->inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003661
Daniel Vetter262de142014-02-14 14:01:20 +01003662 return vma;
Ben Widawsky2f633152013-07-17 12:19:03 -07003663
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003664err_remove_node:
Dan Carpenter6286ef92013-07-19 08:46:27 +03003665 drm_mm_remove_node(&vma->node);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003666err_free_vma:
Ben Widawsky2f633152013-07-17 12:19:03 -07003667 i915_gem_vma_destroy(vma);
Daniel Vetter262de142014-02-14 14:01:20 +01003668 vma = ERR_PTR(ret);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003669err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003670 i915_gem_object_unpin_pages(obj);
Daniel Vetter262de142014-02-14 14:01:20 +01003671 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003672}
3673
Chris Wilson000433b2013-08-08 14:41:09 +01003674bool
Chris Wilson2c225692013-08-09 12:26:45 +01003675i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3676 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003677{
Eric Anholt673a3942008-07-30 12:06:12 -07003678 /* If we don't have a page list set up, then we're not pinned
3679 * to GPU, and we can ignore the cache flush because it'll happen
3680 * again at bind time.
3681 */
Chris Wilson05394f32010-11-08 19:18:58 +00003682 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003683 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003684
Imre Deak769ce462013-02-13 21:56:05 +02003685 /*
3686 * Stolen memory is always coherent with the GPU as it is explicitly
3687 * marked as wc by the system, or the system is cache-coherent.
3688 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003689 if (obj->stolen || obj->phys_handle)
Chris Wilson000433b2013-08-08 14:41:09 +01003690 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003691
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003692 /* If the GPU is snooping the contents of the CPU cache,
3693 * we do not need to manually clear the CPU cache lines. However,
3694 * the caches are only snooped when the render cache is
3695 * flushed/invalidated. As we always have to emit invalidations
3696 * and flushes when moving into and out of the RENDER domain, correct
3697 * snooping behaviour occurs naturally as the result of our domain
3698 * tracking.
3699 */
Chris Wilson0f719792015-01-13 13:32:52 +00003700 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3701 obj->cache_dirty = true;
Chris Wilson000433b2013-08-08 14:41:09 +01003702 return false;
Chris Wilson0f719792015-01-13 13:32:52 +00003703 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003704
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003705 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003706 drm_clflush_sg(obj->pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003707 obj->cache_dirty = false;
Chris Wilson000433b2013-08-08 14:41:09 +01003708
3709 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003710}
3711
3712/** Flushes the GTT write domain for the object if it's dirty. */
3713static void
Chris Wilson05394f32010-11-08 19:18:58 +00003714i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003715{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003716 uint32_t old_write_domain;
3717
Chris Wilson05394f32010-11-08 19:18:58 +00003718 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003719 return;
3720
Chris Wilson63256ec2011-01-04 18:42:07 +00003721 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003722 * to it immediately go to main memory as far as we know, so there's
3723 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003724 *
3725 * However, we do have to enforce the order so that all writes through
3726 * the GTT land before any writes to the device, such as updates to
3727 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003728 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003729 wmb();
3730
Chris Wilson05394f32010-11-08 19:18:58 +00003731 old_write_domain = obj->base.write_domain;
3732 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003733
Rodrigo Vivide152b62015-07-07 16:28:51 -07003734 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003735
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003736 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003737 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003738 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003739}
3740
3741/** Flushes the CPU write domain for the object if it's dirty. */
3742static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003743i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003744{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003745 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003746
Chris Wilson05394f32010-11-08 19:18:58 +00003747 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003748 return;
3749
Daniel Vettere62b59e2015-01-21 14:53:48 +01003750 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilson000433b2013-08-08 14:41:09 +01003751 i915_gem_chipset_flush(obj->base.dev);
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_CPU);
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
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003763/**
3764 * Moves a single object to the GTT read, and possibly write domain.
3765 *
3766 * This function returns when the move is complete, including waiting on
3767 * flushes to occur.
3768 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003769int
Chris Wilson20217462010-11-23 15:26:33 +00003770i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003771{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003772 struct drm_device *dev = obj->base.dev;
3773 struct drm_i915_private *dev_priv = to_i915(dev);
3774 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003775 uint32_t old_write_domain, old_read_domains;
Chris Wilson43566de2015-01-02 16:29:29 +05303776 struct i915_vma *vma;
Eric Anholte47c68e2008-11-14 13:35:19 -08003777 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003778
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003779 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3780 return 0;
3781
Chris Wilson0201f1e2012-07-20 12:41:01 +01003782 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003783 if (ret)
3784 return ret;
3785
Chris Wilson43566de2015-01-02 16:29:29 +05303786 /* Flush and acquire obj->pages so that we are coherent through
3787 * direct access in memory with previous cached writes through
3788 * shmemfs and that our cache domain tracking remains valid.
3789 * For example, if the obj->filp was moved to swap without us
3790 * being notified and releasing the pages, we would mistakenly
3791 * continue to assume that the obj remained out of the CPU cached
3792 * domain.
3793 */
3794 ret = i915_gem_object_get_pages(obj);
3795 if (ret)
3796 return ret;
3797
Daniel Vettere62b59e2015-01-21 14:53:48 +01003798 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003799
Chris Wilsond0a57782012-10-09 19:24:37 +01003800 /* Serialise direct access to this object with the barriers for
3801 * coherent writes from the GPU, by effectively invalidating the
3802 * GTT domain upon first access.
3803 */
3804 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3805 mb();
3806
Chris Wilson05394f32010-11-08 19:18:58 +00003807 old_write_domain = obj->base.write_domain;
3808 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003809
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003810 /* It should now be out of any other write domains, and we can update
3811 * the domain values for our changes.
3812 */
Chris Wilson05394f32010-11-08 19:18:58 +00003813 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3814 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003815 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003816 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3817 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3818 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003819 }
3820
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003821 trace_i915_gem_object_change_domain(obj,
3822 old_read_domains,
3823 old_write_domain);
3824
Chris Wilson8325a092012-04-24 15:52:35 +01003825 /* And bump the LRU for this access */
Chris Wilson43566de2015-01-02 16:29:29 +05303826 vma = i915_gem_obj_to_ggtt(obj);
3827 if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003828 list_move_tail(&vma->vm_link,
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03003829 &ggtt->base.inactive_list);
Chris Wilson8325a092012-04-24 15:52:35 +01003830
Eric Anholte47c68e2008-11-14 13:35:19 -08003831 return 0;
3832}
3833
Chris Wilsonef55f922015-10-09 14:11:27 +01003834/**
3835 * Changes the cache-level of an object across all VMA.
3836 *
3837 * After this function returns, the object will be in the new cache-level
3838 * across all GTT and the contents of the backing storage will be coherent,
3839 * with respect to the new cache-level. In order to keep the backing storage
3840 * coherent for all users, we only allow a single cache level to be set
3841 * globally on the object and prevent it from being changed whilst the
3842 * hardware is reading from the object. That is if the object is currently
3843 * on the scanout it will be set to uncached (or equivalent display
3844 * cache coherency) and all non-MOCS GPU access will also be uncached so
3845 * that all direct access to the scanout remains coherent.
3846 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003847int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3848 enum i915_cache_level cache_level)
3849{
Daniel Vetter7bddb012012-02-09 17:15:47 +01003850 struct drm_device *dev = obj->base.dev;
Chris Wilsondf6f7832014-03-21 07:40:56 +00003851 struct i915_vma *vma, *next;
Chris Wilsonef55f922015-10-09 14:11:27 +01003852 bool bound = false;
Ville Syrjäläed75a552015-08-11 19:47:10 +03003853 int ret = 0;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003854
3855 if (obj->cache_level == cache_level)
Ville Syrjäläed75a552015-08-11 19:47:10 +03003856 goto out;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003857
Chris Wilsonef55f922015-10-09 14:11:27 +01003858 /* Inspect the list of currently bound VMA and unbind any that would
3859 * be invalid given the new cache-level. This is principally to
3860 * catch the issue of the CS prefetch crossing page boundaries and
3861 * reading an invalid PTE on older architectures.
3862 */
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003863 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003864 if (!drm_mm_node_allocated(&vma->node))
3865 continue;
3866
3867 if (vma->pin_count) {
3868 DRM_DEBUG("can not change the cache level of pinned objects\n");
3869 return -EBUSY;
3870 }
3871
Chris Wilson4144f9b2014-09-11 08:43:48 +01003872 if (!i915_gem_valid_gtt_space(vma, cache_level)) {
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003873 ret = i915_vma_unbind(vma);
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003874 if (ret)
3875 return ret;
Chris Wilsonef55f922015-10-09 14:11:27 +01003876 } else
3877 bound = true;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003878 }
3879
Chris Wilsonef55f922015-10-09 14:11:27 +01003880 /* We can reuse the existing drm_mm nodes but need to change the
3881 * cache-level on the PTE. We could simply unbind them all and
3882 * rebind with the correct cache-level on next use. However since
3883 * we already have a valid slot, dma mapping, pages etc, we may as
3884 * rewrite the PTE in the belief that doing so tramples upon less
3885 * state and so involves less work.
3886 */
3887 if (bound) {
3888 /* Before we change the PTE, the GPU must not be accessing it.
3889 * If we wait upon the object, we know that all the bound
3890 * VMA are no longer active.
3891 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01003892 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003893 if (ret)
3894 return ret;
3895
Chris Wilsonef55f922015-10-09 14:11:27 +01003896 if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
3897 /* Access to snoopable pages through the GTT is
3898 * incoherent and on some machines causes a hard
3899 * lockup. Relinquish the CPU mmaping to force
3900 * userspace to refault in the pages and we can
3901 * then double check if the GTT mapping is still
3902 * valid for that pointer access.
3903 */
3904 i915_gem_release_mmap(obj);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003905
Chris Wilsonef55f922015-10-09 14:11:27 +01003906 /* As we no longer need a fence for GTT access,
3907 * we can relinquish it now (and so prevent having
3908 * to steal a fence from someone else on the next
3909 * fence request). Note GPU activity would have
3910 * dropped the fence as all snoopable access is
3911 * supposed to be linear.
3912 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003913 ret = i915_gem_object_put_fence(obj);
3914 if (ret)
3915 return ret;
Chris Wilsonef55f922015-10-09 14:11:27 +01003916 } else {
3917 /* We either have incoherent backing store and
3918 * so no GTT access or the architecture is fully
3919 * coherent. In such cases, existing GTT mmaps
3920 * ignore the cache bit in the PTE and we can
3921 * rewrite it without confusing the GPU or having
3922 * to force userspace to fault back in its mmaps.
3923 */
Chris Wilsone4ffd172011-04-04 09:44:39 +01003924 }
3925
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003926 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilsonef55f922015-10-09 14:11:27 +01003927 if (!drm_mm_node_allocated(&vma->node))
3928 continue;
3929
3930 ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
3931 if (ret)
3932 return ret;
3933 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003934 }
3935
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00003936 list_for_each_entry(vma, &obj->vma_list, obj_link)
Chris Wilson2c225692013-08-09 12:26:45 +01003937 vma->node.color = cache_level;
3938 obj->cache_level = cache_level;
3939
Ville Syrjäläed75a552015-08-11 19:47:10 +03003940out:
Chris Wilsonef55f922015-10-09 14:11:27 +01003941 /* Flush the dirty CPU caches to the backing storage so that the
3942 * object is now coherent at its new cache level (with respect
3943 * to the access domain).
3944 */
Chris Wilson0f719792015-01-13 13:32:52 +00003945 if (obj->cache_dirty &&
3946 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
3947 cpu_write_needs_clflush(obj)) {
3948 if (i915_gem_clflush_object(obj, true))
3949 i915_gem_chipset_flush(obj->base.dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003950 }
3951
Chris Wilsone4ffd172011-04-04 09:44:39 +01003952 return 0;
3953}
3954
Ben Widawsky199adf42012-09-21 17:01:20 -07003955int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3956 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003957{
Ben Widawsky199adf42012-09-21 17:01:20 -07003958 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003959 struct drm_i915_gem_object *obj;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003960
3961 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson432be692015-05-07 12:14:55 +01003962 if (&obj->base == NULL)
3963 return -ENOENT;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003964
Chris Wilson651d7942013-08-08 14:41:10 +01003965 switch (obj->cache_level) {
3966 case I915_CACHE_LLC:
3967 case I915_CACHE_L3_LLC:
3968 args->caching = I915_CACHING_CACHED;
3969 break;
3970
Chris Wilson4257d3b2013-08-08 14:41:11 +01003971 case I915_CACHE_WT:
3972 args->caching = I915_CACHING_DISPLAY;
3973 break;
3974
Chris Wilson651d7942013-08-08 14:41:10 +01003975 default:
3976 args->caching = I915_CACHING_NONE;
3977 break;
3978 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003979
Chris Wilson432be692015-05-07 12:14:55 +01003980 drm_gem_object_unreference_unlocked(&obj->base);
3981 return 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003982}
3983
Ben Widawsky199adf42012-09-21 17:01:20 -07003984int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3985 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003986{
Imre Deakfd0fe6a2015-11-04 21:25:32 +02003987 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky199adf42012-09-21 17:01:20 -07003988 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003989 struct drm_i915_gem_object *obj;
3990 enum i915_cache_level level;
3991 int ret;
3992
Ben Widawsky199adf42012-09-21 17:01:20 -07003993 switch (args->caching) {
3994 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003995 level = I915_CACHE_NONE;
3996 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003997 case I915_CACHING_CACHED:
Imre Deake5756c12015-08-14 18:43:30 +03003998 /*
3999 * Due to a HW issue on BXT A stepping, GPU stores via a
4000 * snooped mapping may leave stale data in a corresponding CPU
4001 * cacheline, whereas normally such cachelines would get
4002 * invalidated.
4003 */
Tvrtko Ursulinca377802016-03-02 12:10:31 +00004004 if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
Imre Deake5756c12015-08-14 18:43:30 +03004005 return -ENODEV;
4006
Chris Wilsone6994ae2012-07-10 10:27:08 +01004007 level = I915_CACHE_LLC;
4008 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01004009 case I915_CACHING_DISPLAY:
4010 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
4011 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01004012 default:
4013 return -EINVAL;
4014 }
4015
Imre Deakfd0fe6a2015-11-04 21:25:32 +02004016 intel_runtime_pm_get(dev_priv);
4017
Ben Widawsky3bc29132012-09-26 16:15:20 -07004018 ret = i915_mutex_lock_interruptible(dev);
4019 if (ret)
Imre Deakfd0fe6a2015-11-04 21:25:32 +02004020 goto rpm_put;
Ben Widawsky3bc29132012-09-26 16:15:20 -07004021
Chris Wilsone6994ae2012-07-10 10:27:08 +01004022 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4023 if (&obj->base == NULL) {
4024 ret = -ENOENT;
4025 goto unlock;
4026 }
4027
4028 ret = i915_gem_object_set_cache_level(obj, level);
4029
4030 drm_gem_object_unreference(&obj->base);
4031unlock:
4032 mutex_unlock(&dev->struct_mutex);
Imre Deakfd0fe6a2015-11-04 21:25:32 +02004033rpm_put:
4034 intel_runtime_pm_put(dev_priv);
4035
Chris Wilsone6994ae2012-07-10 10:27:08 +01004036 return ret;
4037}
4038
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004039/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004040 * Prepare buffer for display plane (scanout, cursors, etc).
4041 * Can be called from an uninterruptible phase (modesetting) and allows
4042 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004043 */
4044int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004045i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
4046 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004047 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004048{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004049 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004050 int ret;
4051
Chris Wilsoncc98b412013-08-09 12:25:09 +01004052 /* Mark the pin_display early so that we account for the
4053 * display coherency whilst setting up the cache domains.
4054 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004055 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004056
Eric Anholta7ef0642011-03-29 16:59:54 -07004057 /* The display engine is not coherent with the LLC cache on gen6. As
4058 * a result, we make sure that the pinning that is about to occur is
4059 * done with uncached PTEs. This is lowest common denominator for all
4060 * chipsets.
4061 *
4062 * However for gen6+, we could do better by using the GFDT bit instead
4063 * of uncaching, which would allow us to flush all the LLC-cached data
4064 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
4065 */
Chris Wilson651d7942013-08-08 14:41:10 +01004066 ret = i915_gem_object_set_cache_level(obj,
4067 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
Eric Anholta7ef0642011-03-29 16:59:54 -07004068 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004069 goto err_unpin_display;
Eric Anholta7ef0642011-03-29 16:59:54 -07004070
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004071 /* As the user may map the buffer once pinned in the display plane
4072 * (e.g. libkms for the bootup splash), we have to ensure that we
4073 * always use map_and_fenceable for all scanout buffers.
4074 */
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00004075 ret = i915_gem_object_ggtt_pin(obj, view, alignment,
4076 view->type == I915_GGTT_VIEW_NORMAL ?
4077 PIN_MAPPABLE : 0);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004078 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004079 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004080
Daniel Vettere62b59e2015-01-21 14:53:48 +01004081 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004082
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004083 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00004084 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004085
4086 /* It should now be out of any other write domains, and we can update
4087 * the domain values for our changes.
4088 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01004089 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00004090 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004091
4092 trace_i915_gem_object_change_domain(obj,
4093 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004094 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004095
4096 return 0;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004097
4098err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004099 obj->pin_display--;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004100 return ret;
4101}
4102
4103void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004104i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
4105 const struct i915_ggtt_view *view)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004106{
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004107 if (WARN_ON(obj->pin_display == 0))
4108 return;
4109
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004110 i915_gem_object_ggtt_unpin_view(obj, view);
4111
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004112 obj->pin_display--;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004113}
4114
Eric Anholte47c68e2008-11-14 13:35:19 -08004115/**
4116 * Moves a single object to the CPU read, and possibly write domain.
4117 *
4118 * This function returns when the move is complete, including waiting on
4119 * flushes to occur.
4120 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004121int
Chris Wilson919926a2010-11-12 13:42:53 +00004122i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004123{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004124 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08004125 int ret;
4126
Chris Wilson8d7e3de2011-02-07 15:23:02 +00004127 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
4128 return 0;
4129
Chris Wilson0201f1e2012-07-20 12:41:01 +01004130 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00004131 if (ret)
4132 return ret;
4133
Eric Anholte47c68e2008-11-14 13:35:19 -08004134 i915_gem_object_flush_gtt_write_domain(obj);
4135
Chris Wilson05394f32010-11-08 19:18:58 +00004136 old_write_domain = obj->base.write_domain;
4137 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004138
Eric Anholte47c68e2008-11-14 13:35:19 -08004139 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00004140 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01004141 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08004142
Chris Wilson05394f32010-11-08 19:18:58 +00004143 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004144 }
4145
4146 /* It should now be out of any other write domains, and we can update
4147 * the domain values for our changes.
4148 */
Chris Wilson05394f32010-11-08 19:18:58 +00004149 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08004150
4151 /* If we're writing through the CPU, then the GPU read domains will
4152 * need to be invalidated at next use.
4153 */
4154 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00004155 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4156 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004157 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004158
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004159 trace_i915_gem_object_change_domain(obj,
4160 old_read_domains,
4161 old_write_domain);
4162
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004163 return 0;
4164}
4165
Eric Anholt673a3942008-07-30 12:06:12 -07004166/* Throttle our rendering by waiting until the ring has completed our requests
4167 * emitted over 20 msec ago.
4168 *
Eric Anholtb9624422009-06-03 07:27:35 +00004169 * Note that if we were to use the current jiffies each time around the loop,
4170 * we wouldn't escape the function with any frames outstanding if the time to
4171 * render a frame was over 20ms.
4172 *
Eric Anholt673a3942008-07-30 12:06:12 -07004173 * This should get us reasonable parallelism between CPU and GPU but also
4174 * relatively low latency when blocking on a particular request to finish.
4175 */
4176static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004177i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004178{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004179 struct drm_i915_private *dev_priv = dev->dev_private;
4180 struct drm_i915_file_private *file_priv = file->driver_priv;
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004181 unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
John Harrison54fb2412014-11-24 18:49:27 +00004182 struct drm_i915_gem_request *request, *target = NULL;
Daniel Vetterf69061b2012-12-06 09:01:42 +01004183 unsigned reset_counter;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004184 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004185
Daniel Vetter308887a2012-11-14 17:14:06 +01004186 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
4187 if (ret)
4188 return ret;
4189
4190 ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
4191 if (ret)
4192 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004193
Chris Wilson1c255952010-09-26 11:03:27 +01004194 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004195 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00004196 if (time_after_eq(request->emitted_jiffies, recent_enough))
4197 break;
4198
John Harrisonfcfa423c2015-05-29 17:44:12 +01004199 /*
4200 * Note that the request might not have been submitted yet.
4201 * In which case emitted_jiffies will be zero.
4202 */
4203 if (!request->emitted_jiffies)
4204 continue;
4205
John Harrison54fb2412014-11-24 18:49:27 +00004206 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004207 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01004208 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
John Harrisonff865882014-11-24 18:49:28 +00004209 if (target)
4210 i915_gem_request_reference(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004211 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004212
John Harrison54fb2412014-11-24 18:49:27 +00004213 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004214 return 0;
4215
John Harrison9c654812014-11-24 18:49:35 +00004216 ret = __i915_wait_request(target, reset_counter, true, NULL, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004217 if (ret == 0)
4218 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00004219
Chris Wilson41037f92015-03-27 11:01:36 +00004220 i915_gem_request_unreference__unlocked(target);
John Harrisonff865882014-11-24 18:49:28 +00004221
Eric Anholt673a3942008-07-30 12:06:12 -07004222 return ret;
4223}
4224
Chris Wilsond23db882014-05-23 08:48:08 +02004225static bool
4226i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
4227{
4228 struct drm_i915_gem_object *obj = vma->obj;
4229
4230 if (alignment &&
4231 vma->node.start & (alignment - 1))
4232 return true;
4233
4234 if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
4235 return true;
4236
4237 if (flags & PIN_OFFSET_BIAS &&
4238 vma->node.start < (flags & PIN_OFFSET_MASK))
4239 return true;
4240
Chris Wilson506a8e82015-12-08 11:55:07 +00004241 if (flags & PIN_OFFSET_FIXED &&
4242 vma->node.start != (flags & PIN_OFFSET_MASK))
4243 return true;
4244
Chris Wilsond23db882014-05-23 08:48:08 +02004245 return false;
4246}
4247
Chris Wilsond0710ab2015-11-20 14:16:39 +00004248void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
4249{
4250 struct drm_i915_gem_object *obj = vma->obj;
4251 bool mappable, fenceable;
4252 u32 fence_size, fence_alignment;
4253
4254 fence_size = i915_gem_get_gtt_size(obj->base.dev,
4255 obj->base.size,
4256 obj->tiling_mode);
4257 fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
4258 obj->base.size,
4259 obj->tiling_mode,
4260 true);
4261
4262 fenceable = (vma->node.size == fence_size &&
4263 (vma->node.start & (fence_alignment - 1)) == 0);
4264
4265 mappable = (vma->node.start + fence_size <=
Joonas Lahtinen62106b42016-03-18 10:42:57 +02004266 to_i915(obj->base.dev)->ggtt.mappable_end);
Chris Wilsond0710ab2015-11-20 14:16:39 +00004267
4268 obj->map_and_fenceable = mappable && fenceable;
4269}
4270
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004271static int
4272i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
4273 struct i915_address_space *vm,
4274 const struct i915_ggtt_view *ggtt_view,
4275 uint32_t alignment,
4276 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07004277{
Ben Widawsky6e7186a2014-05-06 22:21:36 -07004278 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004279 struct i915_vma *vma;
Chris Wilsonef79e172014-10-31 13:53:52 +00004280 unsigned bound;
Eric Anholt673a3942008-07-30 12:06:12 -07004281 int ret;
4282
Ben Widawsky6e7186a2014-05-06 22:21:36 -07004283 if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
4284 return -ENODEV;
4285
Daniel Vetterbf3d1492014-02-14 14:01:12 +01004286 if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
Daniel Vetter1ec9e262014-02-14 14:01:11 +01004287 return -EINVAL;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004288
Chris Wilsonc826c442014-10-31 13:53:53 +00004289 if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
4290 return -EINVAL;
4291
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004292 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
4293 return -EINVAL;
4294
4295 vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
4296 i915_gem_obj_to_vma(obj, vm);
4297
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004298 if (vma) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004299 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
4300 return -EBUSY;
4301
Chris Wilsond23db882014-05-23 08:48:08 +02004302 if (i915_vma_misplaced(vma, alignment, flags)) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004303 WARN(vma->pin_count,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004304 "bo is already pinned in %s with incorrect alignment:"
Michel Thierry088e0df2015-08-07 17:40:17 +01004305 " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004306 " obj->map_and_fenceable=%d\n",
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004307 ggtt_view ? "ggtt" : "ppgtt",
Michel Thierry088e0df2015-08-07 17:40:17 +01004308 upper_32_bits(vma->node.start),
4309 lower_32_bits(vma->node.start),
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004310 alignment,
Chris Wilsond23db882014-05-23 08:48:08 +02004311 !!(flags & PIN_MAPPABLE),
Chris Wilson05394f32010-11-08 19:18:58 +00004312 obj->map_and_fenceable);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004313 ret = i915_vma_unbind(vma);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004314 if (ret)
4315 return ret;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004316
4317 vma = NULL;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004318 }
4319 }
4320
Chris Wilsonef79e172014-10-31 13:53:52 +00004321 bound = vma ? vma->bound : 0;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004322 if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004323 vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
4324 flags);
Daniel Vetter262de142014-02-14 14:01:20 +01004325 if (IS_ERR(vma))
4326 return PTR_ERR(vma);
Daniel Vetter08755462015-04-20 09:04:05 -07004327 } else {
4328 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004329 if (ret)
4330 return ret;
4331 }
Daniel Vetter74898d72012-02-15 23:50:22 +01004332
Joonas Lahtinen91e67112015-05-06 14:33:58 +03004333 if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
4334 (bound ^ vma->bound) & GLOBAL_BIND) {
Chris Wilsond0710ab2015-11-20 14:16:39 +00004335 __i915_vma_set_map_and_fenceable(vma);
Joonas Lahtinen91e67112015-05-06 14:33:58 +03004336 WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
4337 }
Chris Wilsonef79e172014-10-31 13:53:52 +00004338
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004339 vma->pin_count++;
Eric Anholt673a3942008-07-30 12:06:12 -07004340 return 0;
4341}
4342
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004343int
4344i915_gem_object_pin(struct drm_i915_gem_object *obj,
4345 struct i915_address_space *vm,
4346 uint32_t alignment,
4347 uint64_t flags)
4348{
4349 return i915_gem_object_do_pin(obj, vm,
4350 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
4351 alignment, flags);
4352}
4353
4354int
4355i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4356 const struct i915_ggtt_view *view,
4357 uint32_t alignment,
4358 uint64_t flags)
4359{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004360 struct drm_device *dev = obj->base.dev;
4361 struct drm_i915_private *dev_priv = to_i915(dev);
4362 struct i915_ggtt *ggtt = &dev_priv->ggtt;
4363
Matthew Auldade7daa2016-03-24 15:54:20 +00004364 BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004365
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004366 return i915_gem_object_do_pin(obj, &ggtt->base, view,
Tvrtko Ursulin6fafab72015-03-17 15:36:51 +00004367 alignment, flags | PIN_GLOBAL);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004368}
4369
Eric Anholt673a3942008-07-30 12:06:12 -07004370void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004371i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
4372 const struct i915_ggtt_view *view)
Eric Anholt673a3942008-07-30 12:06:12 -07004373{
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004374 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
Eric Anholt673a3942008-07-30 12:06:12 -07004375
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004376 BUG_ON(!vma);
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004377 WARN_ON(vma->pin_count == 0);
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004378 WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004379
Chris Wilson30154652015-04-07 17:28:24 +01004380 --vma->pin_count;
Eric Anholt673a3942008-07-30 12:06:12 -07004381}
4382
4383int
Eric Anholt673a3942008-07-30 12:06:12 -07004384i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004385 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004386{
4387 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004388 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004389 int ret;
4390
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004391 ret = i915_mutex_lock_interruptible(dev);
4392 if (ret)
4393 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004394
Chris Wilson05394f32010-11-08 19:18:58 +00004395 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004396 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004397 ret = -ENOENT;
4398 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004399 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004400
Chris Wilson0be555b2010-08-04 15:36:30 +01004401 /* Count all active objects as busy, even if they are currently not used
4402 * by the gpu. Users of this interface expect objects to eventually
4403 * become non-busy without any further actions, therefore emit any
4404 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004405 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004406 ret = i915_gem_object_flush_active(obj);
Chris Wilsonb4716182015-04-27 13:41:17 +01004407 if (ret)
4408 goto unref;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004409
Chris Wilson426960b2016-01-15 16:51:46 +00004410 args->busy = 0;
4411 if (obj->active) {
4412 int i;
4413
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004414 for (i = 0; i < I915_NUM_ENGINES; i++) {
Chris Wilson426960b2016-01-15 16:51:46 +00004415 struct drm_i915_gem_request *req;
4416
4417 req = obj->last_read_req[i];
4418 if (req)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00004419 args->busy |= 1 << (16 + req->engine->exec_id);
Chris Wilson426960b2016-01-15 16:51:46 +00004420 }
4421 if (obj->last_write_req)
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00004422 args->busy |= obj->last_write_req->engine->exec_id;
Chris Wilson426960b2016-01-15 16:51:46 +00004423 }
Eric Anholt673a3942008-07-30 12:06:12 -07004424
Chris Wilsonb4716182015-04-27 13:41:17 +01004425unref:
Chris Wilson05394f32010-11-08 19:18:58 +00004426 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004427unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004428 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004429 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004430}
4431
4432int
4433i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4434 struct drm_file *file_priv)
4435{
Akshay Joshi0206e352011-08-16 15:34:10 -04004436 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004437}
4438
Chris Wilson3ef94da2009-09-14 16:50:29 +01004439int
4440i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4441 struct drm_file *file_priv)
4442{
Daniel Vetter656bfa32014-11-20 09:26:30 +01004443 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004444 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004445 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004446 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004447
4448 switch (args->madv) {
4449 case I915_MADV_DONTNEED:
4450 case I915_MADV_WILLNEED:
4451 break;
4452 default:
4453 return -EINVAL;
4454 }
4455
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004456 ret = i915_mutex_lock_interruptible(dev);
4457 if (ret)
4458 return ret;
4459
Chris Wilson05394f32010-11-08 19:18:58 +00004460 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004461 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004462 ret = -ENOENT;
4463 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004464 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004465
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004466 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004467 ret = -EINVAL;
4468 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004469 }
4470
Daniel Vetter656bfa32014-11-20 09:26:30 +01004471 if (obj->pages &&
4472 obj->tiling_mode != I915_TILING_NONE &&
4473 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4474 if (obj->madv == I915_MADV_WILLNEED)
4475 i915_gem_object_unpin_pages(obj);
4476 if (args->madv == I915_MADV_WILLNEED)
4477 i915_gem_object_pin_pages(obj);
4478 }
4479
Chris Wilson05394f32010-11-08 19:18:58 +00004480 if (obj->madv != __I915_MADV_PURGED)
4481 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004482
Chris Wilson6c085a72012-08-20 11:40:46 +02004483 /* if the object is no longer attached, discard its backing storage */
Daniel Vetterbe6a0372015-03-18 10:46:04 +01004484 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004485 i915_gem_object_truncate(obj);
4486
Chris Wilson05394f32010-11-08 19:18:58 +00004487 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004488
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004489out:
Chris Wilson05394f32010-11-08 19:18:58 +00004490 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004491unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004492 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004493 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004494}
4495
Chris Wilson37e680a2012-06-07 15:38:42 +01004496void i915_gem_object_init(struct drm_i915_gem_object *obj,
4497 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004498{
Chris Wilsonb4716182015-04-27 13:41:17 +01004499 int i;
4500
Ben Widawsky35c20a62013-05-31 11:28:48 -07004501 INIT_LIST_HEAD(&obj->global_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00004502 for (i = 0; i < I915_NUM_ENGINES; i++)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004503 INIT_LIST_HEAD(&obj->engine_list[i]);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004504 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004505 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004506 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004507
Chris Wilson37e680a2012-06-07 15:38:42 +01004508 obj->ops = ops;
4509
Chris Wilson0327d6b2012-08-11 15:41:06 +01004510 obj->fence_reg = I915_FENCE_REG_NONE;
4511 obj->madv = I915_MADV_WILLNEED;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004512
4513 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
4514}
4515
Chris Wilson37e680a2012-06-07 15:38:42 +01004516static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
Chris Wilsonde472662016-01-22 18:32:31 +00004517 .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
Chris Wilson37e680a2012-06-07 15:38:42 +01004518 .get_pages = i915_gem_object_get_pages_gtt,
4519 .put_pages = i915_gem_object_put_pages_gtt,
4520};
4521
Chris Wilson05394f32010-11-08 19:18:58 +00004522struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4523 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004524{
Daniel Vetterc397b902010-04-09 19:05:07 +00004525 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004526 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004527 gfp_t mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00004528
Chris Wilson42dcedd2012-11-15 11:32:30 +00004529 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004530 if (obj == NULL)
4531 return NULL;
4532
4533 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
Chris Wilson42dcedd2012-11-15 11:32:30 +00004534 i915_gem_object_free(obj);
Daniel Vetterc397b902010-04-09 19:05:07 +00004535 return NULL;
4536 }
4537
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004538 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4539 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4540 /* 965gm cannot relocate objects above 4GiB. */
4541 mask &= ~__GFP_HIGHMEM;
4542 mask |= __GFP_DMA32;
4543 }
4544
Al Viro496ad9a2013-01-23 17:07:38 -05004545 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004546 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004547
Chris Wilson37e680a2012-06-07 15:38:42 +01004548 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004549
Daniel Vetterc397b902010-04-09 19:05:07 +00004550 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4551 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4552
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004553 if (HAS_LLC(dev)) {
4554 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004555 * cache) for about a 10% performance improvement
4556 * compared to uncached. Graphics requests other than
4557 * display scanout are coherent with the CPU in
4558 * accessing this cache. This means in this mode we
4559 * don't need to clflush on the CPU side, and on the
4560 * GPU side we only need to flush internal caches to
4561 * get data visible to the CPU.
4562 *
4563 * However, we maintain the display planes as UC, and so
4564 * need to rebind when first used as such.
4565 */
4566 obj->cache_level = I915_CACHE_LLC;
4567 } else
4568 obj->cache_level = I915_CACHE_NONE;
4569
Daniel Vetterd861e332013-07-24 23:25:03 +02004570 trace_i915_gem_object_create(obj);
4571
Chris Wilson05394f32010-11-08 19:18:58 +00004572 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004573}
4574
Chris Wilson340fbd82014-05-22 09:16:52 +01004575static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4576{
4577 /* If we are the last user of the backing storage (be it shmemfs
4578 * pages or stolen etc), we know that the pages are going to be
4579 * immediately released. In this case, we can then skip copying
4580 * back the contents from the GPU.
4581 */
4582
4583 if (obj->madv != I915_MADV_WILLNEED)
4584 return false;
4585
4586 if (obj->base.filp == NULL)
4587 return true;
4588
4589 /* At first glance, this looks racy, but then again so would be
4590 * userspace racing mmap against close. However, the first external
4591 * reference to the filp can only be obtained through the
4592 * i915_gem_mmap_ioctl() which safeguards us against the user
4593 * acquiring such a reference whilst we are in the middle of
4594 * freeing the object.
4595 */
4596 return atomic_long_read(&obj->base.filp->f_count) == 1;
4597}
4598
Chris Wilson1488fc02012-04-24 15:47:31 +01004599void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004600{
Chris Wilson1488fc02012-04-24 15:47:31 +01004601 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004602 struct drm_device *dev = obj->base.dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004603 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004604 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004605
Paulo Zanonif65c9162013-11-27 18:20:34 -02004606 intel_runtime_pm_get(dev_priv);
4607
Chris Wilson26e12f892011-03-20 11:20:19 +00004608 trace_i915_gem_object_destroy(obj);
4609
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004610 list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004611 int ret;
4612
4613 vma->pin_count = 0;
4614 ret = i915_vma_unbind(vma);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004615 if (WARN_ON(ret == -ERESTARTSYS)) {
4616 bool was_interruptible;
Chris Wilson1488fc02012-04-24 15:47:31 +01004617
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004618 was_interruptible = dev_priv->mm.interruptible;
4619 dev_priv->mm.interruptible = false;
Chris Wilson1488fc02012-04-24 15:47:31 +01004620
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004621 WARN_ON(i915_vma_unbind(vma));
Chris Wilson1488fc02012-04-24 15:47:31 +01004622
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004623 dev_priv->mm.interruptible = was_interruptible;
4624 }
Chris Wilson1488fc02012-04-24 15:47:31 +01004625 }
4626
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004627 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4628 * before progressing. */
4629 if (obj->stolen)
4630 i915_gem_object_unpin_pages(obj);
4631
Daniel Vettera071fa02014-06-18 23:28:09 +02004632 WARN_ON(obj->frontbuffer_bits);
4633
Daniel Vetter656bfa32014-11-20 09:26:30 +01004634 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4635 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
4636 obj->tiling_mode != I915_TILING_NONE)
4637 i915_gem_object_unpin_pages(obj);
4638
Ben Widawsky401c29f2013-05-31 11:28:47 -07004639 if (WARN_ON(obj->pages_pin_count))
4640 obj->pages_pin_count = 0;
Chris Wilson340fbd82014-05-22 09:16:52 +01004641 if (discard_backing_storage(obj))
Chris Wilson55372522014-03-25 13:23:06 +00004642 obj->madv = I915_MADV_DONTNEED;
Chris Wilson37e680a2012-06-07 15:38:42 +01004643 i915_gem_object_put_pages(obj);
Chris Wilsond8cb5082012-08-11 15:41:03 +01004644 i915_gem_object_free_mmap_offset(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004645
Chris Wilson9da3da62012-06-01 15:20:22 +01004646 BUG_ON(obj->pages);
4647
Chris Wilson2f745ad2012-09-04 21:02:58 +01004648 if (obj->base.import_attach)
4649 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004650
Chris Wilson5cc9ed42014-05-16 14:22:37 +01004651 if (obj->ops->release)
4652 obj->ops->release(obj);
4653
Chris Wilson05394f32010-11-08 19:18:58 +00004654 drm_gem_object_release(&obj->base);
4655 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004656
Chris Wilson05394f32010-11-08 19:18:58 +00004657 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004658 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004659
4660 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004661}
4662
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004663struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
4664 struct i915_address_space *vm)
Ben Widawsky2f633152013-07-17 12:19:03 -07004665{
Daniel Vettere656a6c2013-08-14 14:14:04 +02004666 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004667 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Tvrtko Ursulin1b683722015-11-12 11:59:55 +00004668 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
4669 vma->vm == vm)
Daniel Vettere656a6c2013-08-14 14:14:04 +02004670 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004671 }
4672 return NULL;
4673}
Daniel Vettere656a6c2013-08-14 14:14:04 +02004674
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004675struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
4676 const struct i915_ggtt_view *view)
4677{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004678 struct drm_device *dev = obj->base.dev;
4679 struct drm_i915_private *dev_priv = to_i915(dev);
4680 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004681 struct i915_vma *vma;
4682
Matthew Auldade7daa2016-03-24 15:54:20 +00004683 BUG_ON(!view);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004684
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004685 list_for_each_entry(vma, &obj->vma_list, obj_link)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004686 if (vma->vm == &ggtt->base &&
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004687 i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004688 return vma;
Daniel Vettere656a6c2013-08-14 14:14:04 +02004689 return NULL;
4690}
4691
Ben Widawsky2f633152013-07-17 12:19:03 -07004692void i915_gem_vma_destroy(struct i915_vma *vma)
4693{
4694 WARN_ON(vma->node.allocated);
Chris Wilsonaaa05662013-08-20 12:56:40 +01004695
4696 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4697 if (!list_empty(&vma->exec_list))
4698 return;
4699
Chris Wilson596c5922016-02-26 11:03:20 +00004700 if (!vma->is_ggtt)
4701 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004702
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00004703 list_del(&vma->obj_link);
Daniel Vetterb93dab62013-08-26 11:23:47 +02004704
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004705 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
Ben Widawsky2f633152013-07-17 12:19:03 -07004706}
4707
Chris Wilsone3efda42014-04-09 09:19:41 +01004708static void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004709i915_gem_stop_engines(struct drm_device *dev)
Chris Wilsone3efda42014-04-09 09:19:41 +01004710{
4711 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004712 struct intel_engine_cs *engine;
Chris Wilsone3efda42014-04-09 09:19:41 +01004713
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004714 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004715 dev_priv->gt.stop_engine(engine);
Chris Wilsone3efda42014-04-09 09:19:41 +01004716}
4717
Jesse Barnes5669fca2009-02-17 15:13:31 -08004718int
Chris Wilson45c5f202013-10-16 11:50:01 +01004719i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004720{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004721 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson45c5f202013-10-16 11:50:01 +01004722 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004723
Chris Wilson45c5f202013-10-16 11:50:01 +01004724 mutex_lock(&dev->struct_mutex);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004725 ret = i915_gpu_idle(dev);
Chris Wilsonf7403342013-09-13 23:57:04 +01004726 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004727 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004728
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004729 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004730
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004731 i915_gem_stop_engines(dev);
Chris Wilson45c5f202013-10-16 11:50:01 +01004732 mutex_unlock(&dev->struct_mutex);
4733
Chris Wilson737b1502015-01-26 18:03:03 +02004734 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004735 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
Deepak S274fa1c2014-08-05 07:51:20 -07004736 flush_delayed_work(&dev_priv->mm.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004737
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004738 /* Assert that we sucessfully flushed all the work and
4739 * reset the GPU back to its idle, low power state.
4740 */
4741 WARN_ON(dev_priv->mm.busy);
4742
Eric Anholt673a3942008-07-30 12:06:12 -07004743 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004744
4745err:
4746 mutex_unlock(&dev->struct_mutex);
4747 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004748}
4749
John Harrison6909a662015-05-29 17:43:51 +01004750int i915_gem_l3_remap(struct drm_i915_gem_request *req, int slice)
Ben Widawskyb9524a12012-05-25 16:56:24 -07004751{
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00004752 struct intel_engine_cs *engine = req->engine;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004753 struct drm_device *dev = engine->dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004754 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07004755 u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
Ben Widawskyc3787e22013-09-17 21:12:44 -07004756 int i, ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004757
Ben Widawsky040d2ba2013-09-19 11:01:40 -07004758 if (!HAS_L3_DPF(dev) || !remap_info)
Ben Widawskyc3787e22013-09-17 21:12:44 -07004759 return 0;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004760
John Harrison5fb9de12015-05-29 17:44:07 +01004761 ret = intel_ring_begin(req, GEN7_L3LOG_SIZE / 4 * 3);
Ben Widawskyc3787e22013-09-17 21:12:44 -07004762 if (ret)
4763 return ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004764
Ben Widawskyc3787e22013-09-17 21:12:44 -07004765 /*
4766 * Note: We do not worry about the concurrent register cacheline hang
4767 * here because no other code should access these registers other than
4768 * at initialization time.
4769 */
Ville Syrjälä6fa1c5f2015-11-04 23:20:02 +02004770 for (i = 0; i < GEN7_L3LOG_SIZE / 4; i++) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004771 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
4772 intel_ring_emit_reg(engine, GEN7_L3LOG(slice, i));
4773 intel_ring_emit(engine, remap_info[i]);
Ben Widawskyb9524a12012-05-25 16:56:24 -07004774 }
4775
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004776 intel_ring_advance(engine);
Ben Widawskyb9524a12012-05-25 16:56:24 -07004777
Ben Widawskyc3787e22013-09-17 21:12:44 -07004778 return ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004779}
4780
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004781void i915_gem_init_swizzling(struct drm_device *dev)
4782{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004783 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004784
Daniel Vetter11782b02012-01-31 16:47:55 +01004785 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004786 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4787 return;
4788
4789 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4790 DISP_TILE_SURFACE_SWIZZLING);
4791
Daniel Vetter11782b02012-01-31 16:47:55 +01004792 if (IS_GEN5(dev))
4793 return;
4794
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004795 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4796 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004797 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004798 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004799 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky31a53362013-11-02 21:07:04 -07004800 else if (IS_GEN8(dev))
4801 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004802 else
4803 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004804}
Daniel Vettere21af882012-02-09 20:53:27 +01004805
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004806static void init_unused_ring(struct drm_device *dev, u32 base)
4807{
4808 struct drm_i915_private *dev_priv = dev->dev_private;
4809
4810 I915_WRITE(RING_CTL(base), 0);
4811 I915_WRITE(RING_HEAD(base), 0);
4812 I915_WRITE(RING_TAIL(base), 0);
4813 I915_WRITE(RING_START(base), 0);
4814}
4815
4816static void init_unused_rings(struct drm_device *dev)
4817{
4818 if (IS_I830(dev)) {
4819 init_unused_ring(dev, PRB1_BASE);
4820 init_unused_ring(dev, SRB0_BASE);
4821 init_unused_ring(dev, SRB1_BASE);
4822 init_unused_ring(dev, SRB2_BASE);
4823 init_unused_ring(dev, SRB3_BASE);
4824 } else if (IS_GEN2(dev)) {
4825 init_unused_ring(dev, SRB0_BASE);
4826 init_unused_ring(dev, SRB1_BASE);
4827 } else if (IS_GEN3(dev)) {
4828 init_unused_ring(dev, PRB1_BASE);
4829 init_unused_ring(dev, PRB2_BASE);
4830 }
4831}
4832
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004833int i915_gem_init_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004834{
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004835 struct drm_i915_private *dev_priv = dev->dev_private;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004836 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004837
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004838 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004839 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004840 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004841
4842 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004843 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004844 if (ret)
4845 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004846 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004847
Jani Nikulad39398f2015-10-07 11:17:44 +03004848 if (HAS_BLT(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01004849 ret = intel_init_blt_ring_buffer(dev);
4850 if (ret)
4851 goto cleanup_bsd_ring;
4852 }
4853
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004854 if (HAS_VEBOX(dev)) {
4855 ret = intel_init_vebox_ring_buffer(dev);
4856 if (ret)
4857 goto cleanup_blt_ring;
4858 }
4859
Zhao Yakui845f74a2014-04-17 10:37:37 +08004860 if (HAS_BSD2(dev)) {
4861 ret = intel_init_bsd2_ring_buffer(dev);
4862 if (ret)
4863 goto cleanup_vebox_ring;
4864 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004865
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004866 return 0;
4867
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004868cleanup_vebox_ring:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004869 intel_cleanup_engine(&dev_priv->engine[VECS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004870cleanup_blt_ring:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004871 intel_cleanup_engine(&dev_priv->engine[BCS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004872cleanup_bsd_ring:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004873 intel_cleanup_engine(&dev_priv->engine[VCS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004874cleanup_render_ring:
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004875 intel_cleanup_engine(&dev_priv->engine[RCS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004876
4877 return ret;
4878}
4879
4880int
4881i915_gem_init_hw(struct drm_device *dev)
4882{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004883 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004884 struct intel_engine_cs *engine;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004885 int ret, j;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004886
4887 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
4888 return -EIO;
4889
Chris Wilson5e4f5182015-02-13 14:35:59 +00004890 /* Double layer security blanket, see i915_gem_init() */
4891 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4892
Mika Kuoppala3accaf72016-04-13 17:26:43 +03004893 if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004894 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004895
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004896 if (IS_HASWELL(dev))
4897 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
4898 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004899
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004900 if (HAS_PCH_NOP(dev)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004901 if (IS_IVYBRIDGE(dev)) {
4902 u32 temp = I915_READ(GEN7_MSG_CTL);
4903 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4904 I915_WRITE(GEN7_MSG_CTL, temp);
4905 } else if (INTEL_INFO(dev)->gen >= 7) {
4906 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4907 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4908 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4909 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004910 }
4911
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004912 i915_gem_init_swizzling(dev);
4913
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004914 /*
4915 * At least 830 can leave some of the unused rings
4916 * "active" (ie. head != tail) after resume which
4917 * will prevent c3 entry. Makes sure all unused rings
4918 * are totally idle.
4919 */
4920 init_unused_rings(dev);
4921
Dave Gordoned54c1a2016-01-19 19:02:54 +00004922 BUG_ON(!dev_priv->kernel_context);
John Harrison90638cc2015-05-29 17:43:37 +01004923
John Harrison4ad2fd82015-06-18 13:11:20 +01004924 ret = i915_ppgtt_init_hw(dev);
4925 if (ret) {
4926 DRM_ERROR("PPGTT enable HW failed %d\n", ret);
4927 goto out;
4928 }
4929
4930 /* Need to do basic initialisation of all rings first: */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004931 for_each_engine(engine, dev_priv) {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004932 ret = engine->init_hw(engine);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004933 if (ret)
Chris Wilson5e4f5182015-02-13 14:35:59 +00004934 goto out;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004935 }
Mika Kuoppala99433932013-01-22 14:12:17 +02004936
Alex Dai33a732f2015-08-12 15:43:36 +01004937 /* We can't enable contexts until all firmware is loaded */
Jesse Barnes87bcdd22015-09-10 14:55:00 -07004938 if (HAS_GUC_UCODE(dev)) {
4939 ret = intel_guc_ucode_load(dev);
4940 if (ret) {
Daniel Vetter9f9e5392015-10-23 11:10:59 +02004941 DRM_ERROR("Failed to initialize GuC, error %d\n", ret);
4942 ret = -EIO;
4943 goto out;
Jesse Barnes87bcdd22015-09-10 14:55:00 -07004944 }
Alex Dai33a732f2015-08-12 15:43:36 +01004945 }
4946
Nick Hoathe84fe802015-09-11 12:53:46 +01004947 /*
4948 * Increment the next seqno by 0x100 so we have a visible break
4949 * on re-initialisation
4950 */
4951 ret = i915_gem_set_seqno(dev, dev_priv->next_seqno+0x100);
4952 if (ret)
4953 goto out;
4954
John Harrison4ad2fd82015-06-18 13:11:20 +01004955 /* Now it is safe to go back round and do everything else: */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004956 for_each_engine(engine, dev_priv) {
John Harrisondc4be60712015-05-29 17:43:39 +01004957 struct drm_i915_gem_request *req;
4958
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004959 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +00004960 if (IS_ERR(req)) {
4961 ret = PTR_ERR(req);
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004962 i915_gem_cleanup_engines(dev);
John Harrisondc4be60712015-05-29 17:43:39 +01004963 goto out;
4964 }
4965
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004966 if (engine->id == RCS) {
John Harrison4ad2fd82015-06-18 13:11:20 +01004967 for (j = 0; j < NUM_L3_SLICES(dev); j++)
John Harrison6909a662015-05-29 17:43:51 +01004968 i915_gem_l3_remap(req, j);
John Harrison4ad2fd82015-06-18 13:11:20 +01004969 }
Ben Widawskyc3787e22013-09-17 21:12:44 -07004970
John Harrisonb3dd6b92015-05-29 17:43:40 +01004971 ret = i915_ppgtt_init_ring(req);
John Harrison4ad2fd82015-06-18 13:11:20 +01004972 if (ret && ret != -EIO) {
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004973 DRM_ERROR("PPGTT enable %s failed %d\n",
4974 engine->name, ret);
John Harrisondc4be60712015-05-29 17:43:39 +01004975 i915_gem_request_cancel(req);
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004976 i915_gem_cleanup_engines(dev);
John Harrison4ad2fd82015-06-18 13:11:20 +01004977 goto out;
4978 }
David Woodhousef48a0162015-01-20 17:21:42 +00004979
John Harrisonb3dd6b92015-05-29 17:43:40 +01004980 ret = i915_gem_context_enable(req);
John Harrison90638cc2015-05-29 17:43:37 +01004981 if (ret && ret != -EIO) {
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004982 DRM_ERROR("Context enable %s failed %d\n",
4983 engine->name, ret);
John Harrisondc4be60712015-05-29 17:43:39 +01004984 i915_gem_request_cancel(req);
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00004985 i915_gem_cleanup_engines(dev);
John Harrison90638cc2015-05-29 17:43:37 +01004986 goto out;
4987 }
John Harrisondc4be60712015-05-29 17:43:39 +01004988
John Harrison75289872015-05-29 17:43:49 +01004989 i915_add_request_no_flush(req);
Daniel Vetter82460d92014-08-06 20:19:53 +02004990 }
4991
Chris Wilson5e4f5182015-02-13 14:35:59 +00004992out:
4993 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004994 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004995}
4996
Chris Wilson1070a422012-04-24 15:47:41 +01004997int i915_gem_init(struct drm_device *dev)
4998{
4999 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1070a422012-04-24 15:47:41 +01005000 int ret;
5001
Oscar Mateo127f1002014-07-24 17:04:11 +01005002 i915.enable_execlists = intel_sanitize_enable_execlists(dev,
5003 i915.enable_execlists);
5004
Chris Wilson1070a422012-04-24 15:47:41 +01005005 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08005006
Oscar Mateoa83014d2014-07-24 17:04:21 +01005007 if (!i915.enable_execlists) {
John Harrisonf3dc74c2015-03-19 12:30:06 +00005008 dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005009 dev_priv->gt.init_engines = i915_gem_init_engines;
5010 dev_priv->gt.cleanup_engine = intel_cleanup_engine;
5011 dev_priv->gt.stop_engine = intel_stop_engine;
Oscar Mateo454afeb2014-07-24 17:04:22 +01005012 } else {
John Harrisonf3dc74c2015-03-19 12:30:06 +00005013 dev_priv->gt.execbuf_submit = intel_execlists_submission;
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005014 dev_priv->gt.init_engines = intel_logical_rings_init;
5015 dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
5016 dev_priv->gt.stop_engine = intel_logical_ring_stop;
Oscar Mateoa83014d2014-07-24 17:04:21 +01005017 }
5018
Chris Wilson5e4f5182015-02-13 14:35:59 +00005019 /* This is just a security blanket to placate dragons.
5020 * On some systems, we very sporadically observe that the first TLBs
5021 * used by the CS may be stale, despite us poking the TLB reset. If
5022 * we hold the forcewake during initialisation these problems
5023 * just magically go away.
5024 */
5025 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5026
Daniel Vetter6c5566a2014-08-06 15:04:50 +02005027 ret = i915_gem_init_userptr(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02005028 if (ret)
5029 goto out_unlock;
Daniel Vetter6c5566a2014-08-06 15:04:50 +02005030
Joonas Lahtinend85489d2016-03-24 16:47:46 +02005031 i915_gem_init_ggtt(dev);
Jesse Barnesd62b4892013-03-08 10:45:53 -08005032
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005033 ret = i915_gem_context_init(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02005034 if (ret)
5035 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08005036
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005037 ret = dev_priv->gt.init_engines(dev);
Daniel Vetter35a57ff2014-11-20 00:33:07 +01005038 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02005039 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02005040
5041 ret = i915_gem_init_hw(dev);
Chris Wilson60990322014-04-09 09:19:42 +01005042 if (ret == -EIO) {
5043 /* Allow ring initialisation to fail by marking the GPU as
5044 * wedged. But we only want to do this where the GPU is angry,
5045 * for all other failure, such as an allocation failure, bail.
5046 */
5047 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
Peter Zijlstra805de8f42015-04-24 01:12:32 +02005048 atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
Chris Wilson60990322014-04-09 09:19:42 +01005049 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01005050 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02005051
5052out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00005053 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson60990322014-04-09 09:19:42 +01005054 mutex_unlock(&dev->struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01005055
Chris Wilson60990322014-04-09 09:19:42 +01005056 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01005057}
5058
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005059void
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005060i915_gem_cleanup_engines(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005061{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03005062 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005063 struct intel_engine_cs *engine;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005064
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005065 for_each_engine(engine, dev_priv)
Tvrtko Ursulin117897f2016-03-16 11:00:40 +00005066 dev_priv->gt.cleanup_engine(engine);
Niu,Binga6478282015-07-04 00:27:34 +08005067
Mika Kuoppalaee4b6fa2016-03-16 17:54:00 +02005068 if (i915.enable_execlists)
5069 /*
5070 * Neither the BIOS, ourselves or any other kernel
5071 * expects the system to be in execlists mode on startup,
5072 * so we need to reset the GPU back to legacy mode.
5073 */
5074 intel_gpu_reset(dev, ALL_ENGINES);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08005075}
5076
Chris Wilson64193402010-10-24 12:38:05 +01005077static void
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00005078init_engine_lists(struct intel_engine_cs *engine)
Chris Wilson64193402010-10-24 12:38:05 +01005079{
Tvrtko Ursulin0bc40be2016-03-16 11:00:37 +00005080 INIT_LIST_HEAD(&engine->active_list);
5081 INIT_LIST_HEAD(&engine->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01005082}
5083
Eric Anholt673a3942008-07-30 12:06:12 -07005084void
Imre Deak40ae4e12016-03-16 14:54:03 +02005085i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
5086{
5087 struct drm_device *dev = dev_priv->dev;
5088
5089 if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
5090 !IS_CHERRYVIEW(dev_priv))
5091 dev_priv->num_fence_regs = 32;
5092 else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
5093 IS_I945GM(dev_priv) || IS_G33(dev_priv))
5094 dev_priv->num_fence_regs = 16;
5095 else
5096 dev_priv->num_fence_regs = 8;
5097
5098 if (intel_vgpu_active(dev))
5099 dev_priv->num_fence_regs =
5100 I915_READ(vgtif_reg(avail_rs.fence_num));
5101
5102 /* Initialize fence registers to zero */
5103 i915_gem_restore_fences(dev);
5104
5105 i915_gem_detect_bit_6_swizzle(dev);
5106}
5107
5108void
Imre Deakd64aa092016-01-19 15:26:29 +02005109i915_gem_load_init(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07005110{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03005111 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005112 int i;
5113
Chris Wilsonefab6d82015-04-07 16:20:57 +01005114 dev_priv->objects =
Chris Wilson42dcedd2012-11-15 11:32:30 +00005115 kmem_cache_create("i915_gem_object",
5116 sizeof(struct drm_i915_gem_object), 0,
5117 SLAB_HWCACHE_ALIGN,
5118 NULL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01005119 dev_priv->vmas =
5120 kmem_cache_create("i915_gem_vma",
5121 sizeof(struct i915_vma), 0,
5122 SLAB_HWCACHE_ALIGN,
5123 NULL);
Chris Wilsonefab6d82015-04-07 16:20:57 +01005124 dev_priv->requests =
5125 kmem_cache_create("i915_gem_request",
5126 sizeof(struct drm_i915_gem_request), 0,
5127 SLAB_HWCACHE_ALIGN,
5128 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07005129
Ben Widawskyfc8c0672013-07-31 16:59:54 -07005130 INIT_LIST_HEAD(&dev_priv->vm_list);
Ben Widawskya33afea2013-09-17 21:12:45 -07005131 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02005132 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
5133 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07005134 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Tvrtko Ursulin666796d2016-03-16 11:00:39 +00005135 for (i = 0; i < I915_NUM_ENGINES; i++)
5136 init_engine_lists(&dev_priv->engine[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02005137 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02005138 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07005139 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
5140 i915_gem_retire_work_handler);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005141 INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
5142 i915_gem_idle_work_handler);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005143 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005144
Chris Wilson72bfa192010-12-19 11:42:05 +00005145 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
5146
Nick Hoathe84fe802015-09-11 12:53:46 +01005147 /*
5148 * Set initial sequence number for requests.
5149 * Using this number allows the wraparound to happen early,
5150 * catching any obvious problems.
5151 */
5152 dev_priv->next_seqno = ((u32)~0 - 0x1100);
5153 dev_priv->last_seqno = ((u32)~0 - 0x1101);
5154
Chris Wilson19b2dbd2013-06-12 10:15:12 +01005155 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Eric Anholt10ed13e2011-05-06 13:53:49 -07005156
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005157 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01005158
Chris Wilsonce453d82011-02-21 14:43:56 +00005159 dev_priv->mm.interruptible = true;
5160
Daniel Vetterf99d7062014-06-19 16:01:59 +02005161 mutex_init(&dev_priv->fb_tracking.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07005162}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005163
Imre Deakd64aa092016-01-19 15:26:29 +02005164void i915_gem_load_cleanup(struct drm_device *dev)
5165{
5166 struct drm_i915_private *dev_priv = to_i915(dev);
5167
5168 kmem_cache_destroy(dev_priv->requests);
5169 kmem_cache_destroy(dev_priv->vmas);
5170 kmem_cache_destroy(dev_priv->objects);
5171}
5172
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005173void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005174{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005175 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005176
5177 /* Clean up our request list when the client is going away, so that
5178 * later retire_requests won't dereference our soon-to-be-gone
5179 * file_priv.
5180 */
Chris Wilson1c255952010-09-26 11:03:27 +01005181 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005182 while (!list_empty(&file_priv->mm.request_list)) {
5183 struct drm_i915_gem_request *request;
5184
5185 request = list_first_entry(&file_priv->mm.request_list,
5186 struct drm_i915_gem_request,
5187 client_list);
5188 list_del(&request->client_list);
5189 request->file_priv = NULL;
5190 }
Chris Wilson1c255952010-09-26 11:03:27 +01005191 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01005192
Chris Wilson2e1b8732015-04-27 13:41:22 +01005193 if (!list_empty(&file_priv->rps.link)) {
Chris Wilson8d3afd72015-05-21 21:01:47 +01005194 spin_lock(&to_i915(dev)->rps.client_lock);
Chris Wilson2e1b8732015-04-27 13:41:22 +01005195 list_del(&file_priv->rps.link);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005196 spin_unlock(&to_i915(dev)->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005197 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005198}
5199
5200int i915_gem_open(struct drm_device *dev, struct drm_file *file)
5201{
5202 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005203 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005204
5205 DRM_DEBUG_DRIVER("\n");
5206
5207 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5208 if (!file_priv)
5209 return -ENOMEM;
5210
5211 file->driver_priv = file_priv;
5212 file_priv->dev_priv = dev->dev_private;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005213 file_priv->file = file;
Chris Wilson2e1b8732015-04-27 13:41:22 +01005214 INIT_LIST_HEAD(&file_priv->rps.link);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005215
5216 spin_lock_init(&file_priv->mm.lock);
5217 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005218
Tvrtko Ursulinde1add32016-01-15 15:12:50 +00005219 file_priv->bsd_ring = -1;
5220
Ben Widawskye422b882013-12-06 14:10:58 -08005221 ret = i915_gem_context_open(dev, file);
5222 if (ret)
5223 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005224
Ben Widawskye422b882013-12-06 14:10:58 -08005225 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005226}
5227
Daniel Vetterb680c372014-09-19 18:27:27 +02005228/**
5229 * i915_gem_track_fb - update frontbuffer tracking
Geliang Tangd9072a32015-09-15 05:58:44 -07005230 * @old: current GEM buffer for the frontbuffer slots
5231 * @new: new GEM buffer for the frontbuffer slots
5232 * @frontbuffer_bits: bitmask of frontbuffer slots
Daniel Vetterb680c372014-09-19 18:27:27 +02005233 *
5234 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5235 * from @old and setting them in @new. Both @old and @new can be NULL.
5236 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005237void i915_gem_track_fb(struct drm_i915_gem_object *old,
5238 struct drm_i915_gem_object *new,
5239 unsigned frontbuffer_bits)
5240{
5241 if (old) {
5242 WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
5243 WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
5244 old->frontbuffer_bits &= ~frontbuffer_bits;
5245 }
5246
5247 if (new) {
5248 WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
5249 WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
5250 new->frontbuffer_bits |= frontbuffer_bits;
5251 }
5252}
5253
Ben Widawskya70a3142013-07-31 16:59:56 -07005254/* All the new VM stuff */
Michel Thierry088e0df2015-08-07 17:40:17 +01005255u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
5256 struct i915_address_space *vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07005257{
5258 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
5259 struct i915_vma *vma;
5260
Daniel Vetter896ab1a2014-08-06 15:04:51 +02005261 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
Ben Widawskya70a3142013-07-31 16:59:56 -07005262
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005263 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00005264 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005265 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5266 continue;
5267 if (vma->vm == vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07005268 return vma->node.start;
Ben Widawskya70a3142013-07-31 16:59:56 -07005269 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005270
Daniel Vetterf25748ea2014-06-17 22:34:38 +02005271 WARN(1, "%s vma for this object not found.\n",
5272 i915_is_ggtt(vm) ? "global" : "ppgtt");
Ben Widawskya70a3142013-07-31 16:59:56 -07005273 return -1;
5274}
5275
Michel Thierry088e0df2015-08-07 17:40:17 +01005276u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
5277 const struct i915_ggtt_view *view)
Ben Widawskya70a3142013-07-31 16:59:56 -07005278{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005279 struct drm_i915_private *dev_priv = to_i915(o->base.dev);
5280 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawskya70a3142013-07-31 16:59:56 -07005281 struct i915_vma *vma;
5282
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005283 list_for_each_entry(vma, &o->vma_list, obj_link)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005284 if (vma->vm == &ggtt->base &&
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005285 i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005286 return vma->node.start;
5287
Tvrtko Ursulin5678ad72015-03-17 14:45:29 +00005288 WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005289 return -1;
5290}
5291
5292bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
5293 struct i915_address_space *vm)
5294{
5295 struct i915_vma *vma;
5296
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005297 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00005298 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005299 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5300 continue;
5301 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
5302 return true;
5303 }
5304
5305 return false;
5306}
5307
5308bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005309 const struct i915_ggtt_view *view)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005310{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005311 struct drm_i915_private *dev_priv = to_i915(o->base.dev);
5312 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005313 struct i915_vma *vma;
5314
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005315 list_for_each_entry(vma, &o->vma_list, obj_link)
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005316 if (vma->vm == &ggtt->base &&
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005317 i915_ggtt_view_equal(&vma->ggtt_view, view) &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00005318 drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07005319 return true;
5320
5321 return false;
5322}
5323
5324bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
5325{
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01005326 struct i915_vma *vma;
Ben Widawskya70a3142013-07-31 16:59:56 -07005327
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005328 list_for_each_entry(vma, &o->vma_list, obj_link)
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01005329 if (drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07005330 return true;
5331
5332 return false;
5333}
5334
5335unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
5336 struct i915_address_space *vm)
5337{
5338 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
5339 struct i915_vma *vma;
5340
Daniel Vetter896ab1a2014-08-06 15:04:51 +02005341 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
Ben Widawskya70a3142013-07-31 16:59:56 -07005342
5343 BUG_ON(list_empty(&o->vma_list));
5344
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005345 list_for_each_entry(vma, &o->vma_list, obj_link) {
Chris Wilson596c5922016-02-26 11:03:20 +00005346 if (vma->is_ggtt &&
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005347 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5348 continue;
Ben Widawskya70a3142013-07-31 16:59:56 -07005349 if (vma->vm == vm)
5350 return vma->node.size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005351 }
Ben Widawskya70a3142013-07-31 16:59:56 -07005352 return 0;
5353}
5354
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005355bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07005356{
5357 struct i915_vma *vma;
Chris Wilson1c7f4bc2016-02-26 11:03:19 +00005358 list_for_each_entry(vma, &obj->vma_list, obj_link)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005359 if (vma->pin_count > 0)
5360 return true;
Joonas Lahtinena6631ae2015-05-06 14:34:58 +03005361
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005362 return false;
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07005363}
Dave Gordonea702992015-07-09 19:29:02 +01005364
Dave Gordon033908a2015-12-10 18:51:23 +00005365/* Like i915_gem_object_get_page(), but mark the returned page dirty */
5366struct page *
5367i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
5368{
5369 struct page *page;
5370
5371 /* Only default objects have per-page dirty tracking */
Chris Wilsonde472662016-01-22 18:32:31 +00005372 if (WARN_ON((obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE) == 0))
Dave Gordon033908a2015-12-10 18:51:23 +00005373 return NULL;
5374
5375 page = i915_gem_object_get_page(obj, n);
5376 set_page_dirty(page);
5377 return page;
5378}
5379
Dave Gordonea702992015-07-09 19:29:02 +01005380/* Allocate a new GEM object and fill it with the supplied data */
5381struct drm_i915_gem_object *
5382i915_gem_object_create_from_data(struct drm_device *dev,
5383 const void *data, size_t size)
5384{
5385 struct drm_i915_gem_object *obj;
5386 struct sg_table *sg;
5387 size_t bytes;
5388 int ret;
5389
5390 obj = i915_gem_alloc_object(dev, round_up(size, PAGE_SIZE));
5391 if (IS_ERR_OR_NULL(obj))
5392 return obj;
5393
5394 ret = i915_gem_object_set_to_cpu_domain(obj, true);
5395 if (ret)
5396 goto fail;
5397
5398 ret = i915_gem_object_get_pages(obj);
5399 if (ret)
5400 goto fail;
5401
5402 i915_gem_object_pin_pages(obj);
5403 sg = obj->pages;
5404 bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
Dave Gordon9e7d18c2015-12-10 18:51:24 +00005405 obj->dirty = 1; /* Backing store is now out of date */
Dave Gordonea702992015-07-09 19:29:02 +01005406 i915_gem_object_unpin_pages(obj);
5407
5408 if (WARN_ON(bytes != size)) {
5409 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
5410 ret = -EFAULT;
5411 goto fail;
5412 }
5413
5414 return obj;
5415
5416fail:
5417 drm_gem_object_unreference(&obj->base);
5418 return ERR_PTR(ret);
5419}