blob: ea2d83d7324ee8b2017c524d7ff1780c5e713d5b [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);
41static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
Chris Wilson88241782011-01-07 17:09:48 +000042static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
43 unsigned alignment,
Chris Wilson86a1ee22012-08-11 15:41:04 +010044 bool map_and_fenceable,
45 bool nonblocking);
Chris Wilson05394f32010-11-08 19:18:58 +000046static int i915_gem_phys_pwrite(struct drm_device *dev,
47 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +100048 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +000049 struct drm_file *file);
Eric Anholt673a3942008-07-30 12:06:12 -070050
Chris Wilson61050802012-04-17 15:31:31 +010051static void i915_gem_write_fence(struct drm_device *dev, int reg,
52 struct drm_i915_gem_object *obj);
53static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
54 struct drm_i915_fence_reg *fence,
55 bool enable);
56
Chris Wilson17250b72010-10-28 12:51:39 +010057static int i915_gem_inactive_shrink(struct shrinker *shrinker,
Ying Han1495f232011-05-24 17:12:27 -070058 struct shrink_control *sc);
Chris Wilson6c085a72012-08-20 11:40:46 +020059static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
60static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
Daniel Vetter8c599672011-12-14 13:57:31 +010061static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
Chris Wilson31169712009-09-14 16:50:28 +010062
Chris Wilson61050802012-04-17 15:31:31 +010063static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
64{
65 if (obj->tiling_mode)
66 i915_gem_release_mmap(obj);
67
68 /* As we do not have an associated fence register, we will force
69 * a tiling change if we ever need to acquire one.
70 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +010071 obj->fence_dirty = false;
Chris Wilson61050802012-04-17 15:31:31 +010072 obj->fence_reg = I915_FENCE_REG_NONE;
73}
74
Chris Wilson73aa8082010-09-30 11:46:12 +010075/* some bookkeeping */
76static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
77 size_t size)
78{
79 dev_priv->mm.object_count++;
80 dev_priv->mm.object_memory += size;
81}
82
83static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
84 size_t size)
85{
86 dev_priv->mm.object_count--;
87 dev_priv->mm.object_memory -= size;
88}
89
Chris Wilson21dd3732011-01-26 15:55:56 +000090static int
Daniel Vetter33196de2012-11-14 17:14:05 +010091i915_gem_wait_for_error(struct i915_gpu_error *error)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010092{
Chris Wilson30dbf0c2010-09-25 10:19:17 +010093 int ret;
94
Daniel Vetter7abb6902013-05-24 21:29:32 +020095#define EXIT_COND (!i915_reset_in_progress(error) || \
96 i915_terminally_wedged(error))
Daniel Vetter1f83fee2012-11-15 17:17:22 +010097 if (EXIT_COND)
Chris Wilson30dbf0c2010-09-25 10:19:17 +010098 return 0;
99
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200100 /*
101 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
102 * userspace. If it takes that long something really bad is going on and
103 * we should simply try to bail out and fail as gracefully as possible.
104 */
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100105 ret = wait_event_interruptible_timeout(error->reset_queue,
106 EXIT_COND,
107 10*HZ);
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200108 if (ret == 0) {
109 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
110 return -EIO;
111 } else if (ret < 0) {
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100112 return ret;
Daniel Vetter0a6759c2012-07-04 22:18:41 +0200113 }
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100114#undef EXIT_COND
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100115
Chris Wilson21dd3732011-01-26 15:55:56 +0000116 return 0;
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100117}
118
Chris Wilson54cf91d2010-11-25 18:00:26 +0000119int i915_mutex_lock_interruptible(struct drm_device *dev)
Chris Wilson76c1dec2010-09-25 11:22:51 +0100120{
Daniel Vetter33196de2012-11-14 17:14:05 +0100121 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson76c1dec2010-09-25 11:22:51 +0100122 int ret;
123
Daniel Vetter33196de2012-11-14 17:14:05 +0100124 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
Chris Wilson76c1dec2010-09-25 11:22:51 +0100125 if (ret)
126 return ret;
127
128 ret = mutex_lock_interruptible(&dev->struct_mutex);
129 if (ret)
130 return ret;
131
Chris Wilson23bc5982010-09-29 16:10:57 +0100132 WARN_ON(i915_verify_lists(dev));
Chris Wilson76c1dec2010-09-25 11:22:51 +0100133 return 0;
134}
Chris Wilson30dbf0c2010-09-25 10:19:17 +0100135
Chris Wilson7d1c4802010-08-07 21:45:03 +0100136static inline bool
Chris Wilson05394f32010-11-08 19:18:58 +0000137i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
Chris Wilson7d1c4802010-08-07 21:45:03 +0100138{
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700139 return i915_gem_obj_ggtt_bound(obj) && !obj->active;
Chris Wilson7d1c4802010-08-07 21:45:03 +0100140}
141
Eric Anholt673a3942008-07-30 12:06:12 -0700142int
143i915_gem_init_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000144 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700145{
Ben Widawsky93d18792013-01-17 12:45:17 -0800146 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700147 struct drm_i915_gem_init *args = data;
Chris Wilson20217462010-11-23 15:26:33 +0000148
Daniel Vetter7bb6fb82012-04-24 08:22:52 +0200149 if (drm_core_check_feature(dev, DRIVER_MODESET))
150 return -ENODEV;
151
Chris Wilson20217462010-11-23 15:26:33 +0000152 if (args->gtt_start >= args->gtt_end ||
153 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
154 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700155
Daniel Vetterf534bc02012-03-26 22:37:04 +0200156 /* GEM with user mode setting was never supported on ilk and later. */
157 if (INTEL_INFO(dev)->gen >= 5)
158 return -ENODEV;
159
Eric Anholt673a3942008-07-30 12:06:12 -0700160 mutex_lock(&dev->struct_mutex);
Ben Widawskyd7e50082012-12-18 10:31:25 -0800161 i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
162 args->gtt_end);
Ben Widawsky93d18792013-01-17 12:45:17 -0800163 dev_priv->gtt.mappable_end = args->gtt_end;
Eric Anholt673a3942008-07-30 12:06:12 -0700164 mutex_unlock(&dev->struct_mutex);
165
Chris Wilson20217462010-11-23 15:26:33 +0000166 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700167}
168
Eric Anholt5a125c32008-10-22 21:40:13 -0700169int
170i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000171 struct drm_file *file)
Eric Anholt5a125c32008-10-22 21:40:13 -0700172{
Chris Wilson73aa8082010-09-30 11:46:12 +0100173 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt5a125c32008-10-22 21:40:13 -0700174 struct drm_i915_gem_get_aperture *args = data;
Chris Wilson6299f992010-11-24 12:23:44 +0000175 struct drm_i915_gem_object *obj;
176 size_t pinned;
Eric Anholt5a125c32008-10-22 21:40:13 -0700177
Chris Wilson6299f992010-11-24 12:23:44 +0000178 pinned = 0;
Chris Wilson73aa8082010-09-30 11:46:12 +0100179 mutex_lock(&dev->struct_mutex);
Ben Widawsky35c20a62013-05-31 11:28:48 -0700180 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
Chris Wilson1b502472012-04-24 15:47:30 +0100181 if (obj->pin_count)
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700182 pinned += i915_gem_obj_ggtt_size(obj);
Chris Wilson73aa8082010-09-30 11:46:12 +0100183 mutex_unlock(&dev->struct_mutex);
Eric Anholt5a125c32008-10-22 21:40:13 -0700184
Ben Widawsky5d4545a2013-01-17 12:45:15 -0800185 args->aper_size = dev_priv->gtt.total;
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 args->aper_available_size = args->aper_size - pinned;
Chris Wilson6299f992010-11-24 12:23:44 +0000187
Eric Anholt5a125c32008-10-22 21:40:13 -0700188 return 0;
189}
190
Chris Wilson42dcedd2012-11-15 11:32:30 +0000191void *i915_gem_object_alloc(struct drm_device *dev)
192{
193 struct drm_i915_private *dev_priv = dev->dev_private;
194 return kmem_cache_alloc(dev_priv->slab, GFP_KERNEL | __GFP_ZERO);
195}
196
197void i915_gem_object_free(struct drm_i915_gem_object *obj)
198{
199 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
200 kmem_cache_free(dev_priv->slab, obj);
201}
202
Dave Airlieff72145b2011-02-07 12:16:14 +1000203static int
204i915_gem_create(struct drm_file *file,
205 struct drm_device *dev,
206 uint64_t size,
207 uint32_t *handle_p)
Eric Anholt673a3942008-07-30 12:06:12 -0700208{
Chris Wilson05394f32010-11-08 19:18:58 +0000209 struct drm_i915_gem_object *obj;
Pekka Paalanena1a2d1d2009-08-23 12:40:55 +0300210 int ret;
211 u32 handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700212
Dave Airlieff72145b2011-02-07 12:16:14 +1000213 size = roundup(size, PAGE_SIZE);
Chris Wilson8ffc0242011-09-14 14:14:28 +0200214 if (size == 0)
215 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -0700216
217 /* Allocate the new object */
Dave Airlieff72145b2011-02-07 12:16:14 +1000218 obj = i915_gem_alloc_object(dev, size);
Eric Anholt673a3942008-07-30 12:06:12 -0700219 if (obj == NULL)
220 return -ENOMEM;
221
Chris Wilson05394f32010-11-08 19:18:58 +0000222 ret = drm_gem_handle_create(file, &obj->base, &handle);
Chris Wilson1dfd9752010-09-06 14:44:14 +0100223 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +0000224 drm_gem_object_release(&obj->base);
225 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
Chris Wilson42dcedd2012-11-15 11:32:30 +0000226 i915_gem_object_free(obj);
Eric Anholt673a3942008-07-30 12:06:12 -0700227 return ret;
Chris Wilson1dfd9752010-09-06 14:44:14 +0100228 }
229
Chris Wilson202f2fe2010-10-14 13:20:40 +0100230 /* drop reference from allocate - handle holds it now */
Chris Wilson05394f32010-11-08 19:18:58 +0000231 drm_gem_object_unreference(&obj->base);
Chris Wilson202f2fe2010-10-14 13:20:40 +0100232 trace_i915_gem_object_create(obj);
233
Dave Airlieff72145b2011-02-07 12:16:14 +1000234 *handle_p = handle;
Eric Anholt673a3942008-07-30 12:06:12 -0700235 return 0;
236}
237
Dave Airlieff72145b2011-02-07 12:16:14 +1000238int
239i915_gem_dumb_create(struct drm_file *file,
240 struct drm_device *dev,
241 struct drm_mode_create_dumb *args)
242{
243 /* have to work out size/pitch and return them */
Chris Wilsoned0291f2011-03-19 08:21:45 +0000244 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
Dave Airlieff72145b2011-02-07 12:16:14 +1000245 args->size = args->pitch * args->height;
246 return i915_gem_create(file, dev,
247 args->size, &args->handle);
248}
249
Dave Airlieff72145b2011-02-07 12:16:14 +1000250/**
251 * Creates a new mm object and returns a handle to it.
252 */
253int
254i915_gem_create_ioctl(struct drm_device *dev, void *data,
255 struct drm_file *file)
256{
257 struct drm_i915_gem_create *args = data;
Daniel Vetter63ed2cb2012-04-23 16:50:50 +0200258
Dave Airlieff72145b2011-02-07 12:16:14 +1000259 return i915_gem_create(file, dev,
260 args->size, &args->handle);
261}
262
Daniel Vetter8c599672011-12-14 13:57:31 +0100263static inline int
Daniel Vetter8461d222011-12-14 13:57:32 +0100264__copy_to_user_swizzled(char __user *cpu_vaddr,
265 const char *gpu_vaddr, int gpu_offset,
266 int length)
267{
268 int ret, cpu_offset = 0;
269
270 while (length > 0) {
271 int cacheline_end = ALIGN(gpu_offset + 1, 64);
272 int this_length = min(cacheline_end - gpu_offset, length);
273 int swizzled_gpu_offset = gpu_offset ^ 64;
274
275 ret = __copy_to_user(cpu_vaddr + cpu_offset,
276 gpu_vaddr + swizzled_gpu_offset,
277 this_length);
278 if (ret)
279 return ret + length;
280
281 cpu_offset += this_length;
282 gpu_offset += this_length;
283 length -= this_length;
284 }
285
286 return 0;
287}
288
289static inline int
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700290__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
291 const char __user *cpu_vaddr,
Daniel Vetter8c599672011-12-14 13:57:31 +0100292 int length)
293{
294 int ret, cpu_offset = 0;
295
296 while (length > 0) {
297 int cacheline_end = ALIGN(gpu_offset + 1, 64);
298 int this_length = min(cacheline_end - gpu_offset, length);
299 int swizzled_gpu_offset = gpu_offset ^ 64;
300
301 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
302 cpu_vaddr + cpu_offset,
303 this_length);
304 if (ret)
305 return ret + length;
306
307 cpu_offset += this_length;
308 gpu_offset += this_length;
309 length -= this_length;
310 }
311
312 return 0;
313}
314
Daniel Vetterd174bd62012-03-25 19:47:40 +0200315/* Per-page copy function for the shmem pread fastpath.
316 * Flushes invalid cachelines before reading the target if
317 * needs_clflush is set. */
Eric Anholteb014592009-03-10 11:44:52 -0700318static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200319shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
320 char __user *user_data,
321 bool page_do_bit17_swizzling, bool needs_clflush)
322{
323 char *vaddr;
324 int ret;
325
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200326 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200327 return -EINVAL;
328
329 vaddr = kmap_atomic(page);
330 if (needs_clflush)
331 drm_clflush_virt_range(vaddr + shmem_page_offset,
332 page_length);
333 ret = __copy_to_user_inatomic(user_data,
334 vaddr + shmem_page_offset,
335 page_length);
336 kunmap_atomic(vaddr);
337
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100338 return ret ? -EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200339}
340
Daniel Vetter23c18c72012-03-25 19:47:42 +0200341static void
342shmem_clflush_swizzled_range(char *addr, unsigned long length,
343 bool swizzled)
344{
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200345 if (unlikely(swizzled)) {
Daniel Vetter23c18c72012-03-25 19:47:42 +0200346 unsigned long start = (unsigned long) addr;
347 unsigned long end = (unsigned long) addr + length;
348
349 /* For swizzling simply ensure that we always flush both
350 * channels. Lame, but simple and it works. Swizzled
351 * pwrite/pread is far from a hotpath - current userspace
352 * doesn't use it at all. */
353 start = round_down(start, 128);
354 end = round_up(end, 128);
355
356 drm_clflush_virt_range((void *)start, end - start);
357 } else {
358 drm_clflush_virt_range(addr, length);
359 }
360
361}
362
Daniel Vetterd174bd62012-03-25 19:47:40 +0200363/* Only difference to the fast-path function is that this can handle bit17
364 * and uses non-atomic copy and kmap functions. */
365static int
366shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
367 char __user *user_data,
368 bool page_do_bit17_swizzling, bool needs_clflush)
369{
370 char *vaddr;
371 int ret;
372
373 vaddr = kmap(page);
374 if (needs_clflush)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200375 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
376 page_length,
377 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200378
379 if (page_do_bit17_swizzling)
380 ret = __copy_to_user_swizzled(user_data,
381 vaddr, shmem_page_offset,
382 page_length);
383 else
384 ret = __copy_to_user(user_data,
385 vaddr + shmem_page_offset,
386 page_length);
387 kunmap(page);
388
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100389 return ret ? - EFAULT : 0;
Daniel Vetterd174bd62012-03-25 19:47:40 +0200390}
391
Eric Anholteb014592009-03-10 11:44:52 -0700392static int
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200393i915_gem_shmem_pread(struct drm_device *dev,
394 struct drm_i915_gem_object *obj,
395 struct drm_i915_gem_pread *args,
396 struct drm_file *file)
Eric Anholteb014592009-03-10 11:44:52 -0700397{
Daniel Vetter8461d222011-12-14 13:57:32 +0100398 char __user *user_data;
Eric Anholteb014592009-03-10 11:44:52 -0700399 ssize_t remain;
Daniel Vetter8461d222011-12-14 13:57:32 +0100400 loff_t offset;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100401 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8461d222011-12-14 13:57:32 +0100402 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vetter96d79b52012-03-25 19:47:36 +0200403 int prefaulted = 0;
Daniel Vetter84897312012-03-25 19:47:31 +0200404 int needs_clflush = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200405 struct sg_page_iter sg_iter;
Eric Anholteb014592009-03-10 11:44:52 -0700406
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200407 user_data = to_user_ptr(args->data_ptr);
Eric Anholteb014592009-03-10 11:44:52 -0700408 remain = args->size;
409
Daniel Vetter8461d222011-12-14 13:57:32 +0100410 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholteb014592009-03-10 11:44:52 -0700411
Daniel Vetter84897312012-03-25 19:47:31 +0200412 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
413 /* If we're not in the cpu read domain, set ourself into the gtt
414 * read domain and manually flush cachelines (if required). This
415 * optimizes for the case when the gpu will dirty the data
416 * anyway again before the next pread happens. */
417 if (obj->cache_level == I915_CACHE_NONE)
418 needs_clflush = 1;
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700419 if (i915_gem_obj_ggtt_bound(obj)) {
Chris Wilson6c085a72012-08-20 11:40:46 +0200420 ret = i915_gem_object_set_to_gtt_domain(obj, false);
421 if (ret)
422 return ret;
423 }
Daniel Vetter84897312012-03-25 19:47:31 +0200424 }
Eric Anholteb014592009-03-10 11:44:52 -0700425
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100426 ret = i915_gem_object_get_pages(obj);
427 if (ret)
428 return ret;
429
430 i915_gem_object_pin_pages(obj);
431
Eric Anholteb014592009-03-10 11:44:52 -0700432 offset = args->offset;
Daniel Vetter8461d222011-12-14 13:57:32 +0100433
Imre Deak67d5a502013-02-18 19:28:02 +0200434 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
435 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200436 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +0100437
438 if (remain <= 0)
439 break;
440
Eric Anholteb014592009-03-10 11:44:52 -0700441 /* Operation in this page
442 *
Eric Anholteb014592009-03-10 11:44:52 -0700443 * shmem_page_offset = offset within page in shmem file
Eric Anholteb014592009-03-10 11:44:52 -0700444 * page_length = bytes to copy for this page
445 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100446 shmem_page_offset = offset_in_page(offset);
Eric Anholteb014592009-03-10 11:44:52 -0700447 page_length = remain;
448 if ((shmem_page_offset + page_length) > PAGE_SIZE)
449 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholteb014592009-03-10 11:44:52 -0700450
Daniel Vetter8461d222011-12-14 13:57:32 +0100451 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
452 (page_to_phys(page) & (1 << 17)) != 0;
453
Daniel Vetterd174bd62012-03-25 19:47:40 +0200454 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
455 user_data, page_do_bit17_swizzling,
456 needs_clflush);
457 if (ret == 0)
458 goto next_page;
Eric Anholteb014592009-03-10 11:44:52 -0700459
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200460 mutex_unlock(&dev->struct_mutex);
461
Daniel Vetter96d79b52012-03-25 19:47:36 +0200462 if (!prefaulted) {
Daniel Vetterf56f8212012-03-25 19:47:41 +0200463 ret = fault_in_multipages_writeable(user_data, remain);
Daniel Vetter96d79b52012-03-25 19:47:36 +0200464 /* Userspace is tricking us, but we've already clobbered
465 * its pages with the prefault and promised to write the
466 * data up to the first fault. Hence ignore any errors
467 * and just continue. */
468 (void)ret;
469 prefaulted = 1;
470 }
471
Daniel Vetterd174bd62012-03-25 19:47:40 +0200472 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
473 user_data, page_do_bit17_swizzling,
474 needs_clflush);
Eric Anholteb014592009-03-10 11:44:52 -0700475
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200476 mutex_lock(&dev->struct_mutex);
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100477
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200478next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100479 mark_page_accessed(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100480
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100481 if (ret)
Daniel Vetter8461d222011-12-14 13:57:32 +0100482 goto out;
Daniel Vetter8461d222011-12-14 13:57:32 +0100483
Eric Anholteb014592009-03-10 11:44:52 -0700484 remain -= page_length;
Daniel Vetter8461d222011-12-14 13:57:32 +0100485 user_data += page_length;
Eric Anholteb014592009-03-10 11:44:52 -0700486 offset += page_length;
487 }
488
Chris Wilson4f27b752010-10-14 15:26:45 +0100489out:
Chris Wilsonf60d7f02012-09-04 21:02:56 +0100490 i915_gem_object_unpin_pages(obj);
491
Eric Anholteb014592009-03-10 11:44:52 -0700492 return ret;
493}
494
Eric Anholt673a3942008-07-30 12:06:12 -0700495/**
496 * Reads data from the object referenced by handle.
497 *
498 * On error, the contents of *data are undefined.
499 */
500int
501i915_gem_pread_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +0000502 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700503{
504 struct drm_i915_gem_pread *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000505 struct drm_i915_gem_object *obj;
Chris Wilson35b62a82010-09-26 20:23:38 +0100506 int ret = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700507
Chris Wilson51311d02010-11-17 09:10:42 +0000508 if (args->size == 0)
509 return 0;
510
511 if (!access_ok(VERIFY_WRITE,
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200512 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000513 args->size))
514 return -EFAULT;
515
Chris Wilson4f27b752010-10-14 15:26:45 +0100516 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100517 if (ret)
Chris Wilson4f27b752010-10-14 15:26:45 +0100518 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700519
Chris Wilson05394f32010-11-08 19:18:58 +0000520 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000521 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100522 ret = -ENOENT;
523 goto unlock;
Chris Wilson4f27b752010-10-14 15:26:45 +0100524 }
Eric Anholt673a3942008-07-30 12:06:12 -0700525
Chris Wilson7dcd2492010-09-26 20:21:44 +0100526 /* Bounds check source. */
Chris Wilson05394f32010-11-08 19:18:58 +0000527 if (args->offset > obj->base.size ||
528 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100529 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100530 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100531 }
532
Daniel Vetter1286ff72012-05-10 15:25:09 +0200533 /* prime objects have no backing filp to GEM pread/pwrite
534 * pages from.
535 */
536 if (!obj->base.filp) {
537 ret = -EINVAL;
538 goto out;
539 }
540
Chris Wilsondb53a302011-02-03 11:57:46 +0000541 trace_i915_gem_object_pread(obj, args->offset, args->size);
542
Daniel Vetterdbf7bff2012-03-25 19:47:29 +0200543 ret = i915_gem_shmem_pread(dev, obj, args, file);
Eric Anholt673a3942008-07-30 12:06:12 -0700544
Chris Wilson35b62a82010-09-26 20:23:38 +0100545out:
Chris Wilson05394f32010-11-08 19:18:58 +0000546 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100547unlock:
Chris Wilson4f27b752010-10-14 15:26:45 +0100548 mutex_unlock(&dev->struct_mutex);
Eric Anholteb014592009-03-10 11:44:52 -0700549 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700550}
551
Keith Packard0839ccb2008-10-30 19:38:48 -0700552/* This is the fast write path which cannot handle
553 * page faults in the source data
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700554 */
Linus Torvalds9b7530cc2008-10-20 14:16:43 -0700555
Keith Packard0839ccb2008-10-30 19:38:48 -0700556static inline int
557fast_user_write(struct io_mapping *mapping,
558 loff_t page_base, int page_offset,
559 char __user *user_data,
560 int length)
561{
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700562 void __iomem *vaddr_atomic;
563 void *vaddr;
Keith Packard0839ccb2008-10-30 19:38:48 -0700564 unsigned long unwritten;
565
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700566 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
Ben Widawsky4f0c7cf2012-04-16 14:07:47 -0700567 /* We can use the cpu mem copy function because this is X86. */
568 vaddr = (void __force*)vaddr_atomic + page_offset;
569 unwritten = __copy_from_user_inatomic_nocache(vaddr,
Keith Packard0839ccb2008-10-30 19:38:48 -0700570 user_data, length);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700571 io_mapping_unmap_atomic(vaddr_atomic);
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100572 return unwritten;
Keith Packard0839ccb2008-10-30 19:38:48 -0700573}
574
Eric Anholt3de09aa2009-03-09 09:42:23 -0700575/**
576 * This is the fast pwrite path, where we copy the data directly from the
577 * user into the GTT, uncached.
578 */
Eric Anholt673a3942008-07-30 12:06:12 -0700579static int
Chris Wilson05394f32010-11-08 19:18:58 +0000580i915_gem_gtt_pwrite_fast(struct drm_device *dev,
581 struct drm_i915_gem_object *obj,
Eric Anholt3de09aa2009-03-09 09:42:23 -0700582 struct drm_i915_gem_pwrite *args,
Chris Wilson05394f32010-11-08 19:18:58 +0000583 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700584{
Keith Packard0839ccb2008-10-30 19:38:48 -0700585 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -0700586 ssize_t remain;
Keith Packard0839ccb2008-10-30 19:38:48 -0700587 loff_t offset, page_base;
Eric Anholt673a3942008-07-30 12:06:12 -0700588 char __user *user_data;
Daniel Vetter935aaa62012-03-25 19:47:35 +0200589 int page_offset, page_length, ret;
590
Chris Wilson86a1ee22012-08-11 15:41:04 +0100591 ret = i915_gem_object_pin(obj, 0, true, true);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200592 if (ret)
593 goto out;
594
595 ret = i915_gem_object_set_to_gtt_domain(obj, true);
596 if (ret)
597 goto out_unpin;
598
599 ret = i915_gem_object_put_fence(obj);
600 if (ret)
601 goto out_unpin;
Eric Anholt673a3942008-07-30 12:06:12 -0700602
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200603 user_data = to_user_ptr(args->data_ptr);
Eric Anholt673a3942008-07-30 12:06:12 -0700604 remain = args->size;
Eric Anholt673a3942008-07-30 12:06:12 -0700605
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700606 offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700607
608 while (remain > 0) {
609 /* Operation in this page
610 *
Keith Packard0839ccb2008-10-30 19:38:48 -0700611 * page_base = page offset within aperture
612 * page_offset = offset within page
613 * page_length = bytes to copy for this page
Eric Anholt673a3942008-07-30 12:06:12 -0700614 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100615 page_base = offset & PAGE_MASK;
616 page_offset = offset_in_page(offset);
Keith Packard0839ccb2008-10-30 19:38:48 -0700617 page_length = remain;
618 if ((page_offset + remain) > PAGE_SIZE)
619 page_length = PAGE_SIZE - page_offset;
Eric Anholt673a3942008-07-30 12:06:12 -0700620
Keith Packard0839ccb2008-10-30 19:38:48 -0700621 /* If we get a fault while copying data, then (presumably) our
Eric Anholt3de09aa2009-03-09 09:42:23 -0700622 * source page isn't available. Return the error and we'll
623 * retry in the slow path.
Keith Packard0839ccb2008-10-30 19:38:48 -0700624 */
Ben Widawsky5d4545a2013-01-17 12:45:15 -0800625 if (fast_user_write(dev_priv->gtt.mappable, page_base,
Daniel Vetter935aaa62012-03-25 19:47:35 +0200626 page_offset, user_data, page_length)) {
627 ret = -EFAULT;
628 goto out_unpin;
629 }
Eric Anholt673a3942008-07-30 12:06:12 -0700630
Keith Packard0839ccb2008-10-30 19:38:48 -0700631 remain -= page_length;
632 user_data += page_length;
633 offset += page_length;
Eric Anholt673a3942008-07-30 12:06:12 -0700634 }
Eric Anholt673a3942008-07-30 12:06:12 -0700635
Daniel Vetter935aaa62012-03-25 19:47:35 +0200636out_unpin:
637 i915_gem_object_unpin(obj);
638out:
Eric Anholt3de09aa2009-03-09 09:42:23 -0700639 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700640}
641
Daniel Vetterd174bd62012-03-25 19:47:40 +0200642/* Per-page copy function for the shmem pwrite fastpath.
643 * Flushes invalid cachelines before writing to the target if
644 * needs_clflush_before is set and flushes out any written cachelines after
645 * writing if needs_clflush is set. */
Eric Anholt673a3942008-07-30 12:06:12 -0700646static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200647shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
648 char __user *user_data,
649 bool page_do_bit17_swizzling,
650 bool needs_clflush_before,
651 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700652{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200653 char *vaddr;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700654 int ret;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700655
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200656 if (unlikely(page_do_bit17_swizzling))
Daniel Vetterd174bd62012-03-25 19:47:40 +0200657 return -EINVAL;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700658
Daniel Vetterd174bd62012-03-25 19:47:40 +0200659 vaddr = kmap_atomic(page);
660 if (needs_clflush_before)
661 drm_clflush_virt_range(vaddr + shmem_page_offset,
662 page_length);
663 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
664 user_data,
665 page_length);
666 if (needs_clflush_after)
667 drm_clflush_virt_range(vaddr + shmem_page_offset,
668 page_length);
669 kunmap_atomic(vaddr);
Eric Anholt3de09aa2009-03-09 09:42:23 -0700670
Chris Wilson755d2212012-09-04 21:02:55 +0100671 return ret ? -EFAULT : 0;
Eric Anholt3de09aa2009-03-09 09:42:23 -0700672}
673
Daniel Vetterd174bd62012-03-25 19:47:40 +0200674/* Only difference to the fast-path function is that this can handle bit17
675 * and uses non-atomic copy and kmap functions. */
Eric Anholt3043c602008-10-02 12:24:47 -0700676static int
Daniel Vetterd174bd62012-03-25 19:47:40 +0200677shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
678 char __user *user_data,
679 bool page_do_bit17_swizzling,
680 bool needs_clflush_before,
681 bool needs_clflush_after)
Eric Anholt673a3942008-07-30 12:06:12 -0700682{
Daniel Vetterd174bd62012-03-25 19:47:40 +0200683 char *vaddr;
684 int ret;
Eric Anholt40123c12009-03-09 13:42:30 -0700685
Daniel Vetterd174bd62012-03-25 19:47:40 +0200686 vaddr = kmap(page);
Daniel Vettere7e58eb2012-03-25 19:47:43 +0200687 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
Daniel Vetter23c18c72012-03-25 19:47:42 +0200688 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
689 page_length,
690 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200691 if (page_do_bit17_swizzling)
692 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
Chris Wilsone5281cc2010-10-28 13:45:36 +0100693 user_data,
694 page_length);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200695 else
696 ret = __copy_from_user(vaddr + shmem_page_offset,
697 user_data,
698 page_length);
699 if (needs_clflush_after)
Daniel Vetter23c18c72012-03-25 19:47:42 +0200700 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
701 page_length,
702 page_do_bit17_swizzling);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200703 kunmap(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100704
Chris Wilson755d2212012-09-04 21:02:55 +0100705 return ret ? -EFAULT : 0;
Eric Anholt40123c12009-03-09 13:42:30 -0700706}
707
Eric Anholt40123c12009-03-09 13:42:30 -0700708static int
Daniel Vettere244a442012-03-25 19:47:28 +0200709i915_gem_shmem_pwrite(struct drm_device *dev,
710 struct drm_i915_gem_object *obj,
711 struct drm_i915_gem_pwrite *args,
712 struct drm_file *file)
Eric Anholt40123c12009-03-09 13:42:30 -0700713{
Eric Anholt40123c12009-03-09 13:42:30 -0700714 ssize_t remain;
Daniel Vetter8c599672011-12-14 13:57:31 +0100715 loff_t offset;
716 char __user *user_data;
Ben Widawskyeb2c0c82012-02-15 14:42:43 +0100717 int shmem_page_offset, page_length, ret = 0;
Daniel Vetter8c599672011-12-14 13:57:31 +0100718 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
Daniel Vettere244a442012-03-25 19:47:28 +0200719 int hit_slowpath = 0;
Daniel Vetter58642882012-03-25 19:47:37 +0200720 int needs_clflush_after = 0;
721 int needs_clflush_before = 0;
Imre Deak67d5a502013-02-18 19:28:02 +0200722 struct sg_page_iter sg_iter;
Eric Anholt40123c12009-03-09 13:42:30 -0700723
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200724 user_data = to_user_ptr(args->data_ptr);
Eric Anholt40123c12009-03-09 13:42:30 -0700725 remain = args->size;
726
Daniel Vetter8c599672011-12-14 13:57:31 +0100727 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
Eric Anholt40123c12009-03-09 13:42:30 -0700728
Daniel Vetter58642882012-03-25 19:47:37 +0200729 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
730 /* If we're not in the cpu write domain, set ourself into the gtt
731 * write domain and manually flush cachelines (if required). This
732 * optimizes for the case when the gpu will use the data
733 * right away and we therefore have to clflush anyway. */
734 if (obj->cache_level == I915_CACHE_NONE)
735 needs_clflush_after = 1;
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700736 if (i915_gem_obj_ggtt_bound(obj)) {
Chris Wilson6c085a72012-08-20 11:40:46 +0200737 ret = i915_gem_object_set_to_gtt_domain(obj, true);
738 if (ret)
739 return ret;
740 }
Daniel Vetter58642882012-03-25 19:47:37 +0200741 }
742 /* Same trick applies for invalidate partially written cachelines before
743 * writing. */
744 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
745 && obj->cache_level == I915_CACHE_NONE)
746 needs_clflush_before = 1;
747
Chris Wilson755d2212012-09-04 21:02:55 +0100748 ret = i915_gem_object_get_pages(obj);
749 if (ret)
750 return ret;
751
752 i915_gem_object_pin_pages(obj);
753
Eric Anholt40123c12009-03-09 13:42:30 -0700754 offset = args->offset;
Chris Wilson05394f32010-11-08 19:18:58 +0000755 obj->dirty = 1;
Eric Anholt40123c12009-03-09 13:42:30 -0700756
Imre Deak67d5a502013-02-18 19:28:02 +0200757 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
758 offset >> PAGE_SHIFT) {
Imre Deak2db76d72013-03-26 15:14:18 +0200759 struct page *page = sg_page_iter_page(&sg_iter);
Daniel Vetter58642882012-03-25 19:47:37 +0200760 int partial_cacheline_write;
Chris Wilsone5281cc2010-10-28 13:45:36 +0100761
Chris Wilson9da3da62012-06-01 15:20:22 +0100762 if (remain <= 0)
763 break;
764
Eric Anholt40123c12009-03-09 13:42:30 -0700765 /* Operation in this page
766 *
Eric Anholt40123c12009-03-09 13:42:30 -0700767 * shmem_page_offset = offset within page in shmem file
Eric Anholt40123c12009-03-09 13:42:30 -0700768 * page_length = bytes to copy for this page
769 */
Chris Wilsonc8cbbb82011-05-12 22:17:11 +0100770 shmem_page_offset = offset_in_page(offset);
Eric Anholt40123c12009-03-09 13:42:30 -0700771
772 page_length = remain;
773 if ((shmem_page_offset + page_length) > PAGE_SIZE)
774 page_length = PAGE_SIZE - shmem_page_offset;
Eric Anholt40123c12009-03-09 13:42:30 -0700775
Daniel Vetter58642882012-03-25 19:47:37 +0200776 /* If we don't overwrite a cacheline completely we need to be
777 * careful to have up-to-date data by first clflushing. Don't
778 * overcomplicate things and flush the entire patch. */
779 partial_cacheline_write = needs_clflush_before &&
780 ((shmem_page_offset | page_length)
781 & (boot_cpu_data.x86_clflush_size - 1));
782
Daniel Vetter8c599672011-12-14 13:57:31 +0100783 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
784 (page_to_phys(page) & (1 << 17)) != 0;
785
Daniel Vetterd174bd62012-03-25 19:47:40 +0200786 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
787 user_data, page_do_bit17_swizzling,
788 partial_cacheline_write,
789 needs_clflush_after);
790 if (ret == 0)
791 goto next_page;
Eric Anholt40123c12009-03-09 13:42:30 -0700792
Daniel Vettere244a442012-03-25 19:47:28 +0200793 hit_slowpath = 1;
Daniel Vettere244a442012-03-25 19:47:28 +0200794 mutex_unlock(&dev->struct_mutex);
Daniel Vetterd174bd62012-03-25 19:47:40 +0200795 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
796 user_data, page_do_bit17_swizzling,
797 partial_cacheline_write,
798 needs_clflush_after);
Eric Anholt40123c12009-03-09 13:42:30 -0700799
Daniel Vettere244a442012-03-25 19:47:28 +0200800 mutex_lock(&dev->struct_mutex);
Chris Wilson755d2212012-09-04 21:02:55 +0100801
Daniel Vettere244a442012-03-25 19:47:28 +0200802next_page:
Chris Wilsone5281cc2010-10-28 13:45:36 +0100803 set_page_dirty(page);
804 mark_page_accessed(page);
Chris Wilsone5281cc2010-10-28 13:45:36 +0100805
Chris Wilson755d2212012-09-04 21:02:55 +0100806 if (ret)
Daniel Vetter8c599672011-12-14 13:57:31 +0100807 goto out;
Daniel Vetter8c599672011-12-14 13:57:31 +0100808
Eric Anholt40123c12009-03-09 13:42:30 -0700809 remain -= page_length;
Daniel Vetter8c599672011-12-14 13:57:31 +0100810 user_data += page_length;
Eric Anholt40123c12009-03-09 13:42:30 -0700811 offset += page_length;
812 }
813
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100814out:
Chris Wilson755d2212012-09-04 21:02:55 +0100815 i915_gem_object_unpin_pages(obj);
816
Daniel Vettere244a442012-03-25 19:47:28 +0200817 if (hit_slowpath) {
Daniel Vetter8dcf0152012-11-15 16:53:58 +0100818 /*
819 * Fixup: Flush cpu caches in case we didn't flush the dirty
820 * cachelines in-line while writing and the object moved
821 * out of the cpu write domain while we've dropped the lock.
822 */
823 if (!needs_clflush_after &&
824 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Daniel Vettere244a442012-03-25 19:47:28 +0200825 i915_gem_clflush_object(obj);
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800826 i915_gem_chipset_flush(dev);
Daniel Vettere244a442012-03-25 19:47:28 +0200827 }
Daniel Vetter8c599672011-12-14 13:57:31 +0100828 }
Eric Anholt40123c12009-03-09 13:42:30 -0700829
Daniel Vetter58642882012-03-25 19:47:37 +0200830 if (needs_clflush_after)
Ben Widawskye76e9ae2012-11-04 09:21:27 -0800831 i915_gem_chipset_flush(dev);
Daniel Vetter58642882012-03-25 19:47:37 +0200832
Eric Anholt40123c12009-03-09 13:42:30 -0700833 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700834}
835
836/**
837 * Writes data to the object referenced by handle.
838 *
839 * On error, the contents of the buffer that were to be modified are undefined.
840 */
841int
842i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100843 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700844{
845 struct drm_i915_gem_pwrite *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +0000846 struct drm_i915_gem_object *obj;
Chris Wilson51311d02010-11-17 09:10:42 +0000847 int ret;
848
849 if (args->size == 0)
850 return 0;
851
852 if (!access_ok(VERIFY_READ,
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200853 to_user_ptr(args->data_ptr),
Chris Wilson51311d02010-11-17 09:10:42 +0000854 args->size))
855 return -EFAULT;
856
Ville Syrjälä2bb46292013-02-22 16:12:51 +0200857 ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
Daniel Vetterf56f8212012-03-25 19:47:41 +0200858 args->size);
Chris Wilson51311d02010-11-17 09:10:42 +0000859 if (ret)
860 return -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700861
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100862 ret = i915_mutex_lock_interruptible(dev);
863 if (ret)
864 return ret;
865
Chris Wilson05394f32010-11-08 19:18:58 +0000866 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +0000867 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100868 ret = -ENOENT;
869 goto unlock;
870 }
Eric Anholt673a3942008-07-30 12:06:12 -0700871
Chris Wilson7dcd2492010-09-26 20:21:44 +0100872 /* Bounds check destination. */
Chris Wilson05394f32010-11-08 19:18:58 +0000873 if (args->offset > obj->base.size ||
874 args->size > obj->base.size - args->offset) {
Chris Wilsonce9d4192010-09-26 20:50:05 +0100875 ret = -EINVAL;
Chris Wilson35b62a82010-09-26 20:23:38 +0100876 goto out;
Chris Wilsonce9d4192010-09-26 20:50:05 +0100877 }
878
Daniel Vetter1286ff72012-05-10 15:25:09 +0200879 /* prime objects have no backing filp to GEM pread/pwrite
880 * pages from.
881 */
882 if (!obj->base.filp) {
883 ret = -EINVAL;
884 goto out;
885 }
886
Chris Wilsondb53a302011-02-03 11:57:46 +0000887 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
888
Daniel Vetter935aaa62012-03-25 19:47:35 +0200889 ret = -EFAULT;
Eric Anholt673a3942008-07-30 12:06:12 -0700890 /* We can only do the GTT pwrite on untiled buffers, as otherwise
891 * it would end up going through the fenced access, and we'll get
892 * different detiling behavior between reading and writing.
893 * pread/pwrite currently are reading and writing from the CPU
894 * perspective, requiring manual detiling by the client.
895 */
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100896 if (obj->phys_obj) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100897 ret = i915_gem_phys_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100898 goto out;
899 }
900
Chris Wilson86a1ee22012-08-11 15:41:04 +0100901 if (obj->cache_level == I915_CACHE_NONE &&
Daniel Vetterc07496f2012-04-13 15:51:51 +0200902 obj->tiling_mode == I915_TILING_NONE &&
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100903 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100904 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
Daniel Vetter935aaa62012-03-25 19:47:35 +0200905 /* Note that the gtt paths might fail with non-page-backed user
906 * pointers (e.g. gtt mappings when moving data between
907 * textures). Fallback to the shmem path in that case. */
Eric Anholt40123c12009-03-09 13:42:30 -0700908 }
Eric Anholt673a3942008-07-30 12:06:12 -0700909
Chris Wilson86a1ee22012-08-11 15:41:04 +0100910 if (ret == -EFAULT || ret == -ENOSPC)
Daniel Vetter935aaa62012-03-25 19:47:35 +0200911 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
Daniel Vetter5c0480f2011-12-14 13:57:30 +0100912
Chris Wilson35b62a82010-09-26 20:23:38 +0100913out:
Chris Wilson05394f32010-11-08 19:18:58 +0000914 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +0100915unlock:
Chris Wilsonfbd5a262010-10-14 15:03:58 +0100916 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700917 return ret;
918}
919
Chris Wilsonb3612372012-08-24 09:35:08 +0100920int
Daniel Vetter33196de2012-11-14 17:14:05 +0100921i915_gem_check_wedge(struct i915_gpu_error *error,
Chris Wilsonb3612372012-08-24 09:35:08 +0100922 bool interruptible)
923{
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100924 if (i915_reset_in_progress(error)) {
Chris Wilsonb3612372012-08-24 09:35:08 +0100925 /* Non-interruptible callers can't handle -EAGAIN, hence return
926 * -EIO unconditionally for these. */
927 if (!interruptible)
928 return -EIO;
929
Daniel Vetter1f83fee2012-11-15 17:17:22 +0100930 /* Recovery complete, but the reset failed ... */
931 if (i915_terminally_wedged(error))
Chris Wilsonb3612372012-08-24 09:35:08 +0100932 return -EIO;
933
934 return -EAGAIN;
935 }
936
937 return 0;
938}
939
940/*
941 * Compare seqno against outstanding lazy request. Emit a request if they are
942 * equal.
943 */
944static int
945i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
946{
947 int ret;
948
949 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
950
951 ret = 0;
952 if (seqno == ring->outstanding_lazy_request)
Mika Kuoppala0025c072013-06-12 12:35:30 +0300953 ret = i915_add_request(ring, NULL);
Chris Wilsonb3612372012-08-24 09:35:08 +0100954
955 return ret;
956}
957
958/**
959 * __wait_seqno - wait until execution of seqno has finished
960 * @ring: the ring expected to report seqno
961 * @seqno: duh!
Daniel Vetterf69061b2012-12-06 09:01:42 +0100962 * @reset_counter: reset sequence associated with the given seqno
Chris Wilsonb3612372012-08-24 09:35:08 +0100963 * @interruptible: do an interruptible wait (normally yes)
964 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
965 *
Daniel Vetterf69061b2012-12-06 09:01:42 +0100966 * Note: It is of utmost importance that the passed in seqno and reset_counter
967 * values have been read by the caller in an smp safe manner. Where read-side
968 * locks are involved, it is sufficient to read the reset_counter before
969 * unlocking the lock that protects the seqno. For lockless tricks, the
970 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
971 * inserted.
972 *
Chris Wilsonb3612372012-08-24 09:35:08 +0100973 * Returns 0 if the seqno was found within the alloted time. Else returns the
974 * errno with remaining time filled in timeout argument.
975 */
976static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
Daniel Vetterf69061b2012-12-06 09:01:42 +0100977 unsigned reset_counter,
Chris Wilsonb3612372012-08-24 09:35:08 +0100978 bool interruptible, struct timespec *timeout)
979{
980 drm_i915_private_t *dev_priv = ring->dev->dev_private;
981 struct timespec before, now, wait_time={1,0};
982 unsigned long timeout_jiffies;
983 long end;
984 bool wait_forever = true;
985 int ret;
986
987 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
988 return 0;
989
990 trace_i915_gem_request_wait_begin(ring, seqno);
991
992 if (timeout != NULL) {
993 wait_time = *timeout;
994 wait_forever = false;
995 }
996
Imre Deake054cc32013-05-21 20:03:19 +0300997 timeout_jiffies = timespec_to_jiffies_timeout(&wait_time);
Chris Wilsonb3612372012-08-24 09:35:08 +0100998
999 if (WARN_ON(!ring->irq_get(ring)))
1000 return -ENODEV;
1001
1002 /* Record current time in case interrupted by signal, or wedged * */
1003 getrawmonotonic(&before);
1004
1005#define EXIT_COND \
1006 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
Daniel Vetterf69061b2012-12-06 09:01:42 +01001007 i915_reset_in_progress(&dev_priv->gpu_error) || \
1008 reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilsonb3612372012-08-24 09:35:08 +01001009 do {
1010 if (interruptible)
1011 end = wait_event_interruptible_timeout(ring->irq_queue,
1012 EXIT_COND,
1013 timeout_jiffies);
1014 else
1015 end = wait_event_timeout(ring->irq_queue, EXIT_COND,
1016 timeout_jiffies);
1017
Daniel Vetterf69061b2012-12-06 09:01:42 +01001018 /* We need to check whether any gpu reset happened in between
1019 * the caller grabbing the seqno and now ... */
1020 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
1021 end = -EAGAIN;
1022
1023 /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
1024 * gone. */
Daniel Vetter33196de2012-11-14 17:14:05 +01001025 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
Chris Wilsonb3612372012-08-24 09:35:08 +01001026 if (ret)
1027 end = ret;
1028 } while (end == 0 && wait_forever);
1029
1030 getrawmonotonic(&now);
1031
1032 ring->irq_put(ring);
1033 trace_i915_gem_request_wait_end(ring, seqno);
1034#undef EXIT_COND
1035
1036 if (timeout) {
1037 struct timespec sleep_time = timespec_sub(now, before);
1038 *timeout = timespec_sub(*timeout, sleep_time);
Chris Wilson4f42f4e2013-04-26 16:22:46 +03001039 if (!timespec_valid(timeout)) /* i.e. negative time remains */
1040 set_normalized_timespec(timeout, 0, 0);
Chris Wilsonb3612372012-08-24 09:35:08 +01001041 }
1042
1043 switch (end) {
1044 case -EIO:
1045 case -EAGAIN: /* Wedged */
1046 case -ERESTARTSYS: /* Signal */
1047 return (int)end;
1048 case 0: /* Timeout */
Chris Wilsonb3612372012-08-24 09:35:08 +01001049 return -ETIME;
1050 default: /* Completed */
1051 WARN_ON(end < 0); /* We're not aware of other errors */
1052 return 0;
1053 }
1054}
1055
1056/**
1057 * Waits for a sequence number to be signaled, and cleans up the
1058 * request and object lists appropriately for that event.
1059 */
1060int
1061i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
1062{
1063 struct drm_device *dev = ring->dev;
1064 struct drm_i915_private *dev_priv = dev->dev_private;
1065 bool interruptible = dev_priv->mm.interruptible;
1066 int ret;
1067
1068 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1069 BUG_ON(seqno == 0);
1070
Daniel Vetter33196de2012-11-14 17:14:05 +01001071 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
Chris Wilsonb3612372012-08-24 09:35:08 +01001072 if (ret)
1073 return ret;
1074
1075 ret = i915_gem_check_olr(ring, seqno);
1076 if (ret)
1077 return ret;
1078
Daniel Vetterf69061b2012-12-06 09:01:42 +01001079 return __wait_seqno(ring, seqno,
1080 atomic_read(&dev_priv->gpu_error.reset_counter),
1081 interruptible, NULL);
Chris Wilsonb3612372012-08-24 09:35:08 +01001082}
1083
Chris Wilsond26e3af2013-06-29 22:05:26 +01001084static int
1085i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
1086 struct intel_ring_buffer *ring)
1087{
1088 i915_gem_retire_requests_ring(ring);
1089
1090 /* Manually manage the write flush as we may have not yet
1091 * retired the buffer.
1092 *
1093 * Note that the last_write_seqno is always the earlier of
1094 * the two (read/write) seqno, so if we haved successfully waited,
1095 * we know we have passed the last write.
1096 */
1097 obj->last_write_seqno = 0;
1098 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1099
1100 return 0;
1101}
1102
Chris Wilsonb3612372012-08-24 09:35:08 +01001103/**
1104 * Ensures that all rendering to the object has completed and the object is
1105 * safe to unbind from the GTT or access from the CPU.
1106 */
1107static __must_check int
1108i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1109 bool readonly)
1110{
1111 struct intel_ring_buffer *ring = obj->ring;
1112 u32 seqno;
1113 int ret;
1114
1115 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1116 if (seqno == 0)
1117 return 0;
1118
1119 ret = i915_wait_seqno(ring, seqno);
1120 if (ret)
1121 return ret;
1122
Chris Wilsond26e3af2013-06-29 22:05:26 +01001123 return i915_gem_object_wait_rendering__tail(obj, ring);
Chris Wilsonb3612372012-08-24 09:35:08 +01001124}
1125
Chris Wilson3236f572012-08-24 09:35:09 +01001126/* A nonblocking variant of the above wait. This is a highly dangerous routine
1127 * as the object state may change during this call.
1128 */
1129static __must_check int
1130i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
1131 bool readonly)
1132{
1133 struct drm_device *dev = obj->base.dev;
1134 struct drm_i915_private *dev_priv = dev->dev_private;
1135 struct intel_ring_buffer *ring = obj->ring;
Daniel Vetterf69061b2012-12-06 09:01:42 +01001136 unsigned reset_counter;
Chris Wilson3236f572012-08-24 09:35:09 +01001137 u32 seqno;
1138 int ret;
1139
1140 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1141 BUG_ON(!dev_priv->mm.interruptible);
1142
1143 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1144 if (seqno == 0)
1145 return 0;
1146
Daniel Vetter33196de2012-11-14 17:14:05 +01001147 ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
Chris Wilson3236f572012-08-24 09:35:09 +01001148 if (ret)
1149 return ret;
1150
1151 ret = i915_gem_check_olr(ring, seqno);
1152 if (ret)
1153 return ret;
1154
Daniel Vetterf69061b2012-12-06 09:01:42 +01001155 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilson3236f572012-08-24 09:35:09 +01001156 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf69061b2012-12-06 09:01:42 +01001157 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
Chris Wilson3236f572012-08-24 09:35:09 +01001158 mutex_lock(&dev->struct_mutex);
Chris Wilsond26e3af2013-06-29 22:05:26 +01001159 if (ret)
1160 return ret;
Chris Wilson3236f572012-08-24 09:35:09 +01001161
Chris Wilsond26e3af2013-06-29 22:05:26 +01001162 return i915_gem_object_wait_rendering__tail(obj, ring);
Chris Wilson3236f572012-08-24 09:35:09 +01001163}
1164
Eric Anholt673a3942008-07-30 12:06:12 -07001165/**
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001166 * Called when user space prepares to use an object with the CPU, either
1167 * through the mmap ioctl's mapping or a GTT mapping.
Eric Anholt673a3942008-07-30 12:06:12 -07001168 */
1169int
1170i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001171 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001172{
1173 struct drm_i915_gem_set_domain *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001174 struct drm_i915_gem_object *obj;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001175 uint32_t read_domains = args->read_domains;
1176 uint32_t write_domain = args->write_domain;
Eric Anholt673a3942008-07-30 12:06:12 -07001177 int ret;
1178
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001179 /* Only handle setting domains to types used by the CPU. */
Chris Wilson21d509e2009-06-06 09:46:02 +01001180 if (write_domain & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001181 return -EINVAL;
1182
Chris Wilson21d509e2009-06-06 09:46:02 +01001183 if (read_domains & I915_GEM_GPU_DOMAINS)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001184 return -EINVAL;
1185
1186 /* Having something in the write domain implies it's in the read
1187 * domain, and only that read domain. Enforce that in the request.
1188 */
1189 if (write_domain != 0 && read_domains != write_domain)
1190 return -EINVAL;
1191
Chris Wilson76c1dec2010-09-25 11:22:51 +01001192 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001193 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001194 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001195
Chris Wilson05394f32010-11-08 19:18:58 +00001196 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001197 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001198 ret = -ENOENT;
1199 goto unlock;
Chris Wilson76c1dec2010-09-25 11:22:51 +01001200 }
Jesse Barnes652c3932009-08-17 13:31:43 -07001201
Chris Wilson3236f572012-08-24 09:35:09 +01001202 /* Try to flush the object off the GPU without holding the lock.
1203 * We will repeat the flush holding the lock in the normal manner
1204 * to catch cases where we are gazumped.
1205 */
1206 ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
1207 if (ret)
1208 goto unref;
1209
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001210 if (read_domains & I915_GEM_DOMAIN_GTT) {
1211 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
Eric Anholt02354392008-11-26 13:58:13 -08001212
1213 /* Silently promote "you're not bound, there was nothing to do"
1214 * to success, since the client was just asking us to
1215 * make sure everything was done.
1216 */
1217 if (ret == -EINVAL)
1218 ret = 0;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001219 } else {
Eric Anholte47c68e2008-11-14 13:35:19 -08001220 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
Eric Anholt2ef7eea2008-11-10 10:53:25 -08001221 }
1222
Chris Wilson3236f572012-08-24 09:35:09 +01001223unref:
Chris Wilson05394f32010-11-08 19:18:58 +00001224 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001225unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001226 mutex_unlock(&dev->struct_mutex);
1227 return ret;
1228}
1229
1230/**
1231 * Called when user space has done writes to this buffer
1232 */
1233int
1234i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001235 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001236{
1237 struct drm_i915_gem_sw_finish *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00001238 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001239 int ret = 0;
1240
Chris Wilson76c1dec2010-09-25 11:22:51 +01001241 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001242 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001243 return ret;
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001244
Chris Wilson05394f32010-11-08 19:18:58 +00001245 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001246 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001247 ret = -ENOENT;
1248 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07001249 }
1250
Eric Anholt673a3942008-07-30 12:06:12 -07001251 /* Pinned buffers may be scanout, so flush the cache */
Chris Wilson05394f32010-11-08 19:18:58 +00001252 if (obj->pin_count)
Eric Anholte47c68e2008-11-14 13:35:19 -08001253 i915_gem_object_flush_cpu_write_domain(obj);
1254
Chris Wilson05394f32010-11-08 19:18:58 +00001255 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001256unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07001257 mutex_unlock(&dev->struct_mutex);
1258 return ret;
1259}
1260
1261/**
1262 * Maps the contents of an object, returning the address it is mapped
1263 * into.
1264 *
1265 * While the mapping holds a reference on the contents of the object, it doesn't
1266 * imply a ref on the object itself.
1267 */
1268int
1269i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00001270 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001271{
1272 struct drm_i915_gem_mmap *args = data;
1273 struct drm_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07001274 unsigned long addr;
1275
Chris Wilson05394f32010-11-08 19:18:58 +00001276 obj = drm_gem_object_lookup(dev, file, args->handle);
Eric Anholt673a3942008-07-30 12:06:12 -07001277 if (obj == NULL)
Chris Wilsonbf79cb92010-08-04 14:19:46 +01001278 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -07001279
Daniel Vetter1286ff72012-05-10 15:25:09 +02001280 /* prime objects have no backing filp to GEM mmap
1281 * pages from.
1282 */
1283 if (!obj->filp) {
1284 drm_gem_object_unreference_unlocked(obj);
1285 return -EINVAL;
1286 }
1287
Linus Torvalds6be5ceb2012-04-20 17:13:58 -07001288 addr = vm_mmap(obj->filp, 0, args->size,
Eric Anholt673a3942008-07-30 12:06:12 -07001289 PROT_READ | PROT_WRITE, MAP_SHARED,
1290 args->offset);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001291 drm_gem_object_unreference_unlocked(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07001292 if (IS_ERR((void *)addr))
1293 return addr;
1294
1295 args->addr_ptr = (uint64_t) addr;
1296
1297 return 0;
1298}
1299
Jesse Barnesde151cf2008-11-12 10:03:55 -08001300/**
1301 * i915_gem_fault - fault a page into the GTT
1302 * vma: VMA in question
1303 * vmf: fault info
1304 *
1305 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1306 * from userspace. The fault handler takes care of binding the object to
1307 * the GTT (if needed), allocating and programming a fence register (again,
1308 * only if needed based on whether the old reg is still valid or the object
1309 * is tiled) and inserting a new PTE into the faulting process.
1310 *
1311 * Note that the faulting process may involve evicting existing objects
1312 * from the GTT and/or fence registers to make room. So performance may
1313 * suffer if the GTT working set is large or there are few fence registers
1314 * left.
1315 */
1316int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1317{
Chris Wilson05394f32010-11-08 19:18:58 +00001318 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1319 struct drm_device *dev = obj->base.dev;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001320 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001321 pgoff_t page_offset;
1322 unsigned long pfn;
1323 int ret = 0;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001324 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001325
1326 /* We don't use vmf->pgoff since that has the fake offset */
1327 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1328 PAGE_SHIFT;
1329
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001330 ret = i915_mutex_lock_interruptible(dev);
1331 if (ret)
1332 goto out;
Chris Wilsona00b10c2010-09-24 21:15:47 +01001333
Chris Wilsondb53a302011-02-03 11:57:46 +00001334 trace_i915_gem_object_fault(obj, page_offset, true, write);
1335
Chris Wilsoneb119bd2012-12-16 12:43:36 +00001336 /* Access to snoopable pages through the GTT is incoherent. */
1337 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
1338 ret = -EINVAL;
1339 goto unlock;
1340 }
1341
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001342 /* Now bind it into the GTT if needed */
Chris Wilsonc9839302012-11-20 10:45:17 +00001343 ret = i915_gem_object_pin(obj, 0, true, false);
Chris Wilsond9e86c02010-11-10 16:40:20 +00001344 if (ret)
1345 goto unlock;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001346
Chris Wilsonc9839302012-11-20 10:45:17 +00001347 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1348 if (ret)
1349 goto unpin;
1350
1351 ret = i915_gem_object_get_fence(obj);
1352 if (ret)
1353 goto unpin;
Chris Wilson7d1c4802010-08-07 21:45:03 +01001354
Chris Wilson6299f992010-11-24 12:23:44 +00001355 obj->fault_mappable = true;
1356
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001357 pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
1358 pfn >>= PAGE_SHIFT;
1359 pfn += page_offset;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001360
1361 /* Finally, remap it using the new GTT offset */
1362 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
Chris Wilsonc9839302012-11-20 10:45:17 +00001363unpin:
1364 i915_gem_object_unpin(obj);
Chris Wilsonc7150892009-09-23 00:43:56 +01001365unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001366 mutex_unlock(&dev->struct_mutex);
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001367out:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001368 switch (ret) {
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001369 case -EIO:
Daniel Vettera9340cc2012-07-04 22:18:42 +02001370 /* If this -EIO is due to a gpu hang, give the reset code a
1371 * chance to clean up the mess. Otherwise return the proper
1372 * SIGBUS. */
Daniel Vetter1f83fee2012-11-15 17:17:22 +01001373 if (i915_terminally_wedged(&dev_priv->gpu_error))
Daniel Vettera9340cc2012-07-04 22:18:42 +02001374 return VM_FAULT_SIGBUS;
Chris Wilson045e7692010-11-07 09:18:22 +00001375 case -EAGAIN:
Chris Wilsond9bc7e92011-02-07 13:09:31 +00001376 /* Give the error handler a chance to run and move the
1377 * objects off the GPU active list. Next time we service the
1378 * fault, we should be able to transition the page into the
1379 * GTT without touching the GPU (and so avoid further
1380 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1381 * with coherency, just lost writes.
1382 */
Chris Wilson045e7692010-11-07 09:18:22 +00001383 set_need_resched();
Chris Wilsonc7150892009-09-23 00:43:56 +01001384 case 0:
1385 case -ERESTARTSYS:
Chris Wilsonbed636a2011-02-11 20:31:19 +00001386 case -EINTR:
Dmitry Rogozhkine79e0fe2012-10-03 17:15:26 +03001387 case -EBUSY:
1388 /*
1389 * EBUSY is ok: this just means that another thread
1390 * already did the job.
1391 */
Chris Wilsonc7150892009-09-23 00:43:56 +01001392 return VM_FAULT_NOPAGE;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001393 case -ENOMEM:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001394 return VM_FAULT_OOM;
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001395 case -ENOSPC:
1396 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001397 default:
Daniel Vettera7c2e1a2012-10-17 11:17:16 +02001398 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
Chris Wilsonc7150892009-09-23 00:43:56 +01001399 return VM_FAULT_SIGBUS;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001400 }
1401}
1402
1403/**
Chris Wilson901782b2009-07-10 08:18:50 +01001404 * i915_gem_release_mmap - remove physical page mappings
1405 * @obj: obj in question
1406 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001407 * Preserve the reservation of the mmapping with the DRM core code, but
Chris Wilson901782b2009-07-10 08:18:50 +01001408 * relinquish ownership of the pages back to the system.
1409 *
1410 * It is vital that we remove the page mapping if we have mapped a tiled
1411 * object through the GTT and then lose the fence register due to
1412 * resource pressure. Similarly if the object has been moved out of the
1413 * aperture, than pages mapped into userspace must be revoked. Removing the
1414 * mapping will then trigger a page fault on the next user access, allowing
1415 * fixup by i915_gem_fault().
1416 */
Eric Anholtd05ca302009-07-10 13:02:26 -07001417void
Chris Wilson05394f32010-11-08 19:18:58 +00001418i915_gem_release_mmap(struct drm_i915_gem_object *obj)
Chris Wilson901782b2009-07-10 08:18:50 +01001419{
Chris Wilson6299f992010-11-24 12:23:44 +00001420 if (!obj->fault_mappable)
1421 return;
Chris Wilson901782b2009-07-10 08:18:50 +01001422
David Herrmann51335df2013-07-24 21:10:03 +02001423 drm_vma_node_unmap(&obj->base.vma_node, obj->base.dev->dev_mapping);
Chris Wilson6299f992010-11-24 12:23:44 +00001424 obj->fault_mappable = false;
Chris Wilson901782b2009-07-10 08:18:50 +01001425}
1426
Imre Deak0fa87792013-01-07 21:47:35 +02001427uint32_t
Chris Wilsone28f8712011-07-18 13:11:49 -07001428i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
Chris Wilson92b88ae2010-11-09 11:47:32 +00001429{
Chris Wilsone28f8712011-07-18 13:11:49 -07001430 uint32_t gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001431
1432 if (INTEL_INFO(dev)->gen >= 4 ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001433 tiling_mode == I915_TILING_NONE)
1434 return size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001435
1436 /* Previous chips need a power-of-two fence region when tiling */
1437 if (INTEL_INFO(dev)->gen == 3)
Chris Wilsone28f8712011-07-18 13:11:49 -07001438 gtt_size = 1024*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001439 else
Chris Wilsone28f8712011-07-18 13:11:49 -07001440 gtt_size = 512*1024;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001441
Chris Wilsone28f8712011-07-18 13:11:49 -07001442 while (gtt_size < size)
1443 gtt_size <<= 1;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001444
Chris Wilsone28f8712011-07-18 13:11:49 -07001445 return gtt_size;
Chris Wilson92b88ae2010-11-09 11:47:32 +00001446}
1447
Jesse Barnesde151cf2008-11-12 10:03:55 -08001448/**
1449 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1450 * @obj: object to check
1451 *
1452 * Return the required GTT alignment for an object, taking into account
Daniel Vetter5e783302010-11-14 22:32:36 +01001453 * potential fence register mapping.
Jesse Barnesde151cf2008-11-12 10:03:55 -08001454 */
Imre Deakd865110c2013-01-07 21:47:33 +02001455uint32_t
1456i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1457 int tiling_mode, bool fenced)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001458{
Jesse Barnesde151cf2008-11-12 10:03:55 -08001459 /*
1460 * Minimum alignment is 4k (GTT page size), but might be greater
1461 * if a fence register is needed for the object.
1462 */
Imre Deakd865110c2013-01-07 21:47:33 +02001463 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
Chris Wilsone28f8712011-07-18 13:11:49 -07001464 tiling_mode == I915_TILING_NONE)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001465 return 4096;
1466
1467 /*
1468 * Previous chips need to be aligned to the size of the smallest
1469 * fence register that can contain the object.
1470 */
Chris Wilsone28f8712011-07-18 13:11:49 -07001471 return i915_gem_get_gtt_size(dev, size, tiling_mode);
Chris Wilsona00b10c2010-09-24 21:15:47 +01001472}
1473
Chris Wilsond8cb5082012-08-11 15:41:03 +01001474static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1475{
1476 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1477 int ret;
1478
David Herrmann0de23972013-07-24 21:07:52 +02001479 if (drm_vma_node_has_offset(&obj->base.vma_node))
Chris Wilsond8cb5082012-08-11 15:41:03 +01001480 return 0;
1481
Daniel Vetterda494d72012-12-20 15:11:16 +01001482 dev_priv->mm.shrinker_no_lock_stealing = true;
1483
Chris Wilsond8cb5082012-08-11 15:41:03 +01001484 ret = drm_gem_create_mmap_offset(&obj->base);
1485 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001486 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001487
1488 /* Badly fragmented mmap space? The only way we can recover
1489 * space is by destroying unwanted objects. We can't randomly release
1490 * mmap_offsets as userspace expects them to be persistent for the
1491 * lifetime of the objects. The closest we can is to release the
1492 * offsets on purgeable objects by truncating it and marking it purged,
1493 * which prevents userspace from ever using that object again.
1494 */
1495 i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
1496 ret = drm_gem_create_mmap_offset(&obj->base);
1497 if (ret != -ENOSPC)
Daniel Vetterda494d72012-12-20 15:11:16 +01001498 goto out;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001499
1500 i915_gem_shrink_all(dev_priv);
Daniel Vetterda494d72012-12-20 15:11:16 +01001501 ret = drm_gem_create_mmap_offset(&obj->base);
1502out:
1503 dev_priv->mm.shrinker_no_lock_stealing = false;
1504
1505 return ret;
Chris Wilsond8cb5082012-08-11 15:41:03 +01001506}
1507
1508static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1509{
Chris Wilsond8cb5082012-08-11 15:41:03 +01001510 drm_gem_free_mmap_offset(&obj->base);
1511}
1512
Jesse Barnesde151cf2008-11-12 10:03:55 -08001513int
Dave Airlieff72145b2011-02-07 12:16:14 +10001514i915_gem_mmap_gtt(struct drm_file *file,
1515 struct drm_device *dev,
1516 uint32_t handle,
1517 uint64_t *offset)
Jesse Barnesde151cf2008-11-12 10:03:55 -08001518{
Chris Wilsonda761a62010-10-27 17:37:08 +01001519 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00001520 struct drm_i915_gem_object *obj;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001521 int ret;
1522
Chris Wilson76c1dec2010-09-25 11:22:51 +01001523 ret = i915_mutex_lock_interruptible(dev);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001524 if (ret)
Chris Wilson76c1dec2010-09-25 11:22:51 +01001525 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001526
Dave Airlieff72145b2011-02-07 12:16:14 +10001527 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001528 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001529 ret = -ENOENT;
1530 goto unlock;
1531 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08001532
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001533 if (obj->base.size > dev_priv->gtt.mappable_end) {
Chris Wilsonda761a62010-10-27 17:37:08 +01001534 ret = -E2BIG;
Eric Anholtff56b0b2011-10-31 23:16:21 -07001535 goto out;
Chris Wilsonda761a62010-10-27 17:37:08 +01001536 }
1537
Chris Wilson05394f32010-11-08 19:18:58 +00001538 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonab182822009-09-22 18:46:17 +01001539 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001540 ret = -EINVAL;
1541 goto out;
Chris Wilsonab182822009-09-22 18:46:17 +01001542 }
1543
Chris Wilsond8cb5082012-08-11 15:41:03 +01001544 ret = i915_gem_object_create_mmap_offset(obj);
1545 if (ret)
1546 goto out;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001547
David Herrmann0de23972013-07-24 21:07:52 +02001548 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
Jesse Barnesde151cf2008-11-12 10:03:55 -08001549
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001550out:
Chris Wilson05394f32010-11-08 19:18:58 +00001551 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001552unlock:
Jesse Barnesde151cf2008-11-12 10:03:55 -08001553 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01001554 return ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08001555}
1556
Dave Airlieff72145b2011-02-07 12:16:14 +10001557/**
1558 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1559 * @dev: DRM device
1560 * @data: GTT mapping ioctl data
1561 * @file: GEM object info
1562 *
1563 * Simply returns the fake offset to userspace so it can mmap it.
1564 * The mmap call will end up in drm_gem_mmap(), which will set things
1565 * up so we can get faults in the handler above.
1566 *
1567 * The fault handler will take care of binding the object into the GTT
1568 * (since it may have been evicted to make room for something), allocating
1569 * a fence register, and mapping the appropriate aperture address into
1570 * userspace.
1571 */
1572int
1573i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1574 struct drm_file *file)
1575{
1576 struct drm_i915_gem_mmap_gtt *args = data;
1577
Dave Airlieff72145b2011-02-07 12:16:14 +10001578 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1579}
1580
Daniel Vetter225067e2012-08-20 10:23:20 +02001581/* Immediately discard the backing storage */
1582static void
1583i915_gem_object_truncate(struct drm_i915_gem_object *obj)
Chris Wilsone5281cc2010-10-28 13:45:36 +01001584{
Chris Wilsone5281cc2010-10-28 13:45:36 +01001585 struct inode *inode;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001586
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001587 i915_gem_object_free_mmap_offset(obj);
Daniel Vetter1286ff72012-05-10 15:25:09 +02001588
Chris Wilson4d6294bf2012-08-11 15:41:05 +01001589 if (obj->base.filp == NULL)
1590 return;
1591
Daniel Vetter225067e2012-08-20 10:23:20 +02001592 /* Our goal here is to return as much of the memory as
1593 * is possible back to the system as we are called from OOM.
1594 * To do this we must instruct the shmfs to drop all of its
1595 * backing pages, *now*.
Chris Wilsone5281cc2010-10-28 13:45:36 +01001596 */
Al Viro496ad9a2013-01-23 17:07:38 -05001597 inode = file_inode(obj->base.filp);
Daniel Vetter225067e2012-08-20 10:23:20 +02001598 shmem_truncate_range(inode, 0, (loff_t)-1);
Hugh Dickins5949eac2011-06-27 16:18:18 -07001599
Daniel Vetter225067e2012-08-20 10:23:20 +02001600 obj->madv = __I915_MADV_PURGED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001601}
Chris Wilsone5281cc2010-10-28 13:45:36 +01001602
Daniel Vetter225067e2012-08-20 10:23:20 +02001603static inline int
1604i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1605{
1606 return obj->madv == I915_MADV_DONTNEED;
Chris Wilsone5281cc2010-10-28 13:45:36 +01001607}
1608
Chris Wilson5cdf5882010-09-27 15:51:07 +01001609static void
Chris Wilson05394f32010-11-08 19:18:58 +00001610i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001611{
Imre Deak90797e62013-02-18 19:28:03 +02001612 struct sg_page_iter sg_iter;
1613 int ret;
Daniel Vetter1286ff72012-05-10 15:25:09 +02001614
Chris Wilson05394f32010-11-08 19:18:58 +00001615 BUG_ON(obj->madv == __I915_MADV_PURGED);
Eric Anholt856fa192009-03-19 14:10:50 -07001616
Chris Wilson6c085a72012-08-20 11:40:46 +02001617 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1618 if (ret) {
1619 /* In the event of a disaster, abandon all caches and
1620 * hope for the best.
1621 */
1622 WARN_ON(ret != -EIO);
1623 i915_gem_clflush_object(obj);
1624 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1625 }
1626
Daniel Vetter6dacfd22011-09-12 21:30:02 +02001627 if (i915_gem_object_needs_bit17_swizzle(obj))
Eric Anholt280b7132009-03-12 16:56:27 -07001628 i915_gem_object_save_bit_17_swizzle(obj);
1629
Chris Wilson05394f32010-11-08 19:18:58 +00001630 if (obj->madv == I915_MADV_DONTNEED)
1631 obj->dirty = 0;
Chris Wilson3ef94da2009-09-14 16:50:29 +01001632
Imre Deak90797e62013-02-18 19:28:03 +02001633 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
Imre Deak2db76d72013-03-26 15:14:18 +02001634 struct page *page = sg_page_iter_page(&sg_iter);
Chris Wilson9da3da62012-06-01 15:20:22 +01001635
Chris Wilson05394f32010-11-08 19:18:58 +00001636 if (obj->dirty)
Chris Wilson9da3da62012-06-01 15:20:22 +01001637 set_page_dirty(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001638
Chris Wilson05394f32010-11-08 19:18:58 +00001639 if (obj->madv == I915_MADV_WILLNEED)
Chris Wilson9da3da62012-06-01 15:20:22 +01001640 mark_page_accessed(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001641
Chris Wilson9da3da62012-06-01 15:20:22 +01001642 page_cache_release(page);
Chris Wilson3ef94da2009-09-14 16:50:29 +01001643 }
Chris Wilson05394f32010-11-08 19:18:58 +00001644 obj->dirty = 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001645
Chris Wilson9da3da62012-06-01 15:20:22 +01001646 sg_free_table(obj->pages);
1647 kfree(obj->pages);
Chris Wilson37e680a2012-06-07 15:38:42 +01001648}
1649
Chris Wilsondd624af2013-01-15 12:39:35 +00001650int
Chris Wilson37e680a2012-06-07 15:38:42 +01001651i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
1652{
1653 const struct drm_i915_gem_object_ops *ops = obj->ops;
1654
Chris Wilson2f745ad2012-09-04 21:02:58 +01001655 if (obj->pages == NULL)
Chris Wilson37e680a2012-06-07 15:38:42 +01001656 return 0;
1657
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001658 BUG_ON(i915_gem_obj_ggtt_bound(obj));
Chris Wilson37e680a2012-06-07 15:38:42 +01001659
Chris Wilsona5570172012-09-04 21:02:54 +01001660 if (obj->pages_pin_count)
1661 return -EBUSY;
1662
Chris Wilsona2165e32012-12-03 11:49:00 +00001663 /* ->put_pages might need to allocate memory for the bit17 swizzle
1664 * array, hence protect them from being reaped by removing them from gtt
1665 * lists early. */
Ben Widawsky35c20a62013-05-31 11:28:48 -07001666 list_del(&obj->global_list);
Chris Wilsona2165e32012-12-03 11:49:00 +00001667
Chris Wilson37e680a2012-06-07 15:38:42 +01001668 ops->put_pages(obj);
Chris Wilson05394f32010-11-08 19:18:58 +00001669 obj->pages = NULL;
Chris Wilson6c085a72012-08-20 11:40:46 +02001670
Chris Wilson6c085a72012-08-20 11:40:46 +02001671 if (i915_gem_object_is_purgeable(obj))
1672 i915_gem_object_truncate(obj);
1673
1674 return 0;
1675}
1676
1677static long
Daniel Vetter93927ca2013-01-10 18:03:00 +01001678__i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
1679 bool purgeable_only)
Chris Wilson6c085a72012-08-20 11:40:46 +02001680{
1681 struct drm_i915_gem_object *obj, *next;
1682 long count = 0;
1683
1684 list_for_each_entry_safe(obj, next,
1685 &dev_priv->mm.unbound_list,
Ben Widawsky35c20a62013-05-31 11:28:48 -07001686 global_list) {
Daniel Vetter93927ca2013-01-10 18:03:00 +01001687 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
Chris Wilson37e680a2012-06-07 15:38:42 +01001688 i915_gem_object_put_pages(obj) == 0) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001689 count += obj->base.size >> PAGE_SHIFT;
1690 if (count >= target)
1691 return count;
1692 }
1693 }
1694
1695 list_for_each_entry_safe(obj, next,
1696 &dev_priv->mm.inactive_list,
1697 mm_list) {
Daniel Vetter93927ca2013-01-10 18:03:00 +01001698 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
Chris Wilson6c085a72012-08-20 11:40:46 +02001699 i915_gem_object_unbind(obj) == 0 &&
Chris Wilson37e680a2012-06-07 15:38:42 +01001700 i915_gem_object_put_pages(obj) == 0) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001701 count += obj->base.size >> PAGE_SHIFT;
1702 if (count >= target)
1703 return count;
1704 }
1705 }
1706
1707 return count;
1708}
1709
Daniel Vetter93927ca2013-01-10 18:03:00 +01001710static long
1711i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1712{
1713 return __i915_gem_shrink(dev_priv, target, true);
1714}
1715
Chris Wilson6c085a72012-08-20 11:40:46 +02001716static void
1717i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1718{
1719 struct drm_i915_gem_object *obj, *next;
1720
1721 i915_gem_evict_everything(dev_priv->dev);
1722
Ben Widawsky35c20a62013-05-31 11:28:48 -07001723 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
1724 global_list)
Chris Wilson37e680a2012-06-07 15:38:42 +01001725 i915_gem_object_put_pages(obj);
Daniel Vetter225067e2012-08-20 10:23:20 +02001726}
1727
Chris Wilson37e680a2012-06-07 15:38:42 +01001728static int
Chris Wilson6c085a72012-08-20 11:40:46 +02001729i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07001730{
Chris Wilson6c085a72012-08-20 11:40:46 +02001731 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Eric Anholt673a3942008-07-30 12:06:12 -07001732 int page_count, i;
1733 struct address_space *mapping;
Chris Wilson9da3da62012-06-01 15:20:22 +01001734 struct sg_table *st;
1735 struct scatterlist *sg;
Imre Deak90797e62013-02-18 19:28:03 +02001736 struct sg_page_iter sg_iter;
Eric Anholt673a3942008-07-30 12:06:12 -07001737 struct page *page;
Imre Deak90797e62013-02-18 19:28:03 +02001738 unsigned long last_pfn = 0; /* suppress gcc warning */
Chris Wilson6c085a72012-08-20 11:40:46 +02001739 gfp_t gfp;
Eric Anholt673a3942008-07-30 12:06:12 -07001740
Chris Wilson6c085a72012-08-20 11:40:46 +02001741 /* Assert that the object is not currently in any GPU domain. As it
1742 * wasn't in the GTT, there shouldn't be any way it could have been in
1743 * a GPU cache
1744 */
1745 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1746 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1747
Chris Wilson9da3da62012-06-01 15:20:22 +01001748 st = kmalloc(sizeof(*st), GFP_KERNEL);
1749 if (st == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07001750 return -ENOMEM;
1751
Chris Wilson9da3da62012-06-01 15:20:22 +01001752 page_count = obj->base.size / PAGE_SIZE;
1753 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
1754 sg_free_table(st);
1755 kfree(st);
1756 return -ENOMEM;
1757 }
1758
1759 /* Get the list of pages out of our struct file. They'll be pinned
1760 * at this point until we release them.
1761 *
1762 * Fail silently without starting the shrinker
1763 */
Al Viro496ad9a2013-01-23 17:07:38 -05001764 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilson6c085a72012-08-20 11:40:46 +02001765 gfp = mapping_gfp_mask(mapping);
Linus Torvaldscaf49192012-12-10 10:51:16 -08001766 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
Chris Wilson6c085a72012-08-20 11:40:46 +02001767 gfp &= ~(__GFP_IO | __GFP_WAIT);
Imre Deak90797e62013-02-18 19:28:03 +02001768 sg = st->sgl;
1769 st->nents = 0;
1770 for (i = 0; i < page_count; i++) {
Chris Wilson6c085a72012-08-20 11:40:46 +02001771 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1772 if (IS_ERR(page)) {
1773 i915_gem_purge(dev_priv, page_count);
1774 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1775 }
1776 if (IS_ERR(page)) {
1777 /* We've tried hard to allocate the memory by reaping
1778 * our own buffer, now let the real VM do its job and
1779 * go down in flames if truly OOM.
1780 */
Linus Torvaldscaf49192012-12-10 10:51:16 -08001781 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
Chris Wilson6c085a72012-08-20 11:40:46 +02001782 gfp |= __GFP_IO | __GFP_WAIT;
1783
1784 i915_gem_shrink_all(dev_priv);
1785 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1786 if (IS_ERR(page))
1787 goto err_pages;
1788
Linus Torvaldscaf49192012-12-10 10:51:16 -08001789 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
Chris Wilson6c085a72012-08-20 11:40:46 +02001790 gfp &= ~(__GFP_IO | __GFP_WAIT);
1791 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04001792#ifdef CONFIG_SWIOTLB
1793 if (swiotlb_nr_tbl()) {
1794 st->nents++;
1795 sg_set_page(sg, page, PAGE_SIZE, 0);
1796 sg = sg_next(sg);
1797 continue;
1798 }
1799#endif
Imre Deak90797e62013-02-18 19:28:03 +02001800 if (!i || page_to_pfn(page) != last_pfn + 1) {
1801 if (i)
1802 sg = sg_next(sg);
1803 st->nents++;
1804 sg_set_page(sg, page, PAGE_SIZE, 0);
1805 } else {
1806 sg->length += PAGE_SIZE;
1807 }
1808 last_pfn = page_to_pfn(page);
Eric Anholt673a3942008-07-30 12:06:12 -07001809 }
Konrad Rzeszutek Wilk426729d2013-06-24 11:47:48 -04001810#ifdef CONFIG_SWIOTLB
1811 if (!swiotlb_nr_tbl())
1812#endif
1813 sg_mark_end(sg);
Chris Wilson74ce6b62012-10-19 15:51:06 +01001814 obj->pages = st;
1815
Eric Anholt673a3942008-07-30 12:06:12 -07001816 if (i915_gem_object_needs_bit17_swizzle(obj))
1817 i915_gem_object_do_bit_17_swizzle(obj);
1818
1819 return 0;
1820
1821err_pages:
Imre Deak90797e62013-02-18 19:28:03 +02001822 sg_mark_end(sg);
1823 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
Imre Deak2db76d72013-03-26 15:14:18 +02001824 page_cache_release(sg_page_iter_page(&sg_iter));
Chris Wilson9da3da62012-06-01 15:20:22 +01001825 sg_free_table(st);
1826 kfree(st);
Eric Anholt673a3942008-07-30 12:06:12 -07001827 return PTR_ERR(page);
Eric Anholt673a3942008-07-30 12:06:12 -07001828}
1829
Chris Wilson37e680a2012-06-07 15:38:42 +01001830/* Ensure that the associated pages are gathered from the backing storage
1831 * and pinned into our object. i915_gem_object_get_pages() may be called
1832 * multiple times before they are released by a single call to
1833 * i915_gem_object_put_pages() - once the pages are no longer referenced
1834 * either as a result of memory pressure (reaping pages under the shrinker)
1835 * or as the object is itself released.
1836 */
1837int
1838i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
1839{
1840 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1841 const struct drm_i915_gem_object_ops *ops = obj->ops;
1842 int ret;
1843
Chris Wilson2f745ad2012-09-04 21:02:58 +01001844 if (obj->pages)
Chris Wilson37e680a2012-06-07 15:38:42 +01001845 return 0;
1846
Chris Wilson43e28f02013-01-08 10:53:09 +00001847 if (obj->madv != I915_MADV_WILLNEED) {
1848 DRM_ERROR("Attempting to obtain a purgeable object\n");
1849 return -EINVAL;
1850 }
1851
Chris Wilsona5570172012-09-04 21:02:54 +01001852 BUG_ON(obj->pages_pin_count);
1853
Chris Wilson37e680a2012-06-07 15:38:42 +01001854 ret = ops->get_pages(obj);
1855 if (ret)
1856 return ret;
1857
Ben Widawsky35c20a62013-05-31 11:28:48 -07001858 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Chris Wilson37e680a2012-06-07 15:38:42 +01001859 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001860}
1861
Chris Wilson54cf91d2010-11-25 18:00:26 +00001862void
Chris Wilson05394f32010-11-08 19:18:58 +00001863i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
Chris Wilson9d7730912012-11-27 16:22:52 +00001864 struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07001865{
Chris Wilson05394f32010-11-08 19:18:58 +00001866 struct drm_device *dev = obj->base.dev;
Chris Wilson69dc4982010-10-19 10:36:51 +01001867 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson9d7730912012-11-27 16:22:52 +00001868 u32 seqno = intel_ring_get_seqno(ring);
Daniel Vetter617dbe22010-02-11 22:16:02 +01001869
Zou Nan hai852835f2010-05-21 09:08:56 +08001870 BUG_ON(ring == NULL);
Chris Wilson02978ff2013-07-09 09:22:39 +01001871 if (obj->ring != ring && obj->last_write_seqno) {
1872 /* Keep the seqno relative to the current ring */
1873 obj->last_write_seqno = seqno;
1874 }
Chris Wilson05394f32010-11-08 19:18:58 +00001875 obj->ring = ring;
Eric Anholt673a3942008-07-30 12:06:12 -07001876
1877 /* Add a reference if we're newly entering the active list. */
Chris Wilson05394f32010-11-08 19:18:58 +00001878 if (!obj->active) {
1879 drm_gem_object_reference(&obj->base);
1880 obj->active = 1;
Eric Anholt673a3942008-07-30 12:06:12 -07001881 }
Daniel Vettere35a41d2010-02-11 22:13:59 +01001882
Eric Anholt673a3942008-07-30 12:06:12 -07001883 /* Move from whatever list we were on to the tail of execution. */
Chris Wilson05394f32010-11-08 19:18:58 +00001884 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1885 list_move_tail(&obj->ring_list, &ring->active_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001886
Chris Wilson0201f1e2012-07-20 12:41:01 +01001887 obj->last_read_seqno = seqno;
Chris Wilson7dd49062012-03-21 10:48:18 +00001888
Chris Wilsoncaea7472010-11-12 13:53:37 +00001889 if (obj->fenced_gpu_access) {
Chris Wilsoncaea7472010-11-12 13:53:37 +00001890 obj->last_fenced_seqno = seqno;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001891
Chris Wilson7dd49062012-03-21 10:48:18 +00001892 /* Bump MRU to take account of the delayed flush */
1893 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1894 struct drm_i915_fence_reg *reg;
1895
1896 reg = &dev_priv->fence_regs[obj->fence_reg];
1897 list_move_tail(&reg->lru_list,
1898 &dev_priv->mm.fence_list);
1899 }
Chris Wilsoncaea7472010-11-12 13:53:37 +00001900 }
1901}
1902
1903static void
Chris Wilsoncaea7472010-11-12 13:53:37 +00001904i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1905{
1906 struct drm_device *dev = obj->base.dev;
1907 struct drm_i915_private *dev_priv = dev->dev_private;
1908
Chris Wilson65ce3022012-07-20 12:41:02 +01001909 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001910 BUG_ON(!obj->active);
Chris Wilson65ce3022012-07-20 12:41:02 +01001911
Chris Wilsoncaea7472010-11-12 13:53:37 +00001912 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1913
Chris Wilson65ce3022012-07-20 12:41:02 +01001914 list_del_init(&obj->ring_list);
Chris Wilsoncaea7472010-11-12 13:53:37 +00001915 obj->ring = NULL;
1916
Chris Wilson65ce3022012-07-20 12:41:02 +01001917 obj->last_read_seqno = 0;
1918 obj->last_write_seqno = 0;
1919 obj->base.write_domain = 0;
1920
1921 obj->last_fenced_seqno = 0;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001922 obj->fenced_gpu_access = false;
Chris Wilsoncaea7472010-11-12 13:53:37 +00001923
1924 obj->active = 0;
1925 drm_gem_object_unreference(&obj->base);
1926
1927 WARN_ON(i915_verify_lists(dev));
Eric Anholtce44b0e2008-11-06 16:00:31 -08001928}
Eric Anholt673a3942008-07-30 12:06:12 -07001929
Chris Wilson9d7730912012-11-27 16:22:52 +00001930static int
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02001931i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01001932{
Chris Wilson9d7730912012-11-27 16:22:52 +00001933 struct drm_i915_private *dev_priv = dev->dev_private;
1934 struct intel_ring_buffer *ring;
1935 int ret, i, j;
Daniel Vetter53d227f2012-01-25 16:32:49 +01001936
Chris Wilson107f27a52012-12-10 13:56:17 +02001937 /* Carefully retire all requests without writing to the rings */
Chris Wilson9d7730912012-11-27 16:22:52 +00001938 for_each_ring(ring, dev_priv, i) {
Chris Wilson107f27a52012-12-10 13:56:17 +02001939 ret = intel_ring_idle(ring);
1940 if (ret)
1941 return ret;
Chris Wilson9d7730912012-11-27 16:22:52 +00001942 }
Chris Wilson9d7730912012-11-27 16:22:52 +00001943 i915_gem_retire_requests(dev);
Chris Wilson107f27a52012-12-10 13:56:17 +02001944
1945 /* Finally reset hw state */
Chris Wilson9d7730912012-11-27 16:22:52 +00001946 for_each_ring(ring, dev_priv, i) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02001947 intel_ring_init_seqno(ring, seqno);
Mika Kuoppala498d2ac2012-12-04 15:12:04 +02001948
Chris Wilson9d7730912012-11-27 16:22:52 +00001949 for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
1950 ring->sync_seqno[j] = 0;
1951 }
1952
1953 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01001954}
1955
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02001956int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
1957{
1958 struct drm_i915_private *dev_priv = dev->dev_private;
1959 int ret;
1960
1961 if (seqno == 0)
1962 return -EINVAL;
1963
1964 /* HWS page needs to be set less than what we
1965 * will inject to ring
1966 */
1967 ret = i915_gem_init_seqno(dev, seqno - 1);
1968 if (ret)
1969 return ret;
1970
1971 /* Carefully set the last_seqno value so that wrap
1972 * detection still works
1973 */
1974 dev_priv->next_seqno = seqno;
1975 dev_priv->last_seqno = seqno - 1;
1976 if (dev_priv->last_seqno == 0)
1977 dev_priv->last_seqno--;
1978
1979 return 0;
1980}
1981
Chris Wilson9d7730912012-11-27 16:22:52 +00001982int
1983i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
Daniel Vetter53d227f2012-01-25 16:32:49 +01001984{
Chris Wilson9d7730912012-11-27 16:22:52 +00001985 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter53d227f2012-01-25 16:32:49 +01001986
Chris Wilson9d7730912012-11-27 16:22:52 +00001987 /* reserve 0 for non-seqno */
1988 if (dev_priv->next_seqno == 0) {
Mika Kuoppalafca26bb2012-12-19 11:13:08 +02001989 int ret = i915_gem_init_seqno(dev, 0);
Chris Wilson9d7730912012-11-27 16:22:52 +00001990 if (ret)
1991 return ret;
1992
1993 dev_priv->next_seqno = 1;
1994 }
1995
Mika Kuoppalaf72b3432012-12-10 15:41:48 +02001996 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
Chris Wilson9d7730912012-11-27 16:22:52 +00001997 return 0;
Daniel Vetter53d227f2012-01-25 16:32:49 +01001998}
1999
Mika Kuoppala0025c072013-06-12 12:35:30 +03002000int __i915_add_request(struct intel_ring_buffer *ring,
2001 struct drm_file *file,
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002002 struct drm_i915_gem_object *obj,
Mika Kuoppala0025c072013-06-12 12:35:30 +03002003 u32 *out_seqno)
Eric Anholt673a3942008-07-30 12:06:12 -07002004{
Chris Wilsondb53a302011-02-03 11:57:46 +00002005 drm_i915_private_t *dev_priv = ring->dev->dev_private;
Chris Wilsonacb868d2012-09-26 13:47:30 +01002006 struct drm_i915_gem_request *request;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002007 u32 request_ring_position, request_start;
Eric Anholt673a3942008-07-30 12:06:12 -07002008 int was_empty;
Chris Wilson3cce4692010-10-27 16:11:02 +01002009 int ret;
2010
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002011 request_start = intel_ring_get_tail(ring);
Daniel Vettercc889e02012-06-13 20:45:19 +02002012 /*
2013 * Emit any outstanding flushes - execbuf can fail to emit the flush
2014 * after having emitted the batchbuffer command. Hence we need to fix
2015 * things up similar to emitting the lazy request. The difference here
2016 * is that the flush _must_ happen before the next request, no matter
2017 * what.
2018 */
Chris Wilsona7b97612012-07-20 12:41:08 +01002019 ret = intel_ring_flush_all_caches(ring);
2020 if (ret)
2021 return ret;
Daniel Vettercc889e02012-06-13 20:45:19 +02002022
Chris Wilsonacb868d2012-09-26 13:47:30 +01002023 request = kmalloc(sizeof(*request), GFP_KERNEL);
2024 if (request == NULL)
2025 return -ENOMEM;
Daniel Vettercc889e02012-06-13 20:45:19 +02002026
Eric Anholt673a3942008-07-30 12:06:12 -07002027
Chris Wilsona71d8d92012-02-15 11:25:36 +00002028 /* Record the position of the start of the request so that
2029 * should we detect the updated seqno part-way through the
2030 * GPU processing the request, we never over-estimate the
2031 * position of the head.
2032 */
2033 request_ring_position = intel_ring_get_tail(ring);
2034
Chris Wilson9d7730912012-11-27 16:22:52 +00002035 ret = ring->add_request(ring);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002036 if (ret) {
2037 kfree(request);
2038 return ret;
2039 }
Eric Anholt673a3942008-07-30 12:06:12 -07002040
Chris Wilson9d7730912012-11-27 16:22:52 +00002041 request->seqno = intel_ring_get_seqno(ring);
Zou Nan hai852835f2010-05-21 09:08:56 +08002042 request->ring = ring;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002043 request->head = request_start;
Chris Wilsona71d8d92012-02-15 11:25:36 +00002044 request->tail = request_ring_position;
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002045 request->ctx = ring->last_context;
Mika Kuoppala7d736f42013-06-12 15:01:39 +03002046 request->batch_obj = obj;
2047
2048 /* Whilst this request exists, batch_obj will be on the
2049 * active_list, and so will hold the active reference. Only when this
2050 * request is retired will the the batch_obj be moved onto the
2051 * inactive_list and lose its active reference. Hence we do not need
2052 * to explicitly hold another reference here.
2053 */
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002054
2055 if (request->ctx)
2056 i915_gem_context_reference(request->ctx);
2057
Eric Anholt673a3942008-07-30 12:06:12 -07002058 request->emitted_jiffies = jiffies;
Zou Nan hai852835f2010-05-21 09:08:56 +08002059 was_empty = list_empty(&ring->request_list);
2060 list_add_tail(&request->list, &ring->request_list);
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002061 request->file_priv = NULL;
Zou Nan hai852835f2010-05-21 09:08:56 +08002062
Chris Wilsondb53a302011-02-03 11:57:46 +00002063 if (file) {
2064 struct drm_i915_file_private *file_priv = file->driver_priv;
2065
Chris Wilson1c255952010-09-26 11:03:27 +01002066 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002067 request->file_priv = file_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00002068 list_add_tail(&request->client_list,
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002069 &file_priv->mm.request_list);
Chris Wilson1c255952010-09-26 11:03:27 +01002070 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00002071 }
Eric Anholt673a3942008-07-30 12:06:12 -07002072
Chris Wilson9d7730912012-11-27 16:22:52 +00002073 trace_i915_gem_request_add(ring, request->seqno);
Daniel Vetter5391d0c2012-01-25 14:03:57 +01002074 ring->outstanding_lazy_request = 0;
Chris Wilsondb53a302011-02-03 11:57:46 +00002075
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02002076 if (!dev_priv->ums.mm_suspended) {
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07002077 if (i915_enable_hangcheck) {
Daniel Vetter99584db2012-11-14 17:14:04 +01002078 mod_timer(&dev_priv->gpu_error.hangcheck_timer,
Chris Wilsoncecc21f2012-10-05 17:02:56 +01002079 round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
Ben Widawsky3e0dc6b2011-06-29 10:26:42 -07002080 }
Chris Wilsonf047e392012-07-21 12:31:41 +01002081 if (was_empty) {
Chris Wilsonb3b079d2010-09-13 23:44:34 +01002082 queue_delayed_work(dev_priv->wq,
Chris Wilsonbcb45082012-10-05 17:02:57 +01002083 &dev_priv->mm.retire_work,
2084 round_jiffies_up_relative(HZ));
Chris Wilsonf047e392012-07-21 12:31:41 +01002085 intel_mark_busy(dev_priv->dev);
2086 }
Ben Gamarif65d9422009-09-14 17:48:44 -04002087 }
Daniel Vettercc889e02012-06-13 20:45:19 +02002088
Chris Wilsonacb868d2012-09-26 13:47:30 +01002089 if (out_seqno)
Chris Wilson9d7730912012-11-27 16:22:52 +00002090 *out_seqno = request->seqno;
Chris Wilson3cce4692010-10-27 16:11:02 +01002091 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07002092}
2093
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002094static inline void
2095i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
Eric Anholt673a3942008-07-30 12:06:12 -07002096{
Chris Wilson1c255952010-09-26 11:03:27 +01002097 struct drm_i915_file_private *file_priv = request->file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002098
Chris Wilson1c255952010-09-26 11:03:27 +01002099 if (!file_priv)
2100 return;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01002101
Chris Wilson1c255952010-09-26 11:03:27 +01002102 spin_lock(&file_priv->mm.lock);
Herton Ronaldo Krzesinski09bfa512011-03-17 13:45:12 +00002103 if (request->file_priv) {
2104 list_del(&request->client_list);
2105 request->file_priv = NULL;
2106 }
Chris Wilson1c255952010-09-26 11:03:27 +01002107 spin_unlock(&file_priv->mm.lock);
Eric Anholt673a3942008-07-30 12:06:12 -07002108}
2109
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002110static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj)
2111{
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002112 if (acthd >= i915_gem_obj_ggtt_offset(obj) &&
2113 acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size)
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002114 return true;
2115
2116 return false;
2117}
2118
2119static bool i915_head_inside_request(const u32 acthd_unmasked,
2120 const u32 request_start,
2121 const u32 request_end)
2122{
2123 const u32 acthd = acthd_unmasked & HEAD_ADDR;
2124
2125 if (request_start < request_end) {
2126 if (acthd >= request_start && acthd < request_end)
2127 return true;
2128 } else if (request_start > request_end) {
2129 if (acthd >= request_start || acthd < request_end)
2130 return true;
2131 }
2132
2133 return false;
2134}
2135
2136static bool i915_request_guilty(struct drm_i915_gem_request *request,
2137 const u32 acthd, bool *inside)
2138{
2139 /* There is a possibility that unmasked head address
2140 * pointing inside the ring, matches the batch_obj address range.
2141 * However this is extremely unlikely.
2142 */
2143
2144 if (request->batch_obj) {
2145 if (i915_head_inside_object(acthd, request->batch_obj)) {
2146 *inside = true;
2147 return true;
2148 }
2149 }
2150
2151 if (i915_head_inside_request(acthd, request->head, request->tail)) {
2152 *inside = false;
2153 return true;
2154 }
2155
2156 return false;
2157}
2158
2159static void i915_set_reset_status(struct intel_ring_buffer *ring,
2160 struct drm_i915_gem_request *request,
2161 u32 acthd)
2162{
2163 struct i915_ctx_hang_stats *hs = NULL;
2164 bool inside, guilty;
2165
2166 /* Innocent until proven guilty */
2167 guilty = false;
2168
2169 if (ring->hangcheck.action != wait &&
2170 i915_request_guilty(request, acthd, &inside)) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002171 DRM_ERROR("%s hung %s bo (0x%lx ctx %d) at 0x%x\n",
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002172 ring->name,
2173 inside ? "inside" : "flushing",
2174 request->batch_obj ?
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002175 i915_gem_obj_ggtt_offset(request->batch_obj) : 0,
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002176 request->ctx ? request->ctx->id : 0,
2177 acthd);
2178
2179 guilty = true;
2180 }
2181
2182 /* If contexts are disabled or this is the default context, use
2183 * file_priv->reset_state
2184 */
2185 if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID)
2186 hs = &request->ctx->hang_stats;
2187 else if (request->file_priv)
2188 hs = &request->file_priv->hang_stats;
2189
2190 if (hs) {
2191 if (guilty)
2192 hs->batch_active++;
2193 else
2194 hs->batch_pending++;
2195 }
2196}
2197
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002198static void i915_gem_free_request(struct drm_i915_gem_request *request)
2199{
2200 list_del(&request->list);
2201 i915_gem_request_remove_from_client(request);
2202
2203 if (request->ctx)
2204 i915_gem_context_unreference(request->ctx);
2205
2206 kfree(request);
2207}
2208
Chris Wilsondfaae392010-09-22 10:31:52 +01002209static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
2210 struct intel_ring_buffer *ring)
Chris Wilson9375e442010-09-19 12:21:28 +01002211{
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002212 u32 completed_seqno;
2213 u32 acthd;
2214
2215 acthd = intel_ring_get_active_head(ring);
2216 completed_seqno = ring->get_seqno(ring, false);
2217
Chris Wilsondfaae392010-09-22 10:31:52 +01002218 while (!list_empty(&ring->request_list)) {
2219 struct drm_i915_gem_request *request;
Chris Wilson9375e442010-09-19 12:21:28 +01002220
Chris Wilsondfaae392010-09-22 10:31:52 +01002221 request = list_first_entry(&ring->request_list,
2222 struct drm_i915_gem_request,
2223 list);
2224
Mika Kuoppalaaa60c662013-06-12 15:13:20 +03002225 if (request->seqno > completed_seqno)
2226 i915_set_reset_status(ring, request, acthd);
2227
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002228 i915_gem_free_request(request);
Chris Wilsondfaae392010-09-22 10:31:52 +01002229 }
2230
2231 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002232 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07002233
Chris Wilson05394f32010-11-08 19:18:58 +00002234 obj = list_first_entry(&ring->active_list,
2235 struct drm_i915_gem_object,
2236 ring_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002237
Chris Wilson05394f32010-11-08 19:18:58 +00002238 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002239 }
Eric Anholt673a3942008-07-30 12:06:12 -07002240}
2241
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002242void i915_gem_restore_fences(struct drm_device *dev)
Chris Wilson312817a2010-11-22 11:50:11 +00002243{
2244 struct drm_i915_private *dev_priv = dev->dev_private;
2245 int i;
2246
Daniel Vetter4b9de732011-10-09 21:52:02 +02002247 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Chris Wilson312817a2010-11-22 11:50:11 +00002248 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002249 i915_gem_write_fence(dev, i, reg->obj);
Chris Wilson312817a2010-11-22 11:50:11 +00002250 }
2251}
2252
Chris Wilson069efc12010-09-30 16:53:18 +01002253void i915_gem_reset(struct drm_device *dev)
Eric Anholt673a3942008-07-30 12:06:12 -07002254{
Chris Wilsondfaae392010-09-22 10:31:52 +01002255 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002256 struct drm_i915_gem_object *obj;
Chris Wilsonb4519512012-05-11 14:29:30 +01002257 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002258 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002259
Chris Wilsonb4519512012-05-11 14:29:30 +01002260 for_each_ring(ring, dev_priv, i)
2261 i915_gem_reset_ring_lists(dev_priv, ring);
Chris Wilsondfaae392010-09-22 10:31:52 +01002262
Chris Wilsondfaae392010-09-22 10:31:52 +01002263 /* Move everything out of the GPU domains to ensure we do any
2264 * necessary invalidation upon reuse.
2265 */
Chris Wilson05394f32010-11-08 19:18:58 +00002266 list_for_each_entry(obj,
Chris Wilson77f01232010-09-19 12:31:36 +01002267 &dev_priv->mm.inactive_list,
Chris Wilson69dc4982010-10-19 10:36:51 +01002268 mm_list)
Chris Wilson77f01232010-09-19 12:31:36 +01002269 {
Chris Wilson05394f32010-11-08 19:18:58 +00002270 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilson77f01232010-09-19 12:31:36 +01002271 }
Chris Wilson069efc12010-09-30 16:53:18 +01002272
Chris Wilson19b2dbd2013-06-12 10:15:12 +01002273 i915_gem_restore_fences(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07002274}
2275
2276/**
2277 * This function clears the request list as sequence numbers are passed.
2278 */
Chris Wilsona71d8d92012-02-15 11:25:36 +00002279void
Chris Wilsondb53a302011-02-03 11:57:46 +00002280i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
Eric Anholt673a3942008-07-30 12:06:12 -07002281{
Eric Anholt673a3942008-07-30 12:06:12 -07002282 uint32_t seqno;
2283
Chris Wilsondb53a302011-02-03 11:57:46 +00002284 if (list_empty(&ring->request_list))
Karsten Wiese6c0594a2009-02-23 15:07:57 +01002285 return;
2286
Chris Wilsondb53a302011-02-03 11:57:46 +00002287 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002288
Chris Wilsonb2eadbc2012-08-09 10:58:30 +01002289 seqno = ring->get_seqno(ring, true);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002290
Zou Nan hai852835f2010-05-21 09:08:56 +08002291 while (!list_empty(&ring->request_list)) {
Eric Anholt673a3942008-07-30 12:06:12 -07002292 struct drm_i915_gem_request *request;
Eric Anholt673a3942008-07-30 12:06:12 -07002293
Zou Nan hai852835f2010-05-21 09:08:56 +08002294 request = list_first_entry(&ring->request_list,
Eric Anholt673a3942008-07-30 12:06:12 -07002295 struct drm_i915_gem_request,
2296 list);
Eric Anholt673a3942008-07-30 12:06:12 -07002297
Chris Wilsondfaae392010-09-22 10:31:52 +01002298 if (!i915_seqno_passed(seqno, request->seqno))
Eric Anholt673a3942008-07-30 12:06:12 -07002299 break;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002300
Chris Wilsondb53a302011-02-03 11:57:46 +00002301 trace_i915_gem_request_retire(ring, request->seqno);
Chris Wilsona71d8d92012-02-15 11:25:36 +00002302 /* We know the GPU must have read the request to have
2303 * sent us the seqno + interrupt, so use the position
2304 * of tail of the request to update the last known position
2305 * of the GPU head.
2306 */
2307 ring->last_retired_head = request->tail;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002308
Mika Kuoppala0e50e962013-05-02 16:48:08 +03002309 i915_gem_free_request(request);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002310 }
2311
2312 /* Move any buffers on the active list that are no longer referenced
2313 * by the ringbuffer to the flushing/inactive lists as appropriate.
2314 */
2315 while (!list_empty(&ring->active_list)) {
Chris Wilson05394f32010-11-08 19:18:58 +00002316 struct drm_i915_gem_object *obj;
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002317
Akshay Joshi0206e352011-08-16 15:34:10 -04002318 obj = list_first_entry(&ring->active_list,
Chris Wilson05394f32010-11-08 19:18:58 +00002319 struct drm_i915_gem_object,
2320 ring_list);
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002321
Chris Wilson0201f1e2012-07-20 12:41:01 +01002322 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
Chris Wilsonb84d5f02010-09-18 01:38:04 +01002323 break;
2324
Chris Wilson65ce3022012-07-20 12:41:02 +01002325 i915_gem_object_move_to_inactive(obj);
Eric Anholt673a3942008-07-30 12:06:12 -07002326 }
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002327
Chris Wilsondb53a302011-02-03 11:57:46 +00002328 if (unlikely(ring->trace_irq_seqno &&
2329 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002330 ring->irq_put(ring);
Chris Wilsondb53a302011-02-03 11:57:46 +00002331 ring->trace_irq_seqno = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002332 }
Chris Wilson23bc5982010-09-29 16:10:57 +01002333
Chris Wilsondb53a302011-02-03 11:57:46 +00002334 WARN_ON(i915_verify_lists(ring->dev));
Eric Anholt673a3942008-07-30 12:06:12 -07002335}
2336
2337void
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002338i915_gem_retire_requests(struct drm_device *dev)
2339{
2340 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002341 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002342 int i;
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002343
Chris Wilsonb4519512012-05-11 14:29:30 +01002344 for_each_ring(ring, dev_priv, i)
2345 i915_gem_retire_requests_ring(ring);
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002346}
2347
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002348static void
Eric Anholt673a3942008-07-30 12:06:12 -07002349i915_gem_retire_work_handler(struct work_struct *work)
2350{
2351 drm_i915_private_t *dev_priv;
2352 struct drm_device *dev;
Chris Wilsonb4519512012-05-11 14:29:30 +01002353 struct intel_ring_buffer *ring;
Chris Wilson0a587052011-01-09 21:05:44 +00002354 bool idle;
2355 int i;
Eric Anholt673a3942008-07-30 12:06:12 -07002356
2357 dev_priv = container_of(work, drm_i915_private_t,
2358 mm.retire_work.work);
2359 dev = dev_priv->dev;
2360
Chris Wilson891b48c2010-09-29 12:26:37 +01002361 /* Come back later if the device is busy... */
2362 if (!mutex_trylock(&dev->struct_mutex)) {
Chris Wilsonbcb45082012-10-05 17:02:57 +01002363 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2364 round_jiffies_up_relative(HZ));
Chris Wilson891b48c2010-09-29 12:26:37 +01002365 return;
2366 }
2367
Chris Wilsonb09a1fe2010-07-23 23:18:49 +01002368 i915_gem_retire_requests(dev);
Zou Nan haid1b851f2010-05-21 09:08:57 +08002369
Chris Wilson0a587052011-01-09 21:05:44 +00002370 /* Send a periodic flush down the ring so we don't hold onto GEM
2371 * objects indefinitely.
2372 */
2373 idle = true;
Chris Wilsonb4519512012-05-11 14:29:30 +01002374 for_each_ring(ring, dev_priv, i) {
Chris Wilson3bb73ab2012-07-20 12:40:59 +01002375 if (ring->gpu_caches_dirty)
Mika Kuoppala0025c072013-06-12 12:35:30 +03002376 i915_add_request(ring, NULL);
Chris Wilson0a587052011-01-09 21:05:44 +00002377
2378 idle &= list_empty(&ring->request_list);
2379 }
2380
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02002381 if (!dev_priv->ums.mm_suspended && !idle)
Chris Wilsonbcb45082012-10-05 17:02:57 +01002382 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2383 round_jiffies_up_relative(HZ));
Chris Wilsonf047e392012-07-21 12:31:41 +01002384 if (idle)
2385 intel_mark_idle(dev);
Chris Wilson0a587052011-01-09 21:05:44 +00002386
Eric Anholt673a3942008-07-30 12:06:12 -07002387 mutex_unlock(&dev->struct_mutex);
2388}
2389
Ben Widawsky5816d642012-04-11 11:18:19 -07002390/**
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002391 * Ensures that an object will eventually get non-busy by flushing any required
2392 * write domains, emitting any outstanding lazy request and retiring and
2393 * completed requests.
2394 */
2395static int
2396i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2397{
2398 int ret;
2399
2400 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002401 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002402 if (ret)
2403 return ret;
2404
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002405 i915_gem_retire_requests_ring(obj->ring);
2406 }
2407
2408 return 0;
2409}
2410
2411/**
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002412 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2413 * @DRM_IOCTL_ARGS: standard ioctl arguments
2414 *
2415 * Returns 0 if successful, else an error is returned with the remaining time in
2416 * the timeout parameter.
2417 * -ETIME: object is still busy after timeout
2418 * -ERESTARTSYS: signal interrupted the wait
2419 * -ENONENT: object doesn't exist
2420 * Also possible, but rare:
2421 * -EAGAIN: GPU wedged
2422 * -ENOMEM: damn
2423 * -ENODEV: Internal IRQ fail
2424 * -E?: The add request failed
2425 *
2426 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2427 * non-zero timeout parameter the wait ioctl will wait for the given number of
2428 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2429 * without holding struct_mutex the object may become re-busied before this
2430 * function completes. A similar but shorter * race condition exists in the busy
2431 * ioctl
2432 */
2433int
2434i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2435{
Daniel Vetterf69061b2012-12-06 09:01:42 +01002436 drm_i915_private_t *dev_priv = dev->dev_private;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002437 struct drm_i915_gem_wait *args = data;
2438 struct drm_i915_gem_object *obj;
2439 struct intel_ring_buffer *ring = NULL;
Ben Widawskyeac1f142012-06-05 15:24:24 -07002440 struct timespec timeout_stack, *timeout = NULL;
Daniel Vetterf69061b2012-12-06 09:01:42 +01002441 unsigned reset_counter;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002442 u32 seqno = 0;
2443 int ret = 0;
2444
Ben Widawskyeac1f142012-06-05 15:24:24 -07002445 if (args->timeout_ns >= 0) {
2446 timeout_stack = ns_to_timespec(args->timeout_ns);
2447 timeout = &timeout_stack;
2448 }
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002449
2450 ret = i915_mutex_lock_interruptible(dev);
2451 if (ret)
2452 return ret;
2453
2454 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2455 if (&obj->base == NULL) {
2456 mutex_unlock(&dev->struct_mutex);
2457 return -ENOENT;
2458 }
2459
Daniel Vetter30dfebf2012-06-01 15:21:23 +02002460 /* Need to make sure the object gets inactive eventually. */
2461 ret = i915_gem_object_flush_active(obj);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002462 if (ret)
2463 goto out;
2464
2465 if (obj->active) {
Chris Wilson0201f1e2012-07-20 12:41:01 +01002466 seqno = obj->last_read_seqno;
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002467 ring = obj->ring;
2468 }
2469
2470 if (seqno == 0)
2471 goto out;
2472
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002473 /* Do this after OLR check to make sure we make forward progress polling
2474 * on this IOCTL with a 0 timeout (like busy ioctl)
2475 */
2476 if (!args->timeout_ns) {
2477 ret = -ETIME;
2478 goto out;
2479 }
2480
2481 drm_gem_object_unreference(&obj->base);
Daniel Vetterf69061b2012-12-06 09:01:42 +01002482 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002483 mutex_unlock(&dev->struct_mutex);
2484
Daniel Vetterf69061b2012-12-06 09:01:42 +01002485 ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
Chris Wilson4f42f4e2013-04-26 16:22:46 +03002486 if (timeout)
Ben Widawskyeac1f142012-06-05 15:24:24 -07002487 args->timeout_ns = timespec_to_ns(timeout);
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07002488 return ret;
2489
2490out:
2491 drm_gem_object_unreference(&obj->base);
2492 mutex_unlock(&dev->struct_mutex);
2493 return ret;
2494}
2495
2496/**
Ben Widawsky5816d642012-04-11 11:18:19 -07002497 * i915_gem_object_sync - sync an object to a ring.
2498 *
2499 * @obj: object which may be in use on another ring.
2500 * @to: ring we wish to use the object on. May be NULL.
2501 *
2502 * This code is meant to abstract object synchronization with the GPU.
2503 * Calling with NULL implies synchronizing the object with the CPU
2504 * rather than a particular GPU ring.
2505 *
2506 * Returns 0 if successful, else propagates up the lower layer error.
2507 */
Ben Widawsky2911a352012-04-05 14:47:36 -07002508int
2509i915_gem_object_sync(struct drm_i915_gem_object *obj,
2510 struct intel_ring_buffer *to)
2511{
2512 struct intel_ring_buffer *from = obj->ring;
2513 u32 seqno;
2514 int ret, idx;
2515
2516 if (from == NULL || to == from)
2517 return 0;
2518
Ben Widawsky5816d642012-04-11 11:18:19 -07002519 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
Chris Wilson0201f1e2012-07-20 12:41:01 +01002520 return i915_gem_object_wait_rendering(obj, false);
Ben Widawsky2911a352012-04-05 14:47:36 -07002521
2522 idx = intel_ring_sync_index(from, to);
2523
Chris Wilson0201f1e2012-07-20 12:41:01 +01002524 seqno = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002525 if (seqno <= from->sync_seqno[idx])
2526 return 0;
2527
Ben Widawskyb4aca012012-04-25 20:50:12 -07002528 ret = i915_gem_check_olr(obj->ring, seqno);
2529 if (ret)
2530 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002531
Ben Widawsky1500f7e2012-04-11 11:18:21 -07002532 ret = to->sync_to(to, from, seqno);
Ben Widawskye3a5a222012-04-11 11:18:20 -07002533 if (!ret)
Mika Kuoppala7b01e262012-11-28 17:18:45 +02002534 /* We use last_read_seqno because sync_to()
2535 * might have just caused seqno wrap under
2536 * the radar.
2537 */
2538 from->sync_seqno[idx] = obj->last_read_seqno;
Ben Widawsky2911a352012-04-05 14:47:36 -07002539
Ben Widawskye3a5a222012-04-11 11:18:20 -07002540 return ret;
Ben Widawsky2911a352012-04-05 14:47:36 -07002541}
2542
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002543static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2544{
2545 u32 old_write_domain, old_read_domains;
2546
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002547 /* Force a pagefault for domain tracking on next user access */
2548 i915_gem_release_mmap(obj);
2549
Keith Packardb97c3d92011-06-24 21:02:59 -07002550 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2551 return;
2552
Chris Wilson97c809fd2012-10-09 19:24:38 +01002553 /* Wait for any direct GTT access to complete */
2554 mb();
2555
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002556 old_read_domains = obj->base.read_domains;
2557 old_write_domain = obj->base.write_domain;
2558
2559 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2560 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2561
2562 trace_i915_gem_object_change_domain(obj,
2563 old_read_domains,
2564 old_write_domain);
2565}
2566
Eric Anholt673a3942008-07-30 12:06:12 -07002567/**
2568 * Unbinds an object from the GTT aperture.
2569 */
Jesse Barnes0f973f22009-01-26 17:10:45 -08002570int
Chris Wilson05394f32010-11-08 19:18:58 +00002571i915_gem_object_unbind(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07002572{
Daniel Vetter7bddb012012-02-09 17:15:47 +01002573 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Chris Wilson43e28f02013-01-08 10:53:09 +00002574 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07002575
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002576 if (!i915_gem_obj_ggtt_bound(obj))
Eric Anholt673a3942008-07-30 12:06:12 -07002577 return 0;
2578
Chris Wilson31d8d652012-05-24 19:11:20 +01002579 if (obj->pin_count)
2580 return -EBUSY;
Eric Anholt673a3942008-07-30 12:06:12 -07002581
Chris Wilsonc4670ad2012-08-20 10:23:27 +01002582 BUG_ON(obj->pages == NULL);
2583
Chris Wilsona8198ee2011-04-13 22:04:09 +01002584 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002585 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07002586 return ret;
Chris Wilson8dc17752010-07-23 23:18:51 +01002587 /* Continue on if we fail due to EIO, the GPU is hung so we
2588 * should be safe and we need to cleanup or else we might
2589 * cause memory corruption through use-after-free.
2590 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002591
Chris Wilsonb5ffc9b2011-04-13 22:06:03 +01002592 i915_gem_object_finish_gtt(obj);
Chris Wilsona8198ee2011-04-13 22:04:09 +01002593
Daniel Vetter96b47b62009-12-15 17:50:00 +01002594 /* release the fence reg _after_ flushing */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002595 ret = i915_gem_object_put_fence(obj);
Chris Wilson1488fc02012-04-24 15:47:31 +01002596 if (ret)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002597 return ret;
Daniel Vetter96b47b62009-12-15 17:50:00 +01002598
Chris Wilsondb53a302011-02-03 11:57:46 +00002599 trace_i915_gem_object_unbind(obj);
2600
Daniel Vetter74898d72012-02-15 23:50:22 +01002601 if (obj->has_global_gtt_mapping)
2602 i915_gem_gtt_unbind_object(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002603 if (obj->has_aliasing_ppgtt_mapping) {
2604 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2605 obj->has_aliasing_ppgtt_mapping = 0;
2606 }
Daniel Vetter74163902012-02-15 23:50:21 +01002607 i915_gem_gtt_finish_object(obj);
Ben Widawsky401c29f2013-05-31 11:28:47 -07002608 i915_gem_object_unpin_pages(obj);
Daniel Vetter7bddb012012-02-09 17:15:47 +01002609
Chris Wilson6c085a72012-08-20 11:40:46 +02002610 list_del(&obj->mm_list);
Ben Widawsky35c20a62013-05-31 11:28:48 -07002611 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
Daniel Vetter75e9e912010-11-04 17:11:09 +01002612 /* Avoid an unnecessary call to unbind on rebind. */
Chris Wilson05394f32010-11-08 19:18:58 +00002613 obj->map_and_fenceable = true;
Eric Anholt673a3942008-07-30 12:06:12 -07002614
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002615 drm_mm_remove_node(&obj->gtt_space);
Eric Anholt673a3942008-07-30 12:06:12 -07002616
Chris Wilson88241782011-01-07 17:09:48 +00002617 return 0;
Chris Wilson54cf91d2010-11-25 18:00:26 +00002618}
2619
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002620int i915_gpu_idle(struct drm_device *dev)
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002621{
2622 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01002623 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002624 int ret, i;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002625
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002626 /* Flush everything onto the inactive list. */
Chris Wilsonb4519512012-05-11 14:29:30 +01002627 for_each_ring(ring, dev_priv, i) {
Ben Widawskyb6c74882012-08-14 14:35:14 -07002628 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2629 if (ret)
2630 return ret;
2631
Chris Wilson3e960502012-11-27 16:22:54 +00002632 ret = intel_ring_idle(ring);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002633 if (ret)
2634 return ret;
2635 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08002636
Daniel Vetter8a1a49f2010-02-11 22:29:04 +01002637 return 0;
Daniel Vetter4df2faf2010-02-19 11:52:00 +01002638}
2639
Chris Wilson9ce079e2012-04-17 15:31:30 +01002640static void i965_write_fence_reg(struct drm_device *dev, int reg,
2641 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002642{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002643 drm_i915_private_t *dev_priv = dev->dev_private;
Imre Deak56c844e2013-01-07 21:47:34 +02002644 int fence_reg;
2645 int fence_pitch_shift;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002646
Imre Deak56c844e2013-01-07 21:47:34 +02002647 if (INTEL_INFO(dev)->gen >= 6) {
2648 fence_reg = FENCE_REG_SANDYBRIDGE_0;
2649 fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
2650 } else {
2651 fence_reg = FENCE_REG_965_0;
2652 fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
2653 }
2654
Chris Wilsond18b9612013-07-10 13:36:23 +01002655 fence_reg += reg * 8;
2656
2657 /* To w/a incoherency with non-atomic 64-bit register updates,
2658 * we split the 64-bit update into two 32-bit writes. In order
2659 * for a partial fence not to be evaluated between writes, we
2660 * precede the update with write to turn off the fence register,
2661 * and only enable the fence as the last step.
2662 *
2663 * For extra levels of paranoia, we make sure each step lands
2664 * before applying the next step.
2665 */
2666 I915_WRITE(fence_reg, 0);
2667 POSTING_READ(fence_reg);
2668
Chris Wilson9ce079e2012-04-17 15:31:30 +01002669 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002670 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilsond18b9612013-07-10 13:36:23 +01002671 uint64_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002672
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002673 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
Chris Wilson9ce079e2012-04-17 15:31:30 +01002674 0xfffff000) << 32;
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002675 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
Imre Deak56c844e2013-01-07 21:47:34 +02002676 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002677 if (obj->tiling_mode == I915_TILING_Y)
2678 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2679 val |= I965_FENCE_REG_VALID;
Daniel Vetterc6642782010-11-12 13:46:18 +00002680
Chris Wilsond18b9612013-07-10 13:36:23 +01002681 I915_WRITE(fence_reg + 4, val >> 32);
2682 POSTING_READ(fence_reg + 4);
2683
2684 I915_WRITE(fence_reg + 0, val);
2685 POSTING_READ(fence_reg);
2686 } else {
2687 I915_WRITE(fence_reg + 4, 0);
2688 POSTING_READ(fence_reg + 4);
2689 }
Jesse Barnesde151cf2008-11-12 10:03:55 -08002690}
2691
Chris Wilson9ce079e2012-04-17 15:31:30 +01002692static void i915_write_fence_reg(struct drm_device *dev, int reg,
2693 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002694{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002695 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson9ce079e2012-04-17 15:31:30 +01002696 u32 val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002697
Chris Wilson9ce079e2012-04-17 15:31:30 +01002698 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002699 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002700 int pitch_val;
2701 int tile_width;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002702
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002703 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
Chris Wilson9ce079e2012-04-17 15:31:30 +01002704 (size & -size) != size ||
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002705 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
2706 "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2707 i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002708
2709 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2710 tile_width = 128;
2711 else
2712 tile_width = 512;
2713
2714 /* Note: pitch better be a power of two tile widths */
2715 pitch_val = obj->stride / tile_width;
2716 pitch_val = ffs(pitch_val) - 1;
2717
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002718 val = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002719 if (obj->tiling_mode == I915_TILING_Y)
2720 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2721 val |= I915_FENCE_SIZE_BITS(size);
2722 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2723 val |= I830_FENCE_REG_VALID;
2724 } else
2725 val = 0;
2726
2727 if (reg < 8)
2728 reg = FENCE_REG_830_0 + reg * 4;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002729 else
Chris Wilson9ce079e2012-04-17 15:31:30 +01002730 reg = FENCE_REG_945_8 + (reg - 8) * 4;
Jesse Barnes0f973f22009-01-26 17:10:45 -08002731
Chris Wilson9ce079e2012-04-17 15:31:30 +01002732 I915_WRITE(reg, val);
2733 POSTING_READ(reg);
Jesse Barnesde151cf2008-11-12 10:03:55 -08002734}
2735
Chris Wilson9ce079e2012-04-17 15:31:30 +01002736static void i830_write_fence_reg(struct drm_device *dev, int reg,
2737 struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002738{
Jesse Barnesde151cf2008-11-12 10:03:55 -08002739 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002740 uint32_t val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002741
Chris Wilson9ce079e2012-04-17 15:31:30 +01002742 if (obj) {
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002743 u32 size = i915_gem_obj_ggtt_size(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002744 uint32_t pitch_val;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002745
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002746 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
Chris Wilson9ce079e2012-04-17 15:31:30 +01002747 (size & -size) != size ||
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002748 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
2749 "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
2750 i915_gem_obj_ggtt_offset(obj), size);
Eric Anholte76a16d2009-05-26 17:44:56 -07002751
Chris Wilson9ce079e2012-04-17 15:31:30 +01002752 pitch_val = obj->stride / 128;
2753 pitch_val = ffs(pitch_val) - 1;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002754
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002755 val = i915_gem_obj_ggtt_offset(obj);
Chris Wilson9ce079e2012-04-17 15:31:30 +01002756 if (obj->tiling_mode == I915_TILING_Y)
2757 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2758 val |= I830_FENCE_SIZE_BITS(size);
2759 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2760 val |= I830_FENCE_REG_VALID;
2761 } else
2762 val = 0;
Daniel Vetterc6642782010-11-12 13:46:18 +00002763
Chris Wilson9ce079e2012-04-17 15:31:30 +01002764 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2765 POSTING_READ(FENCE_REG_830_0 + reg * 4);
2766}
2767
Chris Wilsond0a57782012-10-09 19:24:37 +01002768inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
2769{
2770 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
2771}
2772
Chris Wilson9ce079e2012-04-17 15:31:30 +01002773static void i915_gem_write_fence(struct drm_device *dev, int reg,
2774 struct drm_i915_gem_object *obj)
2775{
Chris Wilsond0a57782012-10-09 19:24:37 +01002776 struct drm_i915_private *dev_priv = dev->dev_private;
2777
2778 /* Ensure that all CPU reads are completed before installing a fence
2779 * and all writes before removing the fence.
2780 */
2781 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
2782 mb();
2783
Chris Wilson9ce079e2012-04-17 15:31:30 +01002784 switch (INTEL_INFO(dev)->gen) {
2785 case 7:
Imre Deak56c844e2013-01-07 21:47:34 +02002786 case 6:
Chris Wilson9ce079e2012-04-17 15:31:30 +01002787 case 5:
2788 case 4: i965_write_fence_reg(dev, reg, obj); break;
2789 case 3: i915_write_fence_reg(dev, reg, obj); break;
2790 case 2: i830_write_fence_reg(dev, reg, obj); break;
Ben Widawsky7dbf9d62012-12-18 10:31:22 -08002791 default: BUG();
Chris Wilson9ce079e2012-04-17 15:31:30 +01002792 }
Chris Wilsond0a57782012-10-09 19:24:37 +01002793
2794 /* And similarly be paranoid that no direct access to this region
2795 * is reordered to before the fence is installed.
2796 */
2797 if (i915_gem_object_needs_mb(obj))
2798 mb();
Jesse Barnesde151cf2008-11-12 10:03:55 -08002799}
2800
Chris Wilson61050802012-04-17 15:31:31 +01002801static inline int fence_number(struct drm_i915_private *dev_priv,
2802 struct drm_i915_fence_reg *fence)
2803{
2804 return fence - dev_priv->fence_regs;
2805}
2806
2807static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2808 struct drm_i915_fence_reg *fence,
2809 bool enable)
2810{
Chris Wilson2dc8aae2013-05-22 17:08:06 +01002811 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilson46a0b632013-07-10 13:36:24 +01002812 int reg = fence_number(dev_priv, fence);
Chris Wilson61050802012-04-17 15:31:31 +01002813
Chris Wilson46a0b632013-07-10 13:36:24 +01002814 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
Chris Wilson61050802012-04-17 15:31:31 +01002815
2816 if (enable) {
Chris Wilson46a0b632013-07-10 13:36:24 +01002817 obj->fence_reg = reg;
Chris Wilson61050802012-04-17 15:31:31 +01002818 fence->obj = obj;
2819 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2820 } else {
2821 obj->fence_reg = I915_FENCE_REG_NONE;
2822 fence->obj = NULL;
2823 list_del_init(&fence->lru_list);
2824 }
2825}
2826
Chris Wilsond9e86c02010-11-10 16:40:20 +00002827static int
Chris Wilsond0a57782012-10-09 19:24:37 +01002828i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002829{
Chris Wilson1c293ea2012-04-17 15:31:27 +01002830 if (obj->last_fenced_seqno) {
Chris Wilson86d5bc32012-07-20 12:41:04 +01002831 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
Chris Wilson18991842012-04-17 15:31:29 +01002832 if (ret)
2833 return ret;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002834
2835 obj->last_fenced_seqno = 0;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002836 }
2837
Chris Wilson86d5bc32012-07-20 12:41:04 +01002838 obj->fenced_gpu_access = false;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002839 return 0;
2840}
2841
2842int
2843i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2844{
Chris Wilson61050802012-04-17 15:31:31 +01002845 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
Chris Wilsonf9c513e2013-03-26 11:29:27 +00002846 struct drm_i915_fence_reg *fence;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002847 int ret;
2848
Chris Wilsond0a57782012-10-09 19:24:37 +01002849 ret = i915_gem_object_wait_fence(obj);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002850 if (ret)
2851 return ret;
2852
Chris Wilson61050802012-04-17 15:31:31 +01002853 if (obj->fence_reg == I915_FENCE_REG_NONE)
2854 return 0;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002855
Chris Wilsonf9c513e2013-03-26 11:29:27 +00002856 fence = &dev_priv->fence_regs[obj->fence_reg];
2857
Chris Wilson61050802012-04-17 15:31:31 +01002858 i915_gem_object_fence_lost(obj);
Chris Wilsonf9c513e2013-03-26 11:29:27 +00002859 i915_gem_object_update_fence(obj, fence, false);
Chris Wilsond9e86c02010-11-10 16:40:20 +00002860
2861 return 0;
2862}
2863
2864static struct drm_i915_fence_reg *
Chris Wilsona360bb12012-04-17 15:31:25 +01002865i915_find_fence_reg(struct drm_device *dev)
Daniel Vetterae3db242010-02-19 11:51:58 +01002866{
Daniel Vetterae3db242010-02-19 11:51:58 +01002867 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8fe301a2012-04-17 15:31:28 +01002868 struct drm_i915_fence_reg *reg, *avail;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002869 int i;
Daniel Vetterae3db242010-02-19 11:51:58 +01002870
2871 /* First try to find a free reg */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002872 avail = NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002873 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2874 reg = &dev_priv->fence_regs[i];
2875 if (!reg->obj)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002876 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002877
Chris Wilson1690e1e2011-12-14 13:57:08 +01002878 if (!reg->pin_count)
Chris Wilsond9e86c02010-11-10 16:40:20 +00002879 avail = reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002880 }
2881
Chris Wilsond9e86c02010-11-10 16:40:20 +00002882 if (avail == NULL)
2883 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002884
2885 /* None available, try to steal one or wait for a user to finish */
Chris Wilsond9e86c02010-11-10 16:40:20 +00002886 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002887 if (reg->pin_count)
Daniel Vetterae3db242010-02-19 11:51:58 +01002888 continue;
2889
Chris Wilson8fe301a2012-04-17 15:31:28 +01002890 return reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002891 }
2892
Chris Wilson8fe301a2012-04-17 15:31:28 +01002893 return NULL;
Daniel Vetterae3db242010-02-19 11:51:58 +01002894}
2895
Jesse Barnesde151cf2008-11-12 10:03:55 -08002896/**
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002897 * i915_gem_object_get_fence - set up fencing for an object
Jesse Barnesde151cf2008-11-12 10:03:55 -08002898 * @obj: object to map through a fence reg
2899 *
2900 * When mapping objects through the GTT, userspace wants to be able to write
2901 * to them without having to worry about swizzling if the object is tiled.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002902 * This function walks the fence regs looking for a free one for @obj,
2903 * stealing one if it can't find any.
2904 *
2905 * It then sets up the reg based on the object's properties: address, pitch
2906 * and tiling format.
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002907 *
2908 * For an untiled surface, this removes any existing fence.
Jesse Barnesde151cf2008-11-12 10:03:55 -08002909 */
Chris Wilson8c4b8c32009-06-17 22:08:52 +01002910int
Chris Wilson06d98132012-04-17 15:31:24 +01002911i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
Jesse Barnesde151cf2008-11-12 10:03:55 -08002912{
Chris Wilson05394f32010-11-08 19:18:58 +00002913 struct drm_device *dev = obj->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002914 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson14415742012-04-17 15:31:33 +01002915 bool enable = obj->tiling_mode != I915_TILING_NONE;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002916 struct drm_i915_fence_reg *reg;
Daniel Vetterae3db242010-02-19 11:51:58 +01002917 int ret;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002918
Chris Wilson14415742012-04-17 15:31:33 +01002919 /* Have we updated the tiling parameters upon the object and so
2920 * will need to serialise the write to the associated fence register?
2921 */
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002922 if (obj->fence_dirty) {
Chris Wilsond0a57782012-10-09 19:24:37 +01002923 ret = i915_gem_object_wait_fence(obj);
Chris Wilson14415742012-04-17 15:31:33 +01002924 if (ret)
2925 return ret;
2926 }
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002927
Chris Wilsond9e86c02010-11-10 16:40:20 +00002928 /* Just update our place in the LRU if our fence is getting reused. */
Chris Wilson05394f32010-11-08 19:18:58 +00002929 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2930 reg = &dev_priv->fence_regs[obj->fence_reg];
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002931 if (!obj->fence_dirty) {
Chris Wilson14415742012-04-17 15:31:33 +01002932 list_move_tail(&reg->lru_list,
2933 &dev_priv->mm.fence_list);
2934 return 0;
2935 }
2936 } else if (enable) {
2937 reg = i915_find_fence_reg(dev);
2938 if (reg == NULL)
2939 return -EDEADLK;
Chris Wilsond9e86c02010-11-10 16:40:20 +00002940
Chris Wilson14415742012-04-17 15:31:33 +01002941 if (reg->obj) {
2942 struct drm_i915_gem_object *old = reg->obj;
2943
Chris Wilsond0a57782012-10-09 19:24:37 +01002944 ret = i915_gem_object_wait_fence(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002945 if (ret)
2946 return ret;
2947
Chris Wilson14415742012-04-17 15:31:33 +01002948 i915_gem_object_fence_lost(old);
Chris Wilson29c5a582011-03-17 15:23:22 +00002949 }
Chris Wilson14415742012-04-17 15:31:33 +01002950 } else
Eric Anholta09ba7f2009-08-29 12:49:51 -07002951 return 0;
Eric Anholta09ba7f2009-08-29 12:49:51 -07002952
Chris Wilson14415742012-04-17 15:31:33 +01002953 i915_gem_object_update_fence(obj, reg, enable);
Chris Wilson5d82e3e2012-04-21 16:23:23 +01002954 obj->fence_dirty = false;
Chris Wilson14415742012-04-17 15:31:33 +01002955
Chris Wilson9ce079e2012-04-17 15:31:30 +01002956 return 0;
Jesse Barnesde151cf2008-11-12 10:03:55 -08002957}
2958
Chris Wilson42d6ab42012-07-26 11:49:32 +01002959static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2960 struct drm_mm_node *gtt_space,
2961 unsigned long cache_level)
2962{
2963 struct drm_mm_node *other;
2964
2965 /* On non-LLC machines we have to be careful when putting differing
2966 * types of snoopable memory together to avoid the prefetcher
Damien Lespiau4239ca72012-12-03 16:26:16 +00002967 * crossing memory domains and dying.
Chris Wilson42d6ab42012-07-26 11:49:32 +01002968 */
2969 if (HAS_LLC(dev))
2970 return true;
2971
Ben Widawskyc6cfb322013-07-05 14:41:06 -07002972 if (!drm_mm_node_allocated(gtt_space))
Chris Wilson42d6ab42012-07-26 11:49:32 +01002973 return true;
2974
2975 if (list_empty(&gtt_space->node_list))
2976 return true;
2977
2978 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2979 if (other->allocated && !other->hole_follows && other->color != cache_level)
2980 return false;
2981
2982 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
2983 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
2984 return false;
2985
2986 return true;
2987}
2988
2989static void i915_gem_verify_gtt(struct drm_device *dev)
2990{
2991#if WATCH_GTT
2992 struct drm_i915_private *dev_priv = dev->dev_private;
2993 struct drm_i915_gem_object *obj;
2994 int err = 0;
2995
Ben Widawsky35c20a62013-05-31 11:28:48 -07002996 list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
Chris Wilson42d6ab42012-07-26 11:49:32 +01002997 if (obj->gtt_space == NULL) {
2998 printk(KERN_ERR "object found on GTT list with no space reserved\n");
2999 err++;
3000 continue;
3001 }
3002
3003 if (obj->cache_level != obj->gtt_space->color) {
3004 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003005 i915_gem_obj_ggtt_offset(obj),
3006 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
Chris Wilson42d6ab42012-07-26 11:49:32 +01003007 obj->cache_level,
3008 obj->gtt_space->color);
3009 err++;
3010 continue;
3011 }
3012
3013 if (!i915_gem_valid_gtt_space(dev,
3014 obj->gtt_space,
3015 obj->cache_level)) {
3016 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003017 i915_gem_obj_ggtt_offset(obj),
3018 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
Chris Wilson42d6ab42012-07-26 11:49:32 +01003019 obj->cache_level);
3020 err++;
3021 continue;
3022 }
3023 }
3024
3025 WARN_ON(err);
3026#endif
3027}
3028
Jesse Barnesde151cf2008-11-12 10:03:55 -08003029/**
Eric Anholt673a3942008-07-30 12:06:12 -07003030 * Finds free space in the GTT aperture and binds the object there.
3031 */
3032static int
Chris Wilson05394f32010-11-08 19:18:58 +00003033i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
Daniel Vetter920afa72010-09-16 17:54:23 +02003034 unsigned alignment,
Chris Wilson86a1ee22012-08-11 15:41:04 +01003035 bool map_and_fenceable,
3036 bool nonblocking)
Eric Anholt673a3942008-07-30 12:06:12 -07003037{
Chris Wilson05394f32010-11-08 19:18:58 +00003038 struct drm_device *dev = obj->base.dev;
Eric Anholt673a3942008-07-30 12:06:12 -07003039 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter5e783302010-11-14 22:32:36 +01003040 u32 size, fence_size, fence_alignment, unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003041 bool mappable, fenceable;
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003042 size_t gtt_max = map_and_fenceable ?
3043 dev_priv->gtt.mappable_end : dev_priv->gtt.total;
Chris Wilson07f73f62009-09-14 16:50:30 +01003044 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003045
Chris Wilsone28f8712011-07-18 13:11:49 -07003046 fence_size = i915_gem_get_gtt_size(dev,
3047 obj->base.size,
3048 obj->tiling_mode);
3049 fence_alignment = i915_gem_get_gtt_alignment(dev,
3050 obj->base.size,
Imre Deakd865110c2013-01-07 21:47:33 +02003051 obj->tiling_mode, true);
Chris Wilsone28f8712011-07-18 13:11:49 -07003052 unfenced_alignment =
Imre Deakd865110c2013-01-07 21:47:33 +02003053 i915_gem_get_gtt_alignment(dev,
Chris Wilsone28f8712011-07-18 13:11:49 -07003054 obj->base.size,
Imre Deakd865110c2013-01-07 21:47:33 +02003055 obj->tiling_mode, false);
Chris Wilsona00b10c2010-09-24 21:15:47 +01003056
Eric Anholt673a3942008-07-30 12:06:12 -07003057 if (alignment == 0)
Daniel Vetter5e783302010-11-14 22:32:36 +01003058 alignment = map_and_fenceable ? fence_alignment :
3059 unfenced_alignment;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003060 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
Eric Anholt673a3942008-07-30 12:06:12 -07003061 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
3062 return -EINVAL;
3063 }
3064
Chris Wilson05394f32010-11-08 19:18:58 +00003065 size = map_and_fenceable ? fence_size : obj->base.size;
Chris Wilsona00b10c2010-09-24 21:15:47 +01003066
Chris Wilson654fc602010-05-27 13:18:21 +01003067 /* If the object is bigger than the entire aperture, reject it early
3068 * before evicting everything in a vain attempt to find space.
3069 */
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003070 if (obj->base.size > gtt_max) {
Jani Nikula3765f302013-06-07 16:03:50 +03003071 DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
Chris Wilsona36689c2013-05-21 16:58:49 +01003072 obj->base.size,
3073 map_and_fenceable ? "mappable" : "total",
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003074 gtt_max);
Chris Wilson654fc602010-05-27 13:18:21 +01003075 return -E2BIG;
3076 }
3077
Chris Wilson37e680a2012-06-07 15:38:42 +01003078 ret = i915_gem_object_get_pages(obj);
Chris Wilson6c085a72012-08-20 11:40:46 +02003079 if (ret)
3080 return ret;
3081
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003082 i915_gem_object_pin_pages(obj);
3083
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003084search_free:
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003085 ret = drm_mm_insert_node_in_range_generic(&dev_priv->mm.gtt_space,
3086 &obj->gtt_space,
Ben Widawsky0a9ae0d2013-05-25 12:26:35 -07003087 size, alignment,
David Herrmann31e5d7c2013-07-27 13:36:27 +02003088 obj->cache_level, 0, gtt_max,
3089 DRM_MM_SEARCH_DEFAULT);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003090 if (ret) {
Daniel Vetter75e9e912010-11-04 17:11:09 +01003091 ret = i915_gem_evict_something(dev, size, alignment,
Chris Wilson42d6ab42012-07-26 11:49:32 +01003092 obj->cache_level,
Chris Wilson86a1ee22012-08-11 15:41:04 +01003093 map_and_fenceable,
3094 nonblocking);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003095 if (ret == 0)
3096 goto search_free;
Chris Wilson97311292009-09-21 00:22:34 +01003097
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003098 i915_gem_object_unpin_pages(obj);
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003099 return ret;
3100 }
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003101 if (WARN_ON(!i915_gem_valid_gtt_space(dev, &obj->gtt_space,
3102 obj->cache_level))) {
Chris Wilsondc9dd7a2012-12-07 20:37:07 +00003103 i915_gem_object_unpin_pages(obj);
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003104 drm_mm_remove_node(&obj->gtt_space);
Chris Wilson42d6ab42012-07-26 11:49:32 +01003105 return -EINVAL;
Eric Anholt673a3942008-07-30 12:06:12 -07003106 }
3107
Daniel Vetter74163902012-02-15 23:50:21 +01003108 ret = i915_gem_gtt_prepare_object(obj);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +01003109 if (ret) {
Chris Wilsonfbdda6f2012-11-20 10:45:16 +00003110 i915_gem_object_unpin_pages(obj);
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003111 drm_mm_remove_node(&obj->gtt_space);
Chris Wilson6c085a72012-08-20 11:40:46 +02003112 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003113 }
Eric Anholt673a3942008-07-30 12:06:12 -07003114
Ben Widawsky35c20a62013-05-31 11:28:48 -07003115 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
Chris Wilson05394f32010-11-08 19:18:58 +00003116 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
Chris Wilsonbf1a1092010-08-07 11:01:20 +01003117
Daniel Vetter75e9e912010-11-04 17:11:09 +01003118 fenceable =
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003119 i915_gem_obj_ggtt_size(obj) == fence_size &&
3120 (i915_gem_obj_ggtt_offset(obj) & (fence_alignment - 1)) == 0;
Chris Wilsona00b10c2010-09-24 21:15:47 +01003121
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003122 mappable = i915_gem_obj_ggtt_offset(obj) + obj->base.size <=
3123 dev_priv->gtt.mappable_end;
Chris Wilsona00b10c2010-09-24 21:15:47 +01003124
Chris Wilson05394f32010-11-08 19:18:58 +00003125 obj->map_and_fenceable = mappable && fenceable;
Daniel Vetter75e9e912010-11-04 17:11:09 +01003126
Chris Wilsondb53a302011-02-03 11:57:46 +00003127 trace_i915_gem_object_bind(obj, map_and_fenceable);
Chris Wilson42d6ab42012-07-26 11:49:32 +01003128 i915_gem_verify_gtt(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003129 return 0;
3130}
3131
3132void
Chris Wilson05394f32010-11-08 19:18:58 +00003133i915_gem_clflush_object(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003134{
Eric Anholt673a3942008-07-30 12:06:12 -07003135 /* If we don't have a page list set up, then we're not pinned
3136 * to GPU, and we can ignore the cache flush because it'll happen
3137 * again at bind time.
3138 */
Chris Wilson05394f32010-11-08 19:18:58 +00003139 if (obj->pages == NULL)
Eric Anholt673a3942008-07-30 12:06:12 -07003140 return;
3141
Imre Deak769ce462013-02-13 21:56:05 +02003142 /*
3143 * Stolen memory is always coherent with the GPU as it is explicitly
3144 * marked as wc by the system, or the system is cache-coherent.
3145 */
3146 if (obj->stolen)
3147 return;
3148
Chris Wilson9c23f7f2011-03-29 16:59:52 -07003149 /* If the GPU is snooping the contents of the CPU cache,
3150 * we do not need to manually clear the CPU cache lines. However,
3151 * the caches are only snooped when the render cache is
3152 * flushed/invalidated. As we always have to emit invalidations
3153 * and flushes when moving into and out of the RENDER domain, correct
3154 * snooping behaviour occurs naturally as the result of our domain
3155 * tracking.
3156 */
3157 if (obj->cache_level != I915_CACHE_NONE)
3158 return;
3159
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003160 trace_i915_gem_object_clflush(obj);
Eric Anholtcfa16a02009-05-26 18:46:16 -07003161
Chris Wilson9da3da62012-06-01 15:20:22 +01003162 drm_clflush_sg(obj->pages);
Eric Anholte47c68e2008-11-14 13:35:19 -08003163}
3164
3165/** Flushes the GTT write domain for the object if it's dirty. */
3166static void
Chris Wilson05394f32010-11-08 19:18:58 +00003167i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003168{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003169 uint32_t old_write_domain;
3170
Chris Wilson05394f32010-11-08 19:18:58 +00003171 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
Eric Anholte47c68e2008-11-14 13:35:19 -08003172 return;
3173
Chris Wilson63256ec2011-01-04 18:42:07 +00003174 /* No actual flushing is required for the GTT write domain. Writes
Eric Anholte47c68e2008-11-14 13:35:19 -08003175 * to it immediately go to main memory as far as we know, so there's
3176 * no chipset flush. It also doesn't land in render cache.
Chris Wilson63256ec2011-01-04 18:42:07 +00003177 *
3178 * However, we do have to enforce the order so that all writes through
3179 * the GTT land before any writes to the device, such as updates to
3180 * the GATT itself.
Eric Anholte47c68e2008-11-14 13:35:19 -08003181 */
Chris Wilson63256ec2011-01-04 18:42:07 +00003182 wmb();
3183
Chris Wilson05394f32010-11-08 19:18:58 +00003184 old_write_domain = obj->base.write_domain;
3185 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003186
3187 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003188 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003189 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003190}
3191
3192/** Flushes the CPU write domain for the object if it's dirty. */
3193static void
Chris Wilson05394f32010-11-08 19:18:58 +00003194i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
Eric Anholte47c68e2008-11-14 13:35:19 -08003195{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003196 uint32_t old_write_domain;
Eric Anholte47c68e2008-11-14 13:35:19 -08003197
Chris Wilson05394f32010-11-08 19:18:58 +00003198 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
Eric Anholte47c68e2008-11-14 13:35:19 -08003199 return;
3200
3201 i915_gem_clflush_object(obj);
Ben Widawskye76e9ae2012-11-04 09:21:27 -08003202 i915_gem_chipset_flush(obj->base.dev);
Chris Wilson05394f32010-11-08 19:18:58 +00003203 old_write_domain = obj->base.write_domain;
3204 obj->base.write_domain = 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003205
3206 trace_i915_gem_object_change_domain(obj,
Chris Wilson05394f32010-11-08 19:18:58 +00003207 obj->base.read_domains,
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003208 old_write_domain);
Eric Anholte47c68e2008-11-14 13:35:19 -08003209}
3210
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003211/**
3212 * Moves a single object to the GTT read, and possibly write domain.
3213 *
3214 * This function returns when the move is complete, including waiting on
3215 * flushes to occur.
3216 */
Jesse Barnes79e53942008-11-07 14:24:08 -08003217int
Chris Wilson20217462010-11-23 15:26:33 +00003218i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003219{
Chris Wilson8325a092012-04-24 15:52:35 +01003220 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003221 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003222 int ret;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003223
Eric Anholt02354392008-11-26 13:58:13 -08003224 /* Not valid to be called on unbound objects. */
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003225 if (!i915_gem_obj_ggtt_bound(obj))
Eric Anholt02354392008-11-26 13:58:13 -08003226 return -EINVAL;
3227
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003228 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3229 return 0;
3230
Chris Wilson0201f1e2012-07-20 12:41:01 +01003231 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003232 if (ret)
3233 return ret;
3234
Chris Wilson72133422010-09-13 23:56:38 +01003235 i915_gem_object_flush_cpu_write_domain(obj);
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003236
Chris Wilsond0a57782012-10-09 19:24:37 +01003237 /* Serialise direct access to this object with the barriers for
3238 * coherent writes from the GPU, by effectively invalidating the
3239 * GTT domain upon first access.
3240 */
3241 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3242 mb();
3243
Chris Wilson05394f32010-11-08 19:18:58 +00003244 old_write_domain = obj->base.write_domain;
3245 old_read_domains = obj->base.read_domains;
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003246
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003247 /* It should now be out of any other write domains, and we can update
3248 * the domain values for our changes.
3249 */
Chris Wilson05394f32010-11-08 19:18:58 +00003250 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3251 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Eric Anholte47c68e2008-11-14 13:35:19 -08003252 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003253 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3254 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3255 obj->dirty = 1;
Eric Anholte47c68e2008-11-14 13:35:19 -08003256 }
3257
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003258 trace_i915_gem_object_change_domain(obj,
3259 old_read_domains,
3260 old_write_domain);
3261
Chris Wilson8325a092012-04-24 15:52:35 +01003262 /* And bump the LRU for this access */
3263 if (i915_gem_object_is_inactive(obj))
3264 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
3265
Eric Anholte47c68e2008-11-14 13:35:19 -08003266 return 0;
3267}
3268
Chris Wilsone4ffd172011-04-04 09:44:39 +01003269int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3270 enum i915_cache_level cache_level)
3271{
Daniel Vetter7bddb012012-02-09 17:15:47 +01003272 struct drm_device *dev = obj->base.dev;
3273 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone4ffd172011-04-04 09:44:39 +01003274 int ret;
3275
3276 if (obj->cache_level == cache_level)
3277 return 0;
3278
3279 if (obj->pin_count) {
3280 DRM_DEBUG("can not change the cache level of pinned objects\n");
3281 return -EBUSY;
3282 }
3283
Ben Widawskyc6cfb322013-07-05 14:41:06 -07003284 if (!i915_gem_valid_gtt_space(dev, &obj->gtt_space, cache_level)) {
Chris Wilson42d6ab42012-07-26 11:49:32 +01003285 ret = i915_gem_object_unbind(obj);
3286 if (ret)
3287 return ret;
3288 }
3289
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003290 if (i915_gem_obj_ggtt_bound(obj)) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003291 ret = i915_gem_object_finish_gpu(obj);
3292 if (ret)
3293 return ret;
3294
3295 i915_gem_object_finish_gtt(obj);
3296
3297 /* Before SandyBridge, you could not use tiling or fence
3298 * registers with snooped memory, so relinquish any fences
3299 * currently pointing to our region in the aperture.
3300 */
Chris Wilson42d6ab42012-07-26 11:49:32 +01003301 if (INTEL_INFO(dev)->gen < 6) {
Chris Wilsone4ffd172011-04-04 09:44:39 +01003302 ret = i915_gem_object_put_fence(obj);
3303 if (ret)
3304 return ret;
3305 }
3306
Daniel Vetter74898d72012-02-15 23:50:22 +01003307 if (obj->has_global_gtt_mapping)
3308 i915_gem_gtt_bind_object(obj, cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +01003309 if (obj->has_aliasing_ppgtt_mapping)
3310 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3311 obj, cache_level);
Chris Wilson42d6ab42012-07-26 11:49:32 +01003312
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003313 i915_gem_obj_ggtt_set_color(obj, cache_level);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003314 }
3315
3316 if (cache_level == I915_CACHE_NONE) {
3317 u32 old_read_domains, old_write_domain;
3318
3319 /* If we're coming from LLC cached, then we haven't
3320 * actually been tracking whether the data is in the
3321 * CPU cache or not, since we only allow one bit set
3322 * in obj->write_domain and have been skipping the clflushes.
3323 * Just set it to the CPU cache for now.
3324 */
3325 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3326 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3327
3328 old_read_domains = obj->base.read_domains;
3329 old_write_domain = obj->base.write_domain;
3330
3331 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3332 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3333
3334 trace_i915_gem_object_change_domain(obj,
3335 old_read_domains,
3336 old_write_domain);
3337 }
3338
3339 obj->cache_level = cache_level;
Chris Wilson42d6ab42012-07-26 11:49:32 +01003340 i915_gem_verify_gtt(dev);
Chris Wilsone4ffd172011-04-04 09:44:39 +01003341 return 0;
3342}
3343
Ben Widawsky199adf42012-09-21 17:01:20 -07003344int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3345 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003346{
Ben Widawsky199adf42012-09-21 17:01:20 -07003347 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003348 struct drm_i915_gem_object *obj;
3349 int ret;
3350
3351 ret = i915_mutex_lock_interruptible(dev);
3352 if (ret)
3353 return ret;
3354
3355 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3356 if (&obj->base == NULL) {
3357 ret = -ENOENT;
3358 goto unlock;
3359 }
3360
Ben Widawsky199adf42012-09-21 17:01:20 -07003361 args->caching = obj->cache_level != I915_CACHE_NONE;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003362
3363 drm_gem_object_unreference(&obj->base);
3364unlock:
3365 mutex_unlock(&dev->struct_mutex);
3366 return ret;
3367}
3368
Ben Widawsky199adf42012-09-21 17:01:20 -07003369int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3370 struct drm_file *file)
Chris Wilsone6994ae2012-07-10 10:27:08 +01003371{
Ben Widawsky199adf42012-09-21 17:01:20 -07003372 struct drm_i915_gem_caching *args = data;
Chris Wilsone6994ae2012-07-10 10:27:08 +01003373 struct drm_i915_gem_object *obj;
3374 enum i915_cache_level level;
3375 int ret;
3376
Ben Widawsky199adf42012-09-21 17:01:20 -07003377 switch (args->caching) {
3378 case I915_CACHING_NONE:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003379 level = I915_CACHE_NONE;
3380 break;
Ben Widawsky199adf42012-09-21 17:01:20 -07003381 case I915_CACHING_CACHED:
Chris Wilsone6994ae2012-07-10 10:27:08 +01003382 level = I915_CACHE_LLC;
3383 break;
3384 default:
3385 return -EINVAL;
3386 }
3387
Ben Widawsky3bc29132012-09-26 16:15:20 -07003388 ret = i915_mutex_lock_interruptible(dev);
3389 if (ret)
3390 return ret;
3391
Chris Wilsone6994ae2012-07-10 10:27:08 +01003392 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3393 if (&obj->base == NULL) {
3394 ret = -ENOENT;
3395 goto unlock;
3396 }
3397
3398 ret = i915_gem_object_set_cache_level(obj, level);
3399
3400 drm_gem_object_unreference(&obj->base);
3401unlock:
3402 mutex_unlock(&dev->struct_mutex);
3403 return ret;
3404}
3405
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003406/*
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003407 * Prepare buffer for display plane (scanout, cursors, etc).
3408 * Can be called from an uninterruptible phase (modesetting) and allows
3409 * any flushes to be pipelined (for pageflips).
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003410 */
3411int
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003412i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3413 u32 alignment,
Chris Wilson919926a2010-11-12 13:42:53 +00003414 struct intel_ring_buffer *pipelined)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003415{
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003416 u32 old_read_domains, old_write_domain;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003417 int ret;
3418
Chris Wilson0be73282010-12-06 14:36:27 +00003419 if (pipelined != obj->ring) {
Ben Widawsky2911a352012-04-05 14:47:36 -07003420 ret = i915_gem_object_sync(obj, pipelined);
3421 if (ret)
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003422 return ret;
3423 }
3424
Eric Anholta7ef0642011-03-29 16:59:54 -07003425 /* The display engine is not coherent with the LLC cache on gen6. As
3426 * a result, we make sure that the pinning that is about to occur is
3427 * done with uncached PTEs. This is lowest common denominator for all
3428 * chipsets.
3429 *
3430 * However for gen6+, we could do better by using the GFDT bit instead
3431 * of uncaching, which would allow us to flush all the LLC-cached data
3432 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3433 */
3434 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3435 if (ret)
3436 return ret;
3437
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003438 /* As the user may map the buffer once pinned in the display plane
3439 * (e.g. libkms for the bootup splash), we have to ensure that we
3440 * always use map_and_fenceable for all scanout buffers.
3441 */
Chris Wilson86a1ee22012-08-11 15:41:04 +01003442 ret = i915_gem_object_pin(obj, alignment, true, false);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003443 if (ret)
3444 return ret;
3445
Chris Wilsonb118c1e2010-05-27 13:18:14 +01003446 i915_gem_object_flush_cpu_write_domain(obj);
3447
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003448 old_write_domain = obj->base.write_domain;
Chris Wilson05394f32010-11-08 19:18:58 +00003449 old_read_domains = obj->base.read_domains;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003450
3451 /* It should now be out of any other write domains, and we can update
3452 * the domain values for our changes.
3453 */
Chris Wilsone5f1d962012-07-20 12:41:00 +01003454 obj->base.write_domain = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00003455 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003456
3457 trace_i915_gem_object_change_domain(obj,
3458 old_read_domains,
Chris Wilson2da3b9b2011-04-14 09:41:17 +01003459 old_write_domain);
Zhenyu Wangb9241ea2009-11-25 13:09:39 +08003460
3461 return 0;
3462}
3463
Chris Wilson85345512010-11-13 09:49:11 +00003464int
Chris Wilsona8198ee2011-04-13 22:04:09 +01003465i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
Chris Wilson85345512010-11-13 09:49:11 +00003466{
Chris Wilson88241782011-01-07 17:09:48 +00003467 int ret;
3468
Chris Wilsona8198ee2011-04-13 22:04:09 +01003469 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
Chris Wilson85345512010-11-13 09:49:11 +00003470 return 0;
3471
Chris Wilson0201f1e2012-07-20 12:41:01 +01003472 ret = i915_gem_object_wait_rendering(obj, false);
Chris Wilsonc501ae72011-12-14 13:57:23 +01003473 if (ret)
3474 return ret;
3475
Chris Wilsona8198ee2011-04-13 22:04:09 +01003476 /* Ensure that we invalidate the GPU's caches and TLBs. */
3477 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
Chris Wilsonc501ae72011-12-14 13:57:23 +01003478 return 0;
Chris Wilson85345512010-11-13 09:49:11 +00003479}
3480
Eric Anholte47c68e2008-11-14 13:35:19 -08003481/**
3482 * Moves a single object to the CPU read, and possibly write domain.
3483 *
3484 * This function returns when the move is complete, including waiting on
3485 * flushes to occur.
3486 */
Chris Wilsondabdfe02012-03-26 10:10:27 +02003487int
Chris Wilson919926a2010-11-12 13:42:53 +00003488i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
Eric Anholte47c68e2008-11-14 13:35:19 -08003489{
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003490 uint32_t old_write_domain, old_read_domains;
Eric Anholte47c68e2008-11-14 13:35:19 -08003491 int ret;
3492
Chris Wilson8d7e3de2011-02-07 15:23:02 +00003493 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3494 return 0;
3495
Chris Wilson0201f1e2012-07-20 12:41:01 +01003496 ret = i915_gem_object_wait_rendering(obj, !write);
Chris Wilson88241782011-01-07 17:09:48 +00003497 if (ret)
3498 return ret;
3499
Eric Anholte47c68e2008-11-14 13:35:19 -08003500 i915_gem_object_flush_gtt_write_domain(obj);
3501
Chris Wilson05394f32010-11-08 19:18:58 +00003502 old_write_domain = obj->base.write_domain;
3503 old_read_domains = obj->base.read_domains;
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003504
Eric Anholte47c68e2008-11-14 13:35:19 -08003505 /* Flush the CPU cache if it's still invalid. */
Chris Wilson05394f32010-11-08 19:18:58 +00003506 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
Eric Anholte47c68e2008-11-14 13:35:19 -08003507 i915_gem_clflush_object(obj);
Eric Anholte47c68e2008-11-14 13:35:19 -08003508
Chris Wilson05394f32010-11-08 19:18:58 +00003509 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003510 }
3511
3512 /* It should now be out of any other write domains, and we can update
3513 * the domain values for our changes.
3514 */
Chris Wilson05394f32010-11-08 19:18:58 +00003515 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
Eric Anholte47c68e2008-11-14 13:35:19 -08003516
3517 /* If we're writing through the CPU, then the GPU read domains will
3518 * need to be invalidated at next use.
3519 */
3520 if (write) {
Chris Wilson05394f32010-11-08 19:18:58 +00003521 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3522 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
Eric Anholte47c68e2008-11-14 13:35:19 -08003523 }
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003524
Chris Wilson1c5d22f2009-08-25 11:15:50 +01003525 trace_i915_gem_object_change_domain(obj,
3526 old_read_domains,
3527 old_write_domain);
3528
Eric Anholt2ef7eea2008-11-10 10:53:25 -08003529 return 0;
3530}
3531
Eric Anholt673a3942008-07-30 12:06:12 -07003532/* Throttle our rendering by waiting until the ring has completed our requests
3533 * emitted over 20 msec ago.
3534 *
Eric Anholtb9624422009-06-03 07:27:35 +00003535 * Note that if we were to use the current jiffies each time around the loop,
3536 * we wouldn't escape the function with any frames outstanding if the time to
3537 * render a frame was over 20ms.
3538 *
Eric Anholt673a3942008-07-30 12:06:12 -07003539 * This should get us reasonable parallelism between CPU and GPU but also
3540 * relatively low latency when blocking on a particular request to finish.
3541 */
3542static int
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003543i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003544{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003545 struct drm_i915_private *dev_priv = dev->dev_private;
3546 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00003547 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003548 struct drm_i915_gem_request *request;
3549 struct intel_ring_buffer *ring = NULL;
Daniel Vetterf69061b2012-12-06 09:01:42 +01003550 unsigned reset_counter;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003551 u32 seqno = 0;
3552 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003553
Daniel Vetter308887a2012-11-14 17:14:06 +01003554 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3555 if (ret)
3556 return ret;
3557
3558 ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
3559 if (ret)
3560 return ret;
Chris Wilsone110e8d2011-01-26 15:39:14 +00003561
Chris Wilson1c255952010-09-26 11:03:27 +01003562 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003563 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
Eric Anholtb9624422009-06-03 07:27:35 +00003564 if (time_after_eq(request->emitted_jiffies, recent_enough))
3565 break;
3566
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003567 ring = request->ring;
3568 seqno = request->seqno;
Eric Anholtb9624422009-06-03 07:27:35 +00003569 }
Daniel Vetterf69061b2012-12-06 09:01:42 +01003570 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilson1c255952010-09-26 11:03:27 +01003571 spin_unlock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003572
3573 if (seqno == 0)
3574 return 0;
3575
Daniel Vetterf69061b2012-12-06 09:01:42 +01003576 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01003577 if (ret == 0)
3578 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
Eric Anholtb9624422009-06-03 07:27:35 +00003579
Eric Anholt673a3942008-07-30 12:06:12 -07003580 return ret;
3581}
3582
Eric Anholt673a3942008-07-30 12:06:12 -07003583int
Chris Wilson05394f32010-11-08 19:18:58 +00003584i915_gem_object_pin(struct drm_i915_gem_object *obj,
3585 uint32_t alignment,
Chris Wilson86a1ee22012-08-11 15:41:04 +01003586 bool map_and_fenceable,
3587 bool nonblocking)
Eric Anholt673a3942008-07-30 12:06:12 -07003588{
Eric Anholt673a3942008-07-30 12:06:12 -07003589 int ret;
3590
Chris Wilson7e81a422012-09-15 09:41:57 +01003591 if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3592 return -EBUSY;
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003593
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003594 if (i915_gem_obj_ggtt_bound(obj)) {
3595 if ((alignment && i915_gem_obj_ggtt_offset(obj) & (alignment - 1)) ||
Chris Wilson05394f32010-11-08 19:18:58 +00003596 (map_and_fenceable && !obj->map_and_fenceable)) {
3597 WARN(obj->pin_count,
Chris Wilsonae7d49d2010-08-04 12:37:41 +01003598 "bo is already pinned with incorrect alignment:"
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003599 " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
Daniel Vetter75e9e912010-11-04 17:11:09 +01003600 " obj->map_and_fenceable=%d\n",
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003601 i915_gem_obj_ggtt_offset(obj), alignment,
Daniel Vetter75e9e912010-11-04 17:11:09 +01003602 map_and_fenceable,
Chris Wilson05394f32010-11-08 19:18:58 +00003603 obj->map_and_fenceable);
Chris Wilsonac0c6b52010-05-27 13:18:18 +01003604 ret = i915_gem_object_unbind(obj);
3605 if (ret)
3606 return ret;
3607 }
3608 }
3609
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003610 if (!i915_gem_obj_ggtt_bound(obj)) {
Chris Wilson87422672012-11-21 13:04:03 +00003611 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3612
Chris Wilsona00b10c2010-09-24 21:15:47 +01003613 ret = i915_gem_object_bind_to_gtt(obj, alignment,
Chris Wilson86a1ee22012-08-11 15:41:04 +01003614 map_and_fenceable,
3615 nonblocking);
Chris Wilson97311292009-09-21 00:22:34 +01003616 if (ret)
Eric Anholt673a3942008-07-30 12:06:12 -07003617 return ret;
Chris Wilson87422672012-11-21 13:04:03 +00003618
3619 if (!dev_priv->mm.aliasing_ppgtt)
3620 i915_gem_gtt_bind_object(obj, obj->cache_level);
Chris Wilson22c344e2009-02-11 14:26:45 +00003621 }
Jesse Barnes76446ca2009-12-17 22:05:42 -05003622
Daniel Vetter74898d72012-02-15 23:50:22 +01003623 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3624 i915_gem_gtt_bind_object(obj, obj->cache_level);
3625
Chris Wilson1b502472012-04-24 15:47:30 +01003626 obj->pin_count++;
Chris Wilson6299f992010-11-24 12:23:44 +00003627 obj->pin_mappable |= map_and_fenceable;
Eric Anholt673a3942008-07-30 12:06:12 -07003628
3629 return 0;
3630}
3631
3632void
Chris Wilson05394f32010-11-08 19:18:58 +00003633i915_gem_object_unpin(struct drm_i915_gem_object *obj)
Eric Anholt673a3942008-07-30 12:06:12 -07003634{
Chris Wilson05394f32010-11-08 19:18:58 +00003635 BUG_ON(obj->pin_count == 0);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003636 BUG_ON(!i915_gem_obj_ggtt_bound(obj));
Eric Anholt673a3942008-07-30 12:06:12 -07003637
Chris Wilson1b502472012-04-24 15:47:30 +01003638 if (--obj->pin_count == 0)
Chris Wilson6299f992010-11-24 12:23:44 +00003639 obj->pin_mappable = false;
Eric Anholt673a3942008-07-30 12:06:12 -07003640}
3641
3642int
3643i915_gem_pin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003644 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003645{
3646 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003647 struct drm_i915_gem_object *obj;
Eric Anholt673a3942008-07-30 12:06:12 -07003648 int ret;
3649
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003650 ret = i915_mutex_lock_interruptible(dev);
3651 if (ret)
3652 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003653
Chris Wilson05394f32010-11-08 19:18:58 +00003654 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003655 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003656 ret = -ENOENT;
3657 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003658 }
Eric Anholt673a3942008-07-30 12:06:12 -07003659
Chris Wilson05394f32010-11-08 19:18:58 +00003660 if (obj->madv != I915_MADV_WILLNEED) {
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003661 DRM_ERROR("Attempting to pin a purgeable buffer\n");
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003662 ret = -EINVAL;
3663 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003664 }
3665
Chris Wilson05394f32010-11-08 19:18:58 +00003666 if (obj->pin_filp != NULL && obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003667 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3668 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003669 ret = -EINVAL;
3670 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003671 }
3672
Chris Wilson93be8782013-01-02 10:31:22 +00003673 if (obj->user_pin_count == 0) {
Chris Wilson86a1ee22012-08-11 15:41:04 +01003674 ret = i915_gem_object_pin(obj, args->alignment, true, false);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003675 if (ret)
3676 goto out;
Eric Anholt673a3942008-07-30 12:06:12 -07003677 }
3678
Chris Wilson93be8782013-01-02 10:31:22 +00003679 obj->user_pin_count++;
3680 obj->pin_filp = file;
3681
Eric Anholt673a3942008-07-30 12:06:12 -07003682 /* XXX - flush the CPU caches for pinned objects
3683 * as the X server doesn't manage domains yet
3684 */
Eric Anholte47c68e2008-11-14 13:35:19 -08003685 i915_gem_object_flush_cpu_write_domain(obj);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07003686 args->offset = i915_gem_obj_ggtt_offset(obj);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003687out:
Chris Wilson05394f32010-11-08 19:18:58 +00003688 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003689unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003690 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003691 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003692}
3693
3694int
3695i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003696 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003697{
3698 struct drm_i915_gem_pin *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003699 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003700 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003701
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003702 ret = i915_mutex_lock_interruptible(dev);
3703 if (ret)
3704 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003705
Chris Wilson05394f32010-11-08 19:18:58 +00003706 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003707 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003708 ret = -ENOENT;
3709 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003710 }
Chris Wilson76c1dec2010-09-25 11:22:51 +01003711
Chris Wilson05394f32010-11-08 19:18:58 +00003712 if (obj->pin_filp != file) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003713 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3714 args->handle);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003715 ret = -EINVAL;
3716 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08003717 }
Chris Wilson05394f32010-11-08 19:18:58 +00003718 obj->user_pin_count--;
3719 if (obj->user_pin_count == 0) {
3720 obj->pin_filp = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08003721 i915_gem_object_unpin(obj);
3722 }
Eric Anholt673a3942008-07-30 12:06:12 -07003723
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003724out:
Chris Wilson05394f32010-11-08 19:18:58 +00003725 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003726unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003727 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003728 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003729}
3730
3731int
3732i915_gem_busy_ioctl(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00003733 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07003734{
3735 struct drm_i915_gem_busy *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003736 struct drm_i915_gem_object *obj;
Chris Wilson30dbf0c2010-09-25 10:19:17 +01003737 int ret;
3738
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003739 ret = i915_mutex_lock_interruptible(dev);
3740 if (ret)
3741 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003742
Chris Wilson05394f32010-11-08 19:18:58 +00003743 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003744 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003745 ret = -ENOENT;
3746 goto unlock;
Eric Anholt673a3942008-07-30 12:06:12 -07003747 }
Zou Nan haid1b851f2010-05-21 09:08:57 +08003748
Chris Wilson0be555b2010-08-04 15:36:30 +01003749 /* Count all active objects as busy, even if they are currently not used
3750 * by the gpu. Users of this interface expect objects to eventually
3751 * become non-busy without any further actions, therefore emit any
3752 * necessary flushes here.
Eric Anholtc4de0a52008-12-14 19:05:04 -08003753 */
Daniel Vetter30dfebf2012-06-01 15:21:23 +02003754 ret = i915_gem_object_flush_active(obj);
3755
Chris Wilson05394f32010-11-08 19:18:58 +00003756 args->busy = obj->active;
Chris Wilsone9808ed2012-07-04 12:25:08 +01003757 if (obj->ring) {
3758 BUILD_BUG_ON(I915_NUM_RINGS > 16);
3759 args->busy |= intel_ring_flag(obj->ring) << 16;
3760 }
Eric Anholt673a3942008-07-30 12:06:12 -07003761
Chris Wilson05394f32010-11-08 19:18:58 +00003762 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003763unlock:
Eric Anholt673a3942008-07-30 12:06:12 -07003764 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003765 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003766}
3767
3768int
3769i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3770 struct drm_file *file_priv)
3771{
Akshay Joshi0206e352011-08-16 15:34:10 -04003772 return i915_gem_ring_throttle(dev, file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07003773}
3774
Chris Wilson3ef94da2009-09-14 16:50:29 +01003775int
3776i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3777 struct drm_file *file_priv)
3778{
3779 struct drm_i915_gem_madvise *args = data;
Chris Wilson05394f32010-11-08 19:18:58 +00003780 struct drm_i915_gem_object *obj;
Chris Wilson76c1dec2010-09-25 11:22:51 +01003781 int ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003782
3783 switch (args->madv) {
3784 case I915_MADV_DONTNEED:
3785 case I915_MADV_WILLNEED:
3786 break;
3787 default:
3788 return -EINVAL;
3789 }
3790
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003791 ret = i915_mutex_lock_interruptible(dev);
3792 if (ret)
3793 return ret;
3794
Chris Wilson05394f32010-11-08 19:18:58 +00003795 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00003796 if (&obj->base == NULL) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003797 ret = -ENOENT;
3798 goto unlock;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003799 }
Chris Wilson3ef94da2009-09-14 16:50:29 +01003800
Chris Wilson05394f32010-11-08 19:18:58 +00003801 if (obj->pin_count) {
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003802 ret = -EINVAL;
3803 goto out;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003804 }
3805
Chris Wilson05394f32010-11-08 19:18:58 +00003806 if (obj->madv != __I915_MADV_PURGED)
3807 obj->madv = args->madv;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003808
Chris Wilson6c085a72012-08-20 11:40:46 +02003809 /* if the object is no longer attached, discard its backing storage */
3810 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
Chris Wilson2d7ef392009-09-20 23:13:10 +01003811 i915_gem_object_truncate(obj);
3812
Chris Wilson05394f32010-11-08 19:18:58 +00003813 args->retained = obj->madv != __I915_MADV_PURGED;
Chris Wilsonbb6baf72009-09-22 14:24:13 +01003814
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003815out:
Chris Wilson05394f32010-11-08 19:18:58 +00003816 drm_gem_object_unreference(&obj->base);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003817unlock:
Chris Wilson3ef94da2009-09-14 16:50:29 +01003818 mutex_unlock(&dev->struct_mutex);
Chris Wilson1d7cfea2010-10-17 09:45:41 +01003819 return ret;
Chris Wilson3ef94da2009-09-14 16:50:29 +01003820}
3821
Chris Wilson37e680a2012-06-07 15:38:42 +01003822void i915_gem_object_init(struct drm_i915_gem_object *obj,
3823 const struct drm_i915_gem_object_ops *ops)
Chris Wilson0327d6b2012-08-11 15:41:06 +01003824{
Chris Wilson0327d6b2012-08-11 15:41:06 +01003825 INIT_LIST_HEAD(&obj->mm_list);
Ben Widawsky35c20a62013-05-31 11:28:48 -07003826 INIT_LIST_HEAD(&obj->global_list);
Chris Wilson0327d6b2012-08-11 15:41:06 +01003827 INIT_LIST_HEAD(&obj->ring_list);
3828 INIT_LIST_HEAD(&obj->exec_list);
3829
Chris Wilson37e680a2012-06-07 15:38:42 +01003830 obj->ops = ops;
3831
Chris Wilson0327d6b2012-08-11 15:41:06 +01003832 obj->fence_reg = I915_FENCE_REG_NONE;
3833 obj->madv = I915_MADV_WILLNEED;
3834 /* Avoid an unnecessary call to unbind on the first bind. */
3835 obj->map_and_fenceable = true;
3836
3837 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
3838}
3839
Chris Wilson37e680a2012-06-07 15:38:42 +01003840static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
3841 .get_pages = i915_gem_object_get_pages_gtt,
3842 .put_pages = i915_gem_object_put_pages_gtt,
3843};
3844
Chris Wilson05394f32010-11-08 19:18:58 +00003845struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3846 size_t size)
Daniel Vetterac52bc52010-04-09 19:05:06 +00003847{
Daniel Vetterc397b902010-04-09 19:05:07 +00003848 struct drm_i915_gem_object *obj;
Hugh Dickins5949eac2011-06-27 16:18:18 -07003849 struct address_space *mapping;
Daniel Vetter1a240d42012-11-29 22:18:51 +01003850 gfp_t mask;
Daniel Vetterc397b902010-04-09 19:05:07 +00003851
Chris Wilson42dcedd2012-11-15 11:32:30 +00003852 obj = i915_gem_object_alloc(dev);
Daniel Vetterc397b902010-04-09 19:05:07 +00003853 if (obj == NULL)
3854 return NULL;
3855
3856 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
Chris Wilson42dcedd2012-11-15 11:32:30 +00003857 i915_gem_object_free(obj);
Daniel Vetterc397b902010-04-09 19:05:07 +00003858 return NULL;
3859 }
3860
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003861 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
3862 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
3863 /* 965gm cannot relocate objects above 4GiB. */
3864 mask &= ~__GFP_HIGHMEM;
3865 mask |= __GFP_DMA32;
3866 }
3867
Al Viro496ad9a2013-01-23 17:07:38 -05003868 mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsonbed1ea92012-05-24 20:48:12 +01003869 mapping_set_gfp_mask(mapping, mask);
Hugh Dickins5949eac2011-06-27 16:18:18 -07003870
Chris Wilson37e680a2012-06-07 15:38:42 +01003871 i915_gem_object_init(obj, &i915_gem_object_ops);
Chris Wilson73aa8082010-09-30 11:46:12 +01003872
Daniel Vetterc397b902010-04-09 19:05:07 +00003873 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3874 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3875
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02003876 if (HAS_LLC(dev)) {
3877 /* On some devices, we can have the GPU use the LLC (the CPU
Eric Anholta1871112011-03-29 16:59:55 -07003878 * cache) for about a 10% performance improvement
3879 * compared to uncached. Graphics requests other than
3880 * display scanout are coherent with the CPU in
3881 * accessing this cache. This means in this mode we
3882 * don't need to clflush on the CPU side, and on the
3883 * GPU side we only need to flush internal caches to
3884 * get data visible to the CPU.
3885 *
3886 * However, we maintain the display planes as UC, and so
3887 * need to rebind when first used as such.
3888 */
3889 obj->cache_level = I915_CACHE_LLC;
3890 } else
3891 obj->cache_level = I915_CACHE_NONE;
3892
Chris Wilson05394f32010-11-08 19:18:58 +00003893 return obj;
Daniel Vetterac52bc52010-04-09 19:05:06 +00003894}
3895
Eric Anholt673a3942008-07-30 12:06:12 -07003896int i915_gem_init_object(struct drm_gem_object *obj)
3897{
Daniel Vetterc397b902010-04-09 19:05:07 +00003898 BUG();
Jesse Barnesde151cf2008-11-12 10:03:55 -08003899
Eric Anholt673a3942008-07-30 12:06:12 -07003900 return 0;
3901}
3902
Chris Wilson1488fc02012-04-24 15:47:31 +01003903void i915_gem_free_object(struct drm_gem_object *gem_obj)
Chris Wilsonbe726152010-07-23 23:18:50 +01003904{
Chris Wilson1488fc02012-04-24 15:47:31 +01003905 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00003906 struct drm_device *dev = obj->base.dev;
Chris Wilsonbe726152010-07-23 23:18:50 +01003907 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonbe726152010-07-23 23:18:50 +01003908
Chris Wilson26e12f82011-03-20 11:20:19 +00003909 trace_i915_gem_object_destroy(obj);
3910
Chris Wilson1488fc02012-04-24 15:47:31 +01003911 if (obj->phys_obj)
3912 i915_gem_detach_phys_object(dev, obj);
3913
3914 obj->pin_count = 0;
3915 if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3916 bool was_interruptible;
3917
3918 was_interruptible = dev_priv->mm.interruptible;
3919 dev_priv->mm.interruptible = false;
3920
3921 WARN_ON(i915_gem_object_unbind(obj));
3922
3923 dev_priv->mm.interruptible = was_interruptible;
3924 }
3925
Ben Widawsky1d64ae72013-05-31 14:46:20 -07003926 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
3927 * before progressing. */
3928 if (obj->stolen)
3929 i915_gem_object_unpin_pages(obj);
3930
Ben Widawsky401c29f2013-05-31 11:28:47 -07003931 if (WARN_ON(obj->pages_pin_count))
3932 obj->pages_pin_count = 0;
Chris Wilson37e680a2012-06-07 15:38:42 +01003933 i915_gem_object_put_pages(obj);
Chris Wilsond8cb5082012-08-11 15:41:03 +01003934 i915_gem_object_free_mmap_offset(obj);
Chris Wilson0104fdb2012-11-15 11:32:26 +00003935 i915_gem_object_release_stolen(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003936
Chris Wilson9da3da62012-06-01 15:20:22 +01003937 BUG_ON(obj->pages);
3938
Chris Wilson2f745ad2012-09-04 21:02:58 +01003939 if (obj->base.import_attach)
3940 drm_prime_gem_destroy(&obj->base, NULL);
Chris Wilsonbe726152010-07-23 23:18:50 +01003941
Chris Wilson05394f32010-11-08 19:18:58 +00003942 drm_gem_object_release(&obj->base);
3943 i915_gem_info_remove_obj(dev_priv, obj->base.size);
Chris Wilsonbe726152010-07-23 23:18:50 +01003944
Chris Wilson05394f32010-11-08 19:18:58 +00003945 kfree(obj->bit_17);
Chris Wilson42dcedd2012-11-15 11:32:30 +00003946 i915_gem_object_free(obj);
Chris Wilsonbe726152010-07-23 23:18:50 +01003947}
3948
Jesse Barnes5669fca2009-02-17 15:13:31 -08003949int
Eric Anholt673a3942008-07-30 12:06:12 -07003950i915_gem_idle(struct drm_device *dev)
3951{
3952 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson29105cc2010-01-07 10:39:13 +00003953 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07003954
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02003955 if (dev_priv->ums.mm_suspended) {
Keith Packard6dbe2772008-10-14 21:41:13 -07003956 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003957 return 0;
Keith Packard6dbe2772008-10-14 21:41:13 -07003958 }
Eric Anholt673a3942008-07-30 12:06:12 -07003959
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003960 ret = i915_gpu_idle(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003961 if (ret) {
3962 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07003963 return ret;
Keith Packard6dbe2772008-10-14 21:41:13 -07003964 }
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07003965 i915_gem_retire_requests(dev);
Eric Anholt673a3942008-07-30 12:06:12 -07003966
Chris Wilson29105cc2010-01-07 10:39:13 +00003967 /* Under UMS, be paranoid and evict. */
Chris Wilsona39d7ef2012-04-24 18:22:52 +01003968 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson6c085a72012-08-20 11:40:46 +02003969 i915_gem_evict_everything(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003970
Daniel Vetter99584db2012-11-14 17:14:04 +01003971 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
Chris Wilson29105cc2010-01-07 10:39:13 +00003972
3973 i915_kernel_lost_context(dev);
Keith Packard6dbe2772008-10-14 21:41:13 -07003974 i915_gem_cleanup_ringbuffer(dev);
Chris Wilson29105cc2010-01-07 10:39:13 +00003975
Chris Wilson29105cc2010-01-07 10:39:13 +00003976 /* Cancel the retire work handler, which should be idle now. */
3977 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3978
Eric Anholt673a3942008-07-30 12:06:12 -07003979 return 0;
3980}
3981
Ben Widawskyb9524a12012-05-25 16:56:24 -07003982void i915_gem_l3_remap(struct drm_device *dev)
3983{
3984 drm_i915_private_t *dev_priv = dev->dev_private;
3985 u32 misccpctl;
3986 int i;
3987
Daniel Vettereb32e452013-02-14 19:46:07 +01003988 if (!HAS_L3_GPU_CACHE(dev))
Ben Widawskyb9524a12012-05-25 16:56:24 -07003989 return;
3990
Daniel Vettera4da4fa2012-11-02 19:55:07 +01003991 if (!dev_priv->l3_parity.remap_info)
Ben Widawskyb9524a12012-05-25 16:56:24 -07003992 return;
3993
3994 misccpctl = I915_READ(GEN7_MISCCPCTL);
3995 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
3996 POSTING_READ(GEN7_MISCCPCTL);
3997
3998 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
3999 u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004000 if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
Ben Widawskyb9524a12012-05-25 16:56:24 -07004001 DRM_DEBUG("0x%x was already programmed to %x\n",
4002 GEN7_L3LOG_BASE + i, remap);
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004003 if (remap && !dev_priv->l3_parity.remap_info[i/4])
Ben Widawskyb9524a12012-05-25 16:56:24 -07004004 DRM_DEBUG_DRIVER("Clearing remapped register\n");
Daniel Vettera4da4fa2012-11-02 19:55:07 +01004005 I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
Ben Widawskyb9524a12012-05-25 16:56:24 -07004006 }
4007
4008 /* Make sure all the writes land before disabling dop clock gating */
4009 POSTING_READ(GEN7_L3LOG_BASE);
4010
4011 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
4012}
4013
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004014void i915_gem_init_swizzling(struct drm_device *dev)
4015{
4016 drm_i915_private_t *dev_priv = dev->dev_private;
4017
Daniel Vetter11782b02012-01-31 16:47:55 +01004018 if (INTEL_INFO(dev)->gen < 5 ||
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004019 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4020 return;
4021
4022 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4023 DISP_TILE_SURFACE_SWIZZLING);
4024
Daniel Vetter11782b02012-01-31 16:47:55 +01004025 if (IS_GEN5(dev))
4026 return;
4027
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004028 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4029 if (IS_GEN6(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004030 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004031 else if (IS_GEN7(dev))
Daniel Vetter6b26c862012-04-24 14:04:12 +02004032 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
Ben Widawsky8782e262012-12-18 10:31:23 -08004033 else
4034 BUG();
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004035}
Daniel Vettere21af882012-02-09 20:53:27 +01004036
Chris Wilson67b1b572012-07-05 23:49:40 +01004037static bool
4038intel_enable_blt(struct drm_device *dev)
4039{
4040 if (!HAS_BLT(dev))
4041 return false;
4042
4043 /* The blitter was dysfunctional on early prototypes */
4044 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
4045 DRM_INFO("BLT not supported on this pre-production hardware;"
4046 " graphics performance will be degraded.\n");
4047 return false;
4048 }
4049
4050 return true;
4051}
4052
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004053static int i915_gem_init_rings(struct drm_device *dev)
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004054{
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004055 struct drm_i915_private *dev_priv = dev->dev_private;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004056 int ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004057
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004058 ret = intel_init_render_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004059 if (ret)
Chris Wilsonb6913e42010-11-12 10:46:37 +00004060 return ret;
Chris Wilson68f95ba2010-05-27 13:18:22 +01004061
4062 if (HAS_BSD(dev)) {
Xiang, Haihao5c1143b2010-09-16 10:43:11 +08004063 ret = intel_init_bsd_ring_buffer(dev);
Chris Wilson68f95ba2010-05-27 13:18:22 +01004064 if (ret)
4065 goto cleanup_render_ring;
Zou Nan haid1b851f2010-05-21 09:08:57 +08004066 }
Chris Wilson68f95ba2010-05-27 13:18:22 +01004067
Chris Wilson67b1b572012-07-05 23:49:40 +01004068 if (intel_enable_blt(dev)) {
Chris Wilson549f7362010-10-19 11:19:32 +01004069 ret = intel_init_blt_ring_buffer(dev);
4070 if (ret)
4071 goto cleanup_bsd_ring;
4072 }
4073
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004074 if (HAS_VEBOX(dev)) {
4075 ret = intel_init_vebox_ring_buffer(dev);
4076 if (ret)
4077 goto cleanup_blt_ring;
4078 }
4079
4080
Mika Kuoppala99433932013-01-22 14:12:17 +02004081 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
4082 if (ret)
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004083 goto cleanup_vebox_ring;
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004084
4085 return 0;
4086
Ben Widawsky9a8a2212013-05-28 19:22:23 -07004087cleanup_vebox_ring:
4088 intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004089cleanup_blt_ring:
4090 intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
4091cleanup_bsd_ring:
4092 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
4093cleanup_render_ring:
4094 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
4095
4096 return ret;
4097}
4098
4099int
4100i915_gem_init_hw(struct drm_device *dev)
4101{
4102 drm_i915_private_t *dev_priv = dev->dev_private;
4103 int ret;
4104
4105 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
4106 return -EIO;
4107
4108 if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
4109 I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
4110
Ben Widawsky88a2b2a2013-04-05 13:12:43 -07004111 if (HAS_PCH_NOP(dev)) {
4112 u32 temp = I915_READ(GEN7_MSG_CTL);
4113 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4114 I915_WRITE(GEN7_MSG_CTL, temp);
4115 }
4116
Ben Widawsky4fc7c972013-02-08 11:49:24 -08004117 i915_gem_l3_remap(dev);
4118
4119 i915_gem_init_swizzling(dev);
4120
4121 ret = i915_gem_init_rings(dev);
4122 if (ret)
Mika Kuoppala99433932013-01-22 14:12:17 +02004123 return ret;
4124
Ben Widawsky254f9652012-06-04 14:42:42 -07004125 /*
4126 * XXX: There was some w/a described somewhere suggesting loading
4127 * contexts before PPGTT.
4128 */
4129 i915_gem_context_init(dev);
Ben Widawskyb7c36d22013-04-08 18:43:56 -07004130 if (dev_priv->mm.aliasing_ppgtt) {
4131 ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
4132 if (ret) {
4133 i915_gem_cleanup_aliasing_ppgtt(dev);
4134 DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
4135 }
4136 }
Daniel Vettere21af882012-02-09 20:53:27 +01004137
Chris Wilson68f95ba2010-05-27 13:18:22 +01004138 return 0;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004139}
4140
Chris Wilson1070a422012-04-24 15:47:41 +01004141int i915_gem_init(struct drm_device *dev)
4142{
4143 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1070a422012-04-24 15:47:41 +01004144 int ret;
4145
Chris Wilson1070a422012-04-24 15:47:41 +01004146 mutex_lock(&dev->struct_mutex);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004147
4148 if (IS_VALLEYVIEW(dev)) {
4149 /* VLVA0 (potential hack), BIOS isn't actually waking us */
4150 I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
4151 if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
4152 DRM_DEBUG_DRIVER("allow wake ack timed out\n");
4153 }
4154
Ben Widawskyd7e50082012-12-18 10:31:25 -08004155 i915_gem_init_global_gtt(dev);
Jesse Barnesd62b4892013-03-08 10:45:53 -08004156
Chris Wilson1070a422012-04-24 15:47:41 +01004157 ret = i915_gem_init_hw(dev);
4158 mutex_unlock(&dev->struct_mutex);
4159 if (ret) {
4160 i915_gem_cleanup_aliasing_ppgtt(dev);
4161 return ret;
4162 }
4163
Daniel Vetter53ca26c2012-04-26 23:28:03 +02004164 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
4165 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4166 dev_priv->dri1.allow_batchbuffer = 1;
Chris Wilson1070a422012-04-24 15:47:41 +01004167 return 0;
4168}
4169
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004170void
4171i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4172{
4173 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01004174 struct intel_ring_buffer *ring;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004175 int i;
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004176
Chris Wilsonb4519512012-05-11 14:29:30 +01004177 for_each_ring(ring, dev_priv, i)
4178 intel_cleanup_ring_buffer(ring);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08004179}
4180
4181int
Eric Anholt673a3942008-07-30 12:06:12 -07004182i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4183 struct drm_file *file_priv)
4184{
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004185 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonb4519512012-05-11 14:29:30 +01004186 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004187
Jesse Barnes79e53942008-11-07 14:24:08 -08004188 if (drm_core_check_feature(dev, DRIVER_MODESET))
4189 return 0;
4190
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004191 if (i915_reset_in_progress(&dev_priv->gpu_error)) {
Eric Anholt673a3942008-07-30 12:06:12 -07004192 DRM_ERROR("Reenabling wedged hardware, good luck\n");
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004193 atomic_set(&dev_priv->gpu_error.reset_counter, 0);
Eric Anholt673a3942008-07-30 12:06:12 -07004194 }
4195
Eric Anholt673a3942008-07-30 12:06:12 -07004196 mutex_lock(&dev->struct_mutex);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004197 dev_priv->ums.mm_suspended = 0;
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004198
Daniel Vetterf691e2f2012-02-02 09:58:12 +01004199 ret = i915_gem_init_hw(dev);
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004200 if (ret != 0) {
4201 mutex_unlock(&dev->struct_mutex);
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004202 return ret;
Wu Fengguangd816f6a2009-04-18 10:43:32 +08004203 }
Eric Anholt9bb2d6f2008-12-23 18:42:32 -08004204
Chris Wilson69dc4982010-10-19 10:36:51 +01004205 BUG_ON(!list_empty(&dev_priv->mm.active_list));
Eric Anholt673a3942008-07-30 12:06:12 -07004206 mutex_unlock(&dev->struct_mutex);
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004207
Chris Wilson5f353082010-06-07 14:03:03 +01004208 ret = drm_irq_install(dev);
4209 if (ret)
4210 goto cleanup_ringbuffer;
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004211
Eric Anholt673a3942008-07-30 12:06:12 -07004212 return 0;
Chris Wilson5f353082010-06-07 14:03:03 +01004213
4214cleanup_ringbuffer:
4215 mutex_lock(&dev->struct_mutex);
4216 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004217 dev_priv->ums.mm_suspended = 1;
Chris Wilson5f353082010-06-07 14:03:03 +01004218 mutex_unlock(&dev->struct_mutex);
4219
4220 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004221}
4222
4223int
4224i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4225 struct drm_file *file_priv)
4226{
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004227 struct drm_i915_private *dev_priv = dev->dev_private;
4228 int ret;
4229
Jesse Barnes79e53942008-11-07 14:24:08 -08004230 if (drm_core_check_feature(dev, DRIVER_MODESET))
4231 return 0;
4232
Kristian Høgsbergdbb19d32008-08-20 11:04:27 -04004233 drm_irq_uninstall(dev);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004234
4235 mutex_lock(&dev->struct_mutex);
4236 ret = i915_gem_idle(dev);
4237
4238 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4239 * We need to replace this with a semaphore, or something.
4240 * And not confound ums.mm_suspended!
4241 */
4242 if (ret != 0)
4243 dev_priv->ums.mm_suspended = 1;
4244 mutex_unlock(&dev->struct_mutex);
4245
4246 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004247}
4248
4249void
4250i915_gem_lastclose(struct drm_device *dev)
4251{
4252 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07004253
Eric Anholte806b492009-01-22 09:56:58 -08004254 if (drm_core_check_feature(dev, DRIVER_MODESET))
4255 return;
4256
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004257 mutex_lock(&dev->struct_mutex);
Keith Packard6dbe2772008-10-14 21:41:13 -07004258 ret = i915_gem_idle(dev);
4259 if (ret)
4260 DRM_ERROR("failed to idle hardware: %d\n", ret);
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02004261 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -07004262}
4263
Chris Wilson64193402010-10-24 12:38:05 +01004264static void
4265init_ring_lists(struct intel_ring_buffer *ring)
4266{
4267 INIT_LIST_HEAD(&ring->active_list);
4268 INIT_LIST_HEAD(&ring->request_list);
Chris Wilson64193402010-10-24 12:38:05 +01004269}
4270
Eric Anholt673a3942008-07-30 12:06:12 -07004271void
4272i915_gem_load(struct drm_device *dev)
4273{
4274 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson42dcedd2012-11-15 11:32:30 +00004275 int i;
4276
4277 dev_priv->slab =
4278 kmem_cache_create("i915_gem_object",
4279 sizeof(struct drm_i915_gem_object), 0,
4280 SLAB_HWCACHE_ALIGN,
4281 NULL);
Eric Anholt673a3942008-07-30 12:06:12 -07004282
Chris Wilson69dc4982010-10-19 10:36:51 +01004283 INIT_LIST_HEAD(&dev_priv->mm.active_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004284 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
Chris Wilson6c085a72012-08-20 11:40:46 +02004285 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4286 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
Eric Anholta09ba7f2009-08-29 12:49:51 -07004287 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00004288 for (i = 0; i < I915_NUM_RINGS; i++)
4289 init_ring_lists(&dev_priv->ring[i]);
Daniel Vetter4b9de732011-10-09 21:52:02 +02004290 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
Daniel Vetter007cc8a2010-04-28 11:02:31 +02004291 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
Eric Anholt673a3942008-07-30 12:06:12 -07004292 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4293 i915_gem_retire_work_handler);
Daniel Vetter1f83fee2012-11-15 17:17:22 +01004294 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
Chris Wilson31169712009-09-14 16:50:28 +01004295
Dave Airlie94400122010-07-20 13:15:31 +10004296 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4297 if (IS_GEN3(dev)) {
Daniel Vetter50743292012-04-26 22:02:54 +02004298 I915_WRITE(MI_ARB_STATE,
4299 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Dave Airlie94400122010-07-20 13:15:31 +10004300 }
4301
Chris Wilson72bfa192010-12-19 11:42:05 +00004302 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4303
Jesse Barnesde151cf2008-11-12 10:03:55 -08004304 /* Old X drivers will take 0-2 for front, back, depth buffers */
Eric Anholtb397c832010-01-26 09:43:10 -08004305 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4306 dev_priv->fence_reg_start = 3;
Jesse Barnesde151cf2008-11-12 10:03:55 -08004307
Ville Syrjälä42b5aea2013-04-09 13:02:47 +03004308 if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
4309 dev_priv->num_fence_regs = 32;
4310 else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
Jesse Barnesde151cf2008-11-12 10:03:55 -08004311 dev_priv->num_fence_regs = 16;
4312 else
4313 dev_priv->num_fence_regs = 8;
4314
Grégoire Henryb5aa8a02009-06-23 15:41:02 +02004315 /* Initialize fence registers to zero */
Chris Wilson19b2dbd2013-06-12 10:15:12 +01004316 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4317 i915_gem_restore_fences(dev);
Eric Anholt10ed13e2011-05-06 13:53:49 -07004318
Eric Anholt673a3942008-07-30 12:06:12 -07004319 i915_gem_detect_bit_6_swizzle(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05004320 init_waitqueue_head(&dev_priv->pending_flip_queue);
Chris Wilson17250b72010-10-28 12:51:39 +01004321
Chris Wilsonce453d82011-02-21 14:43:56 +00004322 dev_priv->mm.interruptible = true;
4323
Chris Wilson17250b72010-10-28 12:51:39 +01004324 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4325 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4326 register_shrinker(&dev_priv->mm.inactive_shrinker);
Eric Anholt673a3942008-07-30 12:06:12 -07004327}
Dave Airlie71acb5e2008-12-30 20:31:46 +10004328
4329/*
4330 * Create a physically contiguous memory object for this object
4331 * e.g. for cursor + overlay regs
4332 */
Chris Wilson995b6762010-08-20 13:23:26 +01004333static int i915_gem_init_phys_object(struct drm_device *dev,
4334 int id, int size, int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004335{
4336 drm_i915_private_t *dev_priv = dev->dev_private;
4337 struct drm_i915_gem_phys_object *phys_obj;
4338 int ret;
4339
4340 if (dev_priv->mm.phys_objs[id - 1] || !size)
4341 return 0;
4342
Eric Anholt9a298b22009-03-24 12:23:04 -07004343 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004344 if (!phys_obj)
4345 return -ENOMEM;
4346
4347 phys_obj->id = id;
4348
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004349 phys_obj->handle = drm_pci_alloc(dev, size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004350 if (!phys_obj->handle) {
4351 ret = -ENOMEM;
4352 goto kfree_obj;
4353 }
4354#ifdef CONFIG_X86
4355 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4356#endif
4357
4358 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4359
4360 return 0;
4361kfree_obj:
Eric Anholt9a298b22009-03-24 12:23:04 -07004362 kfree(phys_obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004363 return ret;
4364}
4365
Chris Wilson995b6762010-08-20 13:23:26 +01004366static void i915_gem_free_phys_object(struct drm_device *dev, int id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004367{
4368 drm_i915_private_t *dev_priv = dev->dev_private;
4369 struct drm_i915_gem_phys_object *phys_obj;
4370
4371 if (!dev_priv->mm.phys_objs[id - 1])
4372 return;
4373
4374 phys_obj = dev_priv->mm.phys_objs[id - 1];
4375 if (phys_obj->cur_obj) {
4376 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4377 }
4378
4379#ifdef CONFIG_X86
4380 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4381#endif
4382 drm_pci_free(dev, phys_obj->handle);
4383 kfree(phys_obj);
4384 dev_priv->mm.phys_objs[id - 1] = NULL;
4385}
4386
4387void i915_gem_free_all_phys_object(struct drm_device *dev)
4388{
4389 int i;
4390
Dave Airlie260883c2009-01-22 17:58:49 +10004391 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004392 i915_gem_free_phys_object(dev, i);
4393}
4394
4395void i915_gem_detach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004396 struct drm_i915_gem_object *obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004397{
Al Viro496ad9a2013-01-23 17:07:38 -05004398 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
Chris Wilsone5281cc2010-10-28 13:45:36 +01004399 char *vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004400 int i;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004401 int page_count;
4402
Chris Wilson05394f32010-11-08 19:18:58 +00004403 if (!obj->phys_obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004404 return;
Chris Wilson05394f32010-11-08 19:18:58 +00004405 vaddr = obj->phys_obj->handle->vaddr;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004406
Chris Wilson05394f32010-11-08 19:18:58 +00004407 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004408 for (i = 0; i < page_count; i++) {
Hugh Dickins5949eac2011-06-27 16:18:18 -07004409 struct page *page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004410 if (!IS_ERR(page)) {
4411 char *dst = kmap_atomic(page);
4412 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4413 kunmap_atomic(dst);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004414
Chris Wilsone5281cc2010-10-28 13:45:36 +01004415 drm_clflush_pages(&page, 1);
4416
4417 set_page_dirty(page);
4418 mark_page_accessed(page);
4419 page_cache_release(page);
4420 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004421 }
Ben Widawskye76e9ae2012-11-04 09:21:27 -08004422 i915_gem_chipset_flush(dev);
Chris Wilsond78b47b2009-06-17 21:52:49 +01004423
Chris Wilson05394f32010-11-08 19:18:58 +00004424 obj->phys_obj->cur_obj = NULL;
4425 obj->phys_obj = NULL;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004426}
4427
4428int
4429i915_gem_attach_phys_object(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00004430 struct drm_i915_gem_object *obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01004431 int id,
4432 int align)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004433{
Al Viro496ad9a2013-01-23 17:07:38 -05004434 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004435 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004436 int ret = 0;
4437 int page_count;
4438 int i;
4439
4440 if (id > I915_MAX_PHYS_OBJECT)
4441 return -EINVAL;
4442
Chris Wilson05394f32010-11-08 19:18:58 +00004443 if (obj->phys_obj) {
4444 if (obj->phys_obj->id == id)
Dave Airlie71acb5e2008-12-30 20:31:46 +10004445 return 0;
4446 i915_gem_detach_phys_object(dev, obj);
4447 }
4448
Dave Airlie71acb5e2008-12-30 20:31:46 +10004449 /* create a new object */
4450 if (!dev_priv->mm.phys_objs[id - 1]) {
4451 ret = i915_gem_init_phys_object(dev, id,
Chris Wilson05394f32010-11-08 19:18:58 +00004452 obj->base.size, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004453 if (ret) {
Chris Wilson05394f32010-11-08 19:18:58 +00004454 DRM_ERROR("failed to init phys object %d size: %zu\n",
4455 id, obj->base.size);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004456 return ret;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004457 }
4458 }
4459
4460 /* bind to the object */
Chris Wilson05394f32010-11-08 19:18:58 +00004461 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4462 obj->phys_obj->cur_obj = obj;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004463
Chris Wilson05394f32010-11-08 19:18:58 +00004464 page_count = obj->base.size / PAGE_SIZE;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004465
4466 for (i = 0; i < page_count; i++) {
Chris Wilsone5281cc2010-10-28 13:45:36 +01004467 struct page *page;
4468 char *dst, *src;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004469
Hugh Dickins5949eac2011-06-27 16:18:18 -07004470 page = shmem_read_mapping_page(mapping, i);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004471 if (IS_ERR(page))
4472 return PTR_ERR(page);
4473
Chris Wilsonff75b9b2010-10-30 22:52:31 +01004474 src = kmap_atomic(page);
Chris Wilson05394f32010-11-08 19:18:58 +00004475 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004476 memcpy(dst, src, PAGE_SIZE);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -07004477 kunmap_atomic(src);
Chris Wilsone5281cc2010-10-28 13:45:36 +01004478
4479 mark_page_accessed(page);
4480 page_cache_release(page);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004481 }
4482
4483 return 0;
Dave Airlie71acb5e2008-12-30 20:31:46 +10004484}
4485
4486static int
Chris Wilson05394f32010-11-08 19:18:58 +00004487i915_gem_phys_pwrite(struct drm_device *dev,
4488 struct drm_i915_gem_object *obj,
Dave Airlie71acb5e2008-12-30 20:31:46 +10004489 struct drm_i915_gem_pwrite *args,
4490 struct drm_file *file_priv)
4491{
Chris Wilson05394f32010-11-08 19:18:58 +00004492 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
Ville Syrjälä2bb46292013-02-22 16:12:51 +02004493 char __user *user_data = to_user_ptr(args->data_ptr);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004494
Chris Wilsonb47b30c2010-11-08 01:12:29 +00004495 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4496 unsigned long unwritten;
4497
4498 /* The physical object once assigned is fixed for the lifetime
4499 * of the obj, so we can safely drop the lock and continue
4500 * to access vaddr.
4501 */
4502 mutex_unlock(&dev->struct_mutex);
4503 unwritten = copy_from_user(vaddr, user_data, args->size);
4504 mutex_lock(&dev->struct_mutex);
4505 if (unwritten)
4506 return -EFAULT;
4507 }
Dave Airlie71acb5e2008-12-30 20:31:46 +10004508
Ben Widawskye76e9ae2012-11-04 09:21:27 -08004509 i915_gem_chipset_flush(dev);
Dave Airlie71acb5e2008-12-30 20:31:46 +10004510 return 0;
4511}
Eric Anholtb9624422009-06-03 07:27:35 +00004512
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004513void i915_gem_release(struct drm_device *dev, struct drm_file *file)
Eric Anholtb9624422009-06-03 07:27:35 +00004514{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004515 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholtb9624422009-06-03 07:27:35 +00004516
4517 /* Clean up our request list when the client is going away, so that
4518 * later retire_requests won't dereference our soon-to-be-gone
4519 * file_priv.
4520 */
Chris Wilson1c255952010-09-26 11:03:27 +01004521 spin_lock(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01004522 while (!list_empty(&file_priv->mm.request_list)) {
4523 struct drm_i915_gem_request *request;
4524
4525 request = list_first_entry(&file_priv->mm.request_list,
4526 struct drm_i915_gem_request,
4527 client_list);
4528 list_del(&request->client_list);
4529 request->file_priv = NULL;
4530 }
Chris Wilson1c255952010-09-26 11:03:27 +01004531 spin_unlock(&file_priv->mm.lock);
Eric Anholtb9624422009-06-03 07:27:35 +00004532}
Chris Wilson31169712009-09-14 16:50:28 +01004533
Chris Wilson57745062012-11-21 13:04:04 +00004534static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
4535{
4536 if (!mutex_is_locked(mutex))
4537 return false;
4538
4539#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
4540 return mutex->owner == task;
4541#else
4542 /* Since UP may be pre-empted, we cannot assume that we own the lock */
4543 return false;
4544#endif
4545}
4546
Chris Wilson31169712009-09-14 16:50:28 +01004547static int
Ying Han1495f232011-05-24 17:12:27 -07004548i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
Chris Wilson31169712009-09-14 16:50:28 +01004549{
Chris Wilson17250b72010-10-28 12:51:39 +01004550 struct drm_i915_private *dev_priv =
4551 container_of(shrinker,
4552 struct drm_i915_private,
4553 mm.inactive_shrinker);
4554 struct drm_device *dev = dev_priv->dev;
Chris Wilson6c085a72012-08-20 11:40:46 +02004555 struct drm_i915_gem_object *obj;
Ying Han1495f232011-05-24 17:12:27 -07004556 int nr_to_scan = sc->nr_to_scan;
Chris Wilson57745062012-11-21 13:04:04 +00004557 bool unlock = true;
Chris Wilson17250b72010-10-28 12:51:39 +01004558 int cnt;
4559
Chris Wilson57745062012-11-21 13:04:04 +00004560 if (!mutex_trylock(&dev->struct_mutex)) {
4561 if (!mutex_is_locked_by(&dev->struct_mutex, current))
4562 return 0;
4563
Daniel Vetter677feac2012-12-19 14:33:45 +01004564 if (dev_priv->mm.shrinker_no_lock_stealing)
4565 return 0;
4566
Chris Wilson57745062012-11-21 13:04:04 +00004567 unlock = false;
4568 }
Chris Wilson31169712009-09-14 16:50:28 +01004569
Chris Wilson6c085a72012-08-20 11:40:46 +02004570 if (nr_to_scan) {
4571 nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
4572 if (nr_to_scan > 0)
Daniel Vetter93927ca2013-01-10 18:03:00 +01004573 nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
4574 false);
4575 if (nr_to_scan > 0)
Chris Wilson6c085a72012-08-20 11:40:46 +02004576 i915_gem_shrink_all(dev_priv);
Chris Wilson31169712009-09-14 16:50:28 +01004577 }
4578
Chris Wilson17250b72010-10-28 12:51:39 +01004579 cnt = 0;
Ben Widawsky35c20a62013-05-31 11:28:48 -07004580 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
Chris Wilsona5570172012-09-04 21:02:54 +01004581 if (obj->pages_pin_count == 0)
4582 cnt += obj->base.size >> PAGE_SHIFT;
Xiong Zhang06755602013-07-05 18:53:29 +08004583 list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list)
Chris Wilsona5570172012-09-04 21:02:54 +01004584 if (obj->pin_count == 0 && obj->pages_pin_count == 0)
Chris Wilson6c085a72012-08-20 11:40:46 +02004585 cnt += obj->base.size >> PAGE_SHIFT;
Chris Wilson31169712009-09-14 16:50:28 +01004586
Chris Wilson57745062012-11-21 13:04:04 +00004587 if (unlock)
4588 mutex_unlock(&dev->struct_mutex);
Chris Wilson6c085a72012-08-20 11:40:46 +02004589 return cnt;
Chris Wilson31169712009-09-14 16:50:28 +01004590}