blob: d00d24f7a9765773c647f0ae6379e779d8e888a8 [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);
46static __must_check int
Ben Widawsky07fe0b12013-07-31 17:00:10 -070047i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
48 struct i915_address_space *vm,
49 unsigned alignment,
50 bool map_and_fenceable,
51 bool nonblocking);
Chris Wilson05394f32010-11-08 19:18:58 +000052static int i915_gem_phys_pwrite(struct drm_device *dev,
53 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100054 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000055 struct drm_file *file);
Eric Anholt673a3942008-07-30 12:06:12 -070056
Chris Wilson61050802012-04-17 15:31:31 +010057static void i915_gem_write_fence(struct drm_device *dev, int reg,
58 struct drm_i915_gem_object *obj);
59static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
60 struct drm_i915_fence_reg *fence,
61 bool enable);
62
Chris Wilson17250b72010-10-28 12:51:39 +010063static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070064 struct shrink_control *sc);
Chris Wilson6c085a72012-08-20 11:40:46 +020065static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
66static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
Daniel Vetter8c599672011-12-14 13:57:31 +010067static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
Chris Wilson31169712009-09-14 16:50:28 +010068
Chris Wilsonc76ce032013-08-08 14:41:03 +010069static bool cpu_cache_is_coherent(struct drm_device *dev,
70 enum i915_cache_level level)
71{
72 return HAS_LLC(dev) || level != I915_CACHE_NONE;
73}
74
Chris Wilson2c225692013-08-09 12:26:45 +010075static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
76{
77 if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
78 return true;
79
80 return obj->pin_display;
81}
82
Chris Wilson61050802012-04-17 15:31:31 +010083static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
84{
85 if (obj->tiling_mode)
86 i915_gem_release_mmap(obj);
87
88 /* As we do not have an associated fence register, we will force
89 * a tiling change if we ever need to acquire one.
90 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +010091 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +010092 obj->fence_reg = I915_FENCE_REG_NONE;
93}
94
Chris Wilson73aa8082010-09-30 11:46:12 +010095/* some bookkeeping */
96static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
97 size_t size)
98{
Daniel Vetterc20e8352013-07-24 22:40:23 +020099 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100100 dev_priv->mm.object_count++;
101 dev_priv->mm.object_memory += size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200102 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100103}
104
105static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
106 size_t size)
107{
Daniel Vetterc20e8352013-07-24 22:40:23 +0200108 spin_lock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100109 dev_priv->mm.object_count--;
110 dev_priv->mm.object_memory -= size;
Daniel Vetterc20e8352013-07-24 22:40:23 +0200111 spin_unlock(&dev_priv->mm.object_stat_lock);
Chris Wilson73aa8082010-09-30 11:46:12 +0100112}
113
Chris Wilson21dd3732011-01-26 15:55:56 +0000114static int
Daniel Vetter33196de2012-11-14 17:14:05 +0100115i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100116{
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117 int ret;
118
Daniel Vetter7abb6902013-05-24 21:29:32 +0200119#define EXIT_COND (!i915_reset_in_progress(error) || \
120 i915_terminally_wedged(error))
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100121 if (EXIT_COND)
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100122 return 0;
123
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200124 /*
125 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
126 * userspace. If it takes that long something really bad is going on and
127 * we should simply try to bail out and fail as gracefully as possible.
128 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100129 ret = wait_event_interruptible_timeout(error->reset_queue,
130 EXIT_COND,
131 10*HZ);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200132 if (ret == 0) {
133 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
134 return -EIO;
135 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100136 return ret;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200137 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100138#undef EXIT_COND
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100139
Chris Wilson21dd3732011-01-26 15:55:56 +0000140 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100141}
142
Chris Wilson54cf91d2010-11-25 18:00:26 +0000143int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100144{
Daniel Vetter33196de2012-11-14 17:14:05 +0100145 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100146 int ret;
147
Daniel Vetter33196de2012-11-14 17:14:05 +0100148 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100149 if (ret)
150 return ret;
151
152 ret = mutex_lock_interruptible(&dev->struct_mutex);
153 if (ret)
154 return ret;
155
Chris Wilson23bc5982010-09-29 16:10:57 +0100156 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100157 return 0;
158}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100159
Chris Wilson7d1c4802010-08-07 21:45:03 +0100160static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000161i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100162{
Ben Widawsky98438772013-07-31 17:00:12 -0700163 return i915_gem_obj_bound_any(obj) && !obj->active;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100164}
165
Eric Anholt673a3942008-07-30 12:06:12 -0700166int
167i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000168 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700169{
Ben Widawsky93d18792013-01-17 12:45:17 -0800170 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700171 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000172
Daniel Vetter7bb6fb82012-04-24 08:22:52 +0200173 if (drm_core_check_feature(dev, DRIVER_MODESET))
174 return -ENODEV;
175
Chris Wilson20217462010-11-23 15:26:33 +0000176 if (args->gtt_start >= args->gtt_end ||
177 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
178 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700179
Daniel Vetterf534bc02012-03-26 22:37:04 +0200180 /* GEM with user mode setting was never supported on ilk and later. */
181 if (INTEL_INFO(dev)->gen >= 5)
182 return -ENODEV;
183
Eric Anholt673a3942008-07-30 12:06:12 -0700184 mutex_lock(&dev->struct_mutex);
Ben Widawskyd7e50082012-12-18 10:31:25 -0800185 i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
186 args->gtt_end);
Ben Widawsky93d18792013-01-17 12:45:17 -0800187 dev_priv->gtt.mappable_end = args->gtt_end;
Eric Anholt673a3942008-07-30 12:06:12 -0700188 mutex_unlock(&dev->struct_mutex);
189
Chris Wilson20217462010-11-23 15:26:33 +0000190 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700191}
192
Eric Anholt5a125c32008-10-22 21:40:13 -0700193int
194i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000195 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700196{
Chris Wilson73aa8082010-09-30 11:46:12 +0100197 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700198 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000199 struct drm_i915_gem_object *obj;
200 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700201
Chris Wilson6299f992010-11-24 12:23:44 +0000202 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100203 mutex_lock(&dev->struct_mutex);
Ben Widawsky35c20a62013-05-31 11:28:48 -0700204 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
Chris Wilson1b502472012-04-24 15:47:30 +0100205 if (obj->pin_count)
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700206 pinned += i915_gem_obj_ggtt_size(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +0100207 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700208
Ben Widawsky853ba5d2013-07-16 16:50:05 -0700209 args->aper_size = dev_priv->gtt.base.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400210 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000211
Eric Anholt5a125c32008-10-22 21:40:13 -0700212 return 0;
213}
214
Chris Wilson42dcedd2012-11-15 11:32:30 +0000215void *i915_gem_object_alloc(struct drm_device *dev)
216{
217 struct drm_i915_private *dev_priv = dev->dev_private;
Joe Perchesfac15c12013-08-29 13:11:07 -0700218 return kmem_cache_zalloc(dev_priv->slab, GFP_KERNEL);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000219}
220
221void i915_gem_object_free(struct drm_i915_gem_object *obj)
222{
223 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
224 kmem_cache_free(dev_priv->slab, obj);
225}
226
Dave Airlieff72145b2011-02-07 12:16:14 +1000227static int
228i915_gem_create(struct drm_file *file,
229 struct drm_device *dev,
230 uint64_t size,
231 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700232{
Chris Wilson05394f32010-11-08 19:18:58 +0000233 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300234 int ret;
235 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700236
Dave Airlieff72145b2011-02-07 12:16:14 +1000237 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200238 if (size == 0)
239 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700240
241 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000242 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700243 if (obj == NULL)
244 return -ENOMEM;
245
Chris Wilson05394f32010-11-08 19:18:58 +0000246 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100247 /* drop reference from allocate - handle holds it now */
Daniel Vetterd861e332013-07-24 23:25:03 +0200248 drm_gem_object_unreference_unlocked(&obj->base);
249 if (ret)
250 return ret;
Chris Wilson202f2fe2010-10-14 13:20:40 +0100251
Dave Airlieff72145b2011-02-07 12:16:14 +1000252 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700253 return 0;
254}
255
Dave Airlieff72145b2011-02-07 12:16:14 +1000256int
257i915_gem_dumb_create(struct drm_file *file,
258 struct drm_device *dev,
259 struct drm_mode_create_dumb *args)
260{
261 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000262 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000263 args->size = args->pitch * args->height;
264 return i915_gem_create(file, dev,
265 args->size, &args->handle);
266}
267
Dave Airlieff72145b2011-02-07 12:16:14 +1000268/**
269 * Creates a new mm object and returns a handle to it.
270 */
271int
272i915_gem_create_ioctl(struct drm_device *dev, void *data,
273 struct drm_file *file)
274{
275 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200276
Dave Airlieff72145b2011-02-07 12:16:14 +1000277 return i915_gem_create(file, dev,
278 args->size, &args->handle);
279}
280
Daniel Vetter8c599672011-12-14 13:57:31 +0100281static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100282__copy_to_user_swizzled(char __user *cpu_vaddr,
283 const char *gpu_vaddr, int gpu_offset,
284 int length)
285{
286 int ret, cpu_offset = 0;
287
288 while (length > 0) {
289 int cacheline_end = ALIGN(gpu_offset + 1, 64);
290 int this_length = min(cacheline_end - gpu_offset, length);
291 int swizzled_gpu_offset = gpu_offset ^ 64;
292
293 ret = __copy_to_user(cpu_vaddr + cpu_offset,
294 gpu_vaddr + swizzled_gpu_offset,
295 this_length);
296 if (ret)
297 return ret + length;
298
299 cpu_offset += this_length;
300 gpu_offset += this_length;
301 length -= this_length;
302 }
303
304 return 0;
305}
306
307static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700308__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
309 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100310 int length)
311{
312 int ret, cpu_offset = 0;
313
314 while (length > 0) {
315 int cacheline_end = ALIGN(gpu_offset + 1, 64);
316 int this_length = min(cacheline_end - gpu_offset, length);
317 int swizzled_gpu_offset = gpu_offset ^ 64;
318
319 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
320 cpu_vaddr + cpu_offset,
321 this_length);
322 if (ret)
323 return ret + length;
324
325 cpu_offset += this_length;
326 gpu_offset += this_length;
327 length -= this_length;
328 }
329
330 return 0;
331}
332
Daniel Vetterd174bd62012-03-25 19:47:40 +0200333/* Per-page copy function for the shmem pread fastpath.
334 * Flushes invalid cachelines before reading the target if
335 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700336static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200337shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
338 char __user *user_data,
339 bool page_do_bit17_swizzling, bool needs_clflush)
340{
341 char *vaddr;
342 int ret;
343
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200344 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200345 return -EINVAL;
346
347 vaddr = kmap_atomic(page);
348 if (needs_clflush)
349 drm_clflush_virt_range(vaddr + shmem_page_offset,
350 page_length);
351 ret = __copy_to_user_inatomic(user_data,
352 vaddr + shmem_page_offset,
353 page_length);
354 kunmap_atomic(vaddr);
355
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100356 return ret ? -EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200357}
358
Daniel Vetter23c18c72012-03-25 19:47:42 +0200359static void
360shmem_clflush_swizzled_range(char *addr, unsigned long length,
361 bool swizzled)
362{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200363 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200364 unsigned long start = (unsigned long) addr;
365 unsigned long end = (unsigned long) addr + length;
366
367 /* For swizzling simply ensure that we always flush both
368 * channels. Lame, but simple and it works. Swizzled
369 * pwrite/pread is far from a hotpath - current userspace
370 * doesn't use it at all. */
371 start = round_down(start, 128);
372 end = round_up(end, 128);
373
374 drm_clflush_virt_range((void *)start, end - start);
375 } else {
376 drm_clflush_virt_range(addr, length);
377 }
378
379}
380
Daniel Vetterd174bd62012-03-25 19:47:40 +0200381/* Only difference to the fast-path function is that this can handle bit17
382 * and uses non-atomic copy and kmap functions. */
383static int
384shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
385 char __user *user_data,
386 bool page_do_bit17_swizzling, bool needs_clflush)
387{
388 char *vaddr;
389 int ret;
390
391 vaddr = kmap(page);
392 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200393 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
394 page_length,
395 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200396
397 if (page_do_bit17_swizzling)
398 ret = __copy_to_user_swizzled(user_data,
399 vaddr, shmem_page_offset,
400 page_length);
401 else
402 ret = __copy_to_user(user_data,
403 vaddr + shmem_page_offset,
404 page_length);
405 kunmap(page);
406
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100407 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200408}
409
Eric Anholteb014592009-03-10 11:44:52 -0700410static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200411i915_gem_shmem_pread(struct drm_device *dev,
412 struct drm_i915_gem_object *obj,
413 struct drm_i915_gem_pread *args,
414 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700415{
Daniel Vetter8461d222011-12-14 13:57:32 +0100416 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700417 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100418 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100419 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100420 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200421 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200422 int needs_clflush = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200423 struct sg_page_iter sg_iter;
Eric Anholteb014592009-03-10 11:44:52 -0700424
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200425 user_data = to_user_ptr(args->data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700426 remain = args->size;
427
Daniel Vetter8461d222011-12-14 13:57:32 +0100428 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700429
Daniel Vetter84897312012-03-25 19:47:31 +0200430 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
431 /* If we're not in the cpu read domain, set ourself into the gtt
432 * read domain and manually flush cachelines (if required). This
433 * optimizes for the case when the gpu will dirty the data
434 * anyway again before the next pread happens. */
Chris Wilsonc76ce032013-08-08 14:41:03 +0100435 needs_clflush = !cpu_cache_is_coherent(dev, obj->cache_level);
Ben Widawsky23f54482013-09-11 14:57:48 -0700436 ret = i915_gem_object_wait_rendering(obj, true);
437 if (ret)
438 return ret;
Daniel Vetter84897312012-03-25 19:47:31 +0200439 }
Eric Anholteb014592009-03-10 11:44:52 -0700440
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100441 ret = i915_gem_object_get_pages(obj);
442 if (ret)
443 return ret;
444
445 i915_gem_object_pin_pages(obj);
446
Eric Anholteb014592009-03-10 11:44:52 -0700447 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100448
Imre Deak67d5a502013-02-18 19:28:02 +0200449 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
450 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200451 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +0100452
453 if (remain <= 0)
454 break;
455
Eric Anholteb014592009-03-10 11:44:52 -0700456 /* Operation in this page
457 *
Eric Anholteb014592009-03-10 11:44:52 -0700458 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700459 * page_length = bytes to copy for this page
460 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100461 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700462 page_length = remain;
463 if ((shmem_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700465
Daniel Vetter8461d222011-12-14 13:57:32 +0100466 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
467 (page_to_phys(page) & (1 << 17)) != 0;
468
Daniel Vetterd174bd62012-03-25 19:47:40 +0200469 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
470 user_data, page_do_bit17_swizzling,
471 needs_clflush);
472 if (ret == 0)
473 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700474
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200475 mutex_unlock(&dev->struct_mutex);
476
Xiong Zhang0b74b502013-07-19 13:51:24 +0800477 if (likely(!i915_prefault_disable) && !prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200478 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200479 /* Userspace is tricking us, but we've already clobbered
480 * its pages with the prefault and promised to write the
481 * data up to the first fault. Hence ignore any errors
482 * and just continue. */
483 (void)ret;
484 prefaulted = 1;
485 }
486
Daniel Vetterd174bd62012-03-25 19:47:40 +0200487 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
488 user_data, page_do_bit17_swizzling,
489 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700490
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200491 mutex_lock(&dev->struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100492
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200493next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100494 mark_page_accessed(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100495
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100496 if (ret)
Daniel Vetter8461d222011-12-14 13:57:32 +0100497 goto out;
Daniel Vetter8461d222011-12-14 13:57:32 +0100498
Eric Anholteb014592009-03-10 11:44:52 -0700499 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100500 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700501 offset += page_length;
502 }
503
Chris Wilson4f27b752010-10-14 15:26:45 +0100504out:
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100505 i915_gem_object_unpin_pages(obj);
506
Eric Anholteb014592009-03-10 11:44:52 -0700507 return ret;
508}
509
Eric Anholt673a3942008-07-30 12:06:12 -0700510/**
511 * Reads data from the object referenced by handle.
512 *
513 * On error, the contents of *data are undefined.
514 */
515int
516i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000517 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700518{
519 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000520 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100521 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700522
Chris Wilson51311d02010-11-17 09:10:42 +0000523 if (args->size == 0)
524 return 0;
525
526 if (!access_ok(VERIFY_WRITE,
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200527 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000528 args->size))
529 return -EFAULT;
530
Chris Wilson4f27b752010-10-14 15:26:45 +0100531 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100532 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100533 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700534
Chris Wilson05394f32010-11-08 19:18:58 +0000535 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000536 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100537 ret = -ENOENT;
538 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100539 }
Eric Anholt673a3942008-07-30 12:06:12 -0700540
Chris Wilson7dcd2492010-09-26 20:21:44 +0100541 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000542 if (args->offset > obj->base.size ||
543 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100544 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100545 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100546 }
547
Daniel Vetter1286ff72012-05-10 15:25:09 +0200548 /* prime objects have no backing filp to GEM pread/pwrite
549 * pages from.
550 */
551 if (!obj->base.filp) {
552 ret = -EINVAL;
553 goto out;
554 }
555
Chris Wilsondb53a302011-02-03 11:57:46 +0000556 trace_i915_gem_object_pread(obj, args->offset, args->size);
557
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200558 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700559
Chris Wilson35b62a82010-09-26 20:23:38 +0100560out:
Chris Wilson05394f32010-11-08 19:18:58 +0000561 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100562unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100563 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700564 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700565}
566
Keith Packard0839ccb2008-10-30 19:38:48 -0700567/* This is the fast write path which cannot handle
568 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700569 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700570
Keith Packard0839ccb2008-10-30 19:38:48 -0700571static inline int
572fast_user_write(struct io_mapping *mapping,
573 loff_t page_base, int page_offset,
574 char __user *user_data,
575 int length)
576{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700577 void __iomem *vaddr_atomic;
578 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700579 unsigned long unwritten;
580
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700581 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700582 /* We can use the cpu mem copy function because this is X86. */
583 vaddr = (void __force*)vaddr_atomic + page_offset;
584 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700586 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100587 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700588}
589
Eric Anholt3de09aa2009-03-09 09:42:23 -0700590/**
591 * This is the fast pwrite path, where we copy the data directly from the
592 * user into the GTT, uncached.
593 */
Eric Anholt673a3942008-07-30 12:06:12 -0700594static int
Chris Wilson05394f32010-11-08 19:18:58 +0000595i915_gem_gtt_pwrite_fast(struct drm_device *dev,
596 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700597 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000598 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700599{
Keith Packard0839ccb2008-10-30 19:38:48 -0700600 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700601 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700602 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700603 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200604 int page_offset, page_length, ret;
605
Ben Widawskyc37e2202013-07-31 16:59:58 -0700606 ret = i915_gem_obj_ggtt_pin(obj, 0, true, true);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200607 if (ret)
608 goto out;
609
610 ret = i915_gem_object_set_to_gtt_domain(obj, true);
611 if (ret)
612 goto out_unpin;
613
614 ret = i915_gem_object_put_fence(obj);
615 if (ret)
616 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700617
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200618 user_data = to_user_ptr(args->data_ptr);
Eric Anholt673a3942008-07-30 12:06:12 -0700619 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700620
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700621 offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700622
623 while (remain > 0) {
624 /* Operation in this page
625 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700626 * page_base = page offset within aperture
627 * page_offset = offset within page
628 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700629 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100630 page_base = offset & PAGE_MASK;
631 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700632 page_length = remain;
633 if ((page_offset + remain) > PAGE_SIZE)
634 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700635
Keith Packard0839ccb2008-10-30 19:38:48 -0700636 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700637 * source page isn't available. Return the error and we'll
638 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700639 */
Ben Widawsky5d4545a2013-01-17 12:45:15 -0800640 if (fast_user_write(dev_priv->gtt.mappable, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200641 page_offset, user_data, page_length)) {
642 ret = -EFAULT;
643 goto out_unpin;
644 }
Eric Anholt673a3942008-07-30 12:06:12 -0700645
Keith Packard0839ccb2008-10-30 19:38:48 -0700646 remain -= page_length;
647 user_data += page_length;
648 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700649 }
Eric Anholt673a3942008-07-30 12:06:12 -0700650
Daniel Vetter935aaa62012-03-25 19:47:35 +0200651out_unpin:
652 i915_gem_object_unpin(obj);
653out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700654 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700655}
656
Daniel Vetterd174bd62012-03-25 19:47:40 +0200657/* Per-page copy function for the shmem pwrite fastpath.
658 * Flushes invalid cachelines before writing to the target if
659 * needs_clflush_before is set and flushes out any written cachelines after
660 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700661static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200662shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
663 char __user *user_data,
664 bool page_do_bit17_swizzling,
665 bool needs_clflush_before,
666 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700667{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200668 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700669 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700670
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200671 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200672 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700673
Daniel Vetterd174bd62012-03-25 19:47:40 +0200674 vaddr = kmap_atomic(page);
675 if (needs_clflush_before)
676 drm_clflush_virt_range(vaddr + shmem_page_offset,
677 page_length);
678 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
679 user_data,
680 page_length);
681 if (needs_clflush_after)
682 drm_clflush_virt_range(vaddr + shmem_page_offset,
683 page_length);
684 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700685
Chris Wilson755d2212012-09-04 21:02:55 +0100686 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700687}
688
Daniel Vetterd174bd62012-03-25 19:47:40 +0200689/* Only difference to the fast-path function is that this can handle bit17
690 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700691static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200692shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
693 char __user *user_data,
694 bool page_do_bit17_swizzling,
695 bool needs_clflush_before,
696 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700697{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200698 char *vaddr;
699 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700700
Daniel Vetterd174bd62012-03-25 19:47:40 +0200701 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200702 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200703 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
704 page_length,
705 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200706 if (page_do_bit17_swizzling)
707 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100708 user_data,
709 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200710 else
711 ret = __copy_from_user(vaddr + shmem_page_offset,
712 user_data,
713 page_length);
714 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200715 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
716 page_length,
717 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200718 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100719
Chris Wilson755d2212012-09-04 21:02:55 +0100720 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700721}
722
Eric Anholt40123c12009-03-09 13:42:30 -0700723static int
Daniel Vettere244a442012-03-25 19:47:28 +0200724i915_gem_shmem_pwrite(struct drm_device *dev,
725 struct drm_i915_gem_object *obj,
726 struct drm_i915_gem_pwrite *args,
727 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700728{
Eric Anholt40123c12009-03-09 13:42:30 -0700729 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100730 loff_t offset;
731 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100732 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100733 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200734 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200735 int needs_clflush_after = 0;
736 int needs_clflush_before = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200737 struct sg_page_iter sg_iter;
Eric Anholt40123c12009-03-09 13:42:30 -0700738
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200739 user_data = to_user_ptr(args->data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700740 remain = args->size;
741
Daniel Vetter8c599672011-12-14 13:57:31 +0100742 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700743
Daniel Vetter58642882012-03-25 19:47:37 +0200744 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
745 /* If we're not in the cpu write domain, set ourself into the gtt
746 * write domain and manually flush cachelines (if required). This
747 * optimizes for the case when the gpu will use the data
748 * right away and we therefore have to clflush anyway. */
Chris Wilson2c225692013-08-09 12:26:45 +0100749 needs_clflush_after = cpu_write_needs_clflush(obj);
Ben Widawsky23f54482013-09-11 14:57:48 -0700750 ret = i915_gem_object_wait_rendering(obj, false);
751 if (ret)
752 return ret;
Daniel Vetter58642882012-03-25 19:47:37 +0200753 }
Chris Wilsonc76ce032013-08-08 14:41:03 +0100754 /* Same trick applies to invalidate partially written cachelines read
755 * before writing. */
756 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
757 needs_clflush_before =
758 !cpu_cache_is_coherent(dev, obj->cache_level);
Daniel Vetter58642882012-03-25 19:47:37 +0200759
Chris Wilson755d2212012-09-04 21:02:55 +0100760 ret = i915_gem_object_get_pages(obj);
761 if (ret)
762 return ret;
763
764 i915_gem_object_pin_pages(obj);
765
Eric Anholt40123c12009-03-09 13:42:30 -0700766 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000767 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700768
Imre Deak67d5a502013-02-18 19:28:02 +0200769 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
770 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200771 struct page *page = sg_page_iter_page(&sg_iter);
Daniel Vetter58642882012-03-25 19:47:37 +0200772 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100773
Chris Wilson9da3da62012-06-01 15:20:22 +0100774 if (remain <= 0)
775 break;
776
Eric Anholt40123c12009-03-09 13:42:30 -0700777 /* Operation in this page
778 *
Eric Anholt40123c12009-03-09 13:42:30 -0700779 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700780 * page_length = bytes to copy for this page
781 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100782 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700783
784 page_length = remain;
785 if ((shmem_page_offset + page_length) > PAGE_SIZE)
786 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700787
Daniel Vetter58642882012-03-25 19:47:37 +0200788 /* If we don't overwrite a cacheline completely we need to be
789 * careful to have up-to-date data by first clflushing. Don't
790 * overcomplicate things and flush the entire patch. */
791 partial_cacheline_write = needs_clflush_before &&
792 ((shmem_page_offset | page_length)
793 & (boot_cpu_data.x86_clflush_size - 1));
794
Daniel Vetter8c599672011-12-14 13:57:31 +0100795 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
796 (page_to_phys(page) & (1 << 17)) != 0;
797
Daniel Vetterd174bd62012-03-25 19:47:40 +0200798 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
799 user_data, page_do_bit17_swizzling,
800 partial_cacheline_write,
801 needs_clflush_after);
802 if (ret == 0)
803 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700804
Daniel Vettere244a442012-03-25 19:47:28 +0200805 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +0200806 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200807 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
808 user_data, page_do_bit17_swizzling,
809 partial_cacheline_write,
810 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700811
Daniel Vettere244a442012-03-25 19:47:28 +0200812 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +0100813
Daniel Vettere244a442012-03-25 19:47:28 +0200814next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100815 set_page_dirty(page);
816 mark_page_accessed(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100817
Chris Wilson755d2212012-09-04 21:02:55 +0100818 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +0100819 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +0100820
Eric Anholt40123c12009-03-09 13:42:30 -0700821 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100822 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700823 offset += page_length;
824 }
825
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100826out:
Chris Wilson755d2212012-09-04 21:02:55 +0100827 i915_gem_object_unpin_pages(obj);
828
Daniel Vettere244a442012-03-25 19:47:28 +0200829 if (hit_slowpath) {
Daniel Vetter8dcf0152012-11-15 16:53:58 +0100830 /*
831 * Fixup: Flush cpu caches in case we didn't flush the dirty
832 * cachelines in-line while writing and the object moved
833 * out of the cpu write domain while we've dropped the lock.
834 */
835 if (!needs_clflush_after &&
836 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilson000433b2013-08-08 14:41:09 +0100837 if (i915_gem_clflush_object(obj, obj->pin_display))
838 i915_gem_chipset_flush(dev);
Daniel Vettere244a442012-03-25 19:47:28 +0200839 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100840 }
Eric Anholt40123c12009-03-09 13:42:30 -0700841
Daniel Vetter58642882012-03-25 19:47:37 +0200842 if (needs_clflush_after)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800843 i915_gem_chipset_flush(dev);
Daniel Vetter58642882012-03-25 19:47:37 +0200844
Eric Anholt40123c12009-03-09 13:42:30 -0700845 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700846}
847
848/**
849 * Writes data to the object referenced by handle.
850 *
851 * On error, the contents of the buffer that were to be modified are undefined.
852 */
853int
854i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100855 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700856{
857 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000858 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000859 int ret;
860
861 if (args->size == 0)
862 return 0;
863
864 if (!access_ok(VERIFY_READ,
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200865 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000866 args->size))
867 return -EFAULT;
868
Xiong Zhang0b74b502013-07-19 13:51:24 +0800869 if (likely(!i915_prefault_disable)) {
870 ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
871 args->size);
872 if (ret)
873 return -EFAULT;
874 }
Eric Anholt673a3942008-07-30 12:06:12 -0700875
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100876 ret = i915_mutex_lock_interruptible(dev);
877 if (ret)
878 return ret;
879
Chris Wilson05394f32010-11-08 19:18:58 +0000880 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000881 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100882 ret = -ENOENT;
883 goto unlock;
884 }
Eric Anholt673a3942008-07-30 12:06:12 -0700885
Chris Wilson7dcd2492010-09-26 20:21:44 +0100886 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000887 if (args->offset > obj->base.size ||
888 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100889 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100890 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100891 }
892
Daniel Vetter1286ff72012-05-10 15:25:09 +0200893 /* prime objects have no backing filp to GEM pread/pwrite
894 * pages from.
895 */
896 if (!obj->base.filp) {
897 ret = -EINVAL;
898 goto out;
899 }
900
Chris Wilsondb53a302011-02-03 11:57:46 +0000901 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
902
Daniel Vetter935aaa62012-03-25 19:47:35 +0200903 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700904 /* We can only do the GTT pwrite on untiled buffers, as otherwise
905 * it would end up going through the fenced access, and we'll get
906 * different detiling behavior between reading and writing.
907 * pread/pwrite currently are reading and writing from the CPU
908 * perspective, requiring manual detiling by the client.
909 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100910 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100911 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100912 goto out;
913 }
914
Chris Wilson2c225692013-08-09 12:26:45 +0100915 if (obj->tiling_mode == I915_TILING_NONE &&
916 obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
917 cpu_write_needs_clflush(obj)) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100918 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200919 /* Note that the gtt paths might fail with non-page-backed user
920 * pointers (e.g. gtt mappings when moving data between
921 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -0700922 }
Eric Anholt673a3942008-07-30 12:06:12 -0700923
Chris Wilson86a1ee22012-08-11 15:41:04 +0100924 if (ret == -EFAULT || ret == -ENOSPC)
Daniel Vetter935aaa62012-03-25 19:47:35 +0200925 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100926
Chris Wilson35b62a82010-09-26 20:23:38 +0100927out:
Chris Wilson05394f32010-11-08 19:18:58 +0000928 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100929unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100930 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700931 return ret;
932}
933
Chris Wilsonb3612372012-08-24 09:35:08 +0100934int
Daniel Vetter33196de2012-11-14 17:14:05 +0100935i915_gem_check_wedge(struct i915_gpu_error *error,
Chris Wilsonb3612372012-08-24 09:35:08 +0100936 bool interruptible)
937{
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100938 if (i915_reset_in_progress(error)) {
Chris Wilsonb3612372012-08-24 09:35:08 +0100939 /* Non-interruptible callers can't handle -EAGAIN, hence return
940 * -EIO unconditionally for these. */
941 if (!interruptible)
942 return -EIO;
943
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100944 /* Recovery complete, but the reset failed ... */
945 if (i915_terminally_wedged(error))
Chris Wilsonb3612372012-08-24 09:35:08 +0100946 return -EIO;
947
948 return -EAGAIN;
949 }
950
951 return 0;
952}
953
954/*
955 * Compare seqno against outstanding lazy request. Emit a request if they are
956 * equal.
957 */
958static int
959i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
960{
961 int ret;
962
963 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
964
965 ret = 0;
Chris Wilson18235212013-09-04 10:45:51 +0100966 if (seqno == ring->outstanding_lazy_seqno)
Mika Kuoppala0025c072013-06-12 12:35:30 +0300967 ret = i915_add_request(ring, NULL);
Chris Wilsonb3612372012-08-24 09:35:08 +0100968
969 return ret;
970}
971
972/**
973 * __wait_seqno - wait until execution of seqno has finished
974 * @ring: the ring expected to report seqno
975 * @seqno: duh!
Daniel Vetterf69061b2012-12-06 09:01:42 +0100976 * @reset_counter: reset sequence associated with the given seqno
Chris Wilsonb3612372012-08-24 09:35:08 +0100977 * @interruptible: do an interruptible wait (normally yes)
978 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
979 *
Daniel Vetterf69061b2012-12-06 09:01:42 +0100980 * Note: It is of utmost importance that the passed in seqno and reset_counter
981 * values have been read by the caller in an smp safe manner. Where read-side
982 * locks are involved, it is sufficient to read the reset_counter before
983 * unlocking the lock that protects the seqno. For lockless tricks, the
984 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
985 * inserted.
986 *
Chris Wilsonb3612372012-08-24 09:35:08 +0100987 * Returns 0 if the seqno was found within the alloted time. Else returns the
988 * errno with remaining time filled in timeout argument.
989 */
990static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
Daniel Vetterf69061b2012-12-06 09:01:42 +0100991 unsigned reset_counter,
Chris Wilsonb3612372012-08-24 09:35:08 +0100992 bool interruptible, struct timespec *timeout)
993{
994 drm_i915_private_t *dev_priv = ring->dev->dev_private;
995 struct timespec before, now, wait_time={1,0};
996 unsigned long timeout_jiffies;
997 long end;
998 bool wait_forever = true;
999 int ret;
1000
Paulo Zanonic67a4702013-08-19 13:18:09 -03001001 WARN(dev_priv->pc8.irqs_disabled, "IRQs disabled\n");
1002
Chris Wilsonb3612372012-08-24 09:35:08 +01001003 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
1004 return 0;
1005
1006 trace_i915_gem_request_wait_begin(ring, seqno);
1007
1008 if (timeout != NULL) {
1009 wait_time = *timeout;
1010 wait_forever = false;
1011 }
1012
Imre Deake054cc32013-05-21 20:03:19 +03001013 timeout_jiffies = timespec_to_jiffies_timeout(&wait_time);
Chris Wilsonb3612372012-08-24 09:35:08 +01001014
1015 if (WARN_ON(!ring->irq_get(ring)))
1016 return -ENODEV;
1017
1018 /* Record current time in case interrupted by signal, or wedged * */
1019 getrawmonotonic(&before);
1020
1021#define EXIT_COND \
1022 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
Daniel Vetterf69061b2012-12-06 09:01:42 +01001023 i915_reset_in_progress(&dev_priv->gpu_error) || \
1024 reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilsonb3612372012-08-24 09:35:08 +01001025 do {
1026 if (interruptible)
1027 end = wait_event_interruptible_timeout(ring->irq_queue,
1028 EXIT_COND,
1029 timeout_jiffies);
1030 else
1031 end = wait_event_timeout(ring->irq_queue, EXIT_COND,
1032 timeout_jiffies);
1033
Daniel Vetterf69061b2012-12-06 09:01:42 +01001034 /* We need to check whether any gpu reset happened in between
1035 * the caller grabbing the seqno and now ... */
1036 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
1037 end = -EAGAIN;
1038
1039 /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
1040 * gone. */
Daniel Vetter33196de2012-11-14 17:14:05 +01001041 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
Chris Wilsonb3612372012-08-24 09:35:08 +01001042 if (ret)
1043 end = ret;
1044 } while (end == 0 && wait_forever);
1045
1046 getrawmonotonic(&now);
1047
1048 ring->irq_put(ring);
1049 trace_i915_gem_request_wait_end(ring, seqno);
1050#undef EXIT_COND
1051
1052 if (timeout) {
1053 struct timespec sleep_time = timespec_sub(now, before);
1054 *timeout = timespec_sub(*timeout, sleep_time);
Chris Wilson4f42f4e2013-04-26 16:22:46 +03001055 if (!timespec_valid(timeout)) /* i.e. negative time remains */
1056 set_normalized_timespec(timeout, 0, 0);
Chris Wilsonb3612372012-08-24 09:35:08 +01001057 }
1058
1059 switch (end) {
1060 case -EIO:
1061 case -EAGAIN: /* Wedged */
1062 case -ERESTARTSYS: /* Signal */
1063 return (int)end;
1064 case 0: /* Timeout */
Chris Wilsonb3612372012-08-24 09:35:08 +01001065 return -ETIME;
1066 default: /* Completed */
1067 WARN_ON(end < 0); /* We're not aware of other errors */
1068 return 0;
1069 }
1070}
1071
1072/**
1073 * Waits for a sequence number to be signaled, and cleans up the
1074 * request and object lists appropriately for that event.
1075 */
1076int
1077i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
1078{
1079 struct drm_device *dev = ring->dev;
1080 struct drm_i915_private *dev_priv = dev->dev_private;
1081 bool interruptible = dev_priv->mm.interruptible;
1082 int ret;
1083
1084 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1085 BUG_ON(seqno == 0);
1086
Daniel Vetter33196de2012-11-14 17:14:05 +01001087 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
Chris Wilsonb3612372012-08-24 09:35:08 +01001088 if (ret)
1089 return ret;
1090
1091 ret = i915_gem_check_olr(ring, seqno);
1092 if (ret)
1093 return ret;
1094
Daniel Vetterf69061b2012-12-06 09:01:42 +01001095 return __wait_seqno(ring, seqno,
1096 atomic_read(&dev_priv->gpu_error.reset_counter),
1097 interruptible, NULL);
Chris Wilsonb3612372012-08-24 09:35:08 +01001098}
1099
Chris Wilsond26e3af2013-06-29 22:05:26 +01001100static int
1101i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
1102 struct intel_ring_buffer *ring)
1103{
1104 i915_gem_retire_requests_ring(ring);
1105
1106 /* Manually manage the write flush as we may have not yet
1107 * retired the buffer.
1108 *
1109 * Note that the last_write_seqno is always the earlier of
1110 * the two (read/write) seqno, so if we haved successfully waited,
1111 * we know we have passed the last write.
1112 */
1113 obj->last_write_seqno = 0;
1114 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1115
1116 return 0;
1117}
1118
Chris Wilsonb3612372012-08-24 09:35:08 +01001119/**
1120 * Ensures that all rendering to the object has completed and the object is
1121 * safe to unbind from the GTT or access from the CPU.
1122 */
1123static __must_check int
1124i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1125 bool readonly)
1126{
1127 struct intel_ring_buffer *ring = obj->ring;
1128 u32 seqno;
1129 int ret;
1130
1131 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1132 if (seqno == 0)
1133 return 0;
1134
1135 ret = i915_wait_seqno(ring, seqno);
1136 if (ret)
1137 return ret;
1138
Chris Wilsond26e3af2013-06-29 22:05:26 +01001139 return i915_gem_object_wait_rendering__tail(obj, ring);
Chris Wilsonb3612372012-08-24 09:35:08 +01001140}
1141
Chris Wilson3236f572012-08-24 09:35:09 +01001142/* A nonblocking variant of the above wait. This is a highly dangerous routine
1143 * as the object state may change during this call.
1144 */
1145static __must_check int
1146i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
1147 bool readonly)
1148{
1149 struct drm_device *dev = obj->base.dev;
1150 struct drm_i915_private *dev_priv = dev->dev_private;
1151 struct intel_ring_buffer *ring = obj->ring;
Daniel Vetterf69061b2012-12-06 09:01:42 +01001152 unsigned reset_counter;
Chris Wilson3236f572012-08-24 09:35:09 +01001153 u32 seqno;
1154 int ret;
1155
1156 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1157 BUG_ON(!dev_priv->mm.interruptible);
1158
1159 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1160 if (seqno == 0)
1161 return 0;
1162
Daniel Vetter33196de2012-11-14 17:14:05 +01001163 ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
Chris Wilson3236f572012-08-24 09:35:09 +01001164 if (ret)
1165 return ret;
1166
1167 ret = i915_gem_check_olr(ring, seqno);
1168 if (ret)
1169 return ret;
1170
Daniel Vetterf69061b2012-12-06 09:01:42 +01001171 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilson3236f572012-08-24 09:35:09 +01001172 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf69061b2012-12-06 09:01:42 +01001173 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
Chris Wilson3236f572012-08-24 09:35:09 +01001174 mutex_lock(&dev->struct_mutex);
Chris Wilsond26e3af2013-06-29 22:05:26 +01001175 if (ret)
1176 return ret;
Chris Wilson3236f572012-08-24 09:35:09 +01001177
Chris Wilsond26e3af2013-06-29 22:05:26 +01001178 return i915_gem_object_wait_rendering__tail(obj, ring);
Chris Wilson3236f572012-08-24 09:35:09 +01001179}
1180
Eric Anholt673a3942008-07-30 12:06:12 -07001181/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001182 * Called when user space prepares to use an object with the CPU, either
1183 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001184 */
1185int
1186i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001187 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001188{
1189 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001190 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001191 uint32_t read_domains = args->read_domains;
1192 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001193 int ret;
1194
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001195 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001196 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001197 return -EINVAL;
1198
Chris Wilson21d509e2009-06-06 09:46:02 +01001199 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001200 return -EINVAL;
1201
1202 /* Having something in the write domain implies it's in the read
1203 * domain, and only that read domain. Enforce that in the request.
1204 */
1205 if (write_domain != 0 && read_domains != write_domain)
1206 return -EINVAL;
1207
Chris Wilson76c1dec2010-09-25 11:22:51 +01001208 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001209 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001210 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001211
Chris Wilson05394f32010-11-08 19:18:58 +00001212 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001213 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001214 ret = -ENOENT;
1215 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001216 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001217
Chris Wilson3236f572012-08-24 09:35:09 +01001218 /* Try to flush the object off the GPU without holding the lock.
1219 * We will repeat the flush holding the lock in the normal manner
1220 * to catch cases where we are gazumped.
1221 */
1222 ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
1223 if (ret)
1224 goto unref;
1225
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001226 if (read_domains & I915_GEM_DOMAIN_GTT) {
1227 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001228
1229 /* Silently promote "you're not bound, there was nothing to do"
1230 * to success, since the client was just asking us to
1231 * make sure everything was done.
1232 */
1233 if (ret == -EINVAL)
1234 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001235 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001236 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001237 }
1238
Chris Wilson3236f572012-08-24 09:35:09 +01001239unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001240 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001241unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001242 mutex_unlock(&dev->struct_mutex);
1243 return ret;
1244}
1245
1246/**
1247 * Called when user space has done writes to this buffer
1248 */
1249int
1250i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001251 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001252{
1253 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001254 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001255 int ret = 0;
1256
Chris Wilson76c1dec2010-09-25 11:22:51 +01001257 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001258 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001259 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001260
Chris Wilson05394f32010-11-08 19:18:58 +00001261 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001262 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001263 ret = -ENOENT;
1264 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001265 }
1266
Eric Anholt673a3942008-07-30 12:06:12 -07001267 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson2c225692013-08-09 12:26:45 +01001268 if (obj->pin_display)
1269 i915_gem_object_flush_cpu_write_domain(obj, true);
Eric Anholte47c68e2008-11-14 13:35:19 -08001270
Chris Wilson05394f32010-11-08 19:18:58 +00001271 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001272unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001273 mutex_unlock(&dev->struct_mutex);
1274 return ret;
1275}
1276
1277/**
1278 * Maps the contents of an object, returning the address it is mapped
1279 * into.
1280 *
1281 * While the mapping holds a reference on the contents of the object, it doesn't
1282 * imply a ref on the object itself.
1283 */
1284int
1285i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001286 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001287{
1288 struct drm_i915_gem_mmap *args = data;
1289 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001290 unsigned long addr;
1291
Chris Wilson05394f32010-11-08 19:18:58 +00001292 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001293 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001294 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001295
Daniel Vetter1286ff72012-05-10 15:25:09 +02001296 /* prime objects have no backing filp to GEM mmap
1297 * pages from.
1298 */
1299 if (!obj->filp) {
1300 drm_gem_object_unreference_unlocked(obj);
1301 return -EINVAL;
1302 }
1303
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001304 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001305 PROT_READ | PROT_WRITE, MAP_SHARED,
1306 args->offset);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001307 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001308 if (IS_ERR((void *)addr))
1309 return addr;
1310
1311 args->addr_ptr = (uint64_t) addr;
1312
1313 return 0;
1314}
1315
Jesse Barnesde151cf2008-11-12 10:03:55 -08001316/**
1317 * i915_gem_fault - fault a page into the GTT
1318 * vma: VMA in question
1319 * vmf: fault info
1320 *
1321 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1322 * from userspace. The fault handler takes care of binding the object to
1323 * the GTT (if needed), allocating and programming a fence register (again,
1324 * only if needed based on whether the old reg is still valid or the object
1325 * is tiled) and inserting a new PTE into the faulting process.
1326 *
1327 * Note that the faulting process may involve evicting existing objects
1328 * from the GTT and/or fence registers to make room. So performance may
1329 * suffer if the GTT working set is large or there are few fence registers
1330 * left.
1331 */
1332int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1333{
Chris Wilson05394f32010-11-08 19:18:58 +00001334 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1335 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001336 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001337 pgoff_t page_offset;
1338 unsigned long pfn;
1339 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001340 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001341
1342 /* We don't use vmf->pgoff since that has the fake offset */
1343 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1344 PAGE_SHIFT;
1345
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001346 ret = i915_mutex_lock_interruptible(dev);
1347 if (ret)
1348 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001349
Chris Wilsondb53a302011-02-03 11:57:46 +00001350 trace_i915_gem_object_fault(obj, page_offset, true, write);
1351
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001352 /* Access to snoopable pages through the GTT is incoherent. */
1353 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
1354 ret = -EINVAL;
1355 goto unlock;
1356 }
1357
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001358 /* Now bind it into the GTT if needed */
Ben Widawskyc37e2202013-07-31 16:59:58 -07001359 ret = i915_gem_obj_ggtt_pin(obj, 0, true, false);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001360 if (ret)
1361 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001362
Chris Wilsonc9839302012-11-20 10:45:17 +00001363 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1364 if (ret)
1365 goto unpin;
1366
1367 ret = i915_gem_object_get_fence(obj);
1368 if (ret)
1369 goto unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001370
Chris Wilson6299f992010-11-24 12:23:44 +00001371 obj->fault_mappable = true;
1372
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001373 pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
1374 pfn >>= PAGE_SHIFT;
1375 pfn += page_offset;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001376
1377 /* Finally, remap it using the new GTT offset */
1378 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc9839302012-11-20 10:45:17 +00001379unpin:
1380 i915_gem_object_unpin(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001381unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001382 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001383out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001384 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001385 case -EIO:
Daniel Vettera9340cc2012-07-04 22:18:42 +02001386 /* If this -EIO is due to a gpu hang, give the reset code a
1387 * chance to clean up the mess. Otherwise return the proper
1388 * SIGBUS. */
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001389 if (i915_terminally_wedged(&dev_priv->gpu_error))
Daniel Vettera9340cc2012-07-04 22:18:42 +02001390 return VM_FAULT_SIGBUS;
Chris Wilson045e7692010-11-07 09:18:22 +00001391 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001392 /* Give the error handler a chance to run and move the
1393 * objects off the GPU active list. Next time we service the
1394 * fault, we should be able to transition the page into the
1395 * GTT without touching the GPU (and so avoid further
1396 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1397 * with coherency, just lost writes.
1398 */
Chris Wilson045e7692010-11-07 09:18:22 +00001399 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001400 case 0:
1401 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001402 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001403 case -EBUSY:
1404 /*
1405 * EBUSY is ok: this just means that another thread
1406 * already did the job.
1407 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001408 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001409 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001410 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001411 case -ENOSPC:
1412 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001413 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001414 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilsonc7150892009-09-23 00:43:56 +01001415 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001416 }
1417}
1418
1419/**
Chris Wilson901782b2009-07-10 08:18:50 +01001420 * i915_gem_release_mmap - remove physical page mappings
1421 * @obj: obj in question
1422 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001423 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001424 * relinquish ownership of the pages back to the system.
1425 *
1426 * It is vital that we remove the page mapping if we have mapped a tiled
1427 * object through the GTT and then lose the fence register due to
1428 * resource pressure. Similarly if the object has been moved out of the
1429 * aperture, than pages mapped into userspace must be revoked. Removing the
1430 * mapping will then trigger a page fault on the next user access, allowing
1431 * fixup by i915_gem_fault().
1432 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001433void
Chris Wilson05394f32010-11-08 19:18:58 +00001434i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001435{
Chris Wilson6299f992010-11-24 12:23:44 +00001436 if (!obj->fault_mappable)
1437 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001438
David Herrmann51335df2013-07-24 21:10:03 +02001439 drm_vma_node_unmap(&obj->base.vma_node, obj->base.dev->dev_mapping);
Chris Wilson6299f992010-11-24 12:23:44 +00001440 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001441}
1442
Imre Deak0fa87792013-01-07 21:47:35 +02001443uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001444i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001445{
Chris Wilsone28f8712011-07-18 13:11:49 -07001446 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001447
1448 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001449 tiling_mode == I915_TILING_NONE)
1450 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001451
1452 /* Previous chips need a power-of-two fence region when tiling */
1453 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001454 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001455 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001456 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001457
Chris Wilsone28f8712011-07-18 13:11:49 -07001458 while (gtt_size < size)
1459 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001460
Chris Wilsone28f8712011-07-18 13:11:49 -07001461 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001462}
1463
Jesse Barnesde151cf2008-11-12 10:03:55 -08001464/**
1465 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1466 * @obj: object to check
1467 *
1468 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001469 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001470 */
Imre Deakd8651102013-01-07 21:47:33 +02001471uint32_t
1472i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1473 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001474{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001475 /*
1476 * Minimum alignment is 4k (GTT page size), but might be greater
1477 * if a fence register is needed for the object.
1478 */
Imre Deakd8651102013-01-07 21:47:33 +02001479 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001480 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001481 return 4096;
1482
1483 /*
1484 * Previous chips need to be aligned to the size of the smallest
1485 * fence register that can contain the object.
1486 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001487 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001488}
1489
Chris Wilsond8cb5082012-08-11 15:41:03 +01001490static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1491{
1492 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1493 int ret;
1494
David Herrmann0de23972013-07-24 21:07:52 +02001495 if (drm_vma_node_has_offset(&obj->base.vma_node))
Chris Wilsond8cb5082012-08-11 15:41:03 +01001496 return 0;
1497
Daniel Vetterda494d72012-12-20 15:11:16 +01001498 dev_priv->mm.shrinker_no_lock_stealing = true;
1499
Chris Wilsond8cb5082012-08-11 15:41:03 +01001500 ret = drm_gem_create_mmap_offset(&obj->base);
1501 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001502 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001503
1504 /* Badly fragmented mmap space? The only way we can recover
1505 * space is by destroying unwanted objects. We can't randomly release
1506 * mmap_offsets as userspace expects them to be persistent for the
1507 * lifetime of the objects. The closest we can is to release the
1508 * offsets on purgeable objects by truncating it and marking it purged,
1509 * which prevents userspace from ever using that object again.
1510 */
1511 i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
1512 ret = drm_gem_create_mmap_offset(&obj->base);
1513 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001514 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001515
1516 i915_gem_shrink_all(dev_priv);
Daniel Vetterda494d72012-12-20 15:11:16 +01001517 ret = drm_gem_create_mmap_offset(&obj->base);
1518out:
1519 dev_priv->mm.shrinker_no_lock_stealing = false;
1520
1521 return ret;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001522}
1523
1524static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1525{
Chris Wilsond8cb5082012-08-11 15:41:03 +01001526 drm_gem_free_mmap_offset(&obj->base);
1527}
1528
Jesse Barnesde151cf2008-11-12 10:03:55 -08001529int
Dave Airlieff72145b2011-02-07 12:16:14 +10001530i915_gem_mmap_gtt(struct drm_file *file,
1531 struct drm_device *dev,
1532 uint32_t handle,
1533 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001534{
Chris Wilsonda761a62010-10-27 17:37:08 +01001535 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001536 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001537 int ret;
1538
Chris Wilson76c1dec2010-09-25 11:22:51 +01001539 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001540 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001541 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001542
Dave Airlieff72145b2011-02-07 12:16:14 +10001543 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001544 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001545 ret = -ENOENT;
1546 goto unlock;
1547 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001548
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001549 if (obj->base.size > dev_priv->gtt.mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001550 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001551 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001552 }
1553
Chris Wilson05394f32010-11-08 19:18:58 +00001554 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001555 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001556 ret = -EINVAL;
1557 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001558 }
1559
Chris Wilsond8cb5082012-08-11 15:41:03 +01001560 ret = i915_gem_object_create_mmap_offset(obj);
1561 if (ret)
1562 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001563
David Herrmann0de23972013-07-24 21:07:52 +02001564 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001565
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001566out:
Chris Wilson05394f32010-11-08 19:18:58 +00001567 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001568unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001569 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001570 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001571}
1572
Dave Airlieff72145b2011-02-07 12:16:14 +10001573/**
1574 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1575 * @dev: DRM device
1576 * @data: GTT mapping ioctl data
1577 * @file: GEM object info
1578 *
1579 * Simply returns the fake offset to userspace so it can mmap it.
1580 * The mmap call will end up in drm_gem_mmap(), which will set things
1581 * up so we can get faults in the handler above.
1582 *
1583 * The fault handler will take care of binding the object into the GTT
1584 * (since it may have been evicted to make room for something), allocating
1585 * a fence register, and mapping the appropriate aperture address into
1586 * userspace.
1587 */
1588int
1589i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1590 struct drm_file *file)
1591{
1592 struct drm_i915_gem_mmap_gtt *args = data;
1593
Dave Airlieff72145b2011-02-07 12:16:14 +10001594 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1595}
1596
Daniel Vetter225067e2012-08-20 10:23:20 +02001597/* Immediately discard the backing storage */
1598static void
1599i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001600{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001601 struct inode *inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001602
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001603 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001604
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001605 if (obj->base.filp == NULL)
1606 return;
1607
Daniel Vetter225067e2012-08-20 10:23:20 +02001608 /* Our goal here is to return as much of the memory as
1609 * is possible back to the system as we are called from OOM.
1610 * To do this we must instruct the shmfs to drop all of its
1611 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01001612 */
Al Viro496ad9a2013-01-23 17:07:38 -05001613 inode = file_inode(obj->base.filp);
Daniel Vetter225067e2012-08-20 10:23:20 +02001614 shmem_truncate_range(inode, 0, (loff_t)-1);
Hugh Dickins5949eac2011-06-27 16:18:18 -07001615
Daniel Vetter225067e2012-08-20 10:23:20 +02001616 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001617}
Chris Wilsone5281cc2010-10-28 13:45:36 +01001618
Daniel Vetter225067e2012-08-20 10:23:20 +02001619static inline int
1620i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1621{
1622 return obj->madv == I915_MADV_DONTNEED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001623}
1624
Chris Wilson5cdf5882010-09-27 15:51:07 +01001625static void
Chris Wilson05394f32010-11-08 19:18:58 +00001626i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001627{
Imre Deak90797e62013-02-18 19:28:03 +02001628 struct sg_page_iter sg_iter;
1629 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001630
Chris Wilson05394f32010-11-08 19:18:58 +00001631 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001632
Chris Wilson6c085a72012-08-20 11:40:46 +02001633 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1634 if (ret) {
1635 /* In the event of a disaster, abandon all caches and
1636 * hope for the best.
1637 */
1638 WARN_ON(ret != -EIO);
Chris Wilson2c225692013-08-09 12:26:45 +01001639 i915_gem_clflush_object(obj, true);
Chris Wilson6c085a72012-08-20 11:40:46 +02001640 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1641 }
1642
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001643 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001644 i915_gem_object_save_bit_17_swizzle(obj);
1645
Chris Wilson05394f32010-11-08 19:18:58 +00001646 if (obj->madv == I915_MADV_DONTNEED)
1647 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001648
Imre Deak90797e62013-02-18 19:28:03 +02001649 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001650 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +01001651
Chris Wilson05394f32010-11-08 19:18:58 +00001652 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01001653 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001654
Chris Wilson05394f32010-11-08 19:18:58 +00001655 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01001656 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001657
Chris Wilson9da3da62012-06-01 15:20:22 +01001658 page_cache_release(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001659 }
Chris Wilson05394f32010-11-08 19:18:58 +00001660 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001661
Chris Wilson9da3da62012-06-01 15:20:22 +01001662 sg_free_table(obj->pages);
1663 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01001664}
1665
Chris Wilsondd624af2013-01-15 12:39:35 +00001666int
Chris Wilson37e680a2012-06-07 15:38:42 +01001667i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
1668{
1669 const struct drm_i915_gem_object_ops *ops = obj->ops;
1670
Chris Wilson2f745ad2012-09-04 21:02:58 +01001671 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01001672 return 0;
1673
Chris Wilsona5570172012-09-04 21:02:54 +01001674 if (obj->pages_pin_count)
1675 return -EBUSY;
1676
Ben Widawsky98438772013-07-31 17:00:12 -07001677 BUG_ON(i915_gem_obj_bound_any(obj));
Ben Widawsky3e123022013-07-31 17:00:04 -07001678
Chris Wilsona2165e32012-12-03 11:49:00 +00001679 /* ->put_pages might need to allocate memory for the bit17 swizzle
1680 * array, hence protect them from being reaped by removing them from gtt
1681 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001682 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00001683
Chris Wilson37e680a2012-06-07 15:38:42 +01001684 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001685 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02001686
Chris Wilson6c085a72012-08-20 11:40:46 +02001687 if (i915_gem_object_is_purgeable(obj))
1688 i915_gem_object_truncate(obj);
1689
1690 return 0;
1691}
1692
1693static long
Daniel Vetter93927ca2013-01-10 18:03:00 +01001694__i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
1695 bool purgeable_only)
Chris Wilson6c085a72012-08-20 11:40:46 +02001696{
1697 struct drm_i915_gem_object *obj, *next;
1698 long count = 0;
1699
1700 list_for_each_entry_safe(obj, next,
1701 &dev_priv->mm.unbound_list,
Ben Widawsky35c20a62013-05-31 11:28:48 -07001702 global_list) {
Daniel Vetter93927ca2013-01-10 18:03:00 +01001703 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
Chris Wilson37e680a2012-06-07 15:38:42 +01001704 i915_gem_object_put_pages(obj) == 0) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001705 count += obj->base.size >> PAGE_SHIFT;
1706 if (count >= target)
1707 return count;
1708 }
1709 }
1710
Ben Widawsky07fe0b12013-07-31 17:00:10 -07001711 list_for_each_entry_safe(obj, next, &dev_priv->mm.bound_list,
1712 global_list) {
1713 struct i915_vma *vma, *v;
Ben Widawsky80dcfdb2013-07-31 17:00:01 -07001714
1715 if (!i915_gem_object_is_purgeable(obj) && purgeable_only)
1716 continue;
1717
Ben Widawsky07fe0b12013-07-31 17:00:10 -07001718 list_for_each_entry_safe(vma, v, &obj->vma_list, vma_link)
1719 if (i915_vma_unbind(vma))
1720 break;
Ben Widawsky80dcfdb2013-07-31 17:00:01 -07001721
1722 if (!i915_gem_object_put_pages(obj)) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001723 count += obj->base.size >> PAGE_SHIFT;
1724 if (count >= target)
1725 return count;
1726 }
1727 }
1728
1729 return count;
1730}
1731
Daniel Vetter93927ca2013-01-10 18:03:00 +01001732static long
1733i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1734{
1735 return __i915_gem_shrink(dev_priv, target, true);
1736}
1737
Chris Wilson6c085a72012-08-20 11:40:46 +02001738static void
1739i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1740{
1741 struct drm_i915_gem_object *obj, *next;
1742
1743 i915_gem_evict_everything(dev_priv->dev);
1744
Ben Widawsky35c20a62013-05-31 11:28:48 -07001745 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
1746 global_list)
Chris Wilson37e680a2012-06-07 15:38:42 +01001747 i915_gem_object_put_pages(obj);
Daniel Vetter225067e2012-08-20 10:23:20 +02001748}
1749
Chris Wilson37e680a2012-06-07 15:38:42 +01001750static int
Chris Wilson6c085a72012-08-20 11:40:46 +02001751i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001752{
Chris Wilson6c085a72012-08-20 11:40:46 +02001753 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001754 int page_count, i;
1755 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01001756 struct sg_table *st;
1757 struct scatterlist *sg;
Imre Deak90797e62013-02-18 19:28:03 +02001758 struct sg_page_iter sg_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07001759 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02001760 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson6c085a72012-08-20 11:40:46 +02001761 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07001762
Chris Wilson6c085a72012-08-20 11:40:46 +02001763 /* Assert that the object is not currently in any GPU domain. As it
1764 * wasn't in the GTT, there shouldn't be any way it could have been in
1765 * a GPU cache
1766 */
1767 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1768 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1769
Chris Wilson9da3da62012-06-01 15:20:22 +01001770 st = kmalloc(sizeof(*st), GFP_KERNEL);
1771 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07001772 return -ENOMEM;
1773
Chris Wilson9da3da62012-06-01 15:20:22 +01001774 page_count = obj->base.size / PAGE_SIZE;
1775 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
Chris Wilson9da3da62012-06-01 15:20:22 +01001776 kfree(st);
1777 return -ENOMEM;
1778 }
1779
1780 /* Get the list of pages out of our struct file. They'll be pinned
1781 * at this point until we release them.
1782 *
1783 * Fail silently without starting the shrinker
1784 */
Al Viro496ad9a2013-01-23 17:07:38 -05001785 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilson6c085a72012-08-20 11:40:46 +02001786 gfp = mapping_gfp_mask(mapping);
Linus Torvaldscaf49192012-12-10 10:51:16 -08001787 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
Chris Wilson6c085a72012-08-20 11:40:46 +02001788 gfp &= ~(__GFP_IO | __GFP_WAIT);
Imre Deak90797e62013-02-18 19:28:03 +02001789 sg = st->sgl;
1790 st->nents = 0;
1791 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001792 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1793 if (IS_ERR(page)) {
1794 i915_gem_purge(dev_priv, page_count);
1795 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1796 }
1797 if (IS_ERR(page)) {
1798 /* We've tried hard to allocate the memory by reaping
1799 * our own buffer, now let the real VM do its job and
1800 * go down in flames if truly OOM.
1801 */
Linus Torvaldscaf49192012-12-10 10:51:16 -08001802 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
Chris Wilson6c085a72012-08-20 11:40:46 +02001803 gfp |= __GFP_IO | __GFP_WAIT;
1804
1805 i915_gem_shrink_all(dev_priv);
1806 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1807 if (IS_ERR(page))
1808 goto err_pages;
1809
Linus Torvaldscaf49192012-12-10 10:51:16 -08001810 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
Chris Wilson6c085a72012-08-20 11:40:46 +02001811 gfp &= ~(__GFP_IO | __GFP_WAIT);
1812 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04001813#ifdef CONFIG_SWIOTLB
1814 if (swiotlb_nr_tbl()) {
1815 st->nents++;
1816 sg_set_page(sg, page, PAGE_SIZE, 0);
1817 sg = sg_next(sg);
1818 continue;
1819 }
1820#endif
Imre Deak90797e62013-02-18 19:28:03 +02001821 if (!i || page_to_pfn(page) != last_pfn + 1) {
1822 if (i)
1823 sg = sg_next(sg);
1824 st->nents++;
1825 sg_set_page(sg, page, PAGE_SIZE, 0);
1826 } else {
1827 sg->length += PAGE_SIZE;
1828 }
1829 last_pfn = page_to_pfn(page);
Eric Anholt673a3942008-07-30 12:06:12 -07001830 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04001831#ifdef CONFIG_SWIOTLB
1832 if (!swiotlb_nr_tbl())
1833#endif
1834 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01001835 obj->pages = st;
1836
Eric Anholt673a3942008-07-30 12:06:12 -07001837 if (i915_gem_object_needs_bit17_swizzle(obj))
1838 i915_gem_object_do_bit_17_swizzle(obj);
1839
1840 return 0;
1841
1842err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02001843 sg_mark_end(sg);
1844 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
Imre Deak2db76d72013-03-26 15:14:18 +02001845 page_cache_release(sg_page_iter_page(&sg_iter));
Chris Wilson9da3da62012-06-01 15:20:22 +01001846 sg_free_table(st);
1847 kfree(st);
Eric Anholt673a3942008-07-30 12:06:12 -07001848 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07001849}
1850
Chris Wilson37e680a2012-06-07 15:38:42 +01001851/* Ensure that the associated pages are gathered from the backing storage
1852 * and pinned into our object. i915_gem_object_get_pages() may be called
1853 * multiple times before they are released by a single call to
1854 * i915_gem_object_put_pages() - once the pages are no longer referenced
1855 * either as a result of memory pressure (reaping pages under the shrinker)
1856 * or as the object is itself released.
1857 */
1858int
1859i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
1860{
1861 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1862 const struct drm_i915_gem_object_ops *ops = obj->ops;
1863 int ret;
1864
Chris Wilson2f745ad2012-09-04 21:02:58 +01001865 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01001866 return 0;
1867
Chris Wilson43e28f02013-01-08 10:53:09 +00001868 if (obj->madv != I915_MADV_WILLNEED) {
1869 DRM_ERROR("Attempting to obtain a purgeable object\n");
1870 return -EINVAL;
1871 }
1872
Chris Wilsona5570172012-09-04 21:02:54 +01001873 BUG_ON(obj->pages_pin_count);
1874
Chris Wilson37e680a2012-06-07 15:38:42 +01001875 ret = ops->get_pages(obj);
1876 if (ret)
1877 return ret;
1878
Ben Widawsky35c20a62013-05-31 11:28:48 -07001879 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilson37e680a2012-06-07 15:38:42 +01001880 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001881}
1882
Chris Wilson54cf91d2010-11-25 18:00:26 +00001883void
Chris Wilson05394f32010-11-08 19:18:58 +00001884i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson9d7730912012-11-27 16:22:52 +00001885 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001886{
Chris Wilson05394f32010-11-08 19:18:58 +00001887 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001888 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9d7730912012-11-27 16:22:52 +00001889 u32 seqno = intel_ring_get_seqno(ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001890
Zou Nan hai852835f2010-05-21 09:08:56 +08001891 BUG_ON(ring == NULL);
Chris Wilson02978ff2013-07-09 09:22:39 +01001892 if (obj->ring != ring && obj->last_write_seqno) {
1893 /* Keep the seqno relative to the current ring */
1894 obj->last_write_seqno = seqno;
1895 }
Chris Wilson05394f32010-11-08 19:18:58 +00001896 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001897
1898 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001899 if (!obj->active) {
1900 drm_gem_object_reference(&obj->base);
1901 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001902 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001903
Chris Wilson05394f32010-11-08 19:18:58 +00001904 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001905
Chris Wilson0201f1e2012-07-20 12:41:01 +01001906 obj->last_read_seqno = seqno;
Chris Wilson7dd49062012-03-21 10:48:18 +00001907
Chris Wilsoncaea7472010-11-12 13:53:37 +00001908 if (obj->fenced_gpu_access) {
Chris Wilsoncaea7472010-11-12 13:53:37 +00001909 obj->last_fenced_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001910
Chris Wilson7dd49062012-03-21 10:48:18 +00001911 /* Bump MRU to take account of the delayed flush */
1912 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1913 struct drm_i915_fence_reg *reg;
1914
1915 reg = &dev_priv->fence_regs[obj->fence_reg];
1916 list_move_tail(&reg->lru_list,
1917 &dev_priv->mm.fence_list);
1918 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00001919 }
1920}
1921
1922static void
Chris Wilsoncaea7472010-11-12 13:53:37 +00001923i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1924{
Ben Widawskyca191b12013-07-31 17:00:14 -07001925 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1926 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
1927 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001928
Chris Wilson65ce3022012-07-20 12:41:02 +01001929 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001930 BUG_ON(!obj->active);
Chris Wilson65ce3022012-07-20 12:41:02 +01001931
Ben Widawskyca191b12013-07-31 17:00:14 -07001932 list_move_tail(&vma->mm_list, &ggtt_vm->inactive_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001933
Chris Wilson65ce3022012-07-20 12:41:02 +01001934 list_del_init(&obj->ring_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001935 obj->ring = NULL;
1936
Chris Wilson65ce3022012-07-20 12:41:02 +01001937 obj->last_read_seqno = 0;
1938 obj->last_write_seqno = 0;
1939 obj->base.write_domain = 0;
1940
1941 obj->last_fenced_seqno = 0;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001942 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001943
1944 obj->active = 0;
1945 drm_gem_object_unreference(&obj->base);
1946
1947 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001948}
Eric Anholt673a3942008-07-30 12:06:12 -07001949
Chris Wilson9d7730912012-11-27 16:22:52 +00001950static int
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02001951i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01001952{
Chris Wilson9d7730912012-11-27 16:22:52 +00001953 struct drm_i915_private *dev_priv = dev->dev_private;
1954 struct intel_ring_buffer *ring;
1955 int ret, i, j;
Daniel Vetter53d227f2012-01-25 16:32:49 +01001956
Chris Wilson107f27a52012-12-10 13:56:17 +02001957 /* Carefully retire all requests without writing to the rings */
Chris Wilson9d7730912012-11-27 16:22:52 +00001958 for_each_ring(ring, dev_priv, i) {
Chris Wilson107f27a52012-12-10 13:56:17 +02001959 ret = intel_ring_idle(ring);
1960 if (ret)
1961 return ret;
Chris Wilson9d7730912012-11-27 16:22:52 +00001962 }
Chris Wilson9d7730912012-11-27 16:22:52 +00001963 i915_gem_retire_requests(dev);
Chris Wilson107f27a52012-12-10 13:56:17 +02001964
1965 /* Finally reset hw state */
Chris Wilson9d7730912012-11-27 16:22:52 +00001966 for_each_ring(ring, dev_priv, i) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02001967 intel_ring_init_seqno(ring, seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001968
Chris Wilson9d7730912012-11-27 16:22:52 +00001969 for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
1970 ring->sync_seqno[j] = 0;
1971 }
1972
1973 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01001974}
1975
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02001976int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
1977{
1978 struct drm_i915_private *dev_priv = dev->dev_private;
1979 int ret;
1980
1981 if (seqno == 0)
1982 return -EINVAL;
1983
1984 /* HWS page needs to be set less than what we
1985 * will inject to ring
1986 */
1987 ret = i915_gem_init_seqno(dev, seqno - 1);
1988 if (ret)
1989 return ret;
1990
1991 /* Carefully set the last_seqno value so that wrap
1992 * detection still works
1993 */
1994 dev_priv->next_seqno = seqno;
1995 dev_priv->last_seqno = seqno - 1;
1996 if (dev_priv->last_seqno == 0)
1997 dev_priv->last_seqno--;
1998
1999 return 0;
2000}
2001
Chris Wilson9d7730912012-11-27 16:22:52 +00002002int
2003i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01002004{
Chris Wilson9d7730912012-11-27 16:22:52 +00002005 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002006
Chris Wilson9d7730912012-11-27 16:22:52 +00002007 /* reserve 0 for non-seqno */
2008 if (dev_priv->next_seqno == 0) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02002009 int ret = i915_gem_init_seqno(dev, 0);
Chris Wilson9d7730912012-11-27 16:22:52 +00002010 if (ret)
2011 return ret;
2012
2013 dev_priv->next_seqno = 1;
2014 }
2015
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02002016 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
Chris Wilson9d7730912012-11-27 16:22:52 +00002017 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01002018}
2019
Mika Kuoppala0025c072013-06-12 12:35:30 +03002020int __i915_add_request(struct intel_ring_buffer *ring,
2021 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002022 struct drm_i915_gem_object *obj,
Mika Kuoppala0025c072013-06-12 12:35:30 +03002023 u32 *out_seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002024{
Chris Wilsondb53a302011-02-03 11:57:46 +00002025 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilsonacb868d2012-09-26 13:47:30 +01002026 struct drm_i915_gem_request *request;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002027 u32 request_ring_position, request_start;
Eric Anholt673a3942008-07-30 12:06:12 -07002028 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01002029 int ret;
2030
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002031 request_start = intel_ring_get_tail(ring);
Daniel Vettercc889e02012-06-13 20:45:19 +02002032 /*
2033 * Emit any outstanding flushes - execbuf can fail to emit the flush
2034 * after having emitted the batchbuffer command. Hence we need to fix
2035 * things up similar to emitting the lazy request. The difference here
2036 * is that the flush _must_ happen before the next request, no matter
2037 * what.
2038 */
Chris Wilsona7b97612012-07-20 12:41:08 +01002039 ret = intel_ring_flush_all_caches(ring);
2040 if (ret)
2041 return ret;
Daniel Vettercc889e02012-06-13 20:45:19 +02002042
Chris Wilson3c0e2342013-09-04 10:45:52 +01002043 request = ring->preallocated_lazy_request;
2044 if (WARN_ON(request == NULL))
Chris Wilsonacb868d2012-09-26 13:47:30 +01002045 return -ENOMEM;
Daniel Vettercc889e02012-06-13 20:45:19 +02002046
Chris Wilsona71d8d92012-02-15 11:25:36 +00002047 /* Record the position of the start of the request so that
2048 * should we detect the updated seqno part-way through the
2049 * GPU processing the request, we never over-estimate the
2050 * position of the head.
2051 */
2052 request_ring_position = intel_ring_get_tail(ring);
2053
Chris Wilson9d7730912012-11-27 16:22:52 +00002054 ret = ring->add_request(ring);
Chris Wilson3c0e2342013-09-04 10:45:52 +01002055 if (ret)
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002056 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002057
Chris Wilson9d7730912012-11-27 16:22:52 +00002058 request->seqno = intel_ring_get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08002059 request->ring = ring;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002060 request->head = request_start;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002061 request->tail = request_ring_position;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002062
2063 /* Whilst this request exists, batch_obj will be on the
2064 * active_list, and so will hold the active reference. Only when this
2065 * request is retired will the the batch_obj be moved onto the
2066 * inactive_list and lose its active reference. Hence we do not need
2067 * to explicitly hold another reference here.
2068 */
Chris Wilson9a7e0c22013-08-26 19:50:54 -03002069 request->batch_obj = obj;
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002070
Chris Wilson9a7e0c22013-08-26 19:50:54 -03002071 /* Hold a reference to the current context so that we can inspect
2072 * it later in case a hangcheck error event fires.
2073 */
2074 request->ctx = ring->last_context;
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002075 if (request->ctx)
2076 i915_gem_context_reference(request->ctx);
2077
Eric Anholt673a3942008-07-30 12:06:12 -07002078 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08002079 was_empty = list_empty(&ring->request_list);
2080 list_add_tail(&request->list, &ring->request_list);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002081 request->file_priv = NULL;
Zou Nan hai852835f2010-05-21 09:08:56 +08002082
Chris Wilsondb53a302011-02-03 11:57:46 +00002083 if (file) {
2084 struct drm_i915_file_private *file_priv = file->driver_priv;
2085
Chris Wilson1c255952010-09-26 11:03:27 +01002086 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002087 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00002088 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002089 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01002090 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00002091 }
Eric Anholt673a3942008-07-30 12:06:12 -07002092
Chris Wilson9d7730912012-11-27 16:22:52 +00002093 trace_i915_gem_request_add(ring, request->seqno);
Chris Wilson18235212013-09-04 10:45:51 +01002094 ring->outstanding_lazy_seqno = 0;
Chris Wilson3c0e2342013-09-04 10:45:52 +01002095 ring->preallocated_lazy_request = NULL;
Chris Wilsondb53a302011-02-03 11:57:46 +00002096
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02002097 if (!dev_priv->ums.mm_suspended) {
Mika Kuoppala10cd45b2013-07-03 17:22:08 +03002098 i915_queue_hangcheck(ring->dev);
2099
Chris Wilsonf047e392012-07-21 12:31:41 +01002100 if (was_empty) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01002101 queue_delayed_work(dev_priv->wq,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002102 &dev_priv->mm.retire_work,
2103 round_jiffies_up_relative(HZ));
Chris Wilsonf047e392012-07-21 12:31:41 +01002104 intel_mark_busy(dev_priv->dev);
2105 }
Ben Gamarif65d9422009-09-14 17:48:44 -04002106 }
Daniel Vettercc889e02012-06-13 20:45:19 +02002107
Chris Wilsonacb868d2012-09-26 13:47:30 +01002108 if (out_seqno)
Chris Wilson9d7730912012-11-27 16:22:52 +00002109 *out_seqno = request->seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +01002110 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002111}
2112
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002113static inline void
2114i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07002115{
Chris Wilson1c255952010-09-26 11:03:27 +01002116 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002117
Chris Wilson1c255952010-09-26 11:03:27 +01002118 if (!file_priv)
2119 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002120
Chris Wilson1c255952010-09-26 11:03:27 +01002121 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00002122 if (request->file_priv) {
2123 list_del(&request->client_list);
2124 request->file_priv = NULL;
2125 }
Chris Wilson1c255952010-09-26 11:03:27 +01002126 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002127}
2128
Ben Widawskyd1ccbb52013-07-31 17:00:05 -07002129static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj,
2130 struct i915_address_space *vm)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002131{
Ben Widawskyd1ccbb52013-07-31 17:00:05 -07002132 if (acthd >= i915_gem_obj_offset(obj, vm) &&
2133 acthd < i915_gem_obj_offset(obj, vm) + obj->base.size)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002134 return true;
2135
2136 return false;
2137}
2138
2139static bool i915_head_inside_request(const u32 acthd_unmasked,
2140 const u32 request_start,
2141 const u32 request_end)
2142{
2143 const u32 acthd = acthd_unmasked & HEAD_ADDR;
2144
2145 if (request_start < request_end) {
2146 if (acthd >= request_start && acthd < request_end)
2147 return true;
2148 } else if (request_start > request_end) {
2149 if (acthd >= request_start || acthd < request_end)
2150 return true;
2151 }
2152
2153 return false;
2154}
2155
Ben Widawskyd1ccbb52013-07-31 17:00:05 -07002156static struct i915_address_space *
2157request_to_vm(struct drm_i915_gem_request *request)
2158{
2159 struct drm_i915_private *dev_priv = request->ring->dev->dev_private;
2160 struct i915_address_space *vm;
2161
2162 vm = &dev_priv->gtt.base;
2163
2164 return vm;
2165}
2166
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002167static bool i915_request_guilty(struct drm_i915_gem_request *request,
2168 const u32 acthd, bool *inside)
2169{
2170 /* There is a possibility that unmasked head address
2171 * pointing inside the ring, matches the batch_obj address range.
2172 * However this is extremely unlikely.
2173 */
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002174 if (request->batch_obj) {
Ben Widawskyd1ccbb52013-07-31 17:00:05 -07002175 if (i915_head_inside_object(acthd, request->batch_obj,
2176 request_to_vm(request))) {
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002177 *inside = true;
2178 return true;
2179 }
2180 }
2181
2182 if (i915_head_inside_request(acthd, request->head, request->tail)) {
2183 *inside = false;
2184 return true;
2185 }
2186
2187 return false;
2188}
2189
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002190static bool i915_context_is_banned(const struct i915_ctx_hang_stats *hs)
2191{
2192 const unsigned long elapsed = get_seconds() - hs->guilty_ts;
2193
2194 if (hs->banned)
2195 return true;
2196
2197 if (elapsed <= DRM_I915_CTX_BAN_PERIOD) {
2198 DRM_ERROR("context hanging too fast, declaring banned!\n");
2199 return true;
2200 }
2201
2202 return false;
2203}
2204
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002205static void i915_set_reset_status(struct intel_ring_buffer *ring,
2206 struct drm_i915_gem_request *request,
2207 u32 acthd)
2208{
2209 struct i915_ctx_hang_stats *hs = NULL;
2210 bool inside, guilty;
Ben Widawskyd1ccbb52013-07-31 17:00:05 -07002211 unsigned long offset = 0;
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002212
2213 /* Innocent until proven guilty */
2214 guilty = false;
2215
Ben Widawskyd1ccbb52013-07-31 17:00:05 -07002216 if (request->batch_obj)
2217 offset = i915_gem_obj_offset(request->batch_obj,
2218 request_to_vm(request));
2219
Jani Nikulaf2f4d822013-08-11 12:44:01 +03002220 if (ring->hangcheck.action != HANGCHECK_WAIT &&
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002221 i915_request_guilty(request, acthd, &inside)) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002222 DRM_ERROR("%s hung %s bo (0x%lx ctx %d) at 0x%x\n",
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002223 ring->name,
2224 inside ? "inside" : "flushing",
Ben Widawskyd1ccbb52013-07-31 17:00:05 -07002225 offset,
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002226 request->ctx ? request->ctx->id : 0,
2227 acthd);
2228
2229 guilty = true;
2230 }
2231
2232 /* If contexts are disabled or this is the default context, use
2233 * file_priv->reset_state
2234 */
2235 if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID)
2236 hs = &request->ctx->hang_stats;
2237 else if (request->file_priv)
2238 hs = &request->file_priv->hang_stats;
2239
2240 if (hs) {
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002241 if (guilty) {
2242 hs->banned = i915_context_is_banned(hs);
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002243 hs->batch_active++;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002244 hs->guilty_ts = get_seconds();
2245 } else {
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002246 hs->batch_pending++;
Mika Kuoppalabe62acb2013-08-30 16:19:28 +03002247 }
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002248 }
2249}
2250
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002251static void i915_gem_free_request(struct drm_i915_gem_request *request)
2252{
2253 list_del(&request->list);
2254 i915_gem_request_remove_from_client(request);
2255
2256 if (request->ctx)
2257 i915_gem_context_unreference(request->ctx);
2258
2259 kfree(request);
2260}
2261
Chris Wilsondfaae392010-09-22 10:31:52 +01002262static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
2263 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01002264{
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002265 u32 completed_seqno;
2266 u32 acthd;
2267
2268 acthd = intel_ring_get_active_head(ring);
2269 completed_seqno = ring->get_seqno(ring, false);
2270
Chris Wilsondfaae392010-09-22 10:31:52 +01002271 while (!list_empty(&ring->request_list)) {
2272 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01002273
Chris Wilsondfaae392010-09-22 10:31:52 +01002274 request = list_first_entry(&ring->request_list,
2275 struct drm_i915_gem_request,
2276 list);
2277
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002278 if (request->seqno > completed_seqno)
2279 i915_set_reset_status(ring, request, acthd);
2280
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002281 i915_gem_free_request(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01002282 }
2283
2284 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002285 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002286
Chris Wilson05394f32010-11-08 19:18:58 +00002287 obj = list_first_entry(&ring->active_list,
2288 struct drm_i915_gem_object,
2289 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002290
Chris Wilson05394f32010-11-08 19:18:58 +00002291 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002292 }
Eric Anholt673a3942008-07-30 12:06:12 -07002293}
2294
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002295void i915_gem_restore_fences(struct drm_device *dev)
Chris Wilson312817a2010-11-22 11:50:11 +00002296{
2297 struct drm_i915_private *dev_priv = dev->dev_private;
2298 int i;
2299
Daniel Vetter4b9de732011-10-09 21:52:02 +02002300 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00002301 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson7d2cb392010-11-27 17:38:29 +00002302
Daniel Vetter94a335d2013-07-17 14:51:28 +02002303 /*
2304 * Commit delayed tiling changes if we have an object still
2305 * attached to the fence, otherwise just clear the fence.
2306 */
2307 if (reg->obj) {
2308 i915_gem_object_update_fence(reg->obj, reg,
2309 reg->obj->tiling_mode);
2310 } else {
2311 i915_gem_write_fence(dev, i, NULL);
2312 }
Chris Wilson312817a2010-11-22 11:50:11 +00002313 }
2314}
2315
Chris Wilson069efc12010-09-30 16:53:18 +01002316void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002317{
Chris Wilsondfaae392010-09-22 10:31:52 +01002318 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002319 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002320 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002321
Chris Wilsonb4519512012-05-11 14:29:30 +01002322 for_each_ring(ring, dev_priv, i)
2323 i915_gem_reset_ring_lists(dev_priv, ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01002324
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002325 i915_gem_restore_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002326}
2327
2328/**
2329 * This function clears the request list as sequence numbers are passed.
2330 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00002331void
Chris Wilsondb53a302011-02-03 11:57:46 +00002332i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002333{
Eric Anholt673a3942008-07-30 12:06:12 -07002334 uint32_t seqno;
2335
Chris Wilsondb53a302011-02-03 11:57:46 +00002336 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01002337 return;
2338
Chris Wilsondb53a302011-02-03 11:57:46 +00002339 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002340
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01002341 seqno = ring->get_seqno(ring, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002342
Zou Nan hai852835f2010-05-21 09:08:56 +08002343 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002344 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002345
Zou Nan hai852835f2010-05-21 09:08:56 +08002346 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002347 struct drm_i915_gem_request,
2348 list);
Eric Anholt673a3942008-07-30 12:06:12 -07002349
Chris Wilsondfaae392010-09-22 10:31:52 +01002350 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07002351 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002352
Chris Wilsondb53a302011-02-03 11:57:46 +00002353 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002354 /* We know the GPU must have read the request to have
2355 * sent us the seqno + interrupt, so use the position
2356 * of tail of the request to update the last known position
2357 * of the GPU head.
2358 */
2359 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002360
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002361 i915_gem_free_request(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002362 }
2363
2364 /* Move any buffers on the active list that are no longer referenced
2365 * by the ringbuffer to the flushing/inactive lists as appropriate.
2366 */
2367 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002368 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002369
Akshay Joshi0206e352011-08-16 15:34:10 -04002370 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00002371 struct drm_i915_gem_object,
2372 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002373
Chris Wilson0201f1e2012-07-20 12:41:01 +01002374 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002375 break;
2376
Chris Wilson65ce3022012-07-20 12:41:02 +01002377 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002378 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002379
Chris Wilsondb53a302011-02-03 11:57:46 +00002380 if (unlikely(ring->trace_irq_seqno &&
2381 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002382 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00002383 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002384 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002385
Chris Wilsondb53a302011-02-03 11:57:46 +00002386 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002387}
2388
2389void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002390i915_gem_retire_requests(struct drm_device *dev)
2391{
2392 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002393 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002394 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002395
Chris Wilsonb4519512012-05-11 14:29:30 +01002396 for_each_ring(ring, dev_priv, i)
2397 i915_gem_retire_requests_ring(ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002398}
2399
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002400static void
Eric Anholt673a3942008-07-30 12:06:12 -07002401i915_gem_retire_work_handler(struct work_struct *work)
2402{
2403 drm_i915_private_t *dev_priv;
2404 struct drm_device *dev;
Chris Wilsonb4519512012-05-11 14:29:30 +01002405 struct intel_ring_buffer *ring;
Chris Wilson0a587052011-01-09 21:05:44 +00002406 bool idle;
2407 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002408
2409 dev_priv = container_of(work, drm_i915_private_t,
2410 mm.retire_work.work);
2411 dev = dev_priv->dev;
2412
Chris Wilson891b48c2010-09-29 12:26:37 +01002413 /* Come back later if the device is busy... */
2414 if (!mutex_trylock(&dev->struct_mutex)) {
Chris Wilsonbcb45082012-10-05 17:02:57 +01002415 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2416 round_jiffies_up_relative(HZ));
Chris Wilson891b48c2010-09-29 12:26:37 +01002417 return;
2418 }
2419
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002420 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002421
Chris Wilson0a587052011-01-09 21:05:44 +00002422 /* Send a periodic flush down the ring so we don't hold onto GEM
2423 * objects indefinitely.
2424 */
2425 idle = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002426 for_each_ring(ring, dev_priv, i) {
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002427 if (ring->gpu_caches_dirty)
Mika Kuoppala0025c072013-06-12 12:35:30 +03002428 i915_add_request(ring, NULL);
Chris Wilson0a587052011-01-09 21:05:44 +00002429
2430 idle &= list_empty(&ring->request_list);
2431 }
2432
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02002433 if (!dev_priv->ums.mm_suspended && !idle)
Chris Wilsonbcb45082012-10-05 17:02:57 +01002434 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2435 round_jiffies_up_relative(HZ));
Chris Wilsonf047e392012-07-21 12:31:41 +01002436 if (idle)
2437 intel_mark_idle(dev);
Chris Wilson0a587052011-01-09 21:05:44 +00002438
Eric Anholt673a3942008-07-30 12:06:12 -07002439 mutex_unlock(&dev->struct_mutex);
2440}
2441
Ben Widawsky5816d642012-04-11 11:18:19 -07002442/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002443 * Ensures that an object will eventually get non-busy by flushing any required
2444 * write domains, emitting any outstanding lazy request and retiring and
2445 * completed requests.
2446 */
2447static int
2448i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2449{
2450 int ret;
2451
2452 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002453 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002454 if (ret)
2455 return ret;
2456
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002457 i915_gem_retire_requests_ring(obj->ring);
2458 }
2459
2460 return 0;
2461}
2462
2463/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002464 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2465 * @DRM_IOCTL_ARGS: standard ioctl arguments
2466 *
2467 * Returns 0 if successful, else an error is returned with the remaining time in
2468 * the timeout parameter.
2469 * -ETIME: object is still busy after timeout
2470 * -ERESTARTSYS: signal interrupted the wait
2471 * -ENONENT: object doesn't exist
2472 * Also possible, but rare:
2473 * -EAGAIN: GPU wedged
2474 * -ENOMEM: damn
2475 * -ENODEV: Internal IRQ fail
2476 * -E?: The add request failed
2477 *
2478 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2479 * non-zero timeout parameter the wait ioctl will wait for the given number of
2480 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2481 * without holding struct_mutex the object may become re-busied before this
2482 * function completes. A similar but shorter * race condition exists in the busy
2483 * ioctl
2484 */
2485int
2486i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2487{
Daniel Vetterf69061b2012-12-06 09:01:42 +01002488 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002489 struct drm_i915_gem_wait *args = data;
2490 struct drm_i915_gem_object *obj;
2491 struct intel_ring_buffer *ring = NULL;
Ben Widawskyeac1f142012-06-05 15:24:24 -07002492 struct timespec timeout_stack, *timeout = NULL;
Daniel Vetterf69061b2012-12-06 09:01:42 +01002493 unsigned reset_counter;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002494 u32 seqno = 0;
2495 int ret = 0;
2496
Ben Widawskyeac1f142012-06-05 15:24:24 -07002497 if (args->timeout_ns >= 0) {
2498 timeout_stack = ns_to_timespec(args->timeout_ns);
2499 timeout = &timeout_stack;
2500 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002501
2502 ret = i915_mutex_lock_interruptible(dev);
2503 if (ret)
2504 return ret;
2505
2506 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2507 if (&obj->base == NULL) {
2508 mutex_unlock(&dev->struct_mutex);
2509 return -ENOENT;
2510 }
2511
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002512 /* Need to make sure the object gets inactive eventually. */
2513 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002514 if (ret)
2515 goto out;
2516
2517 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002518 seqno = obj->last_read_seqno;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002519 ring = obj->ring;
2520 }
2521
2522 if (seqno == 0)
2523 goto out;
2524
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002525 /* Do this after OLR check to make sure we make forward progress polling
2526 * on this IOCTL with a 0 timeout (like busy ioctl)
2527 */
2528 if (!args->timeout_ns) {
2529 ret = -ETIME;
2530 goto out;
2531 }
2532
2533 drm_gem_object_unreference(&obj->base);
Daniel Vetterf69061b2012-12-06 09:01:42 +01002534 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002535 mutex_unlock(&dev->struct_mutex);
2536
Daniel Vetterf69061b2012-12-06 09:01:42 +01002537 ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
Chris Wilson4f42f4e2013-04-26 16:22:46 +03002538 if (timeout)
Ben Widawskyeac1f142012-06-05 15:24:24 -07002539 args->timeout_ns = timespec_to_ns(timeout);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002540 return ret;
2541
2542out:
2543 drm_gem_object_unreference(&obj->base);
2544 mutex_unlock(&dev->struct_mutex);
2545 return ret;
2546}
2547
2548/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002549 * i915_gem_object_sync - sync an object to a ring.
2550 *
2551 * @obj: object which may be in use on another ring.
2552 * @to: ring we wish to use the object on. May be NULL.
2553 *
2554 * This code is meant to abstract object synchronization with the GPU.
2555 * Calling with NULL implies synchronizing the object with the CPU
2556 * rather than a particular GPU ring.
2557 *
2558 * Returns 0 if successful, else propagates up the lower layer error.
2559 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002560int
2561i915_gem_object_sync(struct drm_i915_gem_object *obj,
2562 struct intel_ring_buffer *to)
2563{
2564 struct intel_ring_buffer *from = obj->ring;
2565 u32 seqno;
2566 int ret, idx;
2567
2568 if (from == NULL || to == from)
2569 return 0;
2570
Ben Widawsky5816d642012-04-11 11:18:19 -07002571 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
Chris Wilson0201f1e2012-07-20 12:41:01 +01002572 return i915_gem_object_wait_rendering(obj, false);
Ben Widawsky2911a352012-04-05 14:47:36 -07002573
2574 idx = intel_ring_sync_index(from, to);
2575
Chris Wilson0201f1e2012-07-20 12:41:01 +01002576 seqno = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002577 if (seqno <= from->sync_seqno[idx])
2578 return 0;
2579
Ben Widawskyb4aca012012-04-25 20:50:12 -07002580 ret = i915_gem_check_olr(obj->ring, seqno);
2581 if (ret)
2582 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002583
Ben Widawsky1500f7e2012-04-11 11:18:21 -07002584 ret = to->sync_to(to, from, seqno);
Ben Widawskye3a5a222012-04-11 11:18:20 -07002585 if (!ret)
Mika Kuoppala7b01e262012-11-28 17:18:45 +02002586 /* We use last_read_seqno because sync_to()
2587 * might have just caused seqno wrap under
2588 * the radar.
2589 */
2590 from->sync_seqno[idx] = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002591
Ben Widawskye3a5a222012-04-11 11:18:20 -07002592 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002593}
2594
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002595static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2596{
2597 u32 old_write_domain, old_read_domains;
2598
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002599 /* Force a pagefault for domain tracking on next user access */
2600 i915_gem_release_mmap(obj);
2601
Keith Packardb97c3d92011-06-24 21:02:59 -07002602 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2603 return;
2604
Chris Wilson97c809fd2012-10-09 19:24:38 +01002605 /* Wait for any direct GTT access to complete */
2606 mb();
2607
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002608 old_read_domains = obj->base.read_domains;
2609 old_write_domain = obj->base.write_domain;
2610
2611 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2612 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2613
2614 trace_i915_gem_object_change_domain(obj,
2615 old_read_domains,
2616 old_write_domain);
2617}
2618
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002619int i915_vma_unbind(struct i915_vma *vma)
Eric Anholt673a3942008-07-30 12:06:12 -07002620{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002621 struct drm_i915_gem_object *obj = vma->obj;
Daniel Vetter7bddb012012-02-09 17:15:47 +01002622 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Chris Wilson43e28f02013-01-08 10:53:09 +00002623 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002624
Daniel Vetterb93dab62013-08-26 11:23:47 +02002625 /* For now we only ever use 1 vma per object */
2626 WARN_ON(!list_is_singular(&obj->vma_list));
2627
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002628 if (list_empty(&vma->vma_link))
Eric Anholt673a3942008-07-30 12:06:12 -07002629 return 0;
2630
Daniel Vetter0ff501c2013-08-29 19:50:31 +02002631 if (!drm_mm_node_allocated(&vma->node)) {
2632 i915_gem_vma_destroy(vma);
2633
2634 return 0;
2635 }
Ben Widawsky433544b2013-08-13 18:09:06 -07002636
Chris Wilson31d8d652012-05-24 19:11:20 +01002637 if (obj->pin_count)
2638 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002639
Chris Wilsonc4670ad2012-08-20 10:23:27 +01002640 BUG_ON(obj->pages == NULL);
2641
Chris Wilsona8198ee2011-04-13 22:04:09 +01002642 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002643 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002644 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002645 /* Continue on if we fail due to EIO, the GPU is hung so we
2646 * should be safe and we need to cleanup or else we might
2647 * cause memory corruption through use-after-free.
2648 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002649
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002650 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002651
Daniel Vetter96b47b62009-12-15 17:50:00 +01002652 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002653 ret = i915_gem_object_put_fence(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002654 if (ret)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002655 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002656
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002657 trace_i915_vma_unbind(vma);
Chris Wilsondb53a302011-02-03 11:57:46 +00002658
Daniel Vetter74898d72012-02-15 23:50:22 +01002659 if (obj->has_global_gtt_mapping)
2660 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002661 if (obj->has_aliasing_ppgtt_mapping) {
2662 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2663 obj->has_aliasing_ppgtt_mapping = 0;
2664 }
Daniel Vetter74163902012-02-15 23:50:21 +01002665 i915_gem_gtt_finish_object(obj);
Ben Widawsky401c29f2013-05-31 11:28:47 -07002666 i915_gem_object_unpin_pages(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002667
Ben Widawskyca191b12013-07-31 17:00:14 -07002668 list_del(&vma->mm_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002669 /* Avoid an unnecessary call to unbind on rebind. */
Ben Widawsky5cacaac2013-07-31 17:00:13 -07002670 if (i915_is_ggtt(vma->vm))
2671 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002672
Ben Widawsky2f633152013-07-17 12:19:03 -07002673 drm_mm_remove_node(&vma->node);
Ben Widawsky433544b2013-08-13 18:09:06 -07002674
Ben Widawsky2f633152013-07-17 12:19:03 -07002675 i915_gem_vma_destroy(vma);
2676
2677 /* Since the unbound list is global, only move to that list if
Daniel Vetterb93dab62013-08-26 11:23:47 +02002678 * no more VMAs exist. */
Ben Widawsky2f633152013-07-17 12:19:03 -07002679 if (list_empty(&obj->vma_list))
2680 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002681
Chris Wilson88241782011-01-07 17:09:48 +00002682 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002683}
2684
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002685/**
2686 * Unbinds an object from the global GTT aperture.
2687 */
2688int
2689i915_gem_object_ggtt_unbind(struct drm_i915_gem_object *obj)
2690{
2691 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2692 struct i915_address_space *ggtt = &dev_priv->gtt.base;
2693
Dan Carpenter58e73e12013-08-09 12:44:11 +03002694 if (!i915_gem_obj_ggtt_bound(obj))
Ben Widawsky07fe0b12013-07-31 17:00:10 -07002695 return 0;
2696
2697 if (obj->pin_count)
2698 return -EBUSY;
2699
2700 BUG_ON(obj->pages == NULL);
2701
2702 return i915_vma_unbind(i915_gem_obj_to_vma(obj, ggtt));
2703}
2704
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002705int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002706{
2707 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002708 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002709 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002710
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002711 /* Flush everything onto the inactive list. */
Chris Wilsonb4519512012-05-11 14:29:30 +01002712 for_each_ring(ring, dev_priv, i) {
Ben Widawskyb6c74882012-08-14 14:35:14 -07002713 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2714 if (ret)
2715 return ret;
2716
Chris Wilson3e960502012-11-27 16:22:54 +00002717 ret = intel_ring_idle(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002718 if (ret)
2719 return ret;
2720 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002721
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002722 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002723}
2724
Chris Wilson9ce079e2012-04-17 15:31:30 +01002725static void i965_write_fence_reg(struct drm_device *dev, int reg,
2726 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002727{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002728 drm_i915_private_t *dev_priv = dev->dev_private;
Imre Deak56c844e2013-01-07 21:47:34 +02002729 int fence_reg;
2730 int fence_pitch_shift;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002731
Imre Deak56c844e2013-01-07 21:47:34 +02002732 if (INTEL_INFO(dev)->gen >= 6) {
2733 fence_reg = FENCE_REG_SANDYBRIDGE_0;
2734 fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
2735 } else {
2736 fence_reg = FENCE_REG_965_0;
2737 fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
2738 }
2739
Chris Wilsond18b9612013-07-10 13:36:23 +01002740 fence_reg += reg * 8;
2741
2742 /* To w/a incoherency with non-atomic 64-bit register updates,
2743 * we split the 64-bit update into two 32-bit writes. In order
2744 * for a partial fence not to be evaluated between writes, we
2745 * precede the update with write to turn off the fence register,
2746 * and only enable the fence as the last step.
2747 *
2748 * For extra levels of paranoia, we make sure each step lands
2749 * before applying the next step.
2750 */
2751 I915_WRITE(fence_reg, 0);
2752 POSTING_READ(fence_reg);
2753
Chris Wilson9ce079e2012-04-17 15:31:30 +01002754 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002755 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilsond18b9612013-07-10 13:36:23 +01002756 uint64_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002757
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002758 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
Chris Wilson9ce079e2012-04-17 15:31:30 +01002759 0xfffff000) << 32;
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002760 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
Imre Deak56c844e2013-01-07 21:47:34 +02002761 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002762 if (obj->tiling_mode == I915_TILING_Y)
2763 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2764 val |= I965_FENCE_REG_VALID;
Daniel Vetterc6642782010-11-12 13:46:18 +00002765
Chris Wilsond18b9612013-07-10 13:36:23 +01002766 I915_WRITE(fence_reg + 4, val >> 32);
2767 POSTING_READ(fence_reg + 4);
2768
2769 I915_WRITE(fence_reg + 0, val);
2770 POSTING_READ(fence_reg);
2771 } else {
2772 I915_WRITE(fence_reg + 4, 0);
2773 POSTING_READ(fence_reg + 4);
2774 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002775}
2776
Chris Wilson9ce079e2012-04-17 15:31:30 +01002777static void i915_write_fence_reg(struct drm_device *dev, int reg,
2778 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002779{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002780 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002781 u32 val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002782
Chris Wilson9ce079e2012-04-17 15:31:30 +01002783 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002784 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002785 int pitch_val;
2786 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002787
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002788 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
Chris Wilson9ce079e2012-04-17 15:31:30 +01002789 (size & -size) != size ||
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002790 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
2791 "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2792 i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002793
2794 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2795 tile_width = 128;
2796 else
2797 tile_width = 512;
2798
2799 /* Note: pitch better be a power of two tile widths */
2800 pitch_val = obj->stride / tile_width;
2801 pitch_val = ffs(pitch_val) - 1;
2802
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002803 val = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002804 if (obj->tiling_mode == I915_TILING_Y)
2805 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2806 val |= I915_FENCE_SIZE_BITS(size);
2807 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2808 val |= I830_FENCE_REG_VALID;
2809 } else
2810 val = 0;
2811
2812 if (reg < 8)
2813 reg = FENCE_REG_830_0 + reg * 4;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002814 else
Chris Wilson9ce079e2012-04-17 15:31:30 +01002815 reg = FENCE_REG_945_8 + (reg - 8) * 4;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002816
Chris Wilson9ce079e2012-04-17 15:31:30 +01002817 I915_WRITE(reg, val);
2818 POSTING_READ(reg);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002819}
2820
Chris Wilson9ce079e2012-04-17 15:31:30 +01002821static void i830_write_fence_reg(struct drm_device *dev, int reg,
2822 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002823{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002824 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002825 uint32_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002826
Chris Wilson9ce079e2012-04-17 15:31:30 +01002827 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002828 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002829 uint32_t pitch_val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002830
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002831 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
Chris Wilson9ce079e2012-04-17 15:31:30 +01002832 (size & -size) != size ||
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002833 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
2834 "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
2835 i915_gem_obj_ggtt_offset(obj), size);
Eric Anholte76a16d2009-05-26 17:44:56 -07002836
Chris Wilson9ce079e2012-04-17 15:31:30 +01002837 pitch_val = obj->stride / 128;
2838 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002839
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002840 val = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002841 if (obj->tiling_mode == I915_TILING_Y)
2842 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2843 val |= I830_FENCE_SIZE_BITS(size);
2844 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2845 val |= I830_FENCE_REG_VALID;
2846 } else
2847 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002848
Chris Wilson9ce079e2012-04-17 15:31:30 +01002849 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2850 POSTING_READ(FENCE_REG_830_0 + reg * 4);
2851}
2852
Chris Wilsond0a57782012-10-09 19:24:37 +01002853inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
2854{
2855 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
2856}
2857
Chris Wilson9ce079e2012-04-17 15:31:30 +01002858static void i915_gem_write_fence(struct drm_device *dev, int reg,
2859 struct drm_i915_gem_object *obj)
2860{
Chris Wilsond0a57782012-10-09 19:24:37 +01002861 struct drm_i915_private *dev_priv = dev->dev_private;
2862
2863 /* Ensure that all CPU reads are completed before installing a fence
2864 * and all writes before removing the fence.
2865 */
2866 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
2867 mb();
2868
Daniel Vetter94a335d2013-07-17 14:51:28 +02002869 WARN(obj && (!obj->stride || !obj->tiling_mode),
2870 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
2871 obj->stride, obj->tiling_mode);
2872
Chris Wilson9ce079e2012-04-17 15:31:30 +01002873 switch (INTEL_INFO(dev)->gen) {
2874 case 7:
Imre Deak56c844e2013-01-07 21:47:34 +02002875 case 6:
Chris Wilson9ce079e2012-04-17 15:31:30 +01002876 case 5:
2877 case 4: i965_write_fence_reg(dev, reg, obj); break;
2878 case 3: i915_write_fence_reg(dev, reg, obj); break;
2879 case 2: i830_write_fence_reg(dev, reg, obj); break;
Ben Widawsky7dbf9d62012-12-18 10:31:22 -08002880 default: BUG();
Chris Wilson9ce079e2012-04-17 15:31:30 +01002881 }
Chris Wilsond0a57782012-10-09 19:24:37 +01002882
2883 /* And similarly be paranoid that no direct access to this region
2884 * is reordered to before the fence is installed.
2885 */
2886 if (i915_gem_object_needs_mb(obj))
2887 mb();
Jesse Barnesde151cf2008-11-12 10:03:55 -08002888}
2889
Chris Wilson61050802012-04-17 15:31:31 +01002890static inline int fence_number(struct drm_i915_private *dev_priv,
2891 struct drm_i915_fence_reg *fence)
2892{
2893 return fence - dev_priv->fence_regs;
2894}
2895
2896static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2897 struct drm_i915_fence_reg *fence,
2898 bool enable)
2899{
Chris Wilson2dc8aae2013-05-22 17:08:06 +01002900 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson46a0b632013-07-10 13:36:24 +01002901 int reg = fence_number(dev_priv, fence);
Chris Wilson61050802012-04-17 15:31:31 +01002902
Chris Wilson46a0b632013-07-10 13:36:24 +01002903 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
Chris Wilson61050802012-04-17 15:31:31 +01002904
2905 if (enable) {
Chris Wilson46a0b632013-07-10 13:36:24 +01002906 obj->fence_reg = reg;
Chris Wilson61050802012-04-17 15:31:31 +01002907 fence->obj = obj;
2908 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2909 } else {
2910 obj->fence_reg = I915_FENCE_REG_NONE;
2911 fence->obj = NULL;
2912 list_del_init(&fence->lru_list);
2913 }
Daniel Vetter94a335d2013-07-17 14:51:28 +02002914 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +01002915}
2916
Chris Wilsond9e86c02010-11-10 16:40:20 +00002917static int
Chris Wilsond0a57782012-10-09 19:24:37 +01002918i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002919{
Chris Wilson1c293ea2012-04-17 15:31:27 +01002920 if (obj->last_fenced_seqno) {
Chris Wilson86d5bc32012-07-20 12:41:04 +01002921 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
Chris Wilson18991842012-04-17 15:31:29 +01002922 if (ret)
2923 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002924
2925 obj->last_fenced_seqno = 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002926 }
2927
Chris Wilson86d5bc32012-07-20 12:41:04 +01002928 obj->fenced_gpu_access = false;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002929 return 0;
2930}
2931
2932int
2933i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2934{
Chris Wilson61050802012-04-17 15:31:31 +01002935 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsonf9c513e2013-03-26 11:29:27 +00002936 struct drm_i915_fence_reg *fence;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002937 int ret;
2938
Chris Wilsond0a57782012-10-09 19:24:37 +01002939 ret = i915_gem_object_wait_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002940 if (ret)
2941 return ret;
2942
Chris Wilson61050802012-04-17 15:31:31 +01002943 if (obj->fence_reg == I915_FENCE_REG_NONE)
2944 return 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002945
Chris Wilsonf9c513e2013-03-26 11:29:27 +00002946 fence = &dev_priv->fence_regs[obj->fence_reg];
2947
Chris Wilson61050802012-04-17 15:31:31 +01002948 i915_gem_object_fence_lost(obj);
Chris Wilsonf9c513e2013-03-26 11:29:27 +00002949 i915_gem_object_update_fence(obj, fence, false);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002950
2951 return 0;
2952}
2953
2954static struct drm_i915_fence_reg *
Chris Wilsona360bb12012-04-17 15:31:25 +01002955i915_find_fence_reg(struct drm_device *dev)
Daniel Vetterae3db242010-02-19 11:51:58 +01002956{
Daniel Vetterae3db242010-02-19 11:51:58 +01002957 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8fe301a2012-04-17 15:31:28 +01002958 struct drm_i915_fence_reg *reg, *avail;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002959 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002960
2961 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002962 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002963 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2964 reg = &dev_priv->fence_regs[i];
2965 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002966 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002967
Chris Wilson1690e1e2011-12-14 13:57:08 +01002968 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002969 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002970 }
2971
Chris Wilsond9e86c02010-11-10 16:40:20 +00002972 if (avail == NULL)
2973 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002974
2975 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002976 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002977 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002978 continue;
2979
Chris Wilson8fe301a2012-04-17 15:31:28 +01002980 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002981 }
2982
Chris Wilson8fe301a2012-04-17 15:31:28 +01002983 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002984}
2985
Jesse Barnesde151cf2008-11-12 10:03:55 -08002986/**
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002987 * i915_gem_object_get_fence - set up fencing for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002988 * @obj: object to map through a fence reg
2989 *
2990 * When mapping objects through the GTT, userspace wants to be able to write
2991 * to them without having to worry about swizzling if the object is tiled.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002992 * This function walks the fence regs looking for a free one for @obj,
2993 * stealing one if it can't find any.
2994 *
2995 * It then sets up the reg based on the object's properties: address, pitch
2996 * and tiling format.
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002997 *
2998 * For an untiled surface, this removes any existing fence.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002999 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01003000int
Chris Wilson06d98132012-04-17 15:31:24 +01003001i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08003002{
Chris Wilson05394f32010-11-08 19:18:58 +00003003 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003004 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson14415742012-04-17 15:31:33 +01003005 bool enable = obj->tiling_mode != I915_TILING_NONE;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003006 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01003007 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003008
Chris Wilson14415742012-04-17 15:31:33 +01003009 /* Have we updated the tiling parameters upon the object and so
3010 * will need to serialise the write to the associated fence register?
3011 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +01003012 if (obj->fence_dirty) {
Chris Wilsond0a57782012-10-09 19:24:37 +01003013 ret = i915_gem_object_wait_fence(obj);
Chris Wilson14415742012-04-17 15:31:33 +01003014 if (ret)
3015 return ret;
3016 }
Chris Wilson9a5a53b2012-03-22 15:10:00 +00003017
Chris Wilsond9e86c02010-11-10 16:40:20 +00003018 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00003019 if (obj->fence_reg != I915_FENCE_REG_NONE) {
3020 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson5d82e3e2012-04-21 16:23:23 +01003021 if (!obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01003022 list_move_tail(&reg->lru_list,
3023 &dev_priv->mm.fence_list);
3024 return 0;
3025 }
3026 } else if (enable) {
3027 reg = i915_find_fence_reg(dev);
3028 if (reg == NULL)
3029 return -EDEADLK;
Chris Wilsond9e86c02010-11-10 16:40:20 +00003030
Chris Wilson14415742012-04-17 15:31:33 +01003031 if (reg->obj) {
3032 struct drm_i915_gem_object *old = reg->obj;
3033
Chris Wilsond0a57782012-10-09 19:24:37 +01003034 ret = i915_gem_object_wait_fence(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00003035 if (ret)
3036 return ret;
3037
Chris Wilson14415742012-04-17 15:31:33 +01003038 i915_gem_object_fence_lost(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00003039 }
Chris Wilson14415742012-04-17 15:31:33 +01003040 } else
Eric Anholta09ba7f2009-08-29 12:49:51 -07003041 return 0;
Eric Anholta09ba7f2009-08-29 12:49:51 -07003042
Chris Wilson14415742012-04-17 15:31:33 +01003043 i915_gem_object_update_fence(obj, reg, enable);
Chris Wilson14415742012-04-17 15:31:33 +01003044
Chris Wilson9ce079e2012-04-17 15:31:30 +01003045 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08003046}
3047
Chris Wilson42d6ab42012-07-26 11:49:32 +01003048static bool i915_gem_valid_gtt_space(struct drm_device *dev,
3049 struct drm_mm_node *gtt_space,
3050 unsigned long cache_level)
3051{
3052 struct drm_mm_node *other;
3053
3054 /* On non-LLC machines we have to be careful when putting differing
3055 * types of snoopable memory together to avoid the prefetcher
Damien Lespiau4239ca72012-12-03 16:26:16 +00003056 * crossing memory domains and dying.
Chris Wilson42d6ab42012-07-26 11:49:32 +01003057 */
3058 if (HAS_LLC(dev))
3059 return true;
3060
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003061 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01003062 return true;
3063
3064 if (list_empty(&gtt_space->node_list))
3065 return true;
3066
3067 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
3068 if (other->allocated && !other->hole_follows && other->color != cache_level)
3069 return false;
3070
3071 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3072 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3073 return false;
3074
3075 return true;
3076}
3077
3078static void i915_gem_verify_gtt(struct drm_device *dev)
3079{
3080#if WATCH_GTT
3081 struct drm_i915_private *dev_priv = dev->dev_private;
3082 struct drm_i915_gem_object *obj;
3083 int err = 0;
3084
Ben Widawsky35c20a62013-05-31 11:28:48 -07003085 list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
Chris Wilson42d6ab42012-07-26 11:49:32 +01003086 if (obj->gtt_space == NULL) {
3087 printk(KERN_ERR "object found on GTT list with no space reserved\n");
3088 err++;
3089 continue;
3090 }
3091
3092 if (obj->cache_level != obj->gtt_space->color) {
3093 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003094 i915_gem_obj_ggtt_offset(obj),
3095 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
Chris Wilson42d6ab42012-07-26 11:49:32 +01003096 obj->cache_level,
3097 obj->gtt_space->color);
3098 err++;
3099 continue;
3100 }
3101
3102 if (!i915_gem_valid_gtt_space(dev,
3103 obj->gtt_space,
3104 obj->cache_level)) {
3105 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003106 i915_gem_obj_ggtt_offset(obj),
3107 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
Chris Wilson42d6ab42012-07-26 11:49:32 +01003108 obj->cache_level);
3109 err++;
3110 continue;
3111 }
3112 }
3113
3114 WARN_ON(err);
3115#endif
3116}
3117
Jesse Barnesde151cf2008-11-12 10:03:55 -08003118/**
Eric Anholt673a3942008-07-30 12:06:12 -07003119 * Finds free space in the GTT aperture and binds the object there.
3120 */
3121static int
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003122i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
3123 struct i915_address_space *vm,
3124 unsigned alignment,
3125 bool map_and_fenceable,
3126 bool nonblocking)
Eric Anholt673a3942008-07-30 12:06:12 -07003127{
Chris Wilson05394f32010-11-08 19:18:58 +00003128 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003129 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter5e783302010-11-14 22:32:36 +01003130 u32 size, fence_size, fence_alignment, unfenced_alignment;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003131 size_t gtt_max =
3132 map_and_fenceable ? dev_priv->gtt.mappable_end : vm->total;
Ben Widawsky2f633152013-07-17 12:19:03 -07003133 struct i915_vma *vma;
Chris Wilson07f73f62009-09-14 16:50:30 +01003134 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003135
Chris Wilsone28f8712011-07-18 13:11:49 -07003136 fence_size = i915_gem_get_gtt_size(dev,
3137 obj->base.size,
3138 obj->tiling_mode);
3139 fence_alignment = i915_gem_get_gtt_alignment(dev,
3140 obj->base.size,
Imre Deakd8651102013-01-07 21:47:33 +02003141 obj->tiling_mode, true);
Chris Wilsone28f8712011-07-18 13:11:49 -07003142 unfenced_alignment =
Imre Deakd8651102013-01-07 21:47:33 +02003143 i915_gem_get_gtt_alignment(dev,
Chris Wilsone28f8712011-07-18 13:11:49 -07003144 obj->base.size,
Imre Deakd8651102013-01-07 21:47:33 +02003145 obj->tiling_mode, false);
Chris Wilsona00b10c2010-09-24 21:15:47 +01003146
Eric Anholt673a3942008-07-30 12:06:12 -07003147 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01003148 alignment = map_and_fenceable ? fence_alignment :
3149 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003150 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003151 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
3152 return -EINVAL;
3153 }
3154
Chris Wilson05394f32010-11-08 19:18:58 +00003155 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01003156
Chris Wilson654fc602010-05-27 13:18:21 +01003157 /* If the object is bigger than the entire aperture, reject it early
3158 * before evicting everything in a vain attempt to find space.
3159 */
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003160 if (obj->base.size > gtt_max) {
Jani Nikula3765f302013-06-07 16:03:50 +03003161 DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
Chris Wilsona36689c2013-05-21 16:58:49 +01003162 obj->base.size,
3163 map_and_fenceable ? "mappable" : "total",
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003164 gtt_max);
Chris Wilson654fc602010-05-27 13:18:21 +01003165 return -E2BIG;
3166 }
3167
Chris Wilson37e680a2012-06-07 15:38:42 +01003168 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003169 if (ret)
3170 return ret;
3171
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003172 i915_gem_object_pin_pages(obj);
3173
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003174 BUG_ON(!i915_is_ggtt(vm));
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003175
Ben Widawskyaccfef22013-08-14 11:38:35 +02003176 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
Dan Carpenterdb473b32013-07-19 08:45:46 +03003177 if (IS_ERR(vma)) {
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003178 ret = PTR_ERR(vma);
3179 goto err_unpin;
Ben Widawsky2f633152013-07-17 12:19:03 -07003180 }
3181
Ben Widawskyaccfef22013-08-14 11:38:35 +02003182 /* For now we only ever use 1 vma per object */
3183 WARN_ON(!list_is_singular(&obj->vma_list));
3184
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003185search_free:
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003186 ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003187 size, alignment,
David Herrmann31e5d7c2013-07-27 13:36:27 +02003188 obj->cache_level, 0, gtt_max,
3189 DRM_MM_SEARCH_DEFAULT);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003190 if (ret) {
Ben Widawskyf6cd1f12013-07-31 17:00:11 -07003191 ret = i915_gem_evict_something(dev, vm, size, alignment,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003192 obj->cache_level,
Chris Wilson86a1ee22012-08-11 15:41:04 +01003193 map_and_fenceable,
3194 nonblocking);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003195 if (ret == 0)
3196 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003197
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003198 goto err_free_vma;
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003199 }
Ben Widawsky2f633152013-07-17 12:19:03 -07003200 if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003201 obj->cache_level))) {
Ben Widawsky2f633152013-07-17 12:19:03 -07003202 ret = -EINVAL;
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003203 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003204 }
3205
Daniel Vetter74163902012-02-15 23:50:21 +01003206 ret = i915_gem_gtt_prepare_object(obj);
Ben Widawsky2f633152013-07-17 12:19:03 -07003207 if (ret)
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003208 goto err_remove_node;
Eric Anholt673a3942008-07-30 12:06:12 -07003209
Ben Widawsky35c20a62013-05-31 11:28:48 -07003210 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Ben Widawskyca191b12013-07-31 17:00:14 -07003211 list_add_tail(&vma->mm_list, &vm->inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003212
Ben Widawsky4bd561b2013-08-13 18:09:07 -07003213 if (i915_is_ggtt(vm)) {
3214 bool mappable, fenceable;
Chris Wilsona00b10c2010-09-24 21:15:47 +01003215
Daniel Vetter49987092013-08-14 10:21:23 +02003216 fenceable = (vma->node.size == fence_size &&
3217 (vma->node.start & (fence_alignment - 1)) == 0);
Chris Wilsona00b10c2010-09-24 21:15:47 +01003218
Daniel Vetter49987092013-08-14 10:21:23 +02003219 mappable = (vma->node.start + obj->base.size <=
3220 dev_priv->gtt.mappable_end);
Ben Widawsky4bd561b2013-08-13 18:09:07 -07003221
Ben Widawsky5cacaac2013-07-31 17:00:13 -07003222 obj->map_and_fenceable = mappable && fenceable;
Ben Widawsky4bd561b2013-08-13 18:09:07 -07003223 }
Daniel Vetter75e9e912010-11-04 17:11:09 +01003224
Ben Widawsky7ace7ef2013-08-09 22:12:12 -07003225 WARN_ON(map_and_fenceable && !obj->map_and_fenceable);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003226
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003227 trace_i915_vma_bind(vma, map_and_fenceable);
Chris Wilson42d6ab42012-07-26 11:49:32 +01003228 i915_gem_verify_gtt(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003229 return 0;
Ben Widawsky2f633152013-07-17 12:19:03 -07003230
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003231err_remove_node:
Dan Carpenter6286ef92013-07-19 08:46:27 +03003232 drm_mm_remove_node(&vma->node);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003233err_free_vma:
Ben Widawsky2f633152013-07-17 12:19:03 -07003234 i915_gem_vma_destroy(vma);
Daniel Vetterbc6bc152013-07-22 12:12:38 +02003235err_unpin:
Ben Widawsky2f633152013-07-17 12:19:03 -07003236 i915_gem_object_unpin_pages(obj);
Ben Widawsky2f633152013-07-17 12:19:03 -07003237 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003238}
3239
Chris Wilson000433b2013-08-08 14:41:09 +01003240bool
Chris Wilson2c225692013-08-09 12:26:45 +01003241i915_gem_clflush_object(struct drm_i915_gem_object *obj,
3242 bool force)
Eric Anholt673a3942008-07-30 12:06:12 -07003243{
Eric Anholt673a3942008-07-30 12:06:12 -07003244 /* If we don't have a page list set up, then we're not pinned
3245 * to GPU, and we can ignore the cache flush because it'll happen
3246 * again at bind time.
3247 */
Chris Wilson05394f32010-11-08 19:18:58 +00003248 if (obj->pages == NULL)
Chris Wilson000433b2013-08-08 14:41:09 +01003249 return false;
Eric Anholt673a3942008-07-30 12:06:12 -07003250
Imre Deak769ce462013-02-13 21:56:05 +02003251 /*
3252 * Stolen memory is always coherent with the GPU as it is explicitly
3253 * marked as wc by the system, or the system is cache-coherent.
3254 */
3255 if (obj->stolen)
Chris Wilson000433b2013-08-08 14:41:09 +01003256 return false;
Imre Deak769ce462013-02-13 21:56:05 +02003257
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003258 /* If the GPU is snooping the contents of the CPU cache,
3259 * we do not need to manually clear the CPU cache lines. However,
3260 * the caches are only snooped when the render cache is
3261 * flushed/invalidated. As we always have to emit invalidations
3262 * and flushes when moving into and out of the RENDER domain, correct
3263 * snooping behaviour occurs naturally as the result of our domain
3264 * tracking.
3265 */
Chris Wilson2c225692013-08-09 12:26:45 +01003266 if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
Chris Wilson000433b2013-08-08 14:41:09 +01003267 return false;
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003268
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003269 trace_i915_gem_object_clflush(obj);
Chris Wilson9da3da62012-06-01 15:20:22 +01003270 drm_clflush_sg(obj->pages);
Chris Wilson000433b2013-08-08 14:41:09 +01003271
3272 return true;
Eric Anholte47c68e2008-11-14 13:35:19 -08003273}
3274
3275/** Flushes the GTT write domain for the object if it's dirty. */
3276static void
Chris Wilson05394f32010-11-08 19:18:58 +00003277i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003278{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003279 uint32_t old_write_domain;
3280
Chris Wilson05394f32010-11-08 19:18:58 +00003281 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003282 return;
3283
Chris Wilson63256ec2011-01-04 18:42:07 +00003284 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003285 * to it immediately go to main memory as far as we know, so there's
3286 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003287 *
3288 * However, we do have to enforce the order so that all writes through
3289 * the GTT land before any writes to the device, such as updates to
3290 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003291 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003292 wmb();
3293
Chris Wilson05394f32010-11-08 19:18:58 +00003294 old_write_domain = obj->base.write_domain;
3295 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003296
3297 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003298 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003299 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003300}
3301
3302/** Flushes the CPU write domain for the object if it's dirty. */
3303static void
Chris Wilson2c225692013-08-09 12:26:45 +01003304i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
3305 bool force)
Eric Anholte47c68e2008-11-14 13:35:19 -08003306{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003307 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003308
Chris Wilson05394f32010-11-08 19:18:58 +00003309 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003310 return;
3311
Chris Wilson000433b2013-08-08 14:41:09 +01003312 if (i915_gem_clflush_object(obj, force))
3313 i915_gem_chipset_flush(obj->base.dev);
3314
Chris Wilson05394f32010-11-08 19:18:58 +00003315 old_write_domain = obj->base.write_domain;
3316 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003317
3318 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003319 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003320 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003321}
3322
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003323/**
3324 * Moves a single object to the GTT read, and possibly write domain.
3325 *
3326 * This function returns when the move is complete, including waiting on
3327 * flushes to occur.
3328 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003329int
Chris Wilson20217462010-11-23 15:26:33 +00003330i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003331{
Chris Wilson8325a092012-04-24 15:52:35 +01003332 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003333 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003334 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003335
Eric Anholt02354392008-11-26 13:58:13 -08003336 /* Not valid to be called on unbound objects. */
Ben Widawsky98438772013-07-31 17:00:12 -07003337 if (!i915_gem_obj_bound_any(obj))
Eric Anholt02354392008-11-26 13:58:13 -08003338 return -EINVAL;
3339
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003340 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3341 return 0;
3342
Chris Wilson0201f1e2012-07-20 12:41:01 +01003343 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003344 if (ret)
3345 return ret;
3346
Chris Wilson2c225692013-08-09 12:26:45 +01003347 i915_gem_object_flush_cpu_write_domain(obj, false);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003348
Chris Wilsond0a57782012-10-09 19:24:37 +01003349 /* Serialise direct access to this object with the barriers for
3350 * coherent writes from the GPU, by effectively invalidating the
3351 * GTT domain upon first access.
3352 */
3353 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3354 mb();
3355
Chris Wilson05394f32010-11-08 19:18:58 +00003356 old_write_domain = obj->base.write_domain;
3357 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003358
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003359 /* It should now be out of any other write domains, and we can update
3360 * the domain values for our changes.
3361 */
Chris Wilson05394f32010-11-08 19:18:58 +00003362 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3363 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003364 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003365 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3366 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3367 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003368 }
3369
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003370 trace_i915_gem_object_change_domain(obj,
3371 old_read_domains,
3372 old_write_domain);
3373
Chris Wilson8325a092012-04-24 15:52:35 +01003374 /* And bump the LRU for this access */
Ben Widawskyca191b12013-07-31 17:00:14 -07003375 if (i915_gem_object_is_inactive(obj)) {
3376 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
3377 &dev_priv->gtt.base);
3378 if (vma)
3379 list_move_tail(&vma->mm_list,
3380 &dev_priv->gtt.base.inactive_list);
3381
3382 }
Chris Wilson8325a092012-04-24 15:52:35 +01003383
Eric Anholte47c68e2008-11-14 13:35:19 -08003384 return 0;
3385}
3386
Chris Wilsone4ffd172011-04-04 09:44:39 +01003387int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3388 enum i915_cache_level cache_level)
3389{
Daniel Vetter7bddb012012-02-09 17:15:47 +01003390 struct drm_device *dev = obj->base.dev;
3391 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003392 struct i915_vma *vma;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003393 int ret;
3394
3395 if (obj->cache_level == cache_level)
3396 return 0;
3397
3398 if (obj->pin_count) {
3399 DRM_DEBUG("can not change the cache level of pinned objects\n");
3400 return -EBUSY;
3401 }
3402
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003403 list_for_each_entry(vma, &obj->vma_list, vma_link) {
3404 if (!i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003405 ret = i915_vma_unbind(vma);
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003406 if (ret)
3407 return ret;
3408
3409 break;
3410 }
Chris Wilson42d6ab42012-07-26 11:49:32 +01003411 }
3412
Ben Widawsky3089c6f2013-07-31 17:00:03 -07003413 if (i915_gem_obj_bound_any(obj)) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003414 ret = i915_gem_object_finish_gpu(obj);
3415 if (ret)
3416 return ret;
3417
3418 i915_gem_object_finish_gtt(obj);
3419
3420 /* Before SandyBridge, you could not use tiling or fence
3421 * registers with snooped memory, so relinquish any fences
3422 * currently pointing to our region in the aperture.
3423 */
Chris Wilson42d6ab42012-07-26 11:49:32 +01003424 if (INTEL_INFO(dev)->gen < 6) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003425 ret = i915_gem_object_put_fence(obj);
3426 if (ret)
3427 return ret;
3428 }
3429
Daniel Vetter74898d72012-02-15 23:50:22 +01003430 if (obj->has_global_gtt_mapping)
3431 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01003432 if (obj->has_aliasing_ppgtt_mapping)
3433 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3434 obj, cache_level);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003435 }
3436
Chris Wilson2c225692013-08-09 12:26:45 +01003437 list_for_each_entry(vma, &obj->vma_list, vma_link)
3438 vma->node.color = cache_level;
3439 obj->cache_level = cache_level;
3440
3441 if (cpu_write_needs_clflush(obj)) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003442 u32 old_read_domains, old_write_domain;
3443
3444 /* If we're coming from LLC cached, then we haven't
3445 * actually been tracking whether the data is in the
3446 * CPU cache or not, since we only allow one bit set
3447 * in obj->write_domain and have been skipping the clflushes.
3448 * Just set it to the CPU cache for now.
3449 */
3450 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003451
3452 old_read_domains = obj->base.read_domains;
3453 old_write_domain = obj->base.write_domain;
3454
3455 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3456 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3457
3458 trace_i915_gem_object_change_domain(obj,
3459 old_read_domains,
3460 old_write_domain);
3461 }
3462
Chris Wilson42d6ab42012-07-26 11:49:32 +01003463 i915_gem_verify_gtt(dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003464 return 0;
3465}
3466
Ben Widawsky199adf42012-09-21 17:01:20 -07003467int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3468 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003469{
Ben Widawsky199adf42012-09-21 17:01:20 -07003470 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003471 struct drm_i915_gem_object *obj;
3472 int ret;
3473
3474 ret = i915_mutex_lock_interruptible(dev);
3475 if (ret)
3476 return ret;
3477
3478 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3479 if (&obj->base == NULL) {
3480 ret = -ENOENT;
3481 goto unlock;
3482 }
3483
Chris Wilson651d7942013-08-08 14:41:10 +01003484 switch (obj->cache_level) {
3485 case I915_CACHE_LLC:
3486 case I915_CACHE_L3_LLC:
3487 args->caching = I915_CACHING_CACHED;
3488 break;
3489
Chris Wilson4257d3b2013-08-08 14:41:11 +01003490 case I915_CACHE_WT:
3491 args->caching = I915_CACHING_DISPLAY;
3492 break;
3493
Chris Wilson651d7942013-08-08 14:41:10 +01003494 default:
3495 args->caching = I915_CACHING_NONE;
3496 break;
3497 }
Chris Wilsone6994ae2012-07-10 10:27:08 +01003498
3499 drm_gem_object_unreference(&obj->base);
3500unlock:
3501 mutex_unlock(&dev->struct_mutex);
3502 return ret;
3503}
3504
Ben Widawsky199adf42012-09-21 17:01:20 -07003505int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3506 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003507{
Ben Widawsky199adf42012-09-21 17:01:20 -07003508 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003509 struct drm_i915_gem_object *obj;
3510 enum i915_cache_level level;
3511 int ret;
3512
Ben Widawsky199adf42012-09-21 17:01:20 -07003513 switch (args->caching) {
3514 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003515 level = I915_CACHE_NONE;
3516 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003517 case I915_CACHING_CACHED:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003518 level = I915_CACHE_LLC;
3519 break;
Chris Wilson4257d3b2013-08-08 14:41:11 +01003520 case I915_CACHING_DISPLAY:
3521 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
3522 break;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003523 default:
3524 return -EINVAL;
3525 }
3526
Ben Widawsky3bc29132012-09-26 16:15:20 -07003527 ret = i915_mutex_lock_interruptible(dev);
3528 if (ret)
3529 return ret;
3530
Chris Wilsone6994ae2012-07-10 10:27:08 +01003531 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3532 if (&obj->base == NULL) {
3533 ret = -ENOENT;
3534 goto unlock;
3535 }
3536
3537 ret = i915_gem_object_set_cache_level(obj, level);
3538
3539 drm_gem_object_unreference(&obj->base);
3540unlock:
3541 mutex_unlock(&dev->struct_mutex);
3542 return ret;
3543}
3544
Chris Wilsoncc98b412013-08-09 12:25:09 +01003545static bool is_pin_display(struct drm_i915_gem_object *obj)
3546{
3547 /* There are 3 sources that pin objects:
3548 * 1. The display engine (scanouts, sprites, cursors);
3549 * 2. Reservations for execbuffer;
3550 * 3. The user.
3551 *
3552 * We can ignore reservations as we hold the struct_mutex and
3553 * are only called outside of the reservation path. The user
3554 * can only increment pin_count once, and so if after
3555 * subtracting the potential reference by the user, any pin_count
3556 * remains, it must be due to another use by the display engine.
3557 */
3558 return obj->pin_count - !!obj->user_pin_count;
3559}
3560
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003561/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003562 * Prepare buffer for display plane (scanout, cursors, etc).
3563 * Can be called from an uninterruptible phase (modesetting) and allows
3564 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003565 */
3566int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003567i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3568 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003569 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003570{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003571 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003572 int ret;
3573
Chris Wilson0be73282010-12-06 14:36:27 +00003574 if (pipelined != obj->ring) {
Ben Widawsky2911a352012-04-05 14:47:36 -07003575 ret = i915_gem_object_sync(obj, pipelined);
3576 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003577 return ret;
3578 }
3579
Chris Wilsoncc98b412013-08-09 12:25:09 +01003580 /* Mark the pin_display early so that we account for the
3581 * display coherency whilst setting up the cache domains.
3582 */
3583 obj->pin_display = true;
3584
Eric Anholta7ef0642011-03-29 16:59:54 -07003585 /* The display engine is not coherent with the LLC cache on gen6. As
3586 * a result, we make sure that the pinning that is about to occur is
3587 * done with uncached PTEs. This is lowest common denominator for all
3588 * chipsets.
3589 *
3590 * However for gen6+, we could do better by using the GFDT bit instead
3591 * of uncaching, which would allow us to flush all the LLC-cached data
3592 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3593 */
Chris Wilson651d7942013-08-08 14:41:10 +01003594 ret = i915_gem_object_set_cache_level(obj,
3595 HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
Eric Anholta7ef0642011-03-29 16:59:54 -07003596 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003597 goto err_unpin_display;
Eric Anholta7ef0642011-03-29 16:59:54 -07003598
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003599 /* As the user may map the buffer once pinned in the display plane
3600 * (e.g. libkms for the bootup splash), we have to ensure that we
3601 * always use map_and_fenceable for all scanout buffers.
3602 */
Ben Widawskyc37e2202013-07-31 16:59:58 -07003603 ret = i915_gem_obj_ggtt_pin(obj, alignment, true, false);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003604 if (ret)
Chris Wilsoncc98b412013-08-09 12:25:09 +01003605 goto err_unpin_display;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003606
Chris Wilson2c225692013-08-09 12:26:45 +01003607 i915_gem_object_flush_cpu_write_domain(obj, true);
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003608
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003609 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003610 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003611
3612 /* It should now be out of any other write domains, and we can update
3613 * the domain values for our changes.
3614 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003615 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003616 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003617
3618 trace_i915_gem_object_change_domain(obj,
3619 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003620 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003621
3622 return 0;
Chris Wilsoncc98b412013-08-09 12:25:09 +01003623
3624err_unpin_display:
3625 obj->pin_display = is_pin_display(obj);
3626 return ret;
3627}
3628
3629void
3630i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj)
3631{
3632 i915_gem_object_unpin(obj);
3633 obj->pin_display = is_pin_display(obj);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003634}
3635
Chris Wilson85345512010-11-13 09:49:11 +00003636int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003637i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003638{
Chris Wilson88241782011-01-07 17:09:48 +00003639 int ret;
3640
Chris Wilsona8198ee2011-04-13 22:04:09 +01003641 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003642 return 0;
3643
Chris Wilson0201f1e2012-07-20 12:41:01 +01003644 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsonc501ae72011-12-14 13:57:23 +01003645 if (ret)
3646 return ret;
3647
Chris Wilsona8198ee2011-04-13 22:04:09 +01003648 /* Ensure that we invalidate the GPU's caches and TLBs. */
3649 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01003650 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00003651}
3652
Eric Anholte47c68e2008-11-14 13:35:19 -08003653/**
3654 * Moves a single object to the CPU read, and possibly write domain.
3655 *
3656 * This function returns when the move is complete, including waiting on
3657 * flushes to occur.
3658 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003659int
Chris Wilson919926a2010-11-12 13:42:53 +00003660i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003661{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003662 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003663 int ret;
3664
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003665 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3666 return 0;
3667
Chris Wilson0201f1e2012-07-20 12:41:01 +01003668 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003669 if (ret)
3670 return ret;
3671
Eric Anholte47c68e2008-11-14 13:35:19 -08003672 i915_gem_object_flush_gtt_write_domain(obj);
3673
Chris Wilson05394f32010-11-08 19:18:58 +00003674 old_write_domain = obj->base.write_domain;
3675 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003676
Eric Anholte47c68e2008-11-14 13:35:19 -08003677 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003678 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Chris Wilson2c225692013-08-09 12:26:45 +01003679 i915_gem_clflush_object(obj, false);
Eric Anholte47c68e2008-11-14 13:35:19 -08003680
Chris Wilson05394f32010-11-08 19:18:58 +00003681 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003682 }
3683
3684 /* It should now be out of any other write domains, and we can update
3685 * the domain values for our changes.
3686 */
Chris Wilson05394f32010-11-08 19:18:58 +00003687 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003688
3689 /* If we're writing through the CPU, then the GPU read domains will
3690 * need to be invalidated at next use.
3691 */
3692 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003693 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3694 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003695 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003696
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003697 trace_i915_gem_object_change_domain(obj,
3698 old_read_domains,
3699 old_write_domain);
3700
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003701 return 0;
3702}
3703
Eric Anholt673a3942008-07-30 12:06:12 -07003704/* Throttle our rendering by waiting until the ring has completed our requests
3705 * emitted over 20 msec ago.
3706 *
Eric Anholtb9624422009-06-03 07:27:35 +00003707 * Note that if we were to use the current jiffies each time around the loop,
3708 * we wouldn't escape the function with any frames outstanding if the time to
3709 * render a frame was over 20ms.
3710 *
Eric Anholt673a3942008-07-30 12:06:12 -07003711 * This should get us reasonable parallelism between CPU and GPU but also
3712 * relatively low latency when blocking on a particular request to finish.
3713 */
3714static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003715i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003716{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003717 struct drm_i915_private *dev_priv = dev->dev_private;
3718 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003719 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003720 struct drm_i915_gem_request *request;
3721 struct intel_ring_buffer *ring = NULL;
Daniel Vetterf69061b2012-12-06 09:01:42 +01003722 unsigned reset_counter;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003723 u32 seqno = 0;
3724 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003725
Daniel Vetter308887a2012-11-14 17:14:06 +01003726 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3727 if (ret)
3728 return ret;
3729
3730 ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
3731 if (ret)
3732 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003733
Chris Wilson1c255952010-09-26 11:03:27 +01003734 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003735 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003736 if (time_after_eq(request->emitted_jiffies, recent_enough))
3737 break;
3738
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003739 ring = request->ring;
3740 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003741 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01003742 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilson1c255952010-09-26 11:03:27 +01003743 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003744
3745 if (seqno == 0)
3746 return 0;
3747
Daniel Vetterf69061b2012-12-06 09:01:42 +01003748 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003749 if (ret == 0)
3750 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003751
Eric Anholt673a3942008-07-30 12:06:12 -07003752 return ret;
3753}
3754
Eric Anholt673a3942008-07-30 12:06:12 -07003755int
Chris Wilson05394f32010-11-08 19:18:58 +00003756i915_gem_object_pin(struct drm_i915_gem_object *obj,
Ben Widawskyc37e2202013-07-31 16:59:58 -07003757 struct i915_address_space *vm,
Chris Wilson05394f32010-11-08 19:18:58 +00003758 uint32_t alignment,
Chris Wilson86a1ee22012-08-11 15:41:04 +01003759 bool map_and_fenceable,
3760 bool nonblocking)
Eric Anholt673a3942008-07-30 12:06:12 -07003761{
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003762 struct i915_vma *vma;
Eric Anholt673a3942008-07-30 12:06:12 -07003763 int ret;
3764
Chris Wilson7e81a422012-09-15 09:41:57 +01003765 if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3766 return -EBUSY;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003767
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003768 WARN_ON(map_and_fenceable && !i915_is_ggtt(vm));
3769
3770 vma = i915_gem_obj_to_vma(obj, vm);
3771
3772 if (vma) {
3773 if ((alignment &&
3774 vma->node.start & (alignment - 1)) ||
Chris Wilson05394f32010-11-08 19:18:58 +00003775 (map_and_fenceable && !obj->map_and_fenceable)) {
3776 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003777 "bo is already pinned with incorrect alignment:"
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003778 " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003779 " obj->map_and_fenceable=%d\n",
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003780 i915_gem_obj_offset(obj, vm), alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003781 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003782 obj->map_and_fenceable);
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003783 ret = i915_vma_unbind(vma);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003784 if (ret)
3785 return ret;
3786 }
3787 }
3788
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003789 if (!i915_gem_obj_bound(obj, vm)) {
Chris Wilson87422672012-11-21 13:04:03 +00003790 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3791
Ben Widawsky07fe0b12013-07-31 17:00:10 -07003792 ret = i915_gem_object_bind_to_vm(obj, vm, alignment,
3793 map_and_fenceable,
3794 nonblocking);
Chris Wilson97311292009-09-21 00:22:34 +01003795 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003796 return ret;
Chris Wilson87422672012-11-21 13:04:03 +00003797
3798 if (!dev_priv->mm.aliasing_ppgtt)
3799 i915_gem_gtt_bind_object(obj, obj->cache_level);
Chris Wilson22c344e2009-02-11 14:26:45 +00003800 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003801
Daniel Vetter74898d72012-02-15 23:50:22 +01003802 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3803 i915_gem_gtt_bind_object(obj, obj->cache_level);
3804
Chris Wilson1b502472012-04-24 15:47:30 +01003805 obj->pin_count++;
Chris Wilson6299f992010-11-24 12:23:44 +00003806 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003807
3808 return 0;
3809}
3810
3811void
Chris Wilson05394f32010-11-08 19:18:58 +00003812i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003813{
Chris Wilson05394f32010-11-08 19:18:58 +00003814 BUG_ON(obj->pin_count == 0);
Ben Widawsky98438772013-07-31 17:00:12 -07003815 BUG_ON(!i915_gem_obj_bound_any(obj));
Eric Anholt673a3942008-07-30 12:06:12 -07003816
Chris Wilson1b502472012-04-24 15:47:30 +01003817 if (--obj->pin_count == 0)
Chris Wilson6299f992010-11-24 12:23:44 +00003818 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003819}
3820
3821int
3822i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003823 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003824{
3825 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003826 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003827 int ret;
3828
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003829 ret = i915_mutex_lock_interruptible(dev);
3830 if (ret)
3831 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003832
Chris Wilson05394f32010-11-08 19:18:58 +00003833 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003834 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003835 ret = -ENOENT;
3836 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003837 }
Eric Anholt673a3942008-07-30 12:06:12 -07003838
Chris Wilson05394f32010-11-08 19:18:58 +00003839 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003840 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003841 ret = -EINVAL;
3842 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003843 }
3844
Chris Wilson05394f32010-11-08 19:18:58 +00003845 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003846 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3847 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003848 ret = -EINVAL;
3849 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003850 }
3851
Chris Wilson93be8782013-01-02 10:31:22 +00003852 if (obj->user_pin_count == 0) {
Ben Widawskyc37e2202013-07-31 16:59:58 -07003853 ret = i915_gem_obj_ggtt_pin(obj, args->alignment, true, false);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003854 if (ret)
3855 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003856 }
3857
Chris Wilson93be8782013-01-02 10:31:22 +00003858 obj->user_pin_count++;
3859 obj->pin_filp = file;
3860
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003861 args->offset = i915_gem_obj_ggtt_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003862out:
Chris Wilson05394f32010-11-08 19:18:58 +00003863 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003864unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003865 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003866 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003867}
3868
3869int
3870i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003871 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003872{
3873 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003874 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003875 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003876
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003877 ret = i915_mutex_lock_interruptible(dev);
3878 if (ret)
3879 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003880
Chris Wilson05394f32010-11-08 19:18:58 +00003881 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003882 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003883 ret = -ENOENT;
3884 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003885 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003886
Chris Wilson05394f32010-11-08 19:18:58 +00003887 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003888 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3889 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003890 ret = -EINVAL;
3891 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003892 }
Chris Wilson05394f32010-11-08 19:18:58 +00003893 obj->user_pin_count--;
3894 if (obj->user_pin_count == 0) {
3895 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003896 i915_gem_object_unpin(obj);
3897 }
Eric Anholt673a3942008-07-30 12:06:12 -07003898
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003899out:
Chris Wilson05394f32010-11-08 19:18:58 +00003900 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003901unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003902 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003903 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003904}
3905
3906int
3907i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003908 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003909{
3910 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003911 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003912 int ret;
3913
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003914 ret = i915_mutex_lock_interruptible(dev);
3915 if (ret)
3916 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003917
Chris Wilson05394f32010-11-08 19:18:58 +00003918 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003919 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003920 ret = -ENOENT;
3921 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003922 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003923
Chris Wilson0be555b2010-08-04 15:36:30 +01003924 /* Count all active objects as busy, even if they are currently not used
3925 * by the gpu. Users of this interface expect objects to eventually
3926 * become non-busy without any further actions, therefore emit any
3927 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003928 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003929 ret = i915_gem_object_flush_active(obj);
3930
Chris Wilson05394f32010-11-08 19:18:58 +00003931 args->busy = obj->active;
Chris Wilsone9808ed2012-07-04 12:25:08 +01003932 if (obj->ring) {
3933 BUILD_BUG_ON(I915_NUM_RINGS > 16);
3934 args->busy |= intel_ring_flag(obj->ring) << 16;
3935 }
Eric Anholt673a3942008-07-30 12:06:12 -07003936
Chris Wilson05394f32010-11-08 19:18:58 +00003937 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003938unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003939 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003940 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003941}
3942
3943int
3944i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3945 struct drm_file *file_priv)
3946{
Akshay Joshi0206e352011-08-16 15:34:10 -04003947 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003948}
3949
Chris Wilson3ef94da2009-09-14 16:50:29 +01003950int
3951i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3952 struct drm_file *file_priv)
3953{
3954 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003955 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003956 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003957
3958 switch (args->madv) {
3959 case I915_MADV_DONTNEED:
3960 case I915_MADV_WILLNEED:
3961 break;
3962 default:
3963 return -EINVAL;
3964 }
3965
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003966 ret = i915_mutex_lock_interruptible(dev);
3967 if (ret)
3968 return ret;
3969
Chris Wilson05394f32010-11-08 19:18:58 +00003970 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003971 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003972 ret = -ENOENT;
3973 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003974 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003975
Chris Wilson05394f32010-11-08 19:18:58 +00003976 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003977 ret = -EINVAL;
3978 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003979 }
3980
Chris Wilson05394f32010-11-08 19:18:58 +00003981 if (obj->madv != __I915_MADV_PURGED)
3982 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003983
Chris Wilson6c085a72012-08-20 11:40:46 +02003984 /* if the object is no longer attached, discard its backing storage */
3985 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003986 i915_gem_object_truncate(obj);
3987
Chris Wilson05394f32010-11-08 19:18:58 +00003988 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003989
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003990out:
Chris Wilson05394f32010-11-08 19:18:58 +00003991 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003992unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003993 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003994 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003995}
3996
Chris Wilson37e680a2012-06-07 15:38:42 +01003997void i915_gem_object_init(struct drm_i915_gem_object *obj,
3998 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01003999{
Ben Widawsky35c20a62013-05-31 11:28:48 -07004000 INIT_LIST_HEAD(&obj->global_list);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004001 INIT_LIST_HEAD(&obj->ring_list);
Ben Widawskyb25cb2f2013-08-14 11:38:33 +02004002 INIT_LIST_HEAD(&obj->obj_exec_link);
Ben Widawsky2f633152013-07-17 12:19:03 -07004003 INIT_LIST_HEAD(&obj->vma_list);
Chris Wilson0327d6b2012-08-11 15:41:06 +01004004
Chris Wilson37e680a2012-06-07 15:38:42 +01004005 obj->ops = ops;
4006
Chris Wilson0327d6b2012-08-11 15:41:06 +01004007 obj->fence_reg = I915_FENCE_REG_NONE;
4008 obj->madv = I915_MADV_WILLNEED;
4009 /* Avoid an unnecessary call to unbind on the first bind. */
4010 obj->map_and_fenceable = true;
4011
4012 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
4013}
4014
Chris Wilson37e680a2012-06-07 15:38:42 +01004015static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
4016 .get_pages = i915_gem_object_get_pages_gtt,
4017 .put_pages = i915_gem_object_put_pages_gtt,
4018};
4019
Chris Wilson05394f32010-11-08 19:18:58 +00004020struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4021 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00004022{
Daniel Vetterc397b902010-04-09 19:05:07 +00004023 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07004024 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01004025 gfp_t mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00004026
Chris Wilson42dcedd2012-11-15 11:32:30 +00004027 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00004028 if (obj == NULL)
4029 return NULL;
4030
4031 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
Chris Wilson42dcedd2012-11-15 11:32:30 +00004032 i915_gem_object_free(obj);
Daniel Vetterc397b902010-04-09 19:05:07 +00004033 return NULL;
4034 }
4035
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004036 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
4037 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
4038 /* 965gm cannot relocate objects above 4GiB. */
4039 mask &= ~__GFP_HIGHMEM;
4040 mask |= __GFP_DMA32;
4041 }
4042
Al Viro496ad9a2013-01-23 17:07:38 -05004043 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01004044 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07004045
Chris Wilson37e680a2012-06-07 15:38:42 +01004046 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01004047
Daniel Vetterc397b902010-04-09 19:05:07 +00004048 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4049 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4050
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02004051 if (HAS_LLC(dev)) {
4052 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07004053 * cache) for about a 10% performance improvement
4054 * compared to uncached. Graphics requests other than
4055 * display scanout are coherent with the CPU in
4056 * accessing this cache. This means in this mode we
4057 * don't need to clflush on the CPU side, and on the
4058 * GPU side we only need to flush internal caches to
4059 * get data visible to the CPU.
4060 *
4061 * However, we maintain the display planes as UC, and so
4062 * need to rebind when first used as such.
4063 */
4064 obj->cache_level = I915_CACHE_LLC;
4065 } else
4066 obj->cache_level = I915_CACHE_NONE;
4067
Daniel Vetterd861e332013-07-24 23:25:03 +02004068 trace_i915_gem_object_create(obj);
4069
Chris Wilson05394f32010-11-08 19:18:58 +00004070 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00004071}
4072
Eric Anholt673a3942008-07-30 12:06:12 -07004073int i915_gem_init_object(struct drm_gem_object *obj)
4074{
Daniel Vetterc397b902010-04-09 19:05:07 +00004075 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08004076
Eric Anholt673a3942008-07-30 12:06:12 -07004077 return 0;
4078}
4079
Chris Wilson1488fc02012-04-24 15:47:31 +01004080void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01004081{
Chris Wilson1488fc02012-04-24 15:47:31 +01004082 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00004083 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01004084 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004085 struct i915_vma *vma, *next;
Chris Wilsonbe726152010-07-23 23:18:50 +01004086
Chris Wilson26e12f892011-03-20 11:20:19 +00004087 trace_i915_gem_object_destroy(obj);
4088
Chris Wilson1488fc02012-04-24 15:47:31 +01004089 if (obj->phys_obj)
4090 i915_gem_detach_phys_object(dev, obj);
4091
4092 obj->pin_count = 0;
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004093 /* NB: 0 or 1 elements */
4094 WARN_ON(!list_empty(&obj->vma_list) &&
4095 !list_is_singular(&obj->vma_list));
4096 list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
4097 int ret = i915_vma_unbind(vma);
4098 if (WARN_ON(ret == -ERESTARTSYS)) {
4099 bool was_interruptible;
Chris Wilson1488fc02012-04-24 15:47:31 +01004100
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004101 was_interruptible = dev_priv->mm.interruptible;
4102 dev_priv->mm.interruptible = false;
Chris Wilson1488fc02012-04-24 15:47:31 +01004103
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004104 WARN_ON(i915_vma_unbind(vma));
Chris Wilson1488fc02012-04-24 15:47:31 +01004105
Ben Widawsky07fe0b12013-07-31 17:00:10 -07004106 dev_priv->mm.interruptible = was_interruptible;
4107 }
Chris Wilson1488fc02012-04-24 15:47:31 +01004108 }
4109
Ben Widawsky1d64ae72013-05-31 14:46:20 -07004110 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
4111 * before progressing. */
4112 if (obj->stolen)
4113 i915_gem_object_unpin_pages(obj);
4114
Ben Widawsky401c29f2013-05-31 11:28:47 -07004115 if (WARN_ON(obj->pages_pin_count))
4116 obj->pages_pin_count = 0;
Chris Wilson37e680a2012-06-07 15:38:42 +01004117 i915_gem_object_put_pages(obj);
Chris Wilsond8cb5082012-08-11 15:41:03 +01004118 i915_gem_object_free_mmap_offset(obj);
Chris Wilson0104fdb2012-11-15 11:32:26 +00004119 i915_gem_object_release_stolen(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004120
Chris Wilson9da3da62012-06-01 15:20:22 +01004121 BUG_ON(obj->pages);
4122
Chris Wilson2f745ad2012-09-04 21:02:58 +01004123 if (obj->base.import_attach)
4124 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01004125
Chris Wilson05394f32010-11-08 19:18:58 +00004126 drm_gem_object_release(&obj->base);
4127 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01004128
Chris Wilson05394f32010-11-08 19:18:58 +00004129 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00004130 i915_gem_object_free(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01004131}
4132
Daniel Vettere656a6c2013-08-14 14:14:04 +02004133struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
Ben Widawsky2f633152013-07-17 12:19:03 -07004134 struct i915_address_space *vm)
4135{
Daniel Vettere656a6c2013-08-14 14:14:04 +02004136 struct i915_vma *vma;
4137 list_for_each_entry(vma, &obj->vma_list, vma_link)
4138 if (vma->vm == vm)
4139 return vma;
4140
4141 return NULL;
4142}
4143
4144static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
4145 struct i915_address_space *vm)
4146{
Ben Widawsky2f633152013-07-17 12:19:03 -07004147 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
4148 if (vma == NULL)
4149 return ERR_PTR(-ENOMEM);
4150
4151 INIT_LIST_HEAD(&vma->vma_link);
Ben Widawskyca191b12013-07-31 17:00:14 -07004152 INIT_LIST_HEAD(&vma->mm_list);
Ben Widawsky82a55ad2013-08-14 11:38:34 +02004153 INIT_LIST_HEAD(&vma->exec_list);
Ben Widawsky2f633152013-07-17 12:19:03 -07004154 vma->vm = vm;
4155 vma->obj = obj;
4156
Ben Widawsky8b9c2b92013-07-31 17:00:16 -07004157 /* Keep GGTT vmas first to make debug easier */
4158 if (i915_is_ggtt(vm))
4159 list_add(&vma->vma_link, &obj->vma_list);
4160 else
4161 list_add_tail(&vma->vma_link, &obj->vma_list);
4162
Ben Widawsky2f633152013-07-17 12:19:03 -07004163 return vma;
4164}
4165
Daniel Vettere656a6c2013-08-14 14:14:04 +02004166struct i915_vma *
4167i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
4168 struct i915_address_space *vm)
4169{
4170 struct i915_vma *vma;
4171
4172 vma = i915_gem_obj_to_vma(obj, vm);
4173 if (!vma)
4174 vma = __i915_gem_vma_create(obj, vm);
4175
4176 return vma;
4177}
4178
Ben Widawsky2f633152013-07-17 12:19:03 -07004179void i915_gem_vma_destroy(struct i915_vma *vma)
4180{
4181 WARN_ON(vma->node.allocated);
Chris Wilsonaaa05662013-08-20 12:56:40 +01004182
4183 /* Keep the vma as a placeholder in the execbuffer reservation lists */
4184 if (!list_empty(&vma->exec_list))
4185 return;
4186
Daniel Vetterb93dab62013-08-26 11:23:47 +02004187 list_del(&vma->vma_link);
4188
Ben Widawsky2f633152013-07-17 12:19:03 -07004189 kfree(vma);
4190}
4191
Jesse Barnes5669fca2009-02-17 15:13:31 -08004192int
Eric Anholt673a3942008-07-30 12:06:12 -07004193i915_gem_idle(struct drm_device *dev)
4194{
4195 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00004196 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004197
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004198 if (dev_priv->ums.mm_suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07004199 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004200 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07004201 }
Eric Anholt673a3942008-07-30 12:06:12 -07004202
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004203 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004204 if (ret) {
4205 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004206 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07004207 }
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07004208 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07004209
Chris Wilson29105cc2010-01-07 10:39:13 +00004210 /* Under UMS, be paranoid and evict. */
Chris Wilsona39d7ef2012-04-24 18:22:52 +01004211 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson6c085a72012-08-20 11:40:46 +02004212 i915_gem_evict_everything(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004213
Daniel Vetter99584db2012-11-14 17:14:04 +01004214 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00004215
4216 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07004217 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00004218
Chris Wilson29105cc2010-01-07 10:39:13 +00004219 /* Cancel the retire work handler, which should be idle now. */
4220 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4221
Eric Anholt673a3942008-07-30 12:06:12 -07004222 return 0;
4223}
4224
Ben Widawskyb9524a12012-05-25 16:56:24 -07004225void i915_gem_l3_remap(struct drm_device *dev)
4226{
4227 drm_i915_private_t *dev_priv = dev->dev_private;
4228 u32 misccpctl;
4229 int i;
4230
Daniel Vettereb32e452013-02-14 19:46:07 +01004231 if (!HAS_L3_GPU_CACHE(dev))
Ben Widawskyb9524a12012-05-25 16:56:24 -07004232 return;
4233
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004234 if (!dev_priv->l3_parity.remap_info)
Ben Widawskyb9524a12012-05-25 16:56:24 -07004235 return;
4236
4237 misccpctl = I915_READ(GEN7_MISCCPCTL);
4238 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
4239 POSTING_READ(GEN7_MISCCPCTL);
4240
4241 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
4242 u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004243 if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
Ben Widawskyb9524a12012-05-25 16:56:24 -07004244 DRM_DEBUG("0x%x was already programmed to %x\n",
4245 GEN7_L3LOG_BASE + i, remap);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004246 if (remap && !dev_priv->l3_parity.remap_info[i/4])
Ben Widawskyb9524a12012-05-25 16:56:24 -07004247 DRM_DEBUG_DRIVER("Clearing remapped register\n");
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004248 I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
Ben Widawskyb9524a12012-05-25 16:56:24 -07004249 }
4250
4251 /* Make sure all the writes land before disabling dop clock gating */
4252 POSTING_READ(GEN7_L3LOG_BASE);
4253
4254 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
4255}
4256
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004257void i915_gem_init_swizzling(struct drm_device *dev)
4258{
4259 drm_i915_private_t *dev_priv = dev->dev_private;
4260
Daniel Vetter11782b02012-01-31 16:47:55 +01004261 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004262 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4263 return;
4264
4265 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4266 DISP_TILE_SURFACE_SWIZZLING);
4267
Daniel Vetter11782b02012-01-31 16:47:55 +01004268 if (IS_GEN5(dev))
4269 return;
4270
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004271 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4272 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004273 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004274 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004275 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004276 else
4277 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004278}
Daniel Vettere21af882012-02-09 20:53:27 +01004279
Chris Wilson67b1b572012-07-05 23:49:40 +01004280static bool
4281intel_enable_blt(struct drm_device *dev)
4282{
4283 if (!HAS_BLT(dev))
4284 return false;
4285
4286 /* The blitter was dysfunctional on early prototypes */
4287 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
4288 DRM_INFO("BLT not supported on this pre-production hardware;"
4289 " graphics performance will be degraded.\n");
4290 return false;
4291 }
4292
4293 return true;
4294}
4295
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004296static int i915_gem_init_rings(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004297{
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004298 struct drm_i915_private *dev_priv = dev->dev_private;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004299 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004300
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004301 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004302 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004303 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004304
4305 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004306 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004307 if (ret)
4308 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004309 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004310
Chris Wilson67b1b572012-07-05 23:49:40 +01004311 if (intel_enable_blt(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01004312 ret = intel_init_blt_ring_buffer(dev);
4313 if (ret)
4314 goto cleanup_bsd_ring;
4315 }
4316
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004317 if (HAS_VEBOX(dev)) {
4318 ret = intel_init_vebox_ring_buffer(dev);
4319 if (ret)
4320 goto cleanup_blt_ring;
4321 }
4322
4323
Mika Kuoppala99433932013-01-22 14:12:17 +02004324 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
4325 if (ret)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004326 goto cleanup_vebox_ring;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004327
4328 return 0;
4329
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004330cleanup_vebox_ring:
4331 intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004332cleanup_blt_ring:
4333 intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
4334cleanup_bsd_ring:
4335 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
4336cleanup_render_ring:
4337 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
4338
4339 return ret;
4340}
4341
4342int
4343i915_gem_init_hw(struct drm_device *dev)
4344{
4345 drm_i915_private_t *dev_priv = dev->dev_private;
4346 int ret;
4347
4348 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
4349 return -EIO;
4350
Ben Widawsky59124502013-07-04 11:02:05 -07004351 if (dev_priv->ellc_size)
Ben Widawsky05e21cc2013-07-04 11:02:04 -07004352 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004353
Rodrigo Vivi94353732013-08-28 16:45:46 -03004354 if (IS_HSW_GT3(dev))
4355 I915_WRITE(MI_PREDICATE_RESULT_2, LOWER_SLICE_ENABLED);
4356 else
4357 I915_WRITE(MI_PREDICATE_RESULT_2, LOWER_SLICE_DISABLED);
4358
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004359 if (HAS_PCH_NOP(dev)) {
4360 u32 temp = I915_READ(GEN7_MSG_CTL);
4361 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4362 I915_WRITE(GEN7_MSG_CTL, temp);
4363 }
4364
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004365 i915_gem_l3_remap(dev);
4366
4367 i915_gem_init_swizzling(dev);
4368
4369 ret = i915_gem_init_rings(dev);
4370 if (ret)
Mika Kuoppala99433932013-01-22 14:12:17 +02004371 return ret;
4372
Ben Widawsky254f9652012-06-04 14:42:42 -07004373 /*
4374 * XXX: There was some w/a described somewhere suggesting loading
4375 * contexts before PPGTT.
4376 */
4377 i915_gem_context_init(dev);
Ben Widawskyb7c36d22013-04-08 18:43:56 -07004378 if (dev_priv->mm.aliasing_ppgtt) {
4379 ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
4380 if (ret) {
4381 i915_gem_cleanup_aliasing_ppgtt(dev);
4382 DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
4383 }
4384 }
Daniel Vettere21af882012-02-09 20:53:27 +01004385
Chris Wilson68f95ba2010-05-27 13:18:22 +01004386 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004387}
4388
Chris Wilson1070a422012-04-24 15:47:41 +01004389int i915_gem_init(struct drm_device *dev)
4390{
4391 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1070a422012-04-24 15:47:41 +01004392 int ret;
4393
Chris Wilson1070a422012-04-24 15:47:41 +01004394 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004395
4396 if (IS_VALLEYVIEW(dev)) {
4397 /* VLVA0 (potential hack), BIOS isn't actually waking us */
4398 I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
4399 if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
4400 DRM_DEBUG_DRIVER("allow wake ack timed out\n");
4401 }
4402
Ben Widawskyd7e50082012-12-18 10:31:25 -08004403 i915_gem_init_global_gtt(dev);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004404
Chris Wilson1070a422012-04-24 15:47:41 +01004405 ret = i915_gem_init_hw(dev);
4406 mutex_unlock(&dev->struct_mutex);
4407 if (ret) {
4408 i915_gem_cleanup_aliasing_ppgtt(dev);
4409 return ret;
4410 }
4411
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004412 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
4413 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4414 dev_priv->dri1.allow_batchbuffer = 1;
Chris Wilson1070a422012-04-24 15:47:41 +01004415 return 0;
4416}
4417
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004418void
4419i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4420{
4421 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01004422 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004423 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004424
Chris Wilsonb4519512012-05-11 14:29:30 +01004425 for_each_ring(ring, dev_priv, i)
4426 intel_cleanup_ring_buffer(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004427}
4428
4429int
Eric Anholt673a3942008-07-30 12:06:12 -07004430i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4431 struct drm_file *file_priv)
4432{
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004433 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01004434 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004435
Jesse Barnes79e53942008-11-07 14:24:08 -08004436 if (drm_core_check_feature(dev, DRIVER_MODESET))
4437 return 0;
4438
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004439 if (i915_reset_in_progress(&dev_priv->gpu_error)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004440 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004441 atomic_set(&dev_priv->gpu_error.reset_counter, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004442 }
4443
Eric Anholt673a3942008-07-30 12:06:12 -07004444 mutex_lock(&dev->struct_mutex);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004445 dev_priv->ums.mm_suspended = 0;
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004446
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004447 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004448 if (ret != 0) {
4449 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004450 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004451 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004452
Ben Widawsky5cef07e2013-07-16 16:50:08 -07004453 BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004454 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004455
Chris Wilson5f353082010-06-07 14:03:03 +01004456 ret = drm_irq_install(dev);
4457 if (ret)
4458 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004459
Eric Anholt673a3942008-07-30 12:06:12 -07004460 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004461
4462cleanup_ringbuffer:
4463 mutex_lock(&dev->struct_mutex);
4464 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004465 dev_priv->ums.mm_suspended = 1;
Chris Wilson5f353082010-06-07 14:03:03 +01004466 mutex_unlock(&dev->struct_mutex);
4467
4468 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004469}
4470
4471int
4472i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4473 struct drm_file *file_priv)
4474{
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004475 struct drm_i915_private *dev_priv = dev->dev_private;
4476 int ret;
4477
Jesse Barnes79e53942008-11-07 14:24:08 -08004478 if (drm_core_check_feature(dev, DRIVER_MODESET))
4479 return 0;
4480
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004481 drm_irq_uninstall(dev);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004482
4483 mutex_lock(&dev->struct_mutex);
4484 ret = i915_gem_idle(dev);
4485
4486 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4487 * We need to replace this with a semaphore, or something.
4488 * And not confound ums.mm_suspended!
4489 */
4490 if (ret != 0)
4491 dev_priv->ums.mm_suspended = 1;
4492 mutex_unlock(&dev->struct_mutex);
4493
4494 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004495}
4496
4497void
4498i915_gem_lastclose(struct drm_device *dev)
4499{
4500 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004501
Eric Anholte806b492009-01-22 09:56:58 -08004502 if (drm_core_check_feature(dev, DRIVER_MODESET))
4503 return;
4504
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004505 mutex_lock(&dev->struct_mutex);
Keith Packard6dbe2772008-10-14 21:41:13 -07004506 ret = i915_gem_idle(dev);
4507 if (ret)
4508 DRM_ERROR("failed to idle hardware: %d\n", ret);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004509 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004510}
4511
Chris Wilson64193402010-10-24 12:38:05 +01004512static void
4513init_ring_lists(struct intel_ring_buffer *ring)
4514{
4515 INIT_LIST_HEAD(&ring->active_list);
4516 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004517}
4518
Ben Widawskyfc8c0672013-07-31 16:59:54 -07004519static void i915_init_vm(struct drm_i915_private *dev_priv,
4520 struct i915_address_space *vm)
4521{
4522 vm->dev = dev_priv->dev;
4523 INIT_LIST_HEAD(&vm->active_list);
4524 INIT_LIST_HEAD(&vm->inactive_list);
4525 INIT_LIST_HEAD(&vm->global_link);
4526 list_add(&vm->global_link, &dev_priv->vm_list);
4527}
4528
Eric Anholt673a3942008-07-30 12:06:12 -07004529void
4530i915_gem_load(struct drm_device *dev)
4531{
4532 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004533 int i;
4534
4535 dev_priv->slab =
4536 kmem_cache_create("i915_gem_object",
4537 sizeof(struct drm_i915_gem_object), 0,
4538 SLAB_HWCACHE_ALIGN,
4539 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004540
Ben Widawskyfc8c0672013-07-31 16:59:54 -07004541 INIT_LIST_HEAD(&dev_priv->vm_list);
4542 i915_init_vm(dev_priv, &dev_priv->gtt.base);
4543
Chris Wilson6c085a72012-08-20 11:40:46 +02004544 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4545 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004546 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004547 for (i = 0; i < I915_NUM_RINGS; i++)
4548 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004549 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004550 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004551 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4552 i915_gem_retire_work_handler);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004553 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004554
Dave Airlie94400122010-07-20 13:15:31 +10004555 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4556 if (IS_GEN3(dev)) {
Daniel Vetter50743292012-04-26 22:02:54 +02004557 I915_WRITE(MI_ARB_STATE,
4558 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Dave Airlie94400122010-07-20 13:15:31 +10004559 }
4560
Chris Wilson72bfa192010-12-19 11:42:05 +00004561 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4562
Jesse Barnesde151cf2008-11-12 10:03:55 -08004563 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004564 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4565 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004566
Ville Syrjälä42b5aea2013-04-09 13:02:47 +03004567 if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
4568 dev_priv->num_fence_regs = 32;
4569 else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004570 dev_priv->num_fence_regs = 16;
4571 else
4572 dev_priv->num_fence_regs = 8;
4573
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004574 /* Initialize fence registers to zero */
Chris Wilson19b2dbd2013-06-12 10:15:12 +01004575 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4576 i915_gem_restore_fences(dev);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004577
Eric Anholt673a3942008-07-30 12:06:12 -07004578 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004579 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004580
Chris Wilsonce453d82011-02-21 14:43:56 +00004581 dev_priv->mm.interruptible = true;
4582
Chris Wilson17250b72010-10-28 12:51:39 +01004583 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4584 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4585 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004586}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004587
4588/*
4589 * Create a physically contiguous memory object for this object
4590 * e.g. for cursor + overlay regs
4591 */
Chris Wilson995b6762010-08-20 13:23:26 +01004592static int i915_gem_init_phys_object(struct drm_device *dev,
4593 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004594{
4595 drm_i915_private_t *dev_priv = dev->dev_private;
4596 struct drm_i915_gem_phys_object *phys_obj;
4597 int ret;
4598
4599 if (dev_priv->mm.phys_objs[id - 1] || !size)
4600 return 0;
4601
Eric Anholt9a298b22009-03-24 12:23:04 -07004602 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004603 if (!phys_obj)
4604 return -ENOMEM;
4605
4606 phys_obj->id = id;
4607
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004608 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004609 if (!phys_obj->handle) {
4610 ret = -ENOMEM;
4611 goto kfree_obj;
4612 }
4613#ifdef CONFIG_X86
4614 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4615#endif
4616
4617 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4618
4619 return 0;
4620kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004621 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004622 return ret;
4623}
4624
Chris Wilson995b6762010-08-20 13:23:26 +01004625static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004626{
4627 drm_i915_private_t *dev_priv = dev->dev_private;
4628 struct drm_i915_gem_phys_object *phys_obj;
4629
4630 if (!dev_priv->mm.phys_objs[id - 1])
4631 return;
4632
4633 phys_obj = dev_priv->mm.phys_objs[id - 1];
4634 if (phys_obj->cur_obj) {
4635 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4636 }
4637
4638#ifdef CONFIG_X86
4639 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4640#endif
4641 drm_pci_free(dev, phys_obj->handle);
4642 kfree(phys_obj);
4643 dev_priv->mm.phys_objs[id - 1] = NULL;
4644}
4645
4646void i915_gem_free_all_phys_object(struct drm_device *dev)
4647{
4648 int i;
4649
Dave Airlie260883c2009-01-22 17:58:49 +10004650 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004651 i915_gem_free_phys_object(dev, i);
4652}
4653
4654void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004655 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004656{
Al Viro496ad9a2013-01-23 17:07:38 -05004657 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004658 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004659 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004660 int page_count;
4661
Chris Wilson05394f32010-11-08 19:18:58 +00004662 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004663 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004664 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004665
Chris Wilson05394f32010-11-08 19:18:58 +00004666 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004667 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004668 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004669 if (!IS_ERR(page)) {
4670 char *dst = kmap_atomic(page);
4671 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4672 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004673
Chris Wilsone5281cc2010-10-28 13:45:36 +01004674 drm_clflush_pages(&page, 1);
4675
4676 set_page_dirty(page);
4677 mark_page_accessed(page);
4678 page_cache_release(page);
4679 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004680 }
Ben Widawskye76e9ae2012-11-04 09:21:27 -08004681 i915_gem_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004682
Chris Wilson05394f32010-11-08 19:18:58 +00004683 obj->phys_obj->cur_obj = NULL;
4684 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004685}
4686
4687int
4688i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004689 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004690 int id,
4691 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004692{
Al Viro496ad9a2013-01-23 17:07:38 -05004693 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004694 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004695 int ret = 0;
4696 int page_count;
4697 int i;
4698
4699 if (id > I915_MAX_PHYS_OBJECT)
4700 return -EINVAL;
4701
Chris Wilson05394f32010-11-08 19:18:58 +00004702 if (obj->phys_obj) {
4703 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004704 return 0;
4705 i915_gem_detach_phys_object(dev, obj);
4706 }
4707
Dave Airlie71acb5e2008-12-30 20:31:46 +10004708 /* create a new object */
4709 if (!dev_priv->mm.phys_objs[id - 1]) {
4710 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004711 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004712 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004713 DRM_ERROR("failed to init phys object %d size: %zu\n",
4714 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004715 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004716 }
4717 }
4718
4719 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004720 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4721 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004722
Chris Wilson05394f32010-11-08 19:18:58 +00004723 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004724
4725 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004726 struct page *page;
4727 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004728
Hugh Dickins5949eac2011-06-27 16:18:18 -07004729 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004730 if (IS_ERR(page))
4731 return PTR_ERR(page);
4732
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004733 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004734 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004735 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004736 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004737
4738 mark_page_accessed(page);
4739 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004740 }
4741
4742 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004743}
4744
4745static int
Chris Wilson05394f32010-11-08 19:18:58 +00004746i915_gem_phys_pwrite(struct drm_device *dev,
4747 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004748 struct drm_i915_gem_pwrite *args,
4749 struct drm_file *file_priv)
4750{
Chris Wilson05394f32010-11-08 19:18:58 +00004751 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Ville Syrjälä2bb46292013-02-22 16:12:51 +02004752 char __user *user_data = to_user_ptr(args->data_ptr);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004753
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004754 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4755 unsigned long unwritten;
4756
4757 /* The physical object once assigned is fixed for the lifetime
4758 * of the obj, so we can safely drop the lock and continue
4759 * to access vaddr.
4760 */
4761 mutex_unlock(&dev->struct_mutex);
4762 unwritten = copy_from_user(vaddr, user_data, args->size);
4763 mutex_lock(&dev->struct_mutex);
4764 if (unwritten)
4765 return -EFAULT;
4766 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004767
Ben Widawskye76e9ae2012-11-04 09:21:27 -08004768 i915_gem_chipset_flush(dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004769 return 0;
4770}
Eric Anholtb9624422009-06-03 07:27:35 +00004771
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004772void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004773{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004774 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004775
4776 /* Clean up our request list when the client is going away, so that
4777 * later retire_requests won't dereference our soon-to-be-gone
4778 * file_priv.
4779 */
Chris Wilson1c255952010-09-26 11:03:27 +01004780 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004781 while (!list_empty(&file_priv->mm.request_list)) {
4782 struct drm_i915_gem_request *request;
4783
4784 request = list_first_entry(&file_priv->mm.request_list,
4785 struct drm_i915_gem_request,
4786 client_list);
4787 list_del(&request->client_list);
4788 request->file_priv = NULL;
4789 }
Chris Wilson1c255952010-09-26 11:03:27 +01004790 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004791}
Chris Wilson31169712009-09-14 16:50:28 +01004792
Chris Wilson57745062012-11-21 13:04:04 +00004793static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
4794{
4795 if (!mutex_is_locked(mutex))
4796 return false;
4797
4798#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
4799 return mutex->owner == task;
4800#else
4801 /* Since UP may be pre-empted, we cannot assume that we own the lock */
4802 return false;
4803#endif
4804}
4805
Chris Wilson31169712009-09-14 16:50:28 +01004806static int
Ying Han1495f232011-05-24 17:12:27 -07004807i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004808{
Chris Wilson17250b72010-10-28 12:51:39 +01004809 struct drm_i915_private *dev_priv =
4810 container_of(shrinker,
4811 struct drm_i915_private,
4812 mm.inactive_shrinker);
4813 struct drm_device *dev = dev_priv->dev;
Chris Wilson6c085a72012-08-20 11:40:46 +02004814 struct drm_i915_gem_object *obj;
Ying Han1495f232011-05-24 17:12:27 -07004815 int nr_to_scan = sc->nr_to_scan;
Chris Wilson57745062012-11-21 13:04:04 +00004816 bool unlock = true;
Chris Wilson17250b72010-10-28 12:51:39 +01004817 int cnt;
4818
Chris Wilson57745062012-11-21 13:04:04 +00004819 if (!mutex_trylock(&dev->struct_mutex)) {
4820 if (!mutex_is_locked_by(&dev->struct_mutex, current))
4821 return 0;
4822
Daniel Vetter677feac2012-12-19 14:33:45 +01004823 if (dev_priv->mm.shrinker_no_lock_stealing)
4824 return 0;
4825
Chris Wilson57745062012-11-21 13:04:04 +00004826 unlock = false;
4827 }
Chris Wilson31169712009-09-14 16:50:28 +01004828
Chris Wilson6c085a72012-08-20 11:40:46 +02004829 if (nr_to_scan) {
4830 nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
4831 if (nr_to_scan > 0)
Daniel Vetter93927ca2013-01-10 18:03:00 +01004832 nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
4833 false);
4834 if (nr_to_scan > 0)
Chris Wilson6c085a72012-08-20 11:40:46 +02004835 i915_gem_shrink_all(dev_priv);
Chris Wilson31169712009-09-14 16:50:28 +01004836 }
4837
Chris Wilson17250b72010-10-28 12:51:39 +01004838 cnt = 0;
Ben Widawsky35c20a62013-05-31 11:28:48 -07004839 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
Chris Wilsona5570172012-09-04 21:02:54 +01004840 if (obj->pages_pin_count == 0)
4841 cnt += obj->base.size >> PAGE_SHIFT;
Ben Widawskyfcb4a572013-07-31 16:59:57 -07004842
4843 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
4844 if (obj->active)
4845 continue;
4846
Chris Wilsona5570172012-09-04 21:02:54 +01004847 if (obj->pin_count == 0 && obj->pages_pin_count == 0)
Chris Wilson6c085a72012-08-20 11:40:46 +02004848 cnt += obj->base.size >> PAGE_SHIFT;
Ben Widawskyfcb4a572013-07-31 16:59:57 -07004849 }
Chris Wilson31169712009-09-14 16:50:28 +01004850
Chris Wilson57745062012-11-21 13:04:04 +00004851 if (unlock)
4852 mutex_unlock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +02004853 return cnt;
Chris Wilson31169712009-09-14 16:50:28 +01004854}
Ben Widawskya70a3142013-07-31 16:59:56 -07004855
4856/* All the new VM stuff */
4857unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o,
4858 struct i915_address_space *vm)
4859{
4860 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
4861 struct i915_vma *vma;
4862
4863 if (vm == &dev_priv->mm.aliasing_ppgtt->base)
4864 vm = &dev_priv->gtt.base;
4865
4866 BUG_ON(list_empty(&o->vma_list));
4867 list_for_each_entry(vma, &o->vma_list, vma_link) {
4868 if (vma->vm == vm)
4869 return vma->node.start;
4870
4871 }
4872 return -1;
4873}
4874
4875bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
4876 struct i915_address_space *vm)
4877{
4878 struct i915_vma *vma;
4879
4880 list_for_each_entry(vma, &o->vma_list, vma_link)
Ben Widawsky8b9c2b92013-07-31 17:00:16 -07004881 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07004882 return true;
4883
4884 return false;
4885}
4886
4887bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
4888{
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01004889 struct i915_vma *vma;
Ben Widawskya70a3142013-07-31 16:59:56 -07004890
Chris Wilson5a1d5eb2013-09-10 11:27:37 +01004891 list_for_each_entry(vma, &o->vma_list, vma_link)
4892 if (drm_mm_node_allocated(&vma->node))
Ben Widawskya70a3142013-07-31 16:59:56 -07004893 return true;
4894
4895 return false;
4896}
4897
4898unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
4899 struct i915_address_space *vm)
4900{
4901 struct drm_i915_private *dev_priv = o->base.dev->dev_private;
4902 struct i915_vma *vma;
4903
4904 if (vm == &dev_priv->mm.aliasing_ppgtt->base)
4905 vm = &dev_priv->gtt.base;
4906
4907 BUG_ON(list_empty(&o->vma_list));
4908
4909 list_for_each_entry(vma, &o->vma_list, vma_link)
4910 if (vma->vm == vm)
4911 return vma->node.size;
4912
4913 return 0;
4914}