blob: b391f30f9985dd79dcb5a4fcaf8c261171b5d189 [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070031#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010032#include "i915_trace.h"
Jesse Barnes652c3932009-08-17 13:31:43 -070033#include "intel_drv.h"
Hugh Dickins5949eac2011-06-27 16:18:18 -070034#include <linux/shmem_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Eric Anholt673a3942008-07-30 12:06:12 -070036#include <linux/swap.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#include <linux/pci.h>
Daniel Vetter1286ff72012-05-10 15:25:09 +020038#include <linux/dma-buf.h>
Eric Anholt673a3942008-07-30 12:06:12 -070039
Chris Wilson05394f32010-11-08 19:18:58 +000040static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson2c225692013-08-09 12:26:45 +010041static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
42 bool force);
Ben Widawsky07fe0b12013-07-31 17:00:10 -070043static __must_check int
Ben Widawsky23f54482013-09-11 14:57:48 -070044i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
45 bool readonly);
Eric Anholt673a3942008-07-30 12:06:12 -070046
Chris Wilson61050802012-04-17 15:31:31 +010047static void i915_gem_write_fence(struct drm_device *dev, int reg,
48 struct drm_i915_gem_object *obj);
49static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
50 struct drm_i915_fence_reg *fence,
51 bool enable);
52
Dave Chinner7dc19d52013-08-28 10:18:11 +100053static unsigned long i915_gem_inactive_count(struct shrinker *shrinker,
54 struct shrink_control *sc);
55static unsigned long i915_gem_inactive_scan(struct shrinker *shrinker,
56 struct shrink_control *sc);
Chris Wilsond9973b42013-10-04 10:33:00 +010057static unsigned long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
58static unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv);
Daniel Vetter8c599672011-12-14 13:57:31 +010059static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
Damien Lespiaucb216aa2014-03-03 17:42:36 +000060static void i915_gem_retire_requests_ring(struct intel_ring_buffer *ring);
Chris Wilson31169712009-09-14 16:50:28 +010061
Chris Wilsonc76ce032013-08-08 14:41:03 +010062static bool cpu_cache_is_coherent(struct drm_device *dev,
63 enum i915_cache_level level)
64{
65 return HAS_LLC(dev) || level != I915_CACHE_NONE;
66}
67
Chris Wilson2c225692013-08-09 12:26:45 +010068static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
69{
70 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
71 return true;
72
73 return obj->pin_display;
74}
75
Chris Wilson61050802012-04-17 15:31:31 +010076static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
77{
78 if (obj->tiling_mode)
79 i915_gem_release_mmap(obj);
80
81 /* As we do not have an associated fence register, we will force
82 * a tiling change if we ever need to acquire one.
83 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +010084 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +010085 obj->fence_reg = I915_FENCE_REG_NONE;
86}
87
Chris Wilson73aa8082010-09-30 11:46:12 +010088/* some bookkeeping */
89static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
90 size_t size)
91{
Daniel Vetterc20e8352013-07-24 22:40:23 +020092 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010093 dev_priv->mm.object_count++;
94 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +020095 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +010096}
97
98static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
99 size_t size)
100{
Daniel Vetterc20e8352013-07-24 22:40:23 +0200101 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100102 dev_priv->mm.object_count--;
103 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200104 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100105}
106
Chris Wilson21dd3732011-01-26 15:55:56 +0000107static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100108i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100109{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100110 int ret;
111
Daniel Vetter7abb6902013-05-24 21:29:32 +0200112#define EXIT_COND (!i915_reset_in_progress(error) || \
113 i915_terminally_wedged(error))
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100114 if (EXIT_COND)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100115 return 0;
116
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200117 /*
118 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
119 * userspace. If it takes that long something really bad is going on and
120 * we should simply try to bail out and fail as gracefully as possible.
121 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100122 ret = wait_event_interruptible_timeout(error->reset_queue,
123 EXIT_COND,
124 10*HZ);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200125 if (ret == 0) {
126 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
127 return -EIO;
128 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100129 return ret;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200130 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100131#undef EXIT_COND
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100132
Chris Wilson21dd3732011-01-26 15:55:56 +0000133 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100134}
135
Chris Wilson54cf91d2010-11-25 18:00:26 +0000136int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100137{
Daniel Vetter33196de2012-11-14 17:14:05 +0100138 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100139 int ret;
140
Daniel Vetter33196de2012-11-14 17:14:05 +0100141 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100142 if (ret)
143 return ret;
144
145 ret = mutex_lock_interruptible(&dev->struct_mutex);
146 if (ret)
147 return ret;
148
Chris Wilson23bc5982010-09-29 16:10:57 +0100149 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100150 return 0;
151}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100152
Chris Wilson7d1c4802010-08-07 21:45:03 +0100153static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000154i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100155{
Ben Widawsky98438772013-07-31 17:00:12 -0700156 return i915_gem_obj_bound_any(obj) && !obj->active;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100157}
158
Eric Anholt673a3942008-07-30 12:06:12 -0700159int
160i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000161 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700162{
Ben Widawsky93d18792013-01-17 12:45:17 -0800163 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700164 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000165
Daniel Vetter7bb6fb82012-04-24 08:22:52 +0200166 if (drm_core_check_feature(dev, DRIVER_MODESET))
167 return -ENODEV;
168
Chris Wilson20217462010-11-23 15:26:33 +0000169 if (args->gtt_start >= args->gtt_end ||
170 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
171 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700172
Daniel Vetterf534bc02012-03-26 22:37:04 +0200173 /* GEM with user mode setting was never supported on ilk and later. */
174 if (INTEL_INFO(dev)->gen >= 5)
175 return -ENODEV;
176
Eric Anholt673a3942008-07-30 12:06:12 -0700177 mutex_lock(&dev->struct_mutex);
Ben Widawskyd7e50082012-12-18 10:31:25 -0800178 i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
179 args->gtt_end);
Ben Widawsky93d18792013-01-17 12:45:17 -0800180 dev_priv->gtt.mappable_end = args->gtt_end;
Eric Anholt673a3942008-07-30 12:06:12 -0700181 mutex_unlock(&dev->struct_mutex);
182
Chris Wilson20217462010-11-23 15:26:33 +0000183 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700184}
185
Eric Anholt5a125c32008-10-22 21:40:13 -0700186int
187i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000188 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700189{
Chris Wilson73aa8082010-09-30 11:46:12 +0100190 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700191 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000192 struct drm_i915_gem_object *obj;
193 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700194
Chris Wilson6299f992010-11-24 12:23:44 +0000195 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100196 mutex_lock(&dev->struct_mutex);
Ben Widawsky35c20a62013-05-31 11:28:48 -0700197 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800198 if (i915_gem_obj_is_pinned(obj))
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700199 pinned += i915_gem_obj_ggtt_size(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +0100200 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700201
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700202 args->aper_size = dev_priv->gtt.base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400203 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000204
Eric Anholt5a125c32008-10-22 21:40:13 -0700205 return 0;
206}
207
Chris Wilson00731152014-05-21 12:42:56 +0100208static void i915_gem_object_detach_phys(struct drm_i915_gem_object *obj)
209{
210 drm_dma_handle_t *phys = obj->phys_handle;
211
212 if (!phys)
213 return;
214
215 if (obj->madv == I915_MADV_WILLNEED) {
216 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
217 char *vaddr = phys->vaddr;
218 int i;
219
220 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
221 struct page *page = shmem_read_mapping_page(mapping, i);
222 if (!IS_ERR(page)) {
223 char *dst = kmap_atomic(page);
224 memcpy(dst, vaddr, PAGE_SIZE);
225 drm_clflush_virt_range(dst, PAGE_SIZE);
226 kunmap_atomic(dst);
227
228 set_page_dirty(page);
229 mark_page_accessed(page);
230 page_cache_release(page);
231 }
232 vaddr += PAGE_SIZE;
233 }
234 i915_gem_chipset_flush(obj->base.dev);
235 }
236
237#ifdef CONFIG_X86
238 set_memory_wb((unsigned long)phys->vaddr, phys->size / PAGE_SIZE);
239#endif
240 drm_pci_free(obj->base.dev, phys);
241 obj->phys_handle = NULL;
242}
243
244int
245i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
246 int align)
247{
248 drm_dma_handle_t *phys;
249 struct address_space *mapping;
250 char *vaddr;
251 int i;
252
253 if (obj->phys_handle) {
254 if ((unsigned long)obj->phys_handle->vaddr & (align -1))
255 return -EBUSY;
256
257 return 0;
258 }
259
260 if (obj->madv != I915_MADV_WILLNEED)
261 return -EFAULT;
262
263 if (obj->base.filp == NULL)
264 return -EINVAL;
265
266 /* create a new object */
267 phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
268 if (!phys)
269 return -ENOMEM;
270
271 vaddr = phys->vaddr;
272#ifdef CONFIG_X86
273 set_memory_wc((unsigned long)vaddr, phys->size / PAGE_SIZE);
274#endif
275 mapping = file_inode(obj->base.filp)->i_mapping;
276 for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
277 struct page *page;
278 char *src;
279
280 page = shmem_read_mapping_page(mapping, i);
281 if (IS_ERR(page)) {
282#ifdef CONFIG_X86
283 set_memory_wb((unsigned long)phys->vaddr, phys->size / PAGE_SIZE);
284#endif
285 drm_pci_free(obj->base.dev, phys);
286 return PTR_ERR(page);
287 }
288
289 src = kmap_atomic(page);
290 memcpy(vaddr, src, PAGE_SIZE);
291 kunmap_atomic(src);
292
293 mark_page_accessed(page);
294 page_cache_release(page);
295
296 vaddr += PAGE_SIZE;
297 }
298
299 obj->phys_handle = phys;
300 return 0;
301}
302
303static int
304i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
305 struct drm_i915_gem_pwrite *args,
306 struct drm_file *file_priv)
307{
308 struct drm_device *dev = obj->base.dev;
309 void *vaddr = obj->phys_handle->vaddr + args->offset;
310 char __user *user_data = to_user_ptr(args->data_ptr);
311
312 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
313 unsigned long unwritten;
314
315 /* The physical object once assigned is fixed for the lifetime
316 * of the obj, so we can safely drop the lock and continue
317 * to access vaddr.
318 */
319 mutex_unlock(&dev->struct_mutex);
320 unwritten = copy_from_user(vaddr, user_data, args->size);
321 mutex_lock(&dev->struct_mutex);
322 if (unwritten)
323 return -EFAULT;
324 }
325
326 i915_gem_chipset_flush(dev);
327 return 0;
328}
329
Chris Wilson42dcedd2012-11-15 11:32:30 +0000330void *i915_gem_object_alloc(struct drm_device *dev)
331{
332 struct drm_i915_private *dev_priv = dev->dev_private;
Joe Perchesfac15c12013-08-29 13:11:07 -0700333 return kmem_cache_zalloc(dev_priv->slab, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000334}
335
336void i915_gem_object_free(struct drm_i915_gem_object *obj)
337{
338 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
339 kmem_cache_free(dev_priv->slab, obj);
340}
341
Dave Airlieff72145b2011-02-07 12:16:14 +1000342static int
343i915_gem_create(struct drm_file *file,
344 struct drm_device *dev,
345 uint64_t size,
346 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700347{
Chris Wilson05394f32010-11-08 19:18:58 +0000348 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300349 int ret;
350 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700351
Dave Airlieff72145b2011-02-07 12:16:14 +1000352 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200353 if (size == 0)
354 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700355
356 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000357 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700358 if (obj == NULL)
359 return -ENOMEM;
360
Chris Wilson05394f32010-11-08 19:18:58 +0000361 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100362 /* drop reference from allocate - handle holds it now */
Daniel Vetterd861e332013-07-24 23:25:03 +0200363 drm_gem_object_unreference_unlocked(&obj->base);
364 if (ret)
365 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100366
Dave Airlieff72145b2011-02-07 12:16:14 +1000367 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700368 return 0;
369}
370
Dave Airlieff72145b2011-02-07 12:16:14 +1000371int
372i915_gem_dumb_create(struct drm_file *file,
373 struct drm_device *dev,
374 struct drm_mode_create_dumb *args)
375{
376 /* have to work out size/pitch and return them */
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300377 args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000378 args->size = args->pitch * args->height;
379 return i915_gem_create(file, dev,
380 args->size, &args->handle);
381}
382
Dave Airlieff72145b2011-02-07 12:16:14 +1000383/**
384 * Creates a new mm object and returns a handle to it.
385 */
386int
387i915_gem_create_ioctl(struct drm_device *dev, void *data,
388 struct drm_file *file)
389{
390 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200391
Dave Airlieff72145b2011-02-07 12:16:14 +1000392 return i915_gem_create(file, dev,
393 args->size, &args->handle);
394}
395
Daniel Vetter8c599672011-12-14 13:57:31 +0100396static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100397__copy_to_user_swizzled(char __user *cpu_vaddr,
398 const char *gpu_vaddr, int gpu_offset,
399 int length)
400{
401 int ret, cpu_offset = 0;
402
403 while (length > 0) {
404 int cacheline_end = ALIGN(gpu_offset + 1, 64);
405 int this_length = min(cacheline_end - gpu_offset, length);
406 int swizzled_gpu_offset = gpu_offset ^ 64;
407
408 ret = __copy_to_user(cpu_vaddr + cpu_offset,
409 gpu_vaddr + swizzled_gpu_offset,
410 this_length);
411 if (ret)
412 return ret + length;
413
414 cpu_offset += this_length;
415 gpu_offset += this_length;
416 length -= this_length;
417 }
418
419 return 0;
420}
421
422static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700423__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
424 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100425 int length)
426{
427 int ret, cpu_offset = 0;
428
429 while (length > 0) {
430 int cacheline_end = ALIGN(gpu_offset + 1, 64);
431 int this_length = min(cacheline_end - gpu_offset, length);
432 int swizzled_gpu_offset = gpu_offset ^ 64;
433
434 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
435 cpu_vaddr + cpu_offset,
436 this_length);
437 if (ret)
438 return ret + length;
439
440 cpu_offset += this_length;
441 gpu_offset += this_length;
442 length -= this_length;
443 }
444
445 return 0;
446}
447
Brad Volkin4c914c02014-02-18 10:15:45 -0800448/*
449 * Pins the specified object's pages and synchronizes the object with
450 * GPU accesses. Sets needs_clflush to non-zero if the caller should
451 * flush the object from the CPU cache.
452 */
453int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
454 int *needs_clflush)
455{
456 int ret;
457
458 *needs_clflush = 0;
459
460 if (!obj->base.filp)
461 return -EINVAL;
462
463 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
464 /* If we're not in the cpu read domain, set ourself into the gtt
465 * read domain and manually flush cachelines (if required). This
466 * optimizes for the case when the gpu will dirty the data
467 * anyway again before the next pread happens. */
468 *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
469 obj->cache_level);
470 ret = i915_gem_object_wait_rendering(obj, true);
471 if (ret)
472 return ret;
473 }
474
475 ret = i915_gem_object_get_pages(obj);
476 if (ret)
477 return ret;
478
479 i915_gem_object_pin_pages(obj);
480
481 return ret;
482}
483
Daniel Vetterd174bd62012-03-25 19:47:40 +0200484/* Per-page copy function for the shmem pread fastpath.
485 * Flushes invalid cachelines before reading the target if
486 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700487static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200488shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
489 char __user *user_data,
490 bool page_do_bit17_swizzling, bool needs_clflush)
491{
492 char *vaddr;
493 int ret;
494
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200495 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200496 return -EINVAL;
497
498 vaddr = kmap_atomic(page);
499 if (needs_clflush)
500 drm_clflush_virt_range(vaddr + shmem_page_offset,
501 page_length);
502 ret = __copy_to_user_inatomic(user_data,
503 vaddr + shmem_page_offset,
504 page_length);
505 kunmap_atomic(vaddr);
506
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100507 return ret ? -EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200508}
509
Daniel Vetter23c18c72012-03-25 19:47:42 +0200510static void
511shmem_clflush_swizzled_range(char *addr, unsigned long length,
512 bool swizzled)
513{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200514 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200515 unsigned long start = (unsigned long) addr;
516 unsigned long end = (unsigned long) addr + length;
517
518 /* For swizzling simply ensure that we always flush both
519 * channels. Lame, but simple and it works. Swizzled
520 * pwrite/pread is far from a hotpath - current userspace
521 * doesn't use it at all. */
522 start = round_down(start, 128);
523 end = round_up(end, 128);
524
525 drm_clflush_virt_range((void *)start, end - start);
526 } else {
527 drm_clflush_virt_range(addr, length);
528 }
529
530}
531
Daniel Vetterd174bd62012-03-25 19:47:40 +0200532/* Only difference to the fast-path function is that this can handle bit17
533 * and uses non-atomic copy and kmap functions. */
534static int
535shmem_pread_slow(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
542 vaddr = kmap(page);
543 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200544 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
545 page_length,
546 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200547
548 if (page_do_bit17_swizzling)
549 ret = __copy_to_user_swizzled(user_data,
550 vaddr, shmem_page_offset,
551 page_length);
552 else
553 ret = __copy_to_user(user_data,
554 vaddr + shmem_page_offset,
555 page_length);
556 kunmap(page);
557
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100558 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200559}
560
Eric Anholteb014592009-03-10 11:44:52 -0700561static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200562i915_gem_shmem_pread(struct drm_device *dev,
563 struct drm_i915_gem_object *obj,
564 struct drm_i915_gem_pread *args,
565 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700566{
Daniel Vetter8461d222011-12-14 13:57:32 +0100567 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700568 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100569 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100570 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100571 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200572 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200573 int needs_clflush = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200574 struct sg_page_iter sg_iter;
Eric Anholteb014592009-03-10 11:44:52 -0700575
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200576 user_data = to_user_ptr(args->data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700577 remain = args->size;
578
Daniel Vetter8461d222011-12-14 13:57:32 +0100579 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700580
Brad Volkin4c914c02014-02-18 10:15:45 -0800581 ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100582 if (ret)
583 return ret;
584
Eric Anholteb014592009-03-10 11:44:52 -0700585 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100586
Imre Deak67d5a502013-02-18 19:28:02 +0200587 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
588 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200589 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +0100590
591 if (remain <= 0)
592 break;
593
Eric Anholteb014592009-03-10 11:44:52 -0700594 /* Operation in this page
595 *
Eric Anholteb014592009-03-10 11:44:52 -0700596 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700597 * page_length = bytes to copy for this page
598 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100599 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700600 page_length = remain;
601 if ((shmem_page_offset + page_length) > PAGE_SIZE)
602 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700603
Daniel Vetter8461d222011-12-14 13:57:32 +0100604 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
605 (page_to_phys(page) & (1 << 17)) != 0;
606
Daniel Vetterd174bd62012-03-25 19:47:40 +0200607 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
608 user_data, page_do_bit17_swizzling,
609 needs_clflush);
610 if (ret == 0)
611 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700612
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200613 mutex_unlock(&dev->struct_mutex);
614
Jani Nikulad330a952014-01-21 11:24:25 +0200615 if (likely(!i915.prefault_disable) && !prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200616 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200617 /* Userspace is tricking us, but we've already clobbered
618 * its pages with the prefault and promised to write the
619 * data up to the first fault. Hence ignore any errors
620 * and just continue. */
621 (void)ret;
622 prefaulted = 1;
623 }
624
Daniel Vetterd174bd62012-03-25 19:47:40 +0200625 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
626 user_data, page_do_bit17_swizzling,
627 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700628
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200629 mutex_lock(&dev->struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100630
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100631 if (ret)
Daniel Vetter8461d222011-12-14 13:57:32 +0100632 goto out;
Daniel Vetter8461d222011-12-14 13:57:32 +0100633
Chris Wilson17793c92014-03-07 08:30:36 +0000634next_page:
Eric Anholteb014592009-03-10 11:44:52 -0700635 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100636 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700637 offset += page_length;
638 }
639
Chris Wilson4f27b752010-10-14 15:26:45 +0100640out:
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100641 i915_gem_object_unpin_pages(obj);
642
Eric Anholteb014592009-03-10 11:44:52 -0700643 return ret;
644}
645
Eric Anholt673a3942008-07-30 12:06:12 -0700646/**
647 * Reads data from the object referenced by handle.
648 *
649 * On error, the contents of *data are undefined.
650 */
651int
652i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000653 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700654{
655 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000656 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100657 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700658
Chris Wilson51311d02010-11-17 09:10:42 +0000659 if (args->size == 0)
660 return 0;
661
662 if (!access_ok(VERIFY_WRITE,
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200663 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000664 args->size))
665 return -EFAULT;
666
Chris Wilson4f27b752010-10-14 15:26:45 +0100667 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100668 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100669 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700670
Chris Wilson05394f32010-11-08 19:18:58 +0000671 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000672 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100673 ret = -ENOENT;
674 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100675 }
Eric Anholt673a3942008-07-30 12:06:12 -0700676
Chris Wilson7dcd2492010-09-26 20:21:44 +0100677 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000678 if (args->offset > obj->base.size ||
679 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100680 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100681 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100682 }
683
Daniel Vetter1286ff72012-05-10 15:25:09 +0200684 /* prime objects have no backing filp to GEM pread/pwrite
685 * pages from.
686 */
687 if (!obj->base.filp) {
688 ret = -EINVAL;
689 goto out;
690 }
691
Chris Wilsondb53a302011-02-03 11:57:46 +0000692 trace_i915_gem_object_pread(obj, args->offset, args->size);
693
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200694 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700695
Chris Wilson35b62a82010-09-26 20:23:38 +0100696out:
Chris Wilson05394f32010-11-08 19:18:58 +0000697 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100698unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100699 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700700 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700701}
702
Keith Packard0839ccb2008-10-30 19:38:48 -0700703/* This is the fast write path which cannot handle
704 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700705 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700706
Keith Packard0839ccb2008-10-30 19:38:48 -0700707static inline int
708fast_user_write(struct io_mapping *mapping,
709 loff_t page_base, int page_offset,
710 char __user *user_data,
711 int length)
712{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700713 void __iomem *vaddr_atomic;
714 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700715 unsigned long unwritten;
716
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700717 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700718 /* We can use the cpu mem copy function because this is X86. */
719 vaddr = (void __force*)vaddr_atomic + page_offset;
720 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700721 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700722 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100723 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700724}
725
Eric Anholt3de09aa2009-03-09 09:42:23 -0700726/**
727 * This is the fast pwrite path, where we copy the data directly from the
728 * user into the GTT, uncached.
729 */
Eric Anholt673a3942008-07-30 12:06:12 -0700730static int
Chris Wilson05394f32010-11-08 19:18:58 +0000731i915_gem_gtt_pwrite_fast(struct drm_device *dev,
732 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700733 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000734 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700735{
Jani Nikula3e31c6c2014-03-31 14:27:16 +0300736 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700737 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700738 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700739 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200740 int page_offset, page_length, ret;
741
Daniel Vetter1ec9e262014-02-14 14:01:11 +0100742 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200743 if (ret)
744 goto out;
745
746 ret = i915_gem_object_set_to_gtt_domain(obj, true);
747 if (ret)
748 goto out_unpin;
749
750 ret = i915_gem_object_put_fence(obj);
751 if (ret)
752 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700753
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200754 user_data = to_user_ptr(args->data_ptr);
Eric Anholt673a3942008-07-30 12:06:12 -0700755 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700756
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700757 offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700758
759 while (remain > 0) {
760 /* Operation in this page
761 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700762 * page_base = page offset within aperture
763 * page_offset = offset within page
764 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700765 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100766 page_base = offset & PAGE_MASK;
767 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700768 page_length = remain;
769 if ((page_offset + remain) > PAGE_SIZE)
770 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700771
Keith Packard0839ccb2008-10-30 19:38:48 -0700772 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700773 * source page isn't available. Return the error and we'll
774 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700775 */
Ben Widawsky5d4545a2013-01-17 12:45:15 -0800776 if (fast_user_write(dev_priv->gtt.mappable, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200777 page_offset, user_data, page_length)) {
778 ret = -EFAULT;
779 goto out_unpin;
780 }
Eric Anholt673a3942008-07-30 12:06:12 -0700781
Keith Packard0839ccb2008-10-30 19:38:48 -0700782 remain -= page_length;
783 user_data += page_length;
784 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700785 }
Eric Anholt673a3942008-07-30 12:06:12 -0700786
Daniel Vetter935aaa62012-03-25 19:47:35 +0200787out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800788 i915_gem_object_ggtt_unpin(obj);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200789out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700790 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700791}
792
Daniel Vetterd174bd62012-03-25 19:47:40 +0200793/* Per-page copy function for the shmem pwrite fastpath.
794 * Flushes invalid cachelines before writing to the target if
795 * needs_clflush_before is set and flushes out any written cachelines after
796 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700797static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200798shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
799 char __user *user_data,
800 bool page_do_bit17_swizzling,
801 bool needs_clflush_before,
802 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700803{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200804 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700805 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700806
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200807 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200808 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700809
Daniel Vetterd174bd62012-03-25 19:47:40 +0200810 vaddr = kmap_atomic(page);
811 if (needs_clflush_before)
812 drm_clflush_virt_range(vaddr + shmem_page_offset,
813 page_length);
Chris Wilsonc2831a92014-03-07 08:30:37 +0000814 ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
815 user_data, page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200816 if (needs_clflush_after)
817 drm_clflush_virt_range(vaddr + shmem_page_offset,
818 page_length);
819 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700820
Chris Wilson755d2212012-09-04 21:02:55 +0100821 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700822}
823
Daniel Vetterd174bd62012-03-25 19:47:40 +0200824/* Only difference to the fast-path function is that this can handle bit17
825 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700826static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200827shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
828 char __user *user_data,
829 bool page_do_bit17_swizzling,
830 bool needs_clflush_before,
831 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700832{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200833 char *vaddr;
834 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700835
Daniel Vetterd174bd62012-03-25 19:47:40 +0200836 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200837 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200838 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
839 page_length,
840 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200841 if (page_do_bit17_swizzling)
842 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100843 user_data,
844 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200845 else
846 ret = __copy_from_user(vaddr + shmem_page_offset,
847 user_data,
848 page_length);
849 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200850 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
851 page_length,
852 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200853 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100854
Chris Wilson755d2212012-09-04 21:02:55 +0100855 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700856}
857
Eric Anholt40123c12009-03-09 13:42:30 -0700858static int
Daniel Vettere244a442012-03-25 19:47:28 +0200859i915_gem_shmem_pwrite(struct drm_device *dev,
860 struct drm_i915_gem_object *obj,
861 struct drm_i915_gem_pwrite *args,
862 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700863{
Eric Anholt40123c12009-03-09 13:42:30 -0700864 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100865 loff_t offset;
866 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100867 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100868 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200869 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200870 int needs_clflush_after = 0;
871 int needs_clflush_before = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200872 struct sg_page_iter sg_iter;
Eric Anholt40123c12009-03-09 13:42:30 -0700873
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200874 user_data = to_user_ptr(args->data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700875 remain = args->size;
876
Daniel Vetter8c599672011-12-14 13:57:31 +0100877 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700878
Daniel Vetter58642882012-03-25 19:47:37 +0200879 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
880 /* If we're not in the cpu write domain, set ourself into the gtt
881 * write domain and manually flush cachelines (if required). This
882 * optimizes for the case when the gpu will use the data
883 * right away and we therefore have to clflush anyway. */
Chris Wilson2c225692013-08-09 12:26:45 +0100884 needs_clflush_after = cpu_write_needs_clflush(obj);
Ben Widawsky23f54482013-09-11 14:57:48 -0700885 ret = i915_gem_object_wait_rendering(obj, false);
886 if (ret)
887 return ret;
Daniel Vetter58642882012-03-25 19:47:37 +0200888 }
Chris Wilsonc76ce032013-08-08 14:41:03 +0100889 /* Same trick applies to invalidate partially written cachelines read
890 * before writing. */
891 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
892 needs_clflush_before =
893 !cpu_cache_is_coherent(dev, obj->cache_level);
Daniel Vetter58642882012-03-25 19:47:37 +0200894
Chris Wilson755d2212012-09-04 21:02:55 +0100895 ret = i915_gem_object_get_pages(obj);
896 if (ret)
897 return ret;
898
899 i915_gem_object_pin_pages(obj);
900
Eric Anholt40123c12009-03-09 13:42:30 -0700901 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000902 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700903
Imre Deak67d5a502013-02-18 19:28:02 +0200904 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
905 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200906 struct page *page = sg_page_iter_page(&sg_iter);
Daniel Vetter58642882012-03-25 19:47:37 +0200907 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100908
Chris Wilson9da3da62012-06-01 15:20:22 +0100909 if (remain <= 0)
910 break;
911
Eric Anholt40123c12009-03-09 13:42:30 -0700912 /* Operation in this page
913 *
Eric Anholt40123c12009-03-09 13:42:30 -0700914 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700915 * page_length = bytes to copy for this page
916 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100917 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700918
919 page_length = remain;
920 if ((shmem_page_offset + page_length) > PAGE_SIZE)
921 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700922
Daniel Vetter58642882012-03-25 19:47:37 +0200923 /* If we don't overwrite a cacheline completely we need to be
924 * careful to have up-to-date data by first clflushing. Don't
925 * overcomplicate things and flush the entire patch. */
926 partial_cacheline_write = needs_clflush_before &&
927 ((shmem_page_offset | page_length)
928 & (boot_cpu_data.x86_clflush_size - 1));
929
Daniel Vetter8c599672011-12-14 13:57:31 +0100930 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
931 (page_to_phys(page) & (1 << 17)) != 0;
932
Daniel Vetterd174bd62012-03-25 19:47:40 +0200933 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
934 user_data, page_do_bit17_swizzling,
935 partial_cacheline_write,
936 needs_clflush_after);
937 if (ret == 0)
938 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700939
Daniel Vettere244a442012-03-25 19:47:28 +0200940 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +0200941 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200942 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
943 user_data, page_do_bit17_swizzling,
944 partial_cacheline_write,
945 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700946
Daniel Vettere244a442012-03-25 19:47:28 +0200947 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +0100948
Chris Wilson755d2212012-09-04 21:02:55 +0100949 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +0100950 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +0100951
Chris Wilson17793c92014-03-07 08:30:36 +0000952next_page:
Eric Anholt40123c12009-03-09 13:42:30 -0700953 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100954 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700955 offset += page_length;
956 }
957
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100958out:
Chris Wilson755d2212012-09-04 21:02:55 +0100959 i915_gem_object_unpin_pages(obj);
960
Daniel Vettere244a442012-03-25 19:47:28 +0200961 if (hit_slowpath) {
Daniel Vetter8dcf0152012-11-15 16:53:58 +0100962 /*
963 * Fixup: Flush cpu caches in case we didn't flush the dirty
964 * cachelines in-line while writing and the object moved
965 * out of the cpu write domain while we've dropped the lock.
966 */
967 if (!needs_clflush_after &&
968 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilson000433b2013-08-08 14:41:09 +0100969 if (i915_gem_clflush_object(obj, obj->pin_display))
970 i915_gem_chipset_flush(dev);
Daniel Vettere244a442012-03-25 19:47:28 +0200971 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100972 }
Eric Anholt40123c12009-03-09 13:42:30 -0700973
Daniel Vetter58642882012-03-25 19:47:37 +0200974 if (needs_clflush_after)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800975 i915_gem_chipset_flush(dev);
Daniel Vetter58642882012-03-25 19:47:37 +0200976
Eric Anholt40123c12009-03-09 13:42:30 -0700977 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700978}
979
980/**
981 * Writes data to the object referenced by handle.
982 *
983 * On error, the contents of the buffer that were to be modified are undefined.
984 */
985int
986i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100987 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700988{
989 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000990 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000991 int ret;
992
993 if (args->size == 0)
994 return 0;
995
996 if (!access_ok(VERIFY_READ,
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200997 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000998 args->size))
999 return -EFAULT;
1000
Jani Nikulad330a952014-01-21 11:24:25 +02001001 if (likely(!i915.prefault_disable)) {
Xiong Zhang0b74b502013-07-19 13:51:24 +08001002 ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
1003 args->size);
1004 if (ret)
1005 return -EFAULT;
1006 }
Eric Anholt673a3942008-07-30 12:06:12 -07001007
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001008 ret = i915_mutex_lock_interruptible(dev);
1009 if (ret)
1010 return ret;
1011
Chris Wilson05394f32010-11-08 19:18:58 +00001012 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001013 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001014 ret = -ENOENT;
1015 goto unlock;
1016 }
Eric Anholt673a3942008-07-30 12:06:12 -07001017
Chris Wilson7dcd2492010-09-26 20:21:44 +01001018 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +00001019 if (args->offset > obj->base.size ||
1020 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +01001021 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +01001022 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +01001023 }
1024
Daniel Vetter1286ff72012-05-10 15:25:09 +02001025 /* prime objects have no backing filp to GEM pread/pwrite
1026 * pages from.
1027 */
1028 if (!obj->base.filp) {
1029 ret = -EINVAL;
1030 goto out;
1031 }
1032
Chris Wilsondb53a302011-02-03 11:57:46 +00001033 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
1034
Daniel Vetter935aaa62012-03-25 19:47:35 +02001035 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -07001036 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1037 * it would end up going through the fenced access, and we'll get
1038 * different detiling behavior between reading and writing.
1039 * pread/pwrite currently are reading and writing from the CPU
1040 * perspective, requiring manual detiling by the client.
1041 */
Chris Wilson00731152014-05-21 12:42:56 +01001042 if (obj->phys_handle) {
1043 ret = i915_gem_phys_pwrite(obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001044 goto out;
1045 }
1046
Chris Wilson2c225692013-08-09 12:26:45 +01001047 if (obj->tiling_mode == I915_TILING_NONE &&
1048 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
1049 cpu_write_needs_clflush(obj)) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001050 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +02001051 /* Note that the gtt paths might fail with non-page-backed user
1052 * pointers (e.g. gtt mappings when moving data between
1053 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -07001054 }
Eric Anholt673a3942008-07-30 12:06:12 -07001055
Chris Wilson86a1ee22012-08-11 15:41:04 +01001056 if (ret == -EFAULT || ret == -ENOSPC)
Daniel Vetter935aaa62012-03-25 19:47:35 +02001057 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +01001058
Chris Wilson35b62a82010-09-26 20:23:38 +01001059out:
Chris Wilson05394f32010-11-08 19:18:58 +00001060 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001061unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +01001062 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07001063 return ret;
1064}
1065
Chris Wilsonb3612372012-08-24 09:35:08 +01001066int
Daniel Vetter33196de2012-11-14 17:14:05 +01001067i915_gem_check_wedge(struct i915_gpu_error *error,
Chris Wilsonb3612372012-08-24 09:35:08 +01001068 bool interruptible)
1069{
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001070 if (i915_reset_in_progress(error)) {
Chris Wilsonb3612372012-08-24 09:35:08 +01001071 /* Non-interruptible callers can't handle -EAGAIN, hence return
1072 * -EIO unconditionally for these. */
1073 if (!interruptible)
1074 return -EIO;
1075
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001076 /* Recovery complete, but the reset failed ... */
1077 if (i915_terminally_wedged(error))
Chris Wilsonb3612372012-08-24 09:35:08 +01001078 return -EIO;
1079
1080 return -EAGAIN;
1081 }
1082
1083 return 0;
1084}
1085
1086/*
1087 * Compare seqno against outstanding lazy request. Emit a request if they are
1088 * equal.
1089 */
1090static int
1091i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
1092{
1093 int ret;
1094
1095 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
1096
1097 ret = 0;
Chris Wilson18235212013-09-04 10:45:51 +01001098 if (seqno == ring->outstanding_lazy_seqno)
Mika Kuoppala0025c072013-06-12 12:35:30 +03001099 ret = i915_add_request(ring, NULL);
Chris Wilsonb3612372012-08-24 09:35:08 +01001100
1101 return ret;
1102}
1103
Chris Wilson094f9a52013-09-25 17:34:55 +01001104static void fake_irq(unsigned long data)
1105{
1106 wake_up_process((struct task_struct *)data);
1107}
1108
1109static bool missed_irq(struct drm_i915_private *dev_priv,
1110 struct intel_ring_buffer *ring)
1111{
1112 return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
1113}
1114
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001115static bool can_wait_boost(struct drm_i915_file_private *file_priv)
1116{
1117 if (file_priv == NULL)
1118 return true;
1119
1120 return !atomic_xchg(&file_priv->rps_wait_boost, true);
1121}
1122
Chris Wilsonb3612372012-08-24 09:35:08 +01001123/**
1124 * __wait_seqno - wait until execution of seqno has finished
1125 * @ring: the ring expected to report seqno
1126 * @seqno: duh!
Daniel Vetterf69061b2012-12-06 09:01:42 +01001127 * @reset_counter: reset sequence associated with the given seqno
Chris Wilsonb3612372012-08-24 09:35:08 +01001128 * @interruptible: do an interruptible wait (normally yes)
1129 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1130 *
Daniel Vetterf69061b2012-12-06 09:01:42 +01001131 * Note: It is of utmost importance that the passed in seqno and reset_counter
1132 * values have been read by the caller in an smp safe manner. Where read-side
1133 * locks are involved, it is sufficient to read the reset_counter before
1134 * unlocking the lock that protects the seqno. For lockless tricks, the
1135 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
1136 * inserted.
1137 *
Chris Wilsonb3612372012-08-24 09:35:08 +01001138 * Returns 0 if the seqno was found within the alloted time. Else returns the
1139 * errno with remaining time filled in timeout argument.
1140 */
1141static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
Daniel Vetterf69061b2012-12-06 09:01:42 +01001142 unsigned reset_counter,
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001143 bool interruptible,
1144 struct timespec *timeout,
1145 struct drm_i915_file_private *file_priv)
Chris Wilsonb3612372012-08-24 09:35:08 +01001146{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001147 struct drm_device *dev = ring->dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03001148 struct drm_i915_private *dev_priv = dev->dev_private;
Mika Kuoppala168c3f22013-12-12 17:54:42 +02001149 const bool irq_test_in_progress =
1150 ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
Chris Wilson094f9a52013-09-25 17:34:55 +01001151 struct timespec before, now;
1152 DEFINE_WAIT(wait);
Mika Kuoppala47e97662013-12-10 17:02:43 +02001153 unsigned long timeout_expire;
Chris Wilsonb3612372012-08-24 09:35:08 +01001154 int ret;
1155
Paulo Zanoni5d584b22014-03-07 20:08:15 -03001156 WARN(dev_priv->pm.irqs_disabled, "IRQs disabled\n");
Paulo Zanonic67a4702013-08-19 13:18:09 -03001157
Chris Wilsonb3612372012-08-24 09:35:08 +01001158 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
1159 return 0;
1160
Mika Kuoppala47e97662013-12-10 17:02:43 +02001161 timeout_expire = timeout ? jiffies + timespec_to_jiffies_timeout(timeout) : 0;
Chris Wilsonb3612372012-08-24 09:35:08 +01001162
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001163 if (INTEL_INFO(dev)->gen >= 6 && can_wait_boost(file_priv)) {
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001164 gen6_rps_boost(dev_priv);
1165 if (file_priv)
1166 mod_delayed_work(dev_priv->wq,
1167 &file_priv->mm.idle_work,
1168 msecs_to_jiffies(100));
1169 }
1170
Mika Kuoppala168c3f22013-12-12 17:54:42 +02001171 if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring)))
Chris Wilsonb3612372012-08-24 09:35:08 +01001172 return -ENODEV;
1173
Chris Wilson094f9a52013-09-25 17:34:55 +01001174 /* Record current time in case interrupted by signal, or wedged */
1175 trace_i915_gem_request_wait_begin(ring, seqno);
Chris Wilsonb3612372012-08-24 09:35:08 +01001176 getrawmonotonic(&before);
Chris Wilson094f9a52013-09-25 17:34:55 +01001177 for (;;) {
1178 struct timer_list timer;
Chris Wilsonb3612372012-08-24 09:35:08 +01001179
Chris Wilson094f9a52013-09-25 17:34:55 +01001180 prepare_to_wait(&ring->irq_queue, &wait,
1181 interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
Chris Wilsonb3612372012-08-24 09:35:08 +01001182
Daniel Vetterf69061b2012-12-06 09:01:42 +01001183 /* We need to check whether any gpu reset happened in between
1184 * the caller grabbing the seqno and now ... */
Chris Wilson094f9a52013-09-25 17:34:55 +01001185 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
1186 /* ... but upgrade the -EAGAIN to an -EIO if the gpu
1187 * is truely gone. */
1188 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1189 if (ret == 0)
1190 ret = -EAGAIN;
1191 break;
1192 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01001193
Chris Wilson094f9a52013-09-25 17:34:55 +01001194 if (i915_seqno_passed(ring->get_seqno(ring, false), seqno)) {
1195 ret = 0;
1196 break;
1197 }
Chris Wilsonb3612372012-08-24 09:35:08 +01001198
Chris Wilson094f9a52013-09-25 17:34:55 +01001199 if (interruptible && signal_pending(current)) {
1200 ret = -ERESTARTSYS;
1201 break;
1202 }
1203
Mika Kuoppala47e97662013-12-10 17:02:43 +02001204 if (timeout && time_after_eq(jiffies, timeout_expire)) {
Chris Wilson094f9a52013-09-25 17:34:55 +01001205 ret = -ETIME;
1206 break;
1207 }
1208
1209 timer.function = NULL;
1210 if (timeout || missed_irq(dev_priv, ring)) {
Mika Kuoppala47e97662013-12-10 17:02:43 +02001211 unsigned long expire;
1212
Chris Wilson094f9a52013-09-25 17:34:55 +01001213 setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
Mika Kuoppala47e97662013-12-10 17:02:43 +02001214 expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
Chris Wilson094f9a52013-09-25 17:34:55 +01001215 mod_timer(&timer, expire);
1216 }
1217
Chris Wilson5035c272013-10-04 09:58:46 +01001218 io_schedule();
Chris Wilson094f9a52013-09-25 17:34:55 +01001219
Chris Wilson094f9a52013-09-25 17:34:55 +01001220 if (timer.function) {
1221 del_singleshot_timer_sync(&timer);
1222 destroy_timer_on_stack(&timer);
1223 }
1224 }
Chris Wilsonb3612372012-08-24 09:35:08 +01001225 getrawmonotonic(&now);
Chris Wilson094f9a52013-09-25 17:34:55 +01001226 trace_i915_gem_request_wait_end(ring, seqno);
Chris Wilsonb3612372012-08-24 09:35:08 +01001227
Mika Kuoppala168c3f22013-12-12 17:54:42 +02001228 if (!irq_test_in_progress)
1229 ring->irq_put(ring);
Chris Wilson094f9a52013-09-25 17:34:55 +01001230
1231 finish_wait(&ring->irq_queue, &wait);
Chris Wilsonb3612372012-08-24 09:35:08 +01001232
1233 if (timeout) {
1234 struct timespec sleep_time = timespec_sub(now, before);
1235 *timeout = timespec_sub(*timeout, sleep_time);
Chris Wilson4f42f4e2013-04-26 16:22:46 +03001236 if (!timespec_valid(timeout)) /* i.e. negative time remains */
1237 set_normalized_timespec(timeout, 0, 0);
Chris Wilsonb3612372012-08-24 09:35:08 +01001238 }
1239
Chris Wilson094f9a52013-09-25 17:34:55 +01001240 return ret;
Chris Wilsonb3612372012-08-24 09:35:08 +01001241}
1242
1243/**
1244 * Waits for a sequence number to be signaled, and cleans up the
1245 * request and object lists appropriately for that event.
1246 */
1247int
1248i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
1249{
1250 struct drm_device *dev = ring->dev;
1251 struct drm_i915_private *dev_priv = dev->dev_private;
1252 bool interruptible = dev_priv->mm.interruptible;
1253 int ret;
1254
1255 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1256 BUG_ON(seqno == 0);
1257
Daniel Vetter33196de2012-11-14 17:14:05 +01001258 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
Chris Wilsonb3612372012-08-24 09:35:08 +01001259 if (ret)
1260 return ret;
1261
1262 ret = i915_gem_check_olr(ring, seqno);
1263 if (ret)
1264 return ret;
1265
Daniel Vetterf69061b2012-12-06 09:01:42 +01001266 return __wait_seqno(ring, seqno,
1267 atomic_read(&dev_priv->gpu_error.reset_counter),
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001268 interruptible, NULL, NULL);
Chris Wilsonb3612372012-08-24 09:35:08 +01001269}
1270
Chris Wilsond26e3af2013-06-29 22:05:26 +01001271static int
1272i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
1273 struct intel_ring_buffer *ring)
1274{
1275 i915_gem_retire_requests_ring(ring);
1276
1277 /* Manually manage the write flush as we may have not yet
1278 * retired the buffer.
1279 *
1280 * Note that the last_write_seqno is always the earlier of
1281 * the two (read/write) seqno, so if we haved successfully waited,
1282 * we know we have passed the last write.
1283 */
1284 obj->last_write_seqno = 0;
1285 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1286
1287 return 0;
1288}
1289
Chris Wilsonb3612372012-08-24 09:35:08 +01001290/**
1291 * Ensures that all rendering to the object has completed and the object is
1292 * safe to unbind from the GTT or access from the CPU.
1293 */
1294static __must_check int
1295i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1296 bool readonly)
1297{
1298 struct intel_ring_buffer *ring = obj->ring;
1299 u32 seqno;
1300 int ret;
1301
1302 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1303 if (seqno == 0)
1304 return 0;
1305
1306 ret = i915_wait_seqno(ring, seqno);
1307 if (ret)
1308 return ret;
1309
Chris Wilsond26e3af2013-06-29 22:05:26 +01001310 return i915_gem_object_wait_rendering__tail(obj, ring);
Chris Wilsonb3612372012-08-24 09:35:08 +01001311}
1312
Chris Wilson3236f572012-08-24 09:35:09 +01001313/* A nonblocking variant of the above wait. This is a highly dangerous routine
1314 * as the object state may change during this call.
1315 */
1316static __must_check int
1317i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
Chris Wilson6e4930f2014-02-07 18:37:06 -02001318 struct drm_i915_file_private *file_priv,
Chris Wilson3236f572012-08-24 09:35:09 +01001319 bool readonly)
1320{
1321 struct drm_device *dev = obj->base.dev;
1322 struct drm_i915_private *dev_priv = dev->dev_private;
1323 struct intel_ring_buffer *ring = obj->ring;
Daniel Vetterf69061b2012-12-06 09:01:42 +01001324 unsigned reset_counter;
Chris Wilson3236f572012-08-24 09:35:09 +01001325 u32 seqno;
1326 int ret;
1327
1328 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1329 BUG_ON(!dev_priv->mm.interruptible);
1330
1331 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1332 if (seqno == 0)
1333 return 0;
1334
Daniel Vetter33196de2012-11-14 17:14:05 +01001335 ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
Chris Wilson3236f572012-08-24 09:35:09 +01001336 if (ret)
1337 return ret;
1338
1339 ret = i915_gem_check_olr(ring, seqno);
1340 if (ret)
1341 return ret;
1342
Daniel Vetterf69061b2012-12-06 09:01:42 +01001343 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilson3236f572012-08-24 09:35:09 +01001344 mutex_unlock(&dev->struct_mutex);
Chris Wilson6e4930f2014-02-07 18:37:06 -02001345 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL, file_priv);
Chris Wilson3236f572012-08-24 09:35:09 +01001346 mutex_lock(&dev->struct_mutex);
Chris Wilsond26e3af2013-06-29 22:05:26 +01001347 if (ret)
1348 return ret;
Chris Wilson3236f572012-08-24 09:35:09 +01001349
Chris Wilsond26e3af2013-06-29 22:05:26 +01001350 return i915_gem_object_wait_rendering__tail(obj, ring);
Chris Wilson3236f572012-08-24 09:35:09 +01001351}
1352
Eric Anholt673a3942008-07-30 12:06:12 -07001353/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001354 * Called when user space prepares to use an object with the CPU, either
1355 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001356 */
1357int
1358i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001359 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001360{
1361 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001362 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001363 uint32_t read_domains = args->read_domains;
1364 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001365 int ret;
1366
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001367 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001368 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001369 return -EINVAL;
1370
Chris Wilson21d509e2009-06-06 09:46:02 +01001371 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001372 return -EINVAL;
1373
1374 /* Having something in the write domain implies it's in the read
1375 * domain, and only that read domain. Enforce that in the request.
1376 */
1377 if (write_domain != 0 && read_domains != write_domain)
1378 return -EINVAL;
1379
Chris Wilson76c1dec2010-09-25 11:22:51 +01001380 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001381 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001382 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001383
Chris Wilson05394f32010-11-08 19:18:58 +00001384 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001385 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001386 ret = -ENOENT;
1387 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001388 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001389
Chris Wilson3236f572012-08-24 09:35:09 +01001390 /* Try to flush the object off the GPU without holding the lock.
1391 * We will repeat the flush holding the lock in the normal manner
1392 * to catch cases where we are gazumped.
1393 */
Chris Wilson6e4930f2014-02-07 18:37:06 -02001394 ret = i915_gem_object_wait_rendering__nonblocking(obj,
1395 file->driver_priv,
1396 !write_domain);
Chris Wilson3236f572012-08-24 09:35:09 +01001397 if (ret)
1398 goto unref;
1399
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001400 if (read_domains & I915_GEM_DOMAIN_GTT) {
1401 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001402
1403 /* Silently promote "you're not bound, there was nothing to do"
1404 * to success, since the client was just asking us to
1405 * make sure everything was done.
1406 */
1407 if (ret == -EINVAL)
1408 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001409 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001410 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001411 }
1412
Chris Wilson3236f572012-08-24 09:35:09 +01001413unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001414 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001415unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001416 mutex_unlock(&dev->struct_mutex);
1417 return ret;
1418}
1419
1420/**
1421 * Called when user space has done writes to this buffer
1422 */
1423int
1424i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001425 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001426{
1427 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001428 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001429 int ret = 0;
1430
Chris Wilson76c1dec2010-09-25 11:22:51 +01001431 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001432 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001433 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001434
Chris Wilson05394f32010-11-08 19:18:58 +00001435 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001436 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001437 ret = -ENOENT;
1438 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001439 }
1440
Eric Anholt673a3942008-07-30 12:06:12 -07001441 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson2c225692013-08-09 12:26:45 +01001442 if (obj->pin_display)
1443 i915_gem_object_flush_cpu_write_domain(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08001444
Chris Wilson05394f32010-11-08 19:18:58 +00001445 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001446unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001447 mutex_unlock(&dev->struct_mutex);
1448 return ret;
1449}
1450
1451/**
1452 * Maps the contents of an object, returning the address it is mapped
1453 * into.
1454 *
1455 * While the mapping holds a reference on the contents of the object, it doesn't
1456 * imply a ref on the object itself.
1457 */
1458int
1459i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001460 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001461{
1462 struct drm_i915_gem_mmap *args = data;
1463 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001464 unsigned long addr;
1465
Chris Wilson05394f32010-11-08 19:18:58 +00001466 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001467 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001468 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001469
Daniel Vetter1286ff72012-05-10 15:25:09 +02001470 /* prime objects have no backing filp to GEM mmap
1471 * pages from.
1472 */
1473 if (!obj->filp) {
1474 drm_gem_object_unreference_unlocked(obj);
1475 return -EINVAL;
1476 }
1477
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001478 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001479 PROT_READ | PROT_WRITE, MAP_SHARED,
1480 args->offset);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001481 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001482 if (IS_ERR((void *)addr))
1483 return addr;
1484
1485 args->addr_ptr = (uint64_t) addr;
1486
1487 return 0;
1488}
1489
Jesse Barnesde151cf2008-11-12 10:03:55 -08001490/**
1491 * i915_gem_fault - fault a page into the GTT
1492 * vma: VMA in question
1493 * vmf: fault info
1494 *
1495 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1496 * from userspace. The fault handler takes care of binding the object to
1497 * the GTT (if needed), allocating and programming a fence register (again,
1498 * only if needed based on whether the old reg is still valid or the object
1499 * is tiled) and inserting a new PTE into the faulting process.
1500 *
1501 * Note that the faulting process may involve evicting existing objects
1502 * from the GTT and/or fence registers to make room. So performance may
1503 * suffer if the GTT working set is large or there are few fence registers
1504 * left.
1505 */
1506int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1507{
Chris Wilson05394f32010-11-08 19:18:58 +00001508 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1509 struct drm_device *dev = obj->base.dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03001510 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001511 pgoff_t page_offset;
1512 unsigned long pfn;
1513 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001514 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001515
Paulo Zanonif65c9162013-11-27 18:20:34 -02001516 intel_runtime_pm_get(dev_priv);
1517
Jesse Barnesde151cf2008-11-12 10:03:55 -08001518 /* We don't use vmf->pgoff since that has the fake offset */
1519 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1520 PAGE_SHIFT;
1521
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001522 ret = i915_mutex_lock_interruptible(dev);
1523 if (ret)
1524 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001525
Chris Wilsondb53a302011-02-03 11:57:46 +00001526 trace_i915_gem_object_fault(obj, page_offset, true, write);
1527
Chris Wilson6e4930f2014-02-07 18:37:06 -02001528 /* Try to flush the object off the GPU first without holding the lock.
1529 * Upon reacquiring the lock, we will perform our sanity checks and then
1530 * repeat the flush holding the lock in the normal manner to catch cases
1531 * where we are gazumped.
1532 */
1533 ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
1534 if (ret)
1535 goto unlock;
1536
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001537 /* Access to snoopable pages through the GTT is incoherent. */
1538 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
1539 ret = -EINVAL;
1540 goto unlock;
1541 }
1542
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001543 /* Now bind it into the GTT if needed */
Daniel Vetter1ec9e262014-02-14 14:01:11 +01001544 ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001545 if (ret)
1546 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001547
Chris Wilsonc9839302012-11-20 10:45:17 +00001548 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1549 if (ret)
1550 goto unpin;
1551
1552 ret = i915_gem_object_get_fence(obj);
1553 if (ret)
1554 goto unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001555
Chris Wilson6299f992010-11-24 12:23:44 +00001556 obj->fault_mappable = true;
1557
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001558 pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
1559 pfn >>= PAGE_SHIFT;
1560 pfn += page_offset;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001561
1562 /* Finally, remap it using the new GTT offset */
1563 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc9839302012-11-20 10:45:17 +00001564unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001565 i915_gem_object_ggtt_unpin(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001566unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001567 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001568out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001569 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001570 case -EIO:
Daniel Vettera9340cc2012-07-04 22:18:42 +02001571 /* If this -EIO is due to a gpu hang, give the reset code a
1572 * chance to clean up the mess. Otherwise return the proper
1573 * SIGBUS. */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001574 if (i915_terminally_wedged(&dev_priv->gpu_error)) {
1575 ret = VM_FAULT_SIGBUS;
1576 break;
1577 }
Chris Wilson045e7692010-11-07 09:18:22 +00001578 case -EAGAIN:
Daniel Vetter571c6082013-09-12 17:57:28 +02001579 /*
1580 * EAGAIN means the gpu is hung and we'll wait for the error
1581 * handler to reset everything when re-faulting in
1582 * i915_mutex_lock_interruptible.
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001583 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001584 case 0:
1585 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001586 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001587 case -EBUSY:
1588 /*
1589 * EBUSY is ok: this just means that another thread
1590 * already did the job.
1591 */
Paulo Zanonif65c9162013-11-27 18:20:34 -02001592 ret = VM_FAULT_NOPAGE;
1593 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001594 case -ENOMEM:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001595 ret = VM_FAULT_OOM;
1596 break;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001597 case -ENOSPC:
Chris Wilson45d67812014-01-31 11:34:57 +00001598 case -EFAULT:
Paulo Zanonif65c9162013-11-27 18:20:34 -02001599 ret = VM_FAULT_SIGBUS;
1600 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001601 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001602 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Paulo Zanonif65c9162013-11-27 18:20:34 -02001603 ret = VM_FAULT_SIGBUS;
1604 break;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001605 }
Paulo Zanonif65c9162013-11-27 18:20:34 -02001606
1607 intel_runtime_pm_put(dev_priv);
1608 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001609}
1610
Paulo Zanoni48018a52013-12-13 15:22:31 -02001611void i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
1612{
1613 struct i915_vma *vma;
1614
1615 /*
1616 * Only the global gtt is relevant for gtt memory mappings, so restrict
1617 * list traversal to objects bound into the global address space. Note
1618 * that the active list should be empty, but better safe than sorry.
1619 */
1620 WARN_ON(!list_empty(&dev_priv->gtt.base.active_list));
1621 list_for_each_entry(vma, &dev_priv->gtt.base.active_list, mm_list)
1622 i915_gem_release_mmap(vma->obj);
1623 list_for_each_entry(vma, &dev_priv->gtt.base.inactive_list, mm_list)
1624 i915_gem_release_mmap(vma->obj);
1625}
1626
Jesse Barnesde151cf2008-11-12 10:03:55 -08001627/**
Chris Wilson901782b2009-07-10 08:18:50 +01001628 * i915_gem_release_mmap - remove physical page mappings
1629 * @obj: obj in question
1630 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001631 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001632 * relinquish ownership of the pages back to the system.
1633 *
1634 * It is vital that we remove the page mapping if we have mapped a tiled
1635 * object through the GTT and then lose the fence register due to
1636 * resource pressure. Similarly if the object has been moved out of the
1637 * aperture, than pages mapped into userspace must be revoked. Removing the
1638 * mapping will then trigger a page fault on the next user access, allowing
1639 * fixup by i915_gem_fault().
1640 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001641void
Chris Wilson05394f32010-11-08 19:18:58 +00001642i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001643{
Chris Wilson6299f992010-11-24 12:23:44 +00001644 if (!obj->fault_mappable)
1645 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001646
David Herrmann6796cb12014-01-03 14:24:19 +01001647 drm_vma_node_unmap(&obj->base.vma_node,
1648 obj->base.dev->anon_inode->i_mapping);
Chris Wilson6299f992010-11-24 12:23:44 +00001649 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001650}
1651
Imre Deak0fa87792013-01-07 21:47:35 +02001652uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001653i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001654{
Chris Wilsone28f8712011-07-18 13:11:49 -07001655 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001656
1657 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001658 tiling_mode == I915_TILING_NONE)
1659 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001660
1661 /* Previous chips need a power-of-two fence region when tiling */
1662 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001663 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001664 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001665 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001666
Chris Wilsone28f8712011-07-18 13:11:49 -07001667 while (gtt_size < size)
1668 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001669
Chris Wilsone28f8712011-07-18 13:11:49 -07001670 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001671}
1672
Jesse Barnesde151cf2008-11-12 10:03:55 -08001673/**
1674 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1675 * @obj: object to check
1676 *
1677 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001678 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001679 */
Imre Deakd865110c2013-01-07 21:47:33 +02001680uint32_t
1681i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1682 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001683{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001684 /*
1685 * Minimum alignment is 4k (GTT page size), but might be greater
1686 * if a fence register is needed for the object.
1687 */
Imre Deakd865110c2013-01-07 21:47:33 +02001688 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001689 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001690 return 4096;
1691
1692 /*
1693 * Previous chips need to be aligned to the size of the smallest
1694 * fence register that can contain the object.
1695 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001696 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001697}
1698
Chris Wilsond8cb5082012-08-11 15:41:03 +01001699static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1700{
1701 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1702 int ret;
1703
David Herrmann0de23972013-07-24 21:07:52 +02001704 if (drm_vma_node_has_offset(&obj->base.vma_node))
Chris Wilsond8cb5082012-08-11 15:41:03 +01001705 return 0;
1706
Daniel Vetterda494d72012-12-20 15:11:16 +01001707 dev_priv->mm.shrinker_no_lock_stealing = true;
1708
Chris Wilsond8cb5082012-08-11 15:41:03 +01001709 ret = drm_gem_create_mmap_offset(&obj->base);
1710 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001711 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001712
1713 /* Badly fragmented mmap space? The only way we can recover
1714 * space is by destroying unwanted objects. We can't randomly release
1715 * mmap_offsets as userspace expects them to be persistent for the
1716 * lifetime of the objects. The closest we can is to release the
1717 * offsets on purgeable objects by truncating it and marking it purged,
1718 * which prevents userspace from ever using that object again.
1719 */
1720 i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
1721 ret = drm_gem_create_mmap_offset(&obj->base);
1722 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001723 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001724
1725 i915_gem_shrink_all(dev_priv);
Daniel Vetterda494d72012-12-20 15:11:16 +01001726 ret = drm_gem_create_mmap_offset(&obj->base);
1727out:
1728 dev_priv->mm.shrinker_no_lock_stealing = false;
1729
1730 return ret;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001731}
1732
1733static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1734{
Chris Wilsond8cb5082012-08-11 15:41:03 +01001735 drm_gem_free_mmap_offset(&obj->base);
1736}
1737
Jesse Barnesde151cf2008-11-12 10:03:55 -08001738int
Dave Airlieff72145b2011-02-07 12:16:14 +10001739i915_gem_mmap_gtt(struct drm_file *file,
1740 struct drm_device *dev,
1741 uint32_t handle,
1742 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001743{
Chris Wilsonda761a62010-10-27 17:37:08 +01001744 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001745 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001746 int ret;
1747
Chris Wilson76c1dec2010-09-25 11:22:51 +01001748 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001749 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001750 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001751
Dave Airlieff72145b2011-02-07 12:16:14 +10001752 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001753 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001754 ret = -ENOENT;
1755 goto unlock;
1756 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001757
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001758 if (obj->base.size > dev_priv->gtt.mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001759 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001760 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001761 }
1762
Chris Wilson05394f32010-11-08 19:18:58 +00001763 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00001764 DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00001765 ret = -EFAULT;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001766 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001767 }
1768
Chris Wilsond8cb5082012-08-11 15:41:03 +01001769 ret = i915_gem_object_create_mmap_offset(obj);
1770 if (ret)
1771 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001772
David Herrmann0de23972013-07-24 21:07:52 +02001773 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001774
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001775out:
Chris Wilson05394f32010-11-08 19:18:58 +00001776 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001777unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001778 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001779 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001780}
1781
Dave Airlieff72145b2011-02-07 12:16:14 +10001782/**
1783 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1784 * @dev: DRM device
1785 * @data: GTT mapping ioctl data
1786 * @file: GEM object info
1787 *
1788 * Simply returns the fake offset to userspace so it can mmap it.
1789 * The mmap call will end up in drm_gem_mmap(), which will set things
1790 * up so we can get faults in the handler above.
1791 *
1792 * The fault handler will take care of binding the object into the GTT
1793 * (since it may have been evicted to make room for something), allocating
1794 * a fence register, and mapping the appropriate aperture address into
1795 * userspace.
1796 */
1797int
1798i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1799 struct drm_file *file)
1800{
1801 struct drm_i915_gem_mmap_gtt *args = data;
1802
Dave Airlieff72145b2011-02-07 12:16:14 +10001803 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1804}
1805
Daniel Vetter225067e2012-08-20 10:23:20 +02001806/* Immediately discard the backing storage */
1807static void
1808i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001809{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001810 struct inode *inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001811
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001812 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001813
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001814 if (obj->base.filp == NULL)
1815 return;
1816
Daniel Vetter225067e2012-08-20 10:23:20 +02001817 /* Our goal here is to return as much of the memory as
1818 * is possible back to the system as we are called from OOM.
1819 * To do this we must instruct the shmfs to drop all of its
1820 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01001821 */
Al Viro496ad9a2013-01-23 17:07:38 -05001822 inode = file_inode(obj->base.filp);
Daniel Vetter225067e2012-08-20 10:23:20 +02001823 shmem_truncate_range(inode, 0, (loff_t)-1);
Hugh Dickins5949eac2011-06-27 16:18:18 -07001824
Daniel Vetter225067e2012-08-20 10:23:20 +02001825 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001826}
Chris Wilsone5281cc2010-10-28 13:45:36 +01001827
Daniel Vetter225067e2012-08-20 10:23:20 +02001828static inline int
1829i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1830{
1831 return obj->madv == I915_MADV_DONTNEED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001832}
1833
Chris Wilson5cdf5882010-09-27 15:51:07 +01001834static void
Chris Wilson05394f32010-11-08 19:18:58 +00001835i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001836{
Imre Deak90797e62013-02-18 19:28:03 +02001837 struct sg_page_iter sg_iter;
1838 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001839
Chris Wilson05394f32010-11-08 19:18:58 +00001840 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001841
Chris Wilson6c085a72012-08-20 11:40:46 +02001842 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1843 if (ret) {
1844 /* In the event of a disaster, abandon all caches and
1845 * hope for the best.
1846 */
1847 WARN_ON(ret != -EIO);
Chris Wilson2c225692013-08-09 12:26:45 +01001848 i915_gem_clflush_object(obj, true);
Chris Wilson6c085a72012-08-20 11:40:46 +02001849 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1850 }
1851
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001852 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001853 i915_gem_object_save_bit_17_swizzle(obj);
1854
Chris Wilson05394f32010-11-08 19:18:58 +00001855 if (obj->madv == I915_MADV_DONTNEED)
1856 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001857
Imre Deak90797e62013-02-18 19:28:03 +02001858 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001859 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +01001860
Chris Wilson05394f32010-11-08 19:18:58 +00001861 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01001862 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001863
Chris Wilson05394f32010-11-08 19:18:58 +00001864 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01001865 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001866
Chris Wilson9da3da62012-06-01 15:20:22 +01001867 page_cache_release(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001868 }
Chris Wilson05394f32010-11-08 19:18:58 +00001869 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001870
Chris Wilson9da3da62012-06-01 15:20:22 +01001871 sg_free_table(obj->pages);
1872 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01001873}
1874
Chris Wilsondd624af2013-01-15 12:39:35 +00001875int
Chris Wilson37e680a2012-06-07 15:38:42 +01001876i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
1877{
1878 const struct drm_i915_gem_object_ops *ops = obj->ops;
1879
Chris Wilson2f745ad2012-09-04 21:02:58 +01001880 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01001881 return 0;
1882
Chris Wilsona5570172012-09-04 21:02:54 +01001883 if (obj->pages_pin_count)
1884 return -EBUSY;
1885
Ben Widawsky98438772013-07-31 17:00:12 -07001886 BUG_ON(i915_gem_obj_bound_any(obj));
Ben Widawsky3e123022013-07-31 17:00:04 -07001887
Chris Wilsona2165e32012-12-03 11:49:00 +00001888 /* ->put_pages might need to allocate memory for the bit17 swizzle
1889 * array, hence protect them from being reaped by removing them from gtt
1890 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001891 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00001892
Chris Wilson37e680a2012-06-07 15:38:42 +01001893 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001894 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02001895
Chris Wilson6c085a72012-08-20 11:40:46 +02001896 if (i915_gem_object_is_purgeable(obj))
1897 i915_gem_object_truncate(obj);
1898
1899 return 0;
1900}
1901
Chris Wilsond9973b42013-10-04 10:33:00 +01001902static unsigned long
Daniel Vetter93927ca2013-01-10 18:03:00 +01001903__i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
1904 bool purgeable_only)
Chris Wilson6c085a72012-08-20 11:40:46 +02001905{
Chris Wilson57094f82013-09-04 10:45:50 +01001906 struct list_head still_bound_list;
Chris Wilson6c085a72012-08-20 11:40:46 +02001907 struct drm_i915_gem_object *obj, *next;
Chris Wilsond9973b42013-10-04 10:33:00 +01001908 unsigned long count = 0;
Chris Wilson6c085a72012-08-20 11:40:46 +02001909
1910 list_for_each_entry_safe(obj, next,
1911 &dev_priv->mm.unbound_list,
Ben Widawsky35c20a62013-05-31 11:28:48 -07001912 global_list) {
Daniel Vetter93927ca2013-01-10 18:03:00 +01001913 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
Chris Wilson37e680a2012-06-07 15:38:42 +01001914 i915_gem_object_put_pages(obj) == 0) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001915 count += obj->base.size >> PAGE_SHIFT;
1916 if (count >= target)
1917 return count;
1918 }
1919 }
1920
Chris Wilson57094f82013-09-04 10:45:50 +01001921 /*
1922 * As we may completely rewrite the bound list whilst unbinding
1923 * (due to retiring requests) we have to strictly process only
1924 * one element of the list at the time, and recheck the list
1925 * on every iteration.
1926 */
1927 INIT_LIST_HEAD(&still_bound_list);
1928 while (count < target && !list_empty(&dev_priv->mm.bound_list)) {
Ben Widawsky07fe0b12013-07-31 17:00:10 -07001929 struct i915_vma *vma, *v;
Ben Widawsky80dcfdb2013-07-31 17:00:01 -07001930
Chris Wilson57094f82013-09-04 10:45:50 +01001931 obj = list_first_entry(&dev_priv->mm.bound_list,
1932 typeof(*obj), global_list);
1933 list_move_tail(&obj->global_list, &still_bound_list);
1934
Ben Widawsky80dcfdb2013-07-31 17:00:01 -07001935 if (!i915_gem_object_is_purgeable(obj) && purgeable_only)
1936 continue;
1937
Chris Wilson57094f82013-09-04 10:45:50 +01001938 /*
1939 * Hold a reference whilst we unbind this object, as we may
1940 * end up waiting for and retiring requests. This might
1941 * release the final reference (held by the active list)
1942 * and result in the object being freed from under us.
1943 * in this object being freed.
1944 *
1945 * Note 1: Shrinking the bound list is special since only active
1946 * (and hence bound objects) can contain such limbo objects, so
1947 * we don't need special tricks for shrinking the unbound list.
1948 * The only other place where we have to be careful with active
1949 * objects suddenly disappearing due to retiring requests is the
1950 * eviction code.
1951 *
1952 * Note 2: Even though the bound list doesn't hold a reference
1953 * to the object we can safely grab one here: The final object
1954 * unreferencing and the bound_list are both protected by the
1955 * dev->struct_mutex and so we won't ever be able to observe an
1956 * object on the bound_list with a reference count equals 0.
1957 */
1958 drm_gem_object_reference(&obj->base);
1959
Ben Widawsky07fe0b12013-07-31 17:00:10 -07001960 list_for_each_entry_safe(vma, v, &obj->vma_list, vma_link)
1961 if (i915_vma_unbind(vma))
1962 break;
Ben Widawsky80dcfdb2013-07-31 17:00:01 -07001963
Chris Wilson57094f82013-09-04 10:45:50 +01001964 if (i915_gem_object_put_pages(obj) == 0)
Chris Wilson6c085a72012-08-20 11:40:46 +02001965 count += obj->base.size >> PAGE_SHIFT;
Chris Wilson57094f82013-09-04 10:45:50 +01001966
1967 drm_gem_object_unreference(&obj->base);
Chris Wilson6c085a72012-08-20 11:40:46 +02001968 }
Chris Wilson57094f82013-09-04 10:45:50 +01001969 list_splice(&still_bound_list, &dev_priv->mm.bound_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02001970
1971 return count;
1972}
1973
Chris Wilsond9973b42013-10-04 10:33:00 +01001974static unsigned long
Daniel Vetter93927ca2013-01-10 18:03:00 +01001975i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1976{
1977 return __i915_gem_shrink(dev_priv, target, true);
1978}
1979
Chris Wilsond9973b42013-10-04 10:33:00 +01001980static unsigned long
Chris Wilson6c085a72012-08-20 11:40:46 +02001981i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1982{
1983 struct drm_i915_gem_object *obj, *next;
Dave Chinner7dc19d52013-08-28 10:18:11 +10001984 long freed = 0;
Chris Wilson6c085a72012-08-20 11:40:46 +02001985
1986 i915_gem_evict_everything(dev_priv->dev);
1987
Ben Widawsky35c20a62013-05-31 11:28:48 -07001988 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
Dave Chinner7dc19d52013-08-28 10:18:11 +10001989 global_list) {
Chris Wilsond9973b42013-10-04 10:33:00 +01001990 if (i915_gem_object_put_pages(obj) == 0)
Dave Chinner7dc19d52013-08-28 10:18:11 +10001991 freed += obj->base.size >> PAGE_SHIFT;
Dave Chinner7dc19d52013-08-28 10:18:11 +10001992 }
1993 return freed;
Daniel Vetter225067e2012-08-20 10:23:20 +02001994}
1995
Chris Wilson37e680a2012-06-07 15:38:42 +01001996static int
Chris Wilson6c085a72012-08-20 11:40:46 +02001997i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001998{
Chris Wilson6c085a72012-08-20 11:40:46 +02001999 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07002000 int page_count, i;
2001 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01002002 struct sg_table *st;
2003 struct scatterlist *sg;
Imre Deak90797e62013-02-18 19:28:03 +02002004 struct sg_page_iter sg_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07002005 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02002006 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson6c085a72012-08-20 11:40:46 +02002007 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07002008
Chris Wilson6c085a72012-08-20 11:40:46 +02002009 /* Assert that the object is not currently in any GPU domain. As it
2010 * wasn't in the GTT, there shouldn't be any way it could have been in
2011 * a GPU cache
2012 */
2013 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2014 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2015
Chris Wilson9da3da62012-06-01 15:20:22 +01002016 st = kmalloc(sizeof(*st), GFP_KERNEL);
2017 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07002018 return -ENOMEM;
2019
Chris Wilson9da3da62012-06-01 15:20:22 +01002020 page_count = obj->base.size / PAGE_SIZE;
2021 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01002022 kfree(st);
2023 return -ENOMEM;
2024 }
2025
2026 /* Get the list of pages out of our struct file. They'll be pinned
2027 * at this point until we release them.
2028 *
2029 * Fail silently without starting the shrinker
2030 */
Al Viro496ad9a2013-01-23 17:07:38 -05002031 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilson6c085a72012-08-20 11:40:46 +02002032 gfp = mapping_gfp_mask(mapping);
Linus Torvaldscaf49192012-12-10 10:51:16 -08002033 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
Chris Wilson6c085a72012-08-20 11:40:46 +02002034 gfp &= ~(__GFP_IO | __GFP_WAIT);
Imre Deak90797e62013-02-18 19:28:03 +02002035 sg = st->sgl;
2036 st->nents = 0;
2037 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02002038 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2039 if (IS_ERR(page)) {
2040 i915_gem_purge(dev_priv, page_count);
2041 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2042 }
2043 if (IS_ERR(page)) {
2044 /* We've tried hard to allocate the memory by reaping
2045 * our own buffer, now let the real VM do its job and
2046 * go down in flames if truly OOM.
2047 */
Linus Torvaldscaf49192012-12-10 10:51:16 -08002048 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
Chris Wilson6c085a72012-08-20 11:40:46 +02002049 gfp |= __GFP_IO | __GFP_WAIT;
2050
2051 i915_gem_shrink_all(dev_priv);
2052 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
2053 if (IS_ERR(page))
2054 goto err_pages;
2055
Linus Torvaldscaf49192012-12-10 10:51:16 -08002056 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
Chris Wilson6c085a72012-08-20 11:40:46 +02002057 gfp &= ~(__GFP_IO | __GFP_WAIT);
2058 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002059#ifdef CONFIG_SWIOTLB
2060 if (swiotlb_nr_tbl()) {
2061 st->nents++;
2062 sg_set_page(sg, page, PAGE_SIZE, 0);
2063 sg = sg_next(sg);
2064 continue;
2065 }
2066#endif
Imre Deak90797e62013-02-18 19:28:03 +02002067 if (!i || page_to_pfn(page) != last_pfn + 1) {
2068 if (i)
2069 sg = sg_next(sg);
2070 st->nents++;
2071 sg_set_page(sg, page, PAGE_SIZE, 0);
2072 } else {
2073 sg->length += PAGE_SIZE;
2074 }
2075 last_pfn = page_to_pfn(page);
Daniel Vetter3bbbe702013-10-07 17:15:45 -03002076
2077 /* Check that the i965g/gm workaround works. */
2078 WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
Eric Anholt673a3942008-07-30 12:06:12 -07002079 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04002080#ifdef CONFIG_SWIOTLB
2081 if (!swiotlb_nr_tbl())
2082#endif
2083 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01002084 obj->pages = st;
2085
Eric Anholt673a3942008-07-30 12:06:12 -07002086 if (i915_gem_object_needs_bit17_swizzle(obj))
2087 i915_gem_object_do_bit_17_swizzle(obj);
2088
2089 return 0;
2090
2091err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02002092 sg_mark_end(sg);
2093 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
Imre Deak2db76d72013-03-26 15:14:18 +02002094 page_cache_release(sg_page_iter_page(&sg_iter));
Chris Wilson9da3da62012-06-01 15:20:22 +01002095 sg_free_table(st);
2096 kfree(st);
Eric Anholt673a3942008-07-30 12:06:12 -07002097 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07002098}
2099
Chris Wilson37e680a2012-06-07 15:38:42 +01002100/* Ensure that the associated pages are gathered from the backing storage
2101 * and pinned into our object. i915_gem_object_get_pages() may be called
2102 * multiple times before they are released by a single call to
2103 * i915_gem_object_put_pages() - once the pages are no longer referenced
2104 * either as a result of memory pressure (reaping pages under the shrinker)
2105 * or as the object is itself released.
2106 */
2107int
2108i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
2109{
2110 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2111 const struct drm_i915_gem_object_ops *ops = obj->ops;
2112 int ret;
2113
Chris Wilson2f745ad2012-09-04 21:02:58 +01002114 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01002115 return 0;
2116
Chris Wilson43e28f02013-01-08 10:53:09 +00002117 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00002118 DRM_DEBUG("Attempting to obtain a purgeable object\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00002119 return -EFAULT;
Chris Wilson43e28f02013-01-08 10:53:09 +00002120 }
2121
Chris Wilsona5570172012-09-04 21:02:54 +01002122 BUG_ON(obj->pages_pin_count);
2123
Chris Wilson37e680a2012-06-07 15:38:42 +01002124 ret = ops->get_pages(obj);
2125 if (ret)
2126 return ret;
2127
Ben Widawsky35c20a62013-05-31 11:28:48 -07002128 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilson37e680a2012-06-07 15:38:42 +01002129 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002130}
2131
Ben Widawskye2d05a82013-09-24 09:57:58 -07002132static void
Chris Wilson05394f32010-11-08 19:18:58 +00002133i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson9d7730912012-11-27 16:22:52 +00002134 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002135{
Chris Wilson05394f32010-11-08 19:18:58 +00002136 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01002137 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9d7730912012-11-27 16:22:52 +00002138 u32 seqno = intel_ring_get_seqno(ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01002139
Zou Nan hai852835f2010-05-21 09:08:56 +08002140 BUG_ON(ring == NULL);
Chris Wilson02978ff2013-07-09 09:22:39 +01002141 if (obj->ring != ring && obj->last_write_seqno) {
2142 /* Keep the seqno relative to the current ring */
2143 obj->last_write_seqno = seqno;
2144 }
Chris Wilson05394f32010-11-08 19:18:58 +00002145 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07002146
2147 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00002148 if (!obj->active) {
2149 drm_gem_object_reference(&obj->base);
2150 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07002151 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01002152
Chris Wilson05394f32010-11-08 19:18:58 +00002153 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002154
Chris Wilson0201f1e2012-07-20 12:41:01 +01002155 obj->last_read_seqno = seqno;
Chris Wilson7dd49062012-03-21 10:48:18 +00002156
Chris Wilsoncaea7472010-11-12 13:53:37 +00002157 if (obj->fenced_gpu_access) {
Chris Wilsoncaea7472010-11-12 13:53:37 +00002158 obj->last_fenced_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002159
Chris Wilson7dd49062012-03-21 10:48:18 +00002160 /* Bump MRU to take account of the delayed flush */
2161 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2162 struct drm_i915_fence_reg *reg;
2163
2164 reg = &dev_priv->fence_regs[obj->fence_reg];
2165 list_move_tail(&reg->lru_list,
2166 &dev_priv->mm.fence_list);
2167 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00002168 }
2169}
2170
Ben Widawskye2d05a82013-09-24 09:57:58 -07002171void i915_vma_move_to_active(struct i915_vma *vma,
2172 struct intel_ring_buffer *ring)
2173{
2174 list_move_tail(&vma->mm_list, &vma->vm->active_list);
2175 return i915_gem_object_move_to_active(vma->obj, ring);
2176}
2177
Chris Wilsoncaea7472010-11-12 13:53:37 +00002178static void
Chris Wilsoncaea7472010-11-12 13:53:37 +00002179i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
2180{
Ben Widawskyca191b12013-07-31 17:00:14 -07002181 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002182 struct i915_address_space *vm;
2183 struct i915_vma *vma;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002184
Chris Wilson65ce3022012-07-20 12:41:02 +01002185 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002186 BUG_ON(!obj->active);
Chris Wilson65ce3022012-07-20 12:41:02 +01002187
Ben Widawskyfeb822c2013-12-06 14:10:51 -08002188 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
2189 vma = i915_gem_obj_to_vma(obj, vm);
2190 if (vma && !list_empty(&vma->mm_list))
2191 list_move_tail(&vma->mm_list, &vm->inactive_list);
2192 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00002193
Chris Wilson65ce3022012-07-20 12:41:02 +01002194 list_del_init(&obj->ring_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00002195 obj->ring = NULL;
2196
Chris Wilson65ce3022012-07-20 12:41:02 +01002197 obj->last_read_seqno = 0;
2198 obj->last_write_seqno = 0;
2199 obj->base.write_domain = 0;
2200
2201 obj->last_fenced_seqno = 0;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002202 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00002203
2204 obj->active = 0;
2205 drm_gem_object_unreference(&obj->base);
2206
2207 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08002208}
Eric Anholt673a3942008-07-30 12:06:12 -07002209
Chris Wilson9d7730912012-11-27 16:22:52 +00002210static int
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002211i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002212{
Chris Wilson9d7730912012-11-27 16:22:52 +00002213 struct drm_i915_private *dev_priv = dev->dev_private;
2214 struct intel_ring_buffer *ring;
2215 int ret, i, j;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002216
Chris Wilson107f27a52012-12-10 13:56:17 +02002217 /* Carefully retire all requests without writing to the rings */
Chris Wilson9d7730912012-11-27 16:22:52 +00002218 for_each_ring(ring, dev_priv, i) {
Chris Wilson107f27a52012-12-10 13:56:17 +02002219 ret = intel_ring_idle(ring);
2220 if (ret)
2221 return ret;
Chris Wilson9d7730912012-11-27 16:22:52 +00002222 }
Chris Wilson9d7730912012-11-27 16:22:52 +00002223 i915_gem_retire_requests(dev);
Chris Wilson107f27a52012-12-10 13:56:17 +02002224
2225 /* Finally reset hw state */
Chris Wilson9d7730912012-11-27 16:22:52 +00002226 for_each_ring(ring, dev_priv, i) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002227 intel_ring_init_seqno(ring, seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02002228
Chris Wilson9d7730912012-11-27 16:22:52 +00002229 for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
2230 ring->sync_seqno[j] = 0;
2231 }
2232
2233 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002234}
2235
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002236int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
2237{
2238 struct drm_i915_private *dev_priv = dev->dev_private;
2239 int ret;
2240
2241 if (seqno == 0)
2242 return -EINVAL;
2243
2244 /* HWS page needs to be set less than what we
2245 * will inject to ring
2246 */
2247 ret = i915_gem_init_seqno(dev, seqno - 1);
2248 if (ret)
2249 return ret;
2250
2251 /* Carefully set the last_seqno value so that wrap
2252 * detection still works
2253 */
2254 dev_priv->next_seqno = seqno;
2255 dev_priv->last_seqno = seqno - 1;
2256 if (dev_priv->last_seqno == 0)
2257 dev_priv->last_seqno--;
2258
2259 return 0;
2260}
2261
Chris Wilson9d7730912012-11-27 16:22:52 +00002262int
2263i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002264{
Chris Wilson9d7730912012-11-27 16:22:52 +00002265 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002266
Chris Wilson9d7730912012-11-27 16:22:52 +00002267 /* reserve 0 for non-seqno */
2268 if (dev_priv->next_seqno == 0) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002269 int ret = i915_gem_init_seqno(dev, 0);
Chris Wilson9d7730912012-11-27 16:22:52 +00002270 if (ret)
2271 return ret;
2272
2273 dev_priv->next_seqno = 1;
2274 }
2275
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02002276 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
Chris Wilson9d7730912012-11-27 16:22:52 +00002277 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002278}
2279
Mika Kuoppala0025c072013-06-12 12:35:30 +03002280int __i915_add_request(struct intel_ring_buffer *ring,
2281 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002282 struct drm_i915_gem_object *obj,
Mika Kuoppala0025c072013-06-12 12:35:30 +03002283 u32 *out_seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002284{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002285 struct drm_i915_private *dev_priv = ring->dev->dev_private;
Chris Wilsonacb868d2012-09-26 13:47:30 +01002286 struct drm_i915_gem_request *request;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002287 u32 request_ring_position, request_start;
Chris Wilson3cce4692010-10-27 16:11:02 +01002288 int ret;
2289
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002290 request_start = intel_ring_get_tail(ring);
Daniel Vettercc889e02012-06-13 20:45:19 +02002291 /*
2292 * Emit any outstanding flushes - execbuf can fail to emit the flush
2293 * after having emitted the batchbuffer command. Hence we need to fix
2294 * things up similar to emitting the lazy request. The difference here
2295 * is that the flush _must_ happen before the next request, no matter
2296 * what.
2297 */
Chris Wilsona7b97612012-07-20 12:41:08 +01002298 ret = intel_ring_flush_all_caches(ring);
2299 if (ret)
2300 return ret;
Daniel Vettercc889e02012-06-13 20:45:19 +02002301
Chris Wilson3c0e2342013-09-04 10:45:52 +01002302 request = ring->preallocated_lazy_request;
2303 if (WARN_ON(request == NULL))
Chris Wilsonacb868d2012-09-26 13:47:30 +01002304 return -ENOMEM;
Daniel Vettercc889e02012-06-13 20:45:19 +02002305
Chris Wilsona71d8d92012-02-15 11:25:36 +00002306 /* Record the position of the start of the request so that
2307 * should we detect the updated seqno part-way through the
2308 * GPU processing the request, we never over-estimate the
2309 * position of the head.
2310 */
2311 request_ring_position = intel_ring_get_tail(ring);
2312
Chris Wilson9d7730912012-11-27 16:22:52 +00002313 ret = ring->add_request(ring);
Chris Wilson3c0e2342013-09-04 10:45:52 +01002314 if (ret)
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002315 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002316
Chris Wilson9d7730912012-11-27 16:22:52 +00002317 request->seqno = intel_ring_get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08002318 request->ring = ring;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002319 request->head = request_start;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002320 request->tail = request_ring_position;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002321
2322 /* Whilst this request exists, batch_obj will be on the
2323 * active_list, and so will hold the active reference. Only when this
2324 * request is retired will the the batch_obj be moved onto the
2325 * inactive_list and lose its active reference. Hence we do not need
2326 * to explicitly hold another reference here.
2327 */
Chris Wilson9a7e0c22013-08-26 19:50:54 -03002328 request->batch_obj = obj;
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002329
Chris Wilson9a7e0c22013-08-26 19:50:54 -03002330 /* Hold a reference to the current context so that we can inspect
2331 * it later in case a hangcheck error event fires.
2332 */
2333 request->ctx = ring->last_context;
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002334 if (request->ctx)
2335 i915_gem_context_reference(request->ctx);
2336
Eric Anholt673a3942008-07-30 12:06:12 -07002337 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08002338 list_add_tail(&request->list, &ring->request_list);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002339 request->file_priv = NULL;
Zou Nan hai852835f2010-05-21 09:08:56 +08002340
Chris Wilsondb53a302011-02-03 11:57:46 +00002341 if (file) {
2342 struct drm_i915_file_private *file_priv = file->driver_priv;
2343
Chris Wilson1c255952010-09-26 11:03:27 +01002344 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002345 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00002346 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002347 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01002348 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00002349 }
Eric Anholt673a3942008-07-30 12:06:12 -07002350
Chris Wilson9d7730912012-11-27 16:22:52 +00002351 trace_i915_gem_request_add(ring, request->seqno);
Chris Wilson18235212013-09-04 10:45:51 +01002352 ring->outstanding_lazy_seqno = 0;
Chris Wilson3c0e2342013-09-04 10:45:52 +01002353 ring->preallocated_lazy_request = NULL;
Chris Wilsondb53a302011-02-03 11:57:46 +00002354
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02002355 if (!dev_priv->ums.mm_suspended) {
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03002356 i915_queue_hangcheck(ring->dev);
2357
Chris Wilsonf62a0072014-02-21 17:55:39 +00002358 cancel_delayed_work_sync(&dev_priv->mm.idle_work);
2359 queue_delayed_work(dev_priv->wq,
2360 &dev_priv->mm.retire_work,
2361 round_jiffies_up_relative(HZ));
2362 intel_mark_busy(dev_priv->dev);
Ben Gamarif65d9422009-09-14 17:48:44 -04002363 }
Daniel Vettercc889e02012-06-13 20:45:19 +02002364
Chris Wilsonacb868d2012-09-26 13:47:30 +01002365 if (out_seqno)
Chris Wilson9d7730912012-11-27 16:22:52 +00002366 *out_seqno = request->seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +01002367 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002368}
2369
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002370static inline void
2371i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07002372{
Chris Wilson1c255952010-09-26 11:03:27 +01002373 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002374
Chris Wilson1c255952010-09-26 11:03:27 +01002375 if (!file_priv)
2376 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002377
Chris Wilson1c255952010-09-26 11:03:27 +01002378 spin_lock(&file_priv->mm.lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002379 list_del(&request->client_list);
2380 request->file_priv = NULL;
Chris Wilson1c255952010-09-26 11:03:27 +01002381 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002382}
2383
Mika Kuoppala939fd762014-01-30 19:04:44 +02002384static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002385 const struct i915_hw_context *ctx)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002386{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002387 unsigned long elapsed;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002388
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002389 elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
2390
2391 if (ctx->hang_stats.banned)
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002392 return true;
2393
2394 if (elapsed <= DRM_I915_CTX_BAN_PERIOD) {
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002395 if (!i915_gem_context_is_default(ctx)) {
Mika Kuoppala3fac8972014-01-30 16:05:48 +02002396 DRM_DEBUG("context hanging too fast, banning!\n");
Ville Syrjäläccc7bed2014-02-21 16:26:47 +02002397 return true;
2398 } else if (dev_priv->gpu_error.stop_rings == 0) {
2399 DRM_ERROR("gpu hanging too fast, banning!\n");
2400 return true;
Mika Kuoppala3fac8972014-01-30 16:05:48 +02002401 }
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002402 }
2403
2404 return false;
2405}
2406
Mika Kuoppala939fd762014-01-30 19:04:44 +02002407static void i915_set_reset_status(struct drm_i915_private *dev_priv,
2408 struct i915_hw_context *ctx,
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002409 const bool guilty)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002410{
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002411 struct i915_ctx_hang_stats *hs;
2412
2413 if (WARN_ON(!ctx))
2414 return;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002415
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002416 hs = &ctx->hang_stats;
2417
2418 if (guilty) {
Mika Kuoppala939fd762014-01-30 19:04:44 +02002419 hs->banned = i915_context_is_banned(dev_priv, ctx);
Mika Kuoppala44e2c072014-01-30 16:01:15 +02002420 hs->batch_active++;
2421 hs->guilty_ts = get_seconds();
2422 } else {
2423 hs->batch_pending++;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002424 }
2425}
2426
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002427static void i915_gem_free_request(struct drm_i915_gem_request *request)
2428{
2429 list_del(&request->list);
2430 i915_gem_request_remove_from_client(request);
2431
2432 if (request->ctx)
2433 i915_gem_context_unreference(request->ctx);
2434
2435 kfree(request);
2436}
2437
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002438struct drm_i915_gem_request *
2439i915_gem_find_active_request(struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01002440{
Chris Wilson4db080f2013-12-04 11:37:09 +00002441 struct drm_i915_gem_request *request;
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002442 u32 completed_seqno;
2443
2444 completed_seqno = ring->get_seqno(ring, false);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002445
Chris Wilson4db080f2013-12-04 11:37:09 +00002446 list_for_each_entry(request, &ring->request_list, list) {
2447 if (i915_seqno_passed(completed_seqno, request->seqno))
2448 continue;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002449
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002450 return request;
Chris Wilson4db080f2013-12-04 11:37:09 +00002451 }
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002452
2453 return NULL;
2454}
2455
2456static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
2457 struct intel_ring_buffer *ring)
2458{
2459 struct drm_i915_gem_request *request;
2460 bool ring_hung;
2461
Chris Wilson8d9fc7f2014-02-25 17:11:23 +02002462 request = i915_gem_find_active_request(ring);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002463
2464 if (request == NULL)
2465 return;
2466
2467 ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
2468
Mika Kuoppala939fd762014-01-30 19:04:44 +02002469 i915_set_reset_status(dev_priv, request->ctx, ring_hung);
Mika Kuoppalab6b0fac2014-01-30 19:04:43 +02002470
2471 list_for_each_entry_continue(request, &ring->request_list, list)
Mika Kuoppala939fd762014-01-30 19:04:44 +02002472 i915_set_reset_status(dev_priv, request->ctx, false);
Chris Wilson4db080f2013-12-04 11:37:09 +00002473}
2474
2475static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
2476 struct intel_ring_buffer *ring)
2477{
Chris Wilsondfaae392010-09-22 10:31:52 +01002478 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002479 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002480
Chris Wilson05394f32010-11-08 19:18:58 +00002481 obj = list_first_entry(&ring->active_list,
2482 struct drm_i915_gem_object,
2483 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002484
Chris Wilson05394f32010-11-08 19:18:58 +00002485 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002486 }
Ben Widawsky1d62bee2014-01-01 10:15:13 -08002487
2488 /*
2489 * We must free the requests after all the corresponding objects have
2490 * been moved off active lists. Which is the same order as the normal
2491 * retire_requests function does. This is important if object hold
2492 * implicit references on things like e.g. ppgtt address spaces through
2493 * the request.
2494 */
2495 while (!list_empty(&ring->request_list)) {
2496 struct drm_i915_gem_request *request;
2497
2498 request = list_first_entry(&ring->request_list,
2499 struct drm_i915_gem_request,
2500 list);
2501
2502 i915_gem_free_request(request);
2503 }
Eric Anholt673a3942008-07-30 12:06:12 -07002504}
2505
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002506void i915_gem_restore_fences(struct drm_device *dev)
Chris Wilson312817a2010-11-22 11:50:11 +00002507{
2508 struct drm_i915_private *dev_priv = dev->dev_private;
2509 int i;
2510
Daniel Vetter4b9de732011-10-09 21:52:02 +02002511 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00002512 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00002513
Daniel Vetter94a335d2013-07-17 14:51:28 +02002514 /*
2515 * Commit delayed tiling changes if we have an object still
2516 * attached to the fence, otherwise just clear the fence.
2517 */
2518 if (reg->obj) {
2519 i915_gem_object_update_fence(reg->obj, reg,
2520 reg->obj->tiling_mode);
2521 } else {
2522 i915_gem_write_fence(dev, i, NULL);
2523 }
Chris Wilson312817a2010-11-22 11:50:11 +00002524 }
2525}
2526
Chris Wilson069efc12010-09-30 16:53:18 +01002527void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002528{
Chris Wilsondfaae392010-09-22 10:31:52 +01002529 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002530 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002531 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002532
Chris Wilson4db080f2013-12-04 11:37:09 +00002533 /*
2534 * Before we free the objects from the requests, we need to inspect
2535 * them for finding the guilty party. As the requests only borrow
2536 * their reference to the objects, the inspection must be done first.
2537 */
Chris Wilsonb4519512012-05-11 14:29:30 +01002538 for_each_ring(ring, dev_priv, i)
Chris Wilson4db080f2013-12-04 11:37:09 +00002539 i915_gem_reset_ring_status(dev_priv, ring);
2540
2541 for_each_ring(ring, dev_priv, i)
2542 i915_gem_reset_ring_cleanup(dev_priv, ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01002543
Ben Widawsky3d57e5b2013-10-14 10:01:36 -07002544 i915_gem_cleanup_ringbuffer(dev);
2545
Ben Widawskyacce9ff2013-12-06 14:11:03 -08002546 i915_gem_context_reset(dev);
2547
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002548 i915_gem_restore_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002549}
2550
2551/**
2552 * This function clears the request list as sequence numbers are passed.
2553 */
Damien Lespiaucb216aa2014-03-03 17:42:36 +00002554static void
Chris Wilsondb53a302011-02-03 11:57:46 +00002555i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002556{
Eric Anholt673a3942008-07-30 12:06:12 -07002557 uint32_t seqno;
2558
Chris Wilsondb53a302011-02-03 11:57:46 +00002559 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01002560 return;
2561
Chris Wilsondb53a302011-02-03 11:57:46 +00002562 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002563
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01002564 seqno = ring->get_seqno(ring, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002565
Chris Wilsone9103032014-01-07 11:45:14 +00002566 /* Move any buffers on the active list that are no longer referenced
2567 * by the ringbuffer to the flushing/inactive lists as appropriate,
2568 * before we free the context associated with the requests.
2569 */
2570 while (!list_empty(&ring->active_list)) {
2571 struct drm_i915_gem_object *obj;
2572
2573 obj = list_first_entry(&ring->active_list,
2574 struct drm_i915_gem_object,
2575 ring_list);
2576
2577 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
2578 break;
2579
2580 i915_gem_object_move_to_inactive(obj);
2581 }
2582
2583
Zou Nan hai852835f2010-05-21 09:08:56 +08002584 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002585 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002586
Zou Nan hai852835f2010-05-21 09:08:56 +08002587 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002588 struct drm_i915_gem_request,
2589 list);
Eric Anholt673a3942008-07-30 12:06:12 -07002590
Chris Wilsondfaae392010-09-22 10:31:52 +01002591 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07002592 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002593
Chris Wilsondb53a302011-02-03 11:57:46 +00002594 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002595 /* We know the GPU must have read the request to have
2596 * sent us the seqno + interrupt, so use the position
2597 * of tail of the request to update the last known position
2598 * of the GPU head.
2599 */
2600 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002601
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002602 i915_gem_free_request(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002603 }
2604
Chris Wilsondb53a302011-02-03 11:57:46 +00002605 if (unlikely(ring->trace_irq_seqno &&
2606 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002607 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00002608 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002609 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002610
Chris Wilsondb53a302011-02-03 11:57:46 +00002611 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002612}
2613
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002614bool
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002615i915_gem_retire_requests(struct drm_device *dev)
2616{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002617 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002618 struct intel_ring_buffer *ring;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002619 bool idle = true;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002620 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002621
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002622 for_each_ring(ring, dev_priv, i) {
Chris Wilsonb4519512012-05-11 14:29:30 +01002623 i915_gem_retire_requests_ring(ring);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002624 idle &= list_empty(&ring->request_list);
2625 }
2626
2627 if (idle)
2628 mod_delayed_work(dev_priv->wq,
2629 &dev_priv->mm.idle_work,
2630 msecs_to_jiffies(100));
2631
2632 return idle;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002633}
2634
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002635static void
Eric Anholt673a3942008-07-30 12:06:12 -07002636i915_gem_retire_work_handler(struct work_struct *work)
2637{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002638 struct drm_i915_private *dev_priv =
2639 container_of(work, typeof(*dev_priv), mm.retire_work.work);
2640 struct drm_device *dev = dev_priv->dev;
Chris Wilson0a587052011-01-09 21:05:44 +00002641 bool idle;
Eric Anholt673a3942008-07-30 12:06:12 -07002642
Chris Wilson891b48c2010-09-29 12:26:37 +01002643 /* Come back later if the device is busy... */
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002644 idle = false;
2645 if (mutex_trylock(&dev->struct_mutex)) {
2646 idle = i915_gem_retire_requests(dev);
2647 mutex_unlock(&dev->struct_mutex);
2648 }
2649 if (!idle)
Chris Wilsonbcb45082012-10-05 17:02:57 +01002650 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2651 round_jiffies_up_relative(HZ));
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002652}
Chris Wilson891b48c2010-09-29 12:26:37 +01002653
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002654static void
2655i915_gem_idle_work_handler(struct work_struct *work)
2656{
2657 struct drm_i915_private *dev_priv =
2658 container_of(work, typeof(*dev_priv), mm.idle_work.work);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002659
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002660 intel_mark_idle(dev_priv->dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002661}
2662
Ben Widawsky5816d642012-04-11 11:18:19 -07002663/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002664 * Ensures that an object will eventually get non-busy by flushing any required
2665 * write domains, emitting any outstanding lazy request and retiring and
2666 * completed requests.
2667 */
2668static int
2669i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2670{
2671 int ret;
2672
2673 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002674 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002675 if (ret)
2676 return ret;
2677
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002678 i915_gem_retire_requests_ring(obj->ring);
2679 }
2680
2681 return 0;
2682}
2683
2684/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002685 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2686 * @DRM_IOCTL_ARGS: standard ioctl arguments
2687 *
2688 * Returns 0 if successful, else an error is returned with the remaining time in
2689 * the timeout parameter.
2690 * -ETIME: object is still busy after timeout
2691 * -ERESTARTSYS: signal interrupted the wait
2692 * -ENONENT: object doesn't exist
2693 * Also possible, but rare:
2694 * -EAGAIN: GPU wedged
2695 * -ENOMEM: damn
2696 * -ENODEV: Internal IRQ fail
2697 * -E?: The add request failed
2698 *
2699 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2700 * non-zero timeout parameter the wait ioctl will wait for the given number of
2701 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2702 * without holding struct_mutex the object may become re-busied before this
2703 * function completes. A similar but shorter * race condition exists in the busy
2704 * ioctl
2705 */
2706int
2707i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2708{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002709 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002710 struct drm_i915_gem_wait *args = data;
2711 struct drm_i915_gem_object *obj;
2712 struct intel_ring_buffer *ring = NULL;
Ben Widawskyeac1f142012-06-05 15:24:24 -07002713 struct timespec timeout_stack, *timeout = NULL;
Daniel Vetterf69061b2012-12-06 09:01:42 +01002714 unsigned reset_counter;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002715 u32 seqno = 0;
2716 int ret = 0;
2717
Ben Widawskyeac1f142012-06-05 15:24:24 -07002718 if (args->timeout_ns >= 0) {
2719 timeout_stack = ns_to_timespec(args->timeout_ns);
2720 timeout = &timeout_stack;
2721 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002722
2723 ret = i915_mutex_lock_interruptible(dev);
2724 if (ret)
2725 return ret;
2726
2727 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2728 if (&obj->base == NULL) {
2729 mutex_unlock(&dev->struct_mutex);
2730 return -ENOENT;
2731 }
2732
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002733 /* Need to make sure the object gets inactive eventually. */
2734 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002735 if (ret)
2736 goto out;
2737
2738 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002739 seqno = obj->last_read_seqno;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002740 ring = obj->ring;
2741 }
2742
2743 if (seqno == 0)
2744 goto out;
2745
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002746 /* Do this after OLR check to make sure we make forward progress polling
2747 * on this IOCTL with a 0 timeout (like busy ioctl)
2748 */
2749 if (!args->timeout_ns) {
2750 ret = -ETIME;
2751 goto out;
2752 }
2753
2754 drm_gem_object_unreference(&obj->base);
Daniel Vetterf69061b2012-12-06 09:01:42 +01002755 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002756 mutex_unlock(&dev->struct_mutex);
2757
Chris Wilsonb29c19b2013-09-25 17:34:56 +01002758 ret = __wait_seqno(ring, seqno, reset_counter, true, timeout, file->driver_priv);
Chris Wilson4f42f4e2013-04-26 16:22:46 +03002759 if (timeout)
Ben Widawskyeac1f142012-06-05 15:24:24 -07002760 args->timeout_ns = timespec_to_ns(timeout);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002761 return ret;
2762
2763out:
2764 drm_gem_object_unreference(&obj->base);
2765 mutex_unlock(&dev->struct_mutex);
2766 return ret;
2767}
2768
2769/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002770 * i915_gem_object_sync - sync an object to a ring.
2771 *
2772 * @obj: object which may be in use on another ring.
2773 * @to: ring we wish to use the object on. May be NULL.
2774 *
2775 * This code is meant to abstract object synchronization with the GPU.
2776 * Calling with NULL implies synchronizing the object with the CPU
2777 * rather than a particular GPU ring.
2778 *
2779 * Returns 0 if successful, else propagates up the lower layer error.
2780 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002781int
2782i915_gem_object_sync(struct drm_i915_gem_object *obj,
2783 struct intel_ring_buffer *to)
2784{
2785 struct intel_ring_buffer *from = obj->ring;
2786 u32 seqno;
2787 int ret, idx;
2788
2789 if (from == NULL || to == from)
2790 return 0;
2791
Ben Widawsky5816d642012-04-11 11:18:19 -07002792 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
Chris Wilson0201f1e2012-07-20 12:41:01 +01002793 return i915_gem_object_wait_rendering(obj, false);
Ben Widawsky2911a352012-04-05 14:47:36 -07002794
2795 idx = intel_ring_sync_index(from, to);
2796
Chris Wilson0201f1e2012-07-20 12:41:01 +01002797 seqno = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002798 if (seqno <= from->sync_seqno[idx])
2799 return 0;
2800
Ben Widawskyb4aca012012-04-25 20:50:12 -07002801 ret = i915_gem_check_olr(obj->ring, seqno);
2802 if (ret)
2803 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002804
Chris Wilsonb52b89d2013-09-25 11:43:28 +01002805 trace_i915_gem_ring_sync_to(from, to, seqno);
Ben Widawsky1500f7e2012-04-11 11:18:21 -07002806 ret = to->sync_to(to, from, seqno);
Ben Widawskye3a5a222012-04-11 11:18:20 -07002807 if (!ret)
Mika Kuoppala7b01e262012-11-28 17:18:45 +02002808 /* We use last_read_seqno because sync_to()
2809 * might have just caused seqno wrap under
2810 * the radar.
2811 */
2812 from->sync_seqno[idx] = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002813
Ben Widawskye3a5a222012-04-11 11:18:20 -07002814 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002815}
2816
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002817static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2818{
2819 u32 old_write_domain, old_read_domains;
2820
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002821 /* Force a pagefault for domain tracking on next user access */
2822 i915_gem_release_mmap(obj);
2823
Keith Packardb97c3d92011-06-24 21:02:59 -07002824 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2825 return;
2826
Chris Wilson97c809fd2012-10-09 19:24:38 +01002827 /* Wait for any direct GTT access to complete */
2828 mb();
2829
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002830 old_read_domains = obj->base.read_domains;
2831 old_write_domain = obj->base.write_domain;
2832
2833 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2834 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2835
2836 trace_i915_gem_object_change_domain(obj,
2837 old_read_domains,
2838 old_write_domain);
2839}
2840
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002841int i915_vma_unbind(struct i915_vma *vma)
Eric Anholt673a3942008-07-30 12:06:12 -07002842{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002843 struct drm_i915_gem_object *obj = vma->obj;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002844 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson43e28f02013-01-08 10:53:09 +00002845 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002846
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002847 if (list_empty(&vma->vma_link))
Eric Anholt673a3942008-07-30 12:06:12 -07002848 return 0;
2849
Daniel Vetter0ff501c2013-08-29 19:50:31 +02002850 if (!drm_mm_node_allocated(&vma->node)) {
2851 i915_gem_vma_destroy(vma);
Daniel Vetter0ff501c2013-08-29 19:50:31 +02002852 return 0;
2853 }
Ben Widawsky433544b2013-08-13 18:09:06 -07002854
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08002855 if (vma->pin_count)
Chris Wilson31d8d652012-05-24 19:11:20 +01002856 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002857
Chris Wilsonc4670ad2012-08-20 10:23:27 +01002858 BUG_ON(obj->pages == NULL);
2859
Chris Wilsona8198ee2011-04-13 22:04:09 +01002860 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002861 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002862 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002863 /* Continue on if we fail due to EIO, the GPU is hung so we
2864 * should be safe and we need to cleanup or else we might
2865 * cause memory corruption through use-after-free.
2866 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002867
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002868 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002869
Daniel Vetter96b47b62009-12-15 17:50:00 +01002870 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002871 ret = i915_gem_object_put_fence(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002872 if (ret)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002873 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002874
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002875 trace_i915_vma_unbind(vma);
Chris Wilsondb53a302011-02-03 11:57:46 +00002876
Ben Widawsky6f65e292013-12-06 14:10:56 -08002877 vma->unbind_vma(vma);
2878
Daniel Vetter74163902012-02-15 23:50:21 +01002879 i915_gem_gtt_finish_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002880
Chris Wilson64bf9302014-02-25 14:23:28 +00002881 list_del_init(&vma->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002882 /* Avoid an unnecessary call to unbind on rebind. */
Ben Widawsky5cacaac2013-07-31 17:00:13 -07002883 if (i915_is_ggtt(vma->vm))
2884 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002885
Ben Widawsky2f633152013-07-17 12:19:03 -07002886 drm_mm_remove_node(&vma->node);
2887 i915_gem_vma_destroy(vma);
2888
2889 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02002890 * no more VMAs exist. */
Ben Widawsky2f633152013-07-17 12:19:03 -07002891 if (list_empty(&obj->vma_list))
2892 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002893
Chris Wilson70903c32013-12-04 09:59:09 +00002894 /* And finally now the object is completely decoupled from this vma,
2895 * we can drop its hold on the backing storage and allow it to be
2896 * reaped by the shrinker.
2897 */
2898 i915_gem_object_unpin_pages(obj);
2899
Chris Wilson88241782011-01-07 17:09:48 +00002900 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002901}
2902
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002903int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002904{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002905 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002906 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002907 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002908
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002909 /* Flush everything onto the inactive list. */
Chris Wilsonb4519512012-05-11 14:29:30 +01002910 for_each_ring(ring, dev_priv, i) {
Chris Wilson691e6412014-04-09 09:07:36 +01002911 ret = i915_switch_context(ring, ring->default_context);
Ben Widawskyb6c74882012-08-14 14:35:14 -07002912 if (ret)
2913 return ret;
2914
Chris Wilson3e960502012-11-27 16:22:54 +00002915 ret = intel_ring_idle(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002916 if (ret)
2917 return ret;
2918 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002919
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002920 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002921}
2922
Chris Wilson9ce079e2012-04-17 15:31:30 +01002923static void i965_write_fence_reg(struct drm_device *dev, int reg,
2924 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002925{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002926 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak56c844e2013-01-07 21:47:34 +02002927 int fence_reg;
2928 int fence_pitch_shift;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002929
Imre Deak56c844e2013-01-07 21:47:34 +02002930 if (INTEL_INFO(dev)->gen >= 6) {
2931 fence_reg = FENCE_REG_SANDYBRIDGE_0;
2932 fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
2933 } else {
2934 fence_reg = FENCE_REG_965_0;
2935 fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
2936 }
2937
Chris Wilsond18b9612013-07-10 13:36:23 +01002938 fence_reg += reg * 8;
2939
2940 /* To w/a incoherency with non-atomic 64-bit register updates,
2941 * we split the 64-bit update into two 32-bit writes. In order
2942 * for a partial fence not to be evaluated between writes, we
2943 * precede the update with write to turn off the fence register,
2944 * and only enable the fence as the last step.
2945 *
2946 * For extra levels of paranoia, we make sure each step lands
2947 * before applying the next step.
2948 */
2949 I915_WRITE(fence_reg, 0);
2950 POSTING_READ(fence_reg);
2951
Chris Wilson9ce079e2012-04-17 15:31:30 +01002952 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002953 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilsond18b9612013-07-10 13:36:23 +01002954 uint64_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002955
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002956 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
Chris Wilson9ce079e2012-04-17 15:31:30 +01002957 0xfffff000) << 32;
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002958 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
Imre Deak56c844e2013-01-07 21:47:34 +02002959 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002960 if (obj->tiling_mode == I915_TILING_Y)
2961 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2962 val |= I965_FENCE_REG_VALID;
Daniel Vetterc6642782010-11-12 13:46:18 +00002963
Chris Wilsond18b9612013-07-10 13:36:23 +01002964 I915_WRITE(fence_reg + 4, val >> 32);
2965 POSTING_READ(fence_reg + 4);
2966
2967 I915_WRITE(fence_reg + 0, val);
2968 POSTING_READ(fence_reg);
2969 } else {
2970 I915_WRITE(fence_reg + 4, 0);
2971 POSTING_READ(fence_reg + 4);
2972 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002973}
2974
Chris Wilson9ce079e2012-04-17 15:31:30 +01002975static void i915_write_fence_reg(struct drm_device *dev, int reg,
2976 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002977{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03002978 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002979 u32 val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002980
Chris Wilson9ce079e2012-04-17 15:31:30 +01002981 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002982 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002983 int pitch_val;
2984 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002985
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002986 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
Chris Wilson9ce079e2012-04-17 15:31:30 +01002987 (size & -size) != size ||
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002988 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
2989 "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2990 i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002991
2992 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2993 tile_width = 128;
2994 else
2995 tile_width = 512;
2996
2997 /* Note: pitch better be a power of two tile widths */
2998 pitch_val = obj->stride / tile_width;
2999 pitch_val = ffs(pitch_val) - 1;
3000
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003001 val = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01003002 if (obj->tiling_mode == I915_TILING_Y)
3003 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
3004 val |= I915_FENCE_SIZE_BITS(size);
3005 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
3006 val |= I830_FENCE_REG_VALID;
3007 } else
3008 val = 0;
3009
3010 if (reg < 8)
3011 reg = FENCE_REG_830_0 + reg * 4;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003012 else
Chris Wilson9ce079e2012-04-17 15:31:30 +01003013 reg = FENCE_REG_945_8 + (reg - 8) * 4;
Jesse Barnes0f973f22009-01-26 17:10:45 -08003014
Chris Wilson9ce079e2012-04-17 15:31:30 +01003015 I915_WRITE(reg, val);
3016 POSTING_READ(reg);
Jesse Barnesde151cf2008-11-12 10:03:55 -08003017}
3018
Chris Wilson9ce079e2012-04-17 15:31:30 +01003019static void i830_write_fence_reg(struct drm_device *dev, int reg,
3020 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08003021{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003022 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003023 uint32_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003024
Chris Wilson9ce079e2012-04-17 15:31:30 +01003025 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003026 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01003027 uint32_t pitch_val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003028
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003029 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
Chris Wilson9ce079e2012-04-17 15:31:30 +01003030 (size & -size) != size ||
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003031 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
3032 "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
3033 i915_gem_obj_ggtt_offset(obj), size);
Eric Anholte76a16d2009-05-26 17:44:56 -07003034
Chris Wilson9ce079e2012-04-17 15:31:30 +01003035 pitch_val = obj->stride / 128;
3036 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003037
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003038 val = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01003039 if (obj->tiling_mode == I915_TILING_Y)
3040 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
3041 val |= I830_FENCE_SIZE_BITS(size);
3042 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
3043 val |= I830_FENCE_REG_VALID;
3044 } else
3045 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00003046
Chris Wilson9ce079e2012-04-17 15:31:30 +01003047 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
3048 POSTING_READ(FENCE_REG_830_0 + reg * 4);
3049}
3050
Chris Wilsond0a57782012-10-09 19:24:37 +01003051inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
3052{
3053 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
3054}
3055
Chris Wilson9ce079e2012-04-17 15:31:30 +01003056static void i915_gem_write_fence(struct drm_device *dev, int reg,
3057 struct drm_i915_gem_object *obj)
3058{
Chris Wilsond0a57782012-10-09 19:24:37 +01003059 struct drm_i915_private *dev_priv = dev->dev_private;
3060
3061 /* Ensure that all CPU reads are completed before installing a fence
3062 * and all writes before removing the fence.
3063 */
3064 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
3065 mb();
3066
Daniel Vetter94a335d2013-07-17 14:51:28 +02003067 WARN(obj && (!obj->stride || !obj->tiling_mode),
3068 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
3069 obj->stride, obj->tiling_mode);
3070
Chris Wilson9ce079e2012-04-17 15:31:30 +01003071 switch (INTEL_INFO(dev)->gen) {
Ben Widawsky5ab31332013-11-02 21:07:03 -07003072 case 8:
Chris Wilson9ce079e2012-04-17 15:31:30 +01003073 case 7:
Imre Deak56c844e2013-01-07 21:47:34 +02003074 case 6:
Chris Wilson9ce079e2012-04-17 15:31:30 +01003075 case 5:
3076 case 4: i965_write_fence_reg(dev, reg, obj); break;
3077 case 3: i915_write_fence_reg(dev, reg, obj); break;
3078 case 2: i830_write_fence_reg(dev, reg, obj); break;
Ben Widawsky7dbf9d62012-12-18 10:31:22 -08003079 default: BUG();
Chris Wilson9ce079e2012-04-17 15:31:30 +01003080 }
Chris Wilsond0a57782012-10-09 19:24:37 +01003081
3082 /* And similarly be paranoid that no direct access to this region
3083 * is reordered to before the fence is installed.
3084 */
3085 if (i915_gem_object_needs_mb(obj))
3086 mb();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003087}
3088
Chris Wilson61050802012-04-17 15:31:31 +01003089static inline int fence_number(struct drm_i915_private *dev_priv,
3090 struct drm_i915_fence_reg *fence)
3091{
3092 return fence - dev_priv->fence_regs;
3093}
3094
3095static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
3096 struct drm_i915_fence_reg *fence,
3097 bool enable)
3098{
Chris Wilson2dc8aae2013-05-22 17:08:06 +01003099 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson46a0b632013-07-10 13:36:24 +01003100 int reg = fence_number(dev_priv, fence);
Chris Wilson61050802012-04-17 15:31:31 +01003101
Chris Wilson46a0b632013-07-10 13:36:24 +01003102 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
Chris Wilson61050802012-04-17 15:31:31 +01003103
3104 if (enable) {
Chris Wilson46a0b632013-07-10 13:36:24 +01003105 obj->fence_reg = reg;
Chris Wilson61050802012-04-17 15:31:31 +01003106 fence->obj = obj;
3107 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
3108 } else {
3109 obj->fence_reg = I915_FENCE_REG_NONE;
3110 fence->obj = NULL;
3111 list_del_init(&fence->lru_list);
3112 }
Daniel Vetter94a335d2013-07-17 14:51:28 +02003113 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +01003114}
3115
Chris Wilsond9e86c02010-11-10 16:40:20 +00003116static int
Chris Wilsond0a57782012-10-09 19:24:37 +01003117i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00003118{
Chris Wilson1c293ea2012-04-17 15:31:27 +01003119 if (obj->last_fenced_seqno) {
Chris Wilson86d5bc32012-07-20 12:41:04 +01003120 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
Chris Wilson18991842012-04-17 15:31:29 +01003121 if (ret)
3122 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003123
3124 obj->last_fenced_seqno = 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003125 }
3126
Chris Wilson86d5bc32012-07-20 12:41:04 +01003127 obj->fenced_gpu_access = false;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003128 return 0;
3129}
3130
3131int
3132i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
3133{
Chris Wilson61050802012-04-17 15:31:31 +01003134 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsonf9c513e2013-03-26 11:29:27 +00003135 struct drm_i915_fence_reg *fence;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003136 int ret;
3137
Chris Wilsond0a57782012-10-09 19:24:37 +01003138 ret = i915_gem_object_wait_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00003139 if (ret)
3140 return ret;
3141
Chris Wilson61050802012-04-17 15:31:31 +01003142 if (obj->fence_reg == I915_FENCE_REG_NONE)
3143 return 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01003144
Chris Wilsonf9c513e2013-03-26 11:29:27 +00003145 fence = &dev_priv->fence_regs[obj->fence_reg];
3146
Chris Wilson61050802012-04-17 15:31:31 +01003147 i915_gem_object_fence_lost(obj);
Chris Wilsonf9c513e2013-03-26 11:29:27 +00003148 i915_gem_object_update_fence(obj, fence, false);
Chris Wilsond9e86c02010-11-10 16:40:20 +00003149
3150 return 0;
3151}
3152
3153static struct drm_i915_fence_reg *
Chris Wilsona360bb12012-04-17 15:31:25 +01003154i915_find_fence_reg(struct drm_device *dev)
Daniel Vetterae3db242010-02-19 11:51:58 +01003155{
Daniel Vetterae3db242010-02-19 11:51:58 +01003156 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8fe301a2012-04-17 15:31:28 +01003157 struct drm_i915_fence_reg *reg, *avail;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003158 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01003159
3160 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00003161 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01003162 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
3163 reg = &dev_priv->fence_regs[i];
3164 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00003165 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003166
Chris Wilson1690e1e2011-12-14 13:57:08 +01003167 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00003168 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003169 }
3170
Chris Wilsond9e86c02010-11-10 16:40:20 +00003171 if (avail == NULL)
Chris Wilson5dce5b932014-01-20 10:17:36 +00003172 goto deadlock;
Daniel Vetterae3db242010-02-19 11:51:58 +01003173
3174 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00003175 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01003176 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01003177 continue;
3178
Chris Wilson8fe301a2012-04-17 15:31:28 +01003179 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003180 }
3181
Chris Wilson5dce5b932014-01-20 10:17:36 +00003182deadlock:
3183 /* Wait for completion of pending flips which consume fences */
3184 if (intel_has_pending_fb_unpin(dev))
3185 return ERR_PTR(-EAGAIN);
3186
3187 return ERR_PTR(-EDEADLK);
Daniel Vetterae3db242010-02-19 11:51:58 +01003188}
3189
Jesse Barnesde151cf2008-11-12 10:03:55 -08003190/**
Chris Wilson9a5a53b2012-03-22 15:10:00 +00003191 * i915_gem_object_get_fence - set up fencing for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08003192 * @obj: object to map through a fence reg
3193 *
3194 * When mapping objects through the GTT, userspace wants to be able to write
3195 * to them without having to worry about swizzling if the object is tiled.
Jesse Barnesde151cf2008-11-12 10:03:55 -08003196 * This function walks the fence regs looking for a free one for @obj,
3197 * stealing one if it can't find any.
3198 *
3199 * It then sets up the reg based on the object's properties: address, pitch
3200 * and tiling format.
Chris Wilson9a5a53b2012-03-22 15:10:00 +00003201 *
3202 * For an untiled surface, this removes any existing fence.
Jesse Barnesde151cf2008-11-12 10:03:55 -08003203 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003204int
Chris Wilson06d98132012-04-17 15:31:24 +01003205i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08003206{
Chris Wilson05394f32010-11-08 19:18:58 +00003207 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003208 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson14415742012-04-17 15:31:33 +01003209 bool enable = obj->tiling_mode != I915_TILING_NONE;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003210 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003211 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003212
Chris Wilson14415742012-04-17 15:31:33 +01003213 /* Have we updated the tiling parameters upon the object and so
3214 * will need to serialise the write to the associated fence register?
3215 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +01003216 if (obj->fence_dirty) {
Chris Wilsond0a57782012-10-09 19:24:37 +01003217 ret = i915_gem_object_wait_fence(obj);
Chris Wilson14415742012-04-17 15:31:33 +01003218 if (ret)
3219 return ret;
3220 }
Chris Wilson9a5a53b2012-03-22 15:10:00 +00003221
Chris Wilsond9e86c02010-11-10 16:40:20 +00003222 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00003223 if (obj->fence_reg != I915_FENCE_REG_NONE) {
3224 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson5d82e3e2012-04-21 16:23:23 +01003225 if (!obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01003226 list_move_tail(&reg->lru_list,
3227 &dev_priv->mm.fence_list);
3228 return 0;
3229 }
3230 } else if (enable) {
3231 reg = i915_find_fence_reg(dev);
Chris Wilson5dce5b932014-01-20 10:17:36 +00003232 if (IS_ERR(reg))
3233 return PTR_ERR(reg);
Chris Wilsond9e86c02010-11-10 16:40:20 +00003234
Chris Wilson14415742012-04-17 15:31:33 +01003235 if (reg->obj) {
3236 struct drm_i915_gem_object *old = reg->obj;
3237
Chris Wilsond0a57782012-10-09 19:24:37 +01003238 ret = i915_gem_object_wait_fence(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00003239 if (ret)
3240 return ret;
3241
Chris Wilson14415742012-04-17 15:31:33 +01003242 i915_gem_object_fence_lost(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00003243 }
Chris Wilson14415742012-04-17 15:31:33 +01003244 } else
Eric Anholta09ba7f2009-08-29 12:49:51 -07003245 return 0;
Eric Anholta09ba7f2009-08-29 12:49:51 -07003246
Chris Wilson14415742012-04-17 15:31:33 +01003247 i915_gem_object_update_fence(obj, reg, enable);
Chris Wilson14415742012-04-17 15:31:33 +01003248
Chris Wilson9ce079e2012-04-17 15:31:30 +01003249 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003250}
3251
Chris Wilson42d6ab42012-07-26 11:49:32 +01003252static bool i915_gem_valid_gtt_space(struct drm_device *dev,
3253 struct drm_mm_node *gtt_space,
3254 unsigned long cache_level)
3255{
3256 struct drm_mm_node *other;
3257
3258 /* On non-LLC machines we have to be careful when putting differing
3259 * types of snoopable memory together to avoid the prefetcher
Damien Lespiau4239ca72012-12-03 16:26:16 +00003260 * crossing memory domains and dying.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003261 */
3262 if (HAS_LLC(dev))
3263 return true;
3264
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003265 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003266 return true;
3267
3268 if (list_empty(&gtt_space->node_list))
3269 return true;
3270
3271 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3272 if (other->allocated && !other->hole_follows && other->color != cache_level)
3273 return false;
3274
3275 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3276 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3277 return false;
3278
3279 return true;
3280}
3281
3282static void i915_gem_verify_gtt(struct drm_device *dev)
3283{
3284#if WATCH_GTT
3285 struct drm_i915_private *dev_priv = dev->dev_private;
3286 struct drm_i915_gem_object *obj;
3287 int err = 0;
3288
Ben Widawsky35c20a62013-05-31 11:28:48 -07003289 list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
Chris Wilson42d6ab42012-07-26 11:49:32 +01003290 if (obj->gtt_space == NULL) {
3291 printk(KERN_ERR "object found on GTT list with no space reserved\n");
3292 err++;
3293 continue;
3294 }
3295
3296 if (obj->cache_level != obj->gtt_space->color) {
3297 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003298 i915_gem_obj_ggtt_offset(obj),
3299 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
Chris Wilson42d6ab42012-07-26 11:49:32 +01003300 obj->cache_level,
3301 obj->gtt_space->color);
3302 err++;
3303 continue;
3304 }
3305
3306 if (!i915_gem_valid_gtt_space(dev,
3307 obj->gtt_space,
3308 obj->cache_level)) {
3309 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003310 i915_gem_obj_ggtt_offset(obj),
3311 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
Chris Wilson42d6ab42012-07-26 11:49:32 +01003312 obj->cache_level);
3313 err++;
3314 continue;
3315 }
3316 }
3317
3318 WARN_ON(err);
3319#endif
3320}
3321
Jesse Barnesde151cf2008-11-12 10:03:55 -08003322/**
Eric Anholt673a3942008-07-30 12:06:12 -07003323 * Finds free space in the GTT aperture and binds the object there.
3324 */
Daniel Vetter262de142014-02-14 14:01:20 +01003325static struct i915_vma *
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003326i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3327 struct i915_address_space *vm,
3328 unsigned alignment,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003329 unsigned flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003330{
Chris Wilson05394f32010-11-08 19:18:58 +00003331 struct drm_device *dev = obj->base.dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003332 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter5e783302010-11-14 22:32:36 +01003333 u32 size, fence_size, fence_alignment, unfenced_alignment;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003334 size_t gtt_max =
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003335 flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total;
Ben Widawsky2f633152013-07-17 12:19:03 -07003336 struct i915_vma *vma;
Chris Wilson07f73f62009-09-14 16:50:30 +01003337 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003338
Chris Wilsone28f8712011-07-18 13:11:49 -07003339 fence_size = i915_gem_get_gtt_size(dev,
3340 obj->base.size,
3341 obj->tiling_mode);
3342 fence_alignment = i915_gem_get_gtt_alignment(dev,
3343 obj->base.size,
Imre Deakd865110c2013-01-07 21:47:33 +02003344 obj->tiling_mode, true);
Chris Wilsone28f8712011-07-18 13:11:49 -07003345 unfenced_alignment =
Imre Deakd865110c2013-01-07 21:47:33 +02003346 i915_gem_get_gtt_alignment(dev,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003347 obj->base.size,
3348 obj->tiling_mode, false);
Chris Wilsona00b10c2010-09-24 21:15:47 +01003349
Eric Anholt673a3942008-07-30 12:06:12 -07003350 if (alignment == 0)
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003351 alignment = flags & PIN_MAPPABLE ? fence_alignment :
Daniel Vetter5e783302010-11-14 22:32:36 +01003352 unfenced_alignment;
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003353 if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00003354 DRM_DEBUG("Invalid object alignment requested %u\n", alignment);
Daniel Vetter262de142014-02-14 14:01:20 +01003355 return ERR_PTR(-EINVAL);
Eric Anholt673a3942008-07-30 12:06:12 -07003356 }
3357
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003358 size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01003359
Chris Wilson654fc602010-05-27 13:18:21 +01003360 /* If the object is bigger than the entire aperture, reject it early
3361 * before evicting everything in a vain attempt to find space.
3362 */
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003363 if (obj->base.size > gtt_max) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00003364 DRM_DEBUG("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
Chris Wilsona36689c2013-05-21 16:58:49 +01003365 obj->base.size,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003366 flags & PIN_MAPPABLE ? "mappable" : "total",
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003367 gtt_max);
Daniel Vetter262de142014-02-14 14:01:20 +01003368 return ERR_PTR(-E2BIG);
Chris Wilson654fc602010-05-27 13:18:21 +01003369 }
3370
Chris Wilson37e680a2012-06-07 15:38:42 +01003371 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003372 if (ret)
Daniel Vetter262de142014-02-14 14:01:20 +01003373 return ERR_PTR(ret);
Chris Wilson6c085a72012-08-20 11:40:46 +02003374
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003375 i915_gem_object_pin_pages(obj);
3376
Ben Widawskyaccfef22013-08-14 11:38:35 +02003377 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Daniel Vetter262de142014-02-14 14:01:20 +01003378 if (IS_ERR(vma))
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003379 goto err_unpin;
Ben Widawsky2f633152013-07-17 12:19:03 -07003380
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003381search_free:
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003382 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003383 size, alignment,
David Herrmann31e5d7c2013-07-27 13:36:27 +02003384 obj->cache_level, 0, gtt_max,
Lauri Kasanen62347f92014-04-02 20:03:57 +03003385 DRM_MM_SEARCH_DEFAULT,
3386 DRM_MM_CREATE_DEFAULT);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003387 if (ret) {
Ben Widawskyf6cd1f12013-07-31 17:00:11 -07003388 ret = i915_gem_evict_something(dev, vm, size, alignment,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003389 obj->cache_level, flags);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003390 if (ret == 0)
3391 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003392
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003393 goto err_free_vma;
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003394 }
Ben Widawsky2f633152013-07-17 12:19:03 -07003395 if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003396 obj->cache_level))) {
Ben Widawsky2f633152013-07-17 12:19:03 -07003397 ret = -EINVAL;
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003398 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003399 }
3400
Daniel Vetter74163902012-02-15 23:50:21 +01003401 ret = i915_gem_gtt_prepare_object(obj);
Ben Widawsky2f633152013-07-17 12:19:03 -07003402 if (ret)
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003403 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003404
Ben Widawsky35c20a62013-05-31 11:28:48 -07003405 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Ben Widawskyca191b12013-07-31 17:00:14 -07003406 list_add_tail(&vma->mm_list, &vm->inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003407
Ben Widawsky4bd561b2013-08-13 18:09:07 -07003408 if (i915_is_ggtt(vm)) {
3409 bool mappable, fenceable;
Chris Wilsona00b10c2010-09-24 21:15:47 +01003410
Daniel Vetter49987092013-08-14 10:21:23 +02003411 fenceable = (vma->node.size == fence_size &&
3412 (vma->node.start & (fence_alignment - 1)) == 0);
Chris Wilsona00b10c2010-09-24 21:15:47 +01003413
Daniel Vetter49987092013-08-14 10:21:23 +02003414 mappable = (vma->node.start + obj->base.size <=
3415 dev_priv->gtt.mappable_end);
Ben Widawsky4bd561b2013-08-13 18:09:07 -07003416
Ben Widawsky5cacaac2013-07-31 17:00:13 -07003417 obj->map_and_fenceable = mappable && fenceable;
Ben Widawsky4bd561b2013-08-13 18:09:07 -07003418 }
Daniel Vetter75e9e912010-11-04 17:11:09 +01003419
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003420 WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003421
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003422 trace_i915_vma_bind(vma, flags);
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003423 vma->bind_vma(vma, obj->cache_level,
3424 flags & (PIN_MAPPABLE | PIN_GLOBAL) ? GLOBAL_BIND : 0);
3425
Chris Wilson42d6ab42012-07-26 11:49:32 +01003426 i915_gem_verify_gtt(dev);
Daniel Vetter262de142014-02-14 14:01:20 +01003427 return vma;
Ben Widawsky2f633152013-07-17 12:19:03 -07003428
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003429err_remove_node:
Dan Carpenter6286ef92013-07-19 08:46:27 +03003430 drm_mm_remove_node(&vma->node);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003431err_free_vma:
Ben Widawsky2f633152013-07-17 12:19:03 -07003432 i915_gem_vma_destroy(vma);
Daniel Vetter262de142014-02-14 14:01:20 +01003433 vma = ERR_PTR(ret);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003434err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003435 i915_gem_object_unpin_pages(obj);
Daniel Vetter262de142014-02-14 14:01:20 +01003436 return vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003437}
3438
Chris Wilson000433b2013-08-08 14:41:09 +01003439bool
Chris Wilson2c225692013-08-09 12:26:45 +01003440i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3441 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003442{
Eric Anholt673a3942008-07-30 12:06:12 -07003443 /* If we don't have a page list set up, then we're not pinned
3444 * to GPU, and we can ignore the cache flush because it'll happen
3445 * again at bind time.
3446 */
Chris Wilson05394f32010-11-08 19:18:58 +00003447 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003448 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003449
Imre Deak769ce462013-02-13 21:56:05 +02003450 /*
3451 * Stolen memory is always coherent with the GPU as it is explicitly
3452 * marked as wc by the system, or the system is cache-coherent.
3453 */
3454 if (obj->stolen)
Chris Wilson000433b2013-08-08 14:41:09 +01003455 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003456
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003457 /* If the GPU is snooping the contents of the CPU cache,
3458 * we do not need to manually clear the CPU cache lines. However,
3459 * the caches are only snooped when the render cache is
3460 * flushed/invalidated. As we always have to emit invalidations
3461 * and flushes when moving into and out of the RENDER domain, correct
3462 * snooping behaviour occurs naturally as the result of our domain
3463 * tracking.
3464 */
Chris Wilson2c225692013-08-09 12:26:45 +01003465 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
Chris Wilson000433b2013-08-08 14:41:09 +01003466 return false;
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003467
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003468 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003469 drm_clflush_sg(obj->pages);
Chris Wilson000433b2013-08-08 14:41:09 +01003470
3471 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003472}
3473
3474/** Flushes the GTT write domain for the object if it's dirty. */
3475static void
Chris Wilson05394f32010-11-08 19:18:58 +00003476i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003477{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003478 uint32_t old_write_domain;
3479
Chris Wilson05394f32010-11-08 19:18:58 +00003480 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003481 return;
3482
Chris Wilson63256ec2011-01-04 18:42:07 +00003483 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003484 * to it immediately go to main memory as far as we know, so there's
3485 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003486 *
3487 * However, we do have to enforce the order so that all writes through
3488 * the GTT land before any writes to the device, such as updates to
3489 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003490 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003491 wmb();
3492
Chris Wilson05394f32010-11-08 19:18:58 +00003493 old_write_domain = obj->base.write_domain;
3494 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003495
3496 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003497 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003498 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003499}
3500
3501/** Flushes the CPU write domain for the object if it's dirty. */
3502static void
Chris Wilson2c225692013-08-09 12:26:45 +01003503i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
3504 bool force)
Eric Anholte47c68e2008-11-14 13:35:19 -08003505{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003506 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003507
Chris Wilson05394f32010-11-08 19:18:58 +00003508 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003509 return;
3510
Chris Wilson000433b2013-08-08 14:41:09 +01003511 if (i915_gem_clflush_object(obj, force))
3512 i915_gem_chipset_flush(obj->base.dev);
3513
Chris Wilson05394f32010-11-08 19:18:58 +00003514 old_write_domain = obj->base.write_domain;
3515 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003516
3517 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003518 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003519 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003520}
3521
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003522/**
3523 * Moves a single object to the GTT read, and possibly write domain.
3524 *
3525 * This function returns when the move is complete, including waiting on
3526 * flushes to occur.
3527 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003528int
Chris Wilson20217462010-11-23 15:26:33 +00003529i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003530{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03003531 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003532 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003533 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003534
Eric Anholt02354392008-11-26 13:58:13 -08003535 /* Not valid to be called on unbound objects. */
Ben Widawsky98438772013-07-31 17:00:12 -07003536 if (!i915_gem_obj_bound_any(obj))
Eric Anholt02354392008-11-26 13:58:13 -08003537 return -EINVAL;
3538
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003539 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3540 return 0;
3541
Chris Wilson0201f1e2012-07-20 12:41:01 +01003542 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003543 if (ret)
3544 return ret;
3545
Chris Wilson2c225692013-08-09 12:26:45 +01003546 i915_gem_object_flush_cpu_write_domain(obj, false);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003547
Chris Wilsond0a57782012-10-09 19:24:37 +01003548 /* Serialise direct access to this object with the barriers for
3549 * coherent writes from the GPU, by effectively invalidating the
3550 * GTT domain upon first access.
3551 */
3552 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3553 mb();
3554
Chris Wilson05394f32010-11-08 19:18:58 +00003555 old_write_domain = obj->base.write_domain;
3556 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003557
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003558 /* It should now be out of any other write domains, and we can update
3559 * the domain values for our changes.
3560 */
Chris Wilson05394f32010-11-08 19:18:58 +00003561 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3562 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003563 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003564 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3565 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3566 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003567 }
3568
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003569 trace_i915_gem_object_change_domain(obj,
3570 old_read_domains,
3571 old_write_domain);
3572
Chris Wilson8325a092012-04-24 15:52:35 +01003573 /* And bump the LRU for this access */
Ben Widawskyca191b12013-07-31 17:00:14 -07003574 if (i915_gem_object_is_inactive(obj)) {
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07003575 struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
Ben Widawskyca191b12013-07-31 17:00:14 -07003576 if (vma)
3577 list_move_tail(&vma->mm_list,
3578 &dev_priv->gtt.base.inactive_list);
3579
3580 }
Chris Wilson8325a092012-04-24 15:52:35 +01003581
Eric Anholte47c68e2008-11-14 13:35:19 -08003582 return 0;
3583}
3584
Chris Wilsone4ffd172011-04-04 09:44:39 +01003585int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3586 enum i915_cache_level cache_level)
3587{
Daniel Vetter7bddb012012-02-09 17:15:47 +01003588 struct drm_device *dev = obj->base.dev;
Chris Wilsondf6f7832014-03-21 07:40:56 +00003589 struct i915_vma *vma, *next;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003590 int ret;
3591
3592 if (obj->cache_level == cache_level)
3593 return 0;
3594
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003595 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003596 DRM_DEBUG("can not change the cache level of pinned objects\n");
3597 return -EBUSY;
3598 }
3599
Chris Wilsondf6f7832014-03-21 07:40:56 +00003600 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003601 if (!i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003602 ret = i915_vma_unbind(vma);
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003603 if (ret)
3604 return ret;
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003605 }
Chris Wilson42d6ab42012-07-26 11:49:32 +01003606 }
3607
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003608 if (i915_gem_obj_bound_any(obj)) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003609 ret = i915_gem_object_finish_gpu(obj);
3610 if (ret)
3611 return ret;
3612
3613 i915_gem_object_finish_gtt(obj);
3614
3615 /* Before SandyBridge, you could not use tiling or fence
3616 * registers with snooped memory, so relinquish any fences
3617 * currently pointing to our region in the aperture.
3618 */
Chris Wilson42d6ab42012-07-26 11:49:32 +01003619 if (INTEL_INFO(dev)->gen < 6) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003620 ret = i915_gem_object_put_fence(obj);
3621 if (ret)
3622 return ret;
3623 }
3624
Ben Widawsky6f65e292013-12-06 14:10:56 -08003625 list_for_each_entry(vma, &obj->vma_list, vma_link)
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003626 if (drm_mm_node_allocated(&vma->node))
3627 vma->bind_vma(vma, cache_level,
3628 obj->has_global_gtt_mapping ? GLOBAL_BIND : 0);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003629 }
3630
Chris Wilson2c225692013-08-09 12:26:45 +01003631 list_for_each_entry(vma, &obj->vma_list, vma_link)
3632 vma->node.color = cache_level;
3633 obj->cache_level = cache_level;
3634
3635 if (cpu_write_needs_clflush(obj)) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003636 u32 old_read_domains, old_write_domain;
3637
3638 /* If we're coming from LLC cached, then we haven't
3639 * actually been tracking whether the data is in the
3640 * CPU cache or not, since we only allow one bit set
3641 * in obj->write_domain and have been skipping the clflushes.
3642 * Just set it to the CPU cache for now.
3643 */
3644 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003645
3646 old_read_domains = obj->base.read_domains;
3647 old_write_domain = obj->base.write_domain;
3648
3649 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3650 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3651
3652 trace_i915_gem_object_change_domain(obj,
3653 old_read_domains,
3654 old_write_domain);
3655 }
3656
Chris Wilson42d6ab42012-07-26 11:49:32 +01003657 i915_gem_verify_gtt(dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003658 return 0;
3659}
3660
Ben Widawsky199adf42012-09-21 17:01:20 -07003661int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3662 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003663{
Ben Widawsky199adf42012-09-21 17:01:20 -07003664 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003665 struct drm_i915_gem_object *obj;
3666 int ret;
3667
3668 ret = i915_mutex_lock_interruptible(dev);
3669 if (ret)
3670 return ret;
3671
3672 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3673 if (&obj->base == NULL) {
3674 ret = -ENOENT;
3675 goto unlock;
3676 }
3677
Chris Wilson651d7942013-08-08 14:41:10 +01003678 switch (obj->cache_level) {
3679 case I915_CACHE_LLC:
3680 case I915_CACHE_L3_LLC:
3681 args->caching = I915_CACHING_CACHED;
3682 break;
3683
Chris Wilson4257d3b2013-08-08 14:41:11 +01003684 case I915_CACHE_WT:
3685 args->caching = I915_CACHING_DISPLAY;
3686 break;
3687
Chris Wilson651d7942013-08-08 14:41:10 +01003688 default:
3689 args->caching = I915_CACHING_NONE;
3690 break;
3691 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003692
3693 drm_gem_object_unreference(&obj->base);
3694unlock:
3695 mutex_unlock(&dev->struct_mutex);
3696 return ret;
3697}
3698
Ben Widawsky199adf42012-09-21 17:01:20 -07003699int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3700 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003701{
Ben Widawsky199adf42012-09-21 17:01:20 -07003702 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003703 struct drm_i915_gem_object *obj;
3704 enum i915_cache_level level;
3705 int ret;
3706
Ben Widawsky199adf42012-09-21 17:01:20 -07003707 switch (args->caching) {
3708 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003709 level = I915_CACHE_NONE;
3710 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003711 case I915_CACHING_CACHED:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003712 level = I915_CACHE_LLC;
3713 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003714 case I915_CACHING_DISPLAY:
3715 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
3716 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003717 default:
3718 return -EINVAL;
3719 }
3720
Ben Widawsky3bc29132012-09-26 16:15:20 -07003721 ret = i915_mutex_lock_interruptible(dev);
3722 if (ret)
3723 return ret;
3724
Chris Wilsone6994ae2012-07-10 10:27:08 +01003725 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3726 if (&obj->base == NULL) {
3727 ret = -ENOENT;
3728 goto unlock;
3729 }
3730
3731 ret = i915_gem_object_set_cache_level(obj, level);
3732
3733 drm_gem_object_unreference(&obj->base);
3734unlock:
3735 mutex_unlock(&dev->struct_mutex);
3736 return ret;
3737}
3738
Chris Wilsoncc98b412013-08-09 12:25:09 +01003739static bool is_pin_display(struct drm_i915_gem_object *obj)
3740{
3741 /* There are 3 sources that pin objects:
3742 * 1. The display engine (scanouts, sprites, cursors);
3743 * 2. Reservations for execbuffer;
3744 * 3. The user.
3745 *
3746 * We can ignore reservations as we hold the struct_mutex and
3747 * are only called outside of the reservation path. The user
3748 * can only increment pin_count once, and so if after
3749 * subtracting the potential reference by the user, any pin_count
3750 * remains, it must be due to another use by the display engine.
3751 */
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003752 return i915_gem_obj_to_ggtt(obj)->pin_count - !!obj->user_pin_count;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003753}
3754
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003755/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003756 * Prepare buffer for display plane (scanout, cursors, etc).
3757 * Can be called from an uninterruptible phase (modesetting) and allows
3758 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003759 */
3760int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003761i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3762 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003763 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003764{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003765 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003766 int ret;
3767
Chris Wilson0be73282010-12-06 14:36:27 +00003768 if (pipelined != obj->ring) {
Ben Widawsky2911a352012-04-05 14:47:36 -07003769 ret = i915_gem_object_sync(obj, pipelined);
3770 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003771 return ret;
3772 }
3773
Chris Wilsoncc98b412013-08-09 12:25:09 +01003774 /* Mark the pin_display early so that we account for the
3775 * display coherency whilst setting up the cache domains.
3776 */
3777 obj->pin_display = true;
3778
Eric Anholta7ef0642011-03-29 16:59:54 -07003779 /* The display engine is not coherent with the LLC cache on gen6. As
3780 * a result, we make sure that the pinning that is about to occur is
3781 * done with uncached PTEs. This is lowest common denominator for all
3782 * chipsets.
3783 *
3784 * However for gen6+, we could do better by using the GFDT bit instead
3785 * of uncaching, which would allow us to flush all the LLC-cached data
3786 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3787 */
Chris Wilson651d7942013-08-08 14:41:10 +01003788 ret = i915_gem_object_set_cache_level(obj,
3789 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
Eric Anholta7ef0642011-03-29 16:59:54 -07003790 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003791 goto err_unpin_display;
Eric Anholta7ef0642011-03-29 16:59:54 -07003792
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003793 /* As the user may map the buffer once pinned in the display plane
3794 * (e.g. libkms for the bootup splash), we have to ensure that we
3795 * always use map_and_fenceable for all scanout buffers.
3796 */
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003797 ret = i915_gem_obj_ggtt_pin(obj, alignment, PIN_MAPPABLE);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003798 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003799 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003800
Chris Wilson2c225692013-08-09 12:26:45 +01003801 i915_gem_object_flush_cpu_write_domain(obj, true);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003802
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003803 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003804 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003805
3806 /* It should now be out of any other write domains, and we can update
3807 * the domain values for our changes.
3808 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003809 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003810 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003811
3812 trace_i915_gem_object_change_domain(obj,
3813 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003814 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003815
3816 return 0;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003817
3818err_unpin_display:
3819 obj->pin_display = is_pin_display(obj);
3820 return ret;
3821}
3822
3823void
3824i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj)
3825{
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003826 i915_gem_object_ggtt_unpin(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01003827 obj->pin_display = is_pin_display(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003828}
3829
Chris Wilson85345512010-11-13 09:49:11 +00003830int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003831i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003832{
Chris Wilson88241782011-01-07 17:09:48 +00003833 int ret;
3834
Chris Wilsona8198ee2011-04-13 22:04:09 +01003835 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003836 return 0;
3837
Chris Wilson0201f1e2012-07-20 12:41:01 +01003838 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsonc501ae72011-12-14 13:57:23 +01003839 if (ret)
3840 return ret;
3841
Chris Wilsona8198ee2011-04-13 22:04:09 +01003842 /* Ensure that we invalidate the GPU's caches and TLBs. */
3843 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01003844 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00003845}
3846
Eric Anholte47c68e2008-11-14 13:35:19 -08003847/**
3848 * Moves a single object to the CPU read, and possibly write domain.
3849 *
3850 * This function returns when the move is complete, including waiting on
3851 * flushes to occur.
3852 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003853int
Chris Wilson919926a2010-11-12 13:42:53 +00003854i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003855{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003856 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003857 int ret;
3858
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003859 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3860 return 0;
3861
Chris Wilson0201f1e2012-07-20 12:41:01 +01003862 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003863 if (ret)
3864 return ret;
3865
Eric Anholte47c68e2008-11-14 13:35:19 -08003866 i915_gem_object_flush_gtt_write_domain(obj);
3867
Chris Wilson05394f32010-11-08 19:18:58 +00003868 old_write_domain = obj->base.write_domain;
3869 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003870
Eric Anholte47c68e2008-11-14 13:35:19 -08003871 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003872 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003873 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003874
Chris Wilson05394f32010-11-08 19:18:58 +00003875 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003876 }
3877
3878 /* It should now be out of any other write domains, and we can update
3879 * the domain values for our changes.
3880 */
Chris Wilson05394f32010-11-08 19:18:58 +00003881 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003882
3883 /* If we're writing through the CPU, then the GPU read domains will
3884 * need to be invalidated at next use.
3885 */
3886 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003887 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3888 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003889 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003890
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003891 trace_i915_gem_object_change_domain(obj,
3892 old_read_domains,
3893 old_write_domain);
3894
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003895 return 0;
3896}
3897
Eric Anholt673a3942008-07-30 12:06:12 -07003898/* Throttle our rendering by waiting until the ring has completed our requests
3899 * emitted over 20 msec ago.
3900 *
Eric Anholtb9624422009-06-03 07:27:35 +00003901 * Note that if we were to use the current jiffies each time around the loop,
3902 * we wouldn't escape the function with any frames outstanding if the time to
3903 * render a frame was over 20ms.
3904 *
Eric Anholt673a3942008-07-30 12:06:12 -07003905 * This should get us reasonable parallelism between CPU and GPU but also
3906 * relatively low latency when blocking on a particular request to finish.
3907 */
3908static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003909i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003910{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003911 struct drm_i915_private *dev_priv = dev->dev_private;
3912 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003913 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003914 struct drm_i915_gem_request *request;
3915 struct intel_ring_buffer *ring = NULL;
Daniel Vetterf69061b2012-12-06 09:01:42 +01003916 unsigned reset_counter;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003917 u32 seqno = 0;
3918 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003919
Daniel Vetter308887a2012-11-14 17:14:06 +01003920 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3921 if (ret)
3922 return ret;
3923
3924 ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
3925 if (ret)
3926 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003927
Chris Wilson1c255952010-09-26 11:03:27 +01003928 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003929 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003930 if (time_after_eq(request->emitted_jiffies, recent_enough))
3931 break;
3932
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003933 ring = request->ring;
3934 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003935 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01003936 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilson1c255952010-09-26 11:03:27 +01003937 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003938
3939 if (seqno == 0)
3940 return 0;
3941
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003942 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003943 if (ret == 0)
3944 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003945
Eric Anholt673a3942008-07-30 12:06:12 -07003946 return ret;
3947}
3948
Eric Anholt673a3942008-07-30 12:06:12 -07003949int
Chris Wilson05394f32010-11-08 19:18:58 +00003950i915_gem_object_pin(struct drm_i915_gem_object *obj,
Ben Widawskyc37e2202013-07-31 16:59:58 -07003951 struct i915_address_space *vm,
Chris Wilson05394f32010-11-08 19:18:58 +00003952 uint32_t alignment,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003953 unsigned flags)
Eric Anholt673a3942008-07-30 12:06:12 -07003954{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003955 struct i915_vma *vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003956 int ret;
3957
Daniel Vetterbf3d1492014-02-14 14:01:12 +01003958 if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003959 return -EINVAL;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003960
3961 vma = i915_gem_obj_to_vma(obj, vm);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003962 if (vma) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003963 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3964 return -EBUSY;
3965
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003966 if ((alignment &&
3967 vma->node.start & (alignment - 1)) ||
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003968 (flags & PIN_MAPPABLE && !obj->map_and_fenceable)) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003969 WARN(vma->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003970 "bo is already pinned with incorrect alignment:"
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003971 " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003972 " obj->map_and_fenceable=%d\n",
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003973 i915_gem_obj_offset(obj, vm), alignment,
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003974 flags & PIN_MAPPABLE,
Chris Wilson05394f32010-11-08 19:18:58 +00003975 obj->map_and_fenceable);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003976 ret = i915_vma_unbind(vma);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003977 if (ret)
3978 return ret;
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003979
3980 vma = NULL;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003981 }
3982 }
3983
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003984 if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
Daniel Vetter262de142014-02-14 14:01:20 +01003985 vma = i915_gem_object_bind_to_vm(obj, vm, alignment, flags);
3986 if (IS_ERR(vma))
3987 return PTR_ERR(vma);
Chris Wilson22c344e2009-02-11 14:26:45 +00003988 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003989
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003990 if (flags & PIN_GLOBAL && !obj->has_global_gtt_mapping)
3991 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
Daniel Vetter74898d72012-02-15 23:50:22 +01003992
Daniel Vetter8ea99c92014-02-14 14:01:21 +01003993 vma->pin_count++;
Daniel Vetter1ec9e262014-02-14 14:01:11 +01003994 if (flags & PIN_MAPPABLE)
3995 obj->pin_mappable |= true;
Eric Anholt673a3942008-07-30 12:06:12 -07003996
3997 return 0;
3998}
3999
4000void
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004001i915_gem_object_ggtt_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07004002{
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004003 struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07004004
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004005 BUG_ON(!vma);
4006 BUG_ON(vma->pin_count == 0);
4007 BUG_ON(!i915_gem_obj_ggtt_bound(obj));
4008
4009 if (--vma->pin_count == 0)
Chris Wilson6299f992010-11-24 12:23:44 +00004010 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07004011}
4012
4013int
4014i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004015 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004016{
4017 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004018 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07004019 int ret;
4020
Daniel Vetter02f6bcc2013-12-18 16:30:22 +01004021 if (INTEL_INFO(dev)->gen >= 6)
4022 return -ENODEV;
4023
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004024 ret = i915_mutex_lock_interruptible(dev);
4025 if (ret)
4026 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004027
Chris Wilson05394f32010-11-08 19:18:58 +00004028 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004029 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004030 ret = -ENOENT;
4031 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004032 }
Eric Anholt673a3942008-07-30 12:06:12 -07004033
Chris Wilson05394f32010-11-08 19:18:58 +00004034 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00004035 DRM_DEBUG("Attempting to pin a purgeable buffer\n");
Chris Wilson8c99e572014-01-31 11:34:58 +00004036 ret = -EFAULT;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004037 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004038 }
4039
Chris Wilson05394f32010-11-08 19:18:58 +00004040 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00004041 DRM_DEBUG("Already pinned in i915_gem_pin_ioctl(): %d\n",
Jesse Barnes79e53942008-11-07 14:24:08 -08004042 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004043 ret = -EINVAL;
4044 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004045 }
4046
Daniel Vetteraa5f8022013-10-10 14:46:37 +02004047 if (obj->user_pin_count == ULONG_MAX) {
4048 ret = -EBUSY;
4049 goto out;
4050 }
4051
Chris Wilson93be8782013-01-02 10:31:22 +00004052 if (obj->user_pin_count == 0) {
Daniel Vetter1ec9e262014-02-14 14:01:11 +01004053 ret = i915_gem_obj_ggtt_pin(obj, args->alignment, PIN_MAPPABLE);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004054 if (ret)
4055 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07004056 }
4057
Chris Wilson93be8782013-01-02 10:31:22 +00004058 obj->user_pin_count++;
4059 obj->pin_filp = file;
4060
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004061 args->offset = i915_gem_obj_ggtt_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004062out:
Chris Wilson05394f32010-11-08 19:18:58 +00004063 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004064unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004065 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004066 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004067}
4068
4069int
4070i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004071 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004072{
4073 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004074 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004075 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004076
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004077 ret = i915_mutex_lock_interruptible(dev);
4078 if (ret)
4079 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004080
Chris Wilson05394f32010-11-08 19:18:58 +00004081 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004082 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004083 ret = -ENOENT;
4084 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004085 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01004086
Chris Wilson05394f32010-11-08 19:18:58 +00004087 if (obj->pin_filp != file) {
Chris Wilsonbd9b6a42014-02-10 09:03:50 +00004088 DRM_DEBUG("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
Jesse Barnes79e53942008-11-07 14:24:08 -08004089 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004090 ret = -EINVAL;
4091 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08004092 }
Chris Wilson05394f32010-11-08 19:18:58 +00004093 obj->user_pin_count--;
4094 if (obj->user_pin_count == 0) {
4095 obj->pin_filp = NULL;
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004096 i915_gem_object_ggtt_unpin(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08004097 }
Eric Anholt673a3942008-07-30 12:06:12 -07004098
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004099out:
Chris Wilson05394f32010-11-08 19:18:58 +00004100 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004101unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004102 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004103 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004104}
4105
4106int
4107i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00004108 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07004109{
4110 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004111 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01004112 int ret;
4113
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004114 ret = i915_mutex_lock_interruptible(dev);
4115 if (ret)
4116 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004117
Chris Wilson05394f32010-11-08 19:18:58 +00004118 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004119 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004120 ret = -ENOENT;
4121 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07004122 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08004123
Chris Wilson0be555b2010-08-04 15:36:30 +01004124 /* Count all active objects as busy, even if they are currently not used
4125 * by the gpu. Users of this interface expect objects to eventually
4126 * become non-busy without any further actions, therefore emit any
4127 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08004128 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02004129 ret = i915_gem_object_flush_active(obj);
4130
Chris Wilson05394f32010-11-08 19:18:58 +00004131 args->busy = obj->active;
Chris Wilsone9808ed2012-07-04 12:25:08 +01004132 if (obj->ring) {
4133 BUILD_BUG_ON(I915_NUM_RINGS > 16);
4134 args->busy |= intel_ring_flag(obj->ring) << 16;
4135 }
Eric Anholt673a3942008-07-30 12:06:12 -07004136
Chris Wilson05394f32010-11-08 19:18:58 +00004137 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004138unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07004139 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004140 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004141}
4142
4143int
4144i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4145 struct drm_file *file_priv)
4146{
Akshay Joshi0206e352011-08-16 15:34:10 -04004147 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07004148}
4149
Chris Wilson3ef94da2009-09-14 16:50:29 +01004150int
4151i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4152 struct drm_file *file_priv)
4153{
4154 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00004155 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01004156 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004157
4158 switch (args->madv) {
4159 case I915_MADV_DONTNEED:
4160 case I915_MADV_WILLNEED:
4161 break;
4162 default:
4163 return -EINVAL;
4164 }
4165
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004166 ret = i915_mutex_lock_interruptible(dev);
4167 if (ret)
4168 return ret;
4169
Chris Wilson05394f32010-11-08 19:18:58 +00004170 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004171 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004172 ret = -ENOENT;
4173 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004174 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01004175
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004176 if (i915_gem_obj_is_pinned(obj)) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004177 ret = -EINVAL;
4178 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004179 }
4180
Chris Wilson05394f32010-11-08 19:18:58 +00004181 if (obj->madv != __I915_MADV_PURGED)
4182 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004183
Chris Wilson6c085a72012-08-20 11:40:46 +02004184 /* if the object is no longer attached, discard its backing storage */
4185 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01004186 i915_gem_object_truncate(obj);
4187
Chris Wilson05394f32010-11-08 19:18:58 +00004188 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01004189
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004190out:
Chris Wilson05394f32010-11-08 19:18:58 +00004191 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004192unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01004193 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01004194 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01004195}
4196
Chris Wilson37e680a2012-06-07 15:38:42 +01004197void i915_gem_object_init(struct drm_i915_gem_object *obj,
4198 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01004199{
Ben Widawsky35c20a62013-05-31 11:28:48 -07004200 INIT_LIST_HEAD(&obj->global_list);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004201 INIT_LIST_HEAD(&obj->ring_list);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004202 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004203 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004204
Chris Wilson37e680a2012-06-07 15:38:42 +01004205 obj->ops = ops;
4206
Chris Wilson0327d6b2012-08-11 15:41:06 +01004207 obj->fence_reg = I915_FENCE_REG_NONE;
4208 obj->madv = I915_MADV_WILLNEED;
4209 /* Avoid an unnecessary call to unbind on the first bind. */
4210 obj->map_and_fenceable = true;
4211
4212 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
4213}
4214
Chris Wilson37e680a2012-06-07 15:38:42 +01004215static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
4216 .get_pages = i915_gem_object_get_pages_gtt,
4217 .put_pages = i915_gem_object_put_pages_gtt,
4218};
4219
Chris Wilson05394f32010-11-08 19:18:58 +00004220struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4221 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004222{
Daniel Vetterc397b902010-04-09 19:05:07 +00004223 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004224 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004225 gfp_t mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00004226
Chris Wilson42dcedd2012-11-15 11:32:30 +00004227 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004228 if (obj == NULL)
4229 return NULL;
4230
4231 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
Chris Wilson42dcedd2012-11-15 11:32:30 +00004232 i915_gem_object_free(obj);
Daniel Vetterc397b902010-04-09 19:05:07 +00004233 return NULL;
4234 }
4235
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004236 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4237 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4238 /* 965gm cannot relocate objects above 4GiB. */
4239 mask &= ~__GFP_HIGHMEM;
4240 mask |= __GFP_DMA32;
4241 }
4242
Al Viro496ad9a2013-01-23 17:07:38 -05004243 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004244 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004245
Chris Wilson37e680a2012-06-07 15:38:42 +01004246 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004247
Daniel Vetterc397b902010-04-09 19:05:07 +00004248 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4249 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4250
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004251 if (HAS_LLC(dev)) {
4252 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004253 * cache) for about a 10% performance improvement
4254 * compared to uncached. Graphics requests other than
4255 * display scanout are coherent with the CPU in
4256 * accessing this cache. This means in this mode we
4257 * don't need to clflush on the CPU side, and on the
4258 * GPU side we only need to flush internal caches to
4259 * get data visible to the CPU.
4260 *
4261 * However, we maintain the display planes as UC, and so
4262 * need to rebind when first used as such.
4263 */
4264 obj->cache_level = I915_CACHE_LLC;
4265 } else
4266 obj->cache_level = I915_CACHE_NONE;
4267
Daniel Vetterd861e332013-07-24 23:25:03 +02004268 trace_i915_gem_object_create(obj);
4269
Chris Wilson05394f32010-11-08 19:18:58 +00004270 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004271}
4272
Chris Wilson1488fc02012-04-24 15:47:31 +01004273void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004274{
Chris Wilson1488fc02012-04-24 15:47:31 +01004275 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004276 struct drm_device *dev = obj->base.dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004277 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004278 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004279
Paulo Zanonif65c9162013-11-27 18:20:34 -02004280 intel_runtime_pm_get(dev_priv);
4281
Chris Wilson26e12f82011-03-20 11:20:19 +00004282 trace_i915_gem_object_destroy(obj);
4283
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004284 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004285 int ret;
4286
4287 vma->pin_count = 0;
4288 ret = i915_vma_unbind(vma);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004289 if (WARN_ON(ret == -ERESTARTSYS)) {
4290 bool was_interruptible;
Chris Wilson1488fc02012-04-24 15:47:31 +01004291
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004292 was_interruptible = dev_priv->mm.interruptible;
4293 dev_priv->mm.interruptible = false;
Chris Wilson1488fc02012-04-24 15:47:31 +01004294
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004295 WARN_ON(i915_vma_unbind(vma));
Chris Wilson1488fc02012-04-24 15:47:31 +01004296
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004297 dev_priv->mm.interruptible = was_interruptible;
4298 }
Chris Wilson1488fc02012-04-24 15:47:31 +01004299 }
4300
Chris Wilson00731152014-05-21 12:42:56 +01004301 i915_gem_object_detach_phys(obj);
4302
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004303 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4304 * before progressing. */
4305 if (obj->stolen)
4306 i915_gem_object_unpin_pages(obj);
4307
Ben Widawsky401c29f2013-05-31 11:28:47 -07004308 if (WARN_ON(obj->pages_pin_count))
4309 obj->pages_pin_count = 0;
Chris Wilson37e680a2012-06-07 15:38:42 +01004310 i915_gem_object_put_pages(obj);
Chris Wilsond8cb5082012-08-11 15:41:03 +01004311 i915_gem_object_free_mmap_offset(obj);
Chris Wilson0104fdb2012-11-15 11:32:26 +00004312 i915_gem_object_release_stolen(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004313
Chris Wilson9da3da62012-06-01 15:20:22 +01004314 BUG_ON(obj->pages);
4315
Chris Wilson2f745ad2012-09-04 21:02:58 +01004316 if (obj->base.import_attach)
4317 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004318
Chris Wilson05394f32010-11-08 19:18:58 +00004319 drm_gem_object_release(&obj->base);
4320 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004321
Chris Wilson05394f32010-11-08 19:18:58 +00004322 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004323 i915_gem_object_free(obj);
Paulo Zanonif65c9162013-11-27 18:20:34 -02004324
4325 intel_runtime_pm_put(dev_priv);
Chris Wilsonbe726152010-07-23 23:18:50 +01004326}
4327
Daniel Vettere656a6c2013-08-14 14:14:04 +02004328struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
Ben Widawsky2f633152013-07-17 12:19:03 -07004329 struct i915_address_space *vm)
4330{
Daniel Vettere656a6c2013-08-14 14:14:04 +02004331 struct i915_vma *vma;
4332 list_for_each_entry(vma, &obj->vma_list, vma_link)
4333 if (vma->vm == vm)
4334 return vma;
4335
4336 return NULL;
4337}
4338
Ben Widawsky2f633152013-07-17 12:19:03 -07004339void i915_gem_vma_destroy(struct i915_vma *vma)
4340{
4341 WARN_ON(vma->node.allocated);
Chris Wilsonaaa056672013-08-20 12:56:40 +01004342
4343 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4344 if (!list_empty(&vma->exec_list))
4345 return;
4346
Ben Widawsky8b9c2b92013-07-31 17:00:16 -07004347 list_del(&vma->vma_link);
Daniel Vetterb93dab62013-08-26 11:23:47 +02004348
Ben Widawsky2f633152013-07-17 12:19:03 -07004349 kfree(vma);
4350}
4351
Jesse Barnes5669fca2009-02-17 15:13:31 -08004352int
Chris Wilson45c5f202013-10-16 11:50:01 +01004353i915_gem_suspend(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07004354{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004355 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson45c5f202013-10-16 11:50:01 +01004356 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07004357
Chris Wilson45c5f202013-10-16 11:50:01 +01004358 mutex_lock(&dev->struct_mutex);
Chris Wilsonf7403342013-09-13 23:57:04 +01004359 if (dev_priv->ums.mm_suspended)
Chris Wilson45c5f202013-10-16 11:50:01 +01004360 goto err;
Eric Anholt673a3942008-07-30 12:06:12 -07004361
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004362 ret = i915_gpu_idle(dev);
Chris Wilsonf7403342013-09-13 23:57:04 +01004363 if (ret)
Chris Wilson45c5f202013-10-16 11:50:01 +01004364 goto err;
Chris Wilsonf7403342013-09-13 23:57:04 +01004365
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004366 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004367
Chris Wilson29105cc2010-01-07 10:39:13 +00004368 /* Under UMS, be paranoid and evict. */
Chris Wilsona39d7ef2012-04-24 18:22:52 +01004369 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson6c085a72012-08-20 11:40:46 +02004370 i915_gem_evict_everything(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004371
Chris Wilson29105cc2010-01-07 10:39:13 +00004372 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004373 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004374
Chris Wilson45c5f202013-10-16 11:50:01 +01004375 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4376 * We need to replace this with a semaphore, or something.
4377 * And not confound ums.mm_suspended!
4378 */
4379 dev_priv->ums.mm_suspended = !drm_core_check_feature(dev,
4380 DRIVER_MODESET);
4381 mutex_unlock(&dev->struct_mutex);
4382
4383 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004384 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004385 cancel_delayed_work_sync(&dev_priv->mm.idle_work);
Chris Wilson29105cc2010-01-07 10:39:13 +00004386
Eric Anholt673a3942008-07-30 12:06:12 -07004387 return 0;
Chris Wilson45c5f202013-10-16 11:50:01 +01004388
4389err:
4390 mutex_unlock(&dev->struct_mutex);
4391 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004392}
4393
Ben Widawskyc3787e22013-09-17 21:12:44 -07004394int i915_gem_l3_remap(struct intel_ring_buffer *ring, int slice)
Ben Widawskyb9524a12012-05-25 16:56:24 -07004395{
Ben Widawskyc3787e22013-09-17 21:12:44 -07004396 struct drm_device *dev = ring->dev;
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004397 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07004398 u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
4399 u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
Ben Widawskyc3787e22013-09-17 21:12:44 -07004400 int i, ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004401
Ben Widawsky040d2ba2013-09-19 11:01:40 -07004402 if (!HAS_L3_DPF(dev) || !remap_info)
Ben Widawskyc3787e22013-09-17 21:12:44 -07004403 return 0;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004404
Ben Widawskyc3787e22013-09-17 21:12:44 -07004405 ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3);
4406 if (ret)
4407 return ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004408
Ben Widawskyc3787e22013-09-17 21:12:44 -07004409 /*
4410 * Note: We do not worry about the concurrent register cacheline hang
4411 * here because no other code should access these registers other than
4412 * at initialization time.
4413 */
Ben Widawskyb9524a12012-05-25 16:56:24 -07004414 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
Ben Widawskyc3787e22013-09-17 21:12:44 -07004415 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
4416 intel_ring_emit(ring, reg_base + i);
4417 intel_ring_emit(ring, remap_info[i/4]);
Ben Widawskyb9524a12012-05-25 16:56:24 -07004418 }
4419
Ben Widawskyc3787e22013-09-17 21:12:44 -07004420 intel_ring_advance(ring);
Ben Widawskyb9524a12012-05-25 16:56:24 -07004421
Ben Widawskyc3787e22013-09-17 21:12:44 -07004422 return ret;
Ben Widawskyb9524a12012-05-25 16:56:24 -07004423}
4424
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004425void i915_gem_init_swizzling(struct drm_device *dev)
4426{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004427 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004428
Daniel Vetter11782b02012-01-31 16:47:55 +01004429 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004430 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4431 return;
4432
4433 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4434 DISP_TILE_SURFACE_SWIZZLING);
4435
Daniel Vetter11782b02012-01-31 16:47:55 +01004436 if (IS_GEN5(dev))
4437 return;
4438
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004439 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4440 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004441 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004442 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004443 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky31a53362013-11-02 21:07:04 -07004444 else if (IS_GEN8(dev))
4445 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
Ben Widawsky8782e262012-12-18 10:31:23 -08004446 else
4447 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004448}
Daniel Vettere21af882012-02-09 20:53:27 +01004449
Chris Wilson67b1b572012-07-05 23:49:40 +01004450static bool
4451intel_enable_blt(struct drm_device *dev)
4452{
4453 if (!HAS_BLT(dev))
4454 return false;
4455
4456 /* The blitter was dysfunctional on early prototypes */
4457 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
4458 DRM_INFO("BLT not supported on this pre-production hardware;"
4459 " graphics performance will be degraded.\n");
4460 return false;
4461 }
4462
4463 return true;
4464}
4465
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004466static int i915_gem_init_rings(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004467{
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004468 struct drm_i915_private *dev_priv = dev->dev_private;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004469 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004470
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004471 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004472 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004473 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004474
4475 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004476 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004477 if (ret)
4478 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004479 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004480
Chris Wilson67b1b572012-07-05 23:49:40 +01004481 if (intel_enable_blt(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01004482 ret = intel_init_blt_ring_buffer(dev);
4483 if (ret)
4484 goto cleanup_bsd_ring;
4485 }
4486
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004487 if (HAS_VEBOX(dev)) {
4488 ret = intel_init_vebox_ring_buffer(dev);
4489 if (ret)
4490 goto cleanup_blt_ring;
4491 }
4492
4493
Mika Kuoppala99433932013-01-22 14:12:17 +02004494 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
4495 if (ret)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004496 goto cleanup_vebox_ring;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004497
4498 return 0;
4499
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004500cleanup_vebox_ring:
4501 intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004502cleanup_blt_ring:
4503 intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
4504cleanup_bsd_ring:
4505 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
4506cleanup_render_ring:
4507 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
4508
4509 return ret;
4510}
4511
4512int
4513i915_gem_init_hw(struct drm_device *dev)
4514{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004515 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky35a85ac2013-09-19 11:13:41 -07004516 int ret, i;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004517
4518 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
4519 return -EIO;
4520
Ben Widawsky59124502013-07-04 11:02:05 -07004521 if (dev_priv->ellc_size)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004522 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004523
Ville Syrjälä0bf21342013-11-29 14:56:12 +02004524 if (IS_HASWELL(dev))
4525 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
4526 LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
Rodrigo Vivi94353732013-08-28 16:45:46 -03004527
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004528 if (HAS_PCH_NOP(dev)) {
Daniel Vetter6ba844b2014-01-22 23:39:30 +01004529 if (IS_IVYBRIDGE(dev)) {
4530 u32 temp = I915_READ(GEN7_MSG_CTL);
4531 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4532 I915_WRITE(GEN7_MSG_CTL, temp);
4533 } else if (INTEL_INFO(dev)->gen >= 7) {
4534 u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
4535 temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
4536 I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
4537 }
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004538 }
4539
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004540 i915_gem_init_swizzling(dev);
4541
4542 ret = i915_gem_init_rings(dev);
4543 if (ret)
Mika Kuoppala99433932013-01-22 14:12:17 +02004544 return ret;
4545
Ben Widawskyc3787e22013-09-17 21:12:44 -07004546 for (i = 0; i < NUM_L3_SLICES(dev); i++)
4547 i915_gem_l3_remap(&dev_priv->ring[RCS], i);
4548
Ben Widawsky254f9652012-06-04 14:42:42 -07004549 /*
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004550 * XXX: Contexts should only be initialized once. Doing a switch to the
4551 * default context switch however is something we'd like to do after
4552 * reset or thaw (the latter may not actually be necessary for HW, but
4553 * goes with our code better). Context switching requires rings (for
4554 * the do_switch), but before enabling PPGTT. So don't move this.
Ben Widawsky254f9652012-06-04 14:42:42 -07004555 */
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004556 ret = i915_gem_context_enable(dev_priv);
Ben Widawsky8245be32013-11-06 13:56:29 -02004557 if (ret) {
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004558 DRM_ERROR("Context enable failed %d\n", ret);
4559 goto err_out;
Ben Widawskyb7c36d22013-04-08 18:43:56 -07004560 }
Daniel Vettere21af882012-02-09 20:53:27 +01004561
Chris Wilson68f95ba2010-05-27 13:18:22 +01004562 return 0;
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004563
4564err_out:
4565 i915_gem_cleanup_ringbuffer(dev);
4566 return ret;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004567}
4568
Chris Wilson1070a422012-04-24 15:47:41 +01004569int i915_gem_init(struct drm_device *dev)
4570{
4571 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1070a422012-04-24 15:47:41 +01004572 int ret;
4573
Chris Wilson1070a422012-04-24 15:47:41 +01004574 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004575
4576 if (IS_VALLEYVIEW(dev)) {
4577 /* VLVA0 (potential hack), BIOS isn't actually waking us */
4578 I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
4579 if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
4580 DRM_DEBUG_DRIVER("allow wake ack timed out\n");
4581 }
4582
Ben Widawskyd7e50082012-12-18 10:31:25 -08004583 i915_gem_init_global_gtt(dev);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004584
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004585 ret = i915_gem_context_init(dev);
Mika Kuoppalae3848692014-01-31 17:14:02 +02004586 if (ret) {
4587 mutex_unlock(&dev->struct_mutex);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004588 return ret;
Mika Kuoppalae3848692014-01-31 17:14:02 +02004589 }
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004590
Chris Wilson1070a422012-04-24 15:47:41 +01004591 ret = i915_gem_init_hw(dev);
4592 mutex_unlock(&dev->struct_mutex);
4593 if (ret) {
Ben Widawskybdf4fd72013-12-06 14:11:18 -08004594 WARN_ON(dev_priv->mm.aliasing_ppgtt);
Ben Widawsky2fa48d82013-12-06 14:11:04 -08004595 i915_gem_context_fini(dev);
Ben Widawskyc39538a2013-12-06 14:10:50 -08004596 drm_mm_takedown(&dev_priv->gtt.base.mm);
Chris Wilson1070a422012-04-24 15:47:41 +01004597 return ret;
4598 }
4599
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004600 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
4601 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4602 dev_priv->dri1.allow_batchbuffer = 1;
Chris Wilson1070a422012-04-24 15:47:41 +01004603 return 0;
4604}
4605
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004606void
4607i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4608{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004609 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01004610 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004611 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004612
Chris Wilsonb4519512012-05-11 14:29:30 +01004613 for_each_ring(ring, dev_priv, i)
4614 intel_cleanup_ring_buffer(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004615}
4616
4617int
Eric Anholt673a3942008-07-30 12:06:12 -07004618i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4619 struct drm_file *file_priv)
4620{
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004621 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01004622 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004623
Jesse Barnes79e53942008-11-07 14:24:08 -08004624 if (drm_core_check_feature(dev, DRIVER_MODESET))
4625 return 0;
4626
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004627 if (i915_reset_in_progress(&dev_priv->gpu_error)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004628 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004629 atomic_set(&dev_priv->gpu_error.reset_counter, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004630 }
4631
Eric Anholt673a3942008-07-30 12:06:12 -07004632 mutex_lock(&dev->struct_mutex);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004633 dev_priv->ums.mm_suspended = 0;
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004634
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004635 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004636 if (ret != 0) {
4637 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004638 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004639 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004640
Ben Widawsky5cef07e2013-07-16 16:50:08 -07004641 BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004642 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004643
Chris Wilson5f353082010-06-07 14:03:03 +01004644 ret = drm_irq_install(dev);
4645 if (ret)
4646 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004647
Eric Anholt673a3942008-07-30 12:06:12 -07004648 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004649
4650cleanup_ringbuffer:
4651 mutex_lock(&dev->struct_mutex);
4652 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004653 dev_priv->ums.mm_suspended = 1;
Chris Wilson5f353082010-06-07 14:03:03 +01004654 mutex_unlock(&dev->struct_mutex);
4655
4656 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004657}
4658
4659int
4660i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4661 struct drm_file *file_priv)
4662{
Jesse Barnes79e53942008-11-07 14:24:08 -08004663 if (drm_core_check_feature(dev, DRIVER_MODESET))
4664 return 0;
4665
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004666 drm_irq_uninstall(dev);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004667
Chris Wilson45c5f202013-10-16 11:50:01 +01004668 return i915_gem_suspend(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004669}
4670
4671void
4672i915_gem_lastclose(struct drm_device *dev)
4673{
4674 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004675
Eric Anholte806b492009-01-22 09:56:58 -08004676 if (drm_core_check_feature(dev, DRIVER_MODESET))
4677 return;
4678
Chris Wilson45c5f202013-10-16 11:50:01 +01004679 ret = i915_gem_suspend(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004680 if (ret)
4681 DRM_ERROR("failed to idle hardware: %d\n", ret);
Eric Anholt673a3942008-07-30 12:06:12 -07004682}
4683
Chris Wilson64193402010-10-24 12:38:05 +01004684static void
4685init_ring_lists(struct intel_ring_buffer *ring)
4686{
4687 INIT_LIST_HEAD(&ring->active_list);
4688 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004689}
4690
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08004691void i915_init_vm(struct drm_i915_private *dev_priv,
4692 struct i915_address_space *vm)
Ben Widawskyfc8c0672013-07-31 16:59:54 -07004693{
Ben Widawsky7e0d96b2013-12-06 14:11:26 -08004694 if (!i915_is_ggtt(vm))
4695 drm_mm_init(&vm->mm, vm->start, vm->total);
Ben Widawskyfc8c0672013-07-31 16:59:54 -07004696 vm->dev = dev_priv->dev;
4697 INIT_LIST_HEAD(&vm->active_list);
4698 INIT_LIST_HEAD(&vm->inactive_list);
4699 INIT_LIST_HEAD(&vm->global_link);
Chris Wilsonf72d21e2014-01-09 22:57:22 +00004700 list_add_tail(&vm->global_link, &dev_priv->vm_list);
Ben Widawskyfc8c0672013-07-31 16:59:54 -07004701}
4702
Eric Anholt673a3942008-07-30 12:06:12 -07004703void
4704i915_gem_load(struct drm_device *dev)
4705{
Jani Nikula3e31c6c2014-03-31 14:27:16 +03004706 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004707 int i;
4708
4709 dev_priv->slab =
4710 kmem_cache_create("i915_gem_object",
4711 sizeof(struct drm_i915_gem_object), 0,
4712 SLAB_HWCACHE_ALIGN,
4713 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004714
Ben Widawskyfc8c0672013-07-31 16:59:54 -07004715 INIT_LIST_HEAD(&dev_priv->vm_list);
4716 i915_init_vm(dev_priv, &dev_priv->gtt.base);
4717
Ben Widawskya33afea2013-09-17 21:12:45 -07004718 INIT_LIST_HEAD(&dev_priv->context_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004719 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4720 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004721 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004722 for (i = 0; i < I915_NUM_RINGS; i++)
4723 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004724 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004725 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004726 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4727 i915_gem_retire_work_handler);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004728 INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
4729 i915_gem_idle_work_handler);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004730 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004731
Dave Airlie94400122010-07-20 13:15:31 +10004732 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4733 if (IS_GEN3(dev)) {
Daniel Vetter50743292012-04-26 22:02:54 +02004734 I915_WRITE(MI_ARB_STATE,
4735 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Dave Airlie94400122010-07-20 13:15:31 +10004736 }
4737
Chris Wilson72bfa192010-12-19 11:42:05 +00004738 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4739
Jesse Barnesde151cf2008-11-12 10:03:55 -08004740 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004741 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4742 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004743
Ville Syrjälä42b5aea2013-04-09 13:02:47 +03004744 if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
4745 dev_priv->num_fence_regs = 32;
4746 else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004747 dev_priv->num_fence_regs = 16;
4748 else
4749 dev_priv->num_fence_regs = 8;
4750
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004751 /* Initialize fence registers to zero */
Chris Wilson19b2dbd2013-06-12 10:15:12 +01004752 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4753 i915_gem_restore_fences(dev);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004754
Eric Anholt673a3942008-07-30 12:06:12 -07004755 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004756 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004757
Chris Wilsonce453d82011-02-21 14:43:56 +00004758 dev_priv->mm.interruptible = true;
4759
Dave Chinner7dc19d52013-08-28 10:18:11 +10004760 dev_priv->mm.inactive_shrinker.scan_objects = i915_gem_inactive_scan;
4761 dev_priv->mm.inactive_shrinker.count_objects = i915_gem_inactive_count;
Chris Wilson17250b72010-10-28 12:51:39 +01004762 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4763 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004764}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004765
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004766void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004767{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004768 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004769
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004770 cancel_delayed_work_sync(&file_priv->mm.idle_work);
4771
Eric Anholtb9624422009-06-03 07:27:35 +00004772 /* Clean up our request list when the client is going away, so that
4773 * later retire_requests won't dereference our soon-to-be-gone
4774 * file_priv.
4775 */
Chris Wilson1c255952010-09-26 11:03:27 +01004776 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004777 while (!list_empty(&file_priv->mm.request_list)) {
4778 struct drm_i915_gem_request *request;
4779
4780 request = list_first_entry(&file_priv->mm.request_list,
4781 struct drm_i915_gem_request,
4782 client_list);
4783 list_del(&request->client_list);
4784 request->file_priv = NULL;
4785 }
Chris Wilson1c255952010-09-26 11:03:27 +01004786 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004787}
Chris Wilson31169712009-09-14 16:50:28 +01004788
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004789static void
4790i915_gem_file_idle_work_handler(struct work_struct *work)
4791{
4792 struct drm_i915_file_private *file_priv =
4793 container_of(work, typeof(*file_priv), mm.idle_work.work);
4794
4795 atomic_set(&file_priv->rps_wait_boost, false);
4796}
4797
4798int i915_gem_open(struct drm_device *dev, struct drm_file *file)
4799{
4800 struct drm_i915_file_private *file_priv;
Ben Widawskye422b882013-12-06 14:10:58 -08004801 int ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004802
4803 DRM_DEBUG_DRIVER("\n");
4804
4805 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
4806 if (!file_priv)
4807 return -ENOMEM;
4808
4809 file->driver_priv = file_priv;
4810 file_priv->dev_priv = dev->dev_private;
Chris Wilsonab0e7ff2014-02-25 17:11:24 +02004811 file_priv->file = file;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004812
4813 spin_lock_init(&file_priv->mm.lock);
4814 INIT_LIST_HEAD(&file_priv->mm.request_list);
4815 INIT_DELAYED_WORK(&file_priv->mm.idle_work,
4816 i915_gem_file_idle_work_handler);
4817
Ben Widawskye422b882013-12-06 14:10:58 -08004818 ret = i915_gem_context_open(dev, file);
4819 if (ret)
4820 kfree(file_priv);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004821
Ben Widawskye422b882013-12-06 14:10:58 -08004822 return ret;
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004823}
4824
Chris Wilson57745062012-11-21 13:04:04 +00004825static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
4826{
4827 if (!mutex_is_locked(mutex))
4828 return false;
4829
4830#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
4831 return mutex->owner == task;
4832#else
4833 /* Since UP may be pre-empted, we cannot assume that we own the lock */
4834 return false;
4835#endif
4836}
4837
Dave Chinner7dc19d52013-08-28 10:18:11 +10004838static unsigned long
4839i915_gem_inactive_count(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004840{
Chris Wilson17250b72010-10-28 12:51:39 +01004841 struct drm_i915_private *dev_priv =
4842 container_of(shrinker,
4843 struct drm_i915_private,
4844 mm.inactive_shrinker);
4845 struct drm_device *dev = dev_priv->dev;
Chris Wilson6c085a72012-08-20 11:40:46 +02004846 struct drm_i915_gem_object *obj;
Chris Wilson57745062012-11-21 13:04:04 +00004847 bool unlock = true;
Dave Chinner7dc19d52013-08-28 10:18:11 +10004848 unsigned long count;
Chris Wilson17250b72010-10-28 12:51:39 +01004849
Chris Wilson57745062012-11-21 13:04:04 +00004850 if (!mutex_trylock(&dev->struct_mutex)) {
4851 if (!mutex_is_locked_by(&dev->struct_mutex, current))
Daniel Vetterd3227042013-09-25 14:00:02 +02004852 return 0;
Chris Wilson57745062012-11-21 13:04:04 +00004853
Daniel Vetter677feac2012-12-19 14:33:45 +01004854 if (dev_priv->mm.shrinker_no_lock_stealing)
Daniel Vetterd3227042013-09-25 14:00:02 +02004855 return 0;
Daniel Vetter677feac2012-12-19 14:33:45 +01004856
Chris Wilson57745062012-11-21 13:04:04 +00004857 unlock = false;
4858 }
Chris Wilson31169712009-09-14 16:50:28 +01004859
Dave Chinner7dc19d52013-08-28 10:18:11 +10004860 count = 0;
Ben Widawsky35c20a62013-05-31 11:28:48 -07004861 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
Chris Wilsona5570172012-09-04 21:02:54 +01004862 if (obj->pages_pin_count == 0)
Dave Chinner7dc19d52013-08-28 10:18:11 +10004863 count += obj->base.size >> PAGE_SHIFT;
Ben Widawskyfcb4a572013-07-31 16:59:57 -07004864
4865 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
4866 if (obj->active)
4867 continue;
4868
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004869 if (!i915_gem_obj_is_pinned(obj) && obj->pages_pin_count == 0)
Dave Chinner7dc19d52013-08-28 10:18:11 +10004870 count += obj->base.size >> PAGE_SHIFT;
Ben Widawskyfcb4a572013-07-31 16:59:57 -07004871 }
Chris Wilson31169712009-09-14 16:50:28 +01004872
Chris Wilson57745062012-11-21 13:04:04 +00004873 if (unlock)
4874 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9973b42013-10-04 10:33:00 +01004875
Dave Chinner7dc19d52013-08-28 10:18:11 +10004876 return count;
Chris Wilson31169712009-09-14 16:50:28 +01004877}
Ben Widawskya70a3142013-07-31 16:59:56 -07004878
4879/* All the new VM stuff */
4880unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o,
4881 struct i915_address_space *vm)
4882{
4883 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
4884 struct i915_vma *vma;
4885
Ben Widawsky6f425322013-12-06 14:10:48 -08004886 if (!dev_priv->mm.aliasing_ppgtt ||
4887 vm == &dev_priv->mm.aliasing_ppgtt->base)
Ben Widawskya70a3142013-07-31 16:59:56 -07004888 vm = &dev_priv->gtt.base;
4889
4890 BUG_ON(list_empty(&o->vma_list));
4891 list_for_each_entry(vma, &o->vma_list, vma_link) {
4892 if (vma->vm == vm)
4893 return vma->node.start;
4894
4895 }
4896 return -1;
4897}
4898
4899bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
4900 struct i915_address_space *vm)
4901{
4902 struct i915_vma *vma;
4903
4904 list_for_each_entry(vma, &o->vma_list, vma_link)
Ben Widawsky8b9c2b92013-07-31 17:00:16 -07004905 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07004906 return true;
4907
4908 return false;
4909}
4910
4911bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
4912{
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01004913 struct i915_vma *vma;
Ben Widawskya70a3142013-07-31 16:59:56 -07004914
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01004915 list_for_each_entry(vma, &o->vma_list, vma_link)
4916 if (drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07004917 return true;
4918
4919 return false;
4920}
4921
4922unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
4923 struct i915_address_space *vm)
4924{
4925 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
4926 struct i915_vma *vma;
4927
Ben Widawsky6f425322013-12-06 14:10:48 -08004928 if (!dev_priv->mm.aliasing_ppgtt ||
4929 vm == &dev_priv->mm.aliasing_ppgtt->base)
Ben Widawskya70a3142013-07-31 16:59:56 -07004930 vm = &dev_priv->gtt.base;
4931
4932 BUG_ON(list_empty(&o->vma_list));
4933
4934 list_for_each_entry(vma, &o->vma_list, vma_link)
4935 if (vma->vm == vm)
4936 return vma->node.size;
4937
4938 return 0;
4939}
4940
Dave Chinner7dc19d52013-08-28 10:18:11 +10004941static unsigned long
4942i915_gem_inactive_scan(struct shrinker *shrinker, struct shrink_control *sc)
4943{
4944 struct drm_i915_private *dev_priv =
4945 container_of(shrinker,
4946 struct drm_i915_private,
4947 mm.inactive_shrinker);
4948 struct drm_device *dev = dev_priv->dev;
Dave Chinner7dc19d52013-08-28 10:18:11 +10004949 unsigned long freed;
4950 bool unlock = true;
4951
4952 if (!mutex_trylock(&dev->struct_mutex)) {
4953 if (!mutex_is_locked_by(&dev->struct_mutex, current))
Daniel Vetterd3227042013-09-25 14:00:02 +02004954 return SHRINK_STOP;
Dave Chinner7dc19d52013-08-28 10:18:11 +10004955
4956 if (dev_priv->mm.shrinker_no_lock_stealing)
Daniel Vetterd3227042013-09-25 14:00:02 +02004957 return SHRINK_STOP;
Dave Chinner7dc19d52013-08-28 10:18:11 +10004958
4959 unlock = false;
4960 }
4961
Chris Wilsond9973b42013-10-04 10:33:00 +01004962 freed = i915_gem_purge(dev_priv, sc->nr_to_scan);
4963 if (freed < sc->nr_to_scan)
4964 freed += __i915_gem_shrink(dev_priv,
4965 sc->nr_to_scan - freed,
4966 false);
4967 if (freed < sc->nr_to_scan)
Dave Chinner7dc19d52013-08-28 10:18:11 +10004968 freed += i915_gem_shrink_all(dev_priv);
4969
4970 if (unlock)
4971 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9973b42013-10-04 10:33:00 +01004972
Dave Chinner7dc19d52013-08-28 10:18:11 +10004973 return freed;
4974}
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07004975
4976struct i915_vma *i915_gem_obj_to_ggtt(struct drm_i915_gem_object *obj)
4977{
4978 struct i915_vma *vma;
4979
4980 if (WARN_ON(list_empty(&obj->vma_list)))
4981 return NULL;
4982
4983 vma = list_first_entry(&obj->vma_list, typeof(*vma), vma_link);
Ben Widawsky6e164c32013-12-06 14:10:49 -08004984 if (vma->vm != obj_to_ggtt(obj))
Ben Widawsky5c2abbe2013-09-24 09:57:57 -07004985 return NULL;
4986
4987 return vma;
4988}