blob: 5ff96f94c2ee6dfe62da0026522e46f775bc8992 [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
44i915_gem_object_retire(struct drm_i915_gem_object *obj);
45
Chris Wilson61050802012-04-17 15:31:31 +010046static void i915_gem_write_fence(struct drm_device *dev, int reg,
47 struct drm_i915_gem_object *obj);
48static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
49 struct drm_i915_fence_reg *fence,
50 bool enable);
51
Chris Wilsonc76ce032013-08-08 14:41:03 +010052static bool cpu_cache_is_coherent(struct drm_device *dev,
53 enum i915_cache_level level)
54{
55 return HAS_LLC(dev) || level != I915_CACHE_NONE;
56}
57
Chris Wilson2c225692013-08-09 12:26:45 +010058static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
59{
60 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
61 return true;
62
63 return obj->pin_display;
64}
65
Chris Wilson61050802012-04-17 15:31:31 +010066static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
67{
68 if (obj->tiling_mode)
69 i915_gem_release_mmap(obj);
70
71 /* As we do not have an associated fence register, we will force
72 * a tiling change if we ever need to acquire one.
73 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +010074 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +010075 obj->fence_reg = I915_FENCE_REG_NONE;
76}
77
Chris Wilson73aa8082010-09-30 11:46:12 +010078/* some bookkeeping */
79static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
80 size_t size)
81{
Daniel Vetterc20e8352013-07-24 22:40:23 +020082 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010083 dev_priv->mm.object_count++;
84 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020085 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010086}
87
88static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
89 size_t size)
90{
Daniel Vetterc20e8352013-07-24 22:40:23 +020091 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010092 dev_priv->mm.object_count--;
93 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020094 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010095}
96
Chris Wilson21dd3732011-01-26 15:55:56 +000097static int
Daniel Vetter33196de2012-11-14 17:14:05 +010098i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010099{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100100 int ret;
101
Daniel Vetter7abb6902013-05-24 21:29:32 +0200102#define EXIT_COND (!i915_reset_in_progress(error) || \
103 i915_terminally_wedged(error))
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100104 if (EXIT_COND)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100105 return 0;
106
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200107 /*
108 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
109 * userspace. If it takes that long something really bad is going on and
110 * we should simply try to bail out and fail as gracefully as possible.
111 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100112 ret = wait_event_interruptible_timeout(error->reset_queue,
113 EXIT_COND,
114 10*HZ);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200115 if (ret == 0) {
116 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
117 return -EIO;
118 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100119 return ret;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200120 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100121#undef EXIT_COND
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100122
Chris Wilson21dd3732011-01-26 15:55:56 +0000123 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100124}
125
Chris Wilson54cf91d2010-11-25 18:00:26 +0000126int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100127{
Daniel Vetter33196de2012-11-14 17:14:05 +0100128 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100129 int ret;
130
Daniel Vetter33196de2012-11-14 17:14:05 +0100131 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100132 if (ret)
133 return ret;
134
135 ret = mutex_lock_interruptible(&dev->struct_mutex);
136 if (ret)
137 return ret;
138
Chris Wilson23bc5982010-09-29 16:10:57 +0100139 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100140 return 0;
141}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100142
Eric Anholt673a3942008-07-30 12:06:12 -0700143int
Eric Anholt5a125c32008-10-22 21:40:13 -0700144i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000145 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700146{
Chris Wilson73aa8082010-09-30 11:46:12 +0100147 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700148 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000149 struct drm_i915_gem_object *obj;
150 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700151
Chris Wilson6299f992010-11-24 12:23:44 +0000152 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100153 mutex_lock(&dev->struct_mutex);
Ben Widawsky35c20a62013-05-31 11:28:48 -0700154 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800155 if (i915_gem_obj_is_pinned(obj))
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700156 pinned += i915_gem_obj_ggtt_size(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +0100157 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700158
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700159 args->aper_size = dev_priv->gtt.base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000161
Eric Anholt5a125c32008-10-22 21:40:13 -0700162 return 0;
163}
164
Chris Wilson6a2c4232014-11-04 04:51:40 -0800165static int
166i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
Chris Wilson00731152014-05-21 12:42:56 +0100167{
Chris Wilson6a2c4232014-11-04 04:51:40 -0800168 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
169 char *vaddr = obj->phys_handle->vaddr;
170 struct sg_table *st;
171 struct scatterlist *sg;
172 int i;
Chris Wilson00731152014-05-21 12:42:56 +0100173
Chris Wilson6a2c4232014-11-04 04:51:40 -0800174 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
175 return -EINVAL;
Chris Wilson00731152014-05-21 12:42:56 +0100176
Chris Wilson6a2c4232014-11-04 04:51:40 -0800177 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
178 struct page *page;
179 char *src;
180
181 page = shmem_read_mapping_page(mapping, i);
182 if (IS_ERR(page))
183 return PTR_ERR(page);
184
185 src = kmap_atomic(page);
186 memcpy(vaddr, src, PAGE_SIZE);
187 drm_clflush_virt_range(vaddr, PAGE_SIZE);
188 kunmap_atomic(src);
189
190 page_cache_release(page);
191 vaddr += PAGE_SIZE;
192 }
193
194 i915_gem_chipset_flush(obj->base.dev);
195
196 st = kmalloc(sizeof(*st), GFP_KERNEL);
197 if (st == NULL)
198 return -ENOMEM;
199
200 if (sg_alloc_table(st, 1, GFP_KERNEL)) {
201 kfree(st);
202 return -ENOMEM;
203 }
204
205 sg = st->sgl;
206 sg->offset = 0;
207 sg->length = obj->base.size;
208
209 sg_dma_address(sg) = obj->phys_handle->busaddr;
210 sg_dma_len(sg) = obj->base.size;
211
212 obj->pages = st;
213 obj->has_dma_mapping = true;
214 return 0;
215}
216
217static void
218i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
219{
220 int ret;
221
222 BUG_ON(obj->madv == __I915_MADV_PURGED);
223
224 ret = i915_gem_object_set_to_cpu_domain(obj, true);
225 if (ret) {
226 /* In the event of a disaster, abandon all caches and
227 * hope for the best.
228 */
229 WARN_ON(ret != -EIO);
230 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
231 }
232
233 if (obj->madv == I915_MADV_DONTNEED)
234 obj->dirty = 0;
235
236 if (obj->dirty) {
Chris Wilson00731152014-05-21 12:42:56 +0100237 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800238 char *vaddr = obj->phys_handle->vaddr;
Chris Wilson00731152014-05-21 12:42:56 +0100239 int i;
240
241 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
Chris Wilson6a2c4232014-11-04 04:51:40 -0800242 struct page *page;
243 char *dst;
Chris Wilson00731152014-05-21 12:42:56 +0100244
Chris Wilson6a2c4232014-11-04 04:51:40 -0800245 page = shmem_read_mapping_page(mapping, i);
246 if (IS_ERR(page))
247 continue;
248
249 dst = kmap_atomic(page);
250 drm_clflush_virt_range(vaddr, PAGE_SIZE);
251 memcpy(dst, vaddr, PAGE_SIZE);
252 kunmap_atomic(dst);
253
254 set_page_dirty(page);
255 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson00731152014-05-21 12:42:56 +0100256 mark_page_accessed(page);
Chris Wilson6a2c4232014-11-04 04:51:40 -0800257 page_cache_release(page);
Chris Wilson00731152014-05-21 12:42:56 +0100258 vaddr += PAGE_SIZE;
259 }
Chris Wilson6a2c4232014-11-04 04:51:40 -0800260 obj->dirty = 0;
Chris Wilson00731152014-05-21 12:42:56 +0100261 }
262
Chris Wilson6a2c4232014-11-04 04:51:40 -0800263 sg_free_table(obj->pages);
264 kfree(obj->pages);
265
266 obj->has_dma_mapping = false;
267}
268
269static void
270i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
271{
272 drm_pci_free(obj->base.dev, obj->phys_handle);
273}
274
275static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
276 .get_pages = i915_gem_object_get_pages_phys,
277 .put_pages = i915_gem_object_put_pages_phys,
278 .release = i915_gem_object_release_phys,
279};
280
281static int
282drop_pages(struct drm_i915_gem_object *obj)
283{
284 struct i915_vma *vma, *next;
285 int ret;
286
287 drm_gem_object_reference(&obj->base);
288 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link)
289 if (i915_vma_unbind(vma))
290 break;
291
292 ret = i915_gem_object_put_pages(obj);
293 drm_gem_object_unreference(&obj->base);
294
295 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100296}
297
298int
299i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
300 int align)
301{
302 drm_dma_handle_t *phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800303 int ret;
Chris Wilson00731152014-05-21 12:42:56 +0100304
305 if (obj->phys_handle) {
306 if ((unsigned long)obj->phys_handle->vaddr & (align -1))
307 return -EBUSY;
308
309 return 0;
310 }
311
312 if (obj->madv != I915_MADV_WILLNEED)
313 return -EFAULT;
314
315 if (obj->base.filp == NULL)
316 return -EINVAL;
317
Chris Wilson6a2c4232014-11-04 04:51:40 -0800318 ret = drop_pages(obj);
319 if (ret)
320 return ret;
321
Chris Wilson00731152014-05-21 12:42:56 +0100322 /* create a new object */
323 phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
324 if (!phys)
325 return -ENOMEM;
326
Chris Wilson00731152014-05-21 12:42:56 +0100327 obj->phys_handle = phys;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800328 obj->ops = &i915_gem_phys_ops;
329
330 return i915_gem_object_get_pages(obj);
Chris Wilson00731152014-05-21 12:42:56 +0100331}
332
333static int
334i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
335 struct drm_i915_gem_pwrite *args,
336 struct drm_file *file_priv)
337{
338 struct drm_device *dev = obj->base.dev;
339 void *vaddr = obj->phys_handle->vaddr + args->offset;
340 char __user *user_data = to_user_ptr(args->data_ptr);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200341 int ret = 0;
Chris Wilson6a2c4232014-11-04 04:51:40 -0800342
343 /* We manually control the domain here and pretend that it
344 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
345 */
346 ret = i915_gem_object_wait_rendering(obj, false);
347 if (ret)
348 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100349
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200350 intel_fb_obj_invalidate(obj, NULL, ORIGIN_CPU);
Chris Wilson00731152014-05-21 12:42:56 +0100351 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
352 unsigned long unwritten;
353
354 /* The physical object once assigned is fixed for the lifetime
355 * of the obj, so we can safely drop the lock and continue
356 * to access vaddr.
357 */
358 mutex_unlock(&dev->struct_mutex);
359 unwritten = copy_from_user(vaddr, user_data, args->size);
360 mutex_lock(&dev->struct_mutex);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200361 if (unwritten) {
362 ret = -EFAULT;
363 goto out;
364 }
Chris Wilson00731152014-05-21 12:42:56 +0100365 }
366
Chris Wilson6a2c4232014-11-04 04:51:40 -0800367 drm_clflush_virt_range(vaddr, args->size);
Chris Wilson00731152014-05-21 12:42:56 +0100368 i915_gem_chipset_flush(dev);
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200369
370out:
371 intel_fb_obj_flush(obj, false);
372 return ret;
Chris Wilson00731152014-05-21 12:42:56 +0100373}
374
Chris Wilson42dcedd2012-11-15 11:32:30 +0000375void *i915_gem_object_alloc(struct drm_device *dev)
376{
377 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonefab6d82015-04-07 16:20:57 +0100378 return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000379}
380
381void i915_gem_object_free(struct drm_i915_gem_object *obj)
382{
383 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsonefab6d82015-04-07 16:20:57 +0100384 kmem_cache_free(dev_priv->objects, obj);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000385}
386
Dave Airlieff72145b2011-02-07 12:16:14 +1000387static int
388i915_gem_create(struct drm_file *file,
389 struct drm_device *dev,
390 uint64_t size,
391 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700392{
Chris Wilson05394f32010-11-08 19:18:58 +0000393 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300394 int ret;
395 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700396
Dave Airlieff72145b2011-02-07 12:16:14 +1000397 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200398 if (size == 0)
399 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700400
401 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000402 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700403 if (obj == NULL)
404 return -ENOMEM;
405
Chris Wilson05394f32010-11-08 19:18:58 +0000406 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100407 /* drop reference from allocate - handle holds it now */
Daniel Vetterd861e332013-07-24 23:25:03 +0200408 drm_gem_object_unreference_unlocked(&obj->base);
409 if (ret)
410 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100411
Dave Airlieff72145b2011-02-07 12:16:14 +1000412 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700413 return 0;
414}
415
Dave Airlieff72145b2011-02-07 12:16:14 +1000416int
417i915_gem_dumb_create(struct drm_file *file,
418 struct drm_device *dev,
419 struct drm_mode_create_dumb *args)
420{
421 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300422 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000423 args->size = args->pitch * args->height;
424 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000425 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000426}
427
Dave Airlieff72145b2011-02-07 12:16:14 +1000428/**
429 * Creates a new mm object and returns a handle to it.
430 */
431int
432i915_gem_create_ioctl(struct drm_device *dev, void *data,
433 struct drm_file *file)
434{
435 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200436
Dave Airlieff72145b2011-02-07 12:16:14 +1000437 return i915_gem_create(file, dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +1000438 args->size, &args->handle);
Dave Airlieff72145b2011-02-07 12:16:14 +1000439}
440
Daniel Vetter8c599672011-12-14 13:57:31 +0100441static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100442__copy_to_user_swizzled(char __user *cpu_vaddr,
443 const char *gpu_vaddr, int gpu_offset,
444 int length)
445{
446 int ret, cpu_offset = 0;
447
448 while (length > 0) {
449 int cacheline_end = ALIGN(gpu_offset + 1, 64);
450 int this_length = min(cacheline_end - gpu_offset, length);
451 int swizzled_gpu_offset = gpu_offset ^ 64;
452
453 ret = __copy_to_user(cpu_vaddr + cpu_offset,
454 gpu_vaddr + swizzled_gpu_offset,
455 this_length);
456 if (ret)
457 return ret + length;
458
459 cpu_offset += this_length;
460 gpu_offset += this_length;
461 length -= this_length;
462 }
463
464 return 0;
465}
466
467static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700468__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
469 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100470 int length)
471{
472 int ret, cpu_offset = 0;
473
474 while (length > 0) {
475 int cacheline_end = ALIGN(gpu_offset + 1, 64);
476 int this_length = min(cacheline_end - gpu_offset, length);
477 int swizzled_gpu_offset = gpu_offset ^ 64;
478
479 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
480 cpu_vaddr + cpu_offset,
481 this_length);
482 if (ret)
483 return ret + length;
484
485 cpu_offset += this_length;
486 gpu_offset += this_length;
487 length -= this_length;
488 }
489
490 return 0;
491}
492
Brad Volkin4c914c02014-02-18 10:15:45 -0800493/*
494 * Pins the specified object's pages and synchronizes the object with
495 * GPU accesses. Sets needs_clflush to non-zero if the caller should
496 * flush the object from the CPU cache.
497 */
498int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
499 int *needs_clflush)
500{
501 int ret;
502
503 *needs_clflush = 0;
504
505 if (!obj->base.filp)
506 return -EINVAL;
507
508 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
509 /* If we're not in the cpu read domain, set ourself into the gtt
510 * read domain and manually flush cachelines (if required). This
511 * optimizes for the case when the gpu will dirty the data
512 * anyway again before the next pread happens. */
513 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
514 obj->cache_level);
515 ret = i915_gem_object_wait_rendering(obj, true);
516 if (ret)
517 return ret;
Chris Wilsonc8725f32014-03-17 12:21:55 +0000518
519 i915_gem_object_retire(obj);
Brad Volkin4c914c02014-02-18 10:15:45 -0800520 }
521
522 ret = i915_gem_object_get_pages(obj);
523 if (ret)
524 return ret;
525
526 i915_gem_object_pin_pages(obj);
527
528 return ret;
529}
530
Daniel Vetterd174bd62012-03-25 19:47:40 +0200531/* Per-page copy function for the shmem pread fastpath.
532 * Flushes invalid cachelines before reading the target if
533 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700534static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200535shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
536 char __user *user_data,
537 bool page_do_bit17_swizzling, bool needs_clflush)
538{
539 char *vaddr;
540 int ret;
541
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200542 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200543 return -EINVAL;
544
545 vaddr = kmap_atomic(page);
546 if (needs_clflush)
547 drm_clflush_virt_range(vaddr + shmem_page_offset,
548 page_length);
549 ret = __copy_to_user_inatomic(user_data,
550 vaddr + shmem_page_offset,
551 page_length);
552 kunmap_atomic(vaddr);
553
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100554 return ret ? -EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200555}
556
Daniel Vetter23c18c72012-03-25 19:47:42 +0200557static void
558shmem_clflush_swizzled_range(char *addr, unsigned long length,
559 bool swizzled)
560{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200561 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200562 unsigned long start = (unsigned long) addr;
563 unsigned long end = (unsigned long) addr + length;
564
565 /* For swizzling simply ensure that we always flush both
566 * channels. Lame, but simple and it works. Swizzled
567 * pwrite/pread is far from a hotpath - current userspace
568 * doesn't use it at all. */
569 start = round_down(start, 128);
570 end = round_up(end, 128);
571
572 drm_clflush_virt_range((void *)start, end - start);
573 } else {
574 drm_clflush_virt_range(addr, length);
575 }
576
577}
578
Daniel Vetterd174bd62012-03-25 19:47:40 +0200579/* Only difference to the fast-path function is that this can handle bit17
580 * and uses non-atomic copy and kmap functions. */
581static int
582shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
583 char __user *user_data,
584 bool page_do_bit17_swizzling, bool needs_clflush)
585{
586 char *vaddr;
587 int ret;
588
589 vaddr = kmap(page);
590 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200591 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
592 page_length,
593 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200594
595 if (page_do_bit17_swizzling)
596 ret = __copy_to_user_swizzled(user_data,
597 vaddr, shmem_page_offset,
598 page_length);
599 else
600 ret = __copy_to_user(user_data,
601 vaddr + shmem_page_offset,
602 page_length);
603 kunmap(page);
604
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100605 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200606}
607
Eric Anholteb014592009-03-10 11:44:52 -0700608static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200609i915_gem_shmem_pread(struct drm_device *dev,
610 struct drm_i915_gem_object *obj,
611 struct drm_i915_gem_pread *args,
612 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700613{
Daniel Vetter8461d222011-12-14 13:57:32 +0100614 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700615 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100616 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100617 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100618 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200619 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200620 int needs_clflush = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200621 struct sg_page_iter sg_iter;
Eric Anholteb014592009-03-10 11:44:52 -0700622
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200623 user_data = to_user_ptr(args->data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700624 remain = args->size;
625
Daniel Vetter8461d222011-12-14 13:57:32 +0100626 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700627
Brad Volkin4c914c02014-02-18 10:15:45 -0800628 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100629 if (ret)
630 return ret;
631
Eric Anholteb014592009-03-10 11:44:52 -0700632 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100633
Imre Deak67d5a502013-02-18 19:28:02 +0200634 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
635 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200636 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +0100637
638 if (remain <= 0)
639 break;
640
Eric Anholteb014592009-03-10 11:44:52 -0700641 /* Operation in this page
642 *
Eric Anholteb014592009-03-10 11:44:52 -0700643 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700644 * page_length = bytes to copy for this page
645 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100646 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700647 page_length = remain;
648 if ((shmem_page_offset + page_length) > PAGE_SIZE)
649 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700650
Daniel Vetter8461d222011-12-14 13:57:32 +0100651 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
652 (page_to_phys(page) & (1 << 17)) != 0;
653
Daniel Vetterd174bd62012-03-25 19:47:40 +0200654 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
655 user_data, page_do_bit17_swizzling,
656 needs_clflush);
657 if (ret == 0)
658 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700659
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200660 mutex_unlock(&dev->struct_mutex);
661
Jani Nikulad330a952014-01-21 11:24:25 +0200662 if (likely(!i915.prefault_disable) && !prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200663 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200664 /* Userspace is tricking us, but we've already clobbered
665 * its pages with the prefault and promised to write the
666 * data up to the first fault. Hence ignore any errors
667 * and just continue. */
668 (void)ret;
669 prefaulted = 1;
670 }
671
Daniel Vetterd174bd62012-03-25 19:47:40 +0200672 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
673 user_data, page_do_bit17_swizzling,
674 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700675
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200676 mutex_lock(&dev->struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100677
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100678 if (ret)
Daniel Vetter8461d222011-12-14 13:57:32 +0100679 goto out;
Daniel Vetter8461d222011-12-14 13:57:32 +0100680
Chris Wilson17793c92014-03-07 08:30:36 +0000681next_page:
Eric Anholteb014592009-03-10 11:44:52 -0700682 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100683 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700684 offset += page_length;
685 }
686
Chris Wilson4f27b752010-10-14 15:26:45 +0100687out:
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100688 i915_gem_object_unpin_pages(obj);
689
Eric Anholteb014592009-03-10 11:44:52 -0700690 return ret;
691}
692
Eric Anholt673a3942008-07-30 12:06:12 -0700693/**
694 * Reads data from the object referenced by handle.
695 *
696 * On error, the contents of *data are undefined.
697 */
698int
699i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000700 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700701{
702 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000703 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100704 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700705
Chris Wilson51311d02010-11-17 09:10:42 +0000706 if (args->size == 0)
707 return 0;
708
709 if (!access_ok(VERIFY_WRITE,
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200710 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000711 args->size))
712 return -EFAULT;
713
Chris Wilson4f27b752010-10-14 15:26:45 +0100714 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100715 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100716 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700717
Chris Wilson05394f32010-11-08 19:18:58 +0000718 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000719 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100720 ret = -ENOENT;
721 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100722 }
Eric Anholt673a3942008-07-30 12:06:12 -0700723
Chris Wilson7dcd2492010-09-26 20:21:44 +0100724 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000725 if (args->offset > obj->base.size ||
726 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100727 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100728 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100729 }
730
Daniel Vetter1286ff72012-05-10 15:25:09 +0200731 /* prime objects have no backing filp to GEM pread/pwrite
732 * pages from.
733 */
734 if (!obj->base.filp) {
735 ret = -EINVAL;
736 goto out;
737 }
738
Chris Wilsondb53a302011-02-03 11:57:46 +0000739 trace_i915_gem_object_pread(obj, args->offset, args->size);
740
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200741 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700742
Chris Wilson35b62a82010-09-26 20:23:38 +0100743out:
Chris Wilson05394f32010-11-08 19:18:58 +0000744 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100745unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100746 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700747 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700748}
749
Keith Packard0839ccb2008-10-30 19:38:48 -0700750/* This is the fast write path which cannot handle
751 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700752 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700753
Keith Packard0839ccb2008-10-30 19:38:48 -0700754static inline int
755fast_user_write(struct io_mapping *mapping,
756 loff_t page_base, int page_offset,
757 char __user *user_data,
758 int length)
759{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700760 void __iomem *vaddr_atomic;
761 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700762 unsigned long unwritten;
763
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700764 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700765 /* We can use the cpu mem copy function because this is X86. */
766 vaddr = (void __force*)vaddr_atomic + page_offset;
767 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700768 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700769 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100770 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700771}
772
Eric Anholt3de09aa2009-03-09 09:42:23 -0700773/**
774 * This is the fast pwrite path, where we copy the data directly from the
775 * user into the GTT, uncached.
776 */
Eric Anholt673a3942008-07-30 12:06:12 -0700777static int
Chris Wilson05394f32010-11-08 19:18:58 +0000778i915_gem_gtt_pwrite_fast(struct drm_device *dev,
779 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700780 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000781 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700782{
Jani Nikula3e31c6c2014-03-31 14:27:16 +0300783 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700784 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700785 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700786 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200787 int page_offset, page_length, ret;
788
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100789 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200790 if (ret)
791 goto out;
792
793 ret = i915_gem_object_set_to_gtt_domain(obj, true);
794 if (ret)
795 goto out_unpin;
796
797 ret = i915_gem_object_put_fence(obj);
798 if (ret)
799 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700800
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200801 user_data = to_user_ptr(args->data_ptr);
Eric Anholt673a3942008-07-30 12:06:12 -0700802 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700803
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700804 offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700805
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200806 intel_fb_obj_invalidate(obj, NULL, ORIGIN_GTT);
807
Eric Anholt673a3942008-07-30 12:06:12 -0700808 while (remain > 0) {
809 /* Operation in this page
810 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700811 * page_base = page offset within aperture
812 * page_offset = offset within page
813 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700814 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100815 page_base = offset & PAGE_MASK;
816 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700817 page_length = remain;
818 if ((page_offset + remain) > PAGE_SIZE)
819 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700820
Keith Packard0839ccb2008-10-30 19:38:48 -0700821 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700822 * source page isn't available. Return the error and we'll
823 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700824 */
Ben Widawsky5d4545a2013-01-17 12:45:15 -0800825 if (fast_user_write(dev_priv->gtt.mappable, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200826 page_offset, user_data, page_length)) {
827 ret = -EFAULT;
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200828 goto out_flush;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200829 }
Eric Anholt673a3942008-07-30 12:06:12 -0700830
Keith Packard0839ccb2008-10-30 19:38:48 -0700831 remain -= page_length;
832 user_data += page_length;
833 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700834 }
Eric Anholt673a3942008-07-30 12:06:12 -0700835
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200836out_flush:
837 intel_fb_obj_flush(obj, false);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200838out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800839 i915_gem_object_ggtt_unpin(obj);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200840out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700841 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700842}
843
Daniel Vetterd174bd62012-03-25 19:47:40 +0200844/* Per-page copy function for the shmem pwrite fastpath.
845 * Flushes invalid cachelines before writing to the target if
846 * needs_clflush_before is set and flushes out any written cachelines after
847 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700848static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200849shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
850 char __user *user_data,
851 bool page_do_bit17_swizzling,
852 bool needs_clflush_before,
853 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700854{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200855 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700856 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700857
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200858 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200859 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700860
Daniel Vetterd174bd62012-03-25 19:47:40 +0200861 vaddr = kmap_atomic(page);
862 if (needs_clflush_before)
863 drm_clflush_virt_range(vaddr + shmem_page_offset,
864 page_length);
Chris Wilsonc2831a92014-03-07 08:30:37 +0000865 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
866 user_data, page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200867 if (needs_clflush_after)
868 drm_clflush_virt_range(vaddr + shmem_page_offset,
869 page_length);
870 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700871
Chris Wilson755d2212012-09-04 21:02:55 +0100872 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700873}
874
Daniel Vetterd174bd62012-03-25 19:47:40 +0200875/* Only difference to the fast-path function is that this can handle bit17
876 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700877static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200878shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
879 char __user *user_data,
880 bool page_do_bit17_swizzling,
881 bool needs_clflush_before,
882 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700883{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200884 char *vaddr;
885 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700886
Daniel Vetterd174bd62012-03-25 19:47:40 +0200887 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200888 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200889 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
890 page_length,
891 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200892 if (page_do_bit17_swizzling)
893 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100894 user_data,
895 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200896 else
897 ret = __copy_from_user(vaddr + shmem_page_offset,
898 user_data,
899 page_length);
900 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200901 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
902 page_length,
903 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200904 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100905
Chris Wilson755d2212012-09-04 21:02:55 +0100906 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700907}
908
Eric Anholt40123c12009-03-09 13:42:30 -0700909static int
Daniel Vettere244a442012-03-25 19:47:28 +0200910i915_gem_shmem_pwrite(struct drm_device *dev,
911 struct drm_i915_gem_object *obj,
912 struct drm_i915_gem_pwrite *args,
913 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700914{
Eric Anholt40123c12009-03-09 13:42:30 -0700915 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100916 loff_t offset;
917 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100918 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100919 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200920 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200921 int needs_clflush_after = 0;
922 int needs_clflush_before = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200923 struct sg_page_iter sg_iter;
Eric Anholt40123c12009-03-09 13:42:30 -0700924
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200925 user_data = to_user_ptr(args->data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700926 remain = args->size;
927
Daniel Vetter8c599672011-12-14 13:57:31 +0100928 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700929
Daniel Vetter58642882012-03-25 19:47:37 +0200930 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
931 /* If we're not in the cpu write domain, set ourself into the gtt
932 * write domain and manually flush cachelines (if required). This
933 * optimizes for the case when the gpu will use the data
934 * right away and we therefore have to clflush anyway. */
Chris Wilson2c225692013-08-09 12:26:45 +0100935 needs_clflush_after = cpu_write_needs_clflush(obj);
Ben Widawsky23f54482013-09-11 14:57:48 -0700936 ret = i915_gem_object_wait_rendering(obj, false);
937 if (ret)
938 return ret;
Chris Wilsonc8725f32014-03-17 12:21:55 +0000939
940 i915_gem_object_retire(obj);
Daniel Vetter58642882012-03-25 19:47:37 +0200941 }
Chris Wilsonc76ce032013-08-08 14:41:03 +0100942 /* Same trick applies to invalidate partially written cachelines read
943 * before writing. */
944 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
945 needs_clflush_before =
946 !cpu_cache_is_coherent(dev, obj->cache_level);
Daniel Vetter58642882012-03-25 19:47:37 +0200947
Chris Wilson755d2212012-09-04 21:02:55 +0100948 ret = i915_gem_object_get_pages(obj);
949 if (ret)
950 return ret;
951
Paulo Zanoni063e4e62015-02-13 17:23:45 -0200952 intel_fb_obj_invalidate(obj, NULL, ORIGIN_CPU);
953
Chris Wilson755d2212012-09-04 21:02:55 +0100954 i915_gem_object_pin_pages(obj);
955
Eric Anholt40123c12009-03-09 13:42:30 -0700956 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000957 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700958
Imre Deak67d5a502013-02-18 19:28:02 +0200959 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
960 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200961 struct page *page = sg_page_iter_page(&sg_iter);
Daniel Vetter58642882012-03-25 19:47:37 +0200962 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100963
Chris Wilson9da3da62012-06-01 15:20:22 +0100964 if (remain <= 0)
965 break;
966
Eric Anholt40123c12009-03-09 13:42:30 -0700967 /* Operation in this page
968 *
Eric Anholt40123c12009-03-09 13:42:30 -0700969 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700970 * page_length = bytes to copy for this page
971 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100972 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700973
974 page_length = remain;
975 if ((shmem_page_offset + page_length) > PAGE_SIZE)
976 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700977
Daniel Vetter58642882012-03-25 19:47:37 +0200978 /* If we don't overwrite a cacheline completely we need to be
979 * careful to have up-to-date data by first clflushing. Don't
980 * overcomplicate things and flush the entire patch. */
981 partial_cacheline_write = needs_clflush_before &&
982 ((shmem_page_offset | page_length)
983 & (boot_cpu_data.x86_clflush_size - 1));
984
Daniel Vetter8c599672011-12-14 13:57:31 +0100985 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
986 (page_to_phys(page) & (1 << 17)) != 0;
987
Daniel Vetterd174bd62012-03-25 19:47:40 +0200988 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
989 user_data, page_do_bit17_swizzling,
990 partial_cacheline_write,
991 needs_clflush_after);
992 if (ret == 0)
993 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700994
Daniel Vettere244a442012-03-25 19:47:28 +0200995 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +0200996 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200997 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
998 user_data, page_do_bit17_swizzling,
999 partial_cacheline_write,
1000 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -07001001
Daniel Vettere244a442012-03-25 19:47:28 +02001002 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +01001003
Chris Wilson755d2212012-09-04 21:02:55 +01001004 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +01001005 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +01001006
Chris Wilson17793c92014-03-07 08:30:36 +00001007next_page:
Eric Anholt40123c12009-03-09 13:42:30 -07001008 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +01001009 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -07001010 offset += page_length;
1011 }
1012
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001013out:
Chris Wilson755d2212012-09-04 21:02:55 +01001014 i915_gem_object_unpin_pages(obj);
1015
Daniel Vettere244a442012-03-25 19:47:28 +02001016 if (hit_slowpath) {
Daniel Vetter8dcf0152012-11-15 16:53:58 +01001017 /*
1018 * Fixup: Flush cpu caches in case we didn't flush the dirty
1019 * cachelines in-line while writing and the object moved
1020 * out of the cpu write domain while we've dropped the lock.
1021 */
1022 if (!needs_clflush_after &&
1023 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilson000433b2013-08-08 14:41:09 +01001024 if (i915_gem_clflush_object(obj, obj->pin_display))
1025 i915_gem_chipset_flush(dev);
Daniel Vettere244a442012-03-25 19:47:28 +02001026 }
Daniel Vetter8c599672011-12-14 13:57:31 +01001027 }
Eric Anholt40123c12009-03-09 13:42:30 -07001028
Daniel Vetter58642882012-03-25 19:47:37 +02001029 if (needs_clflush_after)
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001030 i915_gem_chipset_flush(dev);
Daniel Vetter58642882012-03-25 19:47:37 +02001031
Paulo Zanoni063e4e62015-02-13 17:23:45 -02001032 intel_fb_obj_flush(obj, false);
Eric Anholt40123c12009-03-09 13:42:30 -07001033 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001034}
1035
1036/**
1037 * Writes data to the object referenced by handle.
1038 *
1039 * On error, the contents of the buffer that were to be modified are undefined.
1040 */
1041int
1042i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001043 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001044{
Imre Deak5d77d9c2014-11-12 16:40:35 +02001045 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001046 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001047 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +00001048 int ret;
1049
1050 if (args->size == 0)
1051 return 0;
1052
1053 if (!access_ok(VERIFY_READ,
Ville Syrjälä2bb46292013-02-22 16:12:51 +02001054 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +00001055 args->size))
1056 return -EFAULT;
1057
Jani Nikulad330a952014-01-21 11:24:25 +02001058 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001059 ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
1060 args->size);
1061 if (ret)
1062 return -EFAULT;
1063 }
Eric Anholt673a3942008-07-30 12:06:12 -07001064
Imre Deak5d77d9c2014-11-12 16:40:35 +02001065 intel_runtime_pm_get(dev_priv);
1066
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001067 ret = i915_mutex_lock_interruptible(dev);
1068 if (ret)
Imre Deak5d77d9c2014-11-12 16:40:35 +02001069 goto put_rpm;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001070
Chris Wilson05394f32010-11-08 19:18:58 +00001071 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001072 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001073 ret = -ENOENT;
1074 goto unlock;
1075 }
Eric Anholt673a3942008-07-30 12:06:12 -07001076
Chris Wilson7dcd2492010-09-26 20:21:44 +01001077 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001078 if (args->offset > obj->base.size ||
1079 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001080 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001081 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001082 }
1083
Daniel Vetter1286ff72012-05-10 15:25:09 +02001084 /* prime objects have no backing filp to GEM pread/pwrite
1085 * pages from.
1086 */
1087 if (!obj->base.filp) {
1088 ret = -EINVAL;
1089 goto out;
1090 }
1091
Chris Wilsondb53a302011-02-03 11:57:46 +00001092 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1093
Daniel Vetter935aaa62012-03-25 19:47:35 +02001094 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001095 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1096 * it would end up going through the fenced access, and we'll get
1097 * different detiling behavior between reading and writing.
1098 * pread/pwrite currently are reading and writing from the CPU
1099 * perspective, requiring manual detiling by the client.
1100 */
Chris Wilson2c225692013-08-09 12:26:45 +01001101 if (obj->tiling_mode == I915_TILING_NONE &&
1102 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
1103 cpu_write_needs_clflush(obj)) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001104 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001105 /* Note that the gtt paths might fail with non-page-backed user
1106 * pointers (e.g. gtt mappings when moving data between
1107 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -07001108 }
Eric Anholt673a3942008-07-30 12:06:12 -07001109
Chris Wilson6a2c4232014-11-04 04:51:40 -08001110 if (ret == -EFAULT || ret == -ENOSPC) {
1111 if (obj->phys_handle)
1112 ret = i915_gem_phys_pwrite(obj, args, file);
1113 else
1114 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
1115 }
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001116
Chris Wilson35b62a82010-09-26 20:23:38 +01001117out:
Chris Wilson05394f32010-11-08 19:18:58 +00001118 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001119unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001120 mutex_unlock(&dev->struct_mutex);
Imre Deak5d77d9c2014-11-12 16:40:35 +02001121put_rpm:
1122 intel_runtime_pm_put(dev_priv);
1123
Eric Anholt673a3942008-07-30 12:06:12 -07001124 return ret;
1125}
1126
Chris Wilsonb3612372012-08-24 09:35:08 +01001127int
Daniel Vetter33196de2012-11-14 17:14:05 +01001128i915_gem_check_wedge(struct i915_gpu_error *error,
Chris Wilsonb3612372012-08-24 09:35:08 +01001129 bool interruptible)
1130{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001131 if (i915_reset_in_progress(error)) {
Chris Wilsonb3612372012-08-24 09:35:08 +01001132 /* Non-interruptible callers can't handle -EAGAIN, hence return
1133 * -EIO unconditionally for these. */
1134 if (!interruptible)
1135 return -EIO;
1136
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001137 /* Recovery complete, but the reset failed ... */
1138 if (i915_terminally_wedged(error))
Chris Wilsonb3612372012-08-24 09:35:08 +01001139 return -EIO;
1140
McAulay, Alistair6689c162014-08-15 18:51:35 +01001141 /*
1142 * Check if GPU Reset is in progress - we need intel_ring_begin
1143 * to work properly to reinit the hw state while the gpu is
1144 * still marked as reset-in-progress. Handle this with a flag.
1145 */
1146 if (!error->reload_in_reset)
1147 return -EAGAIN;
Chris Wilsonb3612372012-08-24 09:35:08 +01001148 }
1149
1150 return 0;
1151}
1152
1153/*
John Harrisonb6660d52014-11-24 18:49:30 +00001154 * Compare arbitrary request against outstanding lazy request. Emit on match.
Chris Wilsonb3612372012-08-24 09:35:08 +01001155 */
Sourab Gupta84c33a62014-06-02 16:47:17 +05301156int
John Harrisonb6660d52014-11-24 18:49:30 +00001157i915_gem_check_olr(struct drm_i915_gem_request *req)
Chris Wilsonb3612372012-08-24 09:35:08 +01001158{
1159 int ret;
1160
John Harrisonb6660d52014-11-24 18:49:30 +00001161 WARN_ON(!mutex_is_locked(&req->ring->dev->struct_mutex));
Chris Wilsonb3612372012-08-24 09:35:08 +01001162
1163 ret = 0;
John Harrisonb6660d52014-11-24 18:49:30 +00001164 if (req == req->ring->outstanding_lazy_request)
John Harrison9400ae52014-11-24 18:49:36 +00001165 ret = i915_add_request(req->ring);
Chris Wilsonb3612372012-08-24 09:35:08 +01001166
1167 return ret;
1168}
1169
Chris Wilson094f9a52013-09-25 17:34:55 +01001170static void fake_irq(unsigned long data)
1171{
1172 wake_up_process((struct task_struct *)data);
1173}
1174
1175static bool missed_irq(struct drm_i915_private *dev_priv,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001176 struct intel_engine_cs *ring)
Chris Wilson094f9a52013-09-25 17:34:55 +01001177{
1178 return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
1179}
1180
Chris Wilson2def4ad92015-04-07 16:20:41 +01001181static int __i915_spin_request(struct drm_i915_gem_request *rq)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001182{
Chris Wilson2def4ad92015-04-07 16:20:41 +01001183 unsigned long timeout;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001184
Chris Wilson2def4ad92015-04-07 16:20:41 +01001185 if (i915_gem_request_get_ring(rq)->irq_refcount)
1186 return -EBUSY;
1187
1188 timeout = jiffies + 1;
1189 while (!need_resched()) {
1190 if (i915_gem_request_completed(rq, true))
1191 return 0;
1192
1193 if (time_after_eq(jiffies, timeout))
1194 break;
1195
1196 cpu_relax_lowlatency();
1197 }
1198 if (i915_gem_request_completed(rq, false))
1199 return 0;
1200
1201 return -EAGAIN;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001202}
1203
Chris Wilsonb3612372012-08-24 09:35:08 +01001204/**
John Harrison9c654812014-11-24 18:49:35 +00001205 * __i915_wait_request - wait until execution of request has finished
1206 * @req: duh!
1207 * @reset_counter: reset sequence associated with the given request
Chris Wilsonb3612372012-08-24 09:35:08 +01001208 * @interruptible: do an interruptible wait (normally yes)
1209 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1210 *
Daniel Vetterf69061b2012-12-06 09:01:42 +01001211 * Note: It is of utmost importance that the passed in seqno and reset_counter
1212 * values have been read by the caller in an smp safe manner. Where read-side
1213 * locks are involved, it is sufficient to read the reset_counter before
1214 * unlocking the lock that protects the seqno. For lockless tricks, the
1215 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
1216 * inserted.
1217 *
John Harrison9c654812014-11-24 18:49:35 +00001218 * Returns 0 if the request was found within the alloted time. Else returns the
Chris Wilsonb3612372012-08-24 09:35:08 +01001219 * errno with remaining time filled in timeout argument.
1220 */
John Harrison9c654812014-11-24 18:49:35 +00001221int __i915_wait_request(struct drm_i915_gem_request *req,
Daniel Vetterf69061b2012-12-06 09:01:42 +01001222 unsigned reset_counter,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001223 bool interruptible,
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00001224 s64 *timeout,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001225 struct drm_i915_file_private *file_priv)
Chris Wilsonb3612372012-08-24 09:35:08 +01001226{
John Harrison9c654812014-11-24 18:49:35 +00001227 struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001228 struct drm_device *dev = ring->dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03001229 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala168c3f22013-12-12 17:54:42 +02001230 const bool irq_test_in_progress =
1231 ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
Chris Wilson094f9a52013-09-25 17:34:55 +01001232 DEFINE_WAIT(wait);
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001233 unsigned long timeout_expire;
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00001234 s64 before, now;
Chris Wilsonb3612372012-08-24 09:35:08 +01001235 int ret;
1236
Jesse Barnes9df7575f2014-06-20 09:29:20 -07001237 WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
Paulo Zanonic67a4702013-08-19 13:18:09 -03001238
John Harrison1b5a4332014-11-24 18:49:42 +00001239 if (i915_gem_request_completed(req, true))
Chris Wilsonb3612372012-08-24 09:35:08 +01001240 return 0;
1241
Daniel Vetter7bd0e222014-12-04 11:12:54 +01001242 timeout_expire = timeout ?
1243 jiffies + nsecs_to_jiffies_timeout((u64)*timeout) : 0;
Chris Wilsonb3612372012-08-24 09:35:08 +01001244
Chris Wilson7c27f522015-04-07 16:20:33 +01001245 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilson1854d5c2015-04-07 16:20:32 +01001246 gen6_rps_boost(dev_priv, file_priv);
Chris Wilsonb3612372012-08-24 09:35:08 +01001247
Chris Wilson094f9a52013-09-25 17:34:55 +01001248 /* Record current time in case interrupted by signal, or wedged */
John Harrison74328ee2014-11-24 18:49:38 +00001249 trace_i915_gem_request_wait_begin(req);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00001250 before = ktime_get_raw_ns();
Chris Wilson2def4ad92015-04-07 16:20:41 +01001251
1252 /* Optimistic spin for the next jiffie before touching IRQs */
1253 ret = __i915_spin_request(req);
1254 if (ret == 0)
1255 goto out;
1256
1257 if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring))) {
1258 ret = -ENODEV;
1259 goto out;
1260 }
1261
Chris Wilson094f9a52013-09-25 17:34:55 +01001262 for (;;) {
1263 struct timer_list timer;
Chris Wilsonb3612372012-08-24 09:35:08 +01001264
Chris Wilson094f9a52013-09-25 17:34:55 +01001265 prepare_to_wait(&ring->irq_queue, &wait,
1266 interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
Chris Wilsonb3612372012-08-24 09:35:08 +01001267
Daniel Vetterf69061b2012-12-06 09:01:42 +01001268 /* We need to check whether any gpu reset happened in between
1269 * the caller grabbing the seqno and now ... */
Chris Wilson094f9a52013-09-25 17:34:55 +01001270 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
1271 /* ... but upgrade the -EAGAIN to an -EIO if the gpu
1272 * is truely gone. */
1273 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1274 if (ret == 0)
1275 ret = -EAGAIN;
1276 break;
1277 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01001278
John Harrison1b5a4332014-11-24 18:49:42 +00001279 if (i915_gem_request_completed(req, false)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001280 ret = 0;
1281 break;
1282 }
Chris Wilsonb3612372012-08-24 09:35:08 +01001283
Chris Wilson094f9a52013-09-25 17:34:55 +01001284 if (interruptible && signal_pending(current)) {
1285 ret = -ERESTARTSYS;
1286 break;
1287 }
1288
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001289 if (timeout && time_after_eq(jiffies, timeout_expire)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001290 ret = -ETIME;
1291 break;
1292 }
1293
1294 timer.function = NULL;
1295 if (timeout || missed_irq(dev_priv, ring)) {
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001296 unsigned long expire;
1297
Chris Wilson094f9a52013-09-25 17:34:55 +01001298 setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
Mika Kuoppala47e9766d2013-12-10 17:02:43 +02001299 expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
Chris Wilson094f9a52013-09-25 17:34:55 +01001300 mod_timer(&timer, expire);
1301 }
1302
Chris Wilson5035c272013-10-04 09:58:46 +01001303 io_schedule();
Chris Wilson094f9a52013-09-25 17:34:55 +01001304
Chris Wilson094f9a52013-09-25 17:34:55 +01001305 if (timer.function) {
1306 del_singleshot_timer_sync(&timer);
1307 destroy_timer_on_stack(&timer);
1308 }
1309 }
Mika Kuoppala168c3f22013-12-12 17:54:42 +02001310 if (!irq_test_in_progress)
1311 ring->irq_put(ring);
Chris Wilson094f9a52013-09-25 17:34:55 +01001312
1313 finish_wait(&ring->irq_queue, &wait);
Chris Wilsonb3612372012-08-24 09:35:08 +01001314
Chris Wilson2def4ad92015-04-07 16:20:41 +01001315out:
1316 now = ktime_get_raw_ns();
1317 trace_i915_gem_request_wait_end(req);
1318
Chris Wilsonb3612372012-08-24 09:35:08 +01001319 if (timeout) {
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00001320 s64 tres = *timeout - (now - before);
1321
1322 *timeout = tres < 0 ? 0 : tres;
Daniel Vetter9cca3062014-11-28 10:29:55 +01001323
1324 /*
1325 * Apparently ktime isn't accurate enough and occasionally has a
1326 * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
1327 * things up to make the test happy. We allow up to 1 jiffy.
1328 *
1329 * This is a regrssion from the timespec->ktime conversion.
1330 */
1331 if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
1332 *timeout = 0;
Chris Wilsonb3612372012-08-24 09:35:08 +01001333 }
1334
Chris Wilson094f9a52013-09-25 17:34:55 +01001335 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001336}
1337
1338/**
Daniel Vettera4b3a572014-11-26 14:17:05 +01001339 * Waits for a request to be signaled, and cleans up the
Chris Wilsonb3612372012-08-24 09:35:08 +01001340 * request and object lists appropriately for that event.
1341 */
1342int
Daniel Vettera4b3a572014-11-26 14:17:05 +01001343i915_wait_request(struct drm_i915_gem_request *req)
Chris Wilsonb3612372012-08-24 09:35:08 +01001344{
Daniel Vettera4b3a572014-11-26 14:17:05 +01001345 struct drm_device *dev;
1346 struct drm_i915_private *dev_priv;
1347 bool interruptible;
Ander Conselvan de Oliveira16e9a212014-11-06 09:26:38 +02001348 unsigned reset_counter;
Chris Wilsonb3612372012-08-24 09:35:08 +01001349 int ret;
1350
Daniel Vettera4b3a572014-11-26 14:17:05 +01001351 BUG_ON(req == NULL);
1352
1353 dev = req->ring->dev;
1354 dev_priv = dev->dev_private;
1355 interruptible = dev_priv->mm.interruptible;
1356
Chris Wilsonb3612372012-08-24 09:35:08 +01001357 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
Chris Wilsonb3612372012-08-24 09:35:08 +01001358
Daniel Vetter33196de2012-11-14 17:14:05 +01001359 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
Chris Wilsonb3612372012-08-24 09:35:08 +01001360 if (ret)
1361 return ret;
1362
Daniel Vettera4b3a572014-11-26 14:17:05 +01001363 ret = i915_gem_check_olr(req);
Chris Wilsonb3612372012-08-24 09:35:08 +01001364 if (ret)
1365 return ret;
1366
Ander Conselvan de Oliveira16e9a212014-11-06 09:26:38 +02001367 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Daniel Vettera4b3a572014-11-26 14:17:05 +01001368 i915_gem_request_reference(req);
John Harrison9c654812014-11-24 18:49:35 +00001369 ret = __i915_wait_request(req, reset_counter,
1370 interruptible, NULL, NULL);
Daniel Vettera4b3a572014-11-26 14:17:05 +01001371 i915_gem_request_unreference(req);
1372 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001373}
1374
Chris Wilsond26e3af2013-06-29 22:05:26 +01001375static int
John Harrison8e6395492014-10-30 18:40:53 +00001376i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj)
Chris Wilsond26e3af2013-06-29 22:05:26 +01001377{
Chris Wilsonc8725f32014-03-17 12:21:55 +00001378 if (!obj->active)
1379 return 0;
Chris Wilsond26e3af2013-06-29 22:05:26 +01001380
1381 /* Manually manage the write flush as we may have not yet
1382 * retired the buffer.
1383 *
John Harrison97b2a6a2014-11-24 18:49:26 +00001384 * Note that the last_write_req is always the earlier of
1385 * the two (read/write) requests, so if we haved successfully waited,
Chris Wilsond26e3af2013-06-29 22:05:26 +01001386 * we know we have passed the last write.
1387 */
John Harrison97b2a6a2014-11-24 18:49:26 +00001388 i915_gem_request_assign(&obj->last_write_req, NULL);
Chris Wilsond26e3af2013-06-29 22:05:26 +01001389
1390 return 0;
1391}
1392
Chris Wilsonb3612372012-08-24 09:35:08 +01001393/**
1394 * Ensures that all rendering to the object has completed and the object is
1395 * safe to unbind from the GTT or access from the CPU.
1396 */
Chris Wilson2e2f3512015-04-27 13:41:14 +01001397int
Chris Wilsonb3612372012-08-24 09:35:08 +01001398i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1399 bool readonly)
1400{
John Harrison97b2a6a2014-11-24 18:49:26 +00001401 struct drm_i915_gem_request *req;
Chris Wilsonb3612372012-08-24 09:35:08 +01001402 int ret;
1403
John Harrison97b2a6a2014-11-24 18:49:26 +00001404 req = readonly ? obj->last_write_req : obj->last_read_req;
1405 if (!req)
Chris Wilsonb3612372012-08-24 09:35:08 +01001406 return 0;
1407
Daniel Vettera4b3a572014-11-26 14:17:05 +01001408 ret = i915_wait_request(req);
Chris Wilsonb3612372012-08-24 09:35:08 +01001409 if (ret)
1410 return ret;
1411
John Harrison8e6395492014-10-30 18:40:53 +00001412 return i915_gem_object_wait_rendering__tail(obj);
Chris Wilsonb3612372012-08-24 09:35:08 +01001413}
1414
Chris Wilson3236f572012-08-24 09:35:09 +01001415/* A nonblocking variant of the above wait. This is a highly dangerous routine
1416 * as the object state may change during this call.
1417 */
1418static __must_check int
1419i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
Chris Wilson6e4930f2014-02-07 18:37:06 -02001420 struct drm_i915_file_private *file_priv,
Chris Wilson3236f572012-08-24 09:35:09 +01001421 bool readonly)
1422{
John Harrison97b2a6a2014-11-24 18:49:26 +00001423 struct drm_i915_gem_request *req;
Chris Wilson3236f572012-08-24 09:35:09 +01001424 struct drm_device *dev = obj->base.dev;
1425 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf69061b2012-12-06 09:01:42 +01001426 unsigned reset_counter;
Chris Wilson3236f572012-08-24 09:35:09 +01001427 int ret;
1428
1429 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1430 BUG_ON(!dev_priv->mm.interruptible);
1431
John Harrison97b2a6a2014-11-24 18:49:26 +00001432 req = readonly ? obj->last_write_req : obj->last_read_req;
1433 if (!req)
Chris Wilson3236f572012-08-24 09:35:09 +01001434 return 0;
1435
Daniel Vetter33196de2012-11-14 17:14:05 +01001436 ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
Chris Wilson3236f572012-08-24 09:35:09 +01001437 if (ret)
1438 return ret;
1439
John Harrisonb6660d52014-11-24 18:49:30 +00001440 ret = i915_gem_check_olr(req);
Chris Wilson3236f572012-08-24 09:35:09 +01001441 if (ret)
1442 return ret;
1443
Daniel Vetterf69061b2012-12-06 09:01:42 +01001444 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
John Harrisonff865882014-11-24 18:49:28 +00001445 i915_gem_request_reference(req);
Chris Wilson3236f572012-08-24 09:35:09 +01001446 mutex_unlock(&dev->struct_mutex);
John Harrison9c654812014-11-24 18:49:35 +00001447 ret = __i915_wait_request(req, reset_counter, true, NULL, file_priv);
Chris Wilson3236f572012-08-24 09:35:09 +01001448 mutex_lock(&dev->struct_mutex);
John Harrisonff865882014-11-24 18:49:28 +00001449 i915_gem_request_unreference(req);
Chris Wilsond26e3af2013-06-29 22:05:26 +01001450 if (ret)
1451 return ret;
Chris Wilson3236f572012-08-24 09:35:09 +01001452
John Harrison8e6395492014-10-30 18:40:53 +00001453 return i915_gem_object_wait_rendering__tail(obj);
Chris Wilson3236f572012-08-24 09:35:09 +01001454}
1455
Eric Anholt673a3942008-07-30 12:06:12 -07001456/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001457 * Called when user space prepares to use an object with the CPU, either
1458 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001459 */
1460int
1461i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001462 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001463{
1464 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001465 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001466 uint32_t read_domains = args->read_domains;
1467 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001468 int ret;
1469
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001470 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001471 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001472 return -EINVAL;
1473
Chris Wilson21d509e2009-06-06 09:46:02 +01001474 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001475 return -EINVAL;
1476
1477 /* Having something in the write domain implies it's in the read
1478 * domain, and only that read domain. Enforce that in the request.
1479 */
1480 if (write_domain != 0 && read_domains != write_domain)
1481 return -EINVAL;
1482
Chris Wilson76c1dec2010-09-25 11:22:51 +01001483 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001484 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001485 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001486
Chris Wilson05394f32010-11-08 19:18:58 +00001487 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001488 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001489 ret = -ENOENT;
1490 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001491 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001492
Chris Wilson3236f572012-08-24 09:35:09 +01001493 /* Try to flush the object off the GPU without holding the lock.
1494 * We will repeat the flush holding the lock in the normal manner
1495 * to catch cases where we are gazumped.
1496 */
Chris Wilson6e4930f2014-02-07 18:37:06 -02001497 ret = i915_gem_object_wait_rendering__nonblocking(obj,
1498 file->driver_priv,
1499 !write_domain);
Chris Wilson3236f572012-08-24 09:35:09 +01001500 if (ret)
1501 goto unref;
1502
Chris Wilson43566de2015-01-02 16:29:29 +05301503 if (read_domains & I915_GEM_DOMAIN_GTT)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001504 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Chris Wilson43566de2015-01-02 16:29:29 +05301505 else
Eric Anholte47c68e2008-11-14 13:35:19 -08001506 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001507
Chris Wilson3236f572012-08-24 09:35:09 +01001508unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001509 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001510unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001511 mutex_unlock(&dev->struct_mutex);
1512 return ret;
1513}
1514
1515/**
1516 * Called when user space has done writes to this buffer
1517 */
1518int
1519i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001520 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001521{
1522 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001523 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001524 int ret = 0;
1525
Chris Wilson76c1dec2010-09-25 11:22:51 +01001526 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001527 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001528 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001529
Chris Wilson05394f32010-11-08 19:18:58 +00001530 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001531 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001532 ret = -ENOENT;
1533 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001534 }
1535
Eric Anholt673a3942008-07-30 12:06:12 -07001536 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson2c225692013-08-09 12:26:45 +01001537 if (obj->pin_display)
Daniel Vettere62b59e2015-01-21 14:53:48 +01001538 i915_gem_object_flush_cpu_write_domain(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08001539
Chris Wilson05394f32010-11-08 19:18:58 +00001540 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001541unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001542 mutex_unlock(&dev->struct_mutex);
1543 return ret;
1544}
1545
1546/**
1547 * Maps the contents of an object, returning the address it is mapped
1548 * into.
1549 *
1550 * While the mapping holds a reference on the contents of the object, it doesn't
1551 * imply a ref on the object itself.
Daniel Vetter34367382014-10-16 12:28:18 +02001552 *
1553 * IMPORTANT:
1554 *
1555 * DRM driver writers who look a this function as an example for how to do GEM
1556 * mmap support, please don't implement mmap support like here. The modern way
1557 * to implement DRM mmap support is with an mmap offset ioctl (like
1558 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1559 * That way debug tooling like valgrind will understand what's going on, hiding
1560 * the mmap call in a driver private ioctl will break that. The i915 driver only
1561 * does cpu mmaps this way because we didn't know better.
Eric Anholt673a3942008-07-30 12:06:12 -07001562 */
1563int
1564i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001565 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001566{
1567 struct drm_i915_gem_mmap *args = data;
1568 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001569 unsigned long addr;
1570
Akash Goel1816f922015-01-02 16:29:30 +05301571 if (args->flags & ~(I915_MMAP_WC))
1572 return -EINVAL;
1573
1574 if (args->flags & I915_MMAP_WC && !cpu_has_pat)
1575 return -ENODEV;
1576
Chris Wilson05394f32010-11-08 19:18:58 +00001577 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001578 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001579 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001580
Daniel Vetter1286ff72012-05-10 15:25:09 +02001581 /* prime objects have no backing filp to GEM mmap
1582 * pages from.
1583 */
1584 if (!obj->filp) {
1585 drm_gem_object_unreference_unlocked(obj);
1586 return -EINVAL;
1587 }
1588
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001589 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001590 PROT_READ | PROT_WRITE, MAP_SHARED,
1591 args->offset);
Akash Goel1816f922015-01-02 16:29:30 +05301592 if (args->flags & I915_MMAP_WC) {
1593 struct mm_struct *mm = current->mm;
1594 struct vm_area_struct *vma;
1595
1596 down_write(&mm->mmap_sem);
1597 vma = find_vma(mm, addr);
1598 if (vma)
1599 vma->vm_page_prot =
1600 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
1601 else
1602 addr = -ENOMEM;
1603 up_write(&mm->mmap_sem);
1604 }
Luca Barbieribc9025b2010-02-09 05:49:12 +00001605 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001606 if (IS_ERR((void *)addr))
1607 return addr;
1608
1609 args->addr_ptr = (uint64_t) addr;
1610
1611 return 0;
1612}
1613
Jesse Barnesde151cf2008-11-12 10:03:55 -08001614/**
1615 * i915_gem_fault - fault a page into the GTT
1616 * vma: VMA in question
1617 * vmf: fault info
1618 *
1619 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1620 * from userspace. The fault handler takes care of binding the object to
1621 * the GTT (if needed), allocating and programming a fence register (again,
1622 * only if needed based on whether the old reg is still valid or the object
1623 * is tiled) and inserting a new PTE into the faulting process.
1624 *
1625 * Note that the faulting process may involve evicting existing objects
1626 * from the GTT and/or fence registers to make room. So performance may
1627 * suffer if the GTT working set is large or there are few fence registers
1628 * left.
1629 */
1630int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1631{
Chris Wilson05394f32010-11-08 19:18:58 +00001632 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1633 struct drm_device *dev = obj->base.dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03001634 struct drm_i915_private *dev_priv = dev->dev_private;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001635 struct i915_ggtt_view view = i915_ggtt_view_normal;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001636 pgoff_t page_offset;
1637 unsigned long pfn;
1638 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001639 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001640
Paulo Zanonif65c9162013-11-27 18:20:34 -02001641 intel_runtime_pm_get(dev_priv);
1642
Jesse Barnesde151cf2008-11-12 10:03:55 -08001643 /* We don't use vmf->pgoff since that has the fake offset */
1644 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1645 PAGE_SHIFT;
1646
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001647 ret = i915_mutex_lock_interruptible(dev);
1648 if (ret)
1649 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001650
Chris Wilsondb53a302011-02-03 11:57:46 +00001651 trace_i915_gem_object_fault(obj, page_offset, true, write);
1652
Chris Wilson6e4930f2014-02-07 18:37:06 -02001653 /* Try to flush the object off the GPU first without holding the lock.
1654 * Upon reacquiring the lock, we will perform our sanity checks and then
1655 * repeat the flush holding the lock in the normal manner to catch cases
1656 * where we are gazumped.
1657 */
1658 ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
1659 if (ret)
1660 goto unlock;
1661
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001662 /* Access to snoopable pages through the GTT is incoherent. */
1663 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
Chris Wilsonddeff6e2014-05-28 16:16:41 +01001664 ret = -EFAULT;
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001665 goto unlock;
1666 }
1667
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001668 /* Use a partial view if the object is bigger than the aperture. */
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001669 if (obj->base.size >= dev_priv->gtt.mappable_end &&
1670 obj->tiling_mode == I915_TILING_NONE) {
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001671 static const unsigned int chunk_size = 256; // 1 MiB
Joonas Lahtinene7ded2d2015-05-08 14:37:39 +03001672
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001673 memset(&view, 0, sizeof(view));
1674 view.type = I915_GGTT_VIEW_PARTIAL;
1675 view.params.partial.offset = rounddown(page_offset, chunk_size);
1676 view.params.partial.size =
1677 min_t(unsigned int,
1678 chunk_size,
1679 (vma->vm_end - vma->vm_start)/PAGE_SIZE -
1680 view.params.partial.offset);
1681 }
1682
1683 /* Now pin it into the GTT if needed */
1684 ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001685 if (ret)
1686 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001687
Chris Wilsonc9839302012-11-20 10:45:17 +00001688 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1689 if (ret)
1690 goto unpin;
1691
1692 ret = i915_gem_object_get_fence(obj);
1693 if (ret)
1694 goto unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001695
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001696 /* Finally, remap it using the new GTT offset */
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001697 pfn = dev_priv->gtt.mappable_base +
1698 i915_gem_obj_ggtt_offset_view(obj, &view);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001699 pfn >>= PAGE_SHIFT;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001700
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001701 if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
1702 /* Overriding existing pages in partial view does not cause
1703 * us any trouble as TLBs are still valid because the fault
1704 * is due to userspace losing part of the mapping or never
1705 * having accessed it before (at this partials' range).
1706 */
1707 unsigned long base = vma->vm_start +
1708 (view.params.partial.offset << PAGE_SHIFT);
1709 unsigned int i;
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001710
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001711 for (i = 0; i < view.params.partial.size; i++) {
1712 ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
Chris Wilsonb90b91d2014-06-10 12:14:40 +01001713 if (ret)
1714 break;
1715 }
1716
1717 obj->fault_mappable = true;
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001718 } else {
1719 if (!obj->fault_mappable) {
1720 unsigned long size = min_t(unsigned long,
1721 vma->vm_end - vma->vm_start,
1722 obj->base.size);
1723 int i;
1724
1725 for (i = 0; i < size >> PAGE_SHIFT; i++) {
1726 ret = vm_insert_pfn(vma,
1727 (unsigned long)vma->vm_start + i * PAGE_SIZE,
1728 pfn + i);
1729 if (ret)
1730 break;
1731 }
1732
1733 obj->fault_mappable = true;
1734 } else
1735 ret = vm_insert_pfn(vma,
1736 (unsigned long)vmf->virtual_address,
1737 pfn + page_offset);
1738 }
Chris Wilsonc9839302012-11-20 10:45:17 +00001739unpin:
Joonas Lahtinenc5ad54c2015-05-06 14:36:09 +03001740 i915_gem_object_ggtt_unpin_view(obj, &view);
Chris Wilsonc7150892009-09-23 00:43:56 +01001741unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001742 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001743out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001744 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001745 case -EIO:
Daniel Vetter2232f032014-09-04 09:36:18 +02001746 /*
1747 * We eat errors when the gpu is terminally wedged to avoid
1748 * userspace unduly crashing (gl has no provisions for mmaps to
1749 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1750 * and so needs to be reported.
1751 */
1752 if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
Paulo Zanonif65c9162013-11-27 18:20:34 -02001753 ret = VM_FAULT_SIGBUS;
1754 break;
1755 }
Chris Wilson045e7692010-11-07 09:18:22 +00001756 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001757 /*
1758 * EAGAIN means the gpu is hung and we'll wait for the error
1759 * handler to reset everything when re-faulting in
1760 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001761 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001762 case 0:
1763 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001764 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001765 case -EBUSY:
1766 /*
1767 * EBUSY is ok: this just means that another thread
1768 * already did the job.
1769 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001770 ret = VM_FAULT_NOPAGE;
1771 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001772 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001773 ret = VM_FAULT_OOM;
1774 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001775 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001776 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001777 ret = VM_FAULT_SIGBUS;
1778 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001779 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001780 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001781 ret = VM_FAULT_SIGBUS;
1782 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001783 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001784
1785 intel_runtime_pm_put(dev_priv);
1786 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001787}
1788
1789/**
Chris Wilson901782b2009-07-10 08:18:50 +01001790 * i915_gem_release_mmap - remove physical page mappings
1791 * @obj: obj in question
1792 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001793 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001794 * relinquish ownership of the pages back to the system.
1795 *
1796 * It is vital that we remove the page mapping if we have mapped a tiled
1797 * object through the GTT and then lose the fence register due to
1798 * resource pressure. Similarly if the object has been moved out of the
1799 * aperture, than pages mapped into userspace must be revoked. Removing the
1800 * mapping will then trigger a page fault on the next user access, allowing
1801 * fixup by i915_gem_fault().
1802 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001803void
Chris Wilson05394f32010-11-08 19:18:58 +00001804i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001805{
Chris Wilson6299f992010-11-24 12:23:44 +00001806 if (!obj->fault_mappable)
1807 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001808
David Herrmann6796cb12014-01-03 14:24:19 +01001809 drm_vma_node_unmap(&obj->base.vma_node,
1810 obj->base.dev->anon_inode->i_mapping);
Chris Wilson6299f992010-11-24 12:23:44 +00001811 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001812}
1813
Chris Wilsoneedd10f2014-06-16 08:57:44 +01001814void
1815i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
1816{
1817 struct drm_i915_gem_object *obj;
1818
1819 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
1820 i915_gem_release_mmap(obj);
1821}
1822
Imre Deak0fa87792013-01-07 21:47:35 +02001823uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001824i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001825{
Chris Wilsone28f8712011-07-18 13:11:49 -07001826 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001827
1828 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001829 tiling_mode == I915_TILING_NONE)
1830 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001831
1832 /* Previous chips need a power-of-two fence region when tiling */
1833 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001834 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001835 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001836 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001837
Chris Wilsone28f8712011-07-18 13:11:49 -07001838 while (gtt_size < size)
1839 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001840
Chris Wilsone28f8712011-07-18 13:11:49 -07001841 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001842}
1843
Jesse Barnesde151cf2008-11-12 10:03:55 -08001844/**
1845 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1846 * @obj: object to check
1847 *
1848 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001849 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001850 */
Imre Deakd8651102013-01-07 21:47:33 +02001851uint32_t
1852i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1853 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001854{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001855 /*
1856 * Minimum alignment is 4k (GTT page size), but might be greater
1857 * if a fence register is needed for the object.
1858 */
Imre Deakd8651102013-01-07 21:47:33 +02001859 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001860 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001861 return 4096;
1862
1863 /*
1864 * Previous chips need to be aligned to the size of the smallest
1865 * fence register that can contain the object.
1866 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001867 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001868}
1869
Chris Wilsond8cb5082012-08-11 15:41:03 +01001870static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1871{
1872 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1873 int ret;
1874
David Herrmann0de23972013-07-24 21:07:52 +02001875 if (drm_vma_node_has_offset(&obj->base.vma_node))
Chris Wilsond8cb5082012-08-11 15:41:03 +01001876 return 0;
1877
Daniel Vetterda494d72012-12-20 15:11:16 +01001878 dev_priv->mm.shrinker_no_lock_stealing = true;
1879
Chris Wilsond8cb5082012-08-11 15:41:03 +01001880 ret = drm_gem_create_mmap_offset(&obj->base);
1881 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001882 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001883
1884 /* Badly fragmented mmap space? The only way we can recover
1885 * space is by destroying unwanted objects. We can't randomly release
1886 * mmap_offsets as userspace expects them to be persistent for the
1887 * lifetime of the objects. The closest we can is to release the
1888 * offsets on purgeable objects by truncating it and marking it purged,
1889 * which prevents userspace from ever using that object again.
1890 */
Chris Wilson21ab4e72014-09-09 11:16:08 +01001891 i915_gem_shrink(dev_priv,
1892 obj->base.size >> PAGE_SHIFT,
1893 I915_SHRINK_BOUND |
1894 I915_SHRINK_UNBOUND |
1895 I915_SHRINK_PURGEABLE);
Chris Wilsond8cb5082012-08-11 15:41:03 +01001896 ret = drm_gem_create_mmap_offset(&obj->base);
1897 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001898 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001899
1900 i915_gem_shrink_all(dev_priv);
Daniel Vetterda494d72012-12-20 15:11:16 +01001901 ret = drm_gem_create_mmap_offset(&obj->base);
1902out:
1903 dev_priv->mm.shrinker_no_lock_stealing = false;
1904
1905 return ret;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001906}
1907
1908static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1909{
Chris Wilsond8cb5082012-08-11 15:41:03 +01001910 drm_gem_free_mmap_offset(&obj->base);
1911}
1912
Dave Airlieda6b51d2014-12-24 13:11:17 +10001913int
Dave Airlieff72145b2011-02-07 12:16:14 +10001914i915_gem_mmap_gtt(struct drm_file *file,
1915 struct drm_device *dev,
Dave Airlieda6b51d2014-12-24 13:11:17 +10001916 uint32_t handle,
Dave Airlieff72145b2011-02-07 12:16:14 +10001917 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001918{
Chris Wilson05394f32010-11-08 19:18:58 +00001919 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001920 int ret;
1921
Chris Wilson76c1dec2010-09-25 11:22:51 +01001922 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001923 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001924 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001925
Dave Airlieff72145b2011-02-07 12:16:14 +10001926 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001927 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001928 ret = -ENOENT;
1929 goto unlock;
1930 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001931
Chris Wilson05394f32010-11-08 19:18:58 +00001932 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00001933 DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00001934 ret = -EFAULT;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001935 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001936 }
1937
Chris Wilsond8cb5082012-08-11 15:41:03 +01001938 ret = i915_gem_object_create_mmap_offset(obj);
1939 if (ret)
1940 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001941
David Herrmann0de23972013-07-24 21:07:52 +02001942 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001943
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001944out:
Chris Wilson05394f32010-11-08 19:18:58 +00001945 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001946unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001947 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001948 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001949}
1950
Dave Airlieff72145b2011-02-07 12:16:14 +10001951/**
1952 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1953 * @dev: DRM device
1954 * @data: GTT mapping ioctl data
1955 * @file: GEM object info
1956 *
1957 * Simply returns the fake offset to userspace so it can mmap it.
1958 * The mmap call will end up in drm_gem_mmap(), which will set things
1959 * up so we can get faults in the handler above.
1960 *
1961 * The fault handler will take care of binding the object into the GTT
1962 * (since it may have been evicted to make room for something), allocating
1963 * a fence register, and mapping the appropriate aperture address into
1964 * userspace.
1965 */
1966int
1967i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1968 struct drm_file *file)
1969{
1970 struct drm_i915_gem_mmap_gtt *args = data;
1971
Dave Airlieda6b51d2014-12-24 13:11:17 +10001972 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
Dave Airlieff72145b2011-02-07 12:16:14 +10001973}
1974
Daniel Vetter225067e2012-08-20 10:23:20 +02001975/* Immediately discard the backing storage */
1976static void
1977i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001978{
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001979 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001980
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001981 if (obj->base.filp == NULL)
1982 return;
1983
Daniel Vetter225067e2012-08-20 10:23:20 +02001984 /* Our goal here is to return as much of the memory as
1985 * is possible back to the system as we are called from OOM.
1986 * To do this we must instruct the shmfs to drop all of its
1987 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01001988 */
Chris Wilson55372522014-03-25 13:23:06 +00001989 shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
Daniel Vetter225067e2012-08-20 10:23:20 +02001990 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001991}
Chris Wilsone5281cc2010-10-28 13:45:36 +01001992
Chris Wilson55372522014-03-25 13:23:06 +00001993/* Try to discard unwanted pages */
1994static void
1995i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
Daniel Vetter225067e2012-08-20 10:23:20 +02001996{
Chris Wilson55372522014-03-25 13:23:06 +00001997 struct address_space *mapping;
1998
1999 switch (obj->madv) {
2000 case I915_MADV_DONTNEED:
2001 i915_gem_object_truncate(obj);
2002 case __I915_MADV_PURGED:
2003 return;
2004 }
2005
2006 if (obj->base.filp == NULL)
2007 return;
2008
2009 mapping = file_inode(obj->base.filp)->i_mapping,
2010 invalidate_mapping_pages(mapping, 0, (loff_t)-1);
Chris Wilsone5281cc2010-10-28 13:45:36 +01002011}
2012
Chris Wilson5cdf5882010-09-27 15:51:07 +01002013static void
Chris Wilson05394f32010-11-08 19:18:58 +00002014i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002015{
Imre Deak90797e62013-02-18 19:28:03 +02002016 struct sg_page_iter sg_iter;
2017 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02002018
Chris Wilson05394f32010-11-08 19:18:58 +00002019 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07002020
Chris Wilson6c085a72012-08-20 11:40:46 +02002021 ret = i915_gem_object_set_to_cpu_domain(obj, true);
2022 if (ret) {
2023 /* In the event of a disaster, abandon all caches and
2024 * hope for the best.
2025 */
2026 WARN_ON(ret != -EIO);
Chris Wilson2c225692013-08-09 12:26:45 +01002027 i915_gem_clflush_object(obj, true);
Chris Wilson6c085a72012-08-20 11:40:46 +02002028 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2029 }
2030
Daniel Vetter6dacfd22011-09-12 21:30:02 +02002031 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07002032 i915_gem_object_save_bit_17_swizzle(obj);
2033
Chris Wilson05394f32010-11-08 19:18:58 +00002034 if (obj->madv == I915_MADV_DONTNEED)
2035 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01002036
Imre Deak90797e62013-02-18 19:28:03 +02002037 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02002038 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +01002039
Chris Wilson05394f32010-11-08 19:18:58 +00002040 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01002041 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002042
Chris Wilson05394f32010-11-08 19:18:58 +00002043 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01002044 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002045
Chris Wilson9da3da62012-06-01 15:20:22 +01002046 page_cache_release(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01002047 }
Chris Wilson05394f32010-11-08 19:18:58 +00002048 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002049
Chris Wilson9da3da62012-06-01 15:20:22 +01002050 sg_free_table(obj->pages);
2051 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01002052}
2053
Chris Wilsondd624af2013-01-15 12:39:35 +00002054int
Chris Wilson37e680a2012-06-07 15:38:42 +01002055i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
2056{
2057 const struct drm_i915_gem_object_ops *ops = obj->ops;
2058
Chris Wilson2f745ad2012-09-04 21:02:58 +01002059 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01002060 return 0;
2061
Chris Wilsona5570172012-09-04 21:02:54 +01002062 if (obj->pages_pin_count)
2063 return -EBUSY;
2064
Ben Widawsky98438772013-07-31 17:00:12 -07002065 BUG_ON(i915_gem_obj_bound_any(obj));
Ben Widawsky3e123022013-07-31 17:00:04 -07002066
Chris Wilsona2165e32012-12-03 11:49:00 +00002067 /* ->put_pages might need to allocate memory for the bit17 swizzle
2068 * array, hence protect them from being reaped by removing them from gtt
2069 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07002070 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00002071
Chris Wilson37e680a2012-06-07 15:38:42 +01002072 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00002073 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02002074
Chris Wilson55372522014-03-25 13:23:06 +00002075 i915_gem_object_invalidate(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02002076
2077 return 0;
2078}
2079
Chris Wilson37e680a2012-06-07 15:38:42 +01002080static int
Chris Wilson6c085a72012-08-20 11:40:46 +02002081i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002082{
Chris Wilson6c085a72012-08-20 11:40:46 +02002083 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002084 int page_count, i;
2085 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002086 struct sg_table *st;
2087 struct scatterlist *sg;
Imre Deak90797e62013-02-18 19:28:03 +02002088 struct sg_page_iter sg_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002089 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002090 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson6c085a72012-08-20 11:40:46 +02002091 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002092
Chris Wilson6c085a72012-08-20 11:40:46 +02002093 /* Assert that the object is not currently in any GPU domain. As it
2094 * wasn't in the GTT, there shouldn't be any way it could have been in
2095 * a GPU cache
2096 */
2097 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2098 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2099
Chris Wilson9da3da62012-06-01 15:20:22 +01002100 st = kmalloc(sizeof(*st), GFP_KERNEL);
2101 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002102 return -ENOMEM;
2103
Chris Wilson9da3da62012-06-01 15:20:22 +01002104 page_count = obj->base.size / PAGE_SIZE;
2105 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002106 kfree(st);
2107 return -ENOMEM;
2108 }
2109
2110 /* Get the list of pages out of our struct file. They'll be pinned
2111 * at this point until we release them.
2112 *
2113 * Fail silently without starting the shrinker
2114 */
Al Viro496ad9a2013-01-23 17:07:38 -05002115 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilson6c085a72012-08-20 11:40:46 +02002116 gfp = mapping_gfp_mask(mapping);
Linus Torvaldscaf49192012-12-10 10:51:16 -08002117 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
Chris Wilson6c085a72012-08-20 11:40:46 +02002118 gfp &= ~(__GFP_IO | __GFP_WAIT);
Imre Deak90797e62013-02-18 19:28:03 +02002119 sg = st->sgl;
2120 st->nents = 0;
2121 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002122 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2123 if (IS_ERR(page)) {
Chris Wilson21ab4e72014-09-09 11:16:08 +01002124 i915_gem_shrink(dev_priv,
2125 page_count,
2126 I915_SHRINK_BOUND |
2127 I915_SHRINK_UNBOUND |
2128 I915_SHRINK_PURGEABLE);
Chris Wilson6c085a72012-08-20 11:40:46 +02002129 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2130 }
2131 if (IS_ERR(page)) {
2132 /* We've tried hard to allocate the memory by reaping
2133 * our own buffer, now let the real VM do its job and
2134 * go down in flames if truly OOM.
2135 */
Chris Wilson6c085a72012-08-20 11:40:46 +02002136 i915_gem_shrink_all(dev_priv);
David Herrmannf461d1b2014-05-25 14:34:10 +02002137 page = shmem_read_mapping_page(mapping, i);
Chris Wilson6c085a72012-08-20 11:40:46 +02002138 if (IS_ERR(page))
2139 goto err_pages;
Chris Wilson6c085a72012-08-20 11:40:46 +02002140 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002141#ifdef CONFIG_SWIOTLB
2142 if (swiotlb_nr_tbl()) {
2143 st->nents++;
2144 sg_set_page(sg, page, PAGE_SIZE, 0);
2145 sg = sg_next(sg);
2146 continue;
2147 }
2148#endif
Imre Deak90797e62013-02-18 19:28:03 +02002149 if (!i || page_to_pfn(page) != last_pfn + 1) {
2150 if (i)
2151 sg = sg_next(sg);
2152 st->nents++;
2153 sg_set_page(sg, page, PAGE_SIZE, 0);
2154 } else {
2155 sg->length += PAGE_SIZE;
2156 }
2157 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002158
2159 /* Check that the i965g/gm workaround works. */
2160 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002161 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002162#ifdef CONFIG_SWIOTLB
2163 if (!swiotlb_nr_tbl())
2164#endif
2165 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002166 obj->pages = st;
2167
Eric Anholt673a3942008-07-30 12:06:12 -07002168 if (i915_gem_object_needs_bit17_swizzle(obj))
2169 i915_gem_object_do_bit_17_swizzle(obj);
2170
Daniel Vetter656bfa32014-11-20 09:26:30 +01002171 if (obj->tiling_mode != I915_TILING_NONE &&
2172 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
2173 i915_gem_object_pin_pages(obj);
2174
Eric Anholt673a3942008-07-30 12:06:12 -07002175 return 0;
2176
2177err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02002178 sg_mark_end(sg);
2179 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
Imre Deak2db76d72013-03-26 15:14:18 +02002180 page_cache_release(sg_page_iter_page(&sg_iter));
Chris Wilson9da3da62012-06-01 15:20:22 +01002181 sg_free_table(st);
2182 kfree(st);
Chris Wilson0820baf2014-03-25 13:23:03 +00002183
2184 /* shmemfs first checks if there is enough memory to allocate the page
2185 * and reports ENOSPC should there be insufficient, along with the usual
2186 * ENOMEM for a genuine allocation failure.
2187 *
2188 * We use ENOSPC in our driver to mean that we have run out of aperture
2189 * space and so want to translate the error from shmemfs back to our
2190 * usual understanding of ENOMEM.
2191 */
2192 if (PTR_ERR(page) == -ENOSPC)
2193 return -ENOMEM;
2194 else
2195 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002196}
2197
Chris Wilson37e680a2012-06-07 15:38:42 +01002198/* Ensure that the associated pages are gathered from the backing storage
2199 * and pinned into our object. i915_gem_object_get_pages() may be called
2200 * multiple times before they are released by a single call to
2201 * i915_gem_object_put_pages() - once the pages are no longer referenced
2202 * either as a result of memory pressure (reaping pages under the shrinker)
2203 * or as the object is itself released.
2204 */
2205int
2206i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2207{
2208 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2209 const struct drm_i915_gem_object_ops *ops = obj->ops;
2210 int ret;
2211
Chris Wilson2f745ad2012-09-04 21:02:58 +01002212 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01002213 return 0;
2214
Chris Wilson43e28f02013-01-08 10:53:09 +00002215 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002216 DRM_DEBUG("Attempting to obtain a purgeable object\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002217 return -EFAULT;
Chris Wilson43e28f02013-01-08 10:53:09 +00002218 }
2219
Chris Wilsona5570172012-09-04 21:02:54 +01002220 BUG_ON(obj->pages_pin_count);
2221
Chris Wilson37e680a2012-06-07 15:38:42 +01002222 ret = ops->get_pages(obj);
2223 if (ret)
2224 return ret;
2225
Ben Widawsky35c20a62013-05-31 11:28:48 -07002226 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilsonee286372015-04-07 16:20:25 +01002227
2228 obj->get_page.sg = obj->pages->sgl;
2229 obj->get_page.last = 0;
2230
Chris Wilson37e680a2012-06-07 15:38:42 +01002231 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002232}
2233
Ben Widawskye2d05a82013-09-24 09:57:58 -07002234static void
Chris Wilson05394f32010-11-08 19:18:58 +00002235i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002236 struct intel_engine_cs *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002237{
John Harrison41c52412014-11-24 18:49:43 +00002238 struct drm_i915_gem_request *req;
2239 struct intel_engine_cs *old_ring;
Daniel Vetter617dbe22010-02-11 22:16:02 +01002240
Zou Nan hai852835f2010-05-21 09:08:56 +08002241 BUG_ON(ring == NULL);
John Harrison41c52412014-11-24 18:49:43 +00002242
2243 req = intel_ring_get_request(ring);
2244 old_ring = i915_gem_request_get_ring(obj->last_read_req);
2245
2246 if (old_ring != ring && obj->last_write_req) {
John Harrison97b2a6a2014-11-24 18:49:26 +00002247 /* Keep the request relative to the current ring */
2248 i915_gem_request_assign(&obj->last_write_req, req);
Chris Wilson02978ff2013-07-09 09:22:39 +01002249 }
Eric Anholt673a3942008-07-30 12:06:12 -07002250
2251 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00002252 if (!obj->active) {
2253 drm_gem_object_reference(&obj->base);
2254 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07002255 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01002256
Chris Wilson05394f32010-11-08 19:18:58 +00002257 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002258
John Harrison97b2a6a2014-11-24 18:49:26 +00002259 i915_gem_request_assign(&obj->last_read_req, req);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002260}
2261
Ben Widawskye2d05a82013-09-24 09:57:58 -07002262void i915_vma_move_to_active(struct i915_vma *vma,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002263 struct intel_engine_cs *ring)
Ben Widawskye2d05a82013-09-24 09:57:58 -07002264{
2265 list_move_tail(&vma->mm_list, &vma->vm->active_list);
2266 return i915_gem_object_move_to_active(vma->obj, ring);
2267}
2268
Chris Wilsoncaea7472010-11-12 13:53:37 +00002269static void
Chris Wilsoncaea7472010-11-12 13:53:37 +00002270i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
2271{
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002272 struct i915_vma *vma;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002273
Chris Wilson65ce3022012-07-20 12:41:02 +01002274 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002275 BUG_ON(!obj->active);
Chris Wilson65ce3022012-07-20 12:41:02 +01002276
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00002277 list_for_each_entry(vma, &obj->vma_list, vma_link) {
2278 if (!list_empty(&vma->mm_list))
2279 list_move_tail(&vma->mm_list, &vma->vm->inactive_list);
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002280 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00002281
Daniel Vetterf99d7062014-06-19 16:01:59 +02002282 intel_fb_obj_flush(obj, true);
2283
Chris Wilson65ce3022012-07-20 12:41:02 +01002284 list_del_init(&obj->ring_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002285
John Harrison97b2a6a2014-11-24 18:49:26 +00002286 i915_gem_request_assign(&obj->last_read_req, NULL);
2287 i915_gem_request_assign(&obj->last_write_req, NULL);
Chris Wilson65ce3022012-07-20 12:41:02 +01002288 obj->base.write_domain = 0;
2289
John Harrison97b2a6a2014-11-24 18:49:26 +00002290 i915_gem_request_assign(&obj->last_fenced_req, NULL);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002291
2292 obj->active = 0;
2293 drm_gem_object_unreference(&obj->base);
2294
2295 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08002296}
Eric Anholt673a3942008-07-30 12:06:12 -07002297
Chris Wilsonc8725f32014-03-17 12:21:55 +00002298static void
2299i915_gem_object_retire(struct drm_i915_gem_object *obj)
2300{
John Harrison41c52412014-11-24 18:49:43 +00002301 if (obj->last_read_req == NULL)
Chris Wilsonc8725f32014-03-17 12:21:55 +00002302 return;
2303
John Harrison1b5a4332014-11-24 18:49:42 +00002304 if (i915_gem_request_completed(obj->last_read_req, true))
Chris Wilsonc8725f32014-03-17 12:21:55 +00002305 i915_gem_object_move_to_inactive(obj);
2306}
2307
Chris Wilson9d7730912012-11-27 16:22:52 +00002308static int
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002309i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002310{
Chris Wilson9d7730912012-11-27 16:22:52 +00002311 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002312 struct intel_engine_cs *ring;
Chris Wilson9d7730912012-11-27 16:22:52 +00002313 int ret, i, j;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002314
Chris Wilson107f27a52012-12-10 13:56:17 +02002315 /* Carefully retire all requests without writing to the rings */
Chris Wilson9d7730912012-11-27 16:22:52 +00002316 for_each_ring(ring, dev_priv, i) {
Chris Wilson107f27a52012-12-10 13:56:17 +02002317 ret = intel_ring_idle(ring);
2318 if (ret)
2319 return ret;
Chris Wilson9d7730912012-11-27 16:22:52 +00002320 }
Chris Wilson9d7730912012-11-27 16:22:52 +00002321 i915_gem_retire_requests(dev);
Chris Wilson107f27a52012-12-10 13:56:17 +02002322
2323 /* Finally reset hw state */
Chris Wilson9d7730912012-11-27 16:22:52 +00002324 for_each_ring(ring, dev_priv, i) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002325 intel_ring_init_seqno(ring, seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002326
Ben Widawskyebc348b2014-04-29 14:52:28 -07002327 for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
2328 ring->semaphore.sync_seqno[j] = 0;
Chris Wilson9d7730912012-11-27 16:22:52 +00002329 }
2330
2331 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002332}
2333
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002334int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
2335{
2336 struct drm_i915_private *dev_priv = dev->dev_private;
2337 int ret;
2338
2339 if (seqno == 0)
2340 return -EINVAL;
2341
2342 /* HWS page needs to be set less than what we
2343 * will inject to ring
2344 */
2345 ret = i915_gem_init_seqno(dev, seqno - 1);
2346 if (ret)
2347 return ret;
2348
2349 /* Carefully set the last_seqno value so that wrap
2350 * detection still works
2351 */
2352 dev_priv->next_seqno = seqno;
2353 dev_priv->last_seqno = seqno - 1;
2354 if (dev_priv->last_seqno == 0)
2355 dev_priv->last_seqno--;
2356
2357 return 0;
2358}
2359
Chris Wilson9d7730912012-11-27 16:22:52 +00002360int
2361i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002362{
Chris Wilson9d7730912012-11-27 16:22:52 +00002363 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002364
Chris Wilson9d7730912012-11-27 16:22:52 +00002365 /* reserve 0 for non-seqno */
2366 if (dev_priv->next_seqno == 0) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002367 int ret = i915_gem_init_seqno(dev, 0);
Chris Wilson9d7730912012-11-27 16:22:52 +00002368 if (ret)
2369 return ret;
2370
2371 dev_priv->next_seqno = 1;
2372 }
2373
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02002374 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
Chris Wilson9d7730912012-11-27 16:22:52 +00002375 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002376}
2377
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002378int __i915_add_request(struct intel_engine_cs *ring,
Mika Kuoppala0025c072013-06-12 12:35:30 +03002379 struct drm_file *file,
John Harrison9400ae52014-11-24 18:49:36 +00002380 struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002381{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002382 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsonacb868d2012-09-26 13:47:30 +01002383 struct drm_i915_gem_request *request;
Oscar Mateo48e29f52014-07-24 17:04:29 +01002384 struct intel_ringbuffer *ringbuf;
Nick Hoath6d3d8272015-01-15 13:10:39 +00002385 u32 request_start;
Chris Wilson3cce4692010-10-27 16:11:02 +01002386 int ret;
2387
John Harrison6259cea2014-11-24 18:49:29 +00002388 request = ring->outstanding_lazy_request;
Oscar Mateo48e29f52014-07-24 17:04:29 +01002389 if (WARN_ON(request == NULL))
2390 return -ENOMEM;
2391
2392 if (i915.enable_execlists) {
Nick Hoath21076372015-01-15 13:10:38 +00002393 ringbuf = request->ctx->engine[ring->id].ringbuf;
Oscar Mateo48e29f52014-07-24 17:04:29 +01002394 } else
2395 ringbuf = ring->buffer;
2396
2397 request_start = intel_ring_get_tail(ringbuf);
Daniel Vettercc889e02012-06-13 20:45:19 +02002398 /*
2399 * Emit any outstanding flushes - execbuf can fail to emit the flush
2400 * after having emitted the batchbuffer command. Hence we need to fix
2401 * things up similar to emitting the lazy request. The difference here
2402 * is that the flush _must_ happen before the next request, no matter
2403 * what.
2404 */
Oscar Mateo48e29f52014-07-24 17:04:29 +01002405 if (i915.enable_execlists) {
Nick Hoath21076372015-01-15 13:10:38 +00002406 ret = logical_ring_flush_all_caches(ringbuf, request->ctx);
Oscar Mateo48e29f52014-07-24 17:04:29 +01002407 if (ret)
2408 return ret;
2409 } else {
2410 ret = intel_ring_flush_all_caches(ring);
2411 if (ret)
2412 return ret;
2413 }
Daniel Vettercc889e02012-06-13 20:45:19 +02002414
Chris Wilsona71d8d92012-02-15 11:25:36 +00002415 /* Record the position of the start of the request so that
2416 * should we detect the updated seqno part-way through the
2417 * GPU processing the request, we never over-estimate the
2418 * position of the head.
2419 */
Nick Hoath6d3d8272015-01-15 13:10:39 +00002420 request->postfix = intel_ring_get_tail(ringbuf);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002421
Oscar Mateo48e29f52014-07-24 17:04:29 +01002422 if (i915.enable_execlists) {
Nick Hoath72f95af2015-01-15 13:10:37 +00002423 ret = ring->emit_request(ringbuf, request);
Oscar Mateo48e29f52014-07-24 17:04:29 +01002424 if (ret)
2425 return ret;
2426 } else {
2427 ret = ring->add_request(ring);
2428 if (ret)
2429 return ret;
Michel Thierry53292cd2015-04-15 18:11:33 +01002430
2431 request->tail = intel_ring_get_tail(ringbuf);
Oscar Mateo48e29f52014-07-24 17:04:29 +01002432 }
Eric Anholt673a3942008-07-30 12:06:12 -07002433
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002434 request->head = request_start;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002435
2436 /* Whilst this request exists, batch_obj will be on the
2437 * active_list, and so will hold the active reference. Only when this
2438 * request is retired will the the batch_obj be moved onto the
2439 * inactive_list and lose its active reference. Hence we do not need
2440 * to explicitly hold another reference here.
2441 */
Chris Wilson9a7e0c22013-08-26 19:50:54 -03002442 request->batch_obj = obj;
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002443
Oscar Mateo48e29f52014-07-24 17:04:29 +01002444 if (!i915.enable_execlists) {
2445 /* Hold a reference to the current context so that we can inspect
2446 * it later in case a hangcheck error event fires.
2447 */
2448 request->ctx = ring->last_context;
2449 if (request->ctx)
2450 i915_gem_context_reference(request->ctx);
2451 }
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002452
Eric Anholt673a3942008-07-30 12:06:12 -07002453 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08002454 list_add_tail(&request->list, &ring->request_list);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002455 request->file_priv = NULL;
Zou Nan hai852835f2010-05-21 09:08:56 +08002456
Chris Wilsondb53a302011-02-03 11:57:46 +00002457 if (file) {
2458 struct drm_i915_file_private *file_priv = file->driver_priv;
2459
Chris Wilson1c255952010-09-26 11:03:27 +01002460 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002461 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00002462 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002463 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01002464 spin_unlock(&file_priv->mm.lock);
Mika Kuoppala071c92d2015-02-12 10:26:02 +02002465
2466 request->pid = get_pid(task_pid(current));
Eric Anholtb9624422009-06-03 07:27:35 +00002467 }
Eric Anholt673a3942008-07-30 12:06:12 -07002468
John Harrison74328ee2014-11-24 18:49:38 +00002469 trace_i915_gem_request_add(request);
John Harrison6259cea2014-11-24 18:49:29 +00002470 ring->outstanding_lazy_request = NULL;
Chris Wilsondb53a302011-02-03 11:57:46 +00002471
Daniel Vetter87255482014-11-19 20:36:48 +01002472 i915_queue_hangcheck(ring->dev);
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03002473
Daniel Vetter87255482014-11-19 20:36:48 +01002474 queue_delayed_work(dev_priv->wq,
2475 &dev_priv->mm.retire_work,
2476 round_jiffies_up_relative(HZ));
2477 intel_mark_busy(dev_priv->dev);
Daniel Vettercc889e02012-06-13 20:45:19 +02002478
Chris Wilson3cce4692010-10-27 16:11:02 +01002479 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002480}
2481
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002482static inline void
2483i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07002484{
Chris Wilson1c255952010-09-26 11:03:27 +01002485 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002486
Chris Wilson1c255952010-09-26 11:03:27 +01002487 if (!file_priv)
2488 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002489
Chris Wilson1c255952010-09-26 11:03:27 +01002490 spin_lock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002491 list_del(&request->client_list);
2492 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01002493 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002494}
2495
Mika Kuoppala939fd762014-01-30 19:04:44 +02002496static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
Oscar Mateo273497e2014-05-22 14:13:37 +01002497 const struct intel_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002498{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002499 unsigned long elapsed;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002500
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002501 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
2502
2503 if (ctx->hang_stats.banned)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002504 return true;
2505
Chris Wilson676fa572014-12-24 08:13:39 -08002506 if (ctx->hang_stats.ban_period_seconds &&
2507 elapsed <= ctx->hang_stats.ban_period_seconds) {
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002508 if (!i915_gem_context_is_default(ctx)) {
Mika Kuoppala3fac8972014-01-30 16:05:48 +02002509 DRM_DEBUG("context hanging too fast, banning!\n");
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002510 return true;
Mika Kuoppala88b4aa82014-03-28 18:18:18 +02002511 } else if (i915_stop_ring_allow_ban(dev_priv)) {
2512 if (i915_stop_ring_allow_warn(dev_priv))
2513 DRM_ERROR("gpu hanging too fast, banning!\n");
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002514 return true;
Mika Kuoppala3fac8972014-01-30 16:05:48 +02002515 }
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002516 }
2517
2518 return false;
2519}
2520
Mika Kuoppala939fd762014-01-30 19:04:44 +02002521static void i915_set_reset_status(struct drm_i915_private *dev_priv,
Oscar Mateo273497e2014-05-22 14:13:37 +01002522 struct intel_context *ctx,
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002523 const bool guilty)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002524{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002525 struct i915_ctx_hang_stats *hs;
2526
2527 if (WARN_ON(!ctx))
2528 return;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002529
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002530 hs = &ctx->hang_stats;
2531
2532 if (guilty) {
Mika Kuoppala939fd762014-01-30 19:04:44 +02002533 hs->banned = i915_context_is_banned(dev_priv, ctx);
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002534 hs->batch_active++;
2535 hs->guilty_ts = get_seconds();
2536 } else {
2537 hs->batch_pending++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002538 }
2539}
2540
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002541static void i915_gem_free_request(struct drm_i915_gem_request *request)
2542{
2543 list_del(&request->list);
2544 i915_gem_request_remove_from_client(request);
2545
Mika Kuoppala071c92d2015-02-12 10:26:02 +02002546 put_pid(request->pid);
2547
John Harrisonabfe2622014-11-24 18:49:24 +00002548 i915_gem_request_unreference(request);
2549}
2550
2551void i915_gem_request_free(struct kref *req_ref)
2552{
2553 struct drm_i915_gem_request *req = container_of(req_ref,
2554 typeof(*req), ref);
2555 struct intel_context *ctx = req->ctx;
2556
Thomas Daniel0794aed2014-11-25 10:39:25 +00002557 if (ctx) {
2558 if (i915.enable_execlists) {
John Harrisonabfe2622014-11-24 18:49:24 +00002559 struct intel_engine_cs *ring = req->ring;
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002560
Thomas Daniel0794aed2014-11-25 10:39:25 +00002561 if (ctx != ring->default_context)
2562 intel_lr_context_unpin(ring, ctx);
2563 }
John Harrisonabfe2622014-11-24 18:49:24 +00002564
Oscar Mateodcb4c122014-11-13 10:28:10 +00002565 i915_gem_context_unreference(ctx);
2566 }
John Harrisonabfe2622014-11-24 18:49:24 +00002567
Chris Wilsonefab6d82015-04-07 16:20:57 +01002568 kmem_cache_free(req->i915->requests, req);
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002569}
2570
John Harrison6689cb22015-03-19 12:30:08 +00002571int i915_gem_request_alloc(struct intel_engine_cs *ring,
2572 struct intel_context *ctx)
2573{
Chris Wilsonefab6d82015-04-07 16:20:57 +01002574 struct drm_i915_private *dev_priv = to_i915(ring->dev);
2575 struct drm_i915_gem_request *rq;
John Harrison6689cb22015-03-19 12:30:08 +00002576 int ret;
John Harrison6689cb22015-03-19 12:30:08 +00002577
2578 if (ring->outstanding_lazy_request)
2579 return 0;
2580
Chris Wilsonefab6d82015-04-07 16:20:57 +01002581 rq = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
2582 if (rq == NULL)
John Harrison6689cb22015-03-19 12:30:08 +00002583 return -ENOMEM;
2584
Chris Wilsonefab6d82015-04-07 16:20:57 +01002585 kref_init(&rq->ref);
2586 rq->i915 = dev_priv;
2587
2588 ret = i915_gem_get_seqno(ring->dev, &rq->seqno);
John Harrison6689cb22015-03-19 12:30:08 +00002589 if (ret) {
Chris Wilsonefab6d82015-04-07 16:20:57 +01002590 kfree(rq);
John Harrison6689cb22015-03-19 12:30:08 +00002591 return ret;
2592 }
2593
Chris Wilsonefab6d82015-04-07 16:20:57 +01002594 rq->ring = ring;
John Harrison6689cb22015-03-19 12:30:08 +00002595
2596 if (i915.enable_execlists)
Chris Wilsonefab6d82015-04-07 16:20:57 +01002597 ret = intel_logical_ring_alloc_request_extras(rq, ctx);
John Harrison6689cb22015-03-19 12:30:08 +00002598 else
Chris Wilsonefab6d82015-04-07 16:20:57 +01002599 ret = intel_ring_alloc_request_extras(rq);
John Harrison6689cb22015-03-19 12:30:08 +00002600 if (ret) {
Chris Wilsonefab6d82015-04-07 16:20:57 +01002601 kfree(rq);
John Harrison6689cb22015-03-19 12:30:08 +00002602 return ret;
2603 }
2604
Chris Wilsonefab6d82015-04-07 16:20:57 +01002605 ring->outstanding_lazy_request = rq;
John Harrison6689cb22015-03-19 12:30:08 +00002606 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002607}
2608
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002609struct drm_i915_gem_request *
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002610i915_gem_find_active_request(struct intel_engine_cs *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01002611{
Chris Wilson4db080f2013-12-04 11:37:09 +00002612 struct drm_i915_gem_request *request;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002613
Chris Wilson4db080f2013-12-04 11:37:09 +00002614 list_for_each_entry(request, &ring->request_list, list) {
John Harrison1b5a4332014-11-24 18:49:42 +00002615 if (i915_gem_request_completed(request, false))
Chris Wilson4db080f2013-12-04 11:37:09 +00002616 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002617
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002618 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002619 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002620
2621 return NULL;
2622}
2623
2624static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002625 struct intel_engine_cs *ring)
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002626{
2627 struct drm_i915_gem_request *request;
2628 bool ring_hung;
2629
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002630 request = i915_gem_find_active_request(ring);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002631
2632 if (request == NULL)
2633 return;
2634
2635 ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
2636
Mika Kuoppala939fd762014-01-30 19:04:44 +02002637 i915_set_reset_status(dev_priv, request->ctx, ring_hung);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002638
2639 list_for_each_entry_continue(request, &ring->request_list, list)
Mika Kuoppala939fd762014-01-30 19:04:44 +02002640 i915_set_reset_status(dev_priv, request->ctx, false);
Chris Wilson4db080f2013-12-04 11:37:09 +00002641}
2642
2643static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002644 struct intel_engine_cs *ring)
Chris Wilson4db080f2013-12-04 11:37:09 +00002645{
Chris Wilsondfaae392010-09-22 10:31:52 +01002646 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002647 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002648
Chris Wilson05394f32010-11-08 19:18:58 +00002649 obj = list_first_entry(&ring->active_list,
2650 struct drm_i915_gem_object,
2651 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002652
Chris Wilson05394f32010-11-08 19:18:58 +00002653 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002654 }
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002655
2656 /*
Oscar Mateodcb4c122014-11-13 10:28:10 +00002657 * Clear the execlists queue up before freeing the requests, as those
2658 * are the ones that keep the context and ringbuffer backing objects
2659 * pinned in place.
2660 */
2661 while (!list_empty(&ring->execlist_queue)) {
Nick Hoath6d3d8272015-01-15 13:10:39 +00002662 struct drm_i915_gem_request *submit_req;
Oscar Mateodcb4c122014-11-13 10:28:10 +00002663
2664 submit_req = list_first_entry(&ring->execlist_queue,
Nick Hoath6d3d8272015-01-15 13:10:39 +00002665 struct drm_i915_gem_request,
Oscar Mateodcb4c122014-11-13 10:28:10 +00002666 execlist_link);
2667 list_del(&submit_req->execlist_link);
Mika Kuoppala1197b4f2015-01-13 11:32:24 +02002668
2669 if (submit_req->ctx != ring->default_context)
2670 intel_lr_context_unpin(ring, submit_req->ctx);
2671
Nick Hoathb3a38992015-02-19 16:30:47 +00002672 i915_gem_request_unreference(submit_req);
Oscar Mateodcb4c122014-11-13 10:28:10 +00002673 }
2674
2675 /*
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002676 * We must free the requests after all the corresponding objects have
2677 * been moved off active lists. Which is the same order as the normal
2678 * retire_requests function does. This is important if object hold
2679 * implicit references on things like e.g. ppgtt address spaces through
2680 * the request.
2681 */
2682 while (!list_empty(&ring->request_list)) {
2683 struct drm_i915_gem_request *request;
2684
2685 request = list_first_entry(&ring->request_list,
2686 struct drm_i915_gem_request,
2687 list);
2688
2689 i915_gem_free_request(request);
2690 }
Chris Wilsone3efda42014-04-09 09:19:41 +01002691
John Harrison6259cea2014-11-24 18:49:29 +00002692 /* This may not have been flushed before the reset, so clean it now */
2693 i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07002694}
2695
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002696void i915_gem_restore_fences(struct drm_device *dev)
Chris Wilson312817a2010-11-22 11:50:11 +00002697{
2698 struct drm_i915_private *dev_priv = dev->dev_private;
2699 int i;
2700
Daniel Vetter4b9de732011-10-09 21:52:02 +02002701 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00002702 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00002703
Daniel Vetter94a335d2013-07-17 14:51:28 +02002704 /*
2705 * Commit delayed tiling changes if we have an object still
2706 * attached to the fence, otherwise just clear the fence.
2707 */
2708 if (reg->obj) {
2709 i915_gem_object_update_fence(reg->obj, reg,
2710 reg->obj->tiling_mode);
2711 } else {
2712 i915_gem_write_fence(dev, i, NULL);
2713 }
Chris Wilson312817a2010-11-22 11:50:11 +00002714 }
2715}
2716
Chris Wilson069efc12010-09-30 16:53:18 +01002717void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002718{
Chris Wilsondfaae392010-09-22 10:31:52 +01002719 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002720 struct intel_engine_cs *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002721 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002722
Chris Wilson4db080f2013-12-04 11:37:09 +00002723 /*
2724 * Before we free the objects from the requests, we need to inspect
2725 * them for finding the guilty party. As the requests only borrow
2726 * their reference to the objects, the inspection must be done first.
2727 */
Chris Wilsonb4519512012-05-11 14:29:30 +01002728 for_each_ring(ring, dev_priv, i)
Chris Wilson4db080f2013-12-04 11:37:09 +00002729 i915_gem_reset_ring_status(dev_priv, ring);
2730
2731 for_each_ring(ring, dev_priv, i)
2732 i915_gem_reset_ring_cleanup(dev_priv, ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01002733
Ben Widawskyacce9ff2013-12-06 14:11:03 -08002734 i915_gem_context_reset(dev);
2735
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002736 i915_gem_restore_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002737}
2738
2739/**
2740 * This function clears the request list as sequence numbers are passed.
2741 */
Chris Wilson1cf0ba12014-05-05 09:07:33 +01002742void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002743i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002744{
Chris Wilsondb53a302011-02-03 11:57:46 +00002745 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01002746 return;
2747
Chris Wilsondb53a302011-02-03 11:57:46 +00002748 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002749
Chris Wilson832a3aa2015-03-18 18:19:22 +00002750 /* Retire requests first as we use it above for the early return.
2751 * If we retire requests last, we may use a later seqno and so clear
2752 * the requests lists without clearing the active list, leading to
2753 * confusion.
Chris Wilsone9103032014-01-07 11:45:14 +00002754 */
Zou Nan hai852835f2010-05-21 09:08:56 +08002755 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002756 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002757
Zou Nan hai852835f2010-05-21 09:08:56 +08002758 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002759 struct drm_i915_gem_request,
2760 list);
Eric Anholt673a3942008-07-30 12:06:12 -07002761
John Harrison1b5a4332014-11-24 18:49:42 +00002762 if (!i915_gem_request_completed(request, true))
Eric Anholt673a3942008-07-30 12:06:12 -07002763 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002764
John Harrison74328ee2014-11-24 18:49:38 +00002765 trace_i915_gem_request_retire(request);
Oscar Mateo48e29f52014-07-24 17:04:29 +01002766
Chris Wilsona71d8d92012-02-15 11:25:36 +00002767 /* We know the GPU must have read the request to have
2768 * sent us the seqno + interrupt, so use the position
2769 * of tail of the request to update the last known position
2770 * of the GPU head.
2771 */
John Harrison98e1bd42015-02-13 11:48:12 +00002772 request->ringbuf->last_retired_head = request->postfix;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002773
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002774 i915_gem_free_request(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002775 }
2776
Chris Wilson832a3aa2015-03-18 18:19:22 +00002777 /* Move any buffers on the active list that are no longer referenced
2778 * by the ringbuffer to the flushing/inactive lists as appropriate,
2779 * before we free the context associated with the requests.
2780 */
2781 while (!list_empty(&ring->active_list)) {
2782 struct drm_i915_gem_object *obj;
2783
2784 obj = list_first_entry(&ring->active_list,
2785 struct drm_i915_gem_object,
2786 ring_list);
2787
2788 if (!i915_gem_request_completed(obj->last_read_req, true))
2789 break;
2790
2791 i915_gem_object_move_to_inactive(obj);
2792 }
2793
John Harrison581c26e82014-11-24 18:49:39 +00002794 if (unlikely(ring->trace_irq_req &&
2795 i915_gem_request_completed(ring->trace_irq_req, true))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002796 ring->irq_put(ring);
John Harrison581c26e82014-11-24 18:49:39 +00002797 i915_gem_request_assign(&ring->trace_irq_req, NULL);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002798 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002799
Chris Wilsondb53a302011-02-03 11:57:46 +00002800 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002801}
2802
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002803bool
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002804i915_gem_retire_requests(struct drm_device *dev)
2805{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002806 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002807 struct intel_engine_cs *ring;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002808 bool idle = true;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002809 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002810
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002811 for_each_ring(ring, dev_priv, i) {
Chris Wilsonb4519512012-05-11 14:29:30 +01002812 i915_gem_retire_requests_ring(ring);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002813 idle &= list_empty(&ring->request_list);
Thomas Danielc86ee3a92014-11-13 10:27:05 +00002814 if (i915.enable_execlists) {
2815 unsigned long flags;
2816
2817 spin_lock_irqsave(&ring->execlist_lock, flags);
2818 idle &= list_empty(&ring->execlist_queue);
2819 spin_unlock_irqrestore(&ring->execlist_lock, flags);
2820
2821 intel_execlists_retire_requests(ring);
2822 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002823 }
2824
2825 if (idle)
2826 mod_delayed_work(dev_priv->wq,
2827 &dev_priv->mm.idle_work,
2828 msecs_to_jiffies(100));
2829
2830 return idle;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002831}
2832
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002833static void
Eric Anholt673a3942008-07-30 12:06:12 -07002834i915_gem_retire_work_handler(struct work_struct *work)
2835{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002836 struct drm_i915_private *dev_priv =
2837 container_of(work, typeof(*dev_priv), mm.retire_work.work);
2838 struct drm_device *dev = dev_priv->dev;
Chris Wilson0a587052011-01-09 21:05:44 +00002839 bool idle;
Eric Anholt673a3942008-07-30 12:06:12 -07002840
Chris Wilson891b48c2010-09-29 12:26:37 +01002841 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002842 idle = false;
2843 if (mutex_trylock(&dev->struct_mutex)) {
2844 idle = i915_gem_retire_requests(dev);
2845 mutex_unlock(&dev->struct_mutex);
2846 }
2847 if (!idle)
Chris Wilsonbcb45082012-10-05 17:02:57 +01002848 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2849 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002850}
Chris Wilson891b48c2010-09-29 12:26:37 +01002851
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002852static void
2853i915_gem_idle_work_handler(struct work_struct *work)
2854{
2855 struct drm_i915_private *dev_priv =
2856 container_of(work, typeof(*dev_priv), mm.idle_work.work);
Chris Wilson35c94182015-04-07 16:20:37 +01002857 struct drm_device *dev = dev_priv->dev;
Chris Wilson423795c2015-04-07 16:21:08 +01002858 struct intel_engine_cs *ring;
2859 int i;
Zou Nan haid1b851f2010-05-21 09:08:57 +08002860
Chris Wilson423795c2015-04-07 16:21:08 +01002861 for_each_ring(ring, dev_priv, i)
2862 if (!list_empty(&ring->request_list))
2863 return;
Zou Nan hai852835f2010-05-21 09:08:56 +08002864
Chris Wilson35c94182015-04-07 16:20:37 +01002865 intel_mark_idle(dev);
2866
2867 if (mutex_trylock(&dev->struct_mutex)) {
2868 struct intel_engine_cs *ring;
2869 int i;
2870
2871 for_each_ring(ring, dev_priv, i)
2872 i915_gem_batch_pool_fini(&ring->batch_pool);
2873
2874 mutex_unlock(&dev->struct_mutex);
2875 }
Eric Anholt673a3942008-07-30 12:06:12 -07002876}
2877
Ben Widawsky5816d642012-04-11 11:18:19 -07002878/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002879 * Ensures that an object will eventually get non-busy by flushing any required
2880 * write domains, emitting any outstanding lazy request and retiring and
2881 * completed requests.
2882 */
2883static int
2884i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2885{
John Harrison41c52412014-11-24 18:49:43 +00002886 struct intel_engine_cs *ring;
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002887 int ret;
2888
2889 if (obj->active) {
John Harrison41c52412014-11-24 18:49:43 +00002890 ring = i915_gem_request_get_ring(obj->last_read_req);
2891
John Harrisonb6660d52014-11-24 18:49:30 +00002892 ret = i915_gem_check_olr(obj->last_read_req);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002893 if (ret)
2894 return ret;
2895
John Harrison41c52412014-11-24 18:49:43 +00002896 i915_gem_retire_requests_ring(ring);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002897 }
2898
2899 return 0;
2900}
2901
2902/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002903 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2904 * @DRM_IOCTL_ARGS: standard ioctl arguments
2905 *
2906 * Returns 0 if successful, else an error is returned with the remaining time in
2907 * the timeout parameter.
2908 * -ETIME: object is still busy after timeout
2909 * -ERESTARTSYS: signal interrupted the wait
2910 * -ENONENT: object doesn't exist
2911 * Also possible, but rare:
2912 * -EAGAIN: GPU wedged
2913 * -ENOMEM: damn
2914 * -ENODEV: Internal IRQ fail
2915 * -E?: The add request failed
2916 *
2917 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2918 * non-zero timeout parameter the wait ioctl will wait for the given number of
2919 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2920 * without holding struct_mutex the object may become re-busied before this
2921 * function completes. A similar but shorter * race condition exists in the busy
2922 * ioctl
2923 */
2924int
2925i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2926{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002927 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002928 struct drm_i915_gem_wait *args = data;
2929 struct drm_i915_gem_object *obj;
John Harrisonff865882014-11-24 18:49:28 +00002930 struct drm_i915_gem_request *req;
Daniel Vetterf69061b2012-12-06 09:01:42 +01002931 unsigned reset_counter;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002932 int ret = 0;
2933
Daniel Vetter11b5d512014-09-29 15:31:26 +02002934 if (args->flags != 0)
2935 return -EINVAL;
2936
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002937 ret = i915_mutex_lock_interruptible(dev);
2938 if (ret)
2939 return ret;
2940
2941 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2942 if (&obj->base == NULL) {
2943 mutex_unlock(&dev->struct_mutex);
2944 return -ENOENT;
2945 }
2946
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002947 /* Need to make sure the object gets inactive eventually. */
2948 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002949 if (ret)
2950 goto out;
2951
John Harrison97b2a6a2014-11-24 18:49:26 +00002952 if (!obj->active || !obj->last_read_req)
2953 goto out;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002954
John Harrisonff865882014-11-24 18:49:28 +00002955 req = obj->last_read_req;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002956
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002957 /* Do this after OLR check to make sure we make forward progress polling
Chris Wilson762e4582015-03-04 18:09:26 +00002958 * on this IOCTL with a timeout == 0 (like busy ioctl)
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002959 */
Chris Wilson762e4582015-03-04 18:09:26 +00002960 if (args->timeout_ns == 0) {
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002961 ret = -ETIME;
2962 goto out;
2963 }
2964
2965 drm_gem_object_unreference(&obj->base);
Daniel Vetterf69061b2012-12-06 09:01:42 +01002966 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
John Harrisonff865882014-11-24 18:49:28 +00002967 i915_gem_request_reference(req);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002968 mutex_unlock(&dev->struct_mutex);
2969
Chris Wilson762e4582015-03-04 18:09:26 +00002970 ret = __i915_wait_request(req, reset_counter, true,
2971 args->timeout_ns > 0 ? &args->timeout_ns : NULL,
John Harrison9c654812014-11-24 18:49:35 +00002972 file->driver_priv);
Chris Wilson41037f92015-03-27 11:01:36 +00002973 i915_gem_request_unreference__unlocked(req);
John Harrisonff865882014-11-24 18:49:28 +00002974 return ret;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002975
2976out:
2977 drm_gem_object_unreference(&obj->base);
2978 mutex_unlock(&dev->struct_mutex);
2979 return ret;
2980}
2981
2982/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002983 * i915_gem_object_sync - sync an object to a ring.
2984 *
2985 * @obj: object which may be in use on another ring.
2986 * @to: ring we wish to use the object on. May be NULL.
2987 *
2988 * This code is meant to abstract object synchronization with the GPU.
2989 * Calling with NULL implies synchronizing the object with the CPU
2990 * rather than a particular GPU ring.
2991 *
2992 * Returns 0 if successful, else propagates up the lower layer error.
2993 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002994int
2995i915_gem_object_sync(struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002996 struct intel_engine_cs *to)
Ben Widawsky2911a352012-04-05 14:47:36 -07002997{
John Harrison41c52412014-11-24 18:49:43 +00002998 struct intel_engine_cs *from;
Ben Widawsky2911a352012-04-05 14:47:36 -07002999 u32 seqno;
3000 int ret, idx;
3001
John Harrison41c52412014-11-24 18:49:43 +00003002 from = i915_gem_request_get_ring(obj->last_read_req);
3003
Ben Widawsky2911a352012-04-05 14:47:36 -07003004 if (from == NULL || to == from)
3005 return 0;
3006
Ben Widawsky5816d642012-04-11 11:18:19 -07003007 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
Chris Wilson0201f1e2012-07-20 12:41:01 +01003008 return i915_gem_object_wait_rendering(obj, false);
Ben Widawsky2911a352012-04-05 14:47:36 -07003009
3010 idx = intel_ring_sync_index(from, to);
3011
John Harrison97b2a6a2014-11-24 18:49:26 +00003012 seqno = i915_gem_request_get_seqno(obj->last_read_req);
Rodrigo Vividdd4dbc2014-06-30 09:51:11 -07003013 /* Optimization: Avoid semaphore sync when we are sure we already
3014 * waited for an object with higher seqno */
Ben Widawskyebc348b2014-04-29 14:52:28 -07003015 if (seqno <= from->semaphore.sync_seqno[idx])
Ben Widawsky2911a352012-04-05 14:47:36 -07003016 return 0;
3017
John Harrisonb6660d52014-11-24 18:49:30 +00003018 ret = i915_gem_check_olr(obj->last_read_req);
Ben Widawskyb4aca012012-04-25 20:50:12 -07003019 if (ret)
3020 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07003021
John Harrison74328ee2014-11-24 18:49:38 +00003022 trace_i915_gem_ring_sync_to(from, to, obj->last_read_req);
Ben Widawskyebc348b2014-04-29 14:52:28 -07003023 ret = to->semaphore.sync_to(to, from, seqno);
Ben Widawskye3a5a222012-04-11 11:18:20 -07003024 if (!ret)
John Harrison97b2a6a2014-11-24 18:49:26 +00003025 /* We use last_read_req because sync_to()
Mika Kuoppala7b01e262012-11-28 17:18:45 +02003026 * might have just caused seqno wrap under
3027 * the radar.
3028 */
John Harrison97b2a6a2014-11-24 18:49:26 +00003029 from->semaphore.sync_seqno[idx] =
3030 i915_gem_request_get_seqno(obj->last_read_req);
Ben Widawsky2911a352012-04-05 14:47:36 -07003031
Ben Widawskye3a5a222012-04-11 11:18:20 -07003032 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07003033}
3034
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003035static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
3036{
3037 u32 old_write_domain, old_read_domains;
3038
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003039 /* Force a pagefault for domain tracking on next user access */
3040 i915_gem_release_mmap(obj);
3041
Keith Packardb97c3d92011-06-24 21:02:59 -07003042 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3043 return;
3044
Chris Wilson97c809fd2012-10-09 19:24:38 +01003045 /* Wait for any direct GTT access to complete */
3046 mb();
3047
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01003048 old_read_domains = obj->base.read_domains;
3049 old_write_domain = obj->base.write_domain;
3050
3051 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
3052 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
3053
3054 trace_i915_gem_object_change_domain(obj,
3055 old_read_domains,
3056 old_write_domain);
3057}
3058
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003059int i915_vma_unbind(struct i915_vma *vma)
Eric Anholt673a3942008-07-30 12:06:12 -07003060{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003061 struct drm_i915_gem_object *obj = vma->obj;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003062 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson43e28f02013-01-08 10:53:09 +00003063 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003064
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003065 if (list_empty(&vma->vma_link))
Eric Anholt673a3942008-07-30 12:06:12 -07003066 return 0;
3067
Daniel Vetter0ff501c2013-08-29 19:50:31 +02003068 if (!drm_mm_node_allocated(&vma->node)) {
3069 i915_gem_vma_destroy(vma);
Daniel Vetter0ff501c2013-08-29 19:50:31 +02003070 return 0;
3071 }
Ben Widawsky433544b2013-08-13 18:09:06 -07003072
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003073 if (vma->pin_count)
Chris Wilson31d8d652012-05-24 19:11:20 +01003074 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07003075
Chris Wilsonc4670ad2012-08-20 10:23:27 +01003076 BUG_ON(obj->pages == NULL);
3077
Chris Wilson2e2f3512015-04-27 13:41:14 +01003078 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilson1488fc02012-04-24 15:47:31 +01003079 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003080 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01003081 /* Continue on if we fail due to EIO, the GPU is hung so we
3082 * should be safe and we need to cleanup or else we might
3083 * cause memory corruption through use-after-free.
3084 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01003085
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003086 if (i915_is_ggtt(vma->vm) &&
3087 vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003088 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01003089
Daniel Vetter8b1bc9b2014-02-14 14:06:07 +01003090 /* release the fence reg _after_ flushing */
3091 ret = i915_gem_object_put_fence(obj);
3092 if (ret)
3093 return ret;
3094 }
Daniel Vetter96b47b62009-12-15 17:50:00 +01003095
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003096 trace_i915_vma_unbind(vma);
Chris Wilsondb53a302011-02-03 11:57:46 +00003097
Daniel Vetter777dc5b2015-04-14 17:35:12 +02003098 vma->vm->unbind_vma(vma);
Mika Kuoppala5e562f12015-04-30 11:02:31 +03003099 vma->bound = 0;
Ben Widawsky6f65e292013-12-06 14:10:56 -08003100
Chris Wilson64bf9302014-02-25 14:23:28 +00003101 list_del_init(&vma->mm_list);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003102 if (i915_is_ggtt(vma->vm)) {
3103 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
3104 obj->map_and_fenceable = false;
3105 } else if (vma->ggtt_view.pages) {
3106 sg_free_table(vma->ggtt_view.pages);
3107 kfree(vma->ggtt_view.pages);
3108 vma->ggtt_view.pages = NULL;
3109 }
3110 }
Eric Anholt673a3942008-07-30 12:06:12 -07003111
Ben Widawsky2f633152013-07-17 12:19:03 -07003112 drm_mm_remove_node(&vma->node);
3113 i915_gem_vma_destroy(vma);
3114
3115 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02003116 * no more VMAs exist. */
Armin Reese9490edb2014-07-11 10:20:07 -07003117 if (list_empty(&obj->vma_list)) {
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003118 /* Throw away the active reference before
3119 * moving to the unbound list. */
3120 i915_gem_object_retire(obj);
3121
Armin Reese9490edb2014-07-11 10:20:07 -07003122 i915_gem_gtt_finish_object(obj);
Ben Widawsky2f633152013-07-17 12:19:03 -07003123 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Armin Reese9490edb2014-07-11 10:20:07 -07003124 }
Eric Anholt673a3942008-07-30 12:06:12 -07003125
Chris Wilson70903c32013-12-04 09:59:09 +00003126 /* And finally now the object is completely decoupled from this vma,
3127 * we can drop its hold on the backing storage and allow it to be
3128 * reaped by the shrinker.
3129 */
3130 i915_gem_object_unpin_pages(obj);
3131
Chris Wilson88241782011-01-07 17:09:48 +00003132 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00003133}
3134
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003135int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003136{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003137 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01003138 struct intel_engine_cs *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003139 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003140
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003141 /* Flush everything onto the inactive list. */
Chris Wilsonb4519512012-05-11 14:29:30 +01003142 for_each_ring(ring, dev_priv, i) {
Thomas Danielecdb5fd2014-08-20 16:29:24 +01003143 if (!i915.enable_execlists) {
3144 ret = i915_switch_context(ring, ring->default_context);
3145 if (ret)
3146 return ret;
3147 }
Ben Widawskyb6c74882012-08-14 14:35:14 -07003148
Chris Wilson3e960502012-11-27 16:22:54 +00003149 ret = intel_ring_idle(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00003150 if (ret)
3151 return ret;
3152 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003153
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01003154 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01003155}
3156
Chris Wilson9ce079e2012-04-17 15:31:30 +01003157static void i965_write_fence_reg(struct drm_device *dev, int reg,
3158 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08003159{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003160 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak56c844e2013-01-07 21:47:34 +02003161 int fence_reg;
3162 int fence_pitch_shift;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003163
Imre Deak56c844e2013-01-07 21:47:34 +02003164 if (INTEL_INFO(dev)->gen >= 6) {
3165 fence_reg = FENCE_REG_SANDYBRIDGE_0;
3166 fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
3167 } else {
3168 fence_reg = FENCE_REG_965_0;
3169 fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
3170 }
3171
Chris Wilsond18b9612013-07-10 13:36:23 +01003172 fence_reg += reg * 8;
3173
3174 /* To w/a incoherency with non-atomic 64-bit register updates,
3175 * we split the 64-bit update into two 32-bit writes. In order
3176 * for a partial fence not to be evaluated between writes, we
3177 * precede the update with write to turn off the fence register,
3178 * and only enable the fence as the last step.
3179 *
3180 * For extra levels of paranoia, we make sure each step lands
3181 * before applying the next step.
3182 */
3183 I915_WRITE(fence_reg, 0);
3184 POSTING_READ(fence_reg);
3185
Chris Wilson9ce079e2012-04-17 15:31:30 +01003186 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003187 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilsond18b9612013-07-10 13:36:23 +01003188 uint64_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003189
Bob Paauweaf1a7302014-12-18 09:51:26 -08003190 /* Adjust fence size to match tiled area */
3191 if (obj->tiling_mode != I915_TILING_NONE) {
3192 uint32_t row_size = obj->stride *
3193 (obj->tiling_mode == I915_TILING_Y ? 32 : 8);
3194 size = (size / row_size) * row_size;
3195 }
3196
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003197 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
Chris Wilson9ce079e2012-04-17 15:31:30 +01003198 0xfffff000) << 32;
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003199 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
Imre Deak56c844e2013-01-07 21:47:34 +02003200 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
Chris Wilson9ce079e2012-04-17 15:31:30 +01003201 if (obj->tiling_mode == I915_TILING_Y)
3202 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
3203 val |= I965_FENCE_REG_VALID;
Daniel Vetterc6642782010-11-12 13:46:18 +00003204
Chris Wilsond18b9612013-07-10 13:36:23 +01003205 I915_WRITE(fence_reg + 4, val >> 32);
3206 POSTING_READ(fence_reg + 4);
3207
3208 I915_WRITE(fence_reg + 0, val);
3209 POSTING_READ(fence_reg);
3210 } else {
3211 I915_WRITE(fence_reg + 4, 0);
3212 POSTING_READ(fence_reg + 4);
3213 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08003214}
3215
Chris Wilson9ce079e2012-04-17 15:31:30 +01003216static void i915_write_fence_reg(struct drm_device *dev, int reg,
3217 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08003218{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003219 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9ce079e2012-04-17 15:31:30 +01003220 u32 val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003221
Chris Wilson9ce079e2012-04-17 15:31:30 +01003222 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003223 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01003224 int pitch_val;
3225 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003226
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003227 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
Chris Wilson9ce079e2012-04-17 15:31:30 +01003228 (size & -size) != size ||
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003229 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
3230 "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
3231 i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
Chris Wilson9ce079e2012-04-17 15:31:30 +01003232
3233 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
3234 tile_width = 128;
3235 else
3236 tile_width = 512;
3237
3238 /* Note: pitch better be a power of two tile widths */
3239 pitch_val = obj->stride / tile_width;
3240 pitch_val = ffs(pitch_val) - 1;
3241
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003242 val = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01003243 if (obj->tiling_mode == I915_TILING_Y)
3244 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
3245 val |= I915_FENCE_SIZE_BITS(size);
3246 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
3247 val |= I830_FENCE_REG_VALID;
3248 } else
3249 val = 0;
3250
3251 if (reg < 8)
3252 reg = FENCE_REG_830_0 + reg * 4;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003253 else
Chris Wilson9ce079e2012-04-17 15:31:30 +01003254 reg = FENCE_REG_945_8 + (reg - 8) * 4;
Jesse Barnes0f973f22009-01-26 17:10:45 -08003255
Chris Wilson9ce079e2012-04-17 15:31:30 +01003256 I915_WRITE(reg, val);
3257 POSTING_READ(reg);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003258}
3259
Chris Wilson9ce079e2012-04-17 15:31:30 +01003260static void i830_write_fence_reg(struct drm_device *dev, int reg,
3261 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08003262{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003263 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003264 uint32_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003265
Chris Wilson9ce079e2012-04-17 15:31:30 +01003266 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003267 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01003268 uint32_t pitch_val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003269
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003270 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
Chris Wilson9ce079e2012-04-17 15:31:30 +01003271 (size & -size) != size ||
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003272 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
3273 "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
3274 i915_gem_obj_ggtt_offset(obj), size);
Eric Anholte76a16d2009-05-26 17:44:56 -07003275
Chris Wilson9ce079e2012-04-17 15:31:30 +01003276 pitch_val = obj->stride / 128;
3277 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003278
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003279 val = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01003280 if (obj->tiling_mode == I915_TILING_Y)
3281 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
3282 val |= I830_FENCE_SIZE_BITS(size);
3283 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
3284 val |= I830_FENCE_REG_VALID;
3285 } else
3286 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00003287
Chris Wilson9ce079e2012-04-17 15:31:30 +01003288 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
3289 POSTING_READ(FENCE_REG_830_0 + reg * 4);
3290}
3291
Chris Wilsond0a57782012-10-09 19:24:37 +01003292inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
3293{
3294 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
3295}
3296
Chris Wilson9ce079e2012-04-17 15:31:30 +01003297static void i915_gem_write_fence(struct drm_device *dev, int reg,
3298 struct drm_i915_gem_object *obj)
3299{
Chris Wilsond0a57782012-10-09 19:24:37 +01003300 struct drm_i915_private *dev_priv = dev->dev_private;
3301
3302 /* Ensure that all CPU reads are completed before installing a fence
3303 * and all writes before removing the fence.
3304 */
3305 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
3306 mb();
3307
Daniel Vetter94a335d2013-07-17 14:51:28 +02003308 WARN(obj && (!obj->stride || !obj->tiling_mode),
3309 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
3310 obj->stride, obj->tiling_mode);
3311
Rodrigo Vivice38ab02014-12-04 06:48:10 -08003312 if (IS_GEN2(dev))
3313 i830_write_fence_reg(dev, reg, obj);
3314 else if (IS_GEN3(dev))
3315 i915_write_fence_reg(dev, reg, obj);
3316 else if (INTEL_INFO(dev)->gen >= 4)
3317 i965_write_fence_reg(dev, reg, obj);
Chris Wilsond0a57782012-10-09 19:24:37 +01003318
3319 /* And similarly be paranoid that no direct access to this region
3320 * is reordered to before the fence is installed.
3321 */
3322 if (i915_gem_object_needs_mb(obj))
3323 mb();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003324}
3325
Chris Wilson61050802012-04-17 15:31:31 +01003326static inline int fence_number(struct drm_i915_private *dev_priv,
3327 struct drm_i915_fence_reg *fence)
3328{
3329 return fence - dev_priv->fence_regs;
3330}
3331
3332static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
3333 struct drm_i915_fence_reg *fence,
3334 bool enable)
3335{
Chris Wilson2dc8aae2013-05-22 17:08:06 +01003336 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson46a0b632013-07-10 13:36:24 +01003337 int reg = fence_number(dev_priv, fence);
Chris Wilson61050802012-04-17 15:31:31 +01003338
Chris Wilson46a0b632013-07-10 13:36:24 +01003339 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
Chris Wilson61050802012-04-17 15:31:31 +01003340
3341 if (enable) {
Chris Wilson46a0b632013-07-10 13:36:24 +01003342 obj->fence_reg = reg;
Chris Wilson61050802012-04-17 15:31:31 +01003343 fence->obj = obj;
3344 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
3345 } else {
3346 obj->fence_reg = I915_FENCE_REG_NONE;
3347 fence->obj = NULL;
3348 list_del_init(&fence->lru_list);
3349 }
Daniel Vetter94a335d2013-07-17 14:51:28 +02003350 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +01003351}
3352
Chris Wilsond9e86c02010-11-10 16:40:20 +00003353static int
Chris Wilsond0a57782012-10-09 19:24:37 +01003354i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00003355{
John Harrison97b2a6a2014-11-24 18:49:26 +00003356 if (obj->last_fenced_req) {
Daniel Vettera4b3a572014-11-26 14:17:05 +01003357 int ret = i915_wait_request(obj->last_fenced_req);
Chris Wilson18991842012-04-17 15:31:29 +01003358 if (ret)
3359 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003360
John Harrison97b2a6a2014-11-24 18:49:26 +00003361 i915_gem_request_assign(&obj->last_fenced_req, NULL);
Chris Wilsond9e86c02010-11-10 16:40:20 +00003362 }
3363
3364 return 0;
3365}
3366
3367int
3368i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
3369{
Chris Wilson61050802012-04-17 15:31:31 +01003370 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsonf9c513e2013-03-26 11:29:27 +00003371 struct drm_i915_fence_reg *fence;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003372 int ret;
3373
Chris Wilsond0a57782012-10-09 19:24:37 +01003374 ret = i915_gem_object_wait_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00003375 if (ret)
3376 return ret;
3377
Chris Wilson61050802012-04-17 15:31:31 +01003378 if (obj->fence_reg == I915_FENCE_REG_NONE)
3379 return 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01003380
Chris Wilsonf9c513e2013-03-26 11:29:27 +00003381 fence = &dev_priv->fence_regs[obj->fence_reg];
3382
Daniel Vetteraff10b302014-02-14 14:06:05 +01003383 if (WARN_ON(fence->pin_count))
3384 return -EBUSY;
3385
Chris Wilson61050802012-04-17 15:31:31 +01003386 i915_gem_object_fence_lost(obj);
Chris Wilsonf9c513e2013-03-26 11:29:27 +00003387 i915_gem_object_update_fence(obj, fence, false);
Chris Wilsond9e86c02010-11-10 16:40:20 +00003388
3389 return 0;
3390}
3391
3392static struct drm_i915_fence_reg *
Chris Wilsona360bb12012-04-17 15:31:25 +01003393i915_find_fence_reg(struct drm_device *dev)
Daniel Vetterae3db242010-02-19 11:51:58 +01003394{
Daniel Vetterae3db242010-02-19 11:51:58 +01003395 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8fe301a2012-04-17 15:31:28 +01003396 struct drm_i915_fence_reg *reg, *avail;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003397 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01003398
3399 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00003400 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01003401 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
3402 reg = &dev_priv->fence_regs[i];
3403 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00003404 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003405
Chris Wilson1690e1e2011-12-14 13:57:08 +01003406 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00003407 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003408 }
3409
Chris Wilsond9e86c02010-11-10 16:40:20 +00003410 if (avail == NULL)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003411 goto deadlock;
Daniel Vetterae3db242010-02-19 11:51:58 +01003412
3413 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00003414 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01003415 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01003416 continue;
3417
Chris Wilson8fe301a2012-04-17 15:31:28 +01003418 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003419 }
3420
Chris Wilson5dce5b932014-01-20 10:17:36 +00003421deadlock:
3422 /* Wait for completion of pending flips which consume fences */
3423 if (intel_has_pending_fb_unpin(dev))
3424 return ERR_PTR(-EAGAIN);
3425
3426 return ERR_PTR(-EDEADLK);
Daniel Vetterae3db242010-02-19 11:51:58 +01003427}
3428
Jesse Barnesde151cf2008-11-12 10:03:55 -08003429/**
Chris Wilson9a5a53b2012-03-22 15:10:00 +00003430 * i915_gem_object_get_fence - set up fencing for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08003431 * @obj: object to map through a fence reg
3432 *
3433 * When mapping objects through the GTT, userspace wants to be able to write
3434 * to them without having to worry about swizzling if the object is tiled.
Jesse Barnesde151cf2008-11-12 10:03:55 -08003435 * This function walks the fence regs looking for a free one for @obj,
3436 * stealing one if it can't find any.
3437 *
3438 * It then sets up the reg based on the object's properties: address, pitch
3439 * and tiling format.
Chris Wilson9a5a53b2012-03-22 15:10:00 +00003440 *
3441 * For an untiled surface, this removes any existing fence.
Jesse Barnesde151cf2008-11-12 10:03:55 -08003442 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003443int
Chris Wilson06d98132012-04-17 15:31:24 +01003444i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08003445{
Chris Wilson05394f32010-11-08 19:18:58 +00003446 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003447 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson14415742012-04-17 15:31:33 +01003448 bool enable = obj->tiling_mode != I915_TILING_NONE;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003449 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003450 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003451
Chris Wilson14415742012-04-17 15:31:33 +01003452 /* Have we updated the tiling parameters upon the object and so
3453 * will need to serialise the write to the associated fence register?
3454 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +01003455 if (obj->fence_dirty) {
Chris Wilsond0a57782012-10-09 19:24:37 +01003456 ret = i915_gem_object_wait_fence(obj);
Chris Wilson14415742012-04-17 15:31:33 +01003457 if (ret)
3458 return ret;
3459 }
Chris Wilson9a5a53b2012-03-22 15:10:00 +00003460
Chris Wilsond9e86c02010-11-10 16:40:20 +00003461 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00003462 if (obj->fence_reg != I915_FENCE_REG_NONE) {
3463 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson5d82e3e2012-04-21 16:23:23 +01003464 if (!obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01003465 list_move_tail(&reg->lru_list,
3466 &dev_priv->mm.fence_list);
3467 return 0;
3468 }
3469 } else if (enable) {
Chris Wilsone6a84462014-08-11 12:00:12 +02003470 if (WARN_ON(!obj->map_and_fenceable))
3471 return -EINVAL;
3472
Chris Wilson14415742012-04-17 15:31:33 +01003473 reg = i915_find_fence_reg(dev);
Chris Wilson5dce5b932014-01-20 10:17:36 +00003474 if (IS_ERR(reg))
3475 return PTR_ERR(reg);
Chris Wilsond9e86c02010-11-10 16:40:20 +00003476
Chris Wilson14415742012-04-17 15:31:33 +01003477 if (reg->obj) {
3478 struct drm_i915_gem_object *old = reg->obj;
3479
Chris Wilsond0a57782012-10-09 19:24:37 +01003480 ret = i915_gem_object_wait_fence(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00003481 if (ret)
3482 return ret;
3483
Chris Wilson14415742012-04-17 15:31:33 +01003484 i915_gem_object_fence_lost(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00003485 }
Chris Wilson14415742012-04-17 15:31:33 +01003486 } else
Eric Anholta09ba7f2009-08-29 12:49:51 -07003487 return 0;
Eric Anholta09ba7f2009-08-29 12:49:51 -07003488
Chris Wilson14415742012-04-17 15:31:33 +01003489 i915_gem_object_update_fence(obj, reg, enable);
Chris Wilson14415742012-04-17 15:31:33 +01003490
Chris Wilson9ce079e2012-04-17 15:31:30 +01003491 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003492}
3493
Chris Wilson4144f9b2014-09-11 08:43:48 +01003494static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003495 unsigned long cache_level)
3496{
Chris Wilson4144f9b2014-09-11 08:43:48 +01003497 struct drm_mm_node *gtt_space = &vma->node;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003498 struct drm_mm_node *other;
3499
Chris Wilson4144f9b2014-09-11 08:43:48 +01003500 /*
3501 * On some machines we have to be careful when putting differing types
3502 * of snoopable memory together to avoid the prefetcher crossing memory
3503 * domains and dying. During vm initialisation, we decide whether or not
3504 * these constraints apply and set the drm_mm.color_adjust
3505 * appropriately.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003506 */
Chris Wilson4144f9b2014-09-11 08:43:48 +01003507 if (vma->vm->mm.color_adjust == NULL)
Chris Wilson42d6ab42012-07-26 11:49:32 +01003508 return true;
3509
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003510 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003511 return true;
3512
3513 if (list_empty(&gtt_space->node_list))
3514 return true;
3515
3516 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3517 if (other->allocated && !other->hole_follows && other->color != cache_level)
3518 return false;
3519
3520 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3521 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3522 return false;
3523
3524 return true;
3525}
3526
Jesse Barnesde151cf2008-11-12 10:03:55 -08003527/**
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003528 * Finds free space in the GTT aperture and binds the object or a view of it
3529 * there.
Eric Anholt673a3942008-07-30 12:06:12 -07003530 */
Daniel Vetter262de142014-02-14 14:01:20 +01003531static struct i915_vma *
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003532i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3533 struct i915_address_space *vm,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003534 const struct i915_ggtt_view *ggtt_view,
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003535 unsigned alignment,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003536 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003537{
Chris Wilson05394f32010-11-08 19:18:58 +00003538 struct drm_device *dev = obj->base.dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003539 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter5e783302010-11-14 22:32:36 +01003540 u32 size, fence_size, fence_alignment, unfenced_alignment;
Chris Wilsond23db882014-05-23 08:48:08 +02003541 unsigned long start =
3542 flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
3543 unsigned long end =
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003544 flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total;
Ben Widawsky2f633152013-07-17 12:19:03 -07003545 struct i915_vma *vma;
Chris Wilson07f73f62009-09-14 16:50:30 +01003546 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003547
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003548 if (i915_is_ggtt(vm)) {
3549 u32 view_size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003550
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003551 if (WARN_ON(!ggtt_view))
3552 return ERR_PTR(-EINVAL);
3553
3554 view_size = i915_ggtt_view_size(obj, ggtt_view);
3555
3556 fence_size = i915_gem_get_gtt_size(dev,
3557 view_size,
3558 obj->tiling_mode);
3559 fence_alignment = i915_gem_get_gtt_alignment(dev,
3560 view_size,
3561 obj->tiling_mode,
3562 true);
3563 unfenced_alignment = i915_gem_get_gtt_alignment(dev,
3564 view_size,
3565 obj->tiling_mode,
3566 false);
3567 size = flags & PIN_MAPPABLE ? fence_size : view_size;
3568 } else {
3569 fence_size = i915_gem_get_gtt_size(dev,
3570 obj->base.size,
3571 obj->tiling_mode);
3572 fence_alignment = i915_gem_get_gtt_alignment(dev,
3573 obj->base.size,
3574 obj->tiling_mode,
3575 true);
3576 unfenced_alignment =
3577 i915_gem_get_gtt_alignment(dev,
3578 obj->base.size,
3579 obj->tiling_mode,
3580 false);
3581 size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
3582 }
Chris Wilsona00b10c2010-09-24 21:15:47 +01003583
Eric Anholt673a3942008-07-30 12:06:12 -07003584 if (alignment == 0)
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003585 alignment = flags & PIN_MAPPABLE ? fence_alignment :
Daniel Vetter5e783302010-11-14 22:32:36 +01003586 unfenced_alignment;
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003587 if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003588 DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
3589 ggtt_view ? ggtt_view->type : 0,
3590 alignment);
Daniel Vetter262de142014-02-14 14:01:20 +01003591 return ERR_PTR(-EINVAL);
Eric Anholt673a3942008-07-30 12:06:12 -07003592 }
3593
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003594 /* If binding the object/GGTT view requires more space than the entire
3595 * aperture has, reject it early before evicting everything in a vain
3596 * attempt to find space.
Chris Wilson654fc602010-05-27 13:18:21 +01003597 */
Joonas Lahtinen91e67112015-05-06 14:33:58 +03003598 if (size > end) {
3599 DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%u > %s aperture=%lu\n",
3600 ggtt_view ? ggtt_view->type : 0,
3601 size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003602 flags & PIN_MAPPABLE ? "mappable" : "total",
Chris Wilsond23db882014-05-23 08:48:08 +02003603 end);
Daniel Vetter262de142014-02-14 14:01:20 +01003604 return ERR_PTR(-E2BIG);
Chris Wilson654fc602010-05-27 13:18:21 +01003605 }
3606
Chris Wilson37e680a2012-06-07 15:38:42 +01003607 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003608 if (ret)
Daniel Vetter262de142014-02-14 14:01:20 +01003609 return ERR_PTR(ret);
Chris Wilson6c085a72012-08-20 11:40:46 +02003610
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003611 i915_gem_object_pin_pages(obj);
3612
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02003613 vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
3614 i915_gem_obj_lookup_or_create_vma(obj, vm);
3615
Daniel Vetter262de142014-02-14 14:01:20 +01003616 if (IS_ERR(vma))
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003617 goto err_unpin;
Ben Widawsky2f633152013-07-17 12:19:03 -07003618
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003619search_free:
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003620 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003621 size, alignment,
Chris Wilsond23db882014-05-23 08:48:08 +02003622 obj->cache_level,
3623 start, end,
Lauri Kasanen62347f92014-04-02 20:03:57 +03003624 DRM_MM_SEARCH_DEFAULT,
3625 DRM_MM_CREATE_DEFAULT);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003626 if (ret) {
Ben Widawskyf6cd1f12013-07-31 17:00:11 -07003627 ret = i915_gem_evict_something(dev, vm, size, alignment,
Chris Wilsond23db882014-05-23 08:48:08 +02003628 obj->cache_level,
3629 start, end,
3630 flags);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003631 if (ret == 0)
3632 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003633
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003634 goto err_free_vma;
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003635 }
Chris Wilson4144f9b2014-09-11 08:43:48 +01003636 if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
Ben Widawsky2f633152013-07-17 12:19:03 -07003637 ret = -EINVAL;
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003638 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003639 }
3640
Daniel Vetter74163902012-02-15 23:50:21 +01003641 ret = i915_gem_gtt_prepare_object(obj);
Ben Widawsky2f633152013-07-17 12:19:03 -07003642 if (ret)
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003643 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003644
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003645 trace_i915_vma_bind(vma, flags);
Daniel Vetter08755462015-04-20 09:04:05 -07003646 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003647 if (ret)
3648 goto err_finish_gtt;
3649
Ben Widawsky35c20a62013-05-31 11:28:48 -07003650 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Ben Widawskyca191b12013-07-31 17:00:14 -07003651 list_add_tail(&vma->mm_list, &vm->inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003652
Daniel Vetter262de142014-02-14 14:01:20 +01003653 return vma;
Ben Widawsky2f633152013-07-17 12:19:03 -07003654
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003655err_finish_gtt:
3656 i915_gem_gtt_finish_object(obj);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003657err_remove_node:
Dan Carpenter6286ef92013-07-19 08:46:27 +03003658 drm_mm_remove_node(&vma->node);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003659err_free_vma:
Ben Widawsky2f633152013-07-17 12:19:03 -07003660 i915_gem_vma_destroy(vma);
Daniel Vetter262de142014-02-14 14:01:20 +01003661 vma = ERR_PTR(ret);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003662err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003663 i915_gem_object_unpin_pages(obj);
Daniel Vetter262de142014-02-14 14:01:20 +01003664 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003665}
3666
Chris Wilson000433b2013-08-08 14:41:09 +01003667bool
Chris Wilson2c225692013-08-09 12:26:45 +01003668i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3669 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003670{
Eric Anholt673a3942008-07-30 12:06:12 -07003671 /* If we don't have a page list set up, then we're not pinned
3672 * to GPU, and we can ignore the cache flush because it'll happen
3673 * again at bind time.
3674 */
Chris Wilson05394f32010-11-08 19:18:58 +00003675 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003676 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003677
Imre Deak769ce462013-02-13 21:56:05 +02003678 /*
3679 * Stolen memory is always coherent with the GPU as it is explicitly
3680 * marked as wc by the system, or the system is cache-coherent.
3681 */
Chris Wilson6a2c4232014-11-04 04:51:40 -08003682 if (obj->stolen || obj->phys_handle)
Chris Wilson000433b2013-08-08 14:41:09 +01003683 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003684
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003685 /* If the GPU is snooping the contents of the CPU cache,
3686 * we do not need to manually clear the CPU cache lines. However,
3687 * the caches are only snooped when the render cache is
3688 * flushed/invalidated. As we always have to emit invalidations
3689 * and flushes when moving into and out of the RENDER domain, correct
3690 * snooping behaviour occurs naturally as the result of our domain
3691 * tracking.
3692 */
Chris Wilson0f719792015-01-13 13:32:52 +00003693 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
3694 obj->cache_dirty = true;
Chris Wilson000433b2013-08-08 14:41:09 +01003695 return false;
Chris Wilson0f719792015-01-13 13:32:52 +00003696 }
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003697
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003698 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003699 drm_clflush_sg(obj->pages);
Chris Wilson0f719792015-01-13 13:32:52 +00003700 obj->cache_dirty = false;
Chris Wilson000433b2013-08-08 14:41:09 +01003701
3702 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003703}
3704
3705/** Flushes the GTT write domain for the object if it's dirty. */
3706static void
Chris Wilson05394f32010-11-08 19:18:58 +00003707i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003708{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003709 uint32_t old_write_domain;
3710
Chris Wilson05394f32010-11-08 19:18:58 +00003711 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003712 return;
3713
Chris Wilson63256ec2011-01-04 18:42:07 +00003714 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003715 * to it immediately go to main memory as far as we know, so there's
3716 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003717 *
3718 * However, we do have to enforce the order so that all writes through
3719 * the GTT land before any writes to the device, such as updates to
3720 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003721 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003722 wmb();
3723
Chris Wilson05394f32010-11-08 19:18:58 +00003724 old_write_domain = obj->base.write_domain;
3725 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003726
Daniel Vetterf99d7062014-06-19 16:01:59 +02003727 intel_fb_obj_flush(obj, false);
3728
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003729 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003730 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003731 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003732}
3733
3734/** Flushes the CPU write domain for the object if it's dirty. */
3735static void
Daniel Vettere62b59e2015-01-21 14:53:48 +01003736i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003737{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003738 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003739
Chris Wilson05394f32010-11-08 19:18:58 +00003740 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003741 return;
3742
Daniel Vettere62b59e2015-01-21 14:53:48 +01003743 if (i915_gem_clflush_object(obj, obj->pin_display))
Chris Wilson000433b2013-08-08 14:41:09 +01003744 i915_gem_chipset_flush(obj->base.dev);
3745
Chris Wilson05394f32010-11-08 19:18:58 +00003746 old_write_domain = obj->base.write_domain;
3747 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003748
Daniel Vetterf99d7062014-06-19 16:01:59 +02003749 intel_fb_obj_flush(obj, false);
3750
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003751 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003752 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003753 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003754}
3755
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003756/**
3757 * Moves a single object to the GTT read, and possibly write domain.
3758 *
3759 * This function returns when the move is complete, including waiting on
3760 * flushes to occur.
3761 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003762int
Chris Wilson20217462010-11-23 15:26:33 +00003763i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003764{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003765 uint32_t old_write_domain, old_read_domains;
Chris Wilson43566de2015-01-02 16:29:29 +05303766 struct i915_vma *vma;
Eric Anholte47c68e2008-11-14 13:35:19 -08003767 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003768
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003769 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3770 return 0;
3771
Chris Wilson0201f1e2012-07-20 12:41:01 +01003772 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003773 if (ret)
3774 return ret;
3775
Chris Wilsonc8725f32014-03-17 12:21:55 +00003776 i915_gem_object_retire(obj);
Chris Wilson43566de2015-01-02 16:29:29 +05303777
3778 /* Flush and acquire obj->pages so that we are coherent through
3779 * direct access in memory with previous cached writes through
3780 * shmemfs and that our cache domain tracking remains valid.
3781 * For example, if the obj->filp was moved to swap without us
3782 * being notified and releasing the pages, we would mistakenly
3783 * continue to assume that the obj remained out of the CPU cached
3784 * domain.
3785 */
3786 ret = i915_gem_object_get_pages(obj);
3787 if (ret)
3788 return ret;
3789
Daniel Vettere62b59e2015-01-21 14:53:48 +01003790 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003791
Chris Wilsond0a57782012-10-09 19:24:37 +01003792 /* Serialise direct access to this object with the barriers for
3793 * coherent writes from the GPU, by effectively invalidating the
3794 * GTT domain upon first access.
3795 */
3796 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3797 mb();
3798
Chris Wilson05394f32010-11-08 19:18:58 +00003799 old_write_domain = obj->base.write_domain;
3800 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003801
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003802 /* It should now be out of any other write domains, and we can update
3803 * the domain values for our changes.
3804 */
Chris Wilson05394f32010-11-08 19:18:58 +00003805 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3806 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003807 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003808 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3809 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3810 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003811 }
3812
Daniel Vetterf99d7062014-06-19 16:01:59 +02003813 if (write)
Paulo Zanonia4001f12015-02-13 17:23:44 -02003814 intel_fb_obj_invalidate(obj, NULL, ORIGIN_GTT);
Daniel Vetterf99d7062014-06-19 16:01:59 +02003815
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003816 trace_i915_gem_object_change_domain(obj,
3817 old_read_domains,
3818 old_write_domain);
3819
Chris Wilson8325a092012-04-24 15:52:35 +01003820 /* And bump the LRU for this access */
Chris Wilson43566de2015-01-02 16:29:29 +05303821 vma = i915_gem_obj_to_ggtt(obj);
3822 if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
Chris Wilsondc8cd1e2014-08-09 17:37:22 +01003823 list_move_tail(&vma->mm_list,
Chris Wilson43566de2015-01-02 16:29:29 +05303824 &to_i915(obj->base.dev)->gtt.base.inactive_list);
Chris Wilson8325a092012-04-24 15:52:35 +01003825
Eric Anholte47c68e2008-11-14 13:35:19 -08003826 return 0;
3827}
3828
Chris Wilsone4ffd172011-04-04 09:44:39 +01003829int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3830 enum i915_cache_level cache_level)
3831{
Daniel Vetter7bddb012012-02-09 17:15:47 +01003832 struct drm_device *dev = obj->base.dev;
Chris Wilsondf6f7832014-03-21 07:40:56 +00003833 struct i915_vma *vma, *next;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003834 int ret;
3835
3836 if (obj->cache_level == cache_level)
3837 return 0;
3838
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003839 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003840 DRM_DEBUG("can not change the cache level of pinned objects\n");
3841 return -EBUSY;
3842 }
3843
Chris Wilsondf6f7832014-03-21 07:40:56 +00003844 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
Chris Wilson4144f9b2014-09-11 08:43:48 +01003845 if (!i915_gem_valid_gtt_space(vma, cache_level)) {
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003846 ret = i915_vma_unbind(vma);
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003847 if (ret)
3848 return ret;
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003849 }
Chris Wilson42d6ab42012-07-26 11:49:32 +01003850 }
3851
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003852 if (i915_gem_obj_bound_any(obj)) {
Chris Wilson2e2f3512015-04-27 13:41:14 +01003853 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003854 if (ret)
3855 return ret;
3856
3857 i915_gem_object_finish_gtt(obj);
3858
3859 /* Before SandyBridge, you could not use tiling or fence
3860 * registers with snooped memory, so relinquish any fences
3861 * currently pointing to our region in the aperture.
3862 */
Chris Wilson42d6ab42012-07-26 11:49:32 +01003863 if (INTEL_INFO(dev)->gen < 6) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003864 ret = i915_gem_object_put_fence(obj);
3865 if (ret)
3866 return ret;
3867 }
3868
Ben Widawsky6f65e292013-12-06 14:10:56 -08003869 list_for_each_entry(vma, &obj->vma_list, vma_link)
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003870 if (drm_mm_node_allocated(&vma->node)) {
3871 ret = i915_vma_bind(vma, cache_level,
Daniel Vetter08755462015-04-20 09:04:05 -07003872 PIN_UPDATE);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00003873 if (ret)
3874 return ret;
3875 }
Chris Wilsone4ffd172011-04-04 09:44:39 +01003876 }
3877
Chris Wilson2c225692013-08-09 12:26:45 +01003878 list_for_each_entry(vma, &obj->vma_list, vma_link)
3879 vma->node.color = cache_level;
3880 obj->cache_level = cache_level;
3881
Chris Wilson0f719792015-01-13 13:32:52 +00003882 if (obj->cache_dirty &&
3883 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
3884 cpu_write_needs_clflush(obj)) {
3885 if (i915_gem_clflush_object(obj, true))
3886 i915_gem_chipset_flush(obj->base.dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003887 }
3888
Chris Wilsone4ffd172011-04-04 09:44:39 +01003889 return 0;
3890}
3891
Ben Widawsky199adf42012-09-21 17:01:20 -07003892int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3893 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003894{
Ben Widawsky199adf42012-09-21 17:01:20 -07003895 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003896 struct drm_i915_gem_object *obj;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003897
3898 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilson432be692015-05-07 12:14:55 +01003899 if (&obj->base == NULL)
3900 return -ENOENT;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003901
Chris Wilson651d7942013-08-08 14:41:10 +01003902 switch (obj->cache_level) {
3903 case I915_CACHE_LLC:
3904 case I915_CACHE_L3_LLC:
3905 args->caching = I915_CACHING_CACHED;
3906 break;
3907
Chris Wilson4257d3b2013-08-08 14:41:11 +01003908 case I915_CACHE_WT:
3909 args->caching = I915_CACHING_DISPLAY;
3910 break;
3911
Chris Wilson651d7942013-08-08 14:41:10 +01003912 default:
3913 args->caching = I915_CACHING_NONE;
3914 break;
3915 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003916
Chris Wilson432be692015-05-07 12:14:55 +01003917 drm_gem_object_unreference_unlocked(&obj->base);
3918 return 0;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003919}
3920
Ben Widawsky199adf42012-09-21 17:01:20 -07003921int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3922 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003923{
Ben Widawsky199adf42012-09-21 17:01:20 -07003924 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003925 struct drm_i915_gem_object *obj;
3926 enum i915_cache_level level;
3927 int ret;
3928
Ben Widawsky199adf42012-09-21 17:01:20 -07003929 switch (args->caching) {
3930 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003931 level = I915_CACHE_NONE;
3932 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003933 case I915_CACHING_CACHED:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003934 level = I915_CACHE_LLC;
3935 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003936 case I915_CACHING_DISPLAY:
3937 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
3938 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003939 default:
3940 return -EINVAL;
3941 }
3942
Ben Widawsky3bc29132012-09-26 16:15:20 -07003943 ret = i915_mutex_lock_interruptible(dev);
3944 if (ret)
3945 return ret;
3946
Chris Wilsone6994ae2012-07-10 10:27:08 +01003947 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3948 if (&obj->base == NULL) {
3949 ret = -ENOENT;
3950 goto unlock;
3951 }
3952
3953 ret = i915_gem_object_set_cache_level(obj, level);
3954
3955 drm_gem_object_unreference(&obj->base);
3956unlock:
3957 mutex_unlock(&dev->struct_mutex);
3958 return ret;
3959}
3960
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003961/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003962 * Prepare buffer for display plane (scanout, cursors, etc).
3963 * Can be called from an uninterruptible phase (modesetting) and allows
3964 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003965 */
3966int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003967i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3968 u32 alignment,
Tvrtko Ursuline6617332015-03-23 11:10:33 +00003969 struct intel_engine_cs *pipelined,
3970 const struct i915_ggtt_view *view)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003971{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003972 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003973 int ret;
3974
John Harrison41c52412014-11-24 18:49:43 +00003975 if (pipelined != i915_gem_request_get_ring(obj->last_read_req)) {
Ben Widawsky2911a352012-04-05 14:47:36 -07003976 ret = i915_gem_object_sync(obj, pipelined);
3977 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003978 return ret;
3979 }
3980
Chris Wilsoncc98b412013-08-09 12:25:09 +01003981 /* Mark the pin_display early so that we account for the
3982 * display coherency whilst setting up the cache domains.
3983 */
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01003984 obj->pin_display++;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003985
Eric Anholta7ef0642011-03-29 16:59:54 -07003986 /* The display engine is not coherent with the LLC cache on gen6. As
3987 * a result, we make sure that the pinning that is about to occur is
3988 * done with uncached PTEs. This is lowest common denominator for all
3989 * chipsets.
3990 *
3991 * However for gen6+, we could do better by using the GFDT bit instead
3992 * of uncaching, which would allow us to flush all the LLC-cached data
3993 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3994 */
Chris Wilson651d7942013-08-08 14:41:10 +01003995 ret = i915_gem_object_set_cache_level(obj,
3996 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
Eric Anholta7ef0642011-03-29 16:59:54 -07003997 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003998 goto err_unpin_display;
Eric Anholta7ef0642011-03-29 16:59:54 -07003999
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004000 /* As the user may map the buffer once pinned in the display plane
4001 * (e.g. libkms for the bootup splash), we have to ensure that we
4002 * always use map_and_fenceable for all scanout buffers.
4003 */
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00004004 ret = i915_gem_object_ggtt_pin(obj, view, alignment,
4005 view->type == I915_GGTT_VIEW_NORMAL ?
4006 PIN_MAPPABLE : 0);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004007 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004008 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004009
Daniel Vettere62b59e2015-01-21 14:53:48 +01004010 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01004011
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004012 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00004013 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004014
4015 /* It should now be out of any other write domains, and we can update
4016 * the domain values for our changes.
4017 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01004018 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00004019 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004020
4021 trace_i915_gem_object_change_domain(obj,
4022 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01004023 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004024
4025 return 0;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004026
4027err_unpin_display:
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004028 obj->pin_display--;
Chris Wilsoncc98b412013-08-09 12:25:09 +01004029 return ret;
4030}
4031
4032void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004033i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
4034 const struct i915_ggtt_view *view)
Chris Wilsoncc98b412013-08-09 12:25:09 +01004035{
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004036 if (WARN_ON(obj->pin_display == 0))
4037 return;
4038
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004039 i915_gem_object_ggtt_unpin_view(obj, view);
4040
Tvrtko Ursulin8a0c39b2015-04-13 11:50:09 +01004041 obj->pin_display--;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08004042}
4043
Eric Anholte47c68e2008-11-14 13:35:19 -08004044/**
4045 * Moves a single object to the CPU read, and possibly write domain.
4046 *
4047 * This function returns when the move is complete, including waiting on
4048 * flushes to occur.
4049 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02004050int
Chris Wilson919926a2010-11-12 13:42:53 +00004051i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08004052{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004053 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08004054 int ret;
4055
Chris Wilson8d7e3de2011-02-07 15:23:02 +00004056 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
4057 return 0;
4058
Chris Wilson0201f1e2012-07-20 12:41:01 +01004059 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00004060 if (ret)
4061 return ret;
4062
Chris Wilsonc8725f32014-03-17 12:21:55 +00004063 i915_gem_object_retire(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08004064 i915_gem_object_flush_gtt_write_domain(obj);
4065
Chris Wilson05394f32010-11-08 19:18:58 +00004066 old_write_domain = obj->base.write_domain;
4067 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004068
Eric Anholte47c68e2008-11-14 13:35:19 -08004069 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00004070 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01004071 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08004072
Chris Wilson05394f32010-11-08 19:18:58 +00004073 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004074 }
4075
4076 /* It should now be out of any other write domains, and we can update
4077 * the domain values for our changes.
4078 */
Chris Wilson05394f32010-11-08 19:18:58 +00004079 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08004080
4081 /* If we're writing through the CPU, then the GPU read domains will
4082 * need to be invalidated at next use.
4083 */
4084 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00004085 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4086 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08004087 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004088
Daniel Vetterf99d7062014-06-19 16:01:59 +02004089 if (write)
Paulo Zanonia4001f12015-02-13 17:23:44 -02004090 intel_fb_obj_invalidate(obj, NULL, ORIGIN_CPU);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004091
Chris Wilson1c5d22f2009-08-25 11:15:50 +01004092 trace_i915_gem_object_change_domain(obj,
4093 old_read_domains,
4094 old_write_domain);
4095
Eric Anholt2ef7eea2008-11-10 10:53:25 -08004096 return 0;
4097}
4098
Eric Anholt673a3942008-07-30 12:06:12 -07004099/* Throttle our rendering by waiting until the ring has completed our requests
4100 * emitted over 20 msec ago.
4101 *
Eric Anholtb9624422009-06-03 07:27:35 +00004102 * Note that if we were to use the current jiffies each time around the loop,
4103 * we wouldn't escape the function with any frames outstanding if the time to
4104 * render a frame was over 20ms.
4105 *
Eric Anholt673a3942008-07-30 12:06:12 -07004106 * This should get us reasonable parallelism between CPU and GPU but also
4107 * relatively low latency when blocking on a particular request to finish.
4108 */
4109static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004110i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004111{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004112 struct drm_i915_private *dev_priv = dev->dev_private;
4113 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004114 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
John Harrison54fb2412014-11-24 18:49:27 +00004115 struct drm_i915_gem_request *request, *target = NULL;
Daniel Vetterf69061b2012-12-06 09:01:42 +01004116 unsigned reset_counter;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004117 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004118
Daniel Vetter308887a2012-11-14 17:14:06 +01004119 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
4120 if (ret)
4121 return ret;
4122
4123 ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
4124 if (ret)
4125 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00004126
Chris Wilson1c255952010-09-26 11:03:27 +01004127 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004128 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00004129 if (time_after_eq(request->emitted_jiffies, recent_enough))
4130 break;
4131
John Harrison54fb2412014-11-24 18:49:27 +00004132 target = request;
Eric Anholtb9624422009-06-03 07:27:35 +00004133 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01004134 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
John Harrisonff865882014-11-24 18:49:28 +00004135 if (target)
4136 i915_gem_request_reference(target);
Chris Wilson1c255952010-09-26 11:03:27 +01004137 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004138
John Harrison54fb2412014-11-24 18:49:27 +00004139 if (target == NULL)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004140 return 0;
4141
John Harrison9c654812014-11-24 18:49:35 +00004142 ret = __i915_wait_request(target, reset_counter, true, NULL, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004143 if (ret == 0)
4144 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00004145
Chris Wilson41037f92015-03-27 11:01:36 +00004146 i915_gem_request_unreference__unlocked(target);
John Harrisonff865882014-11-24 18:49:28 +00004147
Eric Anholt673a3942008-07-30 12:06:12 -07004148 return ret;
4149}
4150
Chris Wilsond23db882014-05-23 08:48:08 +02004151static bool
4152i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
4153{
4154 struct drm_i915_gem_object *obj = vma->obj;
4155
4156 if (alignment &&
4157 vma->node.start & (alignment - 1))
4158 return true;
4159
4160 if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
4161 return true;
4162
4163 if (flags & PIN_OFFSET_BIAS &&
4164 vma->node.start < (flags & PIN_OFFSET_MASK))
4165 return true;
4166
4167 return false;
4168}
4169
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004170static int
4171i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
4172 struct i915_address_space *vm,
4173 const struct i915_ggtt_view *ggtt_view,
4174 uint32_t alignment,
4175 uint64_t flags)
Eric Anholt673a3942008-07-30 12:06:12 -07004176{
Ben Widawsky6e7186a2014-05-06 22:21:36 -07004177 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004178 struct i915_vma *vma;
Chris Wilsonef79e172014-10-31 13:53:52 +00004179 unsigned bound;
Eric Anholt673a3942008-07-30 12:06:12 -07004180 int ret;
4181
Ben Widawsky6e7186a2014-05-06 22:21:36 -07004182 if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
4183 return -ENODEV;
4184
Daniel Vetterbf3d1492014-02-14 14:01:12 +01004185 if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
Daniel Vetter1ec9e262014-02-14 14:01:11 +01004186 return -EINVAL;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004187
Chris Wilsonc826c442014-10-31 13:53:53 +00004188 if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
4189 return -EINVAL;
4190
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004191 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
4192 return -EINVAL;
4193
4194 vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
4195 i915_gem_obj_to_vma(obj, vm);
4196
4197 if (IS_ERR(vma))
4198 return PTR_ERR(vma);
4199
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004200 if (vma) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004201 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
4202 return -EBUSY;
4203
Chris Wilsond23db882014-05-23 08:48:08 +02004204 if (i915_vma_misplaced(vma, alignment, flags)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004205 unsigned long offset;
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004206 offset = ggtt_view ? i915_gem_obj_ggtt_offset_view(obj, ggtt_view) :
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004207 i915_gem_obj_offset(obj, vm);
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004208 WARN(vma->pin_count,
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004209 "bo is already pinned in %s with incorrect alignment:"
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004210 " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01004211 " obj->map_and_fenceable=%d\n",
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004212 ggtt_view ? "ggtt" : "ppgtt",
4213 offset,
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004214 alignment,
Chris Wilsond23db882014-05-23 08:48:08 +02004215 !!(flags & PIN_MAPPABLE),
Chris Wilson05394f32010-11-08 19:18:58 +00004216 obj->map_and_fenceable);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004217 ret = i915_vma_unbind(vma);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004218 if (ret)
4219 return ret;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004220
4221 vma = NULL;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01004222 }
4223 }
4224
Chris Wilsonef79e172014-10-31 13:53:52 +00004225 bound = vma ? vma->bound : 0;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004226 if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004227 vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
4228 flags);
Daniel Vetter262de142014-02-14 14:01:20 +01004229 if (IS_ERR(vma))
4230 return PTR_ERR(vma);
Daniel Vetter08755462015-04-20 09:04:05 -07004231 } else {
4232 ret = i915_vma_bind(vma, obj->cache_level, flags);
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00004233 if (ret)
4234 return ret;
4235 }
Daniel Vetter74898d72012-02-15 23:50:22 +01004236
Joonas Lahtinen91e67112015-05-06 14:33:58 +03004237 if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
4238 (bound ^ vma->bound) & GLOBAL_BIND) {
Chris Wilsonef79e172014-10-31 13:53:52 +00004239 bool mappable, fenceable;
4240 u32 fence_size, fence_alignment;
4241
4242 fence_size = i915_gem_get_gtt_size(obj->base.dev,
4243 obj->base.size,
4244 obj->tiling_mode);
4245 fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
4246 obj->base.size,
4247 obj->tiling_mode,
4248 true);
4249
4250 fenceable = (vma->node.size == fence_size &&
4251 (vma->node.start & (fence_alignment - 1)) == 0);
4252
Chris Wilsone8dec1d2015-02-27 13:58:43 +00004253 mappable = (vma->node.start + fence_size <=
Chris Wilsonef79e172014-10-31 13:53:52 +00004254 dev_priv->gtt.mappable_end);
4255
4256 obj->map_and_fenceable = mappable && fenceable;
Chris Wilsonef79e172014-10-31 13:53:52 +00004257
Joonas Lahtinen91e67112015-05-06 14:33:58 +03004258 WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
4259 }
Chris Wilsonef79e172014-10-31 13:53:52 +00004260
Daniel Vetter8ea99c92014-02-14 14:01:21 +01004261 vma->pin_count++;
Eric Anholt673a3942008-07-30 12:06:12 -07004262 return 0;
4263}
4264
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004265int
4266i915_gem_object_pin(struct drm_i915_gem_object *obj,
4267 struct i915_address_space *vm,
4268 uint32_t alignment,
4269 uint64_t flags)
4270{
4271 return i915_gem_object_do_pin(obj, vm,
4272 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
4273 alignment, flags);
4274}
4275
4276int
4277i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
4278 const struct i915_ggtt_view *view,
4279 uint32_t alignment,
4280 uint64_t flags)
4281{
4282 if (WARN_ONCE(!view, "no view specified"))
4283 return -EINVAL;
4284
4285 return i915_gem_object_do_pin(obj, i915_obj_to_ggtt(obj), view,
Tvrtko Ursulin6fafab72015-03-17 15:36:51 +00004286 alignment, flags | PIN_GLOBAL);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004287}
4288
Eric Anholt673a3942008-07-30 12:06:12 -07004289void
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004290i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
4291 const struct i915_ggtt_view *view)
Eric Anholt673a3942008-07-30 12:06:12 -07004292{
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004293 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
Eric Anholt673a3942008-07-30 12:06:12 -07004294
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004295 BUG_ON(!vma);
Tvrtko Ursuline6617332015-03-23 11:10:33 +00004296 WARN_ON(vma->pin_count == 0);
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004297 WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004298
Chris Wilson30154652015-04-07 17:28:24 +01004299 --vma->pin_count;
Eric Anholt673a3942008-07-30 12:06:12 -07004300}
4301
Daniel Vetterd8ffa602014-05-13 12:11:26 +02004302bool
4303i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
4304{
4305 if (obj->fence_reg != I915_FENCE_REG_NONE) {
4306 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
4307 struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
4308
4309 WARN_ON(!ggtt_vma ||
4310 dev_priv->fence_regs[obj->fence_reg].pin_count >
4311 ggtt_vma->pin_count);
4312 dev_priv->fence_regs[obj->fence_reg].pin_count++;
4313 return true;
4314 } else
4315 return false;
4316}
4317
4318void
4319i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
4320{
4321 if (obj->fence_reg != I915_FENCE_REG_NONE) {
4322 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
4323 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
4324 dev_priv->fence_regs[obj->fence_reg].pin_count--;
4325 }
4326}
4327
Eric Anholt673a3942008-07-30 12:06:12 -07004328int
Eric Anholt673a3942008-07-30 12:06:12 -07004329i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004330 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004331{
4332 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004333 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004334 int ret;
4335
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004336 ret = i915_mutex_lock_interruptible(dev);
4337 if (ret)
4338 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004339
Chris Wilson05394f32010-11-08 19:18:58 +00004340 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004341 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004342 ret = -ENOENT;
4343 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004344 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004345
Chris Wilson0be555b2010-08-04 15:36:30 +01004346 /* Count all active objects as busy, even if they are currently not used
4347 * by the gpu. Users of this interface expect objects to eventually
4348 * become non-busy without any further actions, therefore emit any
4349 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004350 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004351 ret = i915_gem_object_flush_active(obj);
4352
Chris Wilson05394f32010-11-08 19:18:58 +00004353 args->busy = obj->active;
John Harrison41c52412014-11-24 18:49:43 +00004354 if (obj->last_read_req) {
4355 struct intel_engine_cs *ring;
Chris Wilsone9808ed2012-07-04 12:25:08 +01004356 BUILD_BUG_ON(I915_NUM_RINGS > 16);
John Harrison41c52412014-11-24 18:49:43 +00004357 ring = i915_gem_request_get_ring(obj->last_read_req);
4358 args->busy |= intel_ring_flag(ring) << 16;
Chris Wilsone9808ed2012-07-04 12:25:08 +01004359 }
Eric Anholt673a3942008-07-30 12:06:12 -07004360
Chris Wilson05394f32010-11-08 19:18:58 +00004361 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004362unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004363 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004364 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004365}
4366
4367int
4368i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4369 struct drm_file *file_priv)
4370{
Akshay Joshi0206e352011-08-16 15:34:10 -04004371 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004372}
4373
Chris Wilson3ef94da2009-09-14 16:50:29 +01004374int
4375i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4376 struct drm_file *file_priv)
4377{
Daniel Vetter656bfa32014-11-20 09:26:30 +01004378 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004379 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004380 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004381 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004382
4383 switch (args->madv) {
4384 case I915_MADV_DONTNEED:
4385 case I915_MADV_WILLNEED:
4386 break;
4387 default:
4388 return -EINVAL;
4389 }
4390
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004391 ret = i915_mutex_lock_interruptible(dev);
4392 if (ret)
4393 return ret;
4394
Chris Wilson05394f32010-11-08 19:18:58 +00004395 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, 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;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004399 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004400
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004401 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004402 ret = -EINVAL;
4403 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004404 }
4405
Daniel Vetter656bfa32014-11-20 09:26:30 +01004406 if (obj->pages &&
4407 obj->tiling_mode != I915_TILING_NONE &&
4408 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
4409 if (obj->madv == I915_MADV_WILLNEED)
4410 i915_gem_object_unpin_pages(obj);
4411 if (args->madv == I915_MADV_WILLNEED)
4412 i915_gem_object_pin_pages(obj);
4413 }
4414
Chris Wilson05394f32010-11-08 19:18:58 +00004415 if (obj->madv != __I915_MADV_PURGED)
4416 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004417
Chris Wilson6c085a72012-08-20 11:40:46 +02004418 /* if the object is no longer attached, discard its backing storage */
Daniel Vetterbe6a0372015-03-18 10:46:04 +01004419 if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004420 i915_gem_object_truncate(obj);
4421
Chris Wilson05394f32010-11-08 19:18:58 +00004422 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004423
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004424out:
Chris Wilson05394f32010-11-08 19:18:58 +00004425 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004426unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004427 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004428 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004429}
4430
Chris Wilson37e680a2012-06-07 15:38:42 +01004431void i915_gem_object_init(struct drm_i915_gem_object *obj,
4432 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004433{
Ben Widawsky35c20a62013-05-31 11:28:48 -07004434 INIT_LIST_HEAD(&obj->global_list);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004435 INIT_LIST_HEAD(&obj->ring_list);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004436 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004437 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson8d9d5742015-04-07 16:20:38 +01004438 INIT_LIST_HEAD(&obj->batch_pool_link);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004439
Chris Wilson37e680a2012-06-07 15:38:42 +01004440 obj->ops = ops;
4441
Chris Wilson0327d6b2012-08-11 15:41:06 +01004442 obj->fence_reg = I915_FENCE_REG_NONE;
4443 obj->madv = I915_MADV_WILLNEED;
Chris Wilson0327d6b2012-08-11 15:41:06 +01004444
4445 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
4446}
4447
Chris Wilson37e680a2012-06-07 15:38:42 +01004448static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
4449 .get_pages = i915_gem_object_get_pages_gtt,
4450 .put_pages = i915_gem_object_put_pages_gtt,
4451};
4452
Chris Wilson05394f32010-11-08 19:18:58 +00004453struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4454 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004455{
Daniel Vetterc397b902010-04-09 19:05:07 +00004456 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004457 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004458 gfp_t mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00004459
Chris Wilson42dcedd2012-11-15 11:32:30 +00004460 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004461 if (obj == NULL)
4462 return NULL;
4463
4464 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
Chris Wilson42dcedd2012-11-15 11:32:30 +00004465 i915_gem_object_free(obj);
Daniel Vetterc397b902010-04-09 19:05:07 +00004466 return NULL;
4467 }
4468
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004469 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4470 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4471 /* 965gm cannot relocate objects above 4GiB. */
4472 mask &= ~__GFP_HIGHMEM;
4473 mask |= __GFP_DMA32;
4474 }
4475
Al Viro496ad9a2013-01-23 17:07:38 -05004476 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004477 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004478
Chris Wilson37e680a2012-06-07 15:38:42 +01004479 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004480
Daniel Vetterc397b902010-04-09 19:05:07 +00004481 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4482 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4483
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004484 if (HAS_LLC(dev)) {
4485 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004486 * cache) for about a 10% performance improvement
4487 * compared to uncached. Graphics requests other than
4488 * display scanout are coherent with the CPU in
4489 * accessing this cache. This means in this mode we
4490 * don't need to clflush on the CPU side, and on the
4491 * GPU side we only need to flush internal caches to
4492 * get data visible to the CPU.
4493 *
4494 * However, we maintain the display planes as UC, and so
4495 * need to rebind when first used as such.
4496 */
4497 obj->cache_level = I915_CACHE_LLC;
4498 } else
4499 obj->cache_level = I915_CACHE_NONE;
4500
Daniel Vetterd861e332013-07-24 23:25:03 +02004501 trace_i915_gem_object_create(obj);
4502
Chris Wilson05394f32010-11-08 19:18:58 +00004503 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004504}
4505
Chris Wilson340fbd82014-05-22 09:16:52 +01004506static bool discard_backing_storage(struct drm_i915_gem_object *obj)
4507{
4508 /* If we are the last user of the backing storage (be it shmemfs
4509 * pages or stolen etc), we know that the pages are going to be
4510 * immediately released. In this case, we can then skip copying
4511 * back the contents from the GPU.
4512 */
4513
4514 if (obj->madv != I915_MADV_WILLNEED)
4515 return false;
4516
4517 if (obj->base.filp == NULL)
4518 return true;
4519
4520 /* At first glance, this looks racy, but then again so would be
4521 * userspace racing mmap against close. However, the first external
4522 * reference to the filp can only be obtained through the
4523 * i915_gem_mmap_ioctl() which safeguards us against the user
4524 * acquiring such a reference whilst we are in the middle of
4525 * freeing the object.
4526 */
4527 return atomic_long_read(&obj->base.filp->f_count) == 1;
4528}
4529
Chris Wilson1488fc02012-04-24 15:47:31 +01004530void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004531{
Chris Wilson1488fc02012-04-24 15:47:31 +01004532 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004533 struct drm_device *dev = obj->base.dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004534 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004535 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004536
Paulo Zanonif65c9162013-11-27 18:20:34 -02004537 intel_runtime_pm_get(dev_priv);
4538
Chris Wilson26e12f892011-03-20 11:20:19 +00004539 trace_i915_gem_object_destroy(obj);
4540
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004541 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004542 int ret;
4543
4544 vma->pin_count = 0;
4545 ret = i915_vma_unbind(vma);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004546 if (WARN_ON(ret == -ERESTARTSYS)) {
4547 bool was_interruptible;
Chris Wilson1488fc02012-04-24 15:47:31 +01004548
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004549 was_interruptible = dev_priv->mm.interruptible;
4550 dev_priv->mm.interruptible = false;
Chris Wilson1488fc02012-04-24 15:47:31 +01004551
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004552 WARN_ON(i915_vma_unbind(vma));
Chris Wilson1488fc02012-04-24 15:47:31 +01004553
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004554 dev_priv->mm.interruptible = was_interruptible;
4555 }
Chris Wilson1488fc02012-04-24 15:47:31 +01004556 }
4557
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004558 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4559 * before progressing. */
4560 if (obj->stolen)
4561 i915_gem_object_unpin_pages(obj);
4562
Daniel Vettera071fa02014-06-18 23:28:09 +02004563 WARN_ON(obj->frontbuffer_bits);
4564
Daniel Vetter656bfa32014-11-20 09:26:30 +01004565 if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
4566 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
4567 obj->tiling_mode != I915_TILING_NONE)
4568 i915_gem_object_unpin_pages(obj);
4569
Ben Widawsky401c29f2013-05-31 11:28:47 -07004570 if (WARN_ON(obj->pages_pin_count))
4571 obj->pages_pin_count = 0;
Chris Wilson340fbd82014-05-22 09:16:52 +01004572 if (discard_backing_storage(obj))
Chris Wilson55372522014-03-25 13:23:06 +00004573 obj->madv = I915_MADV_DONTNEED;
Chris Wilson37e680a2012-06-07 15:38:42 +01004574 i915_gem_object_put_pages(obj);
Chris Wilsond8cb5082012-08-11 15:41:03 +01004575 i915_gem_object_free_mmap_offset(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004576
Chris Wilson9da3da62012-06-01 15:20:22 +01004577 BUG_ON(obj->pages);
4578
Chris Wilson2f745ad2012-09-04 21:02:58 +01004579 if (obj->base.import_attach)
4580 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004581
Chris Wilson5cc9ed42014-05-16 14:22:37 +01004582 if (obj->ops->release)
4583 obj->ops->release(obj);
4584
Chris Wilson05394f32010-11-08 19:18:58 +00004585 drm_gem_object_release(&obj->base);
4586 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004587
Chris Wilson05394f32010-11-08 19:18:58 +00004588 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004589 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004590
4591 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004592}
4593
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004594struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
4595 struct i915_address_space *vm)
Ben Widawsky2f633152013-07-17 12:19:03 -07004596{
Daniel Vettere656a6c2013-08-14 14:14:04 +02004597 struct i915_vma *vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004598 list_for_each_entry(vma, &obj->vma_list, vma_link) {
4599 if (i915_is_ggtt(vma->vm) &&
4600 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
4601 continue;
4602 if (vma->vm == vm)
Daniel Vettere656a6c2013-08-14 14:14:04 +02004603 return vma;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004604 }
4605 return NULL;
4606}
Daniel Vettere656a6c2013-08-14 14:14:04 +02004607
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004608struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
4609 const struct i915_ggtt_view *view)
4610{
4611 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
4612 struct i915_vma *vma;
4613
4614 if (WARN_ONCE(!view, "no view specified"))
4615 return ERR_PTR(-EINVAL);
4616
4617 list_for_each_entry(vma, &obj->vma_list, vma_link)
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02004618 if (vma->vm == ggtt &&
4619 i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02004620 return vma;
Daniel Vettere656a6c2013-08-14 14:14:04 +02004621 return NULL;
4622}
4623
Ben Widawsky2f633152013-07-17 12:19:03 -07004624void i915_gem_vma_destroy(struct i915_vma *vma)
4625{
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004626 struct i915_address_space *vm = NULL;
Ben Widawsky2f633152013-07-17 12:19:03 -07004627 WARN_ON(vma->node.allocated);
Chris Wilsonaaa05662013-08-20 12:56:40 +01004628
4629 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4630 if (!list_empty(&vma->exec_list))
4631 return;
4632
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004633 vm = vma->vm;
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004634
Daniel Vetter841cd772014-08-06 15:04:48 +02004635 if (!i915_is_ggtt(vm))
4636 i915_ppgtt_put(i915_vm_to_ppgtt(vm));
Michel Thierryb9d06dd2014-08-06 15:04:44 +02004637
Ben Widawsky8b9c2b92013-07-31 17:00:16 -07004638 list_del(&vma->vma_link);
Daniel Vetterb93dab62013-08-26 11:23:47 +02004639
Chris Wilsone20d2ab2015-04-07 16:20:58 +01004640 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
Ben Widawsky2f633152013-07-17 12:19:03 -07004641}
4642
Chris Wilsone3efda42014-04-09 09:19:41 +01004643static void
4644i915_gem_stop_ringbuffers(struct drm_device *dev)
4645{
4646 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004647 struct intel_engine_cs *ring;
Chris Wilsone3efda42014-04-09 09:19:41 +01004648 int i;
4649
4650 for_each_ring(ring, dev_priv, i)
Oscar Mateoa83014d2014-07-24 17:04:21 +01004651 dev_priv->gt.stop_ring(ring);
Chris Wilsone3efda42014-04-09 09:19:41 +01004652}
4653
Jesse Barnes5669fca2009-02-17 15:13:31 -08004654int
Chris Wilson45c5f202013-10-16 11:50:01 +01004655i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004656{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004657 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson45c5f202013-10-16 11:50:01 +01004658 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004659
Chris Wilson45c5f202013-10-16 11:50:01 +01004660 mutex_lock(&dev->struct_mutex);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004661 ret = i915_gpu_idle(dev);
Chris Wilsonf7403342013-09-13 23:57:04 +01004662 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004663 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004664
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004665 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004666
Chris Wilsone3efda42014-04-09 09:19:41 +01004667 i915_gem_stop_ringbuffers(dev);
Chris Wilson45c5f202013-10-16 11:50:01 +01004668 mutex_unlock(&dev->struct_mutex);
4669
Chris Wilson737b1502015-01-26 18:03:03 +02004670 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004671 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
Deepak S274fa1c2014-08-05 07:51:20 -07004672 flush_delayed_work(&dev_priv->mm.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004673
Chris Wilsonbdcf1202014-11-25 11:56:33 +00004674 /* Assert that we sucessfully flushed all the work and
4675 * reset the GPU back to its idle, low power state.
4676 */
4677 WARN_ON(dev_priv->mm.busy);
4678
Eric Anholt673a3942008-07-30 12:06:12 -07004679 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004680
4681err:
4682 mutex_unlock(&dev->struct_mutex);
4683 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004684}
4685
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004686int i915_gem_l3_remap(struct intel_engine_cs *ring, int slice)
Ben Widawskyb9524a12012-05-25 16:56:24 -07004687{
Ben Widawskyc3787e22013-09-17 21:12:44 -07004688 struct drm_device *dev = ring->dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004689 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07004690 u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
4691 u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
Ben Widawskyc3787e22013-09-17 21:12:44 -07004692 int i, ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004693
Ben Widawsky040d2ba2013-09-19 11:01:40 -07004694 if (!HAS_L3_DPF(dev) || !remap_info)
Ben Widawskyc3787e22013-09-17 21:12:44 -07004695 return 0;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004696
Ben Widawskyc3787e22013-09-17 21:12:44 -07004697 ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3);
4698 if (ret)
4699 return ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004700
Ben Widawskyc3787e22013-09-17 21:12:44 -07004701 /*
4702 * Note: We do not worry about the concurrent register cacheline hang
4703 * here because no other code should access these registers other than
4704 * at initialization time.
4705 */
Ben Widawskyb9524a12012-05-25 16:56:24 -07004706 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
Ben Widawskyc3787e22013-09-17 21:12:44 -07004707 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
4708 intel_ring_emit(ring, reg_base + i);
4709 intel_ring_emit(ring, remap_info[i/4]);
Ben Widawskyb9524a12012-05-25 16:56:24 -07004710 }
4711
Ben Widawskyc3787e22013-09-17 21:12:44 -07004712 intel_ring_advance(ring);
Ben Widawskyb9524a12012-05-25 16:56:24 -07004713
Ben Widawskyc3787e22013-09-17 21:12:44 -07004714 return ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004715}
4716
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004717void i915_gem_init_swizzling(struct drm_device *dev)
4718{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004719 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004720
Daniel Vetter11782b02012-01-31 16:47:55 +01004721 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004722 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4723 return;
4724
4725 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4726 DISP_TILE_SURFACE_SWIZZLING);
4727
Daniel Vetter11782b02012-01-31 16:47:55 +01004728 if (IS_GEN5(dev))
4729 return;
4730
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004731 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4732 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004733 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004734 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004735 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky31a53362013-11-02 21:07:04 -07004736 else if (IS_GEN8(dev))
4737 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004738 else
4739 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004740}
Daniel Vettere21af882012-02-09 20:53:27 +01004741
Chris Wilson67b1b572012-07-05 23:49:40 +01004742static bool
4743intel_enable_blt(struct drm_device *dev)
4744{
4745 if (!HAS_BLT(dev))
4746 return false;
4747
4748 /* The blitter was dysfunctional on early prototypes */
4749 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
4750 DRM_INFO("BLT not supported on this pre-production hardware;"
4751 " graphics performance will be degraded.\n");
4752 return false;
4753 }
4754
4755 return true;
4756}
4757
Ville Syrjälä81e7f202014-08-15 01:21:55 +03004758static void init_unused_ring(struct drm_device *dev, u32 base)
4759{
4760 struct drm_i915_private *dev_priv = dev->dev_private;
4761
4762 I915_WRITE(RING_CTL(base), 0);
4763 I915_WRITE(RING_HEAD(base), 0);
4764 I915_WRITE(RING_TAIL(base), 0);
4765 I915_WRITE(RING_START(base), 0);
4766}
4767
4768static void init_unused_rings(struct drm_device *dev)
4769{
4770 if (IS_I830(dev)) {
4771 init_unused_ring(dev, PRB1_BASE);
4772 init_unused_ring(dev, SRB0_BASE);
4773 init_unused_ring(dev, SRB1_BASE);
4774 init_unused_ring(dev, SRB2_BASE);
4775 init_unused_ring(dev, SRB3_BASE);
4776 } else if (IS_GEN2(dev)) {
4777 init_unused_ring(dev, SRB0_BASE);
4778 init_unused_ring(dev, SRB1_BASE);
4779 } else if (IS_GEN3(dev)) {
4780 init_unused_ring(dev, PRB1_BASE);
4781 init_unused_ring(dev, PRB2_BASE);
4782 }
4783}
4784
Oscar Mateoa83014d2014-07-24 17:04:21 +01004785int i915_gem_init_rings(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004786{
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004787 struct drm_i915_private *dev_priv = dev->dev_private;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004788 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004789
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004790 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004791 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004792 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004793
4794 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004795 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004796 if (ret)
4797 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004798 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004799
Chris Wilson67b1b572012-07-05 23:49:40 +01004800 if (intel_enable_blt(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01004801 ret = intel_init_blt_ring_buffer(dev);
4802 if (ret)
4803 goto cleanup_bsd_ring;
4804 }
4805
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004806 if (HAS_VEBOX(dev)) {
4807 ret = intel_init_vebox_ring_buffer(dev);
4808 if (ret)
4809 goto cleanup_blt_ring;
4810 }
4811
Zhao Yakui845f74a2014-04-17 10:37:37 +08004812 if (HAS_BSD2(dev)) {
4813 ret = intel_init_bsd2_ring_buffer(dev);
4814 if (ret)
4815 goto cleanup_vebox_ring;
4816 }
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004817
Mika Kuoppala99433932013-01-22 14:12:17 +02004818 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
4819 if (ret)
Zhao Yakui845f74a2014-04-17 10:37:37 +08004820 goto cleanup_bsd2_ring;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004821
4822 return 0;
4823
Zhao Yakui845f74a2014-04-17 10:37:37 +08004824cleanup_bsd2_ring:
4825 intel_cleanup_ring_buffer(&dev_priv->ring[VCS2]);
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004826cleanup_vebox_ring:
4827 intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004828cleanup_blt_ring:
4829 intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
4830cleanup_bsd_ring:
4831 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
4832cleanup_render_ring:
4833 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
4834
4835 return ret;
4836}
4837
4838int
4839i915_gem_init_hw(struct drm_device *dev)
4840{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004841 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004842 struct intel_engine_cs *ring;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07004843 int ret, i;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004844
4845 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
4846 return -EIO;
4847
Chris Wilson5e4f5182015-02-13 14:35:59 +00004848 /* Double layer security blanket, see i915_gem_init() */
4849 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4850
Ben Widawsky59124502013-07-04 11:02:05 -07004851 if (dev_priv->ellc_size)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004852 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004853
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004854 if (IS_HASWELL(dev))
4855 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
4856 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004857
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004858 if (HAS_PCH_NOP(dev)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004859 if (IS_IVYBRIDGE(dev)) {
4860 u32 temp = I915_READ(GEN7_MSG_CTL);
4861 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4862 I915_WRITE(GEN7_MSG_CTL, temp);
4863 } else if (INTEL_INFO(dev)->gen >= 7) {
4864 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4865 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4866 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4867 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004868 }
4869
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004870 i915_gem_init_swizzling(dev);
4871
Daniel Vetterd5abdfd2014-11-20 09:45:19 +01004872 /*
4873 * At least 830 can leave some of the unused rings
4874 * "active" (ie. head != tail) after resume which
4875 * will prevent c3 entry. Makes sure all unused rings
4876 * are totally idle.
4877 */
4878 init_unused_rings(dev);
4879
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004880 for_each_ring(ring, dev_priv, i) {
4881 ret = ring->init_hw(ring);
4882 if (ret)
Chris Wilson5e4f5182015-02-13 14:35:59 +00004883 goto out;
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004884 }
Mika Kuoppala99433932013-01-22 14:12:17 +02004885
Ben Widawskyc3787e22013-09-17 21:12:44 -07004886 for (i = 0; i < NUM_L3_SLICES(dev); i++)
4887 i915_gem_l3_remap(&dev_priv->ring[RCS], i);
4888
David Woodhousef48a0162015-01-20 17:21:42 +00004889 ret = i915_ppgtt_init_hw(dev);
4890 if (ret && ret != -EIO) {
4891 DRM_ERROR("PPGTT enable failed %d\n", ret);
4892 i915_gem_cleanup_ringbuffer(dev);
4893 }
4894
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004895 ret = i915_gem_context_enable(dev_priv);
Chris Wilson60990322014-04-09 09:19:42 +01004896 if (ret && ret != -EIO) {
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004897 DRM_ERROR("Context enable failed %d\n", ret);
Chris Wilson60990322014-04-09 09:19:42 +01004898 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter82460d92014-08-06 20:19:53 +02004899
Chris Wilson5e4f5182015-02-13 14:35:59 +00004900 goto out;
Daniel Vetter82460d92014-08-06 20:19:53 +02004901 }
4902
Chris Wilson5e4f5182015-02-13 14:35:59 +00004903out:
4904 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004905 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004906}
4907
Chris Wilson1070a422012-04-24 15:47:41 +01004908int i915_gem_init(struct drm_device *dev)
4909{
4910 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1070a422012-04-24 15:47:41 +01004911 int ret;
4912
Oscar Mateo127f1002014-07-24 17:04:11 +01004913 i915.enable_execlists = intel_sanitize_enable_execlists(dev,
4914 i915.enable_execlists);
4915
Chris Wilson1070a422012-04-24 15:47:41 +01004916 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004917
4918 if (IS_VALLEYVIEW(dev)) {
4919 /* VLVA0 (potential hack), BIOS isn't actually waking us */
Imre Deak981a5ae2014-04-14 20:24:22 +03004920 I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
4921 if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
4922 VLV_GTLC_ALLOWWAKEACK), 10))
Jesse Barnesd62b4892013-03-08 10:45:53 -08004923 DRM_DEBUG_DRIVER("allow wake ack timed out\n");
4924 }
4925
Oscar Mateoa83014d2014-07-24 17:04:21 +01004926 if (!i915.enable_execlists) {
John Harrisonf3dc74c2015-03-19 12:30:06 +00004927 dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004928 dev_priv->gt.init_rings = i915_gem_init_rings;
4929 dev_priv->gt.cleanup_ring = intel_cleanup_ring_buffer;
4930 dev_priv->gt.stop_ring = intel_stop_ring_buffer;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004931 } else {
John Harrisonf3dc74c2015-03-19 12:30:06 +00004932 dev_priv->gt.execbuf_submit = intel_execlists_submission;
Oscar Mateo454afeb2014-07-24 17:04:22 +01004933 dev_priv->gt.init_rings = intel_logical_rings_init;
4934 dev_priv->gt.cleanup_ring = intel_logical_ring_cleanup;
4935 dev_priv->gt.stop_ring = intel_logical_ring_stop;
Oscar Mateoa83014d2014-07-24 17:04:21 +01004936 }
4937
Chris Wilson5e4f5182015-02-13 14:35:59 +00004938 /* This is just a security blanket to placate dragons.
4939 * On some systems, we very sporadically observe that the first TLBs
4940 * used by the CS may be stale, despite us poking the TLB reset. If
4941 * we hold the forcewake during initialisation these problems
4942 * just magically go away.
4943 */
4944 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4945
Daniel Vetter6c5566a2014-08-06 15:04:50 +02004946 ret = i915_gem_init_userptr(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004947 if (ret)
4948 goto out_unlock;
Daniel Vetter6c5566a2014-08-06 15:04:50 +02004949
Ben Widawskyd7e50082012-12-18 10:31:25 -08004950 i915_gem_init_global_gtt(dev);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004951
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004952 ret = i915_gem_context_init(dev);
Jani Nikula7bcc3772014-12-05 14:17:42 +02004953 if (ret)
4954 goto out_unlock;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004955
Daniel Vetter35a57ff2014-11-20 00:33:07 +01004956 ret = dev_priv->gt.init_rings(dev);
4957 if (ret)
Jani Nikula7bcc3772014-12-05 14:17:42 +02004958 goto out_unlock;
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004959
4960 ret = i915_gem_init_hw(dev);
Chris Wilson60990322014-04-09 09:19:42 +01004961 if (ret == -EIO) {
4962 /* Allow ring initialisation to fail by marking the GPU as
4963 * wedged. But we only want to do this where the GPU is angry,
4964 * for all other failure, such as an allocation failure, bail.
4965 */
4966 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
4967 atomic_set_mask(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
4968 ret = 0;
Chris Wilson1070a422012-04-24 15:47:41 +01004969 }
Jani Nikula7bcc3772014-12-05 14:17:42 +02004970
4971out_unlock:
Chris Wilson5e4f5182015-02-13 14:35:59 +00004972 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Chris Wilson60990322014-04-09 09:19:42 +01004973 mutex_unlock(&dev->struct_mutex);
Chris Wilson1070a422012-04-24 15:47:41 +01004974
Chris Wilson60990322014-04-09 09:19:42 +01004975 return ret;
Chris Wilson1070a422012-04-24 15:47:41 +01004976}
4977
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004978void
4979i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4980{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004981 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004982 struct intel_engine_cs *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004983 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004984
Chris Wilsonb4519512012-05-11 14:29:30 +01004985 for_each_ring(ring, dev_priv, i)
Oscar Mateoa83014d2014-07-24 17:04:21 +01004986 dev_priv->gt.cleanup_ring(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004987}
4988
Chris Wilson64193402010-10-24 12:38:05 +01004989static void
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004990init_ring_lists(struct intel_engine_cs *ring)
Chris Wilson64193402010-10-24 12:38:05 +01004991{
4992 INIT_LIST_HEAD(&ring->active_list);
4993 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004994}
4995
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08004996void i915_init_vm(struct drm_i915_private *dev_priv,
4997 struct i915_address_space *vm)
Ben Widawskyfc8c0672013-07-31 16:59:54 -07004998{
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08004999 if (!i915_is_ggtt(vm))
5000 drm_mm_init(&vm->mm, vm->start, vm->total);
Ben Widawskyfc8c0672013-07-31 16:59:54 -07005001 vm->dev = dev_priv->dev;
5002 INIT_LIST_HEAD(&vm->active_list);
5003 INIT_LIST_HEAD(&vm->inactive_list);
5004 INIT_LIST_HEAD(&vm->global_link);
Chris Wilsonf72d21e2014-01-09 22:57:22 +00005005 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Ben Widawskyfc8c0672013-07-31 16:59:54 -07005006}
5007
Eric Anholt673a3942008-07-30 12:06:12 -07005008void
5009i915_gem_load(struct drm_device *dev)
5010{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03005011 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson42dcedd2012-11-15 11:32:30 +00005012 int i;
5013
Chris Wilsonefab6d82015-04-07 16:20:57 +01005014 dev_priv->objects =
Chris Wilson42dcedd2012-11-15 11:32:30 +00005015 kmem_cache_create("i915_gem_object",
5016 sizeof(struct drm_i915_gem_object), 0,
5017 SLAB_HWCACHE_ALIGN,
5018 NULL);
Chris Wilsone20d2ab2015-04-07 16:20:58 +01005019 dev_priv->vmas =
5020 kmem_cache_create("i915_gem_vma",
5021 sizeof(struct i915_vma), 0,
5022 SLAB_HWCACHE_ALIGN,
5023 NULL);
Chris Wilsonefab6d82015-04-07 16:20:57 +01005024 dev_priv->requests =
5025 kmem_cache_create("i915_gem_request",
5026 sizeof(struct drm_i915_gem_request), 0,
5027 SLAB_HWCACHE_ALIGN,
5028 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07005029
Ben Widawskyfc8c0672013-07-31 16:59:54 -07005030 INIT_LIST_HEAD(&dev_priv->vm_list);
5031 i915_init_vm(dev_priv, &dev_priv->gtt.base);
5032
Ben Widawskya33afea2013-09-17 21:12:45 -07005033 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02005034 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
5035 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07005036 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00005037 for (i = 0; i < I915_NUM_RINGS; i++)
5038 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02005039 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02005040 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07005041 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
5042 i915_gem_retire_work_handler);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005043 INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
5044 i915_gem_idle_work_handler);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01005045 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01005046
Chris Wilson72bfa192010-12-19 11:42:05 +00005047 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
5048
Ville Syrjälä42b5aea2013-04-09 13:02:47 +03005049 if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
5050 dev_priv->num_fence_regs = 32;
5051 else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08005052 dev_priv->num_fence_regs = 16;
5053 else
5054 dev_priv->num_fence_regs = 8;
5055
Yu Zhangeb822892015-02-10 19:05:49 +08005056 if (intel_vgpu_active(dev))
5057 dev_priv->num_fence_regs =
5058 I915_READ(vgtif_reg(avail_rs.fence_num));
5059
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02005060 /* Initialize fence registers to zero */
Chris Wilson19b2dbd2013-06-12 10:15:12 +01005061 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
5062 i915_gem_restore_fences(dev);
Eric Anholt10ed13e2011-05-06 13:53:49 -07005063
Eric Anholt673a3942008-07-30 12:06:12 -07005064 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005065 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01005066
Chris Wilsonce453d82011-02-21 14:43:56 +00005067 dev_priv->mm.interruptible = true;
5068
Daniel Vetterbe6a0372015-03-18 10:46:04 +01005069 i915_gem_shrinker_init(dev_priv);
Daniel Vetterf99d7062014-06-19 16:01:59 +02005070
5071 mutex_init(&dev_priv->fb_tracking.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07005072}
Dave Airlie71acb5e2008-12-30 20:31:46 +10005073
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005074void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00005075{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005076 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00005077
5078 /* Clean up our request list when the client is going away, so that
5079 * later retire_requests won't dereference our soon-to-be-gone
5080 * file_priv.
5081 */
Chris Wilson1c255952010-09-26 11:03:27 +01005082 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005083 while (!list_empty(&file_priv->mm.request_list)) {
5084 struct drm_i915_gem_request *request;
5085
5086 request = list_first_entry(&file_priv->mm.request_list,
5087 struct drm_i915_gem_request,
5088 client_list);
5089 list_del(&request->client_list);
5090 request->file_priv = NULL;
5091 }
Chris Wilson1c255952010-09-26 11:03:27 +01005092 spin_unlock(&file_priv->mm.lock);
Chris Wilson31169712009-09-14 16:50:28 +01005093
Chris Wilson1854d5c2015-04-07 16:20:32 +01005094 if (!list_empty(&file_priv->rps_boost)) {
5095 mutex_lock(&to_i915(dev)->rps.hw_lock);
5096 list_del(&file_priv->rps_boost);
5097 mutex_unlock(&to_i915(dev)->rps.hw_lock);
5098 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005099}
5100
5101int i915_gem_open(struct drm_device *dev, struct drm_file *file)
5102{
5103 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08005104 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005105
5106 DRM_DEBUG_DRIVER("\n");
5107
5108 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
5109 if (!file_priv)
5110 return -ENOMEM;
5111
5112 file->driver_priv = file_priv;
5113 file_priv->dev_priv = dev->dev_private;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02005114 file_priv->file = file;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005115 INIT_LIST_HEAD(&file_priv->rps_boost);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005116
5117 spin_lock_init(&file_priv->mm.lock);
5118 INIT_LIST_HEAD(&file_priv->mm.request_list);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005119
Ben Widawskye422b882013-12-06 14:10:58 -08005120 ret = i915_gem_context_open(dev, file);
5121 if (ret)
5122 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005123
Ben Widawskye422b882013-12-06 14:10:58 -08005124 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005125}
5126
Daniel Vetterb680c372014-09-19 18:27:27 +02005127/**
5128 * i915_gem_track_fb - update frontbuffer tracking
5129 * old: current GEM buffer for the frontbuffer slots
5130 * new: new GEM buffer for the frontbuffer slots
5131 * frontbuffer_bits: bitmask of frontbuffer slots
5132 *
5133 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
5134 * from @old and setting them in @new. Both @old and @new can be NULL.
5135 */
Daniel Vettera071fa02014-06-18 23:28:09 +02005136void i915_gem_track_fb(struct drm_i915_gem_object *old,
5137 struct drm_i915_gem_object *new,
5138 unsigned frontbuffer_bits)
5139{
5140 if (old) {
5141 WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
5142 WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
5143 old->frontbuffer_bits &= ~frontbuffer_bits;
5144 }
5145
5146 if (new) {
5147 WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
5148 WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
5149 new->frontbuffer_bits |= frontbuffer_bits;
5150 }
5151}
5152
Ben Widawskya70a3142013-07-31 16:59:56 -07005153/* All the new VM stuff */
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005154unsigned long
5155i915_gem_obj_offset(struct drm_i915_gem_object *o,
5156 struct i915_address_space *vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07005157{
5158 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
5159 struct i915_vma *vma;
5160
Daniel Vetter896ab1a2014-08-06 15:04:51 +02005161 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
Ben Widawskya70a3142013-07-31 16:59:56 -07005162
Ben Widawskya70a3142013-07-31 16:59:56 -07005163 list_for_each_entry(vma, &o->vma_list, vma_link) {
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005164 if (i915_is_ggtt(vma->vm) &&
5165 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5166 continue;
5167 if (vma->vm == vm)
Ben Widawskya70a3142013-07-31 16:59:56 -07005168 return vma->node.start;
Ben Widawskya70a3142013-07-31 16:59:56 -07005169 }
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005170
Daniel Vetterf25748ea2014-06-17 22:34:38 +02005171 WARN(1, "%s vma for this object not found.\n",
5172 i915_is_ggtt(vm) ? "global" : "ppgtt");
Ben Widawskya70a3142013-07-31 16:59:56 -07005173 return -1;
5174}
5175
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005176unsigned long
5177i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005178 const struct i915_ggtt_view *view)
Ben Widawskya70a3142013-07-31 16:59:56 -07005179{
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005180 struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
Ben Widawskya70a3142013-07-31 16:59:56 -07005181 struct i915_vma *vma;
5182
5183 list_for_each_entry(vma, &o->vma_list, vma_link)
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005184 if (vma->vm == ggtt &&
5185 i915_ggtt_view_equal(&vma->ggtt_view, view))
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005186 return vma->node.start;
5187
Tvrtko Ursulin5678ad72015-03-17 14:45:29 +00005188 WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005189 return -1;
5190}
5191
5192bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
5193 struct i915_address_space *vm)
5194{
5195 struct i915_vma *vma;
5196
5197 list_for_each_entry(vma, &o->vma_list, vma_link) {
5198 if (i915_is_ggtt(vma->vm) &&
5199 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5200 continue;
5201 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
5202 return true;
5203 }
5204
5205 return false;
5206}
5207
5208bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005209 const struct i915_ggtt_view *view)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005210{
5211 struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
5212 struct i915_vma *vma;
5213
5214 list_for_each_entry(vma, &o->vma_list, vma_link)
5215 if (vma->vm == ggtt &&
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02005216 i915_ggtt_view_equal(&vma->ggtt_view, view) &&
Tvrtko Ursulinfe14d5f2014-12-10 17:27:58 +00005217 drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07005218 return true;
5219
5220 return false;
5221}
5222
5223bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
5224{
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01005225 struct i915_vma *vma;
Ben Widawskya70a3142013-07-31 16:59:56 -07005226
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01005227 list_for_each_entry(vma, &o->vma_list, vma_link)
5228 if (drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07005229 return true;
5230
5231 return false;
5232}
5233
5234unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
5235 struct i915_address_space *vm)
5236{
5237 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
5238 struct i915_vma *vma;
5239
Daniel Vetter896ab1a2014-08-06 15:04:51 +02005240 WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
Ben Widawskya70a3142013-07-31 16:59:56 -07005241
5242 BUG_ON(list_empty(&o->vma_list));
5243
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005244 list_for_each_entry(vma, &o->vma_list, vma_link) {
5245 if (i915_is_ggtt(vma->vm) &&
5246 vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
5247 continue;
Ben Widawskya70a3142013-07-31 16:59:56 -07005248 if (vma->vm == vm)
5249 return vma->node.size;
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005250 }
Ben Widawskya70a3142013-07-31 16:59:56 -07005251 return 0;
5252}
5253
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005254bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07005255{
5256 struct i915_vma *vma;
Joonas Lahtinena6631ae2015-05-06 14:34:58 +03005257 list_for_each_entry(vma, &obj->vma_list, vma_link)
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005258 if (vma->pin_count > 0)
5259 return true;
Joonas Lahtinena6631ae2015-05-06 14:34:58 +03005260
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005261 return false;
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07005262}
Joonas Lahtinenec7adb62015-03-16 14:11:13 +02005263